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