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