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