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