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