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