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