Debian Codegraph @

gcc/ada/gcc-interface/decl.cc

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