]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gcc-interface/decl.c
[Ada] Variable-sized node types
[thirdparty/gcc.git] / gcc / ada / gcc-interface / decl.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2021, 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 "tree.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
37 #include "demangle.h"
38
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "uintp.h"
48 #include "urealp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
54
55 /* The "stdcall" convention is really supported on 32-bit x86/Windows only.
56 The following macro is a helper to avoid having to check for a Windows
57 specific attribute throughout this unit. */
58
59 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #ifdef TARGET_64BIT
61 #define Has_Stdcall_Convention(E) \
62 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
63 #else
64 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #endif
66 #else
67 #define Has_Stdcall_Convention(E) 0
68 #endif
69
70 #define STDCALL_PREFIX "_imp__"
71
72 /* Stack realignment is necessary for functions with foreign conventions when
73 the ABI doesn't mandate as much as what the compiler assumes - that is, up
74 to PREFERRED_STACK_BOUNDARY.
75
76 Such realignment can be requested with a dedicated function type attribute
77 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
78 characterize the situations where the attribute should be set. We rely on
79 compiler configuration settings for 'main' to decide. */
80
81 #ifdef MAIN_STACK_BOUNDARY
82 #define FOREIGN_FORCE_REALIGN_STACK \
83 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
84 #else
85 #define FOREIGN_FORCE_REALIGN_STACK 0
86 #endif
87
88 /* The largest TYPE_ARRAY_MAX_SIZE value we set on an array type.
89 It's an artibrary limit (256 MB) above which we consider that
90 the allocation is essentially unbounded. */
91
92 #define TYPE_ARRAY_SIZE_LIMIT (1 << 28)
93
94 struct incomplete
95 {
96 struct incomplete *next;
97 tree old_type;
98 Entity_Id full_type;
99 };
100
101 /* These variables are used to defer recursively expanding incomplete types
102 while we are processing a record, an array or a subprogram type. */
103 static int defer_incomplete_level = 0;
104 static struct incomplete *defer_incomplete_list;
105
106 /* This variable is used to delay expanding types coming from a limited with
107 clause and completed Taft Amendment types until the end of the spec. */
108 static struct incomplete *defer_limited_with_list;
109
110 typedef struct subst_pair_d {
111 tree discriminant;
112 tree replacement;
113 } subst_pair;
114
115
116 typedef struct variant_desc_d {
117 /* The type of the variant. */
118 tree type;
119
120 /* The associated field. */
121 tree field;
122
123 /* The value of the qualifier. */
124 tree qual;
125
126 /* The type of the variant after transformation. */
127 tree new_type;
128
129 /* The auxiliary data. */
130 tree aux;
131 } variant_desc;
132
133
134 /* A map used to cache the result of annotate_value. */
135 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
136 {
137 static inline hashval_t
138 hash (tree_int_map *m)
139 {
140 return htab_hash_pointer (m->base.from);
141 }
142
143 static inline bool
144 equal (tree_int_map *a, tree_int_map *b)
145 {
146 return a->base.from == b->base.from;
147 }
148
149 static int
150 keep_cache_entry (tree_int_map *&m)
151 {
152 return ggc_marked_p (m->base.from);
153 }
154 };
155
156 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
157
158 /* A map used to associate a dummy type with a list of subprogram entities. */
159 struct GTY((for_user)) tree_entity_vec_map
160 {
161 struct tree_map_base base;
162 vec<Entity_Id, va_gc_atomic> *to;
163 };
164
165 void
166 gt_pch_nx (Entity_Id &)
167 {
168 }
169
170 void
171 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
172 {
173 op (x, cookie);
174 }
175
176 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
177 {
178 static inline hashval_t
179 hash (tree_entity_vec_map *m)
180 {
181 return htab_hash_pointer (m->base.from);
182 }
183
184 static inline bool
185 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
186 {
187 return a->base.from == b->base.from;
188 }
189
190 static int
191 keep_cache_entry (tree_entity_vec_map *&m)
192 {
193 return ggc_marked_p (m->base.from);
194 }
195 };
196
197 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
198
199 static void prepend_one_attribute (struct attrib **,
200 enum attrib_type, tree, tree, Node_Id);
201 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
202 static void prepend_attributes (struct attrib **, Entity_Id);
203 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
204 bool);
205 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
206 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
207 unsigned int);
208 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
209 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
210 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
211 static int adjust_packed (tree, tree, int);
212 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
213 static enum inline_status_t inline_status_for_subprog (Entity_Id);
214 static tree gnu_ext_name_for_subprog (Entity_Id, tree);
215 static void set_nonaliased_component_on_array_type (tree);
216 static void set_reverse_storage_order_on_array_type (tree);
217 static bool same_discriminant_p (Entity_Id, Entity_Id);
218 static bool array_type_has_nonaliased_component (tree, Entity_Id);
219 static bool compile_time_known_address_p (Node_Id);
220 static bool cannot_be_superflat (Node_Id);
221 static bool constructor_address_p (tree);
222 static bool allocatable_size_p (tree, bool);
223 static bool initial_value_needs_conversion (tree, tree);
224 static tree update_n_elem (tree, tree, tree);
225 static int compare_field_bitpos (const PTR, const PTR);
226 static bool components_to_record (Node_Id, Entity_Id, tree, tree, int, bool,
227 bool, bool, bool, bool, bool, bool, tree,
228 tree *);
229 static Uint annotate_value (tree);
230 static void annotate_rep (Entity_Id, tree);
231 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
232 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
233 static vec<variant_desc> build_variant_list (tree, Node_Id, vec<subst_pair>,
234 vec<variant_desc>);
235 static tree maybe_saturate_size (tree, unsigned int align);
236 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool,
237 const char *, const char *);
238 static void set_rm_size (Uint, tree, Entity_Id);
239 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
240 static unsigned int promote_object_alignment (tree, Entity_Id);
241 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
242 static tree create_field_decl_from (tree, tree, tree, tree, tree,
243 vec<subst_pair>);
244 static tree create_rep_part (tree, tree, tree);
245 static tree get_rep_part (tree);
246 static tree create_variant_part_from (tree, vec<variant_desc>, tree,
247 tree, vec<subst_pair>, bool);
248 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair>);
249 static void copy_and_substitute_in_layout (Entity_Id, Entity_Id, tree, tree,
250 vec<subst_pair>, bool);
251 static tree associate_original_type_to_packed_array (tree, Entity_Id);
252 static const char *get_entity_char (Entity_Id);
253
254 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
255 to pass around calls performing profile compatibility checks. */
256
257 typedef struct {
258 Entity_Id gnat_entity; /* The Ada subprogram entity. */
259 tree ada_fntype; /* The corresponding GCC type node. */
260 tree btin_fntype; /* The GCC builtin function type node. */
261 } intrin_binding_t;
262
263 static bool intrin_profiles_compatible_p (intrin_binding_t *);
264
265 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
266 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
267 and associate the ..._DECL node with the input GNAT defining identifier.
268
269 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
270 initial value (in GCC tree form). This is optional for a variable. For
271 a renamed entity, GNU_EXPR gives the object being renamed.
272
273 DEFINITION is true if this call is intended for a definition. This is used
274 for separate compilation where it is necessary to know whether an external
275 declaration or a definition must be created if the GCC equivalent was not
276 created previously. */
277
278 tree
279 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
280 {
281 /* The construct that declared the entity. */
282 const Node_Id gnat_decl = Declaration_Node (gnat_entity);
283 /* The object that the entity renames, if any. */
284 const Entity_Id gnat_renamed_obj = Renamed_Object (gnat_entity);
285 /* The kind of the entity. */
286 const Entity_Kind kind = Ekind (gnat_entity);
287 /* True if this is a type. */
288 const bool is_type = IN (kind, Type_Kind);
289 /* True if this is an artificial entity. */
290 const bool artificial_p = !Comes_From_Source (gnat_entity);
291 /* True if debug info is requested for this entity. */
292 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
293 /* True if this entity is to be considered as imported. */
294 const bool imported_p
295 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
296 /* True if this entity has a foreign convention. */
297 const bool foreign = Has_Foreign_Convention (gnat_entity);
298 /* For a type, contains the equivalent GNAT node to be used in gigi. */
299 Entity_Id gnat_equiv_type = Empty;
300 /* For a type, contains the GNAT node to be used for back-annotation. */
301 Entity_Id gnat_annotate_type = Empty;
302 /* Temporary used to walk the GNAT tree. */
303 Entity_Id gnat_temp;
304 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
305 This node will be associated with the GNAT node by calling at the end
306 of the `switch' statement. */
307 tree gnu_decl = NULL_TREE;
308 /* Contains the GCC type to be used for the GCC node. */
309 tree gnu_type = NULL_TREE;
310 /* Contains the GCC size tree to be used for the GCC node. */
311 tree gnu_size = NULL_TREE;
312 /* Contains the GCC name to be used for the GCC node. */
313 tree gnu_entity_name;
314 /* True if we have already saved gnu_decl as a GNAT association. This can
315 also be used to purposely avoid making such an association but this use
316 case ought not to be applied to types because it can break the deferral
317 mechanism implemented for access types. */
318 bool saved = false;
319 /* True if we incremented defer_incomplete_level. */
320 bool this_deferred = false;
321 /* True if we incremented force_global. */
322 bool this_global = false;
323 /* True if we should check to see if elaborated during processing. */
324 bool maybe_present = false;
325 /* True if we made GNU_DECL and its type here. */
326 bool this_made_decl = false;
327 /* Size and alignment of the GCC node, if meaningful. */
328 unsigned int esize = 0, align = 0;
329 /* Contains the list of attributes directly attached to the entity. */
330 struct attrib *attr_list = NULL;
331
332 /* Since a use of an itype is a definition, process it as such if it is in
333 the main unit, except for E_Access_Subtype because it's actually a use
334 of its base type, see below. */
335 if (!definition
336 && is_type
337 && Is_Itype (gnat_entity)
338 && Ekind (gnat_entity) != E_Access_Subtype
339 && !present_gnu_tree (gnat_entity)
340 && In_Extended_Main_Code_Unit (gnat_entity))
341 {
342 /* Ensure that we are in a subprogram mentioned in the Scope chain of
343 this entity, our current scope is global, or we encountered a task
344 or entry (where we can't currently accurately check scoping). */
345 if (!current_function_decl
346 || DECL_ELABORATION_PROC_P (current_function_decl))
347 {
348 process_type (gnat_entity);
349 return get_gnu_tree (gnat_entity);
350 }
351
352 for (gnat_temp = Scope (gnat_entity);
353 Present (gnat_temp);
354 gnat_temp = Scope (gnat_temp))
355 {
356 if (Is_Type (gnat_temp))
357 gnat_temp = Underlying_Type (gnat_temp);
358
359 if (Ekind (gnat_temp) == E_Subprogram_Body)
360 gnat_temp
361 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
362
363 if (Is_Subprogram (gnat_temp)
364 && Present (Protected_Body_Subprogram (gnat_temp)))
365 gnat_temp = Protected_Body_Subprogram (gnat_temp);
366
367 if (Ekind (gnat_temp) == E_Entry
368 || Ekind (gnat_temp) == E_Entry_Family
369 || Ekind (gnat_temp) == E_Task_Type
370 || (Is_Subprogram (gnat_temp)
371 && present_gnu_tree (gnat_temp)
372 && (current_function_decl
373 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
374 {
375 process_type (gnat_entity);
376 return get_gnu_tree (gnat_entity);
377 }
378 }
379
380 /* This abort means the itype has an incorrect scope, i.e. that its
381 scope does not correspond to the subprogram it is first used in. */
382 gcc_unreachable ();
383 }
384
385 /* If we've already processed this entity, return what we got last time.
386 If we are defining the node, we should not have already processed it.
387 In that case, we will abort below when we try to save a new GCC tree
388 for this object. We also need to handle the case of getting a dummy
389 type when a Full_View exists but be careful so as not to trigger its
390 premature elaboration. Likewise for a cloned subtype without its own
391 freeze node, which typically happens when a generic gets instantiated
392 on an incomplete or private type. */
393 if ((!definition || (is_type && imported_p))
394 && present_gnu_tree (gnat_entity))
395 {
396 gnu_decl = get_gnu_tree (gnat_entity);
397
398 if (TREE_CODE (gnu_decl) == TYPE_DECL
399 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
400 && IN (kind, Incomplete_Or_Private_Kind)
401 && Present (Full_View (gnat_entity))
402 && (present_gnu_tree (Full_View (gnat_entity))
403 || No (Freeze_Node (Full_View (gnat_entity)))))
404 {
405 gnu_decl
406 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE,
407 false);
408 save_gnu_tree (gnat_entity, NULL_TREE, false);
409 save_gnu_tree (gnat_entity, gnu_decl, false);
410 }
411
412 if (TREE_CODE (gnu_decl) == TYPE_DECL
413 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
414 && Ekind (gnat_entity) == E_Record_Subtype
415 && No (Freeze_Node (gnat_entity))
416 && Present (Cloned_Subtype (gnat_entity))
417 && (present_gnu_tree (Cloned_Subtype (gnat_entity))
418 || No (Freeze_Node (Cloned_Subtype (gnat_entity)))))
419 {
420 gnu_decl
421 = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity), NULL_TREE,
422 false);
423 save_gnu_tree (gnat_entity, NULL_TREE, false);
424 save_gnu_tree (gnat_entity, gnu_decl, false);
425 }
426
427 return gnu_decl;
428 }
429
430 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
431 must be specified unless it was specified by the programmer. Exceptions
432 are for access-to-protected-subprogram types and all access subtypes, as
433 another GNAT type is used to lay out the GCC type for them. */
434 gcc_assert (!is_type
435 || Known_Esize (gnat_entity)
436 || Has_Size_Clause (gnat_entity)
437 || (!Is_In_Numeric_Kind (kind)
438 && !IN (kind, Enumeration_Kind)
439 && (!IN (kind, Access_Kind)
440 || kind == E_Access_Protected_Subprogram_Type
441 || kind == E_Anonymous_Access_Protected_Subprogram_Type
442 || kind == E_Access_Subtype
443 || type_annotate_only)));
444
445 /* The RM size must be specified for all discrete and fixed-point types. */
446 gcc_assert (!(Is_In_Discrete_Or_Fixed_Point_Kind (kind)
447 && Unknown_RM_Size (gnat_entity)));
448
449 /* If we get here, it means we have not yet done anything with this entity.
450 If we are not defining it, it must be a type or an entity that is defined
451 elsewhere or externally, otherwise we should have defined it already.
452
453 In other words, the failure of this assertion typically arises when a
454 reference to an entity (type or object) is made before its declaration,
455 either directly or by means of a freeze node which is incorrectly placed.
456 This can also happen for an entity referenced out of context, for example
457 a parameter outside of the subprogram where it is declared. GNAT_ENTITY
458 is the N_Defining_Identifier of the entity, the problematic N_Identifier
459 being the argument passed to Identifier_to_gnu in the parent frame.
460
461 One exception is for an entity, typically an inherited operation, which is
462 a local alias for the parent's operation. It is neither defined, since it
463 is an inherited operation, nor public, since it is declared in the current
464 compilation unit, so we test Is_Public on the Alias entity instead. */
465 gcc_assert (definition
466 || is_type
467 || kind == E_Discriminant
468 || kind == E_Component
469 || kind == E_Label
470 || (kind == E_Constant && Present (Full_View (gnat_entity)))
471 || Is_Public (gnat_entity)
472 || (Present (Alias (gnat_entity))
473 && Is_Public (Alias (gnat_entity)))
474 || type_annotate_only);
475
476 /* Get the name of the entity and set up the line number and filename of
477 the original definition for use in any decl we make. Make sure we do
478 not inherit another source location. */
479 gnu_entity_name = get_entity_name (gnat_entity);
480 if (!renaming_from_instantiation_p (gnat_entity))
481 Sloc_to_locus (Sloc (gnat_entity), &input_location);
482
483 /* For cases when we are not defining (i.e., we are referencing from
484 another compilation unit) public entities, show we are at global level
485 for the purpose of computing scopes. Don't do this for components or
486 discriminants since the relevant test is whether or not the record is
487 being defined. */
488 if (!definition
489 && kind != E_Component
490 && kind != E_Discriminant
491 && Is_Public (gnat_entity)
492 && !Is_Statically_Allocated (gnat_entity))
493 force_global++, this_global = true;
494
495 /* Handle any attributes directly attached to the entity. */
496 if (Has_Gigi_Rep_Item (gnat_entity))
497 prepend_attributes (&attr_list, gnat_entity);
498
499 /* Do some common processing for types. */
500 if (is_type)
501 {
502 /* Compute the equivalent type to be used in gigi. */
503 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
504
505 /* Machine_Attributes on types are expected to be propagated to
506 subtypes. The corresponding Gigi_Rep_Items are only attached
507 to the first subtype though, so we handle the propagation here. */
508 if (Base_Type (gnat_entity) != gnat_entity
509 && !Is_First_Subtype (gnat_entity)
510 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
511 prepend_attributes (&attr_list,
512 First_Subtype (Base_Type (gnat_entity)));
513
514 /* Compute a default value for the size of an elementary type. */
515 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
516 {
517 unsigned int max_esize;
518
519 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
520 esize = UI_To_Int (Esize (gnat_entity));
521
522 if (IN (kind, Float_Kind))
523 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
524 else if (IN (kind, Access_Kind))
525 max_esize = POINTER_SIZE * 2;
526 else
527 max_esize = Enable_128bit_Types ? 128 : LONG_LONG_TYPE_SIZE;
528
529 if (esize > max_esize)
530 esize = max_esize;
531 }
532 }
533
534 switch (kind)
535 {
536 case E_Component:
537 case E_Discriminant:
538 {
539 /* The GNAT record where the component was defined. */
540 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
541
542 /* If the entity is a discriminant of an extended tagged type used to
543 rename a discriminant of the parent type, return the latter. */
544 if (kind == E_Discriminant
545 && Present (Corresponding_Discriminant (gnat_entity))
546 && Is_Tagged_Type (gnat_record))
547 {
548 gnu_decl
549 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
550 gnu_expr, definition);
551 saved = true;
552 break;
553 }
554
555 /* If the entity is an inherited component (in the case of extended
556 tagged record types), just return the original entity, which must
557 be a FIELD_DECL. Likewise for discriminants. If the entity is a
558 non-girder discriminant (in the case of derived untagged record
559 types), return the stored discriminant it renames. */
560 if (Present (Original_Record_Component (gnat_entity))
561 && Original_Record_Component (gnat_entity) != gnat_entity)
562 {
563 gnu_decl
564 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
565 gnu_expr, definition);
566 /* GNU_DECL contains a PLACEHOLDER_EXPR for discriminants. */
567 if (kind == E_Discriminant)
568 saved = true;
569 break;
570 }
571
572 /* Otherwise, if we are not defining this and we have no GCC type
573 for the containing record, make one for it. Then we should
574 have made our own equivalent. */
575 if (!definition && !present_gnu_tree (gnat_record))
576 {
577 /* ??? If this is in a record whose scope is a protected
578 type and we have an Original_Record_Component, use it.
579 This is a workaround for major problems in protected type
580 handling. */
581 Entity_Id Scop = Scope (Scope (gnat_entity));
582 if (Is_Protected_Type (Underlying_Type (Scop))
583 && Present (Original_Record_Component (gnat_entity)))
584 {
585 gnu_decl
586 = gnat_to_gnu_entity (Original_Record_Component
587 (gnat_entity),
588 gnu_expr, false);
589 }
590 else
591 {
592 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
593 gnu_decl = get_gnu_tree (gnat_entity);
594 }
595
596 saved = true;
597 break;
598 }
599
600 /* Here we have no GCC type and this is a reference rather than a
601 definition. This should never happen. Most likely the cause is
602 reference before declaration in the GNAT tree for gnat_entity. */
603 gcc_unreachable ();
604 }
605
606 case E_Named_Integer:
607 case E_Named_Real:
608 {
609 tree gnu_ext_name = NULL_TREE;
610
611 if (Is_Public (gnat_entity))
612 gnu_ext_name = create_concat_name (gnat_entity, NULL);
613
614 /* All references are supposed to be folded in the front-end. */
615 gcc_assert (definition && gnu_expr);
616
617 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
618 gnu_expr = convert (gnu_type, gnu_expr);
619
620 /* Build a CONST_DECL for debugging purposes exclusively. */
621 gnu_decl
622 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
623 gnu_expr, true, Is_Public (gnat_entity),
624 false, false, false, artificial_p,
625 debug_info_p, NULL, gnat_entity, true);
626 }
627 break;
628
629 case E_Constant:
630 /* Ignore constant definitions already marked with the error node. See
631 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
632 if (definition
633 && present_gnu_tree (gnat_entity)
634 && get_gnu_tree (gnat_entity) == error_mark_node)
635 {
636 maybe_present = true;
637 break;
638 }
639
640 /* Ignore deferred constant definitions without address clause since
641 they are processed fully in the front-end. If No_Initialization
642 is set, this is not a deferred constant but a constant whose value
643 is built manually. And constants that are renamings are handled
644 like variables. */
645 if (definition
646 && !gnu_expr
647 && No (Address_Clause (gnat_entity))
648 && !No_Initialization (gnat_decl)
649 && No (gnat_renamed_obj))
650 {
651 gnu_decl = error_mark_node;
652 saved = true;
653 break;
654 }
655
656 /* If this is a use of a deferred constant without address clause,
657 get its full definition. */
658 if (!definition
659 && No (Address_Clause (gnat_entity))
660 && Present (Full_View (gnat_entity)))
661 {
662 gnu_decl
663 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
664 saved = true;
665 break;
666 }
667
668 /* If we have a constant that we are not defining, get the expression it
669 was defined to represent. This is necessary to avoid generating dumb
670 elaboration code in simple cases, and we may throw it away later if it
671 is not a constant. But do not do it for dispatch tables because they
672 are only referenced indirectly and we need to have a consistent view
673 of the exported and of the imported declarations of the tables from
674 external units for them to be properly merged in LTO mode. Moreover
675 simply do not retrieve the expression if it is an allocator because
676 the designated type might still be dummy at this point. Note that we
677 invoke gnat_to_gnu_external and not gnat_to_gnu because the expression
678 may contain N_Expression_With_Actions nodes and thus declarations of
679 objects from other units that we need to discard. Note also that we
680 need to do it even if we are only annotating types, so as to be able
681 to validate representation clauses using constants. */
682 if (!definition
683 && !No_Initialization (gnat_decl)
684 && !Is_Dispatch_Table_Entity (gnat_entity)
685 && Present (gnat_temp = Expression (gnat_decl))
686 && Nkind (gnat_temp) != N_Allocator
687 && (Is_Elementary_Type (Etype (gnat_entity)) || !type_annotate_only))
688 gnu_expr = gnat_to_gnu_external (gnat_temp);
689
690 /* ... fall through ... */
691
692 case E_Exception:
693 case E_Loop_Parameter:
694 case E_Out_Parameter:
695 case E_Variable:
696 {
697 const Entity_Id gnat_type = Etype (gnat_entity);
698 /* Always create a variable for volatile objects and variables seen
699 constant but with a Linker_Section pragma. */
700 bool const_flag
701 = ((kind == E_Constant || kind == E_Variable)
702 && Is_True_Constant (gnat_entity)
703 && !(kind == E_Variable
704 && Present (Linker_Section_Pragma (gnat_entity)))
705 && !Treat_As_Volatile (gnat_entity)
706 && (((Nkind (gnat_decl) == N_Object_Declaration)
707 && Present (Expression (gnat_decl)))
708 || Present (gnat_renamed_obj)
709 || imported_p));
710 bool inner_const_flag = const_flag;
711 bool static_flag = Is_Statically_Allocated (gnat_entity);
712 /* We implement RM 13.3(19) for exported and imported (non-constant)
713 objects by making them volatile. */
714 bool volatile_flag
715 = (Treat_As_Volatile (gnat_entity)
716 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
717 bool mutable_p = false;
718 bool used_by_ref = false;
719 tree gnu_ext_name = NULL_TREE;
720 tree gnu_ada_size = NULL_TREE;
721
722 /* We need to translate the renamed object even though we are only
723 referencing the renaming. But it may contain a call for which
724 we'll generate a temporary to hold the return value and which
725 is part of the definition of the renaming, so discard it. */
726 if (Present (gnat_renamed_obj) && !definition)
727 {
728 if (kind == E_Exception)
729 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
730 NULL_TREE, false);
731 else
732 gnu_expr = gnat_to_gnu_external (gnat_renamed_obj);
733 }
734
735 /* Get the type after elaborating the renamed object. */
736 if (foreign && Is_Descendant_Of_Address (Underlying_Type (gnat_type)))
737 gnu_type = ptr_type_node;
738 else
739 {
740 gnu_type = gnat_to_gnu_type (gnat_type);
741
742 /* If this is a standard exception definition, use the standard
743 exception type. This is necessary to make sure that imported
744 and exported views of exceptions are merged in LTO mode. */
745 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
746 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
747 gnu_type = except_type_node;
748 }
749
750 /* For a debug renaming declaration, build a debug-only entity. */
751 if (Present (Debug_Renaming_Link (gnat_entity)))
752 {
753 /* Force a non-null value to make sure the symbol is retained. */
754 tree value = build1 (INDIRECT_REF, gnu_type,
755 build1 (NOP_EXPR,
756 build_pointer_type (gnu_type),
757 integer_minus_one_node));
758 gnu_decl = build_decl (input_location,
759 VAR_DECL, gnu_entity_name, gnu_type);
760 SET_DECL_VALUE_EXPR (gnu_decl, value);
761 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
762 TREE_STATIC (gnu_decl) = global_bindings_p ();
763 gnat_pushdecl (gnu_decl, gnat_entity);
764 break;
765 }
766
767 /* If this is a loop variable, its type should be the base type.
768 This is because the code for processing a loop determines whether
769 a normal loop end test can be done by comparing the bounds of the
770 loop against those of the base type, which is presumed to be the
771 size used for computation. But this is not correct when the size
772 of the subtype is smaller than the type. */
773 if (kind == E_Loop_Parameter)
774 gnu_type = get_base_type (gnu_type);
775
776 /* Reject non-renamed objects whose type is an unconstrained array or
777 any object whose type is a dummy type or void. */
778 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
779 && No (gnat_renamed_obj))
780 || TYPE_IS_DUMMY_P (gnu_type)
781 || TREE_CODE (gnu_type) == VOID_TYPE)
782 {
783 gcc_assert (type_annotate_only);
784 if (this_global)
785 force_global--;
786 return error_mark_node;
787 }
788
789 /* If an alignment is specified, use it if valid. Note that exceptions
790 are objects but don't have an alignment and there is also no point in
791 setting it for an address clause, since the final type of the object
792 will be a reference type. */
793 if (Known_Alignment (gnat_entity)
794 && kind != E_Exception
795 && No (Address_Clause (gnat_entity)))
796 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
797 TYPE_ALIGN (gnu_type));
798
799 /* Likewise, if a size is specified, use it if valid. */
800 if (Known_Esize (gnat_entity))
801 gnu_size
802 = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
803 VAR_DECL, false, Has_Size_Clause (gnat_entity),
804 NULL, NULL);
805 if (gnu_size)
806 {
807 gnu_type
808 = make_type_from_size (gnu_type, gnu_size,
809 Has_Biased_Representation (gnat_entity));
810
811 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
812 gnu_size = NULL_TREE;
813 }
814
815 /* If this object has self-referential size, it must be a record with
816 a default discriminant. We are supposed to allocate an object of
817 the maximum size in this case, unless it is a constant with an
818 initializing expression, in which case we can get the size from
819 that. Note that the resulting size may still be a variable, so
820 this may end up with an indirect allocation. */
821 if (No (gnat_renamed_obj)
822 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
823 {
824 if (gnu_expr && kind == E_Constant)
825 {
826 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
827 gnu_ada_size = TYPE_ADA_SIZE (TREE_TYPE (gnu_expr));
828 if (CONTAINS_PLACEHOLDER_P (gnu_size))
829 {
830 /* If the initializing expression is itself a constant,
831 despite having a nominal type with self-referential
832 size, we can get the size directly from it. */
833 if (TREE_CODE (gnu_expr) == COMPONENT_REF
834 && TYPE_IS_PADDING_P
835 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
836 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
837 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
838 || DECL_READONLY_ONCE_ELAB
839 (TREE_OPERAND (gnu_expr, 0))))
840 {
841 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
842 gnu_ada_size = gnu_size;
843 }
844 else
845 {
846 gnu_size
847 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size,
848 gnu_expr);
849 gnu_ada_size
850 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_ada_size,
851 gnu_expr);
852 }
853 }
854 }
855 /* We may have no GNU_EXPR because No_Initialization is
856 set even though there's an Expression. */
857 else if (kind == E_Constant
858 && Nkind (gnat_decl) == N_Object_Declaration
859 && Present (Expression (gnat_decl)))
860 {
861 tree gnu_expr_type
862 = gnat_to_gnu_type (Etype (Expression (gnat_decl)));
863 gnu_size = TYPE_SIZE (gnu_expr_type);
864 gnu_ada_size = TYPE_ADA_SIZE (gnu_expr_type);
865 }
866 else
867 {
868 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
869 /* We can be called on unconstrained arrays in this mode. */
870 if (!type_annotate_only)
871 gnu_ada_size = max_size (TYPE_ADA_SIZE (gnu_type), true);
872 mutable_p = true;
873 }
874
875 /* If the size isn't constant and we are at global level, call
876 elaborate_expression_1 to make a variable for it rather than
877 calculating it each time. */
878 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
879 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
880 "SIZE", definition, false);
881 }
882
883 /* If the size is zero byte, make it one byte since some linkers have
884 troubles with zero-sized objects. If the object will have a
885 template, that will make it nonzero so don't bother. Also avoid
886 doing that for an object renaming or an object with an address
887 clause, as we would lose useful information on the view size
888 (e.g. for null array slices) and we are not allocating the object
889 here anyway. */
890 if (((gnu_size
891 && integer_zerop (gnu_size)
892 && !TREE_OVERFLOW (gnu_size))
893 || (TYPE_SIZE (gnu_type)
894 && integer_zerop (TYPE_SIZE (gnu_type))
895 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
896 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
897 && No (gnat_renamed_obj)
898 && No (Address_Clause (gnat_entity)))
899 gnu_size = bitsize_unit_node;
900
901 /* If this is an object with no specified size and alignment, and
902 if either it is full access or we are not optimizing alignment for
903 space and it is composite and not an exception, an Out parameter
904 or a reference to another object, and the size of its type is a
905 constant, set the alignment to the smallest one which is not
906 smaller than the size, with an appropriate cap. */
907 if (!gnu_size && align == 0
908 && (Is_Full_Access (gnat_entity)
909 || (!Optimize_Alignment_Space (gnat_entity)
910 && kind != E_Exception
911 && kind != E_Out_Parameter
912 && Is_Composite_Type (gnat_type)
913 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
914 && !Is_Exported (gnat_entity)
915 && !imported_p
916 && No (gnat_renamed_obj)
917 && No (Address_Clause (gnat_entity))))
918 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
919 align = promote_object_alignment (gnu_type, gnat_entity);
920
921 /* If the object is set to have atomic components, find the component
922 type and validate it.
923
924 ??? Note that we ignore Has_Volatile_Components on objects; it's
925 not at all clear what to do in that case. */
926 if (Has_Atomic_Components (gnat_entity))
927 {
928 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
929 ? TREE_TYPE (gnu_type) : gnu_type);
930
931 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
932 && TYPE_MULTI_ARRAY_P (gnu_inner))
933 gnu_inner = TREE_TYPE (gnu_inner);
934
935 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
936 }
937
938 /* If this is an aliased object with an unconstrained array nominal
939 subtype, make a type that includes the template. We will either
940 allocate or create a variable of that type, see below. */
941 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
942 && Is_Array_Type (Underlying_Type (gnat_type))
943 && !type_annotate_only)
944 {
945 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
946 gnu_type
947 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
948 gnu_type,
949 concat_name (gnu_entity_name,
950 "UNC"),
951 debug_info_p);
952 }
953
954 /* ??? If this is an object of CW type initialized to a value, try to
955 ensure that the object is sufficient aligned for this value, but
956 without pessimizing the allocation. This is a kludge necessary
957 because we don't support dynamic alignment. */
958 if (align == 0
959 && Ekind (gnat_type) == E_Class_Wide_Subtype
960 && No (gnat_renamed_obj)
961 && No (Address_Clause (gnat_entity)))
962 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
963
964 #ifdef MINIMUM_ATOMIC_ALIGNMENT
965 /* If the size is a constant and no alignment is specified, force
966 the alignment to be the minimum valid atomic alignment. The
967 restriction on constant size avoids problems with variable-size
968 temporaries; if the size is variable, there's no issue with
969 atomic access. Also don't do this for a constant, since it isn't
970 necessary and can interfere with constant replacement. Finally,
971 do not do it for Out parameters since that creates an
972 size inconsistency with In parameters. */
973 if (align == 0
974 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
975 && !FLOAT_TYPE_P (gnu_type)
976 && !const_flag && No (gnat_renamed_obj)
977 && !imported_p && No (Address_Clause (gnat_entity))
978 && kind != E_Out_Parameter
979 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
980 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
981 align = MINIMUM_ATOMIC_ALIGNMENT;
982 #endif
983
984 /* Do not take into account aliased adjustments or alignment promotions
985 to compute the size of the object. */
986 tree gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
987
988 /* If the object is aliased, of a constrained nominal subtype and its
989 size might be zero at run time, we force at least the unit size. */
990 if (Is_Aliased (gnat_entity)
991 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
992 && Is_Array_Type (Underlying_Type (gnat_type))
993 && !TREE_CONSTANT (gnu_object_size))
994 gnu_size = size_binop (MAX_EXPR, gnu_object_size, bitsize_unit_node);
995
996 /* Make a new type with the desired size and alignment, if needed. */
997 if (gnu_size || align > 0)
998 {
999 tree orig_type = gnu_type;
1000
1001 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
1002 false, definition, true);
1003
1004 /* If the nominal subtype of the object is unconstrained and its
1005 size is not fixed, compute the Ada size from the Ada size of
1006 the subtype and/or the expression; this will make it possible
1007 for gnat_type_max_size to easily compute a maximum size. */
1008 if (gnu_ada_size && gnu_size && !TREE_CONSTANT (gnu_size))
1009 SET_TYPE_ADA_SIZE (gnu_type, gnu_ada_size);
1010
1011 /* If a padding record was made, declare it now since it will
1012 never be declared otherwise. This is necessary to ensure
1013 that its subtrees are properly marked. */
1014 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
1015 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
1016 debug_info_p, gnat_entity);
1017 }
1018
1019 /* Now check if the type of the object allows atomic access. */
1020 if (Is_Full_Access (gnat_entity))
1021 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
1022
1023 /* If this is a renaming, avoid as much as possible to create a new
1024 object. However, in some cases, creating it is required because
1025 renaming can be applied to objects that are not names in Ada.
1026 This processing needs to be applied to the raw expression so as
1027 to make it more likely to rename the underlying object. */
1028 if (Present (gnat_renamed_obj))
1029 {
1030 /* If the renamed object had padding, strip off the reference to
1031 the inner object and reset our type. */
1032 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
1033 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
1034 /* Strip useless conversions around the object. */
1035 || gnat_useless_type_conversion (gnu_expr))
1036 {
1037 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1038 gnu_type = TREE_TYPE (gnu_expr);
1039 }
1040
1041 /* Or else, if the renamed object has an unconstrained type with
1042 default discriminant, use the padded type. */
1043 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
1044 gnu_type = TREE_TYPE (gnu_expr);
1045
1046 /* If this is a constant renaming stemming from a function call,
1047 treat it as a normal object whose initial value is what is being
1048 renamed. RM 3.3 says that the result of evaluating a function
1049 call is a constant object. Therefore, it can be the inner
1050 object of a constant renaming and the renaming must be fully
1051 instantiated, i.e. it cannot be a reference to (part of) an
1052 existing object. And treat other rvalues the same way. */
1053 tree inner = gnu_expr;
1054 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
1055 inner = TREE_OPERAND (inner, 0);
1056 /* Expand_Dispatching_Call can prepend a comparison of the tags
1057 before the call to "=". */
1058 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
1059 || TREE_CODE (inner) == COMPOUND_EXPR)
1060 inner = TREE_OPERAND (inner, 1);
1061 if ((TREE_CODE (inner) == CALL_EXPR
1062 && !call_is_atomic_load (inner))
1063 || TREE_CODE (inner) == CONSTRUCTOR
1064 || CONSTANT_CLASS_P (inner)
1065 || COMPARISON_CLASS_P (inner)
1066 || BINARY_CLASS_P (inner)
1067 || EXPRESSION_CLASS_P (inner)
1068 /* We need to detect the case where a temporary is created to
1069 hold the return value, since we cannot safely rename it at
1070 top level as it lives only in the elaboration routine. */
1071 || (TREE_CODE (inner) == VAR_DECL
1072 && DECL_RETURN_VALUE_P (inner))
1073 /* We also need to detect the case where the front-end creates
1074 a dangling 'reference to a function call at top level and
1075 substitutes it in the renaming, for example:
1076
1077 q__b : boolean renames r__f.e (1);
1078
1079 can be rewritten into:
1080
1081 q__R1s : constant q__A2s := r__f'reference;
1082 [...]
1083 q__b : boolean renames q__R1s.all.e (1);
1084
1085 We cannot safely rename the rewritten expression since the
1086 underlying object lives only in the elaboration routine. */
1087 || (TREE_CODE (inner) == INDIRECT_REF
1088 && (inner
1089 = remove_conversions (TREE_OPERAND (inner, 0), true))
1090 && TREE_CODE (inner) == VAR_DECL
1091 && DECL_RETURN_VALUE_P (inner)))
1092 ;
1093
1094 /* Otherwise, this is an lvalue being renamed, so it needs to be
1095 elaborated as a reference and substituted for the entity. But
1096 this means that we must evaluate the address of the renaming
1097 in the definition case to instantiate the SAVE_EXPRs. */
1098 else
1099 {
1100 tree gnu_init = NULL_TREE;
1101
1102 if (type_annotate_only && TREE_CODE (gnu_expr) == ERROR_MARK)
1103 break;
1104
1105 gnu_expr
1106 = elaborate_reference (gnu_expr, gnat_entity, definition,
1107 &gnu_init);
1108
1109 /* No DECL_EXPR might be created so the expression needs to be
1110 marked manually because it will likely be shared. */
1111 if (global_bindings_p ())
1112 MARK_VISITED (gnu_expr);
1113
1114 /* This assertion will fail if the renamed object isn't aligned
1115 enough as to make it possible to honor the alignment set on
1116 the renaming. */
1117 if (align)
1118 {
1119 const unsigned int ralign
1120 = DECL_P (gnu_expr)
1121 ? DECL_ALIGN (gnu_expr)
1122 : TYPE_ALIGN (TREE_TYPE (gnu_expr));
1123 gcc_assert (ralign >= align);
1124 }
1125
1126 /* The expression might not be a DECL so save it manually. */
1127 gnu_decl = gnu_expr;
1128 save_gnu_tree (gnat_entity, gnu_decl, true);
1129 saved = true;
1130 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1131
1132 /* If this is only a reference to the entity, we are done. */
1133 if (!definition)
1134 break;
1135
1136 /* Otherwise, emit the initialization statement, if any. */
1137 if (gnu_init)
1138 add_stmt (gnu_init);
1139
1140 /* If it needs to be materialized for debugging purposes, build
1141 the entity as indirect reference to the renamed object. */
1142 if (Materialize_Entity (gnat_entity))
1143 {
1144 gnu_type = build_reference_type (gnu_type);
1145 const_flag = true;
1146 volatile_flag = false;
1147
1148 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
1149
1150 create_var_decl (gnu_entity_name, gnu_ext_name,
1151 TREE_TYPE (gnu_expr), gnu_expr,
1152 const_flag, Is_Public (gnat_entity),
1153 imported_p, static_flag, volatile_flag,
1154 artificial_p, debug_info_p, attr_list,
1155 gnat_entity, false);
1156 }
1157
1158 /* Otherwise, instantiate the SAVE_EXPRs if needed. */
1159 else if (TREE_SIDE_EFFECTS (gnu_expr))
1160 add_stmt (build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr));
1161
1162 break;
1163 }
1164 }
1165
1166 /* If we are defining an aliased object whose nominal subtype is
1167 unconstrained, the object is a record that contains both the
1168 template and the object. If there is an initializer, it will
1169 have already been converted to the right type, but we need to
1170 create the template if there is no initializer. */
1171 if (definition
1172 && !gnu_expr
1173 && TREE_CODE (gnu_type) == RECORD_TYPE
1174 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1175 /* Beware that padding might have been introduced above. */
1176 || (TYPE_PADDING_P (gnu_type)
1177 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1178 == RECORD_TYPE
1179 && TYPE_CONTAINS_TEMPLATE_P
1180 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1181 {
1182 tree template_field
1183 = TYPE_PADDING_P (gnu_type)
1184 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1185 : TYPE_FIELDS (gnu_type);
1186 vec<constructor_elt, va_gc> *v;
1187 vec_alloc (v, 1);
1188 tree t = build_template (TREE_TYPE (template_field),
1189 TREE_TYPE (DECL_CHAIN (template_field)),
1190 NULL_TREE);
1191 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1192 gnu_expr = gnat_build_constructor (gnu_type, v);
1193 }
1194
1195 /* Convert the expression to the type of the object if need be. */
1196 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1197 gnu_expr = convert (gnu_type, gnu_expr);
1198
1199 /* If this is a pointer that doesn't have an initializing expression,
1200 initialize it to NULL, unless the object is declared imported as
1201 per RM B.1(24). */
1202 if (definition
1203 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1204 && !gnu_expr
1205 && !Is_Imported (gnat_entity))
1206 gnu_expr = integer_zero_node;
1207
1208 /* If we are defining the object and it has an Address clause, we must
1209 either get the address expression from the saved GCC tree for the
1210 object if it has a Freeze node, or elaborate the address expression
1211 here since the front-end has guaranteed that the elaboration has no
1212 effects in this case. */
1213 if (definition && Present (Address_Clause (gnat_entity)))
1214 {
1215 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1216 const Node_Id gnat_address = Expression (gnat_clause);
1217 tree gnu_address = present_gnu_tree (gnat_entity)
1218 ? TREE_OPERAND (get_gnu_tree (gnat_entity), 0)
1219 : gnat_to_gnu (gnat_address);
1220
1221 save_gnu_tree (gnat_entity, NULL_TREE, false);
1222
1223 /* Convert the type of the object to a reference type that can
1224 alias everything as per RM 13.3(19). */
1225 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1226 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1227 gnu_type
1228 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1229 gnu_address = convert (gnu_type, gnu_address);
1230 used_by_ref = true;
1231 const_flag
1232 = (!Is_Public (gnat_entity)
1233 || compile_time_known_address_p (gnat_address));
1234 volatile_flag = false;
1235 gnu_size = NULL_TREE;
1236
1237 /* If this is an aliased object with an unconstrained array nominal
1238 subtype, then it can overlay only another aliased object with an
1239 unconstrained array nominal subtype and compatible template. */
1240 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1241 && Is_Array_Type (Underlying_Type (gnat_type))
1242 && !type_annotate_only)
1243 {
1244 tree rec_type = TREE_TYPE (gnu_type);
1245 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1246
1247 /* This is the pattern built for a regular object. */
1248 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1249 && TREE_OPERAND (gnu_address, 1) == off)
1250 gnu_address = TREE_OPERAND (gnu_address, 0);
1251
1252 /* This is the pattern built for an overaligned object. */
1253 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1254 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1255 == PLUS_EXPR
1256 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1257 == off)
1258 gnu_address
1259 = build2 (POINTER_PLUS_EXPR, gnu_type,
1260 TREE_OPERAND (gnu_address, 0),
1261 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1262
1263 /* We make an exception for an absolute address but we warn
1264 that there is a descriptor at the start of the object. */
1265 else if (TREE_CODE (gnu_address) == INTEGER_CST)
1266 {
1267 post_error_ne ("??aliased object& with unconstrained "
1268 "array nominal subtype", gnat_clause,
1269 gnat_entity);
1270 post_error ("\\starts with a descriptor whose size is "
1271 "given by ''Descriptor_Size", gnat_clause);
1272 }
1273
1274 else
1275 {
1276 post_error_ne ("aliased object& with unconstrained array "
1277 "nominal subtype", gnat_clause,
1278 gnat_entity);
1279 post_error ("\\can overlay only aliased object with "
1280 "compatible subtype", gnat_clause);
1281 }
1282 }
1283
1284 /* If we don't have an initializing expression for the underlying
1285 variable, the initializing expression for the pointer is the
1286 specified address. Otherwise, we have to make a COMPOUND_EXPR
1287 to assign both the address and the initial value. */
1288 if (!gnu_expr)
1289 gnu_expr = gnu_address;
1290 else
1291 gnu_expr
1292 = build2 (COMPOUND_EXPR, gnu_type,
1293 build_binary_op (INIT_EXPR, NULL_TREE,
1294 build_unary_op (INDIRECT_REF,
1295 NULL_TREE,
1296 gnu_address),
1297 gnu_expr),
1298 gnu_address);
1299 }
1300
1301 /* If it has an address clause and we are not defining it, mark it
1302 as an indirect object. Likewise for Stdcall objects that are
1303 imported. */
1304 if ((!definition && Present (Address_Clause (gnat_entity)))
1305 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1306 {
1307 /* Convert the type of the object to a reference type that can
1308 alias everything as per RM 13.3(19). */
1309 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1310 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1311 gnu_type
1312 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1313 used_by_ref = true;
1314 const_flag = false;
1315 volatile_flag = false;
1316 gnu_size = NULL_TREE;
1317
1318 /* No point in taking the address of an initializing expression
1319 that isn't going to be used. */
1320 gnu_expr = NULL_TREE;
1321
1322 /* If it has an address clause whose value is known at compile
1323 time, make the object a CONST_DECL. This will avoid a
1324 useless dereference. */
1325 if (Present (Address_Clause (gnat_entity)))
1326 {
1327 Node_Id gnat_address
1328 = Expression (Address_Clause (gnat_entity));
1329
1330 if (compile_time_known_address_p (gnat_address))
1331 {
1332 gnu_expr = gnat_to_gnu (gnat_address);
1333 const_flag = true;
1334 }
1335 }
1336 }
1337
1338 /* If we are at top level and this object is of variable size,
1339 make the actual type a hidden pointer to the real type and
1340 make the initializer be a memory allocation and initialization.
1341 Likewise for objects we aren't defining (presumed to be
1342 external references from other packages), but there we do
1343 not set up an initialization.
1344
1345 If the object's size overflows, make an allocator too, so that
1346 Storage_Error gets raised. Note that we will never free
1347 such memory, so we presume it never will get allocated. */
1348 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1349 global_bindings_p ()
1350 || !definition
1351 || static_flag)
1352 || (gnu_size
1353 && !allocatable_size_p (convert (sizetype,
1354 size_binop
1355 (CEIL_DIV_EXPR, gnu_size,
1356 bitsize_unit_node)),
1357 global_bindings_p ()
1358 || !definition
1359 || static_flag)))
1360 {
1361 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1362 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1363 gnu_type = build_reference_type (gnu_type);
1364 used_by_ref = true;
1365 const_flag = true;
1366 volatile_flag = false;
1367 gnu_size = NULL_TREE;
1368
1369 /* In case this was a aliased object whose nominal subtype is
1370 unconstrained, the pointer above will be a thin pointer and
1371 build_allocator will automatically make the template.
1372
1373 If we have a template initializer only (that we made above),
1374 pretend there is none and rely on what build_allocator creates
1375 again anyway. Otherwise (if we have a full initializer), get
1376 the data part and feed that to build_allocator.
1377
1378 If we are elaborating a mutable object, tell build_allocator to
1379 ignore a possibly simpler size from the initializer, if any, as
1380 we must allocate the maximum possible size in this case. */
1381 if (definition && !imported_p)
1382 {
1383 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1384
1385 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1386 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1387 {
1388 gnu_alloc_type
1389 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1390
1391 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1392 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1393 gnu_expr = NULL_TREE;
1394 else
1395 gnu_expr
1396 = build_component_ref
1397 (gnu_expr,
1398 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1399 false);
1400 }
1401
1402 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1403 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1404 post_error ("?`Storage_Error` will be raised at run time!",
1405 gnat_entity);
1406
1407 gnu_expr
1408 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1409 Empty, Empty, gnat_entity, mutable_p);
1410 }
1411 else
1412 gnu_expr = NULL_TREE;
1413 }
1414
1415 /* If this object would go into the stack and has an alignment larger
1416 than the largest stack alignment the back-end can honor, resort to
1417 a variable of "aligning type". */
1418 if (definition
1419 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1420 && !imported_p
1421 && !static_flag
1422 && !global_bindings_p ())
1423 {
1424 /* Create the new variable. No need for extra room before the
1425 aligned field as this is in automatic storage. */
1426 tree gnu_new_type
1427 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1428 TYPE_SIZE_UNIT (gnu_type),
1429 BIGGEST_ALIGNMENT, 0, gnat_entity);
1430 tree gnu_new_var
1431 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1432 NULL_TREE, gnu_new_type, NULL_TREE,
1433 false, false, false, false, false,
1434 true, debug_info_p && definition, NULL,
1435 gnat_entity);
1436
1437 /* Initialize the aligned field if we have an initializer. */
1438 if (gnu_expr)
1439 add_stmt_with_node
1440 (build_binary_op (INIT_EXPR, NULL_TREE,
1441 build_component_ref
1442 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1443 false),
1444 gnu_expr),
1445 gnat_entity);
1446
1447 /* And setup this entity as a reference to the aligned field. */
1448 gnu_type = build_reference_type (gnu_type);
1449 gnu_expr
1450 = build_unary_op
1451 (ADDR_EXPR, NULL_TREE,
1452 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1453 false));
1454 TREE_CONSTANT (gnu_expr) = 1;
1455
1456 used_by_ref = true;
1457 const_flag = true;
1458 volatile_flag = false;
1459 gnu_size = NULL_TREE;
1460 }
1461
1462 /* If this is an aggregate constant initialized to a constant, force it
1463 to be statically allocated. This saves an initialization copy. */
1464 if (!static_flag
1465 && const_flag
1466 && gnu_expr
1467 && TREE_CONSTANT (gnu_expr)
1468 && AGGREGATE_TYPE_P (gnu_type)
1469 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1470 && !(TYPE_IS_PADDING_P (gnu_type)
1471 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1472 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1473 static_flag = true;
1474
1475 /* If this is an aliased object with an unconstrained array nominal
1476 subtype, we make its type a thin reference, i.e. the reference
1477 counterpart of a thin pointer, so it points to the array part.
1478 This is aimed to make it easier for the debugger to decode the
1479 object. Note that we have to do it this late because of the
1480 couple of allocation adjustments that might be made above. */
1481 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1482 && Is_Array_Type (Underlying_Type (gnat_type))
1483 && !type_annotate_only)
1484 {
1485 /* In case the object with the template has already been allocated
1486 just above, we have nothing to do here. */
1487 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1488 {
1489 /* This variable is a GNAT encoding used by Workbench: let it
1490 go through the debugging information but mark it as
1491 artificial: users are not interested in it. */
1492 tree gnu_unc_var
1493 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1494 NULL_TREE, gnu_type, gnu_expr,
1495 const_flag, Is_Public (gnat_entity),
1496 imported_p || !definition, static_flag,
1497 volatile_flag, true,
1498 debug_info_p && definition,
1499 NULL, gnat_entity);
1500 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1501 TREE_CONSTANT (gnu_expr) = 1;
1502
1503 used_by_ref = true;
1504 const_flag = true;
1505 volatile_flag = false;
1506 inner_const_flag = TREE_READONLY (gnu_unc_var);
1507 gnu_size = NULL_TREE;
1508 }
1509
1510 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1511 gnu_type
1512 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1513 }
1514
1515 /* Convert the expression to the type of the object if need be. */
1516 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1517 gnu_expr = convert (gnu_type, gnu_expr);
1518
1519 /* If this name is external or a name was specified, use it, but don't
1520 use the Interface_Name with an address clause (see cd30005). */
1521 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1522 || (Present (Interface_Name (gnat_entity))
1523 && No (Address_Clause (gnat_entity))))
1524 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1525
1526 /* Deal with a pragma Linker_Section on a constant or variable. */
1527 if ((kind == E_Constant || kind == E_Variable)
1528 && Present (Linker_Section_Pragma (gnat_entity)))
1529 prepend_one_attribute_pragma (&attr_list,
1530 Linker_Section_Pragma (gnat_entity));
1531
1532 /* Now create the variable or the constant and set various flags. */
1533 gnu_decl
1534 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1535 gnu_expr, const_flag, Is_Public (gnat_entity),
1536 imported_p || !definition, static_flag,
1537 volatile_flag, artificial_p,
1538 debug_info_p && definition, attr_list,
1539 gnat_entity, true);
1540 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1541 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1542 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1543
1544 /* If we are defining an Out parameter and optimization isn't enabled,
1545 create a fake PARM_DECL for debugging purposes and make it point to
1546 the VAR_DECL. Suppress debug info for the latter but make sure it
1547 will live in memory so that it can be accessed from within the
1548 debugger through the PARM_DECL. */
1549 if (kind == E_Out_Parameter
1550 && definition
1551 && debug_info_p
1552 && !optimize
1553 && !flag_generate_lto)
1554 {
1555 tree param = create_param_decl (gnu_entity_name, gnu_type);
1556 gnat_pushdecl (param, gnat_entity);
1557 SET_DECL_VALUE_EXPR (param, gnu_decl);
1558 DECL_HAS_VALUE_EXPR_P (param) = 1;
1559 DECL_IGNORED_P (gnu_decl) = 1;
1560 TREE_ADDRESSABLE (gnu_decl) = 1;
1561 }
1562
1563 /* If this is a loop parameter, set the corresponding flag. */
1564 else if (kind == E_Loop_Parameter)
1565 DECL_LOOP_PARM_P (gnu_decl) = 1;
1566
1567 /* If this is a constant and we are defining it or it generates a real
1568 symbol at the object level and we are referencing it, we may want
1569 or need to have a true variable to represent it:
1570 - if the constant is public and not overlaid on something else,
1571 - if its address is taken,
1572 - if it is aliased,
1573 - if optimization isn't enabled, for debugging purposes. */
1574 if (TREE_CODE (gnu_decl) == CONST_DECL
1575 && (definition || Sloc (gnat_entity) > Standard_Location)
1576 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1577 || Address_Taken (gnat_entity)
1578 || Is_Aliased (gnat_entity)
1579 || (!optimize && debug_info_p)))
1580 {
1581 tree gnu_corr_var
1582 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1583 gnu_expr, true, Is_Public (gnat_entity),
1584 !definition, static_flag, volatile_flag,
1585 artificial_p, debug_info_p && definition,
1586 attr_list, gnat_entity, false);
1587
1588 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1589 DECL_IGNORED_P (gnu_decl) = 1;
1590 }
1591
1592 /* If this is a constant, even if we don't need a true variable, we
1593 may need to avoid returning the initializer in every case. That
1594 can happen for the address of a (constant) constructor because,
1595 upon dereferencing it, the constructor will be reinjected in the
1596 tree, which may not be valid in every case; see lvalue_required_p
1597 for more details. */
1598 if (TREE_CODE (gnu_decl) == CONST_DECL)
1599 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1600
1601 /* If this object is declared in a block that contains a block with an
1602 exception handler, and we aren't using the GCC exception mechanism,
1603 we must force this variable in memory in order to avoid an invalid
1604 optimization. */
1605 if (Front_End_Exceptions ()
1606 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1607 TREE_ADDRESSABLE (gnu_decl) = 1;
1608
1609 /* If this is a local variable with non-BLKmode and aggregate type,
1610 and optimization isn't enabled, then force it in memory so that
1611 a register won't be allocated to it with possible subparts left
1612 uninitialized and reaching the register allocator. */
1613 else if (TREE_CODE (gnu_decl) == VAR_DECL
1614 && !DECL_EXTERNAL (gnu_decl)
1615 && !TREE_STATIC (gnu_decl)
1616 && DECL_MODE (gnu_decl) != BLKmode
1617 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1618 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1619 && !optimize)
1620 TREE_ADDRESSABLE (gnu_decl) = 1;
1621
1622 /* If we are defining an object with variable size or an object with
1623 fixed size that will be dynamically allocated, and we are using the
1624 front-end setjmp/longjmp exception mechanism, update the setjmp
1625 buffer. */
1626 if (definition
1627 && Exception_Mechanism == Front_End_SJLJ
1628 && get_block_jmpbuf_decl ()
1629 && DECL_SIZE_UNIT (gnu_decl)
1630 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1631 || (flag_stack_check == GENERIC_STACK_CHECK
1632 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1633 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1634 add_stmt_with_node (build_call_n_expr
1635 (update_setjmp_buf_decl, 1,
1636 build_unary_op (ADDR_EXPR, NULL_TREE,
1637 get_block_jmpbuf_decl ())),
1638 gnat_entity);
1639
1640 /* Back-annotate Esize and Alignment of the object if not already
1641 known. Note that we pick the values of the type, not those of
1642 the object, to shield ourselves from low-level platform-dependent
1643 adjustments like alignment promotion. This is both consistent with
1644 all the treatment above, where alignment and size are set on the
1645 type of the object and not on the object directly, and makes it
1646 possible to support all confirming representation clauses. */
1647 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1648 used_by_ref);
1649 }
1650 break;
1651
1652 case E_Void:
1653 /* Return a TYPE_DECL for "void" that we previously made. */
1654 gnu_decl = TYPE_NAME (void_type_node);
1655 break;
1656
1657 case E_Enumeration_Type:
1658 /* A special case: for the types Character and Wide_Character in
1659 Standard, we do not list all the literals. So if the literals
1660 are not specified, make this an integer type. */
1661 if (No (First_Literal (gnat_entity)))
1662 {
1663 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1664 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1665 else
1666 gnu_type = make_unsigned_type (esize);
1667 TYPE_NAME (gnu_type) = gnu_entity_name;
1668
1669 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1670 This is needed by the DWARF-2 back-end to distinguish between
1671 unsigned integer types and character types. */
1672 TYPE_STRING_FLAG (gnu_type) = 1;
1673
1674 /* This flag is needed by the call just below. */
1675 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1676
1677 finish_character_type (gnu_type);
1678 }
1679 else
1680 {
1681 /* We have a list of enumeral constants in First_Literal. We make a
1682 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1683 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1684 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1685 value of the literal. But when we have a regular boolean type, we
1686 simplify this a little by using a BOOLEAN_TYPE. */
1687 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1688 && !Has_Non_Standard_Rep (gnat_entity);
1689 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1690 tree gnu_list = NULL_TREE;
1691 Entity_Id gnat_literal;
1692
1693 /* Boolean types with foreign convention have precision 1. */
1694 if (is_boolean && foreign)
1695 esize = 1;
1696
1697 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1698 TYPE_PRECISION (gnu_type) = esize;
1699 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1700 set_min_and_max_values_for_integral_type (gnu_type, esize,
1701 TYPE_SIGN (gnu_type));
1702 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1703 layout_type (gnu_type);
1704
1705 for (gnat_literal = First_Literal (gnat_entity);
1706 Present (gnat_literal);
1707 gnat_literal = Next_Literal (gnat_literal))
1708 {
1709 tree gnu_value
1710 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1711 /* Do not generate debug info for individual enumerators. */
1712 tree gnu_literal
1713 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1714 gnu_type, gnu_value, true, false, false,
1715 false, false, artificial_p, false,
1716 NULL, gnat_literal);
1717 save_gnu_tree (gnat_literal, gnu_literal, false);
1718 gnu_list
1719 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1720 }
1721
1722 if (!is_boolean)
1723 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1724
1725 /* Note that the bounds are updated at the end of this function
1726 to avoid an infinite recursion since they refer to the type. */
1727 goto discrete_type;
1728 }
1729 break;
1730
1731 case E_Signed_Integer_Type:
1732 /* For integer types, just make a signed type the appropriate number
1733 of bits. */
1734 gnu_type = make_signed_type (esize);
1735 goto discrete_type;
1736
1737 case E_Ordinary_Fixed_Point_Type:
1738 case E_Decimal_Fixed_Point_Type:
1739 {
1740 /* Small_Value is the scale factor. */
1741 const Ureal gnat_small_value = Small_Value (gnat_entity);
1742 tree scale_factor = NULL_TREE;
1743
1744 gnu_type = make_signed_type (esize);
1745
1746 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1747 binary or decimal scale: it is easier to read for humans. */
1748 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1749 && (Rbase (gnat_small_value) == 2
1750 || Rbase (gnat_small_value) == 10))
1751 {
1752 tree base
1753 = build_int_cst (integer_type_node, Rbase (gnat_small_value));
1754 tree exponent
1755 = build_int_cst (integer_type_node,
1756 UI_To_Int (Denominator (gnat_small_value)));
1757 scale_factor
1758 = build2 (RDIV_EXPR, integer_type_node,
1759 integer_one_node,
1760 build2 (POWER_EXPR, integer_type_node,
1761 base, exponent));
1762 }
1763
1764 /* Use the arbitrary scale factor description. Note that we support
1765 a Small_Value whose magnitude is larger than 64-bit even on 32-bit
1766 platforms, so we unconditionally use a (dummy) 128-bit type. */
1767 else
1768 {
1769 const Uint gnat_num = Norm_Num (gnat_small_value);
1770 const Uint gnat_den = Norm_Den (gnat_small_value);
1771 tree gnu_small_type = make_unsigned_type (128);
1772 tree gnu_num = UI_To_gnu (gnat_num, gnu_small_type);
1773 tree gnu_den = UI_To_gnu (gnat_den, gnu_small_type);
1774
1775 scale_factor
1776 = build2 (RDIV_EXPR, gnu_small_type, gnu_num, gnu_den);
1777 }
1778
1779 TYPE_FIXED_POINT_P (gnu_type) = 1;
1780 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1781 }
1782 goto discrete_type;
1783
1784 case E_Modular_Integer_Type:
1785 {
1786 /* Packed Array Impl. Types are supposed to be subtypes only. */
1787 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1788
1789 /* For modular types, make the unsigned type of the proper number
1790 of bits and then set up the modulus, if required. */
1791 gnu_type = make_unsigned_type (esize);
1792
1793 /* Get the modulus in this type. If the modulus overflows, assume
1794 that this is because it was equal to 2**Esize. Note that there
1795 is no overflow checking done on unsigned types, so we detect the
1796 overflow by looking for a modulus of zero, which is invalid. */
1797 tree gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1798
1799 /* If the modulus is not 2**Esize, then this also means that the upper
1800 bound of the type, i.e. modulus - 1, is not maximal, so we create an
1801 extra subtype to carry it and set the modulus on the base type. */
1802 if (!integer_zerop (gnu_modulus))
1803 {
1804 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1805 TYPE_MODULAR_P (gnu_type) = 1;
1806 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1807 tree gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1808 build_int_cst (gnu_type, 1));
1809 gnu_type
1810 = create_extra_subtype (gnu_type, TYPE_MIN_VALUE (gnu_type),
1811 gnu_high);
1812 }
1813 }
1814 goto discrete_type;
1815
1816 case E_Signed_Integer_Subtype:
1817 case E_Enumeration_Subtype:
1818 case E_Modular_Integer_Subtype:
1819 case E_Ordinary_Fixed_Point_Subtype:
1820 case E_Decimal_Fixed_Point_Subtype:
1821
1822 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1823 not want to call create_range_type since we would like each subtype
1824 node to be distinct. ??? Historically this was in preparation for
1825 when memory aliasing is implemented, but that's obsolete now given
1826 the call to relate_alias_sets below.
1827
1828 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1829 this fact is used by the arithmetic conversion functions.
1830
1831 We elaborate the Ancestor_Subtype if it is not in the current unit
1832 and one of our bounds is non-static. We do this to ensure consistent
1833 naming in the case where several subtypes share the same bounds, by
1834 elaborating the first such subtype first, thus using its name. */
1835
1836 if (!definition
1837 && Present (Ancestor_Subtype (gnat_entity))
1838 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1839 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1840 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1841 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1842
1843 /* Set the precision to the Esize except for bit-packed arrays. */
1844 if (Is_Packed_Array_Impl_Type (gnat_entity))
1845 esize = UI_To_Int (RM_Size (gnat_entity));
1846
1847 /* Boolean types with foreign convention have precision 1. */
1848 if (Is_Boolean_Type (gnat_entity) && foreign)
1849 {
1850 gnu_type = make_node (BOOLEAN_TYPE);
1851 TYPE_PRECISION (gnu_type) = 1;
1852 TYPE_UNSIGNED (gnu_type) = 1;
1853 set_min_and_max_values_for_integral_type (gnu_type, 1, UNSIGNED);
1854 layout_type (gnu_type);
1855 }
1856 /* First subtypes of Character are treated as Character; otherwise
1857 this should be an unsigned type if the base type is unsigned or
1858 if the lower bound is constant and non-negative or if the type
1859 is biased. However, even if the lower bound is constant and
1860 non-negative, we use a signed type for a subtype with the same
1861 size as its signed base type, because this eliminates useless
1862 conversions to it and gives more leeway to the optimizer; but
1863 this means that we will need to explicitly test for this case
1864 when we change the representation based on the RM size. */
1865 else if (kind == E_Enumeration_Subtype
1866 && No (First_Literal (Etype (gnat_entity)))
1867 && Esize (gnat_entity) == RM_Size (gnat_entity)
1868 && esize == CHAR_TYPE_SIZE
1869 && flag_signed_char)
1870 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1871 else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity)))
1872 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1873 && Is_Unsigned_Type (gnat_entity))
1874 || Has_Biased_Representation (gnat_entity))
1875 gnu_type = make_unsigned_type (esize);
1876 else
1877 gnu_type = make_signed_type (esize);
1878 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1879
1880 SET_TYPE_RM_MIN_VALUE
1881 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1882 gnat_entity, "L", definition, true,
1883 debug_info_p));
1884
1885 SET_TYPE_RM_MAX_VALUE
1886 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1887 gnat_entity, "U", definition, true,
1888 debug_info_p));
1889
1890 if (TREE_CODE (gnu_type) == INTEGER_TYPE)
1891 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1892 = Has_Biased_Representation (gnat_entity);
1893
1894 /* Do the same processing for Character subtypes as for types. */
1895 if (TREE_CODE (TREE_TYPE (gnu_type)) == INTEGER_TYPE
1896 && TYPE_STRING_FLAG (TREE_TYPE (gnu_type)))
1897 {
1898 TYPE_NAME (gnu_type) = gnu_entity_name;
1899 TYPE_STRING_FLAG (gnu_type) = 1;
1900 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1901 finish_character_type (gnu_type);
1902 }
1903
1904 /* Inherit our alias set from what we're a subtype of. Subtypes
1905 are not different types and a pointer can designate any instance
1906 within a subtype hierarchy. */
1907 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1908
1909 /* One of the above calls might have caused us to be elaborated,
1910 so don't blow up if so. */
1911 if (present_gnu_tree (gnat_entity))
1912 {
1913 maybe_present = true;
1914 break;
1915 }
1916
1917 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1918 TYPE_STUB_DECL (gnu_type)
1919 = create_type_stub_decl (gnu_entity_name, gnu_type);
1920
1921 discrete_type:
1922
1923 /* We have to handle clauses that under-align the type specially. */
1924 if ((Present (Alignment_Clause (gnat_entity))
1925 || (Is_Packed_Array_Impl_Type (gnat_entity)
1926 && Present
1927 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1928 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1929 {
1930 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1931 if (align >= TYPE_ALIGN (gnu_type))
1932 align = 0;
1933 }
1934
1935 /* If the type we are dealing with represents a bit-packed array,
1936 we need to have the bits left justified on big-endian targets
1937 and right justified on little-endian targets. We also need to
1938 ensure that when the value is read (e.g. for comparison of two
1939 such values), we only get the good bits, since the unused bits
1940 are uninitialized. Both goals are accomplished by wrapping up
1941 the modular type in an enclosing record type. */
1942 if (Is_Packed_Array_Impl_Type (gnat_entity))
1943 {
1944 tree gnu_field_type, gnu_field, t;
1945
1946 gcc_assert (Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1947 TYPE_BIT_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1948
1949 /* Make the original array type a parallel/debug type. */
1950 if (debug_info_p)
1951 {
1952 tree gnu_name
1953 = associate_original_type_to_packed_array (gnu_type,
1954 gnat_entity);
1955 if (gnu_name)
1956 gnu_entity_name = gnu_name;
1957 }
1958
1959 /* Set the RM size before wrapping up the original type. */
1960 SET_TYPE_RM_SIZE (gnu_type,
1961 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1962
1963 /* Create a stripped-down declaration, mainly for debugging. */
1964 t = create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1965 gnat_entity);
1966
1967 /* Now save it and build the enclosing record type. */
1968 gnu_field_type = gnu_type;
1969
1970 gnu_type = make_node (RECORD_TYPE);
1971 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1972 TYPE_PACKED (gnu_type) = 1;
1973 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1974 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1975 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1976
1977 /* Propagate the alignment of the modular type to the record type,
1978 unless there is an alignment clause that under-aligns the type.
1979 This means that bit-packed arrays are given "ceil" alignment for
1980 their size by default, which may seem counter-intuitive but makes
1981 it possible to overlay them on modular types easily. */
1982 SET_TYPE_ALIGN (gnu_type,
1983 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1984
1985 /* Propagate the reverse storage order flag to the record type so
1986 that the required byte swapping is performed when retrieving the
1987 enclosed modular value. */
1988 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1989 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1990
1991 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1992
1993 /* Don't declare the field as addressable since we won't be taking
1994 its address and this would prevent create_field_decl from making
1995 a bitfield. */
1996 gnu_field
1997 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1998 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1999
2000 /* We will output additional debug info manually below. */
2001 finish_record_type (gnu_type, gnu_field, 2, false);
2002 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
2003
2004 /* Make the original array type a parallel/debug type. Note that
2005 gnat_get_array_descr_info needs a TYPE_IMPL_PACKED_ARRAY_P type
2006 so we use an intermediate step for standard DWARF. */
2007 if (debug_info_p)
2008 {
2009 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2010 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
2011 else if (DECL_PARALLEL_TYPE (t))
2012 add_parallel_type (gnu_type, DECL_PARALLEL_TYPE (t));
2013 }
2014 }
2015
2016 /* If the type we are dealing with has got a smaller alignment than the
2017 natural one, we need to wrap it up in a record type and misalign the
2018 latter; we reuse the padding machinery for this purpose. */
2019 else if (align > 0)
2020 {
2021 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2022
2023 /* Set the RM size before wrapping the type. */
2024 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
2025
2026 /* Create a stripped-down declaration, mainly for debugging. */
2027 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
2028 gnat_entity);
2029
2030 gnu_type
2031 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
2032 gnat_entity, false, definition, false);
2033
2034 TYPE_PACKED (gnu_type) = 1;
2035 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
2036 }
2037
2038 break;
2039
2040 case E_Floating_Point_Type:
2041 /* The type of the Low and High bounds can be our type if this is
2042 a type from Standard, so set them at the end of the function. */
2043 gnu_type = make_node (REAL_TYPE);
2044 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2045 layout_type (gnu_type);
2046 break;
2047
2048 case E_Floating_Point_Subtype:
2049 /* See the E_Signed_Integer_Subtype case for the rationale. */
2050 if (!definition
2051 && Present (Ancestor_Subtype (gnat_entity))
2052 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
2053 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
2054 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
2055 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
2056
2057 gnu_type = make_node (REAL_TYPE);
2058 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2059 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2060 TYPE_GCC_MIN_VALUE (gnu_type)
2061 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2062 TYPE_GCC_MAX_VALUE (gnu_type)
2063 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2064 layout_type (gnu_type);
2065
2066 SET_TYPE_RM_MIN_VALUE
2067 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2068 gnat_entity, "L", definition, true,
2069 debug_info_p));
2070
2071 SET_TYPE_RM_MAX_VALUE
2072 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2073 gnat_entity, "U", definition, true,
2074 debug_info_p));
2075
2076 /* Inherit our alias set from what we're a subtype of, as for
2077 integer subtypes. */
2078 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2079
2080 /* One of the above calls might have caused us to be elaborated,
2081 so don't blow up if so. */
2082 maybe_present = true;
2083 break;
2084
2085 /* Array Types and Subtypes
2086
2087 In GNAT unconstrained array types are represented by E_Array_Type and
2088 constrained array types are represented by E_Array_Subtype. They are
2089 translated into UNCONSTRAINED_ARRAY_TYPE and ARRAY_TYPE respectively.
2090 But there are no actual objects of an unconstrained array type; all we
2091 have are pointers to that type. In addition to the type node itself,
2092 4 other types associated with it are built in the process:
2093
2094 1. the array type (suffix XUA) containing the actual data,
2095
2096 2. the template type (suffix XUB) containng the bounds,
2097
2098 3. the fat pointer type (suffix XUP) representing a pointer or a
2099 reference to the unconstrained array type:
2100 XUP = struct { XUA *, XUB * }
2101
2102 4. the object record type (suffix XUT) containing bounds and data:
2103 XUT = struct { XUB, XUA }
2104
2105 The bounds of the array type XUA (de)reference the XUB * field of a
2106 PLACEHOLDER_EXPR for the fat pointer type XUP, so the array type XUA
2107 is to be interpreted in the context of the fat pointer type XUB for
2108 debug info purposes. */
2109
2110 case E_Array_Type:
2111 {
2112 const bool convention_fortran_p
2113 = (Convention (gnat_entity) == Convention_Fortran);
2114 const int ndim = Number_Dimensions (gnat_entity);
2115 tree gnu_template_type;
2116 tree gnu_ptr_template;
2117 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2118 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2119 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2120 tree gnu_max_size = size_one_node, tem, obj;
2121 Entity_Id gnat_index;
2122 int index;
2123 tree comp_type;
2124
2125 /* Create the type for the component now, as it simplifies breaking
2126 type reference loops. */
2127 comp_type
2128 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2129 if (present_gnu_tree (gnat_entity))
2130 {
2131 /* As a side effect, the type may have been translated. */
2132 maybe_present = true;
2133 break;
2134 }
2135
2136 /* We complete an existing dummy fat pointer type in place. This both
2137 avoids further complex adjustments in update_pointer_to and yields
2138 better debugging information in DWARF by leveraging the support for
2139 incomplete declarations of "tagged" types in the DWARF back-end. */
2140 gnu_type = get_dummy_type (gnat_entity);
2141 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2142 {
2143 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2144 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2145 gnu_ptr_template =
2146 TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2147 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2148
2149 /* Save the contents of the dummy type for update_pointer_to. */
2150 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2151 TYPE_FIELDS (TYPE_POINTER_TO (gnu_type))
2152 = copy_node (TYPE_FIELDS (gnu_fat_type));
2153 DECL_CHAIN (TYPE_FIELDS (TYPE_POINTER_TO (gnu_type)))
2154 = copy_node (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2155 }
2156 else
2157 {
2158 gnu_fat_type = make_node (RECORD_TYPE);
2159 gnu_template_type = make_node (RECORD_TYPE);
2160 gnu_ptr_template = build_pointer_type (gnu_template_type);
2161 }
2162
2163 /* Make a node for the array. If we are not defining the array
2164 suppress expanding incomplete types. */
2165 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2166
2167 if (!definition)
2168 {
2169 defer_incomplete_level++;
2170 this_deferred = true;
2171 }
2172
2173 /* Build the fat pointer type. Use a "void *" object instead of
2174 a pointer to the array type since we don't have the array type
2175 yet (it will reference the fat pointer via the bounds). Note
2176 that we reuse the existing fields of a dummy type because for:
2177
2178 type Arr is array (Positive range <>) of Element_Type;
2179 type Array_Ref is access Arr;
2180 Var : Array_Ref := Null;
2181
2182 in a declarative part, Arr will be frozen only after Var, which
2183 means that the fields used in the CONSTRUCTOR built for Null are
2184 those of the dummy type, which in turn means that COMPONENT_REFs
2185 of Var may be built with these fields. Now if COMPONENT_REFs of
2186 Var are also built later with the fields of the final type, the
2187 aliasing machinery may consider that the accesses are distinct
2188 if the FIELD_DECLs are distinct as objects. */
2189 if (COMPLETE_TYPE_P (gnu_fat_type))
2190 {
2191 tem = TYPE_FIELDS (gnu_fat_type);
2192 TREE_TYPE (tem) = ptr_type_node;
2193 TREE_TYPE (DECL_CHAIN (tem)) = gnu_ptr_template;
2194 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 0;
2195 for (tree t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2196 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2197 }
2198 else
2199 {
2200 /* We make the fields addressable for the sake of compatibility
2201 with languages for which the regular fields are addressable. */
2202 tem
2203 = create_field_decl (get_identifier ("P_ARRAY"),
2204 ptr_type_node, gnu_fat_type,
2205 NULL_TREE, NULL_TREE, 0, 1);
2206 DECL_CHAIN (tem)
2207 = create_field_decl (get_identifier ("P_BOUNDS"),
2208 gnu_ptr_template, gnu_fat_type,
2209 NULL_TREE, NULL_TREE, 0, 1);
2210 finish_fat_pointer_type (gnu_fat_type, tem);
2211 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2212 }
2213
2214 /* If the GNAT encodings are used, give the fat pointer type a name.
2215 If this is a packed array, tell the debugger how to interpret the
2216 underlying bits by fetching that of the implementation type. But
2217 in any case, mark it as artificial so the debugger can skip it. */
2218 const Entity_Id gnat_name
2219 = (Present (Packed_Array_Impl_Type (gnat_entity))
2220 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2221 ? Packed_Array_Impl_Type (gnat_entity)
2222 : gnat_entity;
2223 tree xup_name
2224 = (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2225 ? create_concat_name (gnat_name, "XUP")
2226 : gnu_entity_name;
2227 create_type_decl (xup_name, gnu_fat_type, true, debug_info_p,
2228 gnat_entity);
2229
2230 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2231 is the fat pointer. This will be used to access the individual
2232 fields once we build them. */
2233 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2234 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2235 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2236 gnu_template_reference
2237 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2238 TREE_READONLY (gnu_template_reference) = 1;
2239 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2240
2241 /* Now create the GCC type for each index and add the fields for that
2242 index to the template. */
2243 for (index = (convention_fortran_p ? ndim - 1 : 0),
2244 gnat_index = First_Index (gnat_entity);
2245 IN_RANGE (index, 0, ndim - 1);
2246 index += (convention_fortran_p ? - 1 : 1),
2247 gnat_index = Next_Index (gnat_index))
2248 {
2249 char field_name[16];
2250 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2251 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2252 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2253 tree gnu_index_base_type = get_base_type (gnu_index_type);
2254 tree gnu_lb_field, gnu_hb_field;
2255 tree gnu_min, gnu_max, gnu_high;
2256
2257 /* Update the maximum size of the array in elements. */
2258 if (gnu_max_size)
2259 gnu_max_size
2260 = update_n_elem (gnu_max_size, gnu_orig_min, gnu_orig_max);
2261
2262 /* Now build the self-referential bounds of the index type. */
2263 gnu_index_type = maybe_character_type (gnu_index_type);
2264 gnu_index_base_type = maybe_character_type (gnu_index_base_type);
2265
2266 /* Make the FIELD_DECLs for the low and high bounds of this
2267 type and then make extractions of these fields from the
2268 template. */
2269 sprintf (field_name, "LB%d", index);
2270 gnu_lb_field = create_field_decl (get_identifier (field_name),
2271 gnu_index_type,
2272 gnu_template_type, NULL_TREE,
2273 NULL_TREE, 0, 0);
2274 Sloc_to_locus (Sloc (gnat_entity),
2275 &DECL_SOURCE_LOCATION (gnu_lb_field));
2276
2277 field_name[0] = 'U';
2278 gnu_hb_field = create_field_decl (get_identifier (field_name),
2279 gnu_index_type,
2280 gnu_template_type, NULL_TREE,
2281 NULL_TREE, 0, 0);
2282 Sloc_to_locus (Sloc (gnat_entity),
2283 &DECL_SOURCE_LOCATION (gnu_hb_field));
2284
2285 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2286
2287 /* We can't use build_component_ref here since the template type
2288 isn't complete yet. */
2289 gnu_orig_min = build3 (COMPONENT_REF, TREE_TYPE (gnu_lb_field),
2290 gnu_template_reference, gnu_lb_field,
2291 NULL_TREE);
2292 gnu_orig_max = build3 (COMPONENT_REF, TREE_TYPE (gnu_hb_field),
2293 gnu_template_reference, gnu_hb_field,
2294 NULL_TREE);
2295 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2296
2297 gnu_min = convert (sizetype, gnu_orig_min);
2298 gnu_max = convert (sizetype, gnu_orig_max);
2299
2300 /* Compute the size of this dimension. See the E_Array_Subtype
2301 case below for the rationale. */
2302 gnu_high
2303 = build3 (COND_EXPR, sizetype,
2304 build2 (GE_EXPR, boolean_type_node,
2305 gnu_orig_max, gnu_orig_min),
2306 gnu_max,
2307 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2308
2309 /* Make a range type with the new range in the Ada base type.
2310 Then make an index type with the size range in sizetype. */
2311 gnu_index_types[index]
2312 = create_index_type (gnu_min, gnu_high,
2313 create_range_type (gnu_index_base_type,
2314 gnu_orig_min,
2315 gnu_orig_max),
2316 gnat_entity);
2317
2318 TYPE_NAME (gnu_index_types[index])
2319 = create_concat_name (gnat_entity, field_name);
2320 }
2321
2322 /* Install all the fields into the template. */
2323 TYPE_NAME (gnu_template_type)
2324 = create_concat_name (gnat_entity, "XUB");
2325 gnu_template_fields = NULL_TREE;
2326 for (index = 0; index < ndim; index++)
2327 gnu_template_fields
2328 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2329 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2330 debug_info_p);
2331 TYPE_CONTEXT (gnu_template_type) = current_function_decl;
2332
2333 /* If Component_Size is not already specified, annotate it with the
2334 size of the component. */
2335 if (Unknown_Component_Size (gnat_entity))
2336 Set_Component_Size (gnat_entity,
2337 annotate_value (TYPE_SIZE (comp_type)));
2338
2339 /* Compute the maximum size of the array in units. */
2340 if (gnu_max_size)
2341 gnu_max_size
2342 = size_binop (MULT_EXPR, gnu_max_size, TYPE_SIZE_UNIT (comp_type));
2343
2344 /* Now build the array type. */
2345 tem = comp_type;
2346 for (index = ndim - 1; index >= 0; index--)
2347 {
2348 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2349 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2350 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2351 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2352 set_reverse_storage_order_on_array_type (tem);
2353 if (array_type_has_nonaliased_component (tem, gnat_entity))
2354 set_nonaliased_component_on_array_type (tem);
2355 }
2356
2357 /* If an alignment is specified, use it if valid. But ignore it
2358 for the original type of packed array types. If the alignment
2359 was requested with an explicit alignment clause, state so. */
2360 if (No (Packed_Array_Impl_Type (gnat_entity))
2361 && Known_Alignment (gnat_entity))
2362 {
2363 SET_TYPE_ALIGN (tem,
2364 validate_alignment (Alignment (gnat_entity),
2365 gnat_entity,
2366 TYPE_ALIGN (tem)));
2367 if (Present (Alignment_Clause (gnat_entity)))
2368 TYPE_USER_ALIGN (tem) = 1;
2369 }
2370
2371 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2372 implementation types as such so that the debug information back-end
2373 can output the appropriate description for them. */
2374 TYPE_PACKED (tem)
2375 = (Is_Packed (gnat_entity)
2376 || Is_Packed_Array_Impl_Type (gnat_entity));
2377
2378 if (Treat_As_Volatile (gnat_entity))
2379 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2380
2381 /* Adjust the type of the pointer-to-array field of the fat pointer
2382 and record the aliasing relationships if necessary. */
2383 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2384 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2385 record_component_aliases (gnu_fat_type);
2386
2387 /* If the maximum size doesn't overflow, use it. */
2388 if (gnu_max_size
2389 && TREE_CODE (gnu_max_size) == INTEGER_CST
2390 && !TREE_OVERFLOW (gnu_max_size)
2391 && compare_tree_int (gnu_max_size, TYPE_ARRAY_SIZE_LIMIT) <= 0)
2392 TYPE_ARRAY_MAX_SIZE (tem) = gnu_max_size;
2393
2394 /* See the above description for the rationale. */
2395 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2396 artificial_p, debug_info_p, gnat_entity);
2397 TYPE_CONTEXT (tem) = gnu_fat_type;
2398 TYPE_CONTEXT (TYPE_POINTER_TO (tem)) = gnu_fat_type;
2399
2400 /* Create the type to be designated by thin pointers: a record type for
2401 the array and its template. We used to shift the fields to have the
2402 template at a negative offset, but this was somewhat of a kludge; we
2403 now shift thin pointer values explicitly but only those which have a
2404 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2405 Note that GDB can handle standard DWARF information for them, so we
2406 don't have to name them as a GNAT encoding, except if specifically
2407 asked to. */
2408 tree xut_name
2409 = (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2410 ? create_concat_name (gnat_name, "XUT")
2411 : gnu_entity_name;
2412 obj = build_unc_object_type (gnu_template_type, tem, xut_name,
2413 debug_info_p);
2414
2415 SET_TYPE_UNCONSTRAINED_ARRAY (obj, gnu_type);
2416 TYPE_OBJECT_RECORD_TYPE (gnu_type) = obj;
2417
2418 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2419 corresponding fat pointer. */
2420 TREE_TYPE (gnu_type) = gnu_fat_type;
2421 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2422 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2423 SET_TYPE_MODE (gnu_type, BLKmode);
2424 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2425 }
2426 break;
2427
2428 case E_Array_Subtype:
2429
2430 /* This is the actual data type for array variables. Multidimensional
2431 arrays are implemented as arrays of arrays. Note that arrays which
2432 have sparse enumeration subtypes as index components create sparse
2433 arrays, which is obviously space inefficient but so much easier to
2434 code for now.
2435
2436 Also note that the subtype never refers to the unconstrained array
2437 type, which is somewhat at variance with Ada semantics.
2438
2439 First check to see if this is simply a renaming of the array type.
2440 If so, the result is the array type. */
2441
2442 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2443 if (!Is_Constrained (gnat_entity))
2444 ;
2445 else
2446 {
2447 Entity_Id gnat_index, gnat_base_index;
2448 const bool convention_fortran_p
2449 = (Convention (gnat_entity) == Convention_Fortran);
2450 const int ndim = Number_Dimensions (gnat_entity);
2451 tree gnu_base_type = gnu_type;
2452 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2453 tree gnu_max_size = size_one_node;
2454 bool need_index_type_struct = false;
2455 int index;
2456
2457 /* First create the GCC type for each index and find out whether
2458 special types are needed for debugging information. */
2459 for (index = (convention_fortran_p ? ndim - 1 : 0),
2460 gnat_index = First_Index (gnat_entity),
2461 gnat_base_index
2462 = First_Index (Implementation_Base_Type (gnat_entity));
2463 IN_RANGE (index, 0, ndim - 1);
2464 index += (convention_fortran_p ? - 1 : 1),
2465 gnat_index = Next_Index (gnat_index),
2466 gnat_base_index = Next_Index (gnat_base_index))
2467 {
2468 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2469 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2470 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2471 tree gnu_index_base_type = get_base_type (gnu_index_type);
2472 tree gnu_base_index_type
2473 = get_unpadded_type (Etype (gnat_base_index));
2474 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2475 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2476 tree gnu_min, gnu_max, gnu_high;
2477
2478 /* We try to create subtypes for discriminants used as bounds
2479 that are more restrictive than those declared, by using the
2480 bounds of the index type of the base array type. This will
2481 make it possible to calculate the maximum size of the record
2482 type more conservatively. This may have already been done by
2483 the front-end (Exp_Ch3.Adjust_Discriminants), in which case
2484 there will be a conversion that needs to be removed first. */
2485 if (CONTAINS_PLACEHOLDER_P (gnu_orig_min)
2486 && TYPE_RM_SIZE (gnu_base_index_type)
2487 && tree_int_cst_lt (TYPE_RM_SIZE (gnu_base_index_type),
2488 TYPE_RM_SIZE (gnu_index_type)))
2489 {
2490 gnu_orig_min = remove_conversions (gnu_orig_min, false);
2491 TREE_TYPE (gnu_orig_min)
2492 = create_extra_subtype (TREE_TYPE (gnu_orig_min),
2493 gnu_base_orig_min,
2494 gnu_base_orig_max);
2495 }
2496
2497 if (CONTAINS_PLACEHOLDER_P (gnu_orig_max)
2498 && TYPE_RM_SIZE (gnu_base_index_type)
2499 && tree_int_cst_lt (TYPE_RM_SIZE (gnu_base_index_type),
2500 TYPE_RM_SIZE (gnu_index_type)))
2501 {
2502 gnu_orig_max = remove_conversions (gnu_orig_max, false);
2503 TREE_TYPE (gnu_orig_max)
2504 = create_extra_subtype (TREE_TYPE (gnu_orig_max),
2505 gnu_base_orig_min,
2506 gnu_base_orig_max);
2507 }
2508
2509 /* Update the maximum size of the array in elements. Here we
2510 see if any constraint on the index type of the base type
2511 can be used in the case of self-referential bounds on the
2512 index type of the array type. We look for a non-"infinite"
2513 and non-self-referential bound from any type involved and
2514 handle each bound separately. */
2515 if (gnu_max_size)
2516 {
2517 if (CONTAINS_PLACEHOLDER_P (gnu_orig_min))
2518 gnu_min = gnu_base_orig_min;
2519 else
2520 gnu_min = gnu_orig_min;
2521
2522 if (TREE_CODE (gnu_min) != INTEGER_CST
2523 || TREE_OVERFLOW (gnu_min))
2524 gnu_min = TYPE_MIN_VALUE (TREE_TYPE (gnu_min));
2525
2526 if (CONTAINS_PLACEHOLDER_P (gnu_orig_max))
2527 gnu_max = gnu_base_orig_max;
2528 else
2529 gnu_max = gnu_orig_max;
2530
2531 if (TREE_CODE (gnu_max) != INTEGER_CST
2532 || TREE_OVERFLOW (gnu_max))
2533 gnu_max = TYPE_MAX_VALUE (TREE_TYPE (gnu_max));
2534
2535 gnu_max_size
2536 = update_n_elem (gnu_max_size, gnu_min, gnu_max);
2537 }
2538
2539 /* Convert the bounds to the base type for consistency below. */
2540 gnu_index_base_type = maybe_character_type (gnu_index_base_type);
2541 gnu_orig_min = convert (gnu_index_base_type, gnu_orig_min);
2542 gnu_orig_max = convert (gnu_index_base_type, gnu_orig_max);
2543
2544 gnu_min = convert (sizetype, gnu_orig_min);
2545 gnu_max = convert (sizetype, gnu_orig_max);
2546
2547 /* See if the base array type is already flat. If it is, we
2548 are probably compiling an ACATS test but it will cause the
2549 code below to malfunction if we don't handle it specially. */
2550 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2551 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2552 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2553 {
2554 gnu_min = size_one_node;
2555 gnu_max = size_zero_node;
2556 gnu_high = gnu_max;
2557 }
2558
2559 /* Similarly, if one of the values overflows in sizetype and the
2560 range is null, use 1..0 for the sizetype bounds. */
2561 else if (TREE_CODE (gnu_min) == INTEGER_CST
2562 && TREE_CODE (gnu_max) == INTEGER_CST
2563 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2564 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2565 {
2566 gnu_min = size_one_node;
2567 gnu_max = size_zero_node;
2568 gnu_high = gnu_max;
2569 }
2570
2571 /* If the minimum and maximum values both overflow in sizetype,
2572 but the difference in the original type does not overflow in
2573 sizetype, ignore the overflow indication. */
2574 else if (TREE_CODE (gnu_min) == INTEGER_CST
2575 && TREE_CODE (gnu_max) == INTEGER_CST
2576 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2577 && !TREE_OVERFLOW
2578 (convert (sizetype,
2579 fold_build2 (MINUS_EXPR,
2580 gnu_index_base_type,
2581 gnu_orig_max,
2582 gnu_orig_min))))
2583 {
2584 TREE_OVERFLOW (gnu_min) = 0;
2585 TREE_OVERFLOW (gnu_max) = 0;
2586 gnu_high = gnu_max;
2587 }
2588
2589 /* Compute the size of this dimension in the general case. We
2590 need to provide GCC with an upper bound to use but have to
2591 deal with the "superflat" case. There are three ways to do
2592 this. If we can prove that the array can never be superflat,
2593 we can just use the high bound of the index type. */
2594 else if ((Nkind (gnat_index) == N_Range
2595 && cannot_be_superflat (gnat_index))
2596 /* Bit-Packed Array Impl. Types are never superflat. */
2597 || (Is_Packed_Array_Impl_Type (gnat_entity)
2598 && Is_Bit_Packed_Array
2599 (Original_Array_Type (gnat_entity))))
2600 gnu_high = gnu_max;
2601
2602 /* Otherwise, if the high bound is constant but the low bound is
2603 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2604 lower bound. Note that the comparison must be done in the
2605 original type to avoid any overflow during the conversion. */
2606 else if (TREE_CODE (gnu_max) == INTEGER_CST
2607 && TREE_CODE (gnu_min) != INTEGER_CST)
2608 {
2609 gnu_high = gnu_max;
2610 gnu_min
2611 = build_cond_expr (sizetype,
2612 build_binary_op (GE_EXPR,
2613 boolean_type_node,
2614 gnu_orig_max,
2615 gnu_orig_min),
2616 gnu_min,
2617 int_const_binop (PLUS_EXPR, gnu_max,
2618 size_one_node));
2619 }
2620
2621 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2622 in all the other cases. Note that we use int_const_binop for
2623 the shift by 1 if the bound is constant to avoid any unwanted
2624 overflow. */
2625 else
2626 gnu_high
2627 = build_cond_expr (sizetype,
2628 build_binary_op (GE_EXPR,
2629 boolean_type_node,
2630 gnu_orig_max,
2631 gnu_orig_min),
2632 gnu_max,
2633 TREE_CODE (gnu_min) == INTEGER_CST
2634 ? int_const_binop (MINUS_EXPR, gnu_min,
2635 size_one_node)
2636 : size_binop (MINUS_EXPR, gnu_min,
2637 size_one_node));
2638
2639 /* Reuse the index type for the range type. Then make an index
2640 type with the size range in sizetype. */
2641 gnu_index_types[index]
2642 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2643 gnat_entity);
2644
2645 /* We need special types for debugging information to point to
2646 the index types if they have variable bounds, are not integer
2647 types, are biased or are wider than sizetype. These are GNAT
2648 encodings, so we have to include them only when all encodings
2649 are requested. */
2650 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2651 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2652 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2653 || (TREE_TYPE (gnu_index_type)
2654 && TREE_CODE (TREE_TYPE (gnu_index_type))
2655 != INTEGER_TYPE)
2656 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2657 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2658 need_index_type_struct = true;
2659 }
2660
2661 /* Then flatten: create the array of arrays. For an array type
2662 used to implement a packed array, get the component type from
2663 the original array type since the representation clauses that
2664 can affect it are on the latter. */
2665 if (Is_Packed_Array_Impl_Type (gnat_entity)
2666 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2667 {
2668 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2669 for (index = ndim - 1; index >= 0; index--)
2670 gnu_type = TREE_TYPE (gnu_type);
2671
2672 /* One of the above calls might have caused us to be elaborated,
2673 so don't blow up if so. */
2674 if (present_gnu_tree (gnat_entity))
2675 {
2676 maybe_present = true;
2677 break;
2678 }
2679 }
2680 else
2681 {
2682 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2683 debug_info_p);
2684
2685 /* One of the above calls might have caused us to be elaborated,
2686 so don't blow up if so. */
2687 if (present_gnu_tree (gnat_entity))
2688 {
2689 maybe_present = true;
2690 break;
2691 }
2692 }
2693
2694 /* Compute the maximum size of the array in units. */
2695 if (gnu_max_size)
2696 gnu_max_size
2697 = size_binop (MULT_EXPR, gnu_max_size, TYPE_SIZE_UNIT (gnu_type));
2698
2699 /* Now build the array type. */
2700 for (index = ndim - 1; index >= 0; index --)
2701 {
2702 gnu_type = build_nonshared_array_type (gnu_type,
2703 gnu_index_types[index]);
2704 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2705 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2706 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2707 set_reverse_storage_order_on_array_type (gnu_type);
2708 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2709 set_nonaliased_component_on_array_type (gnu_type);
2710
2711 /* Kludge to remove the TREE_OVERFLOW flag for the sake of LTO
2712 on maximally-sized array types designed by access types. */
2713 if (integer_zerop (TYPE_SIZE (gnu_type))
2714 && TREE_OVERFLOW (TYPE_SIZE (gnu_type))
2715 && Is_Itype (gnat_entity)
2716 && (gnat_temp = Associated_Node_For_Itype (gnat_entity))
2717 && IN (Nkind (gnat_temp), N_Declaration)
2718 && Is_Access_Type (Defining_Entity (gnat_temp))
2719 && Is_Entity_Name (First_Index (gnat_entity))
2720 && UI_To_Int (RM_Size (Entity (First_Index (gnat_entity))))
2721 == BITS_PER_WORD)
2722 {
2723 TYPE_SIZE (gnu_type) = bitsize_zero_node;
2724 TYPE_SIZE_UNIT (gnu_type) = size_zero_node;
2725 }
2726 }
2727
2728 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2729 TYPE_STUB_DECL (gnu_type)
2730 = create_type_stub_decl (gnu_entity_name, gnu_type);
2731
2732 /* If this is a multi-dimensional array and we are at global level,
2733 we need to make a variable corresponding to the stride of the
2734 inner dimensions. */
2735 if (ndim > 1 && global_bindings_p ())
2736 {
2737 tree gnu_arr_type;
2738
2739 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2740 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2741 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2742 {
2743 tree eltype = TREE_TYPE (gnu_arr_type);
2744 char stride_name[32];
2745
2746 sprintf (stride_name, "ST%d", index);
2747 TYPE_SIZE (gnu_arr_type)
2748 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2749 gnat_entity, stride_name,
2750 definition, false);
2751
2752 /* ??? For now, store the size as a multiple of the
2753 alignment of the element type in bytes so that we
2754 can see the alignment from the tree. */
2755 sprintf (stride_name, "ST%d_A_UNIT", index);
2756 TYPE_SIZE_UNIT (gnu_arr_type)
2757 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2758 gnat_entity, stride_name,
2759 definition, false,
2760 TYPE_ALIGN (eltype));
2761
2762 /* ??? create_type_decl is not invoked on the inner types so
2763 the MULT_EXPR node built above will never be marked. */
2764 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2765 }
2766 }
2767
2768 /* Set the TYPE_PACKED flag on packed array types and also on their
2769 implementation types, so that the DWARF back-end can output the
2770 appropriate description for them. */
2771 TYPE_PACKED (gnu_type)
2772 = (Is_Packed (gnat_entity)
2773 || Is_Packed_Array_Impl_Type (gnat_entity));
2774
2775 TYPE_BIT_PACKED_ARRAY_TYPE_P (gnu_type)
2776 = (Is_Packed_Array_Impl_Type (gnat_entity)
2777 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2778
2779 /* If the maximum size doesn't overflow, use it. */
2780 if (gnu_max_size
2781 && TREE_CODE (gnu_max_size) == INTEGER_CST
2782 && !TREE_OVERFLOW (gnu_max_size)
2783 && compare_tree_int (gnu_max_size, TYPE_ARRAY_SIZE_LIMIT) <= 0)
2784 TYPE_ARRAY_MAX_SIZE (gnu_type) = gnu_max_size;
2785
2786 /* If we need to write out a record type giving the names of the
2787 bounds for debugging purposes, do it now and make the record
2788 type a parallel type. This is not needed for a packed array
2789 since the bounds are conveyed by the original array type. */
2790 if (need_index_type_struct
2791 && debug_info_p
2792 && !Is_Packed_Array_Impl_Type (gnat_entity))
2793 {
2794 tree gnu_bound_rec = make_node (RECORD_TYPE);
2795 tree gnu_field_list = NULL_TREE;
2796 tree gnu_field;
2797
2798 TYPE_NAME (gnu_bound_rec)
2799 = create_concat_name (gnat_entity, "XA");
2800
2801 for (index = ndim - 1; index >= 0; index--)
2802 {
2803 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2804 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2805
2806 /* Make sure to reference the types themselves, and not just
2807 their names, as the debugger may fall back on them. */
2808 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2809 gnu_bound_rec, NULL_TREE,
2810 NULL_TREE, 0, 0);
2811 DECL_CHAIN (gnu_field) = gnu_field_list;
2812 gnu_field_list = gnu_field;
2813 }
2814
2815 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2816 add_parallel_type (gnu_type, gnu_bound_rec);
2817 }
2818
2819 /* If this is a packed array type, make the original array type a
2820 parallel/debug type. Otherwise, if GNAT encodings are used, do
2821 it for the base array type if it is not artificial to make sure
2822 that it is kept in the debug info. */
2823 if (debug_info_p)
2824 {
2825 if (Is_Packed_Array_Impl_Type (gnat_entity))
2826 {
2827 tree gnu_name
2828 = associate_original_type_to_packed_array (gnu_type,
2829 gnat_entity);
2830 if (gnu_name)
2831 gnu_entity_name = gnu_name;
2832 }
2833
2834 else if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2835 {
2836 tree gnu_base_decl
2837 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2838 false);
2839
2840 if (!DECL_ARTIFICIAL (gnu_base_decl))
2841 add_parallel_type (gnu_type,
2842 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2843 }
2844 }
2845
2846 /* Set our alias set to that of our base type. This gives all
2847 array subtypes the same alias set. */
2848 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2849
2850 /* If this is a packed type implemented specially, then replace our
2851 type with the implementation type. */
2852 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2853 {
2854 /* First finish the type we had been making so that we output
2855 debugging information for it. */
2856 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2857 if (Treat_As_Volatile (gnat_entity))
2858 {
2859 const int quals
2860 = TYPE_QUAL_VOLATILE
2861 | (Is_Full_Access (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2862 gnu_type = change_qualified_type (gnu_type, quals);
2863 }
2864 /* Make it artificial only if the base type was artificial too.
2865 That's sort of "morally" true and will make it possible for
2866 the debugger to look it up by name in DWARF, which is needed
2867 in order to decode the packed array type. */
2868 tree gnu_tmp_decl
2869 = create_type_decl (gnu_entity_name, gnu_type,
2870 !Comes_From_Source (Etype (gnat_entity))
2871 && artificial_p, debug_info_p,
2872 gnat_entity);
2873 /* Save it as our equivalent in case the call below elaborates
2874 this type again. */
2875 save_gnu_tree (gnat_entity, gnu_tmp_decl, false);
2876
2877 gnu_type
2878 = gnat_to_gnu_type (Packed_Array_Impl_Type (gnat_entity));
2879 save_gnu_tree (gnat_entity, NULL_TREE, false);
2880
2881 /* Set the ___XP suffix for GNAT encodings. */
2882 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2883 gnu_entity_name = DECL_NAME (TYPE_NAME (gnu_type));
2884
2885 tree gnu_inner = gnu_type;
2886 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2887 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2888 || TYPE_PADDING_P (gnu_inner)))
2889 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2890
2891 /* We need to attach the index type to the type we just made so
2892 that the actual bounds can later be put into a template. */
2893 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2894 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2895 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2896 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2897 {
2898 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2899 {
2900 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2901 TYPE_MODULUS for modular types so we make an extra
2902 subtype if necessary. */
2903 if (TYPE_MODULAR_P (gnu_inner))
2904 gnu_inner
2905 = create_extra_subtype (gnu_inner,
2906 TYPE_MIN_VALUE (gnu_inner),
2907 TYPE_MAX_VALUE (gnu_inner));
2908
2909 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2910
2911 /* Check for other cases of overloading. */
2912 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2913 }
2914
2915 for (Entity_Id gnat_index = First_Index (gnat_entity);
2916 Present (gnat_index);
2917 gnat_index = Next_Index (gnat_index))
2918 SET_TYPE_ACTUAL_BOUNDS
2919 (gnu_inner,
2920 tree_cons (NULL_TREE,
2921 get_unpadded_type (Etype (gnat_index)),
2922 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2923
2924 if (Convention (gnat_entity) != Convention_Fortran)
2925 SET_TYPE_ACTUAL_BOUNDS
2926 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2927
2928 if (TREE_CODE (gnu_type) == RECORD_TYPE
2929 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2930 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2931 }
2932 }
2933 }
2934 break;
2935
2936 case E_String_Literal_Subtype:
2937 /* Create the type for a string literal. */
2938 {
2939 Entity_Id gnat_full_type
2940 = (Is_Private_Type (Etype (gnat_entity))
2941 && Present (Full_View (Etype (gnat_entity)))
2942 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2943 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2944 tree gnu_string_array_type
2945 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2946 tree gnu_string_index_type
2947 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2948 (TYPE_DOMAIN (gnu_string_array_type))));
2949 tree gnu_lower_bound
2950 = convert (gnu_string_index_type,
2951 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2952 tree gnu_length
2953 = UI_To_gnu (String_Literal_Length (gnat_entity),
2954 gnu_string_index_type);
2955 tree gnu_upper_bound
2956 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2957 gnu_lower_bound,
2958 int_const_binop (MINUS_EXPR, gnu_length,
2959 convert (gnu_string_index_type,
2960 integer_one_node)));
2961 tree gnu_index_type
2962 = create_index_type (convert (sizetype, gnu_lower_bound),
2963 convert (sizetype, gnu_upper_bound),
2964 create_range_type (gnu_string_index_type,
2965 gnu_lower_bound,
2966 gnu_upper_bound),
2967 gnat_entity);
2968
2969 gnu_type
2970 = build_nonshared_array_type (gnat_to_gnu_type
2971 (Component_Type (gnat_entity)),
2972 gnu_index_type);
2973 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2974 set_nonaliased_component_on_array_type (gnu_type);
2975 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2976 }
2977 break;
2978
2979 /* Record Types and Subtypes
2980
2981 A record type definition is transformed into the equivalent of a C
2982 struct definition. The fields that are the discriminants which are
2983 found in the Full_Type_Declaration node and the elements of the
2984 Component_List found in the Record_Type_Definition node. The
2985 Component_List can be a recursive structure since each Variant of
2986 the Variant_Part of the Component_List has a Component_List.
2987
2988 Processing of a record type definition comprises starting the list of
2989 field declarations here from the discriminants and the calling the
2990 function components_to_record to add the rest of the fields from the
2991 component list and return the gnu type node. The function
2992 components_to_record will call itself recursively as it traverses
2993 the tree. */
2994
2995 case E_Record_Type:
2996 {
2997 Node_Id record_definition = Type_Definition (gnat_decl);
2998
2999 if (Has_Complex_Representation (gnat_entity))
3000 {
3001 const Node_Id first_component
3002 = First (Component_Items (Component_List (record_definition)));
3003 tree gnu_component_type
3004 = get_unpadded_type (Etype (Defining_Entity (first_component)));
3005 gnu_type = build_complex_type (gnu_component_type);
3006 break;
3007 }
3008
3009 Node_Id gnat_constr;
3010 Entity_Id gnat_field, gnat_parent_type;
3011 tree gnu_field, gnu_field_list = NULL_TREE;
3012 tree gnu_get_parent;
3013 /* Set PACKED in keeping with gnat_to_gnu_field. */
3014 const int packed
3015 = Is_Packed (gnat_entity)
3016 ? 1
3017 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
3018 ? -1
3019 : 0;
3020 const bool has_align = Known_Alignment (gnat_entity);
3021 const bool has_discr = Has_Discriminants (gnat_entity);
3022 const bool is_extension
3023 = (Is_Tagged_Type (gnat_entity)
3024 && Nkind (record_definition) == N_Derived_Type_Definition);
3025 const bool has_rep
3026 = is_extension
3027 ? Has_Record_Rep_Clause (gnat_entity)
3028 : Has_Specified_Layout (gnat_entity);
3029 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
3030 bool all_rep = has_rep;
3031
3032 /* See if all fields have a rep clause. Stop when we find one
3033 that doesn't. */
3034 if (all_rep)
3035 for (gnat_field = First_Entity (gnat_entity);
3036 Present (gnat_field);
3037 gnat_field = Next_Entity (gnat_field))
3038 if ((Ekind (gnat_field) == E_Component
3039 || Ekind (gnat_field) == E_Discriminant)
3040 && No (Component_Clause (gnat_field)))
3041 {
3042 all_rep = false;
3043 break;
3044 }
3045
3046 /* If this is a record extension, go a level further to find the
3047 record definition. Also, verify we have a Parent_Subtype. */
3048 if (is_extension)
3049 {
3050 if (!type_annotate_only
3051 || Present (Record_Extension_Part (record_definition)))
3052 record_definition = Record_Extension_Part (record_definition);
3053
3054 gcc_assert (Present (Parent_Subtype (gnat_entity))
3055 || type_annotate_only);
3056 }
3057
3058 /* Make a node for the record type. */
3059 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3060 TYPE_NAME (gnu_type) = gnu_entity_name;
3061 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3062 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3063 = Reverse_Storage_Order (gnat_entity);
3064
3065 /* If the record type has discriminants, pointers to it may also point
3066 to constrained subtypes of it, so mark it as may_alias for LTO. */
3067 if (has_discr)
3068 prepend_one_attribute
3069 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3070 get_identifier ("may_alias"), NULL_TREE,
3071 gnat_entity);
3072
3073 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3074
3075 /* If we are not defining it, suppress expanding incomplete types. */
3076 if (!definition)
3077 {
3078 defer_incomplete_level++;
3079 this_deferred = true;
3080 }
3081
3082 /* If both a size and rep clause were specified, put the size on
3083 the record type now so that it can get the proper layout. */
3084 if (has_rep && Known_RM_Size (gnat_entity))
3085 TYPE_SIZE (gnu_type)
3086 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3087
3088 /* Always set the alignment on the record type here so that it can
3089 get the proper layout. */
3090 if (has_align)
3091 SET_TYPE_ALIGN (gnu_type,
3092 validate_alignment (Alignment (gnat_entity),
3093 gnat_entity, 0));
3094 else
3095 {
3096 SET_TYPE_ALIGN (gnu_type, 0);
3097
3098 /* If a type needs strict alignment, then its type size will also
3099 be the RM size (see below). Cap the alignment if needed, lest
3100 it may cause this type size to become too large. */
3101 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3102 {
3103 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3104 unsigned int max_align = max_size & -max_size;
3105 if (max_align < BIGGEST_ALIGNMENT)
3106 TYPE_MAX_ALIGN (gnu_type) = max_align;
3107 }
3108
3109 /* Similarly if an Object_Size clause has been specified. */
3110 else if (Known_Esize (gnat_entity))
3111 {
3112 unsigned int max_size = UI_To_Int (Esize (gnat_entity));
3113 unsigned int max_align = max_size & -max_size;
3114 if (max_align < BIGGEST_ALIGNMENT)
3115 TYPE_MAX_ALIGN (gnu_type) = max_align;
3116 }
3117 }
3118
3119 /* If we have a Parent_Subtype, make a field for the parent. If
3120 this record has rep clauses, force the position to zero. */
3121 if (Present (Parent_Subtype (gnat_entity)))
3122 {
3123 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3124 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3125 tree gnu_parent;
3126 int parent_packed = 0;
3127
3128 /* A major complexity here is that the parent subtype will
3129 reference our discriminants in its Stored_Constraint list.
3130 But those must reference the parent component of this record
3131 which is precisely of the parent subtype we have not built yet!
3132 To break the circle we first build a dummy COMPONENT_REF which
3133 represents the "get to the parent" operation and initialize
3134 each of those discriminants to a COMPONENT_REF of the above
3135 dummy parent referencing the corresponding discriminant of the
3136 base type of the parent subtype. */
3137 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3138 build0 (PLACEHOLDER_EXPR, gnu_type),
3139 build_decl (input_location,
3140 FIELD_DECL, NULL_TREE,
3141 gnu_dummy_parent_type),
3142 NULL_TREE);
3143
3144 if (has_discr)
3145 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3146 Present (gnat_field);
3147 gnat_field = Next_Stored_Discriminant (gnat_field))
3148 if (Present (Corresponding_Discriminant (gnat_field)))
3149 {
3150 tree gnu_field
3151 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3152 (gnat_field));
3153 save_gnu_tree
3154 (gnat_field,
3155 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3156 gnu_get_parent, gnu_field, NULL_TREE),
3157 true);
3158 }
3159
3160 /* Then we build the parent subtype. If it has discriminants but
3161 the type itself has unknown discriminants, this means that it
3162 doesn't contain information about how the discriminants are
3163 derived from those of the ancestor type, so it cannot be used
3164 directly. Instead it is built by cloning the parent subtype
3165 of the underlying record view of the type, for which the above
3166 derivation of discriminants has been made explicit. */
3167 if (Has_Discriminants (gnat_parent)
3168 && Has_Unknown_Discriminants (gnat_entity))
3169 {
3170 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3171
3172 /* If we are defining the type, the underlying record
3173 view must already have been elaborated at this point.
3174 Otherwise do it now as its parent subtype cannot be
3175 technically elaborated on its own. */
3176 if (definition)
3177 gcc_assert (present_gnu_tree (gnat_uview));
3178 else
3179 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3180
3181 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3182
3183 /* Substitute the "get to the parent" of the type for that
3184 of its underlying record view in the cloned type. */
3185 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3186 Present (gnat_field);
3187 gnat_field = Next_Stored_Discriminant (gnat_field))
3188 if (Present (Corresponding_Discriminant (gnat_field)))
3189 {
3190 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3191 tree gnu_ref
3192 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3193 gnu_get_parent, gnu_field, NULL_TREE);
3194 gnu_parent
3195 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3196 }
3197 }
3198 else
3199 gnu_parent = gnat_to_gnu_type (gnat_parent);
3200
3201 /* The parent field needs strict alignment so, if it is to
3202 be created with a component clause below, then we need
3203 to apply the same adjustment as in gnat_to_gnu_field. */
3204 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3205 {
3206 /* ??? For historical reasons, we do it on strict-alignment
3207 platforms only, where it is really required. This means
3208 that a confirming representation clause will change the
3209 behavior of the compiler on the other platforms. */
3210 if (STRICT_ALIGNMENT)
3211 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3212 else
3213 parent_packed
3214 = adjust_packed (gnu_parent, gnu_type, parent_packed);
3215 }
3216
3217 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3218 initially built. The discriminants must reference the fields
3219 of the parent subtype and not those of its base type for the
3220 placeholder machinery to properly work. */
3221 if (has_discr)
3222 {
3223 /* The actual parent subtype is the full view. */
3224 if (Is_Private_Type (gnat_parent))
3225 {
3226 if (Present (Full_View (gnat_parent)))
3227 gnat_parent = Full_View (gnat_parent);
3228 else
3229 gnat_parent = Underlying_Full_View (gnat_parent);
3230 }
3231
3232 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3233 Present (gnat_field);
3234 gnat_field = Next_Stored_Discriminant (gnat_field))
3235 if (Present (Corresponding_Discriminant (gnat_field)))
3236 {
3237 Entity_Id field;
3238 for (field = First_Stored_Discriminant (gnat_parent);
3239 Present (field);
3240 field = Next_Stored_Discriminant (field))
3241 if (same_discriminant_p (gnat_field, field))
3242 break;
3243 gcc_assert (Present (field));
3244 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3245 = gnat_to_gnu_field_decl (field);
3246 }
3247 }
3248
3249 /* The "get to the parent" COMPONENT_REF must be given its
3250 proper type... */
3251 TREE_TYPE (gnu_get_parent) = gnu_parent;
3252
3253 /* ...and reference the _Parent field of this record. */
3254 gnu_field
3255 = create_field_decl (parent_name_id,
3256 gnu_parent, gnu_type,
3257 has_rep
3258 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3259 has_rep
3260 ? bitsize_zero_node : NULL_TREE,
3261 parent_packed, 1);
3262 DECL_INTERNAL_P (gnu_field) = 1;
3263 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3264 TYPE_FIELDS (gnu_type) = gnu_field;
3265 }
3266
3267 /* Make the fields for the discriminants and put them into the record
3268 unless it's an Unchecked_Union. */
3269 if (has_discr)
3270 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3271 Present (gnat_field);
3272 gnat_field = Next_Stored_Discriminant (gnat_field))
3273 {
3274 /* If this is a record extension and this discriminant is the
3275 renaming of another discriminant, we've handled it above. */
3276 if (is_extension
3277 && Present (Corresponding_Discriminant (gnat_field)))
3278 continue;
3279
3280 gnu_field
3281 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3282 debug_info_p);
3283
3284 /* Make an expression using a PLACEHOLDER_EXPR from the
3285 FIELD_DECL node just created and link that with the
3286 corresponding GNAT defining identifier. */
3287 save_gnu_tree (gnat_field,
3288 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3289 build0 (PLACEHOLDER_EXPR, gnu_type),
3290 gnu_field, NULL_TREE),
3291 true);
3292
3293 if (!is_unchecked_union)
3294 {
3295 DECL_CHAIN (gnu_field) = gnu_field_list;
3296 gnu_field_list = gnu_field;
3297 }
3298 }
3299
3300 /* If we have a derived untagged type that renames discriminants in
3301 the parent type, the (stored) discriminants are just a copy of the
3302 discriminants of the parent type. This means that any constraints
3303 added by the renaming in the derivation are disregarded as far as
3304 the layout of the derived type is concerned. To rescue them, we
3305 change the type of the (stored) discriminants to a subtype with
3306 the bounds of the type of the visible discriminants. */
3307 if (has_discr
3308 && !is_extension
3309 && Stored_Constraint (gnat_entity) != No_Elist)
3310 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3311 gnat_constr != No_Elmt;
3312 gnat_constr = Next_Elmt (gnat_constr))
3313 if (Nkind (Node (gnat_constr)) == N_Identifier
3314 /* Ignore access discriminants. */
3315 && !Is_Access_Type (Etype (Node (gnat_constr)))
3316 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3317 {
3318 const Entity_Id gnat_discr = Entity (Node (gnat_constr));
3319 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3320 tree gnu_ref
3321 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3322 NULL_TREE, false);
3323
3324 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3325 just above for one of the stored discriminants. */
3326 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3327
3328 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3329 TREE_TYPE (gnu_ref)
3330 = create_extra_subtype (TREE_TYPE (gnu_ref),
3331 TYPE_MIN_VALUE (gnu_discr_type),
3332 TYPE_MAX_VALUE (gnu_discr_type));
3333 }
3334
3335 /* If this is a derived type with discriminants and these discriminants
3336 affect the initial shape it has inherited, factor them in. */
3337 if (has_discr
3338 && !is_extension
3339 && !Has_Record_Rep_Clause (gnat_entity)
3340 && Stored_Constraint (gnat_entity) != No_Elist
3341 && (gnat_parent_type = Underlying_Type (Etype (gnat_entity)))
3342 && Is_Record_Type (gnat_parent_type)
3343 && Is_Unchecked_Union (gnat_entity)
3344 == Is_Unchecked_Union (gnat_parent_type)
3345 && No_Reordering (gnat_entity) == No_Reordering (gnat_parent_type))
3346 {
3347 tree gnu_parent_type
3348 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type));
3349
3350 if (TYPE_IS_PADDING_P (gnu_parent_type))
3351 gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type));
3352
3353 vec<subst_pair> gnu_subst_list
3354 = build_subst_list (gnat_entity, gnat_parent_type, definition);
3355
3356 /* Set the layout of the type to match that of the parent type,
3357 doing required substitutions. If we are in minimal GNAT
3358 encodings mode, we don't need debug info for the inner record
3359 types, as they will be part of the embedding variant record's
3360 debug info. */
3361 copy_and_substitute_in_layout
3362 (gnat_entity, gnat_parent_type, gnu_type, gnu_parent_type,
3363 gnu_subst_list,
3364 debug_info_p && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL);
3365 }
3366 else
3367 {
3368 /* Add the fields into the record type and finish it up. */
3369 components_to_record (Component_List (record_definition),
3370 gnat_entity, gnu_field_list, gnu_type,
3371 packed, definition, false, all_rep,
3372 is_unchecked_union, artificial_p,
3373 debug_info_p, false,
3374 all_rep ? NULL_TREE : bitsize_zero_node,
3375 NULL);
3376
3377 /* Empty classes have the size of a storage unit in C++. */
3378 if (TYPE_SIZE (gnu_type) == bitsize_zero_node
3379 && Convention (gnat_entity) == Convention_CPP)
3380 {
3381 TYPE_SIZE (gnu_type) = bitsize_unit_node;
3382 TYPE_SIZE_UNIT (gnu_type) = size_one_node;
3383 compute_record_mode (gnu_type);
3384 }
3385
3386 /* If the type needs strict alignment, then no object of the type
3387 may have a size smaller than the natural size, which means that
3388 the RM size of the type is equal to the type size. */
3389 if (Strict_Alignment (gnat_entity))
3390 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3391
3392 /* If there are entities in the chain corresponding to components
3393 that we did not elaborate, ensure we elaborate their types if
3394 they are itypes. */
3395 for (gnat_temp = First_Entity (gnat_entity);
3396 Present (gnat_temp);
3397 gnat_temp = Next_Entity (gnat_temp))
3398 if ((Ekind (gnat_temp) == E_Component
3399 || Ekind (gnat_temp) == E_Discriminant)
3400 && Is_Itype (Etype (gnat_temp))
3401 && !present_gnu_tree (gnat_temp))
3402 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3403 }
3404
3405 /* Fill in locations of fields. */
3406 annotate_rep (gnat_entity, gnu_type);
3407
3408 /* If this is a record type associated with an exception definition,
3409 equate its fields to those of the standard exception type. This
3410 will make it possible to convert between them. */
3411 if (gnu_entity_name == exception_data_name_id)
3412 {
3413 tree gnu_std_field;
3414 for (gnu_field = TYPE_FIELDS (gnu_type),
3415 gnu_std_field = TYPE_FIELDS (except_type_node);
3416 gnu_field;
3417 gnu_field = DECL_CHAIN (gnu_field),
3418 gnu_std_field = DECL_CHAIN (gnu_std_field))
3419 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3420 gcc_assert (!gnu_std_field);
3421 }
3422 }
3423 break;
3424
3425 case E_Class_Wide_Subtype:
3426 /* If an equivalent type is present, that is what we should use.
3427 Otherwise, fall through to handle this like a record subtype
3428 since it may have constraints. */
3429 if (gnat_equiv_type != gnat_entity)
3430 {
3431 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3432 maybe_present = true;
3433 break;
3434 }
3435
3436 /* ... fall through ... */
3437
3438 case E_Record_Subtype:
3439 /* If Cloned_Subtype is Present it means this record subtype has
3440 identical layout to that type or subtype and we should use
3441 that GCC type for this one. The front-end guarantees that
3442 the component list is shared. */
3443 if (Present (Cloned_Subtype (gnat_entity)))
3444 {
3445 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3446 NULL_TREE, false);
3447 gnat_annotate_type = Cloned_Subtype (gnat_entity);
3448 maybe_present = true;
3449 break;
3450 }
3451
3452 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3453 changing the type, make a new type with each field having the type of
3454 the field in the new subtype but the position computed by transforming
3455 every discriminant reference according to the constraints. We don't
3456 see any difference between private and non-private type here since
3457 derivations from types should have been deferred until the completion
3458 of the private type. */
3459 else
3460 {
3461 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3462
3463 if (!definition)
3464 {
3465 defer_incomplete_level++;
3466 this_deferred = true;
3467 }
3468
3469 tree gnu_base_type
3470 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3471
3472 if (present_gnu_tree (gnat_entity))
3473 {
3474 maybe_present = true;
3475 break;
3476 }
3477
3478 /* When the subtype has discriminants and these discriminants affect
3479 the initial shape it has inherited, factor them in. But for an
3480 Unchecked_Union (it must be an itype), just return the type. */
3481 if (Has_Discriminants (gnat_entity)
3482 && Stored_Constraint (gnat_entity) != No_Elist
3483 && Is_Record_Type (gnat_base_type)
3484 && !Is_Unchecked_Union (gnat_base_type))
3485 {
3486 vec<subst_pair> gnu_subst_list
3487 = build_subst_list (gnat_entity, gnat_base_type, definition);
3488 tree gnu_unpad_base_type;
3489
3490 gnu_type = make_node (RECORD_TYPE);
3491 TYPE_NAME (gnu_type) = gnu_entity_name;
3492 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3493 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3494 = Reverse_Storage_Order (gnat_entity);
3495 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3496
3497 /* Set the size, alignment and alias set of the type to match
3498 those of the base type, doing required substitutions. */
3499 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3500 gnu_subst_list);
3501
3502 if (TYPE_IS_PADDING_P (gnu_base_type))
3503 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3504 else
3505 gnu_unpad_base_type = gnu_base_type;
3506
3507 /* Set the layout of the type to match that of the base type,
3508 doing required substitutions. We will output debug info
3509 manually below so pass false as last argument. */
3510 copy_and_substitute_in_layout (gnat_entity, gnat_base_type,
3511 gnu_type, gnu_unpad_base_type,
3512 gnu_subst_list, false);
3513
3514 /* Fill in locations of fields. */
3515 annotate_rep (gnat_entity, gnu_type);
3516
3517 /* If debugging information is being written for the type and if
3518 we are asked to output such encodings, write a record that
3519 shows what we are a subtype of and also make a variable that
3520 indicates our size, if still variable. */
3521 if (debug_info_p
3522 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3523 {
3524 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3525 tree gnu_unpad_base_name
3526 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3527 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3528
3529 TYPE_NAME (gnu_subtype_marker)
3530 = create_concat_name (gnat_entity, "XVS");
3531 finish_record_type (gnu_subtype_marker,
3532 create_field_decl (gnu_unpad_base_name,
3533 build_reference_type
3534 (gnu_unpad_base_type),
3535 gnu_subtype_marker,
3536 NULL_TREE, NULL_TREE,
3537 0, 0),
3538 0, true);
3539
3540 add_parallel_type (gnu_type, gnu_subtype_marker);
3541
3542 if (definition
3543 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3544 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3545 TYPE_SIZE_UNIT (gnu_subtype_marker)
3546 = create_var_decl (create_concat_name (gnat_entity,
3547 "XVZ"),
3548 NULL_TREE, sizetype, gnu_size_unit,
3549 false, false, false, false, false,
3550 true, debug_info_p,
3551 NULL, gnat_entity);
3552 }
3553
3554 /* Or else, if the subtype is artificial and encodings are not
3555 used, use the base record type as the debug type. */
3556 else if (debug_info_p
3557 && artificial_p
3558 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3559 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_unpad_base_type);
3560 }
3561
3562 /* Otherwise, go down all the components in the new type and make
3563 them equivalent to those in the base type. */
3564 else
3565 {
3566 gnu_type = gnu_base_type;
3567
3568 for (gnat_temp = First_Entity (gnat_entity);
3569 Present (gnat_temp);
3570 gnat_temp = Next_Entity (gnat_temp))
3571 if ((Ekind (gnat_temp) == E_Discriminant
3572 && !Is_Unchecked_Union (gnat_base_type))
3573 || Ekind (gnat_temp) == E_Component)
3574 save_gnu_tree (gnat_temp,
3575 gnat_to_gnu_field_decl
3576 (Original_Record_Component (gnat_temp)),
3577 false);
3578 }
3579 }
3580 break;
3581
3582 case E_Access_Subprogram_Type:
3583 case E_Anonymous_Access_Subprogram_Type:
3584 /* Use the special descriptor type for dispatch tables if needed,
3585 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3586 Note that we are only required to do so for static tables in
3587 order to be compatible with the C++ ABI, but Ada 2005 allows
3588 to extend library level tagged types at the local level so
3589 we do it in the non-static case as well. */
3590 if (TARGET_VTABLE_USES_DESCRIPTORS
3591 && Is_Dispatch_Table_Entity (gnat_entity))
3592 {
3593 gnu_type = fdesc_type_node;
3594 gnu_size = TYPE_SIZE (gnu_type);
3595 break;
3596 }
3597
3598 /* ... fall through ... */
3599
3600 case E_Allocator_Type:
3601 case E_Access_Type:
3602 case E_Access_Attribute_Type:
3603 case E_Anonymous_Access_Type:
3604 case E_General_Access_Type:
3605 {
3606 /* The designated type and its equivalent type for gigi. */
3607 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3608 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3609 /* Whether it comes from a limited with. */
3610 const bool is_from_limited_with
3611 = (Is_Incomplete_Type (gnat_desig_equiv)
3612 && From_Limited_With (gnat_desig_equiv));
3613 /* Whether it is a completed Taft Amendment type. Such a type is to
3614 be treated as coming from a limited with clause if it is not in
3615 the main unit, i.e. we break potential circularities here in case
3616 the body of an external unit is loaded for inter-unit inlining. */
3617 const bool is_completed_taft_type
3618 = (Is_Incomplete_Type (gnat_desig_equiv)
3619 && Has_Completion_In_Body (gnat_desig_equiv)
3620 && Present (Full_View (gnat_desig_equiv)));
3621 /* The "full view" of the designated type. If this is an incomplete
3622 entity from a limited with, treat its non-limited view as the full
3623 view. Otherwise, if this is an incomplete or private type, use the
3624 full view. In the former case, we might point to a private type,
3625 in which case, we need its full view. Also, we want to look at the
3626 actual type used for the representation, so this takes a total of
3627 three steps. */
3628 Entity_Id gnat_desig_full_direct_first
3629 = (is_from_limited_with
3630 ? Non_Limited_View (gnat_desig_equiv)
3631 : (Is_Incomplete_Or_Private_Type (gnat_desig_equiv)
3632 ? Full_View (gnat_desig_equiv) : Empty));
3633 Entity_Id gnat_desig_full_direct
3634 = ((is_from_limited_with
3635 && Present (gnat_desig_full_direct_first)
3636 && Is_Private_Type (gnat_desig_full_direct_first))
3637 ? Full_View (gnat_desig_full_direct_first)
3638 : gnat_desig_full_direct_first);
3639 Entity_Id gnat_desig_full
3640 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3641 /* The type actually used to represent the designated type, either
3642 gnat_desig_full or gnat_desig_equiv. */
3643 Entity_Id gnat_desig_rep;
3644 /* We want to know if we'll be seeing the freeze node for any
3645 incomplete type we may be pointing to. */
3646 const bool in_main_unit
3647 = (Present (gnat_desig_full)
3648 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3649 : In_Extended_Main_Code_Unit (gnat_desig_type));
3650 /* True if we make a dummy type here. */
3651 bool made_dummy = false;
3652 /* The mode to be used for the pointer type. */
3653 scalar_int_mode p_mode;
3654 /* The GCC type used for the designated type. */
3655 tree gnu_desig_type = NULL_TREE;
3656
3657 if (!int_mode_for_size (esize, 0).exists (&p_mode)
3658 || !targetm.valid_pointer_mode (p_mode))
3659 p_mode = ptr_mode;
3660
3661 /* If either the designated type or its full view is an unconstrained
3662 array subtype, replace it with the type it's a subtype of. This
3663 avoids problems with multiple copies of unconstrained array types.
3664 Likewise, if the designated type is a subtype of an incomplete
3665 record type, use the parent type to avoid order of elaboration
3666 issues. This can lose some code efficiency, but there is no
3667 alternative. */
3668 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3669 && !Is_Constrained (gnat_desig_equiv))
3670 gnat_desig_equiv = Etype (gnat_desig_equiv);
3671 if (Present (gnat_desig_full)
3672 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3673 && !Is_Constrained (gnat_desig_full))
3674 || (Ekind (gnat_desig_full) == E_Record_Subtype
3675 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3676 gnat_desig_full = Etype (gnat_desig_full);
3677
3678 /* Set the type that's the representation of the designated type. */
3679 gnat_desig_rep
3680 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3681
3682 /* If we already know what the full type is, use it. */
3683 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3684 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3685
3686 /* Get the type of the thing we are to point to and build a pointer to
3687 it. If it is a reference to an incomplete or private type with a
3688 full view that is a record, an array or an access, make a dummy type
3689 and get the actual type later when we have verified it is safe. */
3690 else if ((!in_main_unit
3691 && !present_gnu_tree (gnat_desig_equiv)
3692 && Present (gnat_desig_full)
3693 && (Is_Record_Type (gnat_desig_full)
3694 || Is_Array_Type (gnat_desig_full)
3695 || Is_Access_Type (gnat_desig_full)))
3696 /* Likewise if this is a reference to a record, an array or a
3697 subprogram type and we are to defer elaborating incomplete
3698 types. We do this because this access type may be the full
3699 view of a private type. */
3700 || ((!in_main_unit || imported_p)
3701 && defer_incomplete_level != 0
3702 && !present_gnu_tree (gnat_desig_equiv)
3703 && (Is_Record_Type (gnat_desig_rep)
3704 || Is_Array_Type (gnat_desig_rep)
3705 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3706 /* If this is a reference from a limited_with type back to our
3707 main unit and there's a freeze node for it, either we have
3708 already processed the declaration and made the dummy type,
3709 in which case we just reuse the latter, or we have not yet,
3710 in which case we make the dummy type and it will be reused
3711 when the declaration is finally processed. In both cases,
3712 the pointer eventually created below will be automatically
3713 adjusted when the freeze node is processed. */
3714 || (in_main_unit
3715 && is_from_limited_with
3716 && Present (Freeze_Node (gnat_desig_rep))))
3717 {
3718 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3719 made_dummy = true;
3720 }
3721
3722 /* Otherwise handle the case of a pointer to itself. */
3723 else if (gnat_desig_equiv == gnat_entity)
3724 {
3725 gnu_type
3726 = build_pointer_type_for_mode (void_type_node, p_mode,
3727 No_Strict_Aliasing (gnat_entity));
3728 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3729 }
3730
3731 /* If expansion is disabled, the equivalent type of a concurrent type
3732 is absent, so we use the void pointer type. */
3733 else if (type_annotate_only && No (gnat_desig_equiv))
3734 gnu_type = ptr_type_node;
3735
3736 /* If the ultimately designated type is an incomplete type with no full
3737 view, we use the void pointer type in LTO mode to avoid emitting a
3738 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3739 the name of the dummy type in used by GDB for a global lookup. */
3740 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3741 && No (Full_View (gnat_desig_rep))
3742 && flag_generate_lto)
3743 gnu_type = ptr_type_node;
3744
3745 /* Finally, handle the default case where we can just elaborate our
3746 designated type. */
3747 else
3748 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3749
3750 /* It is possible that a call to gnat_to_gnu_type above resolved our
3751 type. If so, just return it. */
3752 if (present_gnu_tree (gnat_entity))
3753 {
3754 maybe_present = true;
3755 break;
3756 }
3757
3758 /* Access-to-unconstrained-array types need a special treatment. */
3759 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3760 {
3761 /* If the processing above got something that has a pointer, then
3762 we are done. This could have happened either because the type
3763 was elaborated or because somebody else executed the code. */
3764 if (!TYPE_POINTER_TO (gnu_desig_type))
3765 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3766
3767 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3768 }
3769
3770 /* If we haven't done it yet, build the pointer type the usual way. */
3771 else if (!gnu_type)
3772 {
3773 /* Modify the designated type if we are pointing only to constant
3774 objects, but don't do it for a dummy type. */
3775 if (Is_Access_Constant (gnat_entity)
3776 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3777 gnu_desig_type
3778 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3779
3780 gnu_type
3781 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3782 No_Strict_Aliasing (gnat_entity));
3783 }
3784
3785 /* If the designated type is not declared in the main unit and we made
3786 a dummy node for it, save our definition, elaborate the actual type
3787 and replace the dummy type we made with the actual one. But if we
3788 are to defer actually looking up the actual type, make an entry in
3789 the deferred list instead. If this is from a limited with, we may
3790 have to defer until the end of the current unit. */
3791 if (!in_main_unit && made_dummy)
3792 {
3793 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3794 gnu_type
3795 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3796
3797 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3798 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3799 artificial_p, debug_info_p,
3800 gnat_entity);
3801 this_made_decl = true;
3802 gnu_type = TREE_TYPE (gnu_decl);
3803 save_gnu_tree (gnat_entity, gnu_decl, false);
3804 saved = true;
3805
3806 if (defer_incomplete_level == 0
3807 && !is_from_limited_with
3808 && !is_completed_taft_type)
3809 {
3810 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3811 gnat_to_gnu_type (gnat_desig_equiv));
3812 }
3813 else
3814 {
3815 struct incomplete *p = XNEW (struct incomplete);
3816 struct incomplete **head
3817 = (is_from_limited_with || is_completed_taft_type
3818 ? &defer_limited_with_list : &defer_incomplete_list);
3819
3820 p->old_type = gnu_desig_type;
3821 p->full_type = gnat_desig_equiv;
3822 p->next = *head;
3823 *head = p;
3824 }
3825 }
3826 }
3827 break;
3828
3829 case E_Access_Protected_Subprogram_Type:
3830 case E_Anonymous_Access_Protected_Subprogram_Type:
3831 /* If we are just annotating types and have no equivalent record type,
3832 just use the void pointer type. */
3833 if (type_annotate_only && gnat_equiv_type == gnat_entity)
3834 gnu_type = ptr_type_node;
3835
3836 /* The run-time representation is the equivalent type. */
3837 else
3838 {
3839 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3840 maybe_present = true;
3841 }
3842
3843 /* The designated subtype must be elaborated as well, if it does
3844 not have its own freeze node. */
3845 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3846 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3847 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3848 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3849 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3850 NULL_TREE, false);
3851
3852 break;
3853
3854 case E_Access_Subtype:
3855 /* We treat this as identical to its base type; any constraint is
3856 meaningful only to the front-end. */
3857 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3858 maybe_present = true;
3859
3860 /* The designated subtype must be elaborated as well, if it does
3861 not have its own freeze node. But designated subtypes created
3862 for constrained components of records with discriminants are
3863 not frozen by the front-end and not elaborated here, because
3864 their use may appear before the base type is frozen and it is
3865 not clear that they are needed in gigi. With the current model,
3866 there is no correct place where they could be elaborated. */
3867 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3868 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3869 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3870 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3871 {
3872 /* If we are to defer elaborating incomplete types, make a dummy
3873 type node and elaborate it later. */
3874 if (defer_incomplete_level != 0)
3875 {
3876 struct incomplete *p = XNEW (struct incomplete);
3877
3878 p->old_type
3879 = make_dummy_type (Directly_Designated_Type (gnat_entity));
3880 p->full_type = Directly_Designated_Type (gnat_entity);
3881 p->next = defer_incomplete_list;
3882 defer_incomplete_list = p;
3883 }
3884 else if (!Is_Incomplete_Or_Private_Type
3885 (Base_Type (Directly_Designated_Type (gnat_entity))))
3886 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3887 NULL_TREE, false);
3888 }
3889 break;
3890
3891 /* Subprogram Entities
3892
3893 The following access functions are defined for subprograms:
3894
3895 Etype Return type or Standard_Void_Type.
3896 First_Formal The first formal parameter.
3897 Is_Imported Indicates that the subprogram has appeared in
3898 an INTERFACE or IMPORT pragma. For now we
3899 assume that the external language is C.
3900 Is_Exported Likewise but for an EXPORT pragma.
3901 Is_Inlined True if the subprogram is to be inlined.
3902
3903 Each parameter is first checked by calling must_pass_by_ref on its
3904 type to determine if it is passed by reference. For parameters which
3905 are copied in, if they are Ada In Out or Out parameters, their return
3906 value becomes part of a record which becomes the return type of the
3907 function (C function - note that this applies only to Ada procedures
3908 so there is no Ada return type). Additional code to store back the
3909 parameters will be generated on the caller side. This transformation
3910 is done here, not in the front-end.
3911
3912 The intended result of the transformation can be seen from the
3913 equivalent source rewritings that follow:
3914
3915 struct temp {int a,b};
3916 procedure P (A,B: In Out ...) is temp P (int A,B)
3917 begin {
3918 .. ..
3919 end P; return {A,B};
3920 }
3921
3922 temp t;
3923 P(X,Y); t = P(X,Y);
3924 X = t.a , Y = t.b;
3925
3926 For subprogram types we need to perform mainly the same conversions to
3927 GCC form that are needed for procedures and function declarations. The
3928 only difference is that at the end, we make a type declaration instead
3929 of a function declaration. */
3930
3931 case E_Subprogram_Type:
3932 case E_Function:
3933 case E_Procedure:
3934 {
3935 tree gnu_ext_name
3936 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
3937 const enum inline_status_t inline_status
3938 = inline_status_for_subprog (gnat_entity);
3939 bool public_flag = Is_Public (gnat_entity) || imported_p;
3940 /* Subprograms marked both Intrinsic and Always_Inline need not
3941 have a body of their own. */
3942 bool extern_flag
3943 = ((Is_Public (gnat_entity) && !definition)
3944 || imported_p
3945 || (Convention (gnat_entity) == Convention_Intrinsic
3946 && Has_Pragma_Inline_Always (gnat_entity)));
3947 tree gnu_param_list;
3948
3949 /* A parameter may refer to this type, so defer completion of any
3950 incomplete types. */
3951 if (kind == E_Subprogram_Type && !definition)
3952 {
3953 defer_incomplete_level++;
3954 this_deferred = true;
3955 }
3956
3957 /* If the subprogram has an alias, it is probably inherited, so
3958 we can use the original one. If the original "subprogram"
3959 is actually an enumeration literal, it may be the first use
3960 of its type, so we must elaborate that type now. */
3961 if (Present (Alias (gnat_entity)))
3962 {
3963 const Entity_Id gnat_alias = Alias (gnat_entity);
3964
3965 if (Ekind (gnat_alias) == E_Enumeration_Literal)
3966 gnat_to_gnu_entity (Etype (gnat_alias), NULL_TREE, false);
3967
3968 gnu_decl = gnat_to_gnu_entity (gnat_alias, gnu_expr, false);
3969
3970 /* Elaborate any itypes in the parameters of this entity. */
3971 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3972 Present (gnat_temp);
3973 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3974 if (Is_Itype (Etype (gnat_temp)))
3975 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3976
3977 /* Materialize renamed subprograms in the debugging information
3978 when the renamed object is known at compile time; we consider
3979 such renamings as imported declarations.
3980
3981 Because the parameters in generic instantiations are generally
3982 materialized as renamings, we often end up having both the
3983 renamed subprogram and the renaming in the same context and with
3984 the same name; in this case, renaming is both useless debug-wise
3985 and potentially harmful as name resolution in the debugger could
3986 return twice the same entity! So avoid this case. */
3987 if (debug_info_p
3988 && !artificial_p
3989 && (Ekind (gnat_alias) == E_Function
3990 || Ekind (gnat_alias) == E_Procedure)
3991 && !(get_debug_scope (gnat_entity, NULL)
3992 == get_debug_scope (gnat_alias, NULL)
3993 && Name_Equals (Chars (gnat_entity), Chars (gnat_alias)))
3994 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
3995 {
3996 tree decl = build_decl (input_location, IMPORTED_DECL,
3997 gnu_entity_name, void_type_node);
3998 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
3999 gnat_pushdecl (decl, gnat_entity);
4000 }
4001
4002 break;
4003 }
4004
4005 /* Get the GCC tree for the (underlying) subprogram type. If the
4006 entity is an actual subprogram, also get the parameter list. */
4007 gnu_type
4008 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
4009 &gnu_param_list);
4010 if (DECL_P (gnu_type))
4011 {
4012 gnu_decl = gnu_type;
4013 gnu_type = TREE_TYPE (gnu_decl);
4014 break;
4015 }
4016
4017 /* Deal with platform-specific calling conventions. */
4018 if (Has_Stdcall_Convention (gnat_entity))
4019 prepend_one_attribute
4020 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4021 get_identifier ("stdcall"), NULL_TREE,
4022 gnat_entity);
4023
4024 /* If we should request stack realignment for a foreign convention
4025 subprogram, do so. Note that this applies to task entry points
4026 in particular. */
4027 if (FOREIGN_FORCE_REALIGN_STACK && foreign)
4028 prepend_one_attribute
4029 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4030 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4031 gnat_entity);
4032
4033 /* Deal with a pragma Linker_Section on a subprogram. */
4034 if ((kind == E_Function || kind == E_Procedure)
4035 && Present (Linker_Section_Pragma (gnat_entity)))
4036 prepend_one_attribute_pragma (&attr_list,
4037 Linker_Section_Pragma (gnat_entity));
4038
4039 /* If we are defining the subprogram and it has an Address clause
4040 we must get the address expression from the saved GCC tree for the
4041 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4042 the address expression here since the front-end has guaranteed
4043 in that case that the elaboration has no effects. If there is
4044 an Address clause and we are not defining the object, just
4045 make it a constant. */
4046 if (Present (Address_Clause (gnat_entity)))
4047 {
4048 tree gnu_address = NULL_TREE;
4049
4050 if (definition)
4051 gnu_address
4052 = (present_gnu_tree (gnat_entity)
4053 ? get_gnu_tree (gnat_entity)
4054 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4055
4056 save_gnu_tree (gnat_entity, NULL_TREE, false);
4057
4058 /* Convert the type of the object to a reference type that can
4059 alias everything as per RM 13.3(19). */
4060 gnu_type
4061 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4062 if (gnu_address)
4063 gnu_address = convert (gnu_type, gnu_address);
4064
4065 gnu_decl
4066 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4067 gnu_address, false, Is_Public (gnat_entity),
4068 extern_flag, false, false, artificial_p,
4069 debug_info_p, NULL, gnat_entity);
4070 DECL_BY_REF_P (gnu_decl) = 1;
4071 }
4072
4073 /* If this is a mere subprogram type, just create the declaration. */
4074 else if (kind == E_Subprogram_Type)
4075 {
4076 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4077
4078 gnu_decl
4079 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4080 debug_info_p, gnat_entity);
4081 }
4082
4083 /* Otherwise create the subprogram declaration with the external name,
4084 the type and the parameter list. However, if this a reference to
4085 the allocation routines, reuse the canonical declaration nodes as
4086 they come with special properties. */
4087 else
4088 {
4089 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4090 gnu_decl = malloc_decl;
4091 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4092 gnu_decl = realloc_decl;
4093 else
4094 {
4095 gnu_decl
4096 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4097 gnu_type, gnu_param_list,
4098 inline_status, public_flag,
4099 extern_flag, artificial_p,
4100 debug_info_p,
4101 definition && imported_p, attr_list,
4102 gnat_entity);
4103
4104 DECL_STUBBED_P (gnu_decl)
4105 = (Convention (gnat_entity) == Convention_Stubbed);
4106 }
4107 }
4108 }
4109 break;
4110
4111 case E_Incomplete_Type:
4112 case E_Incomplete_Subtype:
4113 case E_Private_Type:
4114 case E_Private_Subtype:
4115 case E_Limited_Private_Type:
4116 case E_Limited_Private_Subtype:
4117 case E_Record_Type_With_Private:
4118 case E_Record_Subtype_With_Private:
4119 {
4120 const bool is_from_limited_with
4121 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4122 /* Get the "full view" of this entity. If this is an incomplete
4123 entity from a limited with, treat its non-limited view as the
4124 full view. Otherwise, use either the full view or the underlying
4125 full view, whichever is present. This is used in all the tests
4126 below. */
4127 const Entity_Id full_view
4128 = is_from_limited_with
4129 ? Non_Limited_View (gnat_entity)
4130 : Present (Full_View (gnat_entity))
4131 ? Full_View (gnat_entity)
4132 : IN (kind, Private_Kind)
4133 ? Underlying_Full_View (gnat_entity)
4134 : Empty;
4135
4136 /* If this is an incomplete type with no full view, it must be a Taft
4137 Amendment type or an incomplete type coming from a limited context,
4138 in which cases we return a dummy type. Otherwise, we just get the
4139 type from its Etype. */
4140 if (No (full_view))
4141 {
4142 if (kind == E_Incomplete_Type)
4143 {
4144 gnu_type = make_dummy_type (gnat_entity);
4145 gnu_decl = TYPE_STUB_DECL (gnu_type);
4146 }
4147 else
4148 {
4149 gnu_decl
4150 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4151 maybe_present = true;
4152 }
4153 }
4154
4155 /* Or else, if we already made a type for the full view, reuse it. */
4156 else if (present_gnu_tree (full_view))
4157 gnu_decl = get_gnu_tree (full_view);
4158
4159 /* Or else, if we are not defining the type or there is no freeze
4160 node on it, get the type for the full view. Likewise if this is
4161 a limited_with'ed type not declared in the main unit, which can
4162 happen for incomplete formal types instantiated on a type coming
4163 from a limited_with clause. */
4164 else if (!definition
4165 || No (Freeze_Node (full_view))
4166 || (is_from_limited_with
4167 && !In_Extended_Main_Code_Unit (full_view)))
4168 {
4169 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4170 maybe_present = true;
4171 }
4172
4173 /* Otherwise, make a dummy type entry which will be replaced later.
4174 Save it as the full declaration's type so we can do any needed
4175 updates when we see it. */
4176 else
4177 {
4178 gnu_type = make_dummy_type (gnat_entity);
4179 gnu_decl = TYPE_STUB_DECL (gnu_type);
4180 if (Has_Completion_In_Body (gnat_entity))
4181 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4182 save_gnu_tree (full_view, gnu_decl, false);
4183 }
4184 }
4185 break;
4186
4187 case E_Class_Wide_Type:
4188 /* Class-wide types are always transformed into their root type. */
4189 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4190 maybe_present = true;
4191 break;
4192
4193 case E_Protected_Type:
4194 case E_Protected_Subtype:
4195 case E_Task_Type:
4196 case E_Task_Subtype:
4197 /* If we are just annotating types and have no equivalent record type,
4198 just return void_type, except for root types that have discriminants
4199 because the discriminants will very likely be used in the declarative
4200 part of the associated body so they need to be translated. */
4201 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4202 {
4203 if (definition
4204 && Has_Discriminants (gnat_entity)
4205 && Root_Type (gnat_entity) == gnat_entity)
4206 {
4207 tree gnu_field_list = NULL_TREE;
4208 Entity_Id gnat_field;
4209
4210 /* This is a minimal version of the E_Record_Type handling. */
4211 gnu_type = make_node (RECORD_TYPE);
4212 TYPE_NAME (gnu_type) = gnu_entity_name;
4213
4214 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4215 Present (gnat_field);
4216 gnat_field = Next_Stored_Discriminant (gnat_field))
4217 {
4218 tree gnu_field
4219 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4220 definition, debug_info_p);
4221
4222 save_gnu_tree (gnat_field,
4223 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4224 build0 (PLACEHOLDER_EXPR, gnu_type),
4225 gnu_field, NULL_TREE),
4226 true);
4227
4228 DECL_CHAIN (gnu_field) = gnu_field_list;
4229 gnu_field_list = gnu_field;
4230 }
4231
4232 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4233 false);
4234 }
4235 else
4236 gnu_type = void_type_node;
4237 }
4238
4239 /* Concurrent types are always transformed into their record type. */
4240 else
4241 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4242 maybe_present = true;
4243 break;
4244
4245 case E_Label:
4246 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4247 break;
4248
4249 case E_Block:
4250 case E_Loop:
4251 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4252 we've already saved it, so we don't try to. */
4253 gnu_decl = error_mark_node;
4254 saved = true;
4255 break;
4256
4257 case E_Abstract_State:
4258 /* This is a SPARK annotation that only reaches here when compiling in
4259 ASIS mode. */
4260 gcc_assert (type_annotate_only);
4261 gnu_decl = error_mark_node;
4262 saved = true;
4263 break;
4264
4265 default:
4266 gcc_unreachable ();
4267 }
4268
4269 /* If we had a case where we evaluated another type and it might have
4270 defined this one, handle it here. */
4271 if (maybe_present && present_gnu_tree (gnat_entity))
4272 {
4273 gnu_decl = get_gnu_tree (gnat_entity);
4274 saved = true;
4275 }
4276
4277 /* If we are processing a type and there is either no DECL for it or
4278 we just made one, do some common processing for the type, such as
4279 handling alignment and possible padding. */
4280 if (is_type && (!gnu_decl || this_made_decl))
4281 {
4282 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4283
4284 /* Process the attributes, if not already done. Note that the type is
4285 already defined so we cannot pass true for IN_PLACE here. */
4286 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4287
4288 /* See if a size was specified, by means of either an Object_Size or
4289 a regular Size clause, and validate it if so.
4290
4291 ??? Don't set the size for a String_Literal since it is either
4292 confirming or we don't handle it properly (if the low bound is
4293 non-constant). */
4294 if (!gnu_size && kind != E_String_Literal_Subtype)
4295 {
4296 if (Known_Esize (gnat_entity))
4297 gnu_size
4298 = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4299 VAR_DECL, false, false, NULL, NULL);
4300 else
4301 gnu_size
4302 = validate_size (RM_Size (gnat_entity), gnu_type, gnat_entity,
4303 TYPE_DECL, false, Has_Size_Clause (gnat_entity),
4304 NULL, NULL);
4305 }
4306
4307 /* If a size was specified, see if we can make a new type of that size
4308 by rearranging the type, for example from a fat to a thin pointer. */
4309 if (gnu_size)
4310 {
4311 gnu_type
4312 = make_type_from_size (gnu_type, gnu_size,
4313 Has_Biased_Representation (gnat_entity));
4314
4315 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4316 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4317 gnu_size = NULL_TREE;
4318 }
4319
4320 /* If the alignment has not already been processed and this is not
4321 an unconstrained array type, see if an alignment is specified.
4322 If not, we pick a default alignment for atomic objects. */
4323 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4324 ;
4325 else if (Known_Alignment (gnat_entity))
4326 {
4327 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4328 TYPE_ALIGN (gnu_type));
4329
4330 /* Warn on suspiciously large alignments. This should catch
4331 errors about the (alignment,byte)/(size,bit) discrepancy. */
4332 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4333 {
4334 tree size;
4335
4336 /* If a size was specified, take it into account. Otherwise
4337 use the RM size for records or unions as the type size has
4338 already been adjusted to the alignment. */
4339 if (gnu_size)
4340 size = gnu_size;
4341 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4342 && !TYPE_FAT_POINTER_P (gnu_type))
4343 size = rm_size (gnu_type);
4344 else
4345 size = TYPE_SIZE (gnu_type);
4346
4347 /* Consider an alignment as suspicious if the alignment/size
4348 ratio is greater or equal to the byte/bit ratio. */
4349 if (tree_fits_uhwi_p (size)
4350 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4351 post_error_ne ("?suspiciously large alignment specified for&",
4352 Expression (Alignment_Clause (gnat_entity)),
4353 gnat_entity);
4354 }
4355 }
4356 else if (Is_Full_Access (gnat_entity) && !gnu_size
4357 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4358 && integer_pow2p (TYPE_SIZE (gnu_type)))
4359 align = MIN (BIGGEST_ALIGNMENT,
4360 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4361 else if (Is_Full_Access (gnat_entity) && gnu_size
4362 && tree_fits_uhwi_p (gnu_size)
4363 && integer_pow2p (gnu_size))
4364 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4365
4366 /* See if we need to pad the type. If we did and built a new type,
4367 then create a stripped-down declaration for the original type,
4368 mainly for debugging, unless there was already one. */
4369 if (gnu_size || align > 0)
4370 {
4371 tree orig_type = gnu_type;
4372
4373 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4374 false, definition, false);
4375
4376 if (gnu_type != orig_type && !gnu_decl)
4377 create_type_decl (gnu_entity_name, orig_type, true, debug_info_p,
4378 gnat_entity);
4379 }
4380
4381 /* Now set the RM size of the type. We cannot do it before padding
4382 because we need to accept arbitrary RM sizes on integral types. */
4383 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4384
4385 /* Back-annotate the alignment of the type if not already set. */
4386 if (Unknown_Alignment (gnat_entity))
4387 {
4388 unsigned int double_align, align;
4389 bool is_capped_double, align_clause;
4390
4391 /* If the default alignment of "double" or larger scalar types is
4392 specifically capped and this is not an array with an alignment
4393 clause on the component type, return the cap. */
4394 if ((double_align = double_float_alignment) > 0)
4395 is_capped_double
4396 = is_double_float_or_array (gnat_entity, &align_clause);
4397 else if ((double_align = double_scalar_alignment) > 0)
4398 is_capped_double
4399 = is_double_scalar_or_array (gnat_entity, &align_clause);
4400 else
4401 is_capped_double = align_clause = false;
4402
4403 if (is_capped_double && !align_clause)
4404 align = double_align;
4405 else
4406 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4407
4408 Set_Alignment (gnat_entity, UI_From_Int (align));
4409 }
4410
4411 /* Likewise for the size, if any. */
4412 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4413 {
4414 tree gnu_size = TYPE_SIZE (gnu_type);
4415
4416 /* If the size is self-referential, annotate the maximum value
4417 after saturating it, if need be, to avoid a No_Uint value. */
4418 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4419 {
4420 const unsigned int align
4421 = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
4422 gnu_size
4423 = maybe_saturate_size (max_size (gnu_size, true), align);
4424 }
4425
4426 /* If we are just annotating types and the type is tagged, the tag
4427 and the parent components are not generated by the front-end so
4428 alignment and sizes must be adjusted. */
4429 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4430 {
4431 const bool derived_p = Is_Derived_Type (gnat_entity);
4432 const Entity_Id gnat_parent
4433 = derived_p ? Etype (Base_Type (gnat_entity)) : Empty;
4434 const unsigned int inherited_align
4435 = derived_p
4436 ? UI_To_Int (Alignment (gnat_parent)) * BITS_PER_UNIT
4437 : POINTER_SIZE;
4438 const unsigned int align
4439 = MAX (TYPE_ALIGN (gnu_type), inherited_align);
4440
4441 Set_Alignment (gnat_entity, UI_From_Int (align / BITS_PER_UNIT));
4442
4443 /* If there is neither size clause nor representation clause, the
4444 sizes need to be adjusted. */
4445 if (Unknown_RM_Size (gnat_entity)
4446 && !VOID_TYPE_P (gnu_type)
4447 && (!TYPE_FIELDS (gnu_type)
4448 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4449 {
4450 tree offset
4451 = derived_p
4452 ? UI_To_gnu (Esize (gnat_parent), bitsizetype)
4453 : bitsize_int (POINTER_SIZE);
4454 if (TYPE_FIELDS (gnu_type))
4455 offset
4456 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4457 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4458 }
4459
4460 gnu_size
4461 = maybe_saturate_size (round_up (gnu_size, align), align);
4462 Set_Esize (gnat_entity, annotate_value (gnu_size));
4463
4464 /* Tagged types are Strict_Alignment so RM_Size = Esize. */
4465 if (Unknown_RM_Size (gnat_entity))
4466 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4467 }
4468
4469 /* Otherwise no adjustment is needed. */
4470 else
4471 Set_Esize (gnat_entity, annotate_value (gnu_size));
4472 }
4473
4474 /* Likewise for the RM size, if any. */
4475 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4476 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4477
4478 /* If we are at global level, GCC will have applied variable_size to
4479 the type, but that won't have done anything. So, if it's not
4480 a constant or self-referential, call elaborate_expression_1 to
4481 make a variable for the size rather than calculating it each time.
4482 Handle both the RM size and the actual size. */
4483 if (TYPE_SIZE (gnu_type)
4484 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4485 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4486 && global_bindings_p ())
4487 {
4488 tree size = TYPE_SIZE (gnu_type);
4489
4490 TYPE_SIZE (gnu_type)
4491 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4492 false);
4493
4494 /* ??? For now, store the size as a multiple of the alignment in
4495 bytes so that we can see the alignment from the tree. */
4496 TYPE_SIZE_UNIT (gnu_type)
4497 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4498 "SIZE_A_UNIT", definition, false,
4499 TYPE_ALIGN (gnu_type));
4500
4501 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4502 may not be marked by the call to create_type_decl below. */
4503 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4504
4505 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4506 {
4507 tree variant_part = get_variant_part (gnu_type);
4508 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4509
4510 if (variant_part)
4511 {
4512 tree union_type = TREE_TYPE (variant_part);
4513 tree offset = DECL_FIELD_OFFSET (variant_part);
4514
4515 /* If the position of the variant part is constant, subtract
4516 it from the size of the type of the parent to get the new
4517 size. This manual CSE reduces the data size. */
4518 if (TREE_CODE (offset) == INTEGER_CST)
4519 {
4520 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4521 TYPE_SIZE (union_type)
4522 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4523 bit_from_pos (offset, bitpos));
4524 TYPE_SIZE_UNIT (union_type)
4525 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4526 byte_from_pos (offset, bitpos));
4527 }
4528 else
4529 {
4530 TYPE_SIZE (union_type)
4531 = elaborate_expression_1 (TYPE_SIZE (union_type),
4532 gnat_entity, "VSIZE",
4533 definition, false);
4534
4535 /* ??? For now, store the size as a multiple of the
4536 alignment in bytes so that we can see the alignment
4537 from the tree. */
4538 TYPE_SIZE_UNIT (union_type)
4539 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4540 gnat_entity, "VSIZE_A_UNIT",
4541 definition, false,
4542 TYPE_ALIGN (union_type));
4543
4544 /* ??? For now, store the offset as a multiple of the
4545 alignment in bytes so that we can see the alignment
4546 from the tree. */
4547 DECL_FIELD_OFFSET (variant_part)
4548 = elaborate_expression_2 (offset, gnat_entity,
4549 "VOFFSET", definition, false,
4550 DECL_OFFSET_ALIGN
4551 (variant_part));
4552 }
4553
4554 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4555 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4556 }
4557
4558 if (operand_equal_p (ada_size, size, 0))
4559 ada_size = TYPE_SIZE (gnu_type);
4560 else
4561 ada_size
4562 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4563 definition, false);
4564 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4565 }
4566 }
4567
4568 /* Similarly, if this is a record type or subtype at global level, call
4569 elaborate_expression_2 on any field position. Skip any fields that
4570 we haven't made trees for to avoid problems with class-wide types. */
4571 if (Is_In_Record_Kind (kind) && global_bindings_p ())
4572 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4573 gnat_temp = Next_Entity (gnat_temp))
4574 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4575 {
4576 tree gnu_field = get_gnu_tree (gnat_temp);
4577
4578 /* ??? For now, store the offset as a multiple of the alignment
4579 in bytes so that we can see the alignment from the tree. */
4580 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4581 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4582 {
4583 DECL_FIELD_OFFSET (gnu_field)
4584 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4585 gnat_temp, "OFFSET", definition,
4586 false,
4587 DECL_OFFSET_ALIGN (gnu_field));
4588
4589 /* ??? The context of gnu_field is not necessarily gnu_type
4590 so the MULT_EXPR node built above may not be marked by
4591 the call to create_type_decl below. */
4592 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4593 }
4594 }
4595
4596 /* Now check if the type allows atomic access. */
4597 if (Is_Full_Access (gnat_entity))
4598 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4599
4600 /* If this is not an unconstrained array type, set some flags. */
4601 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4602 {
4603 /* Record the property that objects of tagged types are guaranteed to
4604 be properly aligned. This is necessary because conversions to the
4605 class-wide type are translated into conversions to the root type,
4606 which can be less aligned than some of its derived types. */
4607 if (Is_Tagged_Type (gnat_entity)
4608 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4609 TYPE_ALIGN_OK (gnu_type) = 1;
4610
4611 /* Record whether the type is passed by reference. */
4612 if (Is_By_Reference_Type (gnat_entity) && !VOID_TYPE_P (gnu_type))
4613 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4614
4615 /* Record whether an alignment clause was specified. */
4616 if (Present (Alignment_Clause (gnat_entity)))
4617 TYPE_USER_ALIGN (gnu_type) = 1;
4618
4619 /* Record whether a pragma Universal_Aliasing was specified. */
4620 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4621 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4622
4623 /* If it is passed by reference, force BLKmode to ensure that
4624 objects of this type will always be put in memory. */
4625 if (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type))
4626 SET_TYPE_MODE (gnu_type, BLKmode);
4627 }
4628
4629 /* If this is a derived type, relate its alias set to that of its parent
4630 to avoid troubles when a call to an inherited primitive is inlined in
4631 a context where a derived object is accessed. The inlined code works
4632 on the parent view so the resulting code may access the same object
4633 using both the parent and the derived alias sets, which thus have to
4634 conflict. As the same issue arises with component references, the
4635 parent alias set also has to conflict with composite types enclosing
4636 derived components. For instance, if we have:
4637
4638 type D is new T;
4639 type R is record
4640 Component : D;
4641 end record;
4642
4643 we want T to conflict with both D and R, in addition to R being a
4644 superset of D by record/component construction.
4645
4646 One way to achieve this is to perform an alias set copy from the
4647 parent to the derived type. This is not quite appropriate, though,
4648 as we don't want separate derived types to conflict with each other:
4649
4650 type I1 is new Integer;
4651 type I2 is new Integer;
4652
4653 We want I1 and I2 to both conflict with Integer but we do not want
4654 I1 to conflict with I2, and an alias set copy on derivation would
4655 have that effect.
4656
4657 The option chosen is to make the alias set of the derived type a
4658 superset of that of its parent type. It trivially fulfills the
4659 simple requirement for the Integer derivation example above, and
4660 the component case as well by superset transitivity:
4661
4662 superset superset
4663 R ----------> D ----------> T
4664
4665 However, for composite types, conversions between derived types are
4666 translated into VIEW_CONVERT_EXPRs so a sequence like:
4667
4668 type Comp1 is new Comp;
4669 type Comp2 is new Comp;
4670 procedure Proc (C : Comp1);
4671
4672 C : Comp2;
4673 Proc (Comp1 (C));
4674
4675 is translated into:
4676
4677 C : Comp2;
4678 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4679
4680 and gimplified into:
4681
4682 C : Comp2;
4683 Comp1 *C.0;
4684 C.0 = (Comp1 *) &C;
4685 Proc (C.0);
4686
4687 i.e. generates code involving type punning. Therefore, Comp1 needs
4688 to conflict with Comp2 and an alias set copy is required.
4689
4690 The language rules ensure the parent type is already frozen here. */
4691 if (kind != E_Subprogram_Type
4692 && Is_Derived_Type (gnat_entity)
4693 && !type_annotate_only)
4694 {
4695 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4696 /* For constrained packed array subtypes, the implementation type is
4697 used instead of the nominal type. */
4698 if (kind == E_Array_Subtype
4699 && Is_Constrained (gnat_entity)
4700 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4701 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4702 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4703 Is_Composite_Type (gnat_entity)
4704 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4705 }
4706
4707 /* Finally get to the appropriate variant, except for the implementation
4708 type of a packed array because the GNU type might be further adjusted
4709 when the original array type is itself processed. */
4710 if (Treat_As_Volatile (gnat_entity)
4711 && !Is_Packed_Array_Impl_Type (gnat_entity))
4712 {
4713 const int quals
4714 = TYPE_QUAL_VOLATILE
4715 | (Is_Full_Access (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4716 /* This is required by free_lang_data_in_type to disable the ODR. */
4717 if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
4718 TYPE_STUB_DECL (gnu_type)
4719 = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type);
4720 gnu_type = change_qualified_type (gnu_type, quals);
4721 }
4722
4723 /* If we already made a decl, just set the type, otherwise create it. */
4724 if (gnu_decl)
4725 {
4726 TREE_TYPE (gnu_decl) = gnu_type;
4727 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4728 }
4729 else
4730 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4731 debug_info_p, gnat_entity);
4732 }
4733
4734 /* Otherwise, for a type reusing an existing DECL, back-annotate values. */
4735 else if (is_type
4736 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
4737 && Present (gnat_annotate_type))
4738 {
4739 if (Unknown_Alignment (gnat_entity))
4740 Set_Alignment (gnat_entity, Alignment (gnat_annotate_type));
4741 if (Unknown_Esize (gnat_entity))
4742 Set_Esize (gnat_entity, Esize (gnat_annotate_type));
4743 if (Unknown_RM_Size (gnat_entity))
4744 Set_RM_Size (gnat_entity, RM_Size (gnat_annotate_type));
4745 }
4746
4747 /* If we haven't already, associate the ..._DECL node that we just made with
4748 the input GNAT entity node. */
4749 if (!saved)
4750 save_gnu_tree (gnat_entity, gnu_decl, false);
4751
4752 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4753 eliminate as many deferred computations as possible. */
4754 process_deferred_decl_context (false);
4755
4756 /* If this is an enumeration or floating-point type, we were not able to set
4757 the bounds since they refer to the type. These are always static. */
4758 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4759 || (kind == E_Floating_Point_Type))
4760 {
4761 tree gnu_scalar_type = gnu_type;
4762 tree gnu_low_bound, gnu_high_bound;
4763
4764 /* If this is a padded type, we need to use the underlying type. */
4765 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4766 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4767
4768 /* If this is a floating point type and we haven't set a floating
4769 point type yet, use this in the evaluation of the bounds. */
4770 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4771 longest_float_type_node = gnu_scalar_type;
4772
4773 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4774 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4775
4776 if (kind == E_Enumeration_Type)
4777 {
4778 /* Enumeration types have specific RM bounds. */
4779 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4780 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4781 }
4782 else
4783 {
4784 /* Floating-point types don't have specific RM bounds. */
4785 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4786 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4787 }
4788 }
4789
4790 /* If we deferred processing of incomplete types, re-enable it. If there
4791 were no other disables and we have deferred types to process, do so. */
4792 if (this_deferred
4793 && --defer_incomplete_level == 0
4794 && defer_incomplete_list)
4795 {
4796 struct incomplete *p, *next;
4797
4798 /* We are back to level 0 for the deferring of incomplete types.
4799 But processing these incomplete types below may itself require
4800 deferring, so preserve what we have and restart from scratch. */
4801 p = defer_incomplete_list;
4802 defer_incomplete_list = NULL;
4803
4804 for (; p; p = next)
4805 {
4806 next = p->next;
4807
4808 if (p->old_type)
4809 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4810 gnat_to_gnu_type (p->full_type));
4811 free (p);
4812 }
4813 }
4814
4815 /* If we are not defining this type, see if it's on one of the lists of
4816 incomplete types. If so, handle the list entry now. */
4817 if (is_type && !definition)
4818 {
4819 struct incomplete *p;
4820
4821 for (p = defer_incomplete_list; p; p = p->next)
4822 if (p->old_type && p->full_type == gnat_entity)
4823 {
4824 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4825 TREE_TYPE (gnu_decl));
4826 p->old_type = NULL_TREE;
4827 }
4828
4829 for (p = defer_limited_with_list; p; p = p->next)
4830 if (p->old_type
4831 && (Non_Limited_View (p->full_type) == gnat_entity
4832 || Full_View (p->full_type) == gnat_entity))
4833 {
4834 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4835 TREE_TYPE (gnu_decl));
4836 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4837 update_profiles_with (p->old_type);
4838 p->old_type = NULL_TREE;
4839 }
4840 }
4841
4842 if (this_global)
4843 force_global--;
4844
4845 /* If this is a packed array type whose original array type is itself
4846 an itype without freeze node, make sure the latter is processed. */
4847 if (Is_Packed_Array_Impl_Type (gnat_entity)
4848 && Is_Itype (Original_Array_Type (gnat_entity))
4849 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4850 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4851 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
4852
4853 return gnu_decl;
4854 }
4855
4856 /* Similar, but if the returned value is a COMPONENT_REF, return the
4857 FIELD_DECL. */
4858
4859 tree
4860 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4861 {
4862 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4863
4864 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4865 gnu_field = TREE_OPERAND (gnu_field, 1);
4866
4867 return gnu_field;
4868 }
4869
4870 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4871 the GCC type corresponding to that entity. */
4872
4873 tree
4874 gnat_to_gnu_type (Entity_Id gnat_entity)
4875 {
4876 tree gnu_decl;
4877
4878 /* The back end never attempts to annotate generic types. */
4879 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4880 return void_type_node;
4881
4882 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4883 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4884
4885 return TREE_TYPE (gnu_decl);
4886 }
4887
4888 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4889 the unpadded version of the GCC type corresponding to that entity. */
4890
4891 tree
4892 get_unpadded_type (Entity_Id gnat_entity)
4893 {
4894 tree type = gnat_to_gnu_type (gnat_entity);
4895
4896 if (TYPE_IS_PADDING_P (type))
4897 type = TREE_TYPE (TYPE_FIELDS (type));
4898
4899 return type;
4900 }
4901
4902 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
4903 a C++ imported method or equivalent.
4904
4905 We use the predicate to find out whether we need to use METHOD_TYPE instead
4906 of FUNCTION_TYPE for GNAT_ENTITY for the sake compatibility with C++. This
4907 in turn determines whether the "thiscall" calling convention is used by the
4908 back-end for GNAT_ENTITY on 32-bit x86/Windows. */
4909
4910 static bool
4911 is_cplusplus_method (Entity_Id gnat_entity)
4912 {
4913 /* A constructor is a method on the C++ side. We deal with it now because
4914 it is declared without the 'this' parameter in the sources and, although
4915 the front-end will create a version with the 'this' parameter for code
4916 generation purposes, we want to return true for both versions. */
4917 if (Is_Constructor (gnat_entity))
4918 return true;
4919
4920 /* Check that the subprogram has C++ convention. */
4921 if (Convention (gnat_entity) != Convention_CPP)
4922 return false;
4923
4924 /* And that the type of the first parameter (indirectly) has it too, but
4925 we make an exception for Interfaces because they need not be imported. */
4926 Entity_Id gnat_first = First_Formal (gnat_entity);
4927 if (No (gnat_first))
4928 return false;
4929 Entity_Id gnat_type = Etype (gnat_first);
4930 if (Is_Access_Type (gnat_type))
4931 gnat_type = Directly_Designated_Type (gnat_type);
4932 if (Convention (gnat_type) != Convention_CPP && !Is_Interface (gnat_type))
4933 return false;
4934
4935 /* This is the main case: a C++ virtual method imported as a primitive
4936 operation of a tagged type. */
4937 if (Is_Dispatching_Operation (gnat_entity))
4938 return true;
4939
4940 /* This is set on the E_Subprogram_Type built for a dispatching call. */
4941 if (Is_Dispatch_Table_Entity (gnat_entity))
4942 return true;
4943
4944 /* A thunk needs to be handled like its associated primitive operation. */
4945 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
4946 return true;
4947
4948 /* Now on to the annoying case: a C++ non-virtual method, imported either
4949 as a non-primitive operation of a tagged type or as a primitive operation
4950 of an untagged type. We cannot reliably differentiate these cases from
4951 their static member or regular function equivalents in Ada, so we ask
4952 the C++ side through the mangled name of the function, as the implicit
4953 'this' parameter is not encoded in the mangled name of a method. */
4954 if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity)))
4955 {
4956 String_Pointer sp = { NULL, NULL };
4957 Get_External_Name (gnat_entity, false, sp);
4958
4959 void *mem;
4960 struct demangle_component *cmp
4961 = cplus_demangle_v3_components (Name_Buffer,
4962 DMGL_GNU_V3
4963 | DMGL_TYPES
4964 | DMGL_PARAMS
4965 | DMGL_RET_DROP,
4966 &mem);
4967 if (!cmp)
4968 return false;
4969
4970 /* We need to release MEM once we have a successful demangling. */
4971 bool ret = false;
4972
4973 if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME
4974 && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE
4975 && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL
4976 && cmp->type == DEMANGLE_COMPONENT_ARGLIST)
4977 {
4978 /* Make sure there is at least one parameter in C++ too. */
4979 if (cmp->u.s_binary.left)
4980 {
4981 unsigned int n_ada_args = 0;
4982 do {
4983 n_ada_args++;
4984 gnat_first = Next_Formal (gnat_first);
4985 } while (Present (gnat_first));
4986
4987 unsigned int n_cpp_args = 0;
4988 do {
4989 n_cpp_args++;
4990 cmp = cmp->u.s_binary.right;
4991 } while (cmp);
4992
4993 if (n_cpp_args < n_ada_args)
4994 ret = true;
4995 }
4996 else
4997 ret = true;
4998 }
4999
5000 free (mem);
5001
5002 return ret;
5003 }
5004
5005 return false;
5006 }
5007
5008 /* Return the inlining status of the GNAT subprogram SUBPROG. */
5009
5010 static enum inline_status_t
5011 inline_status_for_subprog (Entity_Id subprog)
5012 {
5013 if (Has_Pragma_No_Inline (subprog))
5014 return is_suppressed;
5015
5016 if (Has_Pragma_Inline_Always (subprog))
5017 return is_required;
5018
5019 if (Is_Inlined (subprog))
5020 {
5021 tree gnu_type;
5022
5023 /* This is a kludge to work around a pass ordering issue: for small
5024 record types with many components, i.e. typically bit-fields, the
5025 initialization routine can contain many assignments that will be
5026 merged by the GIMPLE store merging pass. But this pass runs very
5027 late in the pipeline, in particular after the inlining decisions
5028 are made, so the inlining heuristics cannot take its outcome into
5029 account. Therefore, we optimistically override the heuristics for
5030 the initialization routine in this case. */
5031 if (Is_Init_Proc (subprog)
5032 && flag_store_merging
5033 && Is_Record_Type (Etype (First_Formal (subprog)))
5034 && (gnu_type = gnat_to_gnu_type (Etype (First_Formal (subprog))))
5035 && !TYPE_IS_BY_REFERENCE_P (gnu_type)
5036 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
5037 && compare_tree_int (TYPE_SIZE (gnu_type), MAX_FIXED_MODE_SIZE) <= 0)
5038 return is_prescribed;
5039
5040 return is_requested;
5041 }
5042
5043 return is_default;
5044 }
5045
5046 /* Finalize the processing of From_Limited_With incomplete types. */
5047
5048 void
5049 finalize_from_limited_with (void)
5050 {
5051 struct incomplete *p, *next;
5052
5053 p = defer_limited_with_list;
5054 defer_limited_with_list = NULL;
5055
5056 for (; p; p = next)
5057 {
5058 next = p->next;
5059
5060 if (p->old_type)
5061 {
5062 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5063 gnat_to_gnu_type (p->full_type));
5064 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
5065 update_profiles_with (p->old_type);
5066 }
5067
5068 free (p);
5069 }
5070 }
5071
5072 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind
5073 of type (such E_Task_Type) that has a different type which Gigi uses
5074 for its representation. If the type does not have a special type for
5075 its representation, return GNAT_ENTITY. */
5076
5077 Entity_Id
5078 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5079 {
5080 Entity_Id gnat_equiv = gnat_entity;
5081
5082 if (No (gnat_entity))
5083 return gnat_entity;
5084
5085 switch (Ekind (gnat_entity))
5086 {
5087 case E_Class_Wide_Subtype:
5088 if (Present (Equivalent_Type (gnat_entity)))
5089 gnat_equiv = Equivalent_Type (gnat_entity);
5090 break;
5091
5092 case E_Access_Protected_Subprogram_Type:
5093 case E_Anonymous_Access_Protected_Subprogram_Type:
5094 if (Present (Equivalent_Type (gnat_entity)))
5095 gnat_equiv = Equivalent_Type (gnat_entity);
5096 break;
5097
5098 case E_Access_Subtype:
5099 gnat_equiv = Etype (gnat_entity);
5100 break;
5101
5102 case E_Array_Subtype:
5103 if (!Is_Constrained (gnat_entity))
5104 gnat_equiv = Etype (gnat_entity);
5105 break;
5106
5107 case E_Class_Wide_Type:
5108 gnat_equiv = Root_Type (gnat_entity);
5109 break;
5110
5111 case E_Protected_Type:
5112 case E_Protected_Subtype:
5113 case E_Task_Type:
5114 case E_Task_Subtype:
5115 if (Present (Corresponding_Record_Type (gnat_entity)))
5116 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5117 break;
5118
5119 default:
5120 break;
5121 }
5122
5123 return gnat_equiv;
5124 }
5125
5126 /* Return a GCC tree for a type corresponding to the component type of the
5127 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5128 is for an array being defined. DEBUG_INFO_P is true if we need to write
5129 debug information for other types that we may create in the process. */
5130
5131 static tree
5132 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5133 bool debug_info_p)
5134 {
5135 const Entity_Id gnat_type = Component_Type (gnat_array);
5136 const bool is_bit_packed = Is_Bit_Packed_Array (gnat_array);
5137 tree gnu_type = gnat_to_gnu_type (gnat_type);
5138 tree gnu_comp_size;
5139 bool has_packed_components;
5140 unsigned int max_align;
5141
5142 /* If an alignment is specified, use it as a cap on the component type
5143 so that it can be honored for the whole type, but ignore it for the
5144 original type of packed array types. */
5145 if (No (Packed_Array_Impl_Type (gnat_array))
5146 && Known_Alignment (gnat_array))
5147 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5148 else
5149 max_align = 0;
5150
5151 /* Try to get a packable form of the component if needed. */
5152 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5153 && !is_bit_packed
5154 && !Has_Aliased_Components (gnat_array)
5155 && !Strict_Alignment (gnat_type)
5156 && RECORD_OR_UNION_TYPE_P (gnu_type)
5157 && !TYPE_FAT_POINTER_P (gnu_type)
5158 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5159 {
5160 gnu_type = make_packable_type (gnu_type, false, max_align);
5161 has_packed_components = true;
5162 }
5163 else
5164 has_packed_components = is_bit_packed;
5165
5166 /* Get and validate any specified Component_Size. */
5167 gnu_comp_size
5168 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5169 has_packed_components ? TYPE_DECL : VAR_DECL, true,
5170 Has_Component_Size_Clause (gnat_array), NULL, NULL);
5171
5172 /* If the component type is a RECORD_TYPE that has a self-referential size,
5173 then use the maximum size for the component size. */
5174 if (!gnu_comp_size
5175 && TREE_CODE (gnu_type) == RECORD_TYPE
5176 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5177 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5178
5179 /* If the array has aliased components and the component size is zero, force
5180 the unit size to ensure that the components have distinct addresses. */
5181 if (!gnu_comp_size
5182 && Has_Aliased_Components (gnat_array)
5183 && integer_zerop (TYPE_SIZE (gnu_type)))
5184 gnu_comp_size = bitsize_unit_node;
5185
5186 /* Honor the component size. This is not needed for bit-packed arrays. */
5187 if (gnu_comp_size && !is_bit_packed)
5188 {
5189 tree orig_type = gnu_type;
5190 unsigned int gnu_comp_align;
5191
5192 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5193 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5194 gnu_type = orig_type;
5195 else
5196 orig_type = gnu_type;
5197
5198 /* We need to make sure that the size is a multiple of the alignment.
5199 But we do not misalign the component type because of the alignment
5200 of the array type here; this either must have been done earlier in
5201 the packed case or should be rejected in the non-packed case. */
5202 if (TREE_CODE (gnu_comp_size) == INTEGER_CST)
5203 {
5204 const unsigned HOST_WIDE_INT int_size = tree_to_uhwi (gnu_comp_size);
5205 gnu_comp_align = int_size & -int_size;
5206 if (gnu_comp_align > TYPE_ALIGN (gnu_type))
5207 gnu_comp_align = 0;
5208 }
5209 else
5210 gnu_comp_align = 0;
5211
5212 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, gnu_comp_align,
5213 gnat_array, true, definition, true);
5214
5215 /* If a padding record was made, declare it now since it will never be
5216 declared otherwise. This is necessary to ensure that its subtrees
5217 are properly marked. */
5218 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5219 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5220 gnat_array);
5221 }
5222
5223 /* This is a very special case where the array has aliased components and the
5224 component size might be zero at run time. As explained above, we force at
5225 least the unit size but we don't want to build a distinct padding type for
5226 each invocation (they are not canonicalized if they have variable size) so
5227 we cache this special padding type as TYPE_PADDING_FOR_COMPONENT. */
5228 else if (Has_Aliased_Components (gnat_array)
5229 && TREE_CODE (gnu_type) == ARRAY_TYPE
5230 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))
5231 {
5232 if (TYPE_PADDING_FOR_COMPONENT (gnu_type))
5233 gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type);
5234 else
5235 {
5236 gnu_comp_size
5237 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5238 TYPE_PADDING_FOR_COMPONENT (gnu_type)
5239 = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5240 true, definition, true);
5241 gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type);
5242 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5243 gnat_array);
5244 }
5245 }
5246
5247 /* Now check if the type of the component allows atomic access. */
5248 if (Has_Atomic_Components (gnat_array) || Is_Full_Access (gnat_type))
5249 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5250
5251 /* If the component type is a padded type made for a non-bit-packed array
5252 of scalars with reverse storage order, we need to propagate the reverse
5253 storage order to the padding type since it is the innermost enclosing
5254 aggregate type around the scalar. */
5255 if (TYPE_IS_PADDING_P (gnu_type)
5256 && !is_bit_packed
5257 && Reverse_Storage_Order (gnat_array)
5258 && Is_Scalar_Type (gnat_type))
5259 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5260
5261 if (Has_Volatile_Components (gnat_array))
5262 {
5263 const int quals
5264 = TYPE_QUAL_VOLATILE
5265 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5266 gnu_type = change_qualified_type (gnu_type, quals);
5267 }
5268
5269 return gnu_type;
5270 }
5271
5272 /* Return whether TYPE requires that formal parameters of TYPE be initialized
5273 when they are Out parameters passed by copy.
5274
5275 This just implements the set of conditions listed in RM 6.4.1(12). */
5276
5277 static bool
5278 type_requires_init_of_formal (Entity_Id type)
5279 {
5280 type = Underlying_Type (type);
5281
5282 if (Is_Access_Type (type))
5283 return true;
5284
5285 if (Is_Scalar_Type (type))
5286 return Has_Default_Aspect (type);
5287
5288 if (Is_Array_Type (type))
5289 return Has_Default_Aspect (type)
5290 || type_requires_init_of_formal (Component_Type (type));
5291
5292 if (Is_Record_Type (type))
5293 for (Entity_Id field = First_Entity (type);
5294 Present (field);
5295 field = Next_Entity (field))
5296 {
5297 if (Ekind (field) == E_Discriminant && !Is_Unchecked_Union (type))
5298 return true;
5299
5300 if (Ekind (field) == E_Component
5301 && (Present (Expression (Parent (field)))
5302 || type_requires_init_of_formal (Etype (field))))
5303 return true;
5304 }
5305
5306 return false;
5307 }
5308
5309 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5310 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5311 the type of the parameter. FIRST is true if this is the first parameter in
5312 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5313 the copy-in copy-out implementation mechanism.
5314
5315 The returned tree is a PARM_DECL, except for the cases where no parameter
5316 needs to be actually passed to the subprogram; the type of this "shadow"
5317 parameter is then returned instead. */
5318
5319 static tree
5320 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5321 Entity_Id gnat_subprog, bool *cico)
5322 {
5323 Mechanism_Type mech = Mechanism (gnat_param);
5324 tree gnu_param_name = get_entity_name (gnat_param);
5325 bool foreign = Has_Foreign_Convention (gnat_subprog);
5326 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5327 /* The parameter can be indirectly modified if its address is taken. */
5328 bool ro_param = in_param && !Address_Taken (gnat_param);
5329 bool by_return = false, by_component_ptr = false;
5330 bool by_ref = false;
5331 bool forced_by_ref = false;
5332 bool restricted_aliasing_p = false;
5333 location_t saved_location = input_location;
5334 tree gnu_param;
5335
5336 /* Make sure to use the proper SLOC for vector ABI warnings. */
5337 if (VECTOR_TYPE_P (gnu_param_type))
5338 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5339
5340 /* Builtins are expanded inline and there is no real call sequence involved.
5341 So the type expected by the underlying expander is always the type of the
5342 argument "as is". */
5343 if (Convention (gnat_subprog) == Convention_Intrinsic
5344 && Present (Interface_Name (gnat_subprog)))
5345 mech = By_Copy;
5346
5347 /* Handle the first parameter of a valued procedure specially: it's a copy
5348 mechanism for which the parameter is never allocated. */
5349 else if (first && Is_Valued_Procedure (gnat_subprog))
5350 {
5351 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5352 mech = By_Copy;
5353 by_return = true;
5354 }
5355
5356 /* Or else, see if a Mechanism was supplied that forced this parameter
5357 to be passed one way or another. */
5358 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5359 forced_by_ref
5360 = (mech == By_Reference
5361 && !foreign
5362 && !TYPE_IS_BY_REFERENCE_P (gnu_param_type)
5363 && !Is_Aliased (gnat_param));
5364
5365 /* Positive mechanism means by copy for sufficiently small parameters. */
5366 else if (mech > 0)
5367 {
5368 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5369 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5370 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5371 mech = By_Reference;
5372 else
5373 mech = By_Copy;
5374 }
5375
5376 /* Otherwise, it's an unsupported mechanism so error out. */
5377 else
5378 {
5379 post_error ("unsupported mechanism for&", gnat_param);
5380 mech = Default;
5381 }
5382
5383 /* If this is either a foreign function or if the underlying type won't
5384 be passed by reference and is as aligned as the original type, strip
5385 off possible padding type. */
5386 if (TYPE_IS_PADDING_P (gnu_param_type))
5387 {
5388 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5389
5390 if (foreign
5391 || (mech != By_Reference
5392 && !must_pass_by_ref (unpadded_type)
5393 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5394 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5395 gnu_param_type = unpadded_type;
5396 }
5397
5398 /* For foreign conventions, pass arrays as pointers to the element type.
5399 First check for unconstrained array and get the underlying array. */
5400 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5401 gnu_param_type
5402 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5403
5404 /* Arrays are passed as pointers to element type for foreign conventions. */
5405 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5406 {
5407 /* Strip off any multi-dimensional entries, then strip
5408 off the last array to get the component type. */
5409 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5410 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5411 gnu_param_type = TREE_TYPE (gnu_param_type);
5412
5413 gnu_param_type = TREE_TYPE (gnu_param_type);
5414 gnu_param_type = build_pointer_type (gnu_param_type);
5415 by_component_ptr = true;
5416 }
5417
5418 /* Fat pointers are passed as thin pointers for foreign conventions. */
5419 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5420 gnu_param_type
5421 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5422
5423 /* Use a pointer type for the "this" pointer of C++ constructors. */
5424 else if (Chars (gnat_param) == Name_uInit && Is_Constructor (gnat_subprog))
5425 {
5426 gcc_assert (mech == By_Reference);
5427 gnu_param_type = build_pointer_type (gnu_param_type);
5428 by_ref = true;
5429 }
5430
5431 /* If we were requested or muss pass by reference, do so.
5432 If we were requested to pass by copy, do so.
5433 Otherwise, for foreign conventions, pass In Out or Out parameters
5434 or aggregates by reference. For COBOL and Fortran, pass all
5435 integer and FP types that way too. For Convention Ada, use
5436 the standard Ada default. */
5437 else if (mech == By_Reference
5438 || must_pass_by_ref (gnu_param_type)
5439 || (mech != By_Copy
5440 && ((foreign
5441 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5442 || (foreign
5443 && (Convention (gnat_subprog) == Convention_Fortran
5444 || Convention (gnat_subprog) == Convention_COBOL)
5445 && (INTEGRAL_TYPE_P (gnu_param_type)
5446 || FLOAT_TYPE_P (gnu_param_type)))
5447 || (!foreign
5448 && default_pass_by_ref (gnu_param_type)))))
5449 {
5450 /* We take advantage of 6.2(12) by considering that references built for
5451 parameters whose type isn't by-ref and for which the mechanism hasn't
5452 been forced to by-ref allow only a restricted form of aliasing. */
5453 restricted_aliasing_p
5454 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5455 gnu_param_type = build_reference_type (gnu_param_type);
5456 by_ref = true;
5457 }
5458
5459 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5460 else if (!in_param)
5461 *cico = true;
5462
5463 input_location = saved_location;
5464
5465 if (mech == By_Copy && (by_ref || by_component_ptr))
5466 post_error ("?cannot pass & by copy", gnat_param);
5467
5468 /* If this is an Out parameter that isn't passed by reference and whose
5469 type doesn't require the initialization of formals, we don't make a
5470 PARM_DECL for it. Instead, it will be a VAR_DECL created when we
5471 process the procedure, so just return its type here. Likewise for
5472 the _Init parameter of an initialization procedure or the special
5473 parameter of a valued procedure, never pass them in. */
5474 if (Ekind (gnat_param) == E_Out_Parameter
5475 && !by_ref
5476 && !by_component_ptr
5477 && (!type_requires_init_of_formal (Etype (gnat_param))
5478 || Is_Init_Proc (gnat_subprog)
5479 || by_return))
5480 {
5481 Set_Mechanism (gnat_param, By_Copy);
5482 return gnu_param_type;
5483 }
5484
5485 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5486 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5487 DECL_BY_REF_P (gnu_param) = by_ref;
5488 DECL_FORCED_BY_REF_P (gnu_param) = forced_by_ref;
5489 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5490 DECL_POINTS_TO_READONLY_P (gnu_param)
5491 = (ro_param && (by_ref || by_component_ptr));
5492 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5493 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5494 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5495
5496 /* If no Mechanism was specified, indicate what we're using, then
5497 back-annotate it. */
5498 if (mech == Default)
5499 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5500
5501 Set_Mechanism (gnat_param, mech);
5502 return gnu_param;
5503 }
5504
5505 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5506 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5507
5508 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5509 the corresponding profile, which means that, by the time the freeze node
5510 of the subprogram is encountered, types involved in its profile may still
5511 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5512 the freeze node of types involved in its profile, either types of formal
5513 parameters or the return type. */
5514
5515 static void
5516 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5517 {
5518 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5519
5520 struct tree_entity_vec_map in;
5521 in.base.from = gnu_type;
5522 struct tree_entity_vec_map **slot
5523 = dummy_to_subprog_map->find_slot (&in, INSERT);
5524 if (!*slot)
5525 {
5526 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5527 e->base.from = gnu_type;
5528 e->to = NULL;
5529 *slot = e;
5530 }
5531
5532 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5533 because the vector might have been just emptied by update_profiles_with.
5534 This can happen when there are 2 freeze nodes associated with different
5535 views of the same type; the type will be really complete only after the
5536 second freeze node is encountered. */
5537 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5538
5539 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5540
5541 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5542 since this would mean updating twice its profile. */
5543 if (v)
5544 {
5545 const unsigned len = v->length ();
5546 unsigned int l = 0, u = len;
5547
5548 /* Entity_Id is a simple integer so we can implement a stable order on
5549 the vector with an ordered insertion scheme and binary search. */
5550 while (l < u)
5551 {
5552 unsigned int m = (l + u) / 2;
5553 int diff = (int) (*v)[m] - (int) gnat_subprog;
5554 if (diff > 0)
5555 u = m;
5556 else if (diff < 0)
5557 l = m + 1;
5558 else
5559 return;
5560 }
5561
5562 /* l == u and therefore is the insertion point. */
5563 vec_safe_insert (v, l, gnat_subprog);
5564 }
5565 else
5566 vec_safe_push (v, gnat_subprog);
5567
5568 (*slot)->to = v;
5569 }
5570
5571 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5572
5573 static void
5574 update_profile (Entity_Id gnat_subprog)
5575 {
5576 tree gnu_param_list;
5577 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5578 Needs_Debug_Info (gnat_subprog),
5579 &gnu_param_list);
5580 if (DECL_P (gnu_type))
5581 {
5582 /* Builtins cannot have their address taken so we can reset them. */
5583 gcc_assert (fndecl_built_in_p (gnu_type));
5584 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5585 save_gnu_tree (gnat_subprog, gnu_type, false);
5586 return;
5587 }
5588
5589 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5590
5591 TREE_TYPE (gnu_subprog) = gnu_type;
5592
5593 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5594 and needs to be adjusted too. */
5595 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5596 {
5597 tree gnu_entity_name = get_entity_name (gnat_subprog);
5598 tree gnu_ext_name
5599 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5600
5601 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5602 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5603 }
5604 }
5605
5606 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5607 a dummy type which appears in profiles. */
5608
5609 void
5610 update_profiles_with (tree gnu_type)
5611 {
5612 struct tree_entity_vec_map in;
5613 in.base.from = gnu_type;
5614 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5615 gcc_assert (e);
5616 vec<Entity_Id, va_gc_atomic> *v = e->to;
5617 e->to = NULL;
5618
5619 /* The flag needs to be reset before calling update_profile, in case
5620 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5621 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5622
5623 unsigned int i;
5624 Entity_Id *iter;
5625 FOR_EACH_VEC_ELT (*v, i, iter)
5626 update_profile (*iter);
5627
5628 vec_free (v);
5629 }
5630
5631 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5632
5633 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5634 context may now appear as parameter and result types. As a consequence,
5635 we may need to defer their translation until after a freeze node is seen
5636 or to the end of the current unit. We also aim at handling temporarily
5637 incomplete types created by the usual delayed elaboration scheme. */
5638
5639 static tree
5640 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5641 {
5642 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5643 so the rationale is exposed in that place. These processings probably
5644 ought to be merged at some point. */
5645 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5646 const bool is_from_limited_with
5647 = (Is_Incomplete_Type (gnat_equiv)
5648 && From_Limited_With (gnat_equiv));
5649 Entity_Id gnat_full_direct_first
5650 = (is_from_limited_with
5651 ? Non_Limited_View (gnat_equiv)
5652 : (Is_Incomplete_Or_Private_Type (gnat_equiv)
5653 ? Full_View (gnat_equiv) : Empty));
5654 Entity_Id gnat_full_direct
5655 = ((is_from_limited_with
5656 && Present (gnat_full_direct_first)
5657 && Is_Private_Type (gnat_full_direct_first))
5658 ? Full_View (gnat_full_direct_first)
5659 : gnat_full_direct_first);
5660 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5661 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5662 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5663 tree gnu_type;
5664
5665 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5666 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5667
5668 else if (is_from_limited_with
5669 && ((!in_main_unit
5670 && !present_gnu_tree (gnat_equiv)
5671 && Present (gnat_full)
5672 && (Is_Record_Type (gnat_full)
5673 || Is_Array_Type (gnat_full)
5674 || Is_Access_Type (gnat_full)))
5675 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5676 {
5677 gnu_type = make_dummy_type (gnat_equiv);
5678
5679 if (!in_main_unit)
5680 {
5681 struct incomplete *p = XNEW (struct incomplete);
5682
5683 p->old_type = gnu_type;
5684 p->full_type = gnat_equiv;
5685 p->next = defer_limited_with_list;
5686 defer_limited_with_list = p;
5687 }
5688 }
5689
5690 else if (type_annotate_only && No (gnat_equiv))
5691 gnu_type = void_type_node;
5692
5693 else
5694 gnu_type = gnat_to_gnu_type (gnat_equiv);
5695
5696 /* Access-to-unconstrained-array types need a special treatment. */
5697 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5698 {
5699 if (!TYPE_POINTER_TO (gnu_type))
5700 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5701 }
5702
5703 return gnu_type;
5704 }
5705
5706 /* Return true if TYPE contains only integral data, recursively if need be. */
5707
5708 static bool
5709 type_contains_only_integral_data (tree type)
5710 {
5711 switch (TREE_CODE (type))
5712 {
5713 case RECORD_TYPE:
5714 case UNION_TYPE:
5715 case QUAL_UNION_TYPE:
5716 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5717 if (!type_contains_only_integral_data (TREE_TYPE (field)))
5718 return false;
5719 return true;
5720
5721 case ARRAY_TYPE:
5722 case COMPLEX_TYPE:
5723 return type_contains_only_integral_data (TREE_TYPE (type));
5724
5725 default:
5726 return INTEGRAL_TYPE_P (type);
5727 }
5728
5729 gcc_unreachable ();
5730 }
5731
5732 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5733 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5734 is true if we need to write debug information for other types that we may
5735 create in the process. Also set PARAM_LIST to the list of parameters.
5736 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5737 directly instead of its type. */
5738
5739 static tree
5740 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5741 bool debug_info_p, tree *param_list)
5742 {
5743 const Entity_Kind kind = Ekind (gnat_subprog);
5744 const bool method_p = is_cplusplus_method (gnat_subprog);
5745 const bool variadic = IN (Convention (gnat_subprog), Convention_C_Variadic);
5746 Entity_Id gnat_return_type = Etype (gnat_subprog);
5747 Entity_Id gnat_param;
5748 tree gnu_type = present_gnu_tree (gnat_subprog)
5749 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5750 tree gnu_return_type;
5751 tree gnu_param_type_list = NULL_TREE;
5752 tree gnu_param_list = NULL_TREE;
5753 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5754 (In Out or Out parameters not passed by reference), in which case it is
5755 the list of nodes used to specify the values of the In Out/Out parameters
5756 that are returned as a record upon procedure return. The TREE_PURPOSE of
5757 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5758 is the PARM_DECL corresponding to that field. This list will be saved in
5759 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5760 tree gnu_cico_list = NULL_TREE;
5761 tree gnu_cico_return_type = NULL_TREE;
5762 tree gnu_cico_field_list = NULL_TREE;
5763 bool gnu_cico_only_integral_type = true;
5764 /* The semantics of "pure" in Ada essentially matches that of "const"
5765 or "pure" in GCC. In particular, both properties are orthogonal
5766 to the "nothrow" property if the EH circuitry is explicit in the
5767 internal representation of the middle-end. If we are to completely
5768 hide the EH circuitry from it, we need to declare that calls to pure
5769 Ada subprograms that can throw have side effects since they can
5770 trigger an "abnormal" transfer of control flow; therefore, they can
5771 be neither "const" nor "pure" in the GCC sense. */
5772 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5773 bool pure_flag = false;
5774 bool return_by_direct_ref_p = false;
5775 bool return_by_invisi_ref_p = false;
5776 bool return_unconstrained_p = false;
5777 bool incomplete_profile_p = false;
5778 int num;
5779
5780 /* Look into the return type and get its associated GCC tree if it is not
5781 void, and then compute various flags for the subprogram type. But make
5782 sure not to do this processing multiple times. */
5783 if (Ekind (gnat_return_type) == E_Void)
5784 gnu_return_type = void_type_node;
5785
5786 else if (gnu_type
5787 && FUNC_OR_METHOD_TYPE_P (gnu_type)
5788 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5789 {
5790 gnu_return_type = TREE_TYPE (gnu_type);
5791 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5792 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5793 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5794 }
5795
5796 else
5797 {
5798 /* For foreign convention subprograms, return System.Address as void *
5799 or equivalent. Note that this comprises GCC builtins. */
5800 if (Has_Foreign_Convention (gnat_subprog)
5801 && Is_Descendant_Of_Address (Underlying_Type (gnat_return_type)))
5802 gnu_return_type = ptr_type_node;
5803 else
5804 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5805
5806 /* If this function returns by reference, make the actual return type
5807 the reference type and make a note of that. */
5808 if (Returns_By_Ref (gnat_subprog))
5809 {
5810 gnu_return_type = build_reference_type (gnu_return_type);
5811 return_by_direct_ref_p = true;
5812 }
5813
5814 /* If the return type is an unconstrained array type, the return value
5815 will be allocated on the secondary stack so the actual return type
5816 is the fat pointer type. */
5817 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5818 {
5819 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5820 return_unconstrained_p = true;
5821 }
5822
5823 /* This is the same unconstrained array case, but for a dummy type. */
5824 else if (TYPE_REFERENCE_TO (gnu_return_type)
5825 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5826 {
5827 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5828 return_unconstrained_p = true;
5829 }
5830
5831 /* Likewise, if the return type requires a transient scope, the return
5832 value will also be allocated on the secondary stack so the actual
5833 return type is the reference type. */
5834 else if (Requires_Transient_Scope (gnat_return_type))
5835 {
5836 gnu_return_type = build_reference_type (gnu_return_type);
5837 return_unconstrained_p = true;
5838 }
5839
5840 /* If the Mechanism is By_Reference, ensure this function uses the
5841 target's by-invisible-reference mechanism, which may not be the
5842 same as above (e.g. it might be passing an extra parameter). */
5843 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5844 return_by_invisi_ref_p = true;
5845
5846 /* Likewise, if the return type is itself By_Reference. */
5847 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5848 return_by_invisi_ref_p = true;
5849
5850 /* If the type is a padded type and the underlying type would not be
5851 passed by reference or the function has a foreign convention, return
5852 the underlying type. */
5853 else if (TYPE_IS_PADDING_P (gnu_return_type)
5854 && (!default_pass_by_ref
5855 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5856 || Has_Foreign_Convention (gnat_subprog)))
5857 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5858
5859 /* If the return type is unconstrained, it must have a maximum size.
5860 Use the padded type as the effective return type. And ensure the
5861 function uses the target's by-invisible-reference mechanism to
5862 avoid copying too much data when it returns. */
5863 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5864 {
5865 tree orig_type = gnu_return_type;
5866 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5867
5868 /* If the size overflows to 0, set it to an arbitrary positive
5869 value so that assignments in the type are preserved. Their
5870 actual size is independent of this positive value. */
5871 if (TREE_CODE (max_return_size) == INTEGER_CST
5872 && TREE_OVERFLOW (max_return_size)
5873 && integer_zerop (max_return_size))
5874 {
5875 max_return_size = copy_node (bitsize_unit_node);
5876 TREE_OVERFLOW (max_return_size) = 1;
5877 }
5878
5879 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5880 0, gnat_subprog, false, definition,
5881 true);
5882
5883 /* Declare it now since it will never be declared otherwise. This
5884 is necessary to ensure that its subtrees are properly marked. */
5885 if (gnu_return_type != orig_type
5886 && !DECL_P (TYPE_NAME (gnu_return_type)))
5887 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5888 true, debug_info_p, gnat_subprog);
5889
5890 return_by_invisi_ref_p = true;
5891 }
5892
5893 /* If the return type has a size that overflows, we usually cannot have
5894 a function that returns that type. This usage doesn't really make
5895 sense anyway, so issue an error here. */
5896 if (!return_by_invisi_ref_p
5897 && TYPE_SIZE_UNIT (gnu_return_type)
5898 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5899 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5900 {
5901 post_error ("cannot return type whose size overflows", gnat_subprog);
5902 gnu_return_type = copy_type (gnu_return_type);
5903 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5904 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5905 }
5906
5907 /* If the return type is incomplete, there are 2 cases: if the function
5908 returns by reference, then the return type is only linked indirectly
5909 in the profile, so the profile can be seen as complete since it need
5910 not be further modified, only the reference types need be adjusted;
5911 otherwise the profile is incomplete and need be adjusted too. */
5912 if (TYPE_IS_DUMMY_P (gnu_return_type))
5913 {
5914 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5915 incomplete_profile_p = true;
5916 }
5917
5918 if (kind == E_Function)
5919 Set_Mechanism (gnat_subprog, return_unconstrained_p
5920 || return_by_direct_ref_p
5921 || return_by_invisi_ref_p
5922 ? By_Reference : By_Copy);
5923 }
5924
5925 /* A procedure (something that doesn't return anything) shouldn't be
5926 considered const since there would be no reason for calling such a
5927 subprogram. Note that procedures with Out (or In Out) parameters
5928 have already been converted into a function with a return type.
5929 Similarly, if the function returns an unconstrained type, then the
5930 function will allocate the return value on the secondary stack and
5931 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5932 if (VOID_TYPE_P (gnu_return_type) || return_unconstrained_p)
5933 const_flag = false;
5934
5935 /* Loop over the parameters and get their associated GCC tree. While doing
5936 this, build a copy-in copy-out structure if we need one. */
5937 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5938 Present (gnat_param);
5939 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5940 {
5941 const bool mech_is_by_ref
5942 = Mechanism (gnat_param) == By_Reference
5943 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5944 tree gnu_param_name = get_entity_name (gnat_param);
5945 tree gnu_param, gnu_param_type;
5946 bool cico = false;
5947
5948 /* For a variadic C function, do not build unnamed parameters. */
5949 if (variadic
5950 && num == (Convention (gnat_subprog) - Convention_C_Variadic_0))
5951 break;
5952
5953 /* Fetch an existing parameter with complete type and reuse it. But we
5954 didn't save the CICO property so we can only do it for In parameters
5955 or parameters passed by reference. */
5956 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5957 && present_gnu_tree (gnat_param)
5958 && (gnu_param = get_gnu_tree (gnat_param))
5959 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5960 {
5961 DECL_CHAIN (gnu_param) = NULL_TREE;
5962 gnu_param_type = TREE_TYPE (gnu_param);
5963 }
5964
5965 /* Otherwise translate the parameter type and act accordingly. */
5966 else
5967 {
5968 Entity_Id gnat_param_type = Etype (gnat_param);
5969
5970 /* For foreign convention subprograms, pass System.Address as void *
5971 or equivalent. Note that this comprises GCC builtins. */
5972 if (Has_Foreign_Convention (gnat_subprog)
5973 && Is_Descendant_Of_Address (Underlying_Type (gnat_param_type)))
5974 gnu_param_type = ptr_type_node;
5975 else
5976 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5977
5978 /* If the parameter type is incomplete, there are 2 cases: if it is
5979 passed by reference, then the type is only linked indirectly in
5980 the profile, so the profile can be seen as complete since it need
5981 not be further modified, only the reference type need be adjusted;
5982 otherwise the profile is incomplete and need be adjusted too. */
5983 if (TYPE_IS_DUMMY_P (gnu_param_type))
5984 {
5985 Node_Id gnat_decl;
5986
5987 if (mech_is_by_ref
5988 || (TYPE_REFERENCE_TO (gnu_param_type)
5989 && TYPE_IS_FAT_POINTER_P
5990 (TYPE_REFERENCE_TO (gnu_param_type)))
5991 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5992 {
5993 gnu_param_type = build_reference_type (gnu_param_type);
5994 gnu_param
5995 = create_param_decl (gnu_param_name, gnu_param_type);
5996 TREE_READONLY (gnu_param) = 1;
5997 DECL_BY_REF_P (gnu_param) = 1;
5998 DECL_POINTS_TO_READONLY_P (gnu_param)
5999 = (Ekind (gnat_param) == E_In_Parameter
6000 && !Address_Taken (gnat_param));
6001 Set_Mechanism (gnat_param, By_Reference);
6002 Sloc_to_locus (Sloc (gnat_param),
6003 &DECL_SOURCE_LOCATION (gnu_param));
6004 }
6005
6006 /* ??? This is a kludge to support null procedures in spec taking
6007 a parameter with an untagged incomplete type coming from a
6008 limited context. The front-end creates a body without knowing
6009 anything about the non-limited view, which is illegal Ada and
6010 cannot be supported. Create a parameter with a fake type. */
6011 else if (kind == E_Procedure
6012 && (gnat_decl = Parent (gnat_subprog))
6013 && Nkind (gnat_decl) == N_Procedure_Specification
6014 && Null_Present (gnat_decl)
6015 && Is_Incomplete_Type (gnat_param_type))
6016 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
6017
6018 else
6019 {
6020 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
6021 Call_to_gnu will stop if it encounters the PARM_DECL. */
6022 gnu_param
6023 = build_decl (input_location, PARM_DECL, gnu_param_name,
6024 gnu_param_type);
6025 associate_subprog_with_dummy_type (gnat_subprog,
6026 gnu_param_type);
6027 incomplete_profile_p = true;
6028 }
6029 }
6030
6031 /* Otherwise build the parameter declaration normally. */
6032 else
6033 {
6034 gnu_param
6035 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
6036 gnat_subprog, &cico);
6037
6038 /* We are returned either a PARM_DECL or a type if no parameter
6039 needs to be passed; in either case, adjust the type. */
6040 if (DECL_P (gnu_param))
6041 gnu_param_type = TREE_TYPE (gnu_param);
6042 else
6043 {
6044 gnu_param_type = gnu_param;
6045 gnu_param = NULL_TREE;
6046 }
6047 }
6048 }
6049
6050 /* If we have a GCC tree for the parameter, register it. */
6051 save_gnu_tree (gnat_param, NULL_TREE, false);
6052 if (gnu_param)
6053 {
6054 gnu_param_type_list
6055 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
6056 DECL_CHAIN (gnu_param) = gnu_param_list;
6057 gnu_param_list = gnu_param;
6058 save_gnu_tree (gnat_param, gnu_param, false);
6059
6060 /* A pure function in the Ada sense which takes an access parameter
6061 may modify memory through it and thus need be considered neither
6062 const nor pure in the GCC sense, unless it's access-to-function.
6063 Likewise it if takes a by-ref In Out or Out parameter. But if it
6064 takes a by-ref In parameter, then it may only read memory through
6065 it and can be considered pure in the GCC sense. */
6066 if ((const_flag || pure_flag)
6067 && ((POINTER_TYPE_P (gnu_param_type)
6068 && TREE_CODE (TREE_TYPE (gnu_param_type)) != FUNCTION_TYPE)
6069 || TYPE_IS_FAT_POINTER_P (gnu_param_type)))
6070 {
6071 const_flag = false;
6072 pure_flag = DECL_POINTS_TO_READONLY_P (gnu_param);
6073 }
6074 }
6075
6076 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
6077 for it in the return type and register the association. */
6078 if (cico && !incomplete_profile_p)
6079 {
6080 if (!gnu_cico_list)
6081 {
6082 gnu_cico_return_type = make_node (RECORD_TYPE);
6083
6084 /* If this is a function, we also need a field for the
6085 return value to be placed. */
6086 if (!VOID_TYPE_P (gnu_return_type))
6087 {
6088 tree gnu_field
6089 = create_field_decl (get_identifier ("RETVAL"),
6090 gnu_return_type,
6091 gnu_cico_return_type, NULL_TREE,
6092 NULL_TREE, 0, 0);
6093 Sloc_to_locus (Sloc (gnat_subprog),
6094 &DECL_SOURCE_LOCATION (gnu_field));
6095 gnu_cico_field_list = gnu_field;
6096 gnu_cico_list
6097 = tree_cons (gnu_field, void_type_node, NULL_TREE);
6098 if (!type_contains_only_integral_data (gnu_return_type))
6099 gnu_cico_only_integral_type = false;
6100 }
6101
6102 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
6103 /* Set a default alignment to speed up accesses. But we should
6104 not increase the size of the structure too much, lest it does
6105 not fit in return registers anymore. */
6106 SET_TYPE_ALIGN (gnu_cico_return_type,
6107 get_mode_alignment (ptr_mode));
6108 }
6109
6110 tree gnu_field
6111 = create_field_decl (gnu_param_name, gnu_param_type,
6112 gnu_cico_return_type, NULL_TREE, NULL_TREE,
6113 0, 0);
6114 Sloc_to_locus (Sloc (gnat_param),
6115 &DECL_SOURCE_LOCATION (gnu_field));
6116 DECL_CHAIN (gnu_field) = gnu_cico_field_list;
6117 gnu_cico_field_list = gnu_field;
6118 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
6119 if (!type_contains_only_integral_data (gnu_param_type))
6120 gnu_cico_only_integral_type = false;
6121 }
6122 }
6123
6124 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
6125 and finish up the return type. */
6126 if (gnu_cico_list && !incomplete_profile_p)
6127 {
6128 /* If we have a CICO list but it has only one entry, we convert
6129 this function into a function that returns this object. */
6130 if (list_length (gnu_cico_list) == 1)
6131 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
6132
6133 /* Do not finalize the return type if the subprogram is stubbed
6134 since structures are incomplete for the back-end. */
6135 else if (Convention (gnat_subprog) != Convention_Stubbed)
6136 {
6137 finish_record_type (gnu_cico_return_type,
6138 nreverse (gnu_cico_field_list),
6139 0, false);
6140
6141 /* Try to promote the mode if the return type is fully returned
6142 in integer registers, again to speed up accesses. */
6143 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
6144 && gnu_cico_only_integral_type
6145 && !targetm.calls.return_in_memory (gnu_cico_return_type,
6146 NULL_TREE))
6147 {
6148 unsigned int size
6149 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
6150 unsigned int i = BITS_PER_UNIT;
6151 scalar_int_mode mode;
6152
6153 while (i < size)
6154 i <<= 1;
6155 if (int_mode_for_size (i, 0).exists (&mode))
6156 {
6157 SET_TYPE_MODE (gnu_cico_return_type, mode);
6158 SET_TYPE_ALIGN (gnu_cico_return_type,
6159 GET_MODE_ALIGNMENT (mode));
6160 TYPE_SIZE (gnu_cico_return_type)
6161 = bitsize_int (GET_MODE_BITSIZE (mode));
6162 TYPE_SIZE_UNIT (gnu_cico_return_type)
6163 = size_int (GET_MODE_SIZE (mode));
6164 }
6165 }
6166
6167 /* But demote the mode if the return type is partly returned in FP
6168 registers to avoid creating problematic paradoxical subregs.
6169 Note that we need to cater to historical 32-bit architectures
6170 that incorrectly use the mode to select the return mechanism. */
6171 else if (INTEGRAL_MODE_P (TYPE_MODE (gnu_cico_return_type))
6172 && !gnu_cico_only_integral_type
6173 && BITS_PER_WORD >= 64
6174 && !targetm.calls.return_in_memory (gnu_cico_return_type,
6175 NULL_TREE))
6176 SET_TYPE_MODE (gnu_cico_return_type, BLKmode);
6177
6178 if (debug_info_p)
6179 rest_of_record_type_compilation (gnu_cico_return_type);
6180 }
6181
6182 gnu_return_type = gnu_cico_return_type;
6183 }
6184
6185 /* The lists have been built in reverse. */
6186 gnu_param_type_list = nreverse (gnu_param_type_list);
6187 if (!variadic)
6188 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
6189 gnu_param_list = nreverse (gnu_param_list);
6190 gnu_cico_list = nreverse (gnu_cico_list);
6191
6192 /* Turn imported C++ constructors into their callable form as done in the
6193 front-end, i.e. add the "this" pointer and void the return type. */
6194 if (method_p
6195 && Is_Constructor (gnat_subprog)
6196 && !VOID_TYPE_P (gnu_return_type))
6197 {
6198 tree gnu_param_type
6199 = build_pointer_type (gnat_to_gnu_profile_type (gnat_return_type));
6200 tree gnu_param_name = get_identifier (Get_Name_String (Name_uInit));
6201 tree gnu_param
6202 = build_decl (input_location, PARM_DECL, gnu_param_name,
6203 gnu_param_type);
6204 gnu_param_type_list
6205 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
6206 DECL_CHAIN (gnu_param) = gnu_param_list;
6207 gnu_param_list = gnu_param;
6208 gnu_return_type = void_type_node;
6209 }
6210
6211 /* If the profile is incomplete, we only set the (temporary) return and
6212 parameter types; otherwise, we build the full type. In either case,
6213 we reuse an already existing GCC tree that we built previously here. */
6214 if (incomplete_profile_p)
6215 {
6216 if (gnu_type && FUNC_OR_METHOD_TYPE_P (gnu_type))
6217 ;
6218 else
6219 gnu_type = make_node (method_p ? METHOD_TYPE : FUNCTION_TYPE);
6220 TREE_TYPE (gnu_type) = gnu_return_type;
6221 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6222 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6223 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6224 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6225 }
6226 else
6227 {
6228 if (gnu_type && FUNC_OR_METHOD_TYPE_P (gnu_type))
6229 {
6230 TREE_TYPE (gnu_type) = gnu_return_type;
6231 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6232 if (method_p)
6233 {
6234 tree gnu_basetype = TREE_TYPE (TREE_VALUE (gnu_param_type_list));
6235 TYPE_METHOD_BASETYPE (gnu_type)
6236 = TYPE_MAIN_VARIANT (gnu_basetype);
6237 }
6238 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6239 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6240 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6241 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6242 TYPE_CANONICAL (gnu_type) = gnu_type;
6243 layout_type (gnu_type);
6244 }
6245 else
6246 {
6247 if (method_p)
6248 {
6249 tree gnu_basetype = TREE_TYPE (TREE_VALUE (gnu_param_type_list));
6250 gnu_type
6251 = build_method_type_directly (gnu_basetype, gnu_return_type,
6252 TREE_CHAIN (gnu_param_type_list));
6253 }
6254 else
6255 gnu_type
6256 = build_function_type (gnu_return_type, gnu_param_type_list);
6257
6258 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
6259 has a different TYPE_CI_CO_LIST or flags. */
6260 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
6261 return_unconstrained_p,
6262 return_by_direct_ref_p,
6263 return_by_invisi_ref_p))
6264 {
6265 gnu_type = copy_type (gnu_type);
6266 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6267 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6268 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6269 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6270 }
6271 }
6272
6273 if (const_flag)
6274 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6275
6276 if (pure_flag)
6277 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_RESTRICT);
6278
6279 if (No_Return (gnat_subprog))
6280 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6281
6282 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6283 corresponding DECL node and check the parameter association. */
6284 if (Convention (gnat_subprog) == Convention_Intrinsic
6285 && Present (Interface_Name (gnat_subprog)))
6286 {
6287 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6288 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6289
6290 /* If we have a builtin DECL for that function, use it. Check if
6291 the profiles are compatible and warn if they are not. Note that
6292 the checker is expected to post diagnostics in this case. */
6293 if (gnu_builtin_decl)
6294 {
6295 intrin_binding_t inb
6296 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6297
6298 if (!intrin_profiles_compatible_p (&inb))
6299 post_error
6300 ("?profile of& doesn''t match the builtin it binds!",
6301 gnat_subprog);
6302
6303 return gnu_builtin_decl;
6304 }
6305
6306 /* Inability to find the builtin DECL most often indicates a genuine
6307 mistake, but imports of unregistered intrinsics are sometimes used
6308 on purpose to allow hooking in alternate bodies; we post a warning
6309 conditioned on Wshadow in this case, to let developers be notified
6310 on demand without risking false positives with common default sets
6311 of options. */
6312 if (warn_shadow)
6313 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6314 }
6315 }
6316
6317 *param_list = gnu_param_list;
6318
6319 return gnu_type;
6320 }
6321
6322 /* Return the external name for GNAT_SUBPROG given its entity name. */
6323
6324 static tree
6325 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6326 {
6327 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6328
6329 /* If there was no specified Interface_Name and the external and
6330 internal names of the subprogram are the same, only use the
6331 internal name to allow disambiguation of nested subprograms. */
6332 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6333 gnu_ext_name = NULL_TREE;
6334
6335 return gnu_ext_name;
6336 }
6337
6338 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6339 build_nonshared_array_type. */
6340
6341 static void
6342 set_nonaliased_component_on_array_type (tree type)
6343 {
6344 TYPE_NONALIASED_COMPONENT (type) = 1;
6345 if (TYPE_CANONICAL (type))
6346 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6347 }
6348
6349 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6350 build_nonshared_array_type. */
6351
6352 static void
6353 set_reverse_storage_order_on_array_type (tree type)
6354 {
6355 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6356 if (TYPE_CANONICAL (type))
6357 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6358 }
6359
6360 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6361
6362 static bool
6363 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6364 {
6365 while (Present (Corresponding_Discriminant (discr1)))
6366 discr1 = Corresponding_Discriminant (discr1);
6367
6368 while (Present (Corresponding_Discriminant (discr2)))
6369 discr2 = Corresponding_Discriminant (discr2);
6370
6371 return
6372 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6373 }
6374
6375 /* Return true if the array type GNU_TYPE, which represents a dimension of
6376 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6377
6378 static bool
6379 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6380 {
6381 /* If the array type has an aliased component in the front-end sense,
6382 then it also has an aliased component in the back-end sense. */
6383 if (Has_Aliased_Components (gnat_type))
6384 return false;
6385
6386 /* If this is a derived type, then it has a non-aliased component if
6387 and only if its parent type also has one. */
6388 if (Is_Derived_Type (gnat_type))
6389 {
6390 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6391 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6392 gnu_parent_type
6393 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6394 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6395 }
6396
6397 /* For a multi-dimensional array type, find the component type. */
6398 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6399 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6400 gnu_type = TREE_TYPE (gnu_type);
6401
6402 /* Consider that an array of pointers has an aliased component, which is
6403 sort of logical and helps with Taft Amendment types in LTO mode. */
6404 if (POINTER_TYPE_P (TREE_TYPE (gnu_type)))
6405 return false;
6406
6407 /* Otherwise, rely exclusively on properties of the element type. */
6408 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6409 }
6410
6411 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6412
6413 static bool
6414 compile_time_known_address_p (Node_Id gnat_address)
6415 {
6416 /* Handle reference to a constant. */
6417 if (Is_Entity_Name (gnat_address)
6418 && Ekind (Entity (gnat_address)) == E_Constant)
6419 {
6420 gnat_address = Constant_Value (Entity (gnat_address));
6421 if (No (gnat_address))
6422 return false;
6423 }
6424
6425 /* Catch System'To_Address. */
6426 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6427 gnat_address = Expression (gnat_address);
6428
6429 return Compile_Time_Known_Value (gnat_address);
6430 }
6431
6432 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6433 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6434
6435 static bool
6436 cannot_be_superflat (Node_Id gnat_range)
6437 {
6438 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6439 Node_Id scalar_range;
6440 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6441
6442 /* If the low bound is not constant, try to find an upper bound. */
6443 while (Nkind (gnat_lb) != N_Integer_Literal
6444 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6445 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6446 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6447 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6448 || Nkind (scalar_range) == N_Range))
6449 gnat_lb = High_Bound (scalar_range);
6450
6451 /* If the high bound is not constant, try to find a lower bound. */
6452 while (Nkind (gnat_hb) != N_Integer_Literal
6453 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6454 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6455 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6456 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6457 || Nkind (scalar_range) == N_Range))
6458 gnat_hb = Low_Bound (scalar_range);
6459
6460 /* If we have failed to find constant bounds, punt. */
6461 if (Nkind (gnat_lb) != N_Integer_Literal
6462 || Nkind (gnat_hb) != N_Integer_Literal)
6463 return false;
6464
6465 /* We need at least a signed 64-bit type to catch most cases. */
6466 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6467 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6468 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6469 return false;
6470
6471 /* If the low bound is the smallest integer, nothing can be smaller. */
6472 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6473 if (TREE_OVERFLOW (gnu_lb_minus_one))
6474 return true;
6475
6476 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6477 }
6478
6479 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6480
6481 static bool
6482 constructor_address_p (tree gnu_expr)
6483 {
6484 while (TREE_CODE (gnu_expr) == NOP_EXPR
6485 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6486 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6487 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6488
6489 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6490 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6491 }
6492
6493 /* Return true if the size in units represented by GNU_SIZE can be handled by
6494 an allocation. If STATIC_P is true, consider only what can be done with a
6495 static allocation. */
6496
6497 static bool
6498 allocatable_size_p (tree gnu_size, bool static_p)
6499 {
6500 /* We can allocate a fixed size if it is a valid for the middle-end. */
6501 if (TREE_CODE (gnu_size) == INTEGER_CST)
6502 return valid_constant_size_p (gnu_size);
6503
6504 /* We can allocate a variable size if this isn't a static allocation. */
6505 else
6506 return !static_p;
6507 }
6508
6509 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6510 initial value of an object of GNU_TYPE. */
6511
6512 static bool
6513 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6514 {
6515 /* Do not convert if the object's type is unconstrained because this would
6516 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6517 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6518 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6519 return false;
6520
6521 /* Do not convert if the object's type is a padding record whose field is of
6522 self-referential size because we want to copy only the actual data. */
6523 if (type_is_padding_self_referential (gnu_type))
6524 return false;
6525
6526 /* Do not convert a call to a function that returns with variable size since
6527 we want to use the return slot optimization in this case. */
6528 if (TREE_CODE (gnu_expr) == CALL_EXPR
6529 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6530 return false;
6531
6532 /* Do not convert to a record type with a variant part from a record type
6533 without one, to keep the object simpler. */
6534 if (TREE_CODE (gnu_type) == RECORD_TYPE
6535 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6536 && get_variant_part (gnu_type)
6537 && !get_variant_part (TREE_TYPE (gnu_expr)))
6538 return false;
6539
6540 /* In all the other cases, convert the expression to the object's type. */
6541 return true;
6542 }
6543
6544 /* Add the contribution of [MIN, MAX] to the current number of elements N_ELEM
6545 of an array type and return the result, or NULL_TREE if it overflowed. */
6546
6547 static tree
6548 update_n_elem (tree n_elem, tree min, tree max)
6549 {
6550 /* First deal with the empty case. */
6551 if (TREE_CODE (min) == INTEGER_CST
6552 && TREE_CODE (max) == INTEGER_CST
6553 && tree_int_cst_lt (max, min))
6554 return size_zero_node;
6555
6556 min = convert (sizetype, min);
6557 max = convert (sizetype, max);
6558
6559 /* Compute the number of elements in this dimension. */
6560 tree this_n_elem
6561 = size_binop (PLUS_EXPR, size_one_node, size_binop (MINUS_EXPR, max, min));
6562
6563 if (TREE_CODE (this_n_elem) == INTEGER_CST && TREE_OVERFLOW (this_n_elem))
6564 return NULL_TREE;
6565
6566 /* Multiply the current number of elements by the result. */
6567 n_elem = size_binop (MULT_EXPR, n_elem, this_n_elem);
6568
6569 if (TREE_CODE (n_elem) == INTEGER_CST && TREE_OVERFLOW (n_elem))
6570 return NULL_TREE;
6571
6572 return n_elem;
6573 }
6574
6575 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6576 be elaborated at the point of its definition, but do nothing else. */
6577
6578 void
6579 elaborate_entity (Entity_Id gnat_entity)
6580 {
6581 switch (Ekind (gnat_entity))
6582 {
6583 case E_Signed_Integer_Subtype:
6584 case E_Modular_Integer_Subtype:
6585 case E_Enumeration_Subtype:
6586 case E_Ordinary_Fixed_Point_Subtype:
6587 case E_Decimal_Fixed_Point_Subtype:
6588 case E_Floating_Point_Subtype:
6589 {
6590 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6591 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6592
6593 /* ??? Tests to avoid Constraint_Error in static expressions
6594 are needed until after the front stops generating bogus
6595 conversions on bounds of real types. */
6596 if (!Raises_Constraint_Error (gnat_lb))
6597 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6598 Needs_Debug_Info (gnat_entity));
6599 if (!Raises_Constraint_Error (gnat_hb))
6600 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6601 Needs_Debug_Info (gnat_entity));
6602 break;
6603 }
6604
6605 case E_Record_Subtype:
6606 case E_Private_Subtype:
6607 case E_Limited_Private_Subtype:
6608 case E_Record_Subtype_With_Private:
6609 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6610 {
6611 Node_Id gnat_discriminant_expr;
6612 Entity_Id gnat_field;
6613
6614 for (gnat_field
6615 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6616 gnat_discriminant_expr
6617 = First_Elmt (Discriminant_Constraint (gnat_entity));
6618 Present (gnat_field);
6619 gnat_field = Next_Discriminant (gnat_field),
6620 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6621 /* Ignore access discriminants. */
6622 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6623 elaborate_expression (Node (gnat_discriminant_expr),
6624 gnat_entity, get_entity_char (gnat_field),
6625 true, false, false);
6626 }
6627 break;
6628
6629 }
6630 }
6631
6632 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6633 NAME, ARGS and ERROR_POINT. */
6634
6635 static void
6636 prepend_one_attribute (struct attrib **attr_list,
6637 enum attrib_type attrib_type,
6638 tree attr_name,
6639 tree attr_args,
6640 Node_Id attr_error_point)
6641 {
6642 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6643
6644 attr->type = attrib_type;
6645 attr->name = attr_name;
6646 attr->args = attr_args;
6647 attr->error_point = attr_error_point;
6648
6649 attr->next = *attr_list;
6650 *attr_list = attr;
6651 }
6652
6653 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6654
6655 static void
6656 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6657 {
6658 const Node_Id gnat_arg = First (Pragma_Argument_Associations (gnat_pragma));
6659 Node_Id gnat_next_arg = Next (gnat_arg);
6660 tree gnu_arg1 = NULL_TREE, gnu_arg_list = NULL_TREE;
6661 enum attrib_type etype;
6662
6663 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6664 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6665 {
6666 case Pragma_Linker_Alias:
6667 etype = ATTR_LINK_ALIAS;
6668 break;
6669
6670 case Pragma_Linker_Constructor:
6671 etype = ATTR_LINK_CONSTRUCTOR;
6672 break;
6673
6674 case Pragma_Linker_Destructor:
6675 etype = ATTR_LINK_DESTRUCTOR;
6676 break;
6677
6678 case Pragma_Linker_Section:
6679 etype = ATTR_LINK_SECTION;
6680 break;
6681
6682 case Pragma_Machine_Attribute:
6683 etype = ATTR_MACHINE_ATTRIBUTE;
6684 break;
6685
6686 case Pragma_Thread_Local_Storage:
6687 etype = ATTR_THREAD_LOCAL_STORAGE;
6688 break;
6689
6690 case Pragma_Weak_External:
6691 etype = ATTR_WEAK_EXTERNAL;
6692 break;
6693
6694 default:
6695 return;
6696 }
6697
6698 /* See what arguments we have and turn them into GCC trees for attribute
6699 handlers. The first one is always expected to be a string meant to be
6700 turned into an identifier. The next ones are all static expressions,
6701 among which strings meant to be turned into an identifier, except for
6702 a couple of specific attributes that require raw strings. */
6703 if (Present (gnat_next_arg))
6704 {
6705 gnu_arg1 = gnat_to_gnu (Expression (gnat_next_arg));
6706 gcc_assert (TREE_CODE (gnu_arg1) == STRING_CST);
6707
6708 const char *const p = TREE_STRING_POINTER (gnu_arg1);
6709 const bool string_args
6710 = strcmp (p, "target") == 0 || strcmp (p, "target_clones") == 0;
6711 gnu_arg1 = get_identifier (p);
6712 if (IDENTIFIER_LENGTH (gnu_arg1) == 0)
6713 return;
6714 gnat_next_arg = Next (gnat_next_arg);
6715
6716 while (Present (gnat_next_arg))
6717 {
6718 tree gnu_arg = gnat_to_gnu (Expression (gnat_next_arg));
6719 if (TREE_CODE (gnu_arg) == STRING_CST && !string_args)
6720 gnu_arg = get_identifier (TREE_STRING_POINTER (gnu_arg));
6721 gnu_arg_list
6722 = chainon (gnu_arg_list, build_tree_list (NULL_TREE, gnu_arg));
6723 gnat_next_arg = Next (gnat_next_arg);
6724 }
6725 }
6726
6727 prepend_one_attribute (attr_list, etype, gnu_arg1, gnu_arg_list,
6728 Present (Next (gnat_arg))
6729 ? Expression (Next (gnat_arg)) : gnat_pragma);
6730 }
6731
6732 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6733
6734 static void
6735 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6736 {
6737 Node_Id gnat_temp;
6738
6739 /* Attributes are stored as Representation Item pragmas. */
6740 for (gnat_temp = First_Rep_Item (gnat_entity);
6741 Present (gnat_temp);
6742 gnat_temp = Next_Rep_Item (gnat_temp))
6743 if (Nkind (gnat_temp) == N_Pragma)
6744 prepend_one_attribute_pragma (attr_list, gnat_temp);
6745 }
6746
6747 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6748 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6749 return the GCC tree to use for that expression. S is the suffix to use
6750 if a variable needs to be created and DEFINITION is true if this is done
6751 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6752 otherwise, we are just elaborating the expression for side-effects. If
6753 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6754 isn't needed for code generation. */
6755
6756 static tree
6757 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6758 bool definition, bool need_value, bool need_debug)
6759 {
6760 tree gnu_expr;
6761
6762 /* If we already elaborated this expression (e.g. it was involved
6763 in the definition of a private type), use the old value. */
6764 if (present_gnu_tree (gnat_expr))
6765 return get_gnu_tree (gnat_expr);
6766
6767 /* If we don't need a value and this is static or a discriminant,
6768 we don't need to do anything. */
6769 if (!need_value
6770 && (Compile_Time_Known_Value (gnat_expr)
6771 || (Nkind (gnat_expr) == N_Identifier
6772 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6773 return NULL_TREE;
6774
6775 /* If it's a static expression, we don't need a variable for debugging. */
6776 if (need_debug && Compile_Time_Known_Value (gnat_expr))
6777 need_debug = false;
6778
6779 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6780 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6781 definition, need_debug);
6782
6783 /* Save the expression in case we try to elaborate this entity again. Since
6784 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6785 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6786 save_gnu_tree (gnat_expr, gnu_expr, true);
6787
6788 return need_value ? gnu_expr : error_mark_node;
6789 }
6790
6791 /* Similar, but take a GNU expression and always return a result. */
6792
6793 static tree
6794 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6795 bool definition, bool need_debug)
6796 {
6797 const bool expr_public_p = Is_Public (gnat_entity);
6798 const bool expr_global_p = expr_public_p || global_bindings_p ();
6799 bool expr_variable_p, use_variable;
6800
6801 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6802 that an expression cannot contain both a discriminant and a variable. */
6803 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6804 return gnu_expr;
6805
6806 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6807 a variable that is initialized to contain the expression when the package
6808 containing the definition is elaborated. If this entity is defined at top
6809 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6810 if this is necessary. */
6811 if (TREE_CONSTANT (gnu_expr))
6812 expr_variable_p = false;
6813 else
6814 {
6815 /* Skip any conversions and simple constant arithmetics to see if the
6816 expression is based on a read-only variable. */
6817 tree inner = remove_conversions (gnu_expr, true);
6818
6819 inner = skip_simple_constant_arithmetic (inner);
6820
6821 if (handled_component_p (inner))
6822 inner = get_inner_constant_reference (inner);
6823
6824 expr_variable_p
6825 = !(inner
6826 && TREE_CODE (inner) == VAR_DECL
6827 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6828 }
6829
6830 /* We only need to use the variable if we are in a global context since GCC
6831 can do the right thing in the local case. However, when not optimizing,
6832 use it for bounds of loop iteration scheme to avoid code duplication. */
6833 use_variable = expr_variable_p
6834 && (expr_global_p
6835 || (!optimize
6836 && definition
6837 && Is_Itype (gnat_entity)
6838 && Nkind (Associated_Node_For_Itype (gnat_entity))
6839 == N_Loop_Parameter_Specification));
6840
6841 /* If the GNAT encodings are not used, we don't need a variable for debug
6842 info purposes if the expression is a constant or another variable, but
6843 we need to be careful because we do not generate debug info for external
6844 variables so DECL_IGNORED_P is not stable across units. */
6845 if (need_debug
6846 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
6847 && (TREE_CONSTANT (gnu_expr)
6848 || (!expr_public_p
6849 && DECL_P (gnu_expr)
6850 && !DECL_IGNORED_P (gnu_expr))))
6851 need_debug = false;
6852
6853 /* Now create it, possibly only for debugging purposes. */
6854 if (use_variable || need_debug)
6855 {
6856 /* The following variable creation can happen when processing the body
6857 of subprograms that are defined out of the extended main unit and
6858 inlined. In this case, we are not at the global scope, and thus the
6859 new variable must not be tagged "external", as we used to do here as
6860 soon as DEFINITION was false. */
6861 tree gnu_decl
6862 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6863 TREE_TYPE (gnu_expr), gnu_expr, true,
6864 expr_public_p, !definition && expr_global_p,
6865 expr_global_p, false, true, need_debug,
6866 NULL, gnat_entity);
6867
6868 /* Using this variable at debug time (if need_debug is true) requires a
6869 proper location. The back-end will compute a location for this
6870 variable only if the variable is used by the generated code.
6871 Returning the variable ensures the caller will use it in generated
6872 code. Note that there is no need for a location if the debug info
6873 contains an integer constant. */
6874 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6875 return gnu_decl;
6876 }
6877
6878 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6879 }
6880
6881 /* Similar, but take an alignment factor and make it explicit in the tree. */
6882
6883 static tree
6884 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6885 bool definition, bool need_debug, unsigned int align)
6886 {
6887 tree unit_align = size_int (align / BITS_PER_UNIT);
6888 return
6889 size_binop (MULT_EXPR,
6890 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6891 gnu_expr,
6892 unit_align),
6893 gnat_entity, s, definition,
6894 need_debug),
6895 unit_align);
6896 }
6897
6898 /* Structure to hold internal data for elaborate_reference. */
6899
6900 struct er_data
6901 {
6902 Entity_Id entity;
6903 bool definition;
6904 unsigned int n;
6905 };
6906
6907 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6908
6909 static tree
6910 elaborate_reference_1 (tree ref, void *data)
6911 {
6912 struct er_data *er = (struct er_data *)data;
6913 char suffix[16];
6914
6915 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6916 if (TREE_CONSTANT (ref))
6917 return ref;
6918
6919 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6920 pointer. This may be more efficient, but will also allow us to more
6921 easily find the match for the PLACEHOLDER_EXPR. */
6922 if (TREE_CODE (ref) == COMPONENT_REF
6923 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6924 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6925 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6926 TREE_OPERAND (ref, 1), NULL_TREE);
6927
6928 /* If this is the displacement of a pointer, elaborate the pointer and then
6929 displace the result. The actual purpose here is to drop the location on
6930 the expression, which may be problematic if replicated on references. */
6931 if (TREE_CODE (ref) == POINTER_PLUS_EXPR
6932 && TREE_CODE (TREE_OPERAND (ref, 1)) == INTEGER_CST)
6933 return build2 (POINTER_PLUS_EXPR, TREE_TYPE (ref),
6934 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6935 TREE_OPERAND (ref, 1));
6936
6937 sprintf (suffix, "EXP%d", ++er->n);
6938 return
6939 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6940 }
6941
6942 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6943 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6944 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6945
6946 static tree
6947 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6948 tree *init)
6949 {
6950 struct er_data er = { gnat_entity, definition, 0 };
6951 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6952 }
6953
6954 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6955 the value passed against the list of choices. */
6956
6957 static tree
6958 choices_to_gnu (tree gnu_operand, Node_Id gnat_choices)
6959 {
6960 tree gnu_result = boolean_false_node, gnu_type;
6961
6962 gnu_operand = maybe_character_value (gnu_operand);
6963 gnu_type = TREE_TYPE (gnu_operand);
6964
6965 for (Node_Id gnat_choice = First (gnat_choices);
6966 Present (gnat_choice);
6967 gnat_choice = Next (gnat_choice))
6968 {
6969 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
6970 tree gnu_test;
6971
6972 switch (Nkind (gnat_choice))
6973 {
6974 case N_Range:
6975 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
6976 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
6977 break;
6978
6979 case N_Subtype_Indication:
6980 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
6981 (Constraint (gnat_choice))));
6982 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
6983 (Constraint (gnat_choice))));
6984 break;
6985
6986 case N_Identifier:
6987 case N_Expanded_Name:
6988 /* This represents either a subtype range or a static value of
6989 some kind; Ekind says which. */
6990 if (Is_Type (Entity (gnat_choice)))
6991 {
6992 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
6993
6994 gnu_low = TYPE_MIN_VALUE (gnu_type);
6995 gnu_high = TYPE_MAX_VALUE (gnu_type);
6996 break;
6997 }
6998
6999 /* ... fall through ... */
7000
7001 case N_Character_Literal:
7002 case N_Integer_Literal:
7003 gnu_low = gnat_to_gnu (gnat_choice);
7004 break;
7005
7006 case N_Others_Choice:
7007 break;
7008
7009 default:
7010 gcc_unreachable ();
7011 }
7012
7013 /* Everything should be folded into constants at this point. */
7014 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
7015 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
7016
7017 if (gnu_low && TREE_TYPE (gnu_low) != gnu_type)
7018 gnu_low = convert (gnu_type, gnu_low);
7019 if (gnu_high && TREE_TYPE (gnu_high) != gnu_type)
7020 gnu_high = convert (gnu_type, gnu_high);
7021
7022 if (gnu_low && gnu_high)
7023 gnu_test
7024 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
7025 build_binary_op (GE_EXPR, boolean_type_node,
7026 gnu_operand, gnu_low, true),
7027 build_binary_op (LE_EXPR, boolean_type_node,
7028 gnu_operand, gnu_high, true),
7029 true);
7030 else if (gnu_low == boolean_true_node
7031 && TREE_TYPE (gnu_operand) == boolean_type_node)
7032 gnu_test = gnu_operand;
7033 else if (gnu_low)
7034 gnu_test
7035 = build_binary_op (EQ_EXPR, boolean_type_node, gnu_operand, gnu_low,
7036 true);
7037 else
7038 gnu_test = boolean_true_node;
7039
7040 if (gnu_result == boolean_false_node)
7041 gnu_result = gnu_test;
7042 else
7043 gnu_result
7044 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_result,
7045 gnu_test, true);
7046 }
7047
7048 return gnu_result;
7049 }
7050
7051 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
7052 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
7053
7054 static int
7055 adjust_packed (tree field_type, tree record_type, int packed)
7056 {
7057 /* If the field contains an array with self-referential size, we'd better
7058 not pack it because this would misalign it and, therefore, cause large
7059 temporaries to be created in case we need to take the address of the
7060 field. See addressable_p and the notes on the addressability issues
7061 for further details. */
7062 if (AGGREGATE_TYPE_P (field_type)
7063 && aggregate_type_contains_array_p (field_type, true))
7064 return 0;
7065
7066 /* In the other cases, we can honor the packing. */
7067 if (packed)
7068 return packed;
7069
7070 /* If the alignment of the record is specified and the field type
7071 is over-aligned, request Storage_Unit alignment for the field. */
7072 if (TYPE_ALIGN (record_type)
7073 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
7074 return -1;
7075
7076 /* Likewise if the maximum alignment of the record is specified. */
7077 if (TYPE_MAX_ALIGN (record_type)
7078 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
7079 return -1;
7080
7081 return 0;
7082 }
7083
7084 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
7085 placed in GNU_RECORD_TYPE.
7086
7087 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
7088 record has Component_Alignment of Storage_Unit.
7089
7090 DEFINITION is true if this field is for a record being defined.
7091
7092 DEBUG_INFO_P is true if we need to write debug information for types
7093 that we may create in the process. */
7094
7095 static tree
7096 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
7097 bool definition, bool debug_info_p)
7098 {
7099 const Node_Id gnat_clause = Component_Clause (gnat_field);
7100 const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field));
7101 const Entity_Id gnat_field_type = Etype (gnat_field);
7102 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
7103 tree gnu_field_id = get_entity_name (gnat_field);
7104 const bool is_aliased = Is_Aliased (gnat_field);
7105 const bool is_full_access
7106 = (Is_Full_Access (gnat_field) || Is_Full_Access (gnat_field_type));
7107 const bool is_independent
7108 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
7109 const bool is_volatile
7110 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
7111 const bool is_by_ref = TYPE_IS_BY_REFERENCE_P (gnu_field_type);
7112 const bool is_strict_alignment = Strict_Alignment (gnat_field_type);
7113 /* We used to consider that volatile fields also require strict alignment,
7114 but that was an interpolation and would cause us to reject a pragma
7115 volatile on a packed record type containing boolean components, while
7116 there is no basis to do so in the RM. In such cases, the writes will
7117 involve load-modify-store sequences, but that's OK for volatile. The
7118 only constraint is the implementation advice whereby only the bits of
7119 the components should be accessed if they both start and end on byte
7120 boundaries, but that should be guaranteed by the GCC memory model.
7121 Note that we have some redundancies (is_full_access => is_independent,
7122 is_aliased => is_independent and is_by_ref => is_strict_alignment)
7123 so the following formula is sufficient. */
7124 const bool needs_strict_alignment = (is_independent || is_strict_alignment);
7125 const char *field_s, *size_s;
7126 tree gnu_field, gnu_size, gnu_pos;
7127 bool is_bitfield;
7128
7129 /* The qualifier to be used in messages. */
7130 if (is_aliased)
7131 field_s = "aliased&";
7132 else if (is_full_access)
7133 {
7134 if (Is_Volatile_Full_Access (gnat_field)
7135 || Is_Volatile_Full_Access (gnat_field_type))
7136 field_s = "volatile full access&";
7137 else
7138 field_s = "atomic&";
7139 }
7140 else if (is_independent)
7141 field_s = "independent&";
7142 else if (is_by_ref)
7143 field_s = "& with by-reference type";
7144 else if (is_strict_alignment)
7145 field_s = "& with aliased part";
7146 else
7147 field_s = "&";
7148
7149 /* The message to be used for incompatible size. */
7150 if (is_aliased || is_full_access)
7151 size_s = "size for %s must be ^";
7152 else if (field_s)
7153 size_s = "size for %s too small{, minimum allowed is ^}";
7154
7155 /* If a field requires strict alignment, we cannot pack it (RM 13.2(7)). */
7156 if (needs_strict_alignment)
7157 packed = 0;
7158 else
7159 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
7160
7161 /* If a size is specified, use it. Otherwise, if the record type is packed,
7162 use the official RM size. See "Handling of Type'Size Values" in Einfo
7163 for further details. */
7164 if (Present (gnat_clause) || Known_Esize (gnat_field))
7165 gnu_size = validate_size (Esize (gnat_field), gnu_field_type, gnat_field,
7166 FIELD_DECL, false, true, size_s, field_s);
7167 else if (packed == 1)
7168 {
7169 gnu_size = rm_size (gnu_field_type);
7170 if (TREE_CODE (gnu_size) != INTEGER_CST)
7171 gnu_size = NULL_TREE;
7172 }
7173 else
7174 gnu_size = NULL_TREE;
7175
7176 /* Likewise for the position. */
7177 if (Present (gnat_clause))
7178 {
7179 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
7180 is_bitfield = !value_factor_p (gnu_pos, BITS_PER_UNIT);
7181 }
7182
7183 /* If the record has rep clauses and this is the tag field, make a rep
7184 clause for it as well. */
7185 else if (Has_Specified_Layout (gnat_record_type)
7186 && Chars (gnat_field) == Name_uTag)
7187 {
7188 gnu_pos = bitsize_zero_node;
7189 gnu_size = TYPE_SIZE (gnu_field_type);
7190 is_bitfield = false;
7191 }
7192
7193 else
7194 {
7195 gnu_pos = NULL_TREE;
7196 is_bitfield = false;
7197 }
7198
7199 /* If the field's type is a fixed-size record that does not require strict
7200 alignment, and the record is packed or we have a position specified for
7201 the field that makes it a bitfield or we have a specified size that is
7202 smaller than that of the field's type, then see if we can get either an
7203 integral mode form of the field's type or a smaller form. If we can,
7204 consider that a size was specified for the field if there wasn't one
7205 already, so we know to make it a bitfield and avoid making things wider.
7206
7207 Changing to an integral mode form is useful when the record is packed as
7208 we can then place the field at a non-byte-aligned position and so achieve
7209 tighter packing. This is in addition required if the field shares a byte
7210 with another field and the front-end lets the back-end handle the access
7211 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
7212
7213 Changing to a smaller form is required if the specified size is smaller
7214 than that of the field's type and the type contains sub-fields that are
7215 padded, in order to avoid generating accesses to these sub-fields that
7216 are wider than the field.
7217
7218 We avoid the transformation if it is not required or potentially useful,
7219 as it might entail an increase of the field's alignment and have ripple
7220 effects on the outer record type. A typical case is a field known to be
7221 byte-aligned and not to share a byte with another field. */
7222 if (!needs_strict_alignment
7223 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
7224 && !TYPE_FAT_POINTER_P (gnu_field_type)
7225 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
7226 && (packed == 1
7227 || is_bitfield
7228 || (gnu_size
7229 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))))
7230 {
7231 tree gnu_packable_type
7232 = make_packable_type (gnu_field_type, true, is_bitfield ? 1 : 0);
7233 if (gnu_packable_type != gnu_field_type)
7234 {
7235 gnu_field_type = gnu_packable_type;
7236 if (!gnu_size)
7237 gnu_size = rm_size (gnu_field_type);
7238 }
7239 }
7240
7241 /* Now check if the type of the field allows atomic access. */
7242 if (Is_Full_Access (gnat_field))
7243 {
7244 const unsigned int align
7245 = promote_object_alignment (gnu_field_type, gnat_field);
7246 if (align > 0)
7247 gnu_field_type
7248 = maybe_pad_type (gnu_field_type, NULL_TREE, align, gnat_field,
7249 false, definition, true);
7250 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
7251 }
7252
7253 /* If a position is specified, check that it is valid. */
7254 if (gnu_pos)
7255 {
7256 Entity_Id gnat_parent = Parent_Subtype (gnat_record_type);
7257
7258 /* Ensure the position doesn't overlap with the parent subtype if there
7259 is one. It would be impossible to build CONSTRUCTORs and accessing
7260 the parent could clobber the component in the extension if directly
7261 done. We accept it with -gnatd.K for the sake of compatibility. */
7262 if (Present (gnat_parent)
7263 && !(Debug_Flag_Dot_KK && Is_Fully_Repped_Tagged_Type (gnat_parent)))
7264 {
7265 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
7266
7267 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
7268 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
7269 post_error_ne_tree
7270 ("position for& must be beyond parent{, minimum allowed is ^}",
7271 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
7272 }
7273
7274 /* If this field needs strict alignment, make sure that the record is
7275 sufficiently aligned and that the position and size are consistent
7276 with the type. But don't do it if we are just annotating types and
7277 the field's type is tagged, since tagged types aren't fully laid out
7278 in this mode. Also, note that atomic implies volatile so the inner
7279 test sequences ordering is significant here. */
7280 if (needs_strict_alignment
7281 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
7282 {
7283 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
7284
7285 if (TYPE_ALIGN (gnu_record_type)
7286 && TYPE_ALIGN (gnu_record_type) < type_align)
7287 SET_TYPE_ALIGN (gnu_record_type, type_align);
7288
7289 /* If the position is not a multiple of the storage unit, then error
7290 out and reset the position. */
7291 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
7292 bitsize_unit_node)))
7293 {
7294 char s[128];
7295 snprintf (s, sizeof (s), "position for %s must be "
7296 "multiple of Storage_Unit", field_s);
7297 post_error_ne (s, First_Bit (gnat_clause), gnat_field);
7298 gnu_pos = NULL_TREE;
7299 }
7300
7301 /* If the position is not a multiple of the alignment of the type,
7302 then error out and reset the position. */
7303 else if (type_align > BITS_PER_UNIT
7304 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
7305 bitsize_int (type_align))))
7306 {
7307 char s[128];
7308 snprintf (s, sizeof (s), "position for %s must be multiple of ^",
7309 field_s);
7310 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
7311 type_align / BITS_PER_UNIT);
7312 post_error_ne_num ("\\because alignment of its type& is ^",
7313 First_Bit (gnat_clause), Etype (gnat_field),
7314 type_align / BITS_PER_UNIT);
7315 gnu_pos = NULL_TREE;
7316 }
7317
7318 if (gnu_size)
7319 {
7320 tree type_size = TYPE_SIZE (gnu_field_type);
7321 int cmp;
7322
7323 /* If the size is not a multiple of the storage unit, then error
7324 out and reset the size. */
7325 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
7326 bitsize_unit_node)))
7327 {
7328 char s[128];
7329 snprintf (s, sizeof (s), "size for %s must be "
7330 "multiple of Storage_Unit", field_s);
7331 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
7332 gnu_size = NULL_TREE;
7333 }
7334
7335 /* If the size is lower than that of the type, or greater for
7336 atomic and aliased, then error out and reset the size. */
7337 else if ((cmp = tree_int_cst_compare (gnu_size, type_size)) < 0
7338 || (cmp > 0 && (is_aliased || is_full_access)))
7339 {
7340 char s[128];
7341 snprintf (s, sizeof (s), size_s, field_s);
7342 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
7343 type_size);
7344 gnu_size = NULL_TREE;
7345 }
7346 }
7347 }
7348 }
7349
7350 else
7351 {
7352 /* If we are packing the record and the field is BLKmode, round the
7353 size up to a byte boundary. */
7354 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7355 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7356 }
7357
7358 /* We need to make the size the maximum for the type if it is
7359 self-referential and an unconstrained type. In that case, we can't
7360 pack the field since we can't make a copy to align it. */
7361 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7362 && !gnu_size
7363 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7364 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7365 {
7366 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7367 packed = 0;
7368 }
7369
7370 /* If a size is specified, adjust the field's type to it. */
7371 if (gnu_size)
7372 {
7373 tree orig_field_type;
7374
7375 /* If the field's type is justified modular, we would need to remove
7376 the wrapper to (better) meet the layout requirements. However we
7377 can do so only if the field is not aliased to preserve the unique
7378 layout, if it has the same storage order as the enclosing record
7379 and if the prescribed size is not greater than that of the packed
7380 array to preserve the justification. */
7381 if (!needs_strict_alignment
7382 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7383 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7384 && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type)
7385 == Reverse_Storage_Order (gnat_record_type)
7386 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7387 <= 0)
7388 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7389
7390 /* Similarly if the field's type is a misaligned integral type, but
7391 there is no restriction on the size as there is no justification. */
7392 if (!needs_strict_alignment
7393 && TYPE_IS_PADDING_P (gnu_field_type)
7394 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7395 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7396
7397 orig_field_type = gnu_field_type;
7398 gnu_field_type
7399 = make_type_from_size (gnu_field_type, gnu_size,
7400 Has_Biased_Representation (gnat_field));
7401
7402 /* If the type has been extended, we may need to cap the alignment. */
7403 if (!needs_strict_alignment
7404 && gnu_field_type != orig_field_type
7405 && tree_int_cst_lt (TYPE_SIZE (orig_field_type), gnu_size))
7406 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
7407
7408 orig_field_type = gnu_field_type;
7409 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7410 false, definition, true);
7411
7412 /* If a padding record was made, declare it now since it will never be
7413 declared otherwise. This is necessary to ensure that its subtrees
7414 are properly marked. */
7415 if (gnu_field_type != orig_field_type
7416 && !DECL_P (TYPE_NAME (gnu_field_type)))
7417 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7418 debug_info_p, gnat_field);
7419 }
7420
7421 /* Otherwise (or if there was an error), don't specify a position. */
7422 else
7423 gnu_pos = NULL_TREE;
7424
7425 /* If the field's type is a padded type made for a scalar field of a record
7426 type with reverse storage order, we need to propagate the reverse storage
7427 order to the padding type since it is the innermost enclosing aggregate
7428 type around the scalar. */
7429 if (TYPE_IS_PADDING_P (gnu_field_type)
7430 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7431 && Is_Scalar_Type (gnat_field_type))
7432 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7433
7434 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7435 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7436
7437 /* Now create the decl for the field. */
7438 gnu_field
7439 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7440 gnu_size, gnu_pos, packed, is_aliased);
7441 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7442 DECL_ALIASED_P (gnu_field) = is_aliased;
7443 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7444
7445 /* If this is a discriminant, then we treat it specially: first, we set its
7446 index number for the back-annotation; second, we record whether it cannot
7447 be changed once it has been set for the computation of loop invariants;
7448 third, we make it addressable in order for the optimizer to more easily
7449 see that it cannot be modified by assignments to the other fields of the
7450 record (see create_field_decl for a more detailed explanation), which is
7451 crucial to hoist the offset and size computations of dynamic fields. */
7452 if (Ekind (gnat_field) == E_Discriminant)
7453 {
7454 DECL_DISCRIMINANT_NUMBER (gnu_field)
7455 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7456 DECL_INVARIANT_P (gnu_field)
7457 = No (Discriminant_Default_Value (gnat_field));
7458 DECL_NONADDRESSABLE_P (gnu_field) = 0;
7459 }
7460
7461 return gnu_field;
7462 }
7463
7464 /* Return true if at least one member of COMPONENT_LIST needs strict
7465 alignment. */
7466
7467 static bool
7468 components_need_strict_alignment (Node_Id component_list)
7469 {
7470 Node_Id component_decl;
7471
7472 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7473 Present (component_decl);
7474 component_decl = Next_Non_Pragma (component_decl))
7475 {
7476 Entity_Id gnat_field = Defining_Entity (component_decl);
7477
7478 if (Is_Independent (gnat_field) || Is_Independent (Etype (gnat_field)))
7479 return true;
7480
7481 if (Strict_Alignment (Etype (gnat_field)))
7482 return true;
7483 }
7484
7485 return false;
7486 }
7487
7488 /* Return true if FIELD is an artificial field. */
7489
7490 static bool
7491 field_is_artificial (tree field)
7492 {
7493 /* These fields are generated by the front-end proper. */
7494 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7495 return true;
7496
7497 /* These fields are generated by gigi. */
7498 if (DECL_INTERNAL_P (field))
7499 return true;
7500
7501 return false;
7502 }
7503
7504 /* Return true if FIELD is a non-artificial field with self-referential
7505 size. */
7506
7507 static bool
7508 field_has_self_size (tree field)
7509 {
7510 if (field_is_artificial (field))
7511 return false;
7512
7513 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7514 return false;
7515
7516 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7517 }
7518
7519 /* Return true if FIELD is a non-artificial field with variable size. */
7520
7521 static bool
7522 field_has_variable_size (tree field)
7523 {
7524 if (field_is_artificial (field))
7525 return false;
7526
7527 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7528 return false;
7529
7530 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7531 }
7532
7533 /* qsort comparer for the bit positions of two record components. */
7534
7535 static int
7536 compare_field_bitpos (const PTR rt1, const PTR rt2)
7537 {
7538 const_tree const field1 = * (const_tree const *) rt1;
7539 const_tree const field2 = * (const_tree const *) rt2;
7540 const int ret
7541 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7542
7543 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7544 }
7545
7546 /* Sort the LIST of fields in reverse order of increasing position. */
7547
7548 static tree
7549 reverse_sort_field_list (tree list)
7550 {
7551 const int len = list_length (list);
7552 tree *field_arr = XALLOCAVEC (tree, len);
7553
7554 for (int i = 0; list; list = DECL_CHAIN (list), i++)
7555 field_arr[i] = list;
7556
7557 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
7558
7559 for (int i = 0; i < len; i++)
7560 {
7561 DECL_CHAIN (field_arr[i]) = list;
7562 list = field_arr[i];
7563 }
7564
7565 return list;
7566 }
7567
7568 /* Reverse function from gnat_to_gnu_field: return the GNAT field present in
7569 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and
7570 corresponding to the GNU tree GNU_FIELD. */
7571
7572 static Entity_Id
7573 gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list,
7574 Entity_Id gnat_record_type)
7575 {
7576 Entity_Id gnat_component_decl, gnat_field;
7577
7578 if (Present (Component_Items (gnat_component_list)))
7579 for (gnat_component_decl
7580 = First_Non_Pragma (Component_Items (gnat_component_list));
7581 Present (gnat_component_decl);
7582 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7583 {
7584 gnat_field = Defining_Entity (gnat_component_decl);
7585 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7586 return gnat_field;
7587 }
7588
7589 if (Has_Discriminants (gnat_record_type))
7590 for (gnat_field = First_Stored_Discriminant (gnat_record_type);
7591 Present (gnat_field);
7592 gnat_field = Next_Stored_Discriminant (gnat_field))
7593 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7594 return gnat_field;
7595
7596 return Empty;
7597 }
7598
7599 /* Issue a warning for the problematic placement of GNU_FIELD present in
7600 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE.
7601 IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant.
7602 DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */
7603
7604 static void
7605 warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list,
7606 Entity_Id gnat_record_type, bool in_variant,
7607 bool do_reorder)
7608 {
7609 if (!Comes_From_Source (gnat_record_type))
7610 return;
7611
7612 Entity_Id gnat_field
7613 = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type);
7614 gcc_assert (Present (gnat_field));
7615
7616 const char *msg1
7617 = in_variant
7618 ? "?variant layout may cause performance issues"
7619 : "?record layout may cause performance issues";
7620 const char *msg2
7621 = Ekind (gnat_field) == E_Discriminant
7622 ? "?discriminant & whose length is not multiple of a byte"
7623 : field_has_self_size (gnu_field)
7624 ? "?component & whose length depends on a discriminant"
7625 : field_has_variable_size (gnu_field)
7626 ? "?component & whose length is not fixed"
7627 : "?component & whose length is not multiple of a byte";
7628 const char *msg3
7629 = do_reorder
7630 ? "?comes too early and was moved down"
7631 : "?comes too early and ought to be moved down";
7632
7633 post_error (msg1, gnat_field);
7634 post_error_ne (msg2, gnat_field, gnat_field);
7635 post_error (msg3, gnat_field);
7636 }
7637
7638 /* Likewise but for every field present on GNU_FIELD_LIST. */
7639
7640 static void
7641 warn_on_list_placement (tree gnu_field_list, Node_Id gnat_component_list,
7642 Entity_Id gnat_record_type, bool in_variant,
7643 bool do_reorder)
7644 {
7645 for (tree gnu_tmp = gnu_field_list; gnu_tmp; gnu_tmp = DECL_CHAIN (gnu_tmp))
7646 warn_on_field_placement (gnu_tmp, gnat_component_list, gnat_record_type,
7647 in_variant, do_reorder);
7648 }
7649
7650 /* Structure holding information for a given variant. */
7651 typedef struct vinfo
7652 {
7653 /* The record type of the variant. */
7654 tree type;
7655
7656 /* The name of the variant. */
7657 tree name;
7658
7659 /* The qualifier of the variant. */
7660 tree qual;
7661
7662 /* Whether the variant has a rep clause. */
7663 bool has_rep;
7664
7665 /* Whether the variant is packed. */
7666 bool packed;
7667
7668 } vinfo_t;
7669
7670 /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to
7671 GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and
7672 finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects
7673 the layout (see below). When called from gnat_to_gnu_entity during the
7674 processing of a record definition, the GCC node for the parent, if any,
7675 will be the single field of GNU_RECORD_TYPE and the GCC nodes for the
7676 discriminants will be on GNU_FIELD_LIST. The other call to this function
7677 is a recursive call for the component list of a variant and, in this case,
7678 GNU_FIELD_LIST is empty. Note that GNAT_COMPONENT_LIST may be Empty.
7679
7680 PACKED is 1 if this is for a packed record or -1 if this is for a record
7681 with Component_Alignment of Storage_Unit.
7682
7683 DEFINITION is true if we are defining this record type.
7684
7685 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7686 out the record. This means the alignment only serves to force fields to
7687 be bitfields, but not to require the record to be that aligned. This is
7688 used for variants.
7689
7690 ALL_REP is true if a rep clause is present for all the fields.
7691
7692 UNCHECKED_UNION is true if we are building this type for a record with a
7693 Pragma Unchecked_Union.
7694
7695 ARTIFICIAL is true if this is a type that was generated by the compiler.
7696
7697 DEBUG_INFO is true if we need to write debug information about the type.
7698
7699 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7700 mean that its contents may be unused as well, only the container itself.
7701
7702 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7703 the outer record type down to this variant level. It is nonzero only if
7704 all the fields down to this level have a rep clause and ALL_REP is false.
7705
7706 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7707 with a rep clause is to be added; in this case, that is all that should
7708 be done with such fields and the return value will be false. */
7709
7710 static bool
7711 components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type,
7712 tree gnu_field_list, tree gnu_record_type, int packed,
7713 bool definition, bool cancel_alignment, bool all_rep,
7714 bool unchecked_union, bool artificial, bool debug_info,
7715 bool maybe_unused, tree first_free_pos,
7716 tree *p_gnu_rep_list)
7717 {
7718 const bool needs_xv_encodings
7719 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7720 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7721 bool variants_have_rep = all_rep;
7722 bool layout_with_rep = false;
7723 bool has_non_packed_fixed_size_field = false;
7724 bool has_self_field = false;
7725 bool has_aliased_after_self_field = false;
7726 Entity_Id gnat_component_decl, gnat_variant_part;
7727 tree gnu_field, gnu_next, gnu_last;
7728 tree gnu_variant_part = NULL_TREE;
7729 tree gnu_rep_list = NULL_TREE;
7730
7731 /* For each component referenced in a component declaration create a GCC
7732 field and add it to the list, skipping pragmas in the GNAT list. */
7733 gnu_last = tree_last (gnu_field_list);
7734 if (Present (gnat_component_list)
7735 && (Present (Component_Items (gnat_component_list))))
7736 for (gnat_component_decl
7737 = First_Non_Pragma (Component_Items (gnat_component_list));
7738 Present (gnat_component_decl);
7739 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7740 {
7741 Entity_Id gnat_field = Defining_Entity (gnat_component_decl);
7742 Name_Id gnat_name = Chars (gnat_field);
7743
7744 /* If present, the _Parent field must have been created as the single
7745 field of the record type. Put it before any other fields. */
7746 if (gnat_name == Name_uParent)
7747 {
7748 gnu_field = TYPE_FIELDS (gnu_record_type);
7749 gnu_field_list = chainon (gnu_field_list, gnu_field);
7750 }
7751 else
7752 {
7753 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7754 definition, debug_info);
7755
7756 /* If this is the _Tag field, put it before any other fields. */
7757 if (gnat_name == Name_uTag)
7758 gnu_field_list = chainon (gnu_field_list, gnu_field);
7759
7760 /* If this is the _Controller field, put it before the other
7761 fields except for the _Tag or _Parent field. */
7762 else if (gnat_name == Name_uController && gnu_last)
7763 {
7764 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7765 DECL_CHAIN (gnu_last) = gnu_field;
7766 }
7767
7768 /* If this is a regular field, put it after the other fields. */
7769 else
7770 {
7771 DECL_CHAIN (gnu_field) = gnu_field_list;
7772 gnu_field_list = gnu_field;
7773 if (!gnu_last)
7774 gnu_last = gnu_field;
7775
7776 /* And record information for the final layout. */
7777 if (field_has_self_size (gnu_field))
7778 has_self_field = true;
7779 else if (has_self_field && DECL_ALIASED_P (gnu_field))
7780 has_aliased_after_self_field = true;
7781 else if (!DECL_FIELD_OFFSET (gnu_field)
7782 && !DECL_PACKED (gnu_field)
7783 && !field_has_variable_size (gnu_field))
7784 has_non_packed_fixed_size_field = true;
7785 }
7786 }
7787
7788 save_gnu_tree (gnat_field, gnu_field, false);
7789 }
7790
7791 /* At the end of the component list there may be a variant part. */
7792 if (Present (gnat_component_list))
7793 gnat_variant_part = Variant_Part (gnat_component_list);
7794 else
7795 gnat_variant_part = Empty;
7796
7797 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7798 mutually exclusive and should go in the same memory. To do this we need
7799 to treat each variant as a record whose elements are created from the
7800 component list for the variant. So here we create the records from the
7801 lists for the variants and put them all into the QUAL_UNION_TYPE.
7802 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7803 use GNU_RECORD_TYPE if there are no fields so far. */
7804 if (Present (gnat_variant_part))
7805 {
7806 Node_Id gnat_discr = Name (gnat_variant_part), variant;
7807 tree gnu_discr = gnat_to_gnu (gnat_discr);
7808 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7809 tree gnu_var_name
7810 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7811 "XVN");
7812 tree gnu_union_name
7813 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7814 tree gnu_union_type;
7815 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7816 bool union_field_needs_strict_alignment = false;
7817 auto_vec <vinfo_t, 16> variant_types;
7818 vinfo_t *gnu_variant;
7819 unsigned int variants_align = 0;
7820 unsigned int i;
7821
7822 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7823 are all in the variant part, to match the layout of C unions. There
7824 is an associated check below. */
7825 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7826 gnu_union_type = gnu_record_type;
7827 else
7828 {
7829 gnu_union_type
7830 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7831
7832 TYPE_NAME (gnu_union_type) = gnu_union_name;
7833 SET_TYPE_ALIGN (gnu_union_type, 0);
7834 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7835 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7836 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7837 }
7838
7839 /* If all the fields down to this level have a rep clause, find out
7840 whether all the fields at this level also have one. If so, then
7841 compute the new first free position to be passed downward. */
7842 this_first_free_pos = first_free_pos;
7843 if (this_first_free_pos)
7844 {
7845 for (gnu_field = gnu_field_list;
7846 gnu_field;
7847 gnu_field = DECL_CHAIN (gnu_field))
7848 if (DECL_FIELD_OFFSET (gnu_field))
7849 {
7850 tree pos = bit_position (gnu_field);
7851 if (!tree_int_cst_lt (pos, this_first_free_pos))
7852 this_first_free_pos
7853 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7854 }
7855 else
7856 {
7857 this_first_free_pos = NULL_TREE;
7858 break;
7859 }
7860 }
7861
7862 /* We build the variants in two passes. The bulk of the work is done in
7863 the first pass, that is to say translating the GNAT nodes, building
7864 the container types and computing the associated properties. However
7865 we cannot finish up the container types during this pass because we
7866 don't know where the variant part will be placed until the end. */
7867 for (variant = First_Non_Pragma (Variants (gnat_variant_part));
7868 Present (variant);
7869 variant = Next_Non_Pragma (variant))
7870 {
7871 tree gnu_variant_type = make_node (RECORD_TYPE);
7872 tree gnu_inner_name, gnu_qual;
7873 bool has_rep;
7874 int field_packed;
7875 vinfo_t vinfo;
7876
7877 Get_Variant_Encoding (variant);
7878 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7879 TYPE_NAME (gnu_variant_type)
7880 = concat_name (gnu_union_name,
7881 IDENTIFIER_POINTER (gnu_inner_name));
7882
7883 /* Set the alignment of the inner type in case we need to make
7884 inner objects into bitfields, but then clear it out so the
7885 record actually gets only the alignment required. */
7886 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7887 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7888 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7889 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7890
7891 /* Similarly, if the outer record has a size specified and all
7892 the fields have a rep clause, we can propagate the size. */
7893 if (all_rep_and_size)
7894 {
7895 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7896 TYPE_SIZE_UNIT (gnu_variant_type)
7897 = TYPE_SIZE_UNIT (gnu_record_type);
7898 }
7899
7900 /* Add the fields into the record type for the variant. Note that
7901 we aren't sure to really use it at this point, see below. */
7902 has_rep
7903 = components_to_record (Component_List (variant), gnat_record_type,
7904 NULL_TREE, gnu_variant_type, packed,
7905 definition, !all_rep_and_size, all_rep,
7906 unchecked_union, true, needs_xv_encodings,
7907 true, this_first_free_pos,
7908 all_rep || this_first_free_pos
7909 ? NULL : &gnu_rep_list);
7910
7911 /* Translate the qualifier and annotate the GNAT node. */
7912 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7913 Set_Present_Expr (variant, annotate_value (gnu_qual));
7914
7915 /* Deal with packedness like in gnat_to_gnu_field. */
7916 if (components_need_strict_alignment (Component_List (variant)))
7917 {
7918 field_packed = 0;
7919 union_field_needs_strict_alignment = true;
7920 }
7921 else
7922 field_packed
7923 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7924
7925 /* Push this variant onto the stack for the second pass. */
7926 vinfo.type = gnu_variant_type;
7927 vinfo.name = gnu_inner_name;
7928 vinfo.qual = gnu_qual;
7929 vinfo.has_rep = has_rep;
7930 vinfo.packed = field_packed;
7931 variant_types.safe_push (vinfo);
7932
7933 /* Compute the global properties that will determine the placement of
7934 the variant part. */
7935 variants_have_rep |= has_rep;
7936 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7937 variants_align = TYPE_ALIGN (gnu_variant_type);
7938 }
7939
7940 /* Round up the first free position to the alignment of the variant part
7941 for the variants without rep clause. This will guarantee a consistent
7942 layout independently of the placement of the variant part. */
7943 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7944 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7945
7946 /* In the second pass, the container types are adjusted if necessary and
7947 finished up, then the corresponding fields of the variant part are
7948 built with their qualifier, unless this is an unchecked union. */
7949 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7950 {
7951 tree gnu_variant_type = gnu_variant->type;
7952 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7953
7954 /* If this is an Unchecked_Union whose fields are all in the variant
7955 part and we have a single field with no representation clause or
7956 placed at offset zero, use the field directly to match the layout
7957 of C unions. */
7958 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7959 && gnu_field_list
7960 && !DECL_CHAIN (gnu_field_list)
7961 && (!DECL_FIELD_OFFSET (gnu_field_list)
7962 || integer_zerop (bit_position (gnu_field_list))))
7963 {
7964 gnu_field = gnu_field_list;
7965 DECL_CONTEXT (gnu_field) = gnu_record_type;
7966 }
7967 else
7968 {
7969 /* Finalize the variant type now. We used to throw away empty
7970 record types but we no longer do that because we need them to
7971 generate complete debug info for the variant; otherwise, the
7972 union type definition will be lacking the fields associated
7973 with these empty variants. */
7974 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7975 {
7976 /* The variant part will be at offset 0 so we need to ensure
7977 that the fields are laid out starting from the first free
7978 position at this level. */
7979 tree gnu_rep_type = make_node (RECORD_TYPE);
7980 tree gnu_rep_part;
7981 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7982 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7983 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7984 gnu_rep_part
7985 = create_rep_part (gnu_rep_type, gnu_variant_type,
7986 this_first_free_pos);
7987 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7988 gnu_field_list = gnu_rep_part;
7989 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7990 false);
7991 }
7992
7993 if (debug_info)
7994 rest_of_record_type_compilation (gnu_variant_type);
7995 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7996 true, needs_xv_encodings, gnat_component_list);
7997
7998 gnu_field
7999 = create_field_decl (gnu_variant->name, gnu_variant_type,
8000 gnu_union_type,
8001 all_rep_and_size
8002 ? TYPE_SIZE (gnu_variant_type) : 0,
8003 variants_have_rep ? bitsize_zero_node : 0,
8004 gnu_variant->packed, 0);
8005
8006 DECL_INTERNAL_P (gnu_field) = 1;
8007
8008 if (!unchecked_union)
8009 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
8010 }
8011
8012 DECL_CHAIN (gnu_field) = gnu_variant_list;
8013 gnu_variant_list = gnu_field;
8014 }
8015
8016 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
8017 if (gnu_variant_list)
8018 {
8019 int union_field_packed;
8020
8021 if (all_rep_and_size)
8022 {
8023 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
8024 TYPE_SIZE_UNIT (gnu_union_type)
8025 = TYPE_SIZE_UNIT (gnu_record_type);
8026 }
8027
8028 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
8029 all_rep_and_size ? 1 : 0, needs_xv_encodings);
8030
8031 /* If GNU_UNION_TYPE is our record type, it means we must have an
8032 Unchecked_Union with no fields. Verify that and, if so, just
8033 return. */
8034 if (gnu_union_type == gnu_record_type)
8035 {
8036 gcc_assert (unchecked_union
8037 && !gnu_field_list
8038 && !gnu_rep_list);
8039 return variants_have_rep;
8040 }
8041
8042 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
8043 needs_xv_encodings, gnat_component_list);
8044
8045 /* Deal with packedness like in gnat_to_gnu_field. */
8046 if (union_field_needs_strict_alignment)
8047 union_field_packed = 0;
8048 else
8049 union_field_packed
8050 = adjust_packed (gnu_union_type, gnu_record_type, packed);
8051
8052 gnu_variant_part
8053 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
8054 all_rep_and_size
8055 ? TYPE_SIZE (gnu_union_type) : 0,
8056 variants_have_rep ? bitsize_zero_node : 0,
8057 union_field_packed, 0);
8058
8059 DECL_INTERNAL_P (gnu_variant_part) = 1;
8060 }
8061 }
8062
8063 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do,
8064 pull them out and put them onto the appropriate list.
8065
8066 Similarly, pull out the fields with zero size and no rep clause, as they
8067 would otherwise modify the layout and thus very likely run afoul of the
8068 Ada semantics, which are different from those of C here.
8069
8070 Finally, if there is an aliased field placed in the list after fields
8071 with self-referential size, pull out the latter in the same way.
8072
8073 Optionally, if the reordering mechanism is enabled, pull out the fields
8074 with self-referential size, variable size and fixed size not a multiple
8075 of a byte, so that they don't cause the regular fields to be either at
8076 self-referential/variable offset or misaligned. Note, in the latter
8077 case, that this can only happen in packed record types so the alignment
8078 is effectively capped to the byte for the whole record. But we don't
8079 do it for packed record types if not all fixed-size fiels can be packed
8080 and for non-packed record types if pragma Optimize_Alignment (Space) is
8081 specified, because this can prevent alignment gaps from being filled.
8082
8083 Optionally, if the layout warning is enabled, keep track of the above 4
8084 different kinds of fields and issue a warning if some of them would be
8085 (or are being) reordered by the reordering mechanism.
8086
8087 ??? If we reorder fields, the debugging information will be affected and
8088 the debugger print fields in a different order from the source code. */
8089 const bool do_reorder
8090 = (Convention (gnat_record_type) == Convention_Ada
8091 && !No_Reordering (gnat_record_type)
8092 && !(Is_Packed (gnat_record_type)
8093 ? has_non_packed_fixed_size_field
8094 : Optimize_Alignment_Space (gnat_record_type))
8095 && !Debug_Flag_Dot_R);
8096 const bool w_reorder
8097 = (Convention (gnat_record_type) == Convention_Ada
8098 && Warn_On_Questionable_Layout
8099 && !(No_Reordering (gnat_record_type) && GNAT_Mode));
8100 const bool in_variant = (p_gnu_rep_list != NULL);
8101 tree gnu_zero_list = NULL_TREE;
8102 tree gnu_self_list = NULL_TREE;
8103 tree gnu_var_list = NULL_TREE;
8104 tree gnu_bitp_list = NULL_TREE;
8105 tree gnu_tmp_bitp_list = NULL_TREE;
8106 unsigned int tmp_bitp_size = 0;
8107 unsigned int last_reorder_field_type = -1;
8108 unsigned int tmp_last_reorder_field_type = -1;
8109
8110 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
8111 do { \
8112 if (gnu_last) \
8113 DECL_CHAIN (gnu_last) = gnu_next; \
8114 else \
8115 gnu_field_list = gnu_next; \
8116 \
8117 DECL_CHAIN (gnu_field) = (LIST); \
8118 (LIST) = gnu_field; \
8119 } while (0)
8120
8121 gnu_last = NULL_TREE;
8122 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
8123 {
8124 gnu_next = DECL_CHAIN (gnu_field);
8125
8126 if (DECL_FIELD_OFFSET (gnu_field))
8127 {
8128 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
8129 continue;
8130 }
8131
8132 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
8133 {
8134 DECL_SIZE_UNIT (gnu_field) = size_zero_node;
8135 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
8136 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
8137 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
8138 if (DECL_ALIASED_P (gnu_field))
8139 SET_TYPE_ALIGN (gnu_record_type,
8140 MAX (TYPE_ALIGN (gnu_record_type),
8141 TYPE_ALIGN (TREE_TYPE (gnu_field))));
8142 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
8143 continue;
8144 }
8145
8146 if (has_aliased_after_self_field && field_has_self_size (gnu_field))
8147 {
8148 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
8149 continue;
8150 }
8151
8152 /* We don't need further processing in default mode. */
8153 if (!w_reorder && !do_reorder)
8154 {
8155 gnu_last = gnu_field;
8156 continue;
8157 }
8158
8159 if (field_has_self_size (gnu_field))
8160 {
8161 if (w_reorder)
8162 {
8163 if (last_reorder_field_type < 4)
8164 warn_on_field_placement (gnu_field, gnat_component_list,
8165 gnat_record_type, in_variant,
8166 do_reorder);
8167 else
8168 last_reorder_field_type = 4;
8169 }
8170
8171 if (do_reorder)
8172 {
8173 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
8174 continue;
8175 }
8176 }
8177
8178 else if (field_has_variable_size (gnu_field))
8179 {
8180 if (w_reorder)
8181 {
8182 if (last_reorder_field_type < 3)
8183 warn_on_field_placement (gnu_field, gnat_component_list,
8184 gnat_record_type, in_variant,
8185 do_reorder);
8186 else
8187 last_reorder_field_type = 3;
8188 }
8189
8190 if (do_reorder)
8191 {
8192 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
8193 continue;
8194 }
8195 }
8196
8197 else
8198 {
8199 /* If the field has no size, then it cannot be bit-packed. */
8200 const unsigned int bitp_size
8201 = DECL_SIZE (gnu_field)
8202 ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT
8203 : 0;
8204
8205 /* If the field is bit-packed, we move it to a temporary list that
8206 contains the contiguously preceding bit-packed fields, because
8207 we want to be able to put them back if the misalignment happens
8208 to cancel itself after several bit-packed fields. */
8209 if (bitp_size != 0)
8210 {
8211 tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT;
8212
8213 if (last_reorder_field_type != 2)
8214 {
8215 tmp_last_reorder_field_type = last_reorder_field_type;
8216 last_reorder_field_type = 2;
8217 }
8218
8219 if (do_reorder)
8220 {
8221 MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list);
8222 continue;
8223 }
8224 }
8225
8226 /* No more bit-packed fields, move the existing ones to the end or
8227 put them back at their original location. */
8228 else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list)
8229 {
8230 last_reorder_field_type = 1;
8231
8232 if (tmp_bitp_size != 0)
8233 {
8234 if (w_reorder && tmp_last_reorder_field_type < 2)
8235 {
8236 if (gnu_tmp_bitp_list)
8237 warn_on_list_placement (gnu_tmp_bitp_list,
8238 gnat_component_list,
8239 gnat_record_type, in_variant,
8240 do_reorder);
8241 else
8242 warn_on_field_placement (gnu_last,
8243 gnat_component_list,
8244 gnat_record_type, in_variant,
8245 do_reorder);
8246 }
8247
8248 if (do_reorder)
8249 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
8250
8251 gnu_tmp_bitp_list = NULL_TREE;
8252 tmp_bitp_size = 0;
8253 }
8254 else
8255 {
8256 /* Rechain the temporary list in front of GNU_FIELD. */
8257 tree gnu_bitp_field = gnu_field;
8258 while (gnu_tmp_bitp_list)
8259 {
8260 tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list);
8261 DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field;
8262 if (gnu_last)
8263 DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list;
8264 else
8265 gnu_field_list = gnu_tmp_bitp_list;
8266 gnu_bitp_field = gnu_tmp_bitp_list;
8267 gnu_tmp_bitp_list = gnu_bitp_next;
8268 }
8269 }
8270 }
8271
8272 else
8273 last_reorder_field_type = 1;
8274 }
8275
8276 gnu_last = gnu_field;
8277 }
8278
8279 #undef MOVE_FROM_FIELD_LIST_TO
8280
8281 gnu_field_list = nreverse (gnu_field_list);
8282
8283 /* If permitted, we reorder the fields as follows:
8284
8285 1) all (groups of) fields whose length is fixed and multiple of a byte,
8286 2) the remaining fields whose length is fixed and not multiple of a byte,
8287 3) the remaining fields whose length doesn't depend on discriminants,
8288 4) all fields whose length depends on discriminants,
8289 5) the variant part,
8290
8291 within the record and within each variant recursively. */
8292
8293 if (w_reorder)
8294 {
8295 /* If we have pending bit-packed fields, warn if they would be moved
8296 to after regular fields. */
8297 if (last_reorder_field_type == 2
8298 && tmp_bitp_size != 0
8299 && tmp_last_reorder_field_type < 2)
8300 {
8301 if (gnu_tmp_bitp_list)
8302 warn_on_list_placement (gnu_tmp_bitp_list,
8303 gnat_component_list, gnat_record_type,
8304 in_variant, do_reorder);
8305 else
8306 warn_on_field_placement (gnu_field_list,
8307 gnat_component_list, gnat_record_type,
8308 in_variant, do_reorder);
8309 }
8310 }
8311
8312 if (do_reorder)
8313 {
8314 /* If we have pending bit-packed fields on the temporary list, we put
8315 them either on the bit-packed list or back on the regular list. */
8316 if (gnu_tmp_bitp_list)
8317 {
8318 if (tmp_bitp_size != 0)
8319 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
8320 else
8321 gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list);
8322 }
8323
8324 gnu_field_list
8325 = chainon (gnu_field_list,
8326 chainon (gnu_bitp_list,
8327 chainon (gnu_var_list, gnu_self_list)));
8328 }
8329
8330 /* Otherwise, if there is an aliased field placed after a field whose length
8331 depends on discriminants, we put all the fields of the latter sort, last.
8332 We need to do this in case an object of this record type is mutable. */
8333 else if (has_aliased_after_self_field)
8334 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
8335
8336 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
8337 in our REP list to the previous level because this level needs them in
8338 order to do a correct layout, i.e. avoid having overlapping fields. */
8339 if (p_gnu_rep_list && gnu_rep_list)
8340 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
8341
8342 /* Deal with the case of an extension of a record type with variable size and
8343 partial rep clause, for which the _Parent field is forced at offset 0 and
8344 has variable size. Note that we cannot do it if the field has fixed size
8345 because we rely on the presence of the REP part built below to trigger the
8346 reordering of the fields in a derived record type when all the fields have
8347 a fixed position. */
8348 else if (gnu_rep_list
8349 && !DECL_CHAIN (gnu_rep_list)
8350 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
8351 && !variants_have_rep
8352 && first_free_pos
8353 && integer_zerop (first_free_pos)
8354 && integer_zerop (bit_position (gnu_rep_list)))
8355 {
8356 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
8357 gnu_field_list = gnu_rep_list;
8358 gnu_rep_list = NULL_TREE;
8359 }
8360
8361 /* Otherwise, sort the fields by bit position and put them into their own
8362 record, before the others, if we also have fields without rep clause. */
8363 else if (gnu_rep_list)
8364 {
8365 tree gnu_parent, gnu_rep_type;
8366
8367 /* If all the fields have a rep clause, we can do a flat layout. */
8368 layout_with_rep = !gnu_field_list
8369 && (!gnu_variant_part || variants_have_rep);
8370
8371 /* Same as above but the extension itself has a rep clause, in which case
8372 we need to set aside the _Parent field to lay out the REP part. */
8373 if (TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
8374 && !layout_with_rep
8375 && !variants_have_rep
8376 && first_free_pos
8377 && integer_zerop (first_free_pos)
8378 && integer_zerop (bit_position (gnu_rep_list)))
8379 {
8380 gnu_parent = gnu_rep_list;
8381 gnu_rep_list = DECL_CHAIN (gnu_rep_list);
8382 }
8383 else
8384 gnu_parent = NULL_TREE;
8385
8386 gnu_rep_type
8387 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
8388
8389 /* Sort the fields in order of increasing bit position. */
8390 const int len = list_length (gnu_rep_list);
8391 tree *gnu_arr = XALLOCAVEC (tree, len);
8392
8393 gnu_field = gnu_rep_list;
8394 for (int i = 0; i < len; i++)
8395 {
8396 gnu_arr[i] = gnu_field;
8397 gnu_field = DECL_CHAIN (gnu_field);
8398 }
8399
8400 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
8401
8402 gnu_rep_list = NULL_TREE;
8403 for (int i = len - 1; i >= 0; i--)
8404 {
8405 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
8406 gnu_rep_list = gnu_arr[i];
8407 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
8408 }
8409
8410 /* Do the layout of the REP part, if any. */
8411 if (layout_with_rep)
8412 gnu_field_list = gnu_rep_list;
8413 else
8414 {
8415 TYPE_NAME (gnu_rep_type)
8416 = create_concat_name (gnat_record_type, "REP");
8417 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
8418 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
8419 finish_record_type (gnu_rep_type, gnu_rep_list, 1, false);
8420
8421 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
8422 without rep clause are laid out starting from this position.
8423 Therefore, we force it as a minimal size on the REP part. */
8424 tree gnu_rep_part
8425 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
8426
8427 /* If this is an extension, put back the _Parent field as the first
8428 field of the REP part at offset 0 and update its layout. */
8429 if (gnu_parent)
8430 {
8431 const unsigned int align = DECL_ALIGN (gnu_parent);
8432 DECL_CHAIN (gnu_parent) = TYPE_FIELDS (gnu_rep_type);
8433 TYPE_FIELDS (gnu_rep_type) = gnu_parent;
8434 DECL_CONTEXT (gnu_parent) = gnu_rep_type;
8435 if (align > TYPE_ALIGN (gnu_rep_type))
8436 {
8437 SET_TYPE_ALIGN (gnu_rep_type, align);
8438 TYPE_SIZE (gnu_rep_type)
8439 = round_up (TYPE_SIZE (gnu_rep_type), align);
8440 TYPE_SIZE_UNIT (gnu_rep_type)
8441 = round_up (TYPE_SIZE_UNIT (gnu_rep_type), align);
8442 SET_DECL_ALIGN (gnu_rep_part, align);
8443 }
8444 }
8445
8446 if (debug_info)
8447 rest_of_record_type_compilation (gnu_rep_type);
8448
8449 /* Chain the REP part at the beginning of the field list. */
8450 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
8451 gnu_field_list = gnu_rep_part;
8452 }
8453 }
8454
8455 /* Chain the variant part at the end of the field list. */
8456 if (gnu_variant_part)
8457 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
8458
8459 if (cancel_alignment)
8460 SET_TYPE_ALIGN (gnu_record_type, 0);
8461
8462 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
8463
8464 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
8465 debug_info && !maybe_unused);
8466
8467 /* Chain the fields with zero size at the beginning of the field list. */
8468 if (gnu_zero_list)
8469 TYPE_FIELDS (gnu_record_type)
8470 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
8471
8472 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
8473 }
8474
8475 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
8476 placed into an Esize, Component_Bit_Offset, or Component_Size value
8477 in the GNAT tree. */
8478
8479 static Uint
8480 annotate_value (tree gnu_size)
8481 {
8482 static int var_count = 0;
8483 TCode tcode;
8484 Node_Ref_Or_Val ops[3] = { No_Uint, No_Uint, No_Uint };
8485 struct tree_int_map in;
8486
8487 /* See if we've already saved the value for this node. */
8488 if (EXPR_P (gnu_size) || DECL_P (gnu_size))
8489 {
8490 struct tree_int_map *e;
8491
8492 in.base.from = gnu_size;
8493 e = annotate_value_cache->find (&in);
8494
8495 if (e)
8496 return (Node_Ref_Or_Val) e->to;
8497 }
8498 else
8499 in.base.from = NULL_TREE;
8500
8501 /* If we do not return inside this switch, TCODE will be set to the
8502 code to be used in a call to Create_Node. */
8503 switch (TREE_CODE (gnu_size))
8504 {
8505 case INTEGER_CST:
8506 /* For negative values, build NEGATE_EXPR of the opposite. Such values
8507 can appear for discriminants in expressions for variants. */
8508 if (tree_int_cst_sgn (gnu_size) < 0)
8509 {
8510 tree t = wide_int_to_tree (sizetype, -wi::to_wide (gnu_size));
8511 tcode = Negate_Expr;
8512 ops[0] = UI_From_gnu (t);
8513 }
8514 else
8515 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8516 break;
8517
8518 case COMPONENT_REF:
8519 /* The only case we handle here is a simple discriminant reference. */
8520 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8521 {
8522 tree ref = gnu_size;
8523 gnu_size = TREE_OPERAND (ref, 1);
8524
8525 /* Climb up the chain of successive extensions, if any. */
8526 while (TREE_CODE (TREE_OPERAND (ref, 0)) == COMPONENT_REF
8527 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (ref, 0), 1))
8528 == parent_name_id)
8529 ref = TREE_OPERAND (ref, 0);
8530
8531 if (TREE_CODE (TREE_OPERAND (ref, 0)) == PLACEHOLDER_EXPR)
8532 {
8533 /* Fall through to common processing as a FIELD_DECL. */
8534 tcode = Discrim_Val;
8535 ops[0] = UI_From_gnu (DECL_DISCRIMINANT_NUMBER (gnu_size));
8536 }
8537 else
8538 return No_Uint;
8539 }
8540 else
8541 return No_Uint;
8542 break;
8543
8544 case VAR_DECL:
8545 tcode = Dynamic_Val;
8546 ops[0] = UI_From_Int (++var_count);
8547 break;
8548
8549 CASE_CONVERT:
8550 case NON_LVALUE_EXPR:
8551 return annotate_value (TREE_OPERAND (gnu_size, 0));
8552
8553 /* Now just list the operations we handle. */
8554 case COND_EXPR: tcode = Cond_Expr; break;
8555 case MINUS_EXPR: tcode = Minus_Expr; break;
8556 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8557 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8558 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8559 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8560 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8561 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8562 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8563 case NEGATE_EXPR: tcode = Negate_Expr; break;
8564 case MIN_EXPR: tcode = Min_Expr; break;
8565 case MAX_EXPR: tcode = Max_Expr; break;
8566 case ABS_EXPR: tcode = Abs_Expr; break;
8567 case TRUTH_ANDIF_EXPR:
8568 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8569 case TRUTH_ORIF_EXPR:
8570 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8571 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8572 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8573 case LT_EXPR: tcode = Lt_Expr; break;
8574 case LE_EXPR: tcode = Le_Expr; break;
8575 case GT_EXPR: tcode = Gt_Expr; break;
8576 case GE_EXPR: tcode = Ge_Expr; break;
8577 case EQ_EXPR: tcode = Eq_Expr; break;
8578 case NE_EXPR: tcode = Ne_Expr; break;
8579
8580 case PLUS_EXPR:
8581 /* Turn addition of negative constant into subtraction. */
8582 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST
8583 && tree_int_cst_sign_bit (TREE_OPERAND (gnu_size, 1)))
8584 {
8585 tcode = Minus_Expr;
8586 wide_int wop1 = -wi::to_wide (TREE_OPERAND (gnu_size, 1));
8587 ops[1] = annotate_value (wide_int_to_tree (sizetype, wop1));
8588 break;
8589 }
8590
8591 /* ... fall through ... */
8592
8593 case MULT_EXPR:
8594 tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr);
8595 /* Fold conversions from bytes to bits into inner operations. */
8596 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST
8597 && CONVERT_EXPR_P (TREE_OPERAND (gnu_size, 0)))
8598 {
8599 tree inner_op = TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 0);
8600 if (TREE_CODE (inner_op) == TREE_CODE (gnu_size)
8601 && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST)
8602 {
8603 ops[0] = annotate_value (TREE_OPERAND (inner_op, 0));
8604 tree inner_op_op1 = TREE_OPERAND (inner_op, 1);
8605 tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1);
8606 widest_int op1;
8607 if (TREE_CODE (gnu_size) == MULT_EXPR)
8608 op1 = (wi::to_widest (inner_op_op1)
8609 * wi::to_widest (gnu_size_op1));
8610 else
8611 {
8612 op1 = (wi::to_widest (inner_op_op1)
8613 + wi::to_widest (gnu_size_op1));
8614 if (wi::zext (op1, TYPE_PRECISION (sizetype)) == 0)
8615 return ops[0];
8616 }
8617 ops[1] = annotate_value (wide_int_to_tree (sizetype, op1));
8618 }
8619 }
8620 break;
8621
8622 case BIT_AND_EXPR:
8623 tcode = Bit_And_Expr;
8624 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8625 Such values can appear in expressions with aligning patterns. */
8626 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8627 {
8628 wide_int wop1 = -wi::to_wide (TREE_OPERAND (gnu_size, 1));
8629 tree op1 = wide_int_to_tree (sizetype, wop1);
8630 ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8631 }
8632 break;
8633
8634 case CALL_EXPR:
8635 /* In regular mode, inline back only if symbolic annotation is requested
8636 in order to avoid memory explosion on big discriminated record types.
8637 But not in ASIS mode, as symbolic annotation is required for DDA. */
8638 if (List_Representation_Info >= 3 || type_annotate_only)
8639 {
8640 tree t = maybe_inline_call_in_expr (gnu_size);
8641 return t ? annotate_value (t) : No_Uint;
8642 }
8643 else
8644 return Uint_Minus_1;
8645
8646 default:
8647 return No_Uint;
8648 }
8649
8650 /* Now get each of the operands that's relevant for this code. If any
8651 cannot be expressed as a repinfo node, say we can't. */
8652 for (int i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8653 if (ops[i] == No_Uint)
8654 {
8655 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8656 if (ops[i] == No_Uint)
8657 return No_Uint;
8658 }
8659
8660 Node_Ref_Or_Val ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8661
8662 /* Save the result in the cache. */
8663 if (in.base.from)
8664 {
8665 struct tree_int_map **h;
8666 /* We can't assume the hash table data hasn't moved since the initial
8667 look up, so we have to search again. Allocating and inserting an
8668 entry at that point would be an alternative, but then we'd better
8669 discard the entry if we decided not to cache it. */
8670 h = annotate_value_cache->find_slot (&in, INSERT);
8671 gcc_assert (!*h);
8672 *h = ggc_alloc<tree_int_map> ();
8673 (*h)->base.from = in.base.from;
8674 (*h)->to = ret;
8675 }
8676
8677 return ret;
8678 }
8679
8680 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8681 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8682 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8683 BY_REF is true if the object is used by reference. */
8684
8685 void
8686 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8687 {
8688 if (by_ref)
8689 {
8690 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8691 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8692 else
8693 gnu_type = TREE_TYPE (gnu_type);
8694 }
8695
8696 if (Unknown_Esize (gnat_entity))
8697 {
8698 if (TREE_CODE (gnu_type) == RECORD_TYPE
8699 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8700 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8701 else if (!size)
8702 size = TYPE_SIZE (gnu_type);
8703
8704 if (size)
8705 Set_Esize (gnat_entity, annotate_value (size));
8706 }
8707
8708 if (Unknown_Alignment (gnat_entity))
8709 Set_Alignment (gnat_entity,
8710 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8711 }
8712
8713 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8714 Return NULL_TREE if there is no such element in the list. */
8715
8716 static tree
8717 purpose_member_field (const_tree elem, tree list)
8718 {
8719 while (list)
8720 {
8721 tree field = TREE_PURPOSE (list);
8722 if (SAME_FIELD_P (field, elem))
8723 return list;
8724 list = TREE_CHAIN (list);
8725 }
8726 return NULL_TREE;
8727 }
8728
8729 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8730 set Component_Bit_Offset and Esize of the components to the position and
8731 size used by Gigi. */
8732
8733 static void
8734 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8735 {
8736 /* For an extension, the inherited components have not been translated because
8737 they are fetched from the _Parent component on the fly. */
8738 const bool is_extension
8739 = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity);
8740
8741 /* We operate by first making a list of all fields and their position (we
8742 can get the size easily) and then update all the sizes in the tree. */
8743 tree gnu_list
8744 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8745 BIGGEST_ALIGNMENT, NULL_TREE);
8746
8747 for (Entity_Id gnat_field = First_Entity (gnat_entity);
8748 Present (gnat_field);
8749 gnat_field = Next_Entity (gnat_field))
8750 if ((Ekind (gnat_field) == E_Component
8751 && (is_extension || present_gnu_tree (gnat_field)))
8752 || (Ekind (gnat_field) == E_Discriminant
8753 && !Is_Unchecked_Union (Scope (gnat_field))))
8754 {
8755 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8756 gnu_list);
8757 if (t)
8758 {
8759 tree offset = TREE_VEC_ELT (TREE_VALUE (t), 0);
8760 tree bit_offset = TREE_VEC_ELT (TREE_VALUE (t), 2);
8761
8762 /* If we are just annotating types and the type is tagged, the tag
8763 and the parent components are not generated by the front-end so
8764 we need to add the appropriate offset to each component without
8765 representation clause. */
8766 if (type_annotate_only
8767 && Is_Tagged_Type (gnat_entity)
8768 && No (Component_Clause (gnat_field)))
8769 {
8770 tree parent_bit_offset;
8771
8772 /* For a component appearing in the current extension, the
8773 offset is the size of the parent. */
8774 if (Is_Derived_Type (gnat_entity)
8775 && Original_Record_Component (gnat_field) == gnat_field)
8776 parent_bit_offset
8777 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8778 bitsizetype);
8779 else
8780 parent_bit_offset = bitsize_int (POINTER_SIZE);
8781
8782 if (TYPE_FIELDS (gnu_type))
8783 parent_bit_offset
8784 = round_up (parent_bit_offset,
8785 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8786
8787 offset
8788 = size_binop (PLUS_EXPR, offset,
8789 fold_convert (sizetype,
8790 size_binop (TRUNC_DIV_EXPR,
8791 parent_bit_offset,
8792 bitsize_unit_node)));
8793 }
8794
8795 /* If the field has a variable offset, also compute the normalized
8796 position since it's easier to do on trees here than to deduce
8797 it from the annotated expression of Component_Bit_Offset. */
8798 if (TREE_CODE (offset) != INTEGER_CST)
8799 {
8800 normalize_offset (&offset, &bit_offset, BITS_PER_UNIT);
8801 Set_Normalized_Position (gnat_field,
8802 annotate_value (offset));
8803 Set_Normalized_First_Bit (gnat_field,
8804 annotate_value (bit_offset));
8805 }
8806
8807 Set_Component_Bit_Offset
8808 (gnat_field,
8809 annotate_value (bit_from_pos (offset, bit_offset)));
8810
8811 Set_Esize (gnat_field,
8812 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8813 }
8814 else if (is_extension)
8815 {
8816 /* If there is no entry, this is an inherited component whose
8817 position is the same as in the parent type. */
8818 Entity_Id gnat_orig = Original_Record_Component (gnat_field);
8819
8820 /* If we are just annotating types, discriminants renaming those of
8821 the parent have no entry so deal with them specifically. */
8822 if (type_annotate_only
8823 && gnat_orig == gnat_field
8824 && Ekind (gnat_field) == E_Discriminant)
8825 gnat_orig = Corresponding_Discriminant (gnat_field);
8826
8827 if (Known_Normalized_Position (gnat_orig))
8828 {
8829 Set_Normalized_Position (gnat_field,
8830 Normalized_Position (gnat_orig));
8831 Set_Normalized_First_Bit (gnat_field,
8832 Normalized_First_Bit (gnat_orig));
8833 }
8834
8835 Set_Component_Bit_Offset (gnat_field,
8836 Component_Bit_Offset (gnat_orig));
8837
8838 Set_Esize (gnat_field, Esize (gnat_orig));
8839 }
8840 }
8841 }
8842
8843 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8844 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8845 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8846 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8847 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8848 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8849 pre-existing list to be chained to the newly created entries. */
8850
8851 static tree
8852 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8853 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8854 {
8855 tree gnu_field;
8856
8857 for (gnu_field = TYPE_FIELDS (gnu_type);
8858 gnu_field;
8859 gnu_field = DECL_CHAIN (gnu_field))
8860 {
8861 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8862 DECL_FIELD_BIT_OFFSET (gnu_field));
8863 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8864 DECL_FIELD_OFFSET (gnu_field));
8865 unsigned int our_offset_align
8866 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8867 tree v = make_tree_vec (3);
8868
8869 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8870 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8871 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8872 gnu_list = tree_cons (gnu_field, v, gnu_list);
8873
8874 /* Recurse on internal fields, flattening the nested fields except for
8875 those in the variant part, if requested. */
8876 if (DECL_INTERNAL_P (gnu_field))
8877 {
8878 tree gnu_field_type = TREE_TYPE (gnu_field);
8879 if (do_not_flatten_variant
8880 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8881 gnu_list
8882 = build_position_list (gnu_field_type, do_not_flatten_variant,
8883 size_zero_node, bitsize_zero_node,
8884 BIGGEST_ALIGNMENT, gnu_list);
8885 else
8886 gnu_list
8887 = build_position_list (gnu_field_type, do_not_flatten_variant,
8888 gnu_our_offset, gnu_our_bitpos,
8889 our_offset_align, gnu_list);
8890 }
8891 }
8892
8893 return gnu_list;
8894 }
8895
8896 /* Return a list describing the substitutions needed to reflect the
8897 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8898 be in any order. The values in an element of the list are in the form
8899 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8900 a definition of GNAT_SUBTYPE. */
8901
8902 static vec<subst_pair>
8903 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8904 {
8905 vec<subst_pair> gnu_list = vNULL;
8906 Entity_Id gnat_discrim;
8907 Node_Id gnat_constr;
8908
8909 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8910 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8911 Present (gnat_discrim);
8912 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8913 gnat_constr = Next_Elmt (gnat_constr))
8914 /* Ignore access discriminants. */
8915 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8916 {
8917 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8918 tree replacement
8919 = elaborate_expression (Node (gnat_constr), gnat_subtype,
8920 get_entity_char (gnat_discrim),
8921 definition, true, false);
8922 /* If this is a definition, we need to make sure that the SAVE_EXPRs
8923 are instantiated on every possibly path in size computations. */
8924 if (definition && TREE_CODE (replacement) == SAVE_EXPR)
8925 add_stmt (replacement);
8926 replacement = convert (TREE_TYPE (gnu_field), replacement);
8927 subst_pair s = { gnu_field, replacement };
8928 gnu_list.safe_push (s);
8929 }
8930
8931 return gnu_list;
8932 }
8933
8934 /* Scan all fields in {GNU_QUAL_UNION_TYPE,GNAT_VARIANT_PART} and return a list
8935 describing the variants of GNU_QUAL_UNION_TYPE that are still relevant after
8936 applying the substitutions described in SUBST_LIST. GNU_LIST is an existing
8937 list to be prepended to the newly created entries. */
8938
8939 static vec<variant_desc>
8940 build_variant_list (tree gnu_qual_union_type, Node_Id gnat_variant_part,
8941 vec<subst_pair> subst_list, vec<variant_desc> gnu_list)
8942 {
8943 Node_Id gnat_variant;
8944 tree gnu_field;
8945
8946 for (gnu_field = TYPE_FIELDS (gnu_qual_union_type),
8947 gnat_variant
8948 = Present (gnat_variant_part)
8949 ? First_Non_Pragma (Variants (gnat_variant_part))
8950 : Empty;
8951 gnu_field;
8952 gnu_field = DECL_CHAIN (gnu_field),
8953 gnat_variant
8954 = Present (gnat_variant_part)
8955 ? Next_Non_Pragma (gnat_variant)
8956 : Empty)
8957 {
8958 tree qual = DECL_QUALIFIER (gnu_field);
8959 unsigned int i;
8960 subst_pair *s;
8961
8962 FOR_EACH_VEC_ELT (subst_list, i, s)
8963 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8964
8965 /* If the new qualifier is not unconditionally false, its variant may
8966 still be accessed. */
8967 if (!integer_zerop (qual))
8968 {
8969 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8970 variant_desc v
8971 = { variant_type, gnu_field, qual, NULL_TREE, NULL_TREE };
8972
8973 gnu_list.safe_push (v);
8974
8975 /* Annotate the GNAT node if present. */
8976 if (Present (gnat_variant))
8977 Set_Present_Expr (gnat_variant, annotate_value (qual));
8978
8979 /* Recurse on the variant subpart of the variant, if any. */
8980 variant_subpart = get_variant_part (variant_type);
8981 if (variant_subpart)
8982 gnu_list
8983 = build_variant_list (TREE_TYPE (variant_subpart),
8984 Present (gnat_variant)
8985 ? Variant_Part
8986 (Component_List (gnat_variant))
8987 : Empty,
8988 subst_list,
8989 gnu_list);
8990
8991 /* If the new qualifier is unconditionally true, the subsequent
8992 variants cannot be accessed. */
8993 if (integer_onep (qual))
8994 break;
8995 }
8996 }
8997
8998 return gnu_list;
8999 }
9000
9001 /* If SIZE has overflowed, return the maximum valid size, which is the upper
9002 bound of the signed sizetype in bits, rounded down to ALIGN. Otherwise
9003 return SIZE unmodified. */
9004
9005 static tree
9006 maybe_saturate_size (tree size, unsigned int align)
9007 {
9008 if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size))
9009 {
9010 size
9011 = size_binop (MULT_EXPR,
9012 fold_convert (bitsizetype, TYPE_MAX_VALUE (ssizetype)),
9013 build_int_cst (bitsizetype, BITS_PER_UNIT));
9014 size = round_down (size, align);
9015 }
9016
9017 return size;
9018 }
9019
9020 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
9021 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
9022 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
9023 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
9024 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
9025 true if we are being called to process the Component_Size of GNAT_OBJECT;
9026 this is used only for error messages. ZERO_OK is true if a size of zero
9027 is permitted; if ZERO_OK is false, it means that a size of zero should be
9028 treated as an unspecified size. S1 and S2 are used for error messages. */
9029
9030 static tree
9031 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
9032 enum tree_code kind, bool component_p, bool zero_ok,
9033 const char *s1, const char *s2)
9034 {
9035 Node_Id gnat_error_node;
9036 tree old_size, size;
9037
9038 /* Return 0 if no size was specified. */
9039 if (uint_size == No_Uint)
9040 return NULL_TREE;
9041
9042 /* Ignore a negative size since that corresponds to our back-annotation. */
9043 if (UI_Lt (uint_size, Uint_0))
9044 return NULL_TREE;
9045
9046 /* Find the node to use for error messages. */
9047 if ((Ekind (gnat_object) == E_Component
9048 || Ekind (gnat_object) == E_Discriminant)
9049 && Present (Component_Clause (gnat_object)))
9050 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
9051 else if (Present (Size_Clause (gnat_object)))
9052 gnat_error_node = Expression (Size_Clause (gnat_object));
9053 else if (Has_Object_Size_Clause (gnat_object))
9054 gnat_error_node = Expression (Object_Size_Clause (gnat_object));
9055 else
9056 gnat_error_node = gnat_object;
9057
9058 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
9059 but cannot be represented in bitsizetype. */
9060 size = UI_To_gnu (uint_size, bitsizetype);
9061 if (TREE_OVERFLOW (size))
9062 {
9063 if (component_p)
9064 post_error_ne ("component size for& is too large", gnat_error_node,
9065 gnat_object);
9066 else
9067 post_error_ne ("size for& is too large", gnat_error_node,
9068 gnat_object);
9069 return NULL_TREE;
9070 }
9071
9072 /* Ignore a zero size if it is not permitted. */
9073 if (!zero_ok && integer_zerop (size))
9074 return NULL_TREE;
9075
9076 /* The size of objects is always a multiple of a byte. */
9077 if (kind == VAR_DECL
9078 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
9079 {
9080 if (component_p)
9081 post_error_ne ("component size for& must be multiple of Storage_Unit",
9082 gnat_error_node, gnat_object);
9083 else
9084 post_error_ne ("size for& must be multiple of Storage_Unit",
9085 gnat_error_node, gnat_object);
9086 return NULL_TREE;
9087 }
9088
9089 /* If this is an integral type or a bit-packed array type, the front-end has
9090 already verified the size, so we need not do it again (which would mean
9091 checking against the bounds). However, if this is an aliased object, it
9092 may not be smaller than the type of the object. */
9093 if ((INTEGRAL_TYPE_P (gnu_type) || BIT_PACKED_ARRAY_TYPE_P (gnu_type))
9094 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
9095 return size;
9096
9097 /* If the object is a record that contains a template, add the size of the
9098 template to the specified size. */
9099 if (TREE_CODE (gnu_type) == RECORD_TYPE
9100 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9101 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
9102
9103 old_size = (kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type));
9104
9105 /* If the old size is self-referential, get the maximum size. */
9106 if (CONTAINS_PLACEHOLDER_P (old_size))
9107 old_size = max_size (old_size, true);
9108
9109 /* If this is an access type or a fat pointer, the minimum size is that given
9110 by the smallest integral mode that's valid for pointers. */
9111 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
9112 {
9113 scalar_int_mode p_mode = NARROWEST_INT_MODE;
9114 while (!targetm.valid_pointer_mode (p_mode))
9115 p_mode = GET_MODE_WIDER_MODE (p_mode).require ();
9116 old_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
9117 }
9118
9119 /* Issue an error either if the default size of the object isn't a constant
9120 or if the new size is smaller than it. */
9121 if (TREE_CODE (old_size) != INTEGER_CST
9122 || TREE_OVERFLOW (old_size)
9123 || tree_int_cst_lt (size, old_size))
9124 {
9125 char buf[128];
9126 const char *s;
9127
9128 if (kind == FIELD_DECL)
9129 {
9130 snprintf (buf, sizeof (buf), s1, s2);
9131 s = buf;
9132 }
9133 else if (component_p)
9134 s = "component size for& too small{, minimum allowed is ^}";
9135 else
9136 s = "size for& too small{, minimum allowed is ^}";
9137 post_error_ne_tree (s, gnat_error_node, gnat_object, old_size);
9138
9139 return NULL_TREE;
9140 }
9141
9142 return size;
9143 }
9144
9145 /* Similarly, but both validate and process a value of RM size. This routine
9146 is only called for types. */
9147
9148 static void
9149 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
9150 {
9151 Node_Id gnat_attr_node;
9152 tree old_size, size;
9153
9154 /* Do nothing if no size was specified. */
9155 if (uint_size == No_Uint)
9156 return;
9157
9158 /* Only issue an error if a Value_Size clause was explicitly given for the
9159 entity; otherwise, we'd be duplicating an error on the Size clause. */
9160 gnat_attr_node
9161 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
9162 if (Present (gnat_attr_node) && Entity (gnat_attr_node) != gnat_entity)
9163 gnat_attr_node = Empty;
9164
9165 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
9166 but cannot be represented in bitsizetype. */
9167 size = UI_To_gnu (uint_size, bitsizetype);
9168 if (TREE_OVERFLOW (size))
9169 {
9170 if (Present (gnat_attr_node))
9171 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
9172 gnat_entity);
9173 return;
9174 }
9175
9176 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
9177 exists, or this is an integer type, in which case the front-end will
9178 have always set it. */
9179 if (No (gnat_attr_node)
9180 && integer_zerop (size)
9181 && !Has_Size_Clause (gnat_entity)
9182 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
9183 return;
9184
9185 old_size = rm_size (gnu_type);
9186
9187 /* If the old size is self-referential, get the maximum size. */
9188 if (CONTAINS_PLACEHOLDER_P (old_size))
9189 old_size = max_size (old_size, true);
9190
9191 /* Issue an error either if the old size of the object isn't a constant or
9192 if the new size is smaller than it. The front-end has already verified
9193 this for scalar and bit-packed array types. */
9194 if (TREE_CODE (old_size) != INTEGER_CST
9195 || TREE_OVERFLOW (old_size)
9196 || (AGGREGATE_TYPE_P (gnu_type)
9197 && !BIT_PACKED_ARRAY_TYPE_P (gnu_type)
9198 && !(TYPE_IS_PADDING_P (gnu_type)
9199 && BIT_PACKED_ARRAY_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_type))))
9200 && tree_int_cst_lt (size, old_size)))
9201 {
9202 if (Present (gnat_attr_node))
9203 post_error_ne_tree
9204 ("Value_Size for& too small{, minimum allowed is ^}",
9205 gnat_attr_node, gnat_entity, old_size);
9206 return;
9207 }
9208
9209 /* Otherwise, set the RM size proper for integral types... */
9210 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
9211 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
9212 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
9213 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
9214 SET_TYPE_RM_SIZE (gnu_type, size);
9215
9216 /* ...or the Ada size for record and union types. */
9217 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
9218 && !TYPE_FAT_POINTER_P (gnu_type))
9219 SET_TYPE_ADA_SIZE (gnu_type, size);
9220 }
9221
9222 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
9223 a type or object whose present alignment is ALIGN. If this alignment is
9224 valid, return it. Otherwise, give an error and return ALIGN. */
9225
9226 static unsigned int
9227 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
9228 {
9229 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
9230 unsigned int new_align;
9231 Node_Id gnat_error_node;
9232
9233 /* Don't worry about checking alignment if alignment was not specified
9234 by the source program and we already posted an error for this entity. */
9235 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
9236 return align;
9237
9238 /* Post the error on the alignment clause if any. Note, for the implicit
9239 base type of an array type, the alignment clause is on the first
9240 subtype. */
9241 if (Present (Alignment_Clause (gnat_entity)))
9242 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
9243
9244 else if (Is_Itype (gnat_entity)
9245 && Is_Array_Type (gnat_entity)
9246 && Etype (gnat_entity) == gnat_entity
9247 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
9248 gnat_error_node =
9249 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
9250
9251 else
9252 gnat_error_node = gnat_entity;
9253
9254 /* Within GCC, an alignment is an integer, so we must make sure a value is
9255 specified that fits in that range. Also, there is an upper bound to
9256 alignments we can support/allow. */
9257 if (!UI_Is_In_Int_Range (alignment)
9258 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
9259 post_error_ne_num ("largest supported alignment for& is ^",
9260 gnat_error_node, gnat_entity, max_allowed_alignment);
9261 else if (!(Present (Alignment_Clause (gnat_entity))
9262 && From_At_Mod (Alignment_Clause (gnat_entity)))
9263 && new_align * BITS_PER_UNIT < align)
9264 {
9265 unsigned int double_align;
9266 bool is_capped_double, align_clause;
9267
9268 /* If the default alignment of "double" or larger scalar types is
9269 specifically capped and the new alignment is above the cap, do
9270 not post an error and change the alignment only if there is an
9271 alignment clause; this makes it possible to have the associated
9272 GCC type overaligned by default for performance reasons. */
9273 if ((double_align = double_float_alignment) > 0)
9274 {
9275 Entity_Id gnat_type
9276 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
9277 is_capped_double
9278 = is_double_float_or_array (gnat_type, &align_clause);
9279 }
9280 else if ((double_align = double_scalar_alignment) > 0)
9281 {
9282 Entity_Id gnat_type
9283 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
9284 is_capped_double
9285 = is_double_scalar_or_array (gnat_type, &align_clause);
9286 }
9287 else
9288 is_capped_double = align_clause = false;
9289
9290 if (is_capped_double && new_align >= double_align)
9291 {
9292 if (align_clause)
9293 align = new_align * BITS_PER_UNIT;
9294 }
9295 else
9296 {
9297 if (is_capped_double)
9298 align = double_align * BITS_PER_UNIT;
9299
9300 post_error_ne_num ("alignment for& must be at least ^",
9301 gnat_error_node, gnat_entity,
9302 align / BITS_PER_UNIT);
9303 }
9304 }
9305 else
9306 {
9307 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
9308 if (new_align > align)
9309 align = new_align;
9310 }
9311
9312 return align;
9313 }
9314
9315 /* Promote the alignment of GNU_TYPE corresponding to GNAT_ENTITY. Return
9316 a positive value on success or zero on failure. */
9317
9318 static unsigned int
9319 promote_object_alignment (tree gnu_type, Entity_Id gnat_entity)
9320 {
9321 unsigned int align, size_cap, align_cap;
9322
9323 /* No point in promoting the alignment if this doesn't prevent BLKmode access
9324 to the object, in particular block copy, as this will for example disable
9325 the NRV optimization for it. No point in jumping through all the hoops
9326 needed in order to support BIGGEST_ALIGNMENT if we don't really have to.
9327 So we cap to the smallest alignment that corresponds to a known efficient
9328 memory access pattern, except for a full access entity. */
9329 if (Is_Full_Access (gnat_entity))
9330 {
9331 size_cap = UINT_MAX;
9332 align_cap = BIGGEST_ALIGNMENT;
9333 }
9334 else
9335 {
9336 size_cap = MAX_FIXED_MODE_SIZE;
9337 align_cap = get_mode_alignment (ptr_mode);
9338 }
9339
9340 /* Do the promotion within the above limits. */
9341 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
9342 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
9343 align = 0;
9344 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
9345 align = align_cap;
9346 else
9347 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
9348
9349 /* But make sure not to under-align the object. */
9350 if (align <= TYPE_ALIGN (gnu_type))
9351 align = 0;
9352
9353 /* And honor the minimum valid atomic alignment, if any. */
9354 #ifdef MINIMUM_ATOMIC_ALIGNMENT
9355 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
9356 align = MINIMUM_ATOMIC_ALIGNMENT;
9357 #endif
9358
9359 return align;
9360 }
9361
9362 /* Verify that TYPE is something we can implement atomically. If not, issue
9363 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
9364 process a component type. */
9365
9366 static void
9367 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
9368 {
9369 Node_Id gnat_error_point = gnat_entity;
9370 Node_Id gnat_node;
9371 machine_mode mode;
9372 enum mode_class mclass;
9373 unsigned int align;
9374 tree size;
9375
9376 /* If this is an anonymous base type, nothing to check, the error will be
9377 reported on the source type if need be. */
9378 if (!Comes_From_Source (gnat_entity))
9379 return;
9380
9381 mode = TYPE_MODE (type);
9382 mclass = GET_MODE_CLASS (mode);
9383 align = TYPE_ALIGN (type);
9384 size = TYPE_SIZE (type);
9385
9386 /* Consider all aligned floating-point types atomic and any aligned types
9387 that are represented by integers no wider than a machine word. */
9388 scalar_int_mode int_mode;
9389 if ((mclass == MODE_FLOAT
9390 || (is_a <scalar_int_mode> (mode, &int_mode)
9391 && GET_MODE_BITSIZE (int_mode) <= BITS_PER_WORD))
9392 && align >= GET_MODE_ALIGNMENT (mode))
9393 return;
9394
9395 /* For the moment, also allow anything that has an alignment equal to its
9396 size and which is smaller than a word. */
9397 if (size
9398 && TREE_CODE (size) == INTEGER_CST
9399 && compare_tree_int (size, align) == 0
9400 && align <= BITS_PER_WORD)
9401 return;
9402
9403 for (gnat_node = First_Rep_Item (gnat_entity);
9404 Present (gnat_node);
9405 gnat_node = Next_Rep_Item (gnat_node))
9406 if (Nkind (gnat_node) == N_Pragma)
9407 {
9408 unsigned char pragma_id
9409 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
9410
9411 if ((pragma_id == Pragma_Atomic && !component_p)
9412 || (pragma_id == Pragma_Atomic_Components && component_p))
9413 {
9414 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
9415 break;
9416 }
9417 }
9418
9419 if (component_p)
9420 post_error_ne ("atomic access to component of & cannot be guaranteed",
9421 gnat_error_point, gnat_entity);
9422 else if (Is_Volatile_Full_Access (gnat_entity))
9423 post_error_ne ("volatile full access to & cannot be guaranteed",
9424 gnat_error_point, gnat_entity);
9425 else
9426 post_error_ne ("atomic access to & cannot be guaranteed",
9427 gnat_error_point, gnat_entity);
9428 }
9429
9430
9431 /* Helper for the intrin compatibility checks family. Evaluate whether
9432 two types are definitely incompatible. */
9433
9434 static bool
9435 intrin_types_incompatible_p (tree t1, tree t2)
9436 {
9437 enum tree_code code;
9438
9439 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
9440 return false;
9441
9442 if (TYPE_MODE (t1) != TYPE_MODE (t2))
9443 return true;
9444
9445 if (TREE_CODE (t1) != TREE_CODE (t2))
9446 return true;
9447
9448 code = TREE_CODE (t1);
9449
9450 switch (code)
9451 {
9452 case INTEGER_TYPE:
9453 case REAL_TYPE:
9454 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
9455
9456 case POINTER_TYPE:
9457 case REFERENCE_TYPE:
9458 /* Assume designated types are ok. We'd need to account for char * and
9459 void * variants to do better, which could rapidly get messy and isn't
9460 clearly worth the effort. */
9461 return false;
9462
9463 default:
9464 break;
9465 }
9466
9467 return false;
9468 }
9469
9470 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
9471 on the Ada/builtin argument lists for the INB binding. */
9472
9473 static bool
9474 intrin_arglists_compatible_p (intrin_binding_t * inb)
9475 {
9476 function_args_iterator ada_iter, btin_iter;
9477
9478 function_args_iter_init (&ada_iter, inb->ada_fntype);
9479 function_args_iter_init (&btin_iter, inb->btin_fntype);
9480
9481 /* Sequence position of the last argument we checked. */
9482 int argpos = 0;
9483
9484 while (true)
9485 {
9486 tree ada_type = function_args_iter_cond (&ada_iter);
9487 tree btin_type = function_args_iter_cond (&btin_iter);
9488
9489 /* If we've exhausted both lists simultaneously, we're done. */
9490 if (!ada_type && !btin_type)
9491 break;
9492
9493 /* If the internal builtin uses a variable list, accept anything. */
9494 if (!btin_type)
9495 break;
9496
9497 /* If we're done with the Ada args and not with the internal builtin
9498 args, or the other way around, complain. */
9499 if (ada_type == void_type_node
9500 && btin_type != void_type_node)
9501 {
9502 post_error ("?Ada arguments list too short!", inb->gnat_entity);
9503 return false;
9504 }
9505
9506 if (btin_type == void_type_node
9507 && ada_type != void_type_node)
9508 {
9509 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
9510 inb->gnat_entity, inb->gnat_entity, argpos);
9511 return false;
9512 }
9513
9514 /* Otherwise, check that types match for the current argument. */
9515 argpos ++;
9516 if (intrin_types_incompatible_p (ada_type, btin_type))
9517 {
9518 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
9519 inb->gnat_entity, inb->gnat_entity, argpos);
9520 return false;
9521 }
9522
9523
9524 function_args_iter_next (&ada_iter);
9525 function_args_iter_next (&btin_iter);
9526 }
9527
9528 return true;
9529 }
9530
9531 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
9532 on the Ada/builtin return values for the INB binding. */
9533
9534 static bool
9535 intrin_return_compatible_p (intrin_binding_t * inb)
9536 {
9537 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
9538 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
9539
9540 /* Accept function imported as procedure, common and convenient. */
9541 if (VOID_TYPE_P (ada_return_type)
9542 && !VOID_TYPE_P (btin_return_type))
9543 return true;
9544
9545 /* Check return types compatibility otherwise. Note that this
9546 handles void/void as well. */
9547 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
9548 {
9549 post_error ("?intrinsic binding type mismatch on return value!",
9550 inb->gnat_entity);
9551 return false;
9552 }
9553
9554 return true;
9555 }
9556
9557 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
9558 compatible. Issue relevant warnings when they are not.
9559
9560 This is intended as a light check to diagnose the most obvious cases, not
9561 as a full fledged type compatibility predicate. It is the programmer's
9562 responsibility to ensure correctness of the Ada declarations in Imports,
9563 especially when binding straight to a compiler internal. */
9564
9565 static bool
9566 intrin_profiles_compatible_p (intrin_binding_t * inb)
9567 {
9568 /* Check compatibility on return values and argument lists, each responsible
9569 for posting warnings as appropriate. Ensure use of the proper sloc for
9570 this purpose. */
9571
9572 bool arglists_compatible_p, return_compatible_p;
9573 location_t saved_location = input_location;
9574
9575 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
9576
9577 return_compatible_p = intrin_return_compatible_p (inb);
9578 arglists_compatible_p = intrin_arglists_compatible_p (inb);
9579
9580 input_location = saved_location;
9581
9582 return return_compatible_p && arglists_compatible_p;
9583 }
9584
9585 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
9586 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
9587 specified size for this field. POS_LIST is a position list describing
9588 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
9589 to this layout. */
9590
9591 static tree
9592 create_field_decl_from (tree old_field, tree field_type, tree record_type,
9593 tree size, tree pos_list,
9594 vec<subst_pair> subst_list)
9595 {
9596 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
9597 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
9598 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
9599 tree new_pos, new_field;
9600 unsigned int i;
9601 subst_pair *s;
9602
9603 if (CONTAINS_PLACEHOLDER_P (pos))
9604 FOR_EACH_VEC_ELT (subst_list, i, s)
9605 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
9606
9607 /* If the position is now a constant, we can set it as the position of the
9608 field when we make it. Otherwise, we need to deal with it specially. */
9609 if (TREE_CONSTANT (pos))
9610 new_pos = bit_from_pos (pos, bitpos);
9611 else
9612 new_pos = NULL_TREE;
9613
9614 new_field
9615 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
9616 size, new_pos, DECL_PACKED (old_field),
9617 !DECL_NONADDRESSABLE_P (old_field));
9618
9619 if (!new_pos)
9620 {
9621 normalize_offset (&pos, &bitpos, offset_align);
9622 /* Finalize the position. */
9623 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
9624 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
9625 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
9626 DECL_SIZE (new_field) = size;
9627 DECL_SIZE_UNIT (new_field)
9628 = convert (sizetype,
9629 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
9630 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
9631 }
9632
9633 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
9634 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
9635 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
9636 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
9637
9638 return new_field;
9639 }
9640
9641 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
9642 it is the minimal size the REP_PART must have. */
9643
9644 static tree
9645 create_rep_part (tree rep_type, tree record_type, tree min_size)
9646 {
9647 tree field;
9648
9649 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
9650 min_size = NULL_TREE;
9651
9652 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
9653 min_size, NULL_TREE, 0, 1);
9654 DECL_INTERNAL_P (field) = 1;
9655
9656 return field;
9657 }
9658
9659 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9660
9661 static tree
9662 get_rep_part (tree record_type)
9663 {
9664 tree field = TYPE_FIELDS (record_type);
9665
9666 /* The REP part is the first field, internal, another record, and its name
9667 starts with an 'R'. */
9668 if (field
9669 && DECL_INTERNAL_P (field)
9670 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9671 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9672 return field;
9673
9674 return NULL_TREE;
9675 }
9676
9677 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9678
9679 tree
9680 get_variant_part (tree record_type)
9681 {
9682 tree field;
9683
9684 /* The variant part is the only internal field that is a qualified union. */
9685 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9686 if (DECL_INTERNAL_P (field)
9687 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9688 return field;
9689
9690 return NULL_TREE;
9691 }
9692
9693 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9694 the list of variants to be used and RECORD_TYPE is the type of the parent.
9695 POS_LIST is a position list describing the layout of fields present in
9696 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9697 layout. DEBUG_INFO_P is true if we need to write debug information. */
9698
9699 static tree
9700 create_variant_part_from (tree old_variant_part,
9701 vec<variant_desc> variant_list,
9702 tree record_type, tree pos_list,
9703 vec<subst_pair> subst_list,
9704 bool debug_info_p)
9705 {
9706 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9707 tree old_union_type = TREE_TYPE (old_variant_part);
9708 tree new_union_type, new_variant_part;
9709 tree union_field_list = NULL_TREE;
9710 variant_desc *v;
9711 unsigned int i;
9712
9713 /* First create the type of the variant part from that of the old one. */
9714 new_union_type = make_node (QUAL_UNION_TYPE);
9715 TYPE_NAME (new_union_type)
9716 = concat_name (TYPE_NAME (record_type),
9717 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9718
9719 /* If the position of the variant part is constant, subtract it from the
9720 size of the type of the parent to get the new size. This manual CSE
9721 reduces the code size when not optimizing. */
9722 if (TREE_CODE (offset) == INTEGER_CST
9723 && TYPE_SIZE (record_type)
9724 && TYPE_SIZE_UNIT (record_type))
9725 {
9726 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9727 tree first_bit = bit_from_pos (offset, bitpos);
9728 TYPE_SIZE (new_union_type)
9729 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9730 TYPE_SIZE_UNIT (new_union_type)
9731 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9732 byte_from_pos (offset, bitpos));
9733 SET_TYPE_ADA_SIZE (new_union_type,
9734 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9735 first_bit));
9736 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9737 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9738 }
9739 else
9740 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9741
9742 /* Now finish up the new variants and populate the union type. */
9743 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9744 {
9745 tree old_field = v->field, new_field;
9746 tree old_variant, old_variant_subpart, new_variant, field_list;
9747
9748 /* Skip variants that don't belong to this nesting level. */
9749 if (DECL_CONTEXT (old_field) != old_union_type)
9750 continue;
9751
9752 /* Retrieve the list of fields already added to the new variant. */
9753 new_variant = v->new_type;
9754 field_list = TYPE_FIELDS (new_variant);
9755
9756 /* If the old variant had a variant subpart, we need to create a new
9757 variant subpart and add it to the field list. */
9758 old_variant = v->type;
9759 old_variant_subpart = get_variant_part (old_variant);
9760 if (old_variant_subpart)
9761 {
9762 tree new_variant_subpart
9763 = create_variant_part_from (old_variant_subpart, variant_list,
9764 new_variant, pos_list, subst_list,
9765 debug_info_p);
9766 DECL_CHAIN (new_variant_subpart) = field_list;
9767 field_list = new_variant_subpart;
9768 }
9769
9770 /* Finish up the new variant and create the field. */
9771 finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p);
9772 create_type_decl (TYPE_NAME (new_variant), new_variant, true,
9773 debug_info_p, Empty);
9774
9775 new_field
9776 = create_field_decl_from (old_field, new_variant, new_union_type,
9777 TYPE_SIZE (new_variant),
9778 pos_list, subst_list);
9779 DECL_QUALIFIER (new_field) = v->qual;
9780 DECL_INTERNAL_P (new_field) = 1;
9781 DECL_CHAIN (new_field) = union_field_list;
9782 union_field_list = new_field;
9783 }
9784
9785 /* Finish up the union type and create the variant part. Note that we don't
9786 reverse the field list because VARIANT_LIST has been traversed in reverse
9787 order. */
9788 finish_record_type (new_union_type, union_field_list, 2, debug_info_p);
9789 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true,
9790 debug_info_p, Empty);
9791
9792 new_variant_part
9793 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9794 TYPE_SIZE (new_union_type),
9795 pos_list, subst_list);
9796 DECL_INTERNAL_P (new_variant_part) = 1;
9797
9798 /* With multiple discriminants it is possible for an inner variant to be
9799 statically selected while outer ones are not; in this case, the list
9800 of fields of the inner variant is not flattened and we end up with a
9801 qualified union with a single member. Drop the useless container. */
9802 if (!DECL_CHAIN (union_field_list))
9803 {
9804 DECL_CONTEXT (union_field_list) = record_type;
9805 DECL_FIELD_OFFSET (union_field_list)
9806 = DECL_FIELD_OFFSET (new_variant_part);
9807 DECL_FIELD_BIT_OFFSET (union_field_list)
9808 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9809 SET_DECL_OFFSET_ALIGN (union_field_list,
9810 DECL_OFFSET_ALIGN (new_variant_part));
9811 new_variant_part = union_field_list;
9812 }
9813
9814 return new_variant_part;
9815 }
9816
9817 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9818 which are both RECORD_TYPE, after applying the substitutions described
9819 in SUBST_LIST. */
9820
9821 static void
9822 copy_and_substitute_in_size (tree new_type, tree old_type,
9823 vec<subst_pair> subst_list)
9824 {
9825 unsigned int i;
9826 subst_pair *s;
9827
9828 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9829 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9830 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9831 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9832 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9833
9834 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9835 FOR_EACH_VEC_ELT (subst_list, i, s)
9836 TYPE_SIZE (new_type)
9837 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9838 s->discriminant, s->replacement);
9839
9840 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9841 FOR_EACH_VEC_ELT (subst_list, i, s)
9842 TYPE_SIZE_UNIT (new_type)
9843 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9844 s->discriminant, s->replacement);
9845
9846 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9847 FOR_EACH_VEC_ELT (subst_list, i, s)
9848 SET_TYPE_ADA_SIZE
9849 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9850 s->discriminant, s->replacement));
9851
9852 /* Finalize the size. */
9853 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9854 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9855 }
9856
9857 /* Return true if DISC is a stored discriminant of RECORD_TYPE. */
9858
9859 static inline bool
9860 is_stored_discriminant (Entity_Id discr, Entity_Id record_type)
9861 {
9862 if (Is_Unchecked_Union (record_type))
9863 return false;
9864 else if (Is_Tagged_Type (record_type))
9865 return No (Corresponding_Discriminant (discr));
9866 else if (Ekind (record_type) == E_Record_Type)
9867 return Original_Record_Component (discr) == discr;
9868 else
9869 return true;
9870 }
9871
9872 /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are
9873 both record types, after applying the substitutions described in SUBST_LIST.
9874 DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */
9875
9876 static void
9877 copy_and_substitute_in_layout (Entity_Id gnat_new_type,
9878 Entity_Id gnat_old_type,
9879 tree gnu_new_type,
9880 tree gnu_old_type,
9881 vec<subst_pair> subst_list,
9882 bool debug_info_p)
9883 {
9884 const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype);
9885 tree gnu_field_list = NULL_TREE;
9886 tree gnu_variable_field_list = NULL_TREE;
9887 bool selected_variant;
9888 vec<variant_desc> gnu_variant_list;
9889
9890 /* Look for REP and variant parts in the old type. */
9891 tree gnu_rep_part = get_rep_part (gnu_old_type);
9892 tree gnu_variant_part = get_variant_part (gnu_old_type);
9893
9894 /* If there is a variant part, we must compute whether the constraints
9895 statically select a particular variant. If so, we simply drop the
9896 qualified union and flatten the list of fields. Otherwise we will
9897 build a new qualified union for the variants that are still relevant. */
9898 if (gnu_variant_part)
9899 {
9900 const Node_Id gnat_decl = Declaration_Node (gnat_new_type);
9901 variant_desc *v;
9902 unsigned int i;
9903
9904 gnu_variant_list
9905 = build_variant_list (TREE_TYPE (gnu_variant_part),
9906 is_subtype
9907 ? Empty
9908 : Variant_Part
9909 (Component_List (Type_Definition (gnat_decl))),
9910 subst_list,
9911 vNULL);
9912
9913 /* If all the qualifiers are unconditionally true, the innermost variant
9914 is statically selected. */
9915 selected_variant = true;
9916 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9917 if (!integer_onep (v->qual))
9918 {
9919 selected_variant = false;
9920 break;
9921 }
9922
9923 /* Otherwise, create the new variants. */
9924 if (!selected_variant)
9925 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9926 {
9927 tree old_variant = v->type;
9928 tree new_variant = make_node (RECORD_TYPE);
9929 tree suffix
9930 = concat_name (DECL_NAME (gnu_variant_part),
9931 IDENTIFIER_POINTER (DECL_NAME (v->field)));
9932 TYPE_NAME (new_variant)
9933 = concat_name (TYPE_NAME (gnu_new_type),
9934 IDENTIFIER_POINTER (suffix));
9935 TYPE_REVERSE_STORAGE_ORDER (new_variant)
9936 = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type);
9937 copy_and_substitute_in_size (new_variant, old_variant, subst_list);
9938 v->new_type = new_variant;
9939 }
9940 }
9941 else
9942 {
9943 gnu_variant_list.create (0);
9944 selected_variant = false;
9945 }
9946
9947 /* Make a list of fields and their position in the old type. */
9948 tree gnu_pos_list
9949 = build_position_list (gnu_old_type,
9950 gnu_variant_list.exists () && !selected_variant,
9951 size_zero_node, bitsize_zero_node,
9952 BIGGEST_ALIGNMENT, NULL_TREE);
9953
9954 /* Now go down every component in the new type and compute its size and
9955 position from those of the component in the old type and the stored
9956 constraints of the new type. */
9957 Entity_Id gnat_field, gnat_old_field;
9958 for (gnat_field = First_Entity (gnat_new_type);
9959 Present (gnat_field);
9960 gnat_field = Next_Entity (gnat_field))
9961 if ((Ekind (gnat_field) == E_Component
9962 || (Ekind (gnat_field) == E_Discriminant
9963 && is_stored_discriminant (gnat_field, gnat_new_type)))
9964 && (gnat_old_field = is_subtype
9965 ? Original_Record_Component (gnat_field)
9966 : Corresponding_Record_Component (gnat_field))
9967 && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type
9968 && present_gnu_tree (gnat_old_field))
9969 {
9970 Name_Id gnat_name = Chars (gnat_field);
9971 tree gnu_old_field = get_gnu_tree (gnat_old_field);
9972 if (TREE_CODE (gnu_old_field) == COMPONENT_REF)
9973 gnu_old_field = TREE_OPERAND (gnu_old_field, 1);
9974 tree gnu_context = DECL_CONTEXT (gnu_old_field);
9975 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
9976 tree gnu_cont_type, gnu_last = NULL_TREE;
9977 variant_desc *v = NULL;
9978
9979 /* If the type is the same, retrieve the GCC type from the
9980 old field to take into account possible adjustments. */
9981 if (Etype (gnat_field) == Etype (gnat_old_field))
9982 gnu_field_type = TREE_TYPE (gnu_old_field);
9983 else
9984 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
9985
9986 /* If there was a component clause, the field types must be the same
9987 for the old and new types, so copy the data from the old field to
9988 avoid recomputation here. Also if the field is justified modular
9989 and the optimization in gnat_to_gnu_field was applied. */
9990 if (Present (Component_Clause (gnat_old_field))
9991 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
9992 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
9993 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
9994 == TREE_TYPE (gnu_old_field)))
9995 {
9996 gnu_size = DECL_SIZE (gnu_old_field);
9997 gnu_field_type = TREE_TYPE (gnu_old_field);
9998 }
9999
10000 /* If the old field was packed and of constant size, we have to get the
10001 old size here as it might differ from what the Etype conveys and the
10002 latter might overlap with the following field. Try to arrange the
10003 type for possible better packing along the way. */
10004 else if (DECL_PACKED (gnu_old_field)
10005 && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST)
10006 {
10007 gnu_size = DECL_SIZE (gnu_old_field);
10008 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
10009 && !TYPE_FAT_POINTER_P (gnu_field_type)
10010 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
10011 gnu_field_type = make_packable_type (gnu_field_type, true, 0);
10012 }
10013
10014 else
10015 gnu_size = TYPE_SIZE (gnu_field_type);
10016
10017 /* If the context of the old field is the old type or its REP part,
10018 put the field directly in the new type; otherwise look up the
10019 context in the variant list and put the field either in the new
10020 type if there is a selected variant or in one new variant. */
10021 if (gnu_context == gnu_old_type
10022 || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part)))
10023 gnu_cont_type = gnu_new_type;
10024 else
10025 {
10026 unsigned int i;
10027 tree rep_part;
10028
10029 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
10030 if (gnu_context == v->type
10031 || ((rep_part = get_rep_part (v->type))
10032 && gnu_context == TREE_TYPE (rep_part)))
10033 break;
10034
10035 if (v)
10036 gnu_cont_type = selected_variant ? gnu_new_type : v->new_type;
10037 else
10038 /* The front-end may pass us zombie components if it fails to
10039 recognize that a constrain statically selects a particular
10040 variant. Discard them. */
10041 continue;
10042 }
10043
10044 /* Now create the new field modeled on the old one. */
10045 gnu_field
10046 = create_field_decl_from (gnu_old_field, gnu_field_type,
10047 gnu_cont_type, gnu_size,
10048 gnu_pos_list, subst_list);
10049 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
10050
10051 /* If the context is a variant, put it in the new variant directly. */
10052 if (gnu_cont_type != gnu_new_type)
10053 {
10054 if (TREE_CODE (gnu_pos) == INTEGER_CST)
10055 {
10056 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
10057 TYPE_FIELDS (gnu_cont_type) = gnu_field;
10058 }
10059 else
10060 {
10061 DECL_CHAIN (gnu_field) = v->aux;
10062 v->aux = gnu_field;
10063 }
10064 }
10065
10066 /* To match the layout crafted in components_to_record, if this is
10067 the _Tag or _Parent field, put it before any other fields. */
10068 else if (gnat_name == Name_uTag || gnat_name == Name_uParent)
10069 gnu_field_list = chainon (gnu_field_list, gnu_field);
10070
10071 /* Similarly, if this is the _Controller field, put it before the
10072 other fields except for the _Tag or _Parent field. */
10073 else if (gnat_name == Name_uController && gnu_last)
10074 {
10075 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
10076 DECL_CHAIN (gnu_last) = gnu_field;
10077 }
10078
10079 /* Otherwise, put it after the other fields. */
10080 else
10081 {
10082 if (TREE_CODE (gnu_pos) == INTEGER_CST)
10083 {
10084 DECL_CHAIN (gnu_field) = gnu_field_list;
10085 gnu_field_list = gnu_field;
10086 if (!gnu_last)
10087 gnu_last = gnu_field;
10088 }
10089 else
10090 {
10091 DECL_CHAIN (gnu_field) = gnu_variable_field_list;
10092 gnu_variable_field_list = gnu_field;
10093 }
10094 }
10095
10096 /* For a stored discriminant in a derived type, replace the field. */
10097 if (!is_subtype && Ekind (gnat_field) == E_Discriminant)
10098 {
10099 tree gnu_ref = get_gnu_tree (gnat_field);
10100 TREE_OPERAND (gnu_ref, 1) = gnu_field;
10101 }
10102 else
10103 save_gnu_tree (gnat_field, gnu_field, false);
10104 }
10105
10106 /* Put the fields with fixed position in order of increasing position. */
10107 if (gnu_field_list)
10108 gnu_field_list = reverse_sort_field_list (gnu_field_list);
10109
10110 /* Put the fields with variable position at the end. */
10111 if (gnu_variable_field_list)
10112 gnu_field_list = chainon (gnu_variable_field_list, gnu_field_list);
10113
10114 /* If there is a variant list and no selected variant, we need to create the
10115 nest of variant parts from the old nest. */
10116 if (gnu_variant_list.exists () && !selected_variant)
10117 {
10118 variant_desc *v;
10119 unsigned int i;
10120
10121 /* Same processing as above for the fields of each variant. */
10122 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
10123 {
10124 if (TYPE_FIELDS (v->new_type))
10125 TYPE_FIELDS (v->new_type)
10126 = reverse_sort_field_list (TYPE_FIELDS (v->new_type));
10127 if (v->aux)
10128 TYPE_FIELDS (v->new_type)
10129 = chainon (v->aux, TYPE_FIELDS (v->new_type));
10130 }
10131
10132 tree new_variant_part
10133 = create_variant_part_from (gnu_variant_part, gnu_variant_list,
10134 gnu_new_type, gnu_pos_list,
10135 subst_list, debug_info_p);
10136 DECL_CHAIN (new_variant_part) = gnu_field_list;
10137 gnu_field_list = new_variant_part;
10138 }
10139
10140 gnu_variant_list.release ();
10141 subst_list.release ();
10142
10143 /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE.
10144 Otherwise sizes and alignment must be computed independently. */
10145 finish_record_type (gnu_new_type, nreverse (gnu_field_list),
10146 is_subtype ? 2 : 1, debug_info_p);
10147
10148 /* Now go through the entities again looking for itypes that we have not yet
10149 elaborated (e.g. Etypes of fields that have Original_Components). */
10150 for (Entity_Id gnat_field = First_Entity (gnat_new_type);
10151 Present (gnat_field);
10152 gnat_field = Next_Entity (gnat_field))
10153 if ((Ekind (gnat_field) == E_Component
10154 || Ekind (gnat_field) == E_Discriminant)
10155 && Is_Itype (Etype (gnat_field))
10156 && !present_gnu_tree (Etype (gnat_field)))
10157 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
10158 }
10159
10160 /* Associate to the implementation type of a packed array type specified by
10161 GNU_TYPE, which is the translation of GNAT_ENTITY, the original array type
10162 if it has been translated. This association is a parallel type for GNAT
10163 encodings or a debug type for standard DWARF. Note that for standard DWARF,
10164 we also want to get the original type name and therefore we return it. */
10165
10166 static tree
10167 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
10168 {
10169 const Entity_Id gnat_original_array_type
10170 = Underlying_Type (Original_Array_Type (gnat_entity));
10171 tree gnu_original_array_type;
10172
10173 if (!present_gnu_tree (gnat_original_array_type))
10174 return NULL_TREE;
10175
10176 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
10177
10178 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
10179 return NULL_TREE;
10180
10181 gcc_assert (TYPE_IMPL_PACKED_ARRAY_P (gnu_type));
10182
10183 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
10184 {
10185 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
10186
10187 tree original_name = TYPE_NAME (gnu_original_array_type);
10188 if (TREE_CODE (original_name) == TYPE_DECL)
10189 original_name = DECL_NAME (original_name);
10190 return original_name;
10191 }
10192 else
10193 {
10194 add_parallel_type (gnu_type, gnu_original_array_type);
10195 return NULL_TREE;
10196 }
10197 }
10198
10199 /* Given a type T, a FIELD_DECL F, and a replacement value R, return an
10200 equivalent type with adjusted size expressions where all occurrences
10201 of references to F in a PLACEHOLDER_EXPR have been replaced by R.
10202
10203 The function doesn't update the layout of the type, i.e. it assumes
10204 that the substitution is purely formal. That's why the replacement
10205 value R must itself contain a PLACEHOLDER_EXPR. */
10206
10207 tree
10208 substitute_in_type (tree t, tree f, tree r)
10209 {
10210 tree nt;
10211
10212 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
10213
10214 switch (TREE_CODE (t))
10215 {
10216 case INTEGER_TYPE:
10217 case ENUMERAL_TYPE:
10218 case BOOLEAN_TYPE:
10219 case REAL_TYPE:
10220
10221 /* First the domain types of arrays. */
10222 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
10223 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
10224 {
10225 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
10226 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
10227
10228 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
10229 return t;
10230
10231 nt = copy_type (t);
10232 TYPE_GCC_MIN_VALUE (nt) = low;
10233 TYPE_GCC_MAX_VALUE (nt) = high;
10234
10235 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
10236 SET_TYPE_INDEX_TYPE
10237 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
10238
10239 return nt;
10240 }
10241
10242 /* Then the subtypes. */
10243 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
10244 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
10245 {
10246 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
10247 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
10248
10249 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
10250 return t;
10251
10252 nt = copy_type (t);
10253 SET_TYPE_RM_MIN_VALUE (nt, low);
10254 SET_TYPE_RM_MAX_VALUE (nt, high);
10255
10256 return nt;
10257 }
10258
10259 return t;
10260
10261 case COMPLEX_TYPE:
10262 nt = substitute_in_type (TREE_TYPE (t), f, r);
10263 if (nt == TREE_TYPE (t))
10264 return t;
10265
10266 return build_complex_type (nt);
10267
10268 case FUNCTION_TYPE:
10269 case METHOD_TYPE:
10270 /* These should never show up here. */
10271 gcc_unreachable ();
10272
10273 case ARRAY_TYPE:
10274 {
10275 tree component = substitute_in_type (TREE_TYPE (t), f, r);
10276 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
10277
10278 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10279 return t;
10280
10281 nt = build_nonshared_array_type (component, domain);
10282 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
10283 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
10284 SET_TYPE_MODE (nt, TYPE_MODE (t));
10285 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
10286 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
10287 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
10288 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
10289 if (TYPE_REVERSE_STORAGE_ORDER (t))
10290 set_reverse_storage_order_on_array_type (nt);
10291 if (TYPE_NONALIASED_COMPONENT (t))
10292 set_nonaliased_component_on_array_type (nt);
10293 return nt;
10294 }
10295
10296 case RECORD_TYPE:
10297 case UNION_TYPE:
10298 case QUAL_UNION_TYPE:
10299 {
10300 bool changed_field = false;
10301 tree field;
10302
10303 /* Start out with no fields, make new fields, and chain them
10304 in. If we haven't actually changed the type of any field,
10305 discard everything we've done and return the old type. */
10306 nt = copy_type (t);
10307 TYPE_FIELDS (nt) = NULL_TREE;
10308
10309 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
10310 {
10311 tree new_field = copy_node (field), new_n;
10312
10313 new_n = substitute_in_type (TREE_TYPE (field), f, r);
10314 if (new_n != TREE_TYPE (field))
10315 {
10316 TREE_TYPE (new_field) = new_n;
10317 changed_field = true;
10318 }
10319
10320 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
10321 if (new_n != DECL_FIELD_OFFSET (field))
10322 {
10323 DECL_FIELD_OFFSET (new_field) = new_n;
10324 changed_field = true;
10325 }
10326
10327 /* Do the substitution inside the qualifier, if any. */
10328 if (TREE_CODE (t) == QUAL_UNION_TYPE)
10329 {
10330 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
10331 if (new_n != DECL_QUALIFIER (field))
10332 {
10333 DECL_QUALIFIER (new_field) = new_n;
10334 changed_field = true;
10335 }
10336 }
10337
10338 DECL_CONTEXT (new_field) = nt;
10339 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
10340
10341 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
10342 TYPE_FIELDS (nt) = new_field;
10343 }
10344
10345 if (!changed_field)
10346 return t;
10347
10348 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
10349 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
10350 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
10351 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
10352 return nt;
10353 }
10354
10355 default:
10356 return t;
10357 }
10358 }
10359
10360 /* Return the RM size of GNU_TYPE. This is the actual number of bits
10361 needed to represent the object. */
10362
10363 tree
10364 rm_size (tree gnu_type)
10365 {
10366 /* For integral types, we store the RM size explicitly. */
10367 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
10368 return TYPE_RM_SIZE (gnu_type);
10369
10370 /* If the type contains a template, return the padded size of the template
10371 plus the RM size of the actual data. */
10372 if (TREE_CODE (gnu_type) == RECORD_TYPE
10373 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
10374 return
10375 size_binop (PLUS_EXPR,
10376 bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type))),
10377 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))));
10378
10379 /* For record or union types, we store the size explicitly. */
10380 if (RECORD_OR_UNION_TYPE_P (gnu_type)
10381 && !TYPE_FAT_POINTER_P (gnu_type)
10382 && TYPE_ADA_SIZE (gnu_type))
10383 return TYPE_ADA_SIZE (gnu_type);
10384
10385 /* For other types, this is just the size. */
10386 return TYPE_SIZE (gnu_type);
10387 }
10388
10389 /* Return the name to be used for GNAT_ENTITY. If a type, create a
10390 fully-qualified name, possibly with type information encoding.
10391 Otherwise, return the name. */
10392
10393 static const char *
10394 get_entity_char (Entity_Id gnat_entity)
10395 {
10396 Get_Encoded_Name (gnat_entity);
10397 return ggc_strdup (Name_Buffer);
10398 }
10399
10400 tree
10401 get_entity_name (Entity_Id gnat_entity)
10402 {
10403 Get_Encoded_Name (gnat_entity);
10404 return get_identifier_with_length (Name_Buffer, Name_Len);
10405 }
10406
10407 /* Return an identifier representing the external name to be used for
10408 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
10409 and the specified suffix. */
10410
10411 tree
10412 create_concat_name (Entity_Id gnat_entity, const char *suffix)
10413 {
10414 const Entity_Kind kind = Ekind (gnat_entity);
10415 const bool has_suffix = (suffix != NULL);
10416 String_Template temp = {1, has_suffix ? (int) strlen (suffix) : 0};
10417 String_Pointer sp = {suffix, &temp};
10418
10419 Get_External_Name (gnat_entity, has_suffix, sp);
10420
10421 /* A variable using the Stdcall convention lives in a DLL. We adjust
10422 its name to use the jump table, the _imp__NAME contains the address
10423 for the NAME variable. */
10424 if ((kind == E_Variable || kind == E_Constant)
10425 && Has_Stdcall_Convention (gnat_entity))
10426 {
10427 const int len = strlen (STDCALL_PREFIX) + Name_Len;
10428 char *new_name = (char *) alloca (len + 1);
10429 strcpy (new_name, STDCALL_PREFIX);
10430 strcat (new_name, Name_Buffer);
10431 return get_identifier_with_length (new_name, len);
10432 }
10433
10434 return get_identifier_with_length (Name_Buffer, Name_Len);
10435 }
10436
10437 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
10438 string, return a new IDENTIFIER_NODE that is the concatenation of
10439 the name followed by "___" and the specified suffix. */
10440
10441 tree
10442 concat_name (tree gnu_name, const char *suffix)
10443 {
10444 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
10445 char *new_name = (char *) alloca (len + 1);
10446 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
10447 strcat (new_name, "___");
10448 strcat (new_name, suffix);
10449 return get_identifier_with_length (new_name, len);
10450 }
10451
10452 /* Initialize the data structures of the decl.c module. */
10453
10454 void
10455 init_gnat_decl (void)
10456 {
10457 /* Initialize the cache of annotated values. */
10458 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
10459
10460 /* Initialize the association of dummy types with subprograms. */
10461 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
10462 }
10463
10464 /* Destroy the data structures of the decl.c module. */
10465
10466 void
10467 destroy_gnat_decl (void)
10468 {
10469 /* Destroy the cache of annotated values. */
10470 annotate_value_cache->empty ();
10471 annotate_value_cache = NULL;
10472
10473 /* Destroy the association of dummy types with subprograms. */
10474 dummy_to_subprog_map->empty ();
10475 dummy_to_subprog_map = NULL;
10476 }
10477
10478 #include "gt-ada-decl.h"