* gcc-interface/decl.c (gnat_to_gnu_entity): Robustify test for types
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobf626e6186d23f1a69be7cc98f749c0d3b024d79e
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. We must do this before we
717 validate the size, since the alignment can affect the size. */
718 if (kind != E_Exception && Known_Alignment (gnat_entity))
720 gcc_assert (Present (Alignment (gnat_entity)));
722 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
723 TYPE_ALIGN (gnu_type));
725 /* No point in changing the type if there is an address clause
726 as the final type of the object will be a reference type. */
727 if (Present (Address_Clause (gnat_entity)))
728 align = 0;
729 else
731 tree orig_type = gnu_type;
733 gnu_type
734 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
735 false, false, definition, true);
737 /* If a padding record was made, declare it now since it will
738 never be declared otherwise. This is necessary to ensure
739 that its subtrees are properly marked. */
740 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
741 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
742 debug_info_p, gnat_entity);
746 /* If we are defining the object, see if it has a Size and validate it
747 if so. If we are not defining the object and a Size clause applies,
748 simply retrieve the value. We don't want to ignore the clause and
749 it is expected to have been validated already. Then get the new
750 type, if any. */
751 if (definition)
752 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
753 gnat_entity, VAR_DECL, false,
754 Has_Size_Clause (gnat_entity));
755 else if (Has_Size_Clause (gnat_entity))
756 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
758 if (gnu_size)
760 gnu_type
761 = make_type_from_size (gnu_type, gnu_size,
762 Has_Biased_Representation (gnat_entity));
764 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
765 gnu_size = NULL_TREE;
768 /* If this object has self-referential size, it must be a record with
769 a default discriminant. We are supposed to allocate an object of
770 the maximum size in this case, unless it is a constant with an
771 initializing expression, in which case we can get the size from
772 that. Note that the resulting size may still be a variable, so
773 this may end up with an indirect allocation. */
774 if (No (Renamed_Object (gnat_entity))
775 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
777 if (gnu_expr && kind == E_Constant)
779 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
780 if (CONTAINS_PLACEHOLDER_P (size))
782 /* If the initializing expression is itself a constant,
783 despite having a nominal type with self-referential
784 size, we can get the size directly from it. */
785 if (TREE_CODE (gnu_expr) == COMPONENT_REF
786 && TYPE_IS_PADDING_P
787 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
788 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
789 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
790 || DECL_READONLY_ONCE_ELAB
791 (TREE_OPERAND (gnu_expr, 0))))
792 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
793 else
794 gnu_size
795 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
797 else
798 gnu_size = size;
800 /* We may have no GNU_EXPR because No_Initialization is
801 set even though there's an Expression. */
802 else if (kind == E_Constant
803 && (Nkind (Declaration_Node (gnat_entity))
804 == N_Object_Declaration)
805 && Present (Expression (Declaration_Node (gnat_entity))))
806 gnu_size
807 = TYPE_SIZE (gnat_to_gnu_type
808 (Etype
809 (Expression (Declaration_Node (gnat_entity)))));
810 else
812 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
813 mutable_p = true;
816 /* If the size isn't constant and we are at global level, call
817 elaborate_expression_1 to make a variable for it rather than
818 calculating it each time. */
819 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
820 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
821 "SIZE", definition, false);
824 /* If the size is zero byte, make it one byte since some linkers have
825 troubles with zero-sized objects. If the object will have a
826 template, that will make it nonzero so don't bother. Also avoid
827 doing that for an object renaming or an object with an address
828 clause, as we would lose useful information on the view size
829 (e.g. for null array slices) and we are not allocating the object
830 here anyway. */
831 if (((gnu_size
832 && integer_zerop (gnu_size)
833 && !TREE_OVERFLOW (gnu_size))
834 || (TYPE_SIZE (gnu_type)
835 && integer_zerop (TYPE_SIZE (gnu_type))
836 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
837 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
838 && No (Renamed_Object (gnat_entity))
839 && No (Address_Clause (gnat_entity)))
840 gnu_size = bitsize_unit_node;
842 /* If this is an object with no specified size and alignment, and
843 if either it is atomic or we are not optimizing alignment for
844 space and it is composite and not an exception, an Out parameter
845 or a reference to another object, and the size of its type is a
846 constant, set the alignment to the smallest one which is not
847 smaller than the size, with an appropriate cap. */
848 if (!gnu_size && align == 0
849 && (Is_Atomic_Or_VFA (gnat_entity)
850 || (!Optimize_Alignment_Space (gnat_entity)
851 && kind != E_Exception
852 && kind != E_Out_Parameter
853 && Is_Composite_Type (gnat_type)
854 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
855 && !Is_Exported (gnat_entity)
856 && !imported_p
857 && No (Renamed_Object (gnat_entity))
858 && No (Address_Clause (gnat_entity))))
859 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
860 align = promote_object_alignment (gnu_type, gnat_entity);
862 /* If the object is set to have atomic components, find the component
863 type and validate it.
865 ??? Note that we ignore Has_Volatile_Components on objects; it's
866 not at all clear what to do in that case. */
867 if (Has_Atomic_Components (gnat_entity))
869 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
870 ? TREE_TYPE (gnu_type) : gnu_type);
872 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
873 && TYPE_MULTI_ARRAY_P (gnu_inner))
874 gnu_inner = TREE_TYPE (gnu_inner);
876 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
879 /* If this is an aliased object with an unconstrained array nominal
880 subtype, make a type that includes the template. We will either
881 allocate or create a variable of that type, see below. */
882 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
883 && Is_Array_Type (Underlying_Type (gnat_type))
884 && !type_annotate_only)
886 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
887 gnu_type
888 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
889 gnu_type,
890 concat_name (gnu_entity_name,
891 "UNC"),
892 debug_info_p);
895 /* ??? If this is an object of CW type initialized to a value, try to
896 ensure that the object is sufficient aligned for this value, but
897 without pessimizing the allocation. This is a kludge necessary
898 because we don't support dynamic alignment. */
899 if (align == 0
900 && Ekind (gnat_type) == E_Class_Wide_Subtype
901 && No (Renamed_Object (gnat_entity))
902 && No (Address_Clause (gnat_entity)))
903 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
905 #ifdef MINIMUM_ATOMIC_ALIGNMENT
906 /* If the size is a constant and no alignment is specified, force
907 the alignment to be the minimum valid atomic alignment. The
908 restriction on constant size avoids problems with variable-size
909 temporaries; if the size is variable, there's no issue with
910 atomic access. Also don't do this for a constant, since it isn't
911 necessary and can interfere with constant replacement. Finally,
912 do not do it for Out parameters since that creates an
913 size inconsistency with In parameters. */
914 if (align == 0
915 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
916 && !FLOAT_TYPE_P (gnu_type)
917 && !const_flag && No (Renamed_Object (gnat_entity))
918 && !imported_p && No (Address_Clause (gnat_entity))
919 && kind != E_Out_Parameter
920 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
921 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
922 align = MINIMUM_ATOMIC_ALIGNMENT;
923 #endif
925 /* Make a new type with the desired size and alignment, if needed.
926 But do not take into account alignment promotions to compute the
927 size of the object. */
928 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
929 if (gnu_size || align > 0)
931 tree orig_type = gnu_type;
933 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
934 false, false, definition, true);
936 /* If a padding record was made, declare it now since it will
937 never be declared otherwise. This is necessary to ensure
938 that its subtrees are properly marked. */
939 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
940 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
941 debug_info_p, gnat_entity);
944 /* Now check if the type of the object allows atomic access. */
945 if (Is_Atomic_Or_VFA (gnat_entity))
946 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
948 /* If this is a renaming, avoid as much as possible to create a new
949 object. However, in some cases, creating it is required because
950 renaming can be applied to objects that are not names in Ada.
951 This processing needs to be applied to the raw expression so as
952 to make it more likely to rename the underlying object. */
953 if (Present (Renamed_Object (gnat_entity)))
955 /* If the renamed object had padding, strip off the reference to
956 the inner object and reset our type. */
957 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
958 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
959 /* Strip useless conversions around the object. */
960 || gnat_useless_type_conversion (gnu_expr))
962 gnu_expr = TREE_OPERAND (gnu_expr, 0);
963 gnu_type = TREE_TYPE (gnu_expr);
966 /* Or else, if the renamed object has an unconstrained type with
967 default discriminant, use the padded type. */
968 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
969 gnu_type = TREE_TYPE (gnu_expr);
971 /* Case 1: if this is a constant renaming stemming from a function
972 call, treat it as a normal object whose initial value is what
973 is being renamed. RM 3.3 says that the result of evaluating a
974 function call is a constant object. Therefore, it can be the
975 inner object of a constant renaming and the renaming must be
976 fully instantiated, i.e. it cannot be a reference to (part of)
977 an existing object. And treat other rvalues (addresses, null
978 expressions, constructors and literals) the same way. */
979 tree inner = gnu_expr;
980 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
981 inner = TREE_OPERAND (inner, 0);
982 /* Expand_Dispatching_Call can prepend a comparison of the tags
983 before the call to "=". */
984 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
985 || TREE_CODE (inner) == COMPOUND_EXPR)
986 inner = TREE_OPERAND (inner, 1);
987 if ((TREE_CODE (inner) == CALL_EXPR
988 && !call_is_atomic_load (inner))
989 || TREE_CODE (inner) == ADDR_EXPR
990 || TREE_CODE (inner) == NULL_EXPR
991 || TREE_CODE (inner) == PLUS_EXPR
992 || TREE_CODE (inner) == CONSTRUCTOR
993 || CONSTANT_CLASS_P (inner)
994 /* We need to detect the case where a temporary is created to
995 hold the return value, since we cannot safely rename it at
996 top level as it lives only in the elaboration routine. */
997 || (TREE_CODE (inner) == VAR_DECL
998 && DECL_RETURN_VALUE_P (inner))
999 /* We also need to detect the case where the front-end creates
1000 a dangling 'reference to a function call at top level and
1001 substitutes it in the renaming, for example:
1003 q__b : boolean renames r__f.e (1);
1005 can be rewritten into:
1007 q__R1s : constant q__A2s := r__f'reference;
1008 [...]
1009 q__b : boolean renames q__R1s.all.e (1);
1011 We cannot safely rename the rewritten expression since the
1012 underlying object lives only in the elaboration routine. */
1013 || (TREE_CODE (inner) == INDIRECT_REF
1014 && (inner
1015 = remove_conversions (TREE_OPERAND (inner, 0), true))
1016 && TREE_CODE (inner) == VAR_DECL
1017 && DECL_RETURN_VALUE_P (inner)))
1020 /* Case 2: if the renaming entity need not be materialized, use
1021 the elaborated renamed expression for the renaming. But this
1022 means that the caller is responsible for evaluating the address
1023 of the renaming in the correct place for the definition case to
1024 instantiate the SAVE_EXPRs. */
1025 else if (!Materialize_Entity (gnat_entity))
1027 tree init = NULL_TREE;
1029 gnu_decl
1030 = elaborate_reference (gnu_expr, gnat_entity, definition,
1031 &init);
1033 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1034 correct place for this case. */
1035 gcc_assert (!init);
1037 /* No DECL_EXPR will be created so the expression needs to be
1038 marked manually because it will likely be shared. */
1039 if (global_bindings_p ())
1040 MARK_VISITED (gnu_decl);
1042 /* This assertion will fail if the renamed object isn't aligned
1043 enough as to make it possible to honor the alignment set on
1044 the renaming. */
1045 if (align)
1047 unsigned int ralign = DECL_P (gnu_decl)
1048 ? DECL_ALIGN (gnu_decl)
1049 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1050 gcc_assert (ralign >= align);
1053 /* The expression might not be a DECL so save it manually. */
1054 save_gnu_tree (gnat_entity, gnu_decl, true);
1055 saved = true;
1056 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1057 break;
1060 /* Case 3: otherwise, make a constant pointer to the object we
1061 are renaming and attach the object to the pointer after it is
1062 elaborated. The object will be referenced directly instead
1063 of indirectly via the pointer to avoid aliasing problems with
1064 non-addressable entities. The pointer is called a "renaming"
1065 pointer in this case. Note that we also need to preserve the
1066 volatility of the renamed object through the indirection. */
1067 else
1069 tree init = NULL_TREE;
1071 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1072 gnu_type
1073 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1074 gnu_type = build_reference_type (gnu_type);
1075 used_by_ref = true;
1076 const_flag = true;
1077 volatile_flag = false;
1078 inner_const_flag = TREE_READONLY (gnu_expr);
1079 gnu_size = NULL_TREE;
1081 renamed_obj
1082 = elaborate_reference (gnu_expr, gnat_entity, definition,
1083 &init);
1085 /* The expression needs to be marked manually because it will
1086 likely be shared, even for a definition since the ADDR_EXPR
1087 built below can cause the first few nodes to be folded. */
1088 if (global_bindings_p ())
1089 MARK_VISITED (renamed_obj);
1091 if (type_annotate_only
1092 && TREE_CODE (renamed_obj) == ERROR_MARK)
1093 gnu_expr = NULL_TREE;
1094 else
1096 gnu_expr
1097 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1098 if (init)
1099 gnu_expr
1100 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1101 gnu_expr);
1106 /* If we are defining an aliased object whose nominal subtype is
1107 unconstrained, the object is a record that contains both the
1108 template and the object. If there is an initializer, it will
1109 have already been converted to the right type, but we need to
1110 create the template if there is no initializer. */
1111 if (definition
1112 && !gnu_expr
1113 && TREE_CODE (gnu_type) == RECORD_TYPE
1114 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1115 /* Beware that padding might have been introduced above. */
1116 || (TYPE_PADDING_P (gnu_type)
1117 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1118 == RECORD_TYPE
1119 && TYPE_CONTAINS_TEMPLATE_P
1120 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1122 tree template_field
1123 = TYPE_PADDING_P (gnu_type)
1124 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1125 : TYPE_FIELDS (gnu_type);
1126 vec<constructor_elt, va_gc> *v;
1127 vec_alloc (v, 1);
1128 tree t = build_template (TREE_TYPE (template_field),
1129 TREE_TYPE (DECL_CHAIN (template_field)),
1130 NULL_TREE);
1131 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1132 gnu_expr = gnat_build_constructor (gnu_type, v);
1135 /* Convert the expression to the type of the object if need be. */
1136 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1137 gnu_expr = convert (gnu_type, gnu_expr);
1139 /* If this is a pointer that doesn't have an initializing expression,
1140 initialize it to NULL, unless the object is declared imported as
1141 per RM B.1(24). */
1142 if (definition
1143 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1144 && !gnu_expr
1145 && !Is_Imported (gnat_entity))
1146 gnu_expr = integer_zero_node;
1148 /* If we are defining the object and it has an Address clause, we must
1149 either get the address expression from the saved GCC tree for the
1150 object if it has a Freeze node, or elaborate the address expression
1151 here since the front-end has guaranteed that the elaboration has no
1152 effects in this case. */
1153 if (definition && Present (Address_Clause (gnat_entity)))
1155 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1156 Node_Id gnat_address = Expression (gnat_clause);
1157 tree gnu_address
1158 = present_gnu_tree (gnat_entity)
1159 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1161 save_gnu_tree (gnat_entity, NULL_TREE, false);
1163 /* Convert the type of the object to a reference type that can
1164 alias everything as per RM 13.3(19). */
1165 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1166 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1167 gnu_type
1168 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1169 gnu_address = convert (gnu_type, gnu_address);
1170 used_by_ref = true;
1171 const_flag
1172 = (!Is_Public (gnat_entity)
1173 || compile_time_known_address_p (gnat_address));
1174 volatile_flag = false;
1175 gnu_size = NULL_TREE;
1177 /* If this is an aliased object with an unconstrained array nominal
1178 subtype, then it can overlay only another aliased object with an
1179 unconstrained array nominal subtype and compatible template. */
1180 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1181 && Is_Array_Type (Underlying_Type (gnat_type))
1182 && !type_annotate_only)
1184 tree rec_type = TREE_TYPE (gnu_type);
1185 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1187 /* This is the pattern built for a regular object. */
1188 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1189 && TREE_OPERAND (gnu_address, 1) == off)
1190 gnu_address = TREE_OPERAND (gnu_address, 0);
1191 /* This is the pattern built for an overaligned object. */
1192 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1193 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1194 == PLUS_EXPR
1195 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1196 == off)
1197 gnu_address
1198 = build2 (POINTER_PLUS_EXPR, gnu_type,
1199 TREE_OPERAND (gnu_address, 0),
1200 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1201 else
1203 post_error_ne ("aliased object& with unconstrained array "
1204 "nominal subtype", gnat_clause,
1205 gnat_entity);
1206 post_error ("\\can overlay only aliased object with "
1207 "compatible subtype", gnat_clause);
1211 /* If we don't have an initializing expression for the underlying
1212 variable, the initializing expression for the pointer is the
1213 specified address. Otherwise, we have to make a COMPOUND_EXPR
1214 to assign both the address and the initial value. */
1215 if (!gnu_expr)
1216 gnu_expr = gnu_address;
1217 else
1218 gnu_expr
1219 = build2 (COMPOUND_EXPR, gnu_type,
1220 build_binary_op (INIT_EXPR, NULL_TREE,
1221 build_unary_op (INDIRECT_REF,
1222 NULL_TREE,
1223 gnu_address),
1224 gnu_expr),
1225 gnu_address);
1228 /* If it has an address clause and we are not defining it, mark it
1229 as an indirect object. Likewise for Stdcall objects that are
1230 imported. */
1231 if ((!definition && Present (Address_Clause (gnat_entity)))
1232 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1234 /* Convert the type of the object to a reference type that can
1235 alias everything as per RM 13.3(19). */
1236 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1237 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1238 gnu_type
1239 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1240 used_by_ref = true;
1241 const_flag = false;
1242 volatile_flag = false;
1243 gnu_size = NULL_TREE;
1245 /* No point in taking the address of an initializing expression
1246 that isn't going to be used. */
1247 gnu_expr = NULL_TREE;
1249 /* If it has an address clause whose value is known at compile
1250 time, make the object a CONST_DECL. This will avoid a
1251 useless dereference. */
1252 if (Present (Address_Clause (gnat_entity)))
1254 Node_Id gnat_address
1255 = Expression (Address_Clause (gnat_entity));
1257 if (compile_time_known_address_p (gnat_address))
1259 gnu_expr = gnat_to_gnu (gnat_address);
1260 const_flag = true;
1265 /* If we are at top level and this object is of variable size,
1266 make the actual type a hidden pointer to the real type and
1267 make the initializer be a memory allocation and initialization.
1268 Likewise for objects we aren't defining (presumed to be
1269 external references from other packages), but there we do
1270 not set up an initialization.
1272 If the object's size overflows, make an allocator too, so that
1273 Storage_Error gets raised. Note that we will never free
1274 such memory, so we presume it never will get allocated. */
1275 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1276 global_bindings_p ()
1277 || !definition
1278 || static_flag)
1279 || (gnu_size
1280 && !allocatable_size_p (convert (sizetype,
1281 size_binop
1282 (CEIL_DIV_EXPR, gnu_size,
1283 bitsize_unit_node)),
1284 global_bindings_p ()
1285 || !definition
1286 || static_flag)))
1288 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1289 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1290 gnu_type = build_reference_type (gnu_type);
1291 used_by_ref = true;
1292 const_flag = true;
1293 volatile_flag = false;
1294 gnu_size = NULL_TREE;
1296 /* In case this was a aliased object whose nominal subtype is
1297 unconstrained, the pointer above will be a thin pointer and
1298 build_allocator will automatically make the template.
1300 If we have a template initializer only (that we made above),
1301 pretend there is none and rely on what build_allocator creates
1302 again anyway. Otherwise (if we have a full initializer), get
1303 the data part and feed that to build_allocator.
1305 If we are elaborating a mutable object, tell build_allocator to
1306 ignore a possibly simpler size from the initializer, if any, as
1307 we must allocate the maximum possible size in this case. */
1308 if (definition && !imported_p)
1310 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1312 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1313 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1315 gnu_alloc_type
1316 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1318 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1319 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1320 gnu_expr = NULL_TREE;
1321 else
1322 gnu_expr
1323 = build_component_ref
1324 (gnu_expr,
1325 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1326 false);
1329 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1330 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1331 post_error ("?`Storage_Error` will be raised at run time!",
1332 gnat_entity);
1334 gnu_expr
1335 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1336 Empty, Empty, gnat_entity, mutable_p);
1338 else
1339 gnu_expr = NULL_TREE;
1342 /* If this object would go into the stack and has an alignment larger
1343 than the largest stack alignment the back-end can honor, resort to
1344 a variable of "aligning type". */
1345 if (definition
1346 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1347 && !imported_p
1348 && !static_flag
1349 && !global_bindings_p ())
1351 /* Create the new variable. No need for extra room before the
1352 aligned field as this is in automatic storage. */
1353 tree gnu_new_type
1354 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1355 TYPE_SIZE_UNIT (gnu_type),
1356 BIGGEST_ALIGNMENT, 0, gnat_entity);
1357 tree gnu_new_var
1358 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1359 NULL_TREE, gnu_new_type, NULL_TREE,
1360 false, false, false, false, false,
1361 true, debug_info_p && definition, NULL,
1362 gnat_entity);
1364 /* Initialize the aligned field if we have an initializer. */
1365 if (gnu_expr)
1366 add_stmt_with_node
1367 (build_binary_op (INIT_EXPR, NULL_TREE,
1368 build_component_ref
1369 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1370 false),
1371 gnu_expr),
1372 gnat_entity);
1374 /* And setup this entity as a reference to the aligned field. */
1375 gnu_type = build_reference_type (gnu_type);
1376 gnu_expr
1377 = build_unary_op
1378 (ADDR_EXPR, NULL_TREE,
1379 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1380 false));
1381 TREE_CONSTANT (gnu_expr) = 1;
1383 used_by_ref = true;
1384 const_flag = true;
1385 volatile_flag = false;
1386 gnu_size = NULL_TREE;
1389 /* If this is an aggregate constant initialized to a constant, force it
1390 to be statically allocated. This saves an initialization copy. */
1391 if (!static_flag
1392 && const_flag
1393 && gnu_expr
1394 && TREE_CONSTANT (gnu_expr)
1395 && AGGREGATE_TYPE_P (gnu_type)
1396 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1397 && !(TYPE_IS_PADDING_P (gnu_type)
1398 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1399 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1400 static_flag = true;
1402 /* If this is an aliased object with an unconstrained array nominal
1403 subtype, we make its type a thin reference, i.e. the reference
1404 counterpart of a thin pointer, so it points to the array part.
1405 This is aimed to make it easier for the debugger to decode the
1406 object. Note that we have to do it this late because of the
1407 couple of allocation adjustments that might be made above. */
1408 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1409 && Is_Array_Type (Underlying_Type (gnat_type))
1410 && !type_annotate_only)
1412 /* In case the object with the template has already been allocated
1413 just above, we have nothing to do here. */
1414 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1416 /* This variable is a GNAT encoding used by Workbench: let it
1417 go through the debugging information but mark it as
1418 artificial: users are not interested in it. */
1419 tree gnu_unc_var
1420 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1421 NULL_TREE, gnu_type, gnu_expr,
1422 const_flag, Is_Public (gnat_entity),
1423 imported_p || !definition, static_flag,
1424 volatile_flag, true,
1425 debug_info_p && definition,
1426 NULL, gnat_entity);
1427 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1428 TREE_CONSTANT (gnu_expr) = 1;
1430 used_by_ref = true;
1431 const_flag = true;
1432 volatile_flag = false;
1433 inner_const_flag = TREE_READONLY (gnu_unc_var);
1434 gnu_size = NULL_TREE;
1437 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1438 gnu_type
1439 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1442 /* Convert the expression to the type of the object if need be. */
1443 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1444 gnu_expr = convert (gnu_type, gnu_expr);
1446 /* If this name is external or a name was specified, use it, but don't
1447 use the Interface_Name with an address clause (see cd30005). */
1448 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1449 || (Present (Interface_Name (gnat_entity))
1450 && No (Address_Clause (gnat_entity))))
1451 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1453 /* Deal with a pragma Linker_Section on a constant or variable. */
1454 if ((kind == E_Constant || kind == E_Variable)
1455 && Present (Linker_Section_Pragma (gnat_entity)))
1456 prepend_one_attribute_pragma (&attr_list,
1457 Linker_Section_Pragma (gnat_entity));
1459 /* Now create the variable or the constant and set various flags. */
1460 gnu_decl
1461 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1462 gnu_expr, const_flag, Is_Public (gnat_entity),
1463 imported_p || !definition, static_flag,
1464 volatile_flag, artificial_p,
1465 debug_info_p && definition, attr_list,
1466 gnat_entity, !renamed_obj);
1467 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1468 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1469 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1471 /* If we are defining an Out parameter and optimization isn't enabled,
1472 create a fake PARM_DECL for debugging purposes and make it point to
1473 the VAR_DECL. Suppress debug info for the latter but make sure it
1474 will live in memory so that it can be accessed from within the
1475 debugger through the PARM_DECL. */
1476 if (kind == E_Out_Parameter
1477 && definition
1478 && debug_info_p
1479 && !optimize
1480 && !flag_generate_lto)
1482 tree param = create_param_decl (gnu_entity_name, gnu_type);
1483 gnat_pushdecl (param, gnat_entity);
1484 SET_DECL_VALUE_EXPR (param, gnu_decl);
1485 DECL_HAS_VALUE_EXPR_P (param) = 1;
1486 DECL_IGNORED_P (gnu_decl) = 1;
1487 TREE_ADDRESSABLE (gnu_decl) = 1;
1490 /* If this is a loop parameter, set the corresponding flag. */
1491 else if (kind == E_Loop_Parameter)
1492 DECL_LOOP_PARM_P (gnu_decl) = 1;
1494 /* If this is a renaming pointer, attach the renamed object to it. */
1495 if (renamed_obj)
1496 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1498 /* If this is a constant and we are defining it or it generates a real
1499 symbol at the object level and we are referencing it, we may want
1500 or need to have a true variable to represent it:
1501 - if optimization isn't enabled, for debugging purposes,
1502 - if the constant is public and not overlaid on something else,
1503 - if its address is taken,
1504 - if either itself or its type is aliased. */
1505 if (TREE_CODE (gnu_decl) == CONST_DECL
1506 && (definition || Sloc (gnat_entity) > Standard_Location)
1507 && ((!optimize && debug_info_p)
1508 || (Is_Public (gnat_entity)
1509 && No (Address_Clause (gnat_entity)))
1510 || Address_Taken (gnat_entity)
1511 || Is_Aliased (gnat_entity)
1512 || Is_Aliased (gnat_type)))
1514 tree gnu_corr_var
1515 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1516 gnu_expr, true, Is_Public (gnat_entity),
1517 !definition, static_flag, volatile_flag,
1518 artificial_p, debug_info_p && definition,
1519 attr_list, gnat_entity, false);
1521 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1524 /* If this is a constant, even if we don't need a true variable, we
1525 may need to avoid returning the initializer in every case. That
1526 can happen for the address of a (constant) constructor because,
1527 upon dereferencing it, the constructor will be reinjected in the
1528 tree, which may not be valid in every case; see lvalue_required_p
1529 for more details. */
1530 if (TREE_CODE (gnu_decl) == CONST_DECL)
1531 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1533 /* If this object is declared in a block that contains a block with an
1534 exception handler, and we aren't using the GCC exception mechanism,
1535 we must force this variable in memory in order to avoid an invalid
1536 optimization. */
1537 if (Front_End_Exceptions ()
1538 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1539 TREE_ADDRESSABLE (gnu_decl) = 1;
1541 /* If this is a local variable with non-BLKmode and aggregate type,
1542 and optimization isn't enabled, then force it in memory so that
1543 a register won't be allocated to it with possible subparts left
1544 uninitialized and reaching the register allocator. */
1545 else if (TREE_CODE (gnu_decl) == VAR_DECL
1546 && !DECL_EXTERNAL (gnu_decl)
1547 && !TREE_STATIC (gnu_decl)
1548 && DECL_MODE (gnu_decl) != BLKmode
1549 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1550 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1551 && !optimize)
1552 TREE_ADDRESSABLE (gnu_decl) = 1;
1554 /* If we are defining an object with variable size or an object with
1555 fixed size that will be dynamically allocated, and we are using the
1556 front-end setjmp/longjmp exception mechanism, update the setjmp
1557 buffer. */
1558 if (definition
1559 && Exception_Mechanism == Front_End_SJLJ
1560 && get_block_jmpbuf_decl ()
1561 && DECL_SIZE_UNIT (gnu_decl)
1562 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1563 || (flag_stack_check == GENERIC_STACK_CHECK
1564 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1565 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1566 add_stmt_with_node (build_call_n_expr
1567 (update_setjmp_buf_decl, 1,
1568 build_unary_op (ADDR_EXPR, NULL_TREE,
1569 get_block_jmpbuf_decl ())),
1570 gnat_entity);
1572 /* Back-annotate Esize and Alignment of the object if not already
1573 known. Note that we pick the values of the type, not those of
1574 the object, to shield ourselves from low-level platform-dependent
1575 adjustments like alignment promotion. This is both consistent with
1576 all the treatment above, where alignment and size are set on the
1577 type of the object and not on the object directly, and makes it
1578 possible to support all confirming representation clauses. */
1579 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1580 used_by_ref);
1582 break;
1584 case E_Void:
1585 /* Return a TYPE_DECL for "void" that we previously made. */
1586 gnu_decl = TYPE_NAME (void_type_node);
1587 break;
1589 case E_Enumeration_Type:
1590 /* A special case: for the types Character and Wide_Character in
1591 Standard, we do not list all the literals. So if the literals
1592 are not specified, make this an integer type. */
1593 if (No (First_Literal (gnat_entity)))
1595 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1596 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1597 else
1598 gnu_type = make_unsigned_type (esize);
1599 TYPE_NAME (gnu_type) = gnu_entity_name;
1601 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1602 This is needed by the DWARF-2 back-end to distinguish between
1603 unsigned integer types and character types. */
1604 TYPE_STRING_FLAG (gnu_type) = 1;
1606 /* This flag is needed by the call just below. */
1607 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1609 finish_character_type (gnu_type);
1611 else
1613 /* We have a list of enumeral constants in First_Literal. We make a
1614 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1615 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1616 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1617 value of the literal. But when we have a regular boolean type, we
1618 simplify this a little by using a BOOLEAN_TYPE. */
1619 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1620 && !Has_Non_Standard_Rep (gnat_entity);
1621 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1622 tree gnu_list = NULL_TREE;
1623 Entity_Id gnat_literal;
1625 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1626 TYPE_PRECISION (gnu_type) = esize;
1627 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1628 set_min_and_max_values_for_integral_type (gnu_type, esize,
1629 TYPE_SIGN (gnu_type));
1630 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1631 layout_type (gnu_type);
1633 for (gnat_literal = First_Literal (gnat_entity);
1634 Present (gnat_literal);
1635 gnat_literal = Next_Literal (gnat_literal))
1637 tree gnu_value
1638 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1639 /* Do not generate debug info for individual enumerators. */
1640 tree gnu_literal
1641 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1642 gnu_type, gnu_value, true, false, false,
1643 false, false, artificial_p, false,
1644 NULL, gnat_literal);
1645 save_gnu_tree (gnat_literal, gnu_literal, false);
1646 gnu_list
1647 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1650 if (!is_boolean)
1651 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1653 /* Note that the bounds are updated at the end of this function
1654 to avoid an infinite recursion since they refer to the type. */
1655 goto discrete_type;
1657 break;
1659 case E_Signed_Integer_Type:
1660 /* For integer types, just make a signed type the appropriate number
1661 of bits. */
1662 gnu_type = make_signed_type (esize);
1663 goto discrete_type;
1665 case E_Ordinary_Fixed_Point_Type:
1666 case E_Decimal_Fixed_Point_Type:
1668 /* Small_Value is the scale factor. */
1669 const Ureal gnat_small_value = Small_Value (gnat_entity);
1670 tree scale_factor = NULL_TREE;
1672 gnu_type = make_signed_type (esize);
1674 /* Try to decode the scale factor and to save it for the fixed-point
1675 types debug hook. */
1677 /* There are various ways to describe the scale factor, however there
1678 are cases where back-end internals cannot hold it. In such cases,
1679 we output invalid scale factor for such cases (i.e. the 0/0
1680 rational constant) but we expect GNAT to output GNAT encodings,
1681 then. Thus, keep this in sync with
1682 Exp_Dbug.Is_Handled_Scale_Factor. */
1684 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1685 binary or decimal scale: it is easier to read for humans. */
1686 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1687 && (Rbase (gnat_small_value) == 2
1688 || Rbase (gnat_small_value) == 10))
1690 /* Given RM restrictions on 'Small values, we assume here that
1691 the denominator fits in an int. */
1692 const tree base = build_int_cst (integer_type_node,
1693 Rbase (gnat_small_value));
1694 const tree exponent
1695 = build_int_cst (integer_type_node,
1696 UI_To_Int (Denominator (gnat_small_value)));
1697 scale_factor
1698 = build2 (RDIV_EXPR, integer_type_node,
1699 integer_one_node,
1700 build2 (POWER_EXPR, integer_type_node,
1701 base, exponent));
1704 /* Default to arbitrary scale factors descriptions. */
1705 else
1707 const Uint num = Norm_Num (gnat_small_value);
1708 const Uint den = Norm_Den (gnat_small_value);
1710 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1712 const tree gnu_num
1713 = build_int_cst (integer_type_node,
1714 UI_To_Int (Norm_Num (gnat_small_value)));
1715 const tree gnu_den
1716 = build_int_cst (integer_type_node,
1717 UI_To_Int (Norm_Den (gnat_small_value)));
1718 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1719 gnu_num, gnu_den);
1721 else
1722 /* If compiler internals cannot represent arbitrary scale
1723 factors, output an invalid scale factor so that debugger
1724 don't try to handle them but so that we still have a type
1725 in the output. Note that GNAT */
1726 scale_factor = integer_zero_node;
1729 TYPE_FIXED_POINT_P (gnu_type) = 1;
1730 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1732 goto discrete_type;
1734 case E_Modular_Integer_Type:
1736 /* For modular types, make the unsigned type of the proper number
1737 of bits and then set up the modulus, if required. */
1738 tree gnu_modulus, gnu_high = NULL_TREE;
1740 /* Packed Array Impl. Types are supposed to be subtypes only. */
1741 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1743 gnu_type = make_unsigned_type (esize);
1745 /* Get the modulus in this type. If it overflows, assume it is because
1746 it is equal to 2**Esize. Note that there is no overflow checking
1747 done on unsigned type, so we detect the overflow by looking for
1748 a modulus of zero, which is otherwise invalid. */
1749 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1751 if (!integer_zerop (gnu_modulus))
1753 TYPE_MODULAR_P (gnu_type) = 1;
1754 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1755 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1756 build_int_cst (gnu_type, 1));
1759 /* If the upper bound is not maximal, make an extra subtype. */
1760 if (gnu_high
1761 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1763 tree gnu_subtype = make_unsigned_type (esize);
1764 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1765 TREE_TYPE (gnu_subtype) = gnu_type;
1766 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1767 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1768 gnu_type = gnu_subtype;
1771 goto discrete_type;
1773 case E_Signed_Integer_Subtype:
1774 case E_Enumeration_Subtype:
1775 case E_Modular_Integer_Subtype:
1776 case E_Ordinary_Fixed_Point_Subtype:
1777 case E_Decimal_Fixed_Point_Subtype:
1779 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1780 not want to call create_range_type since we would like each subtype
1781 node to be distinct. ??? Historically this was in preparation for
1782 when memory aliasing is implemented, but that's obsolete now given
1783 the call to relate_alias_sets below.
1785 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1786 this fact is used by the arithmetic conversion functions.
1788 We elaborate the Ancestor_Subtype if it is not in the current unit
1789 and one of our bounds is non-static. We do this to ensure consistent
1790 naming in the case where several subtypes share the same bounds, by
1791 elaborating the first such subtype first, thus using its name. */
1793 if (!definition
1794 && Present (Ancestor_Subtype (gnat_entity))
1795 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1796 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1797 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1798 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1800 /* Set the precision to the Esize except for bit-packed arrays. */
1801 if (Is_Packed_Array_Impl_Type (gnat_entity)
1802 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1803 esize = UI_To_Int (RM_Size (gnat_entity));
1805 /* First subtypes of Character are treated as Character; otherwise
1806 this should be an unsigned type if the base type is unsigned or
1807 if the lower bound is constant and non-negative or if the type
1808 is biased. However, even if the lower bound is constant and
1809 non-negative, we use a signed type for a subtype with the same
1810 size as its signed base type, because this eliminates useless
1811 conversions to it and gives more leeway to the optimizer; but
1812 this means that we will need to explicitly test for this case
1813 when we change the representation based on the RM size. */
1814 if (kind == E_Enumeration_Subtype
1815 && No (First_Literal (Etype (gnat_entity)))
1816 && Esize (gnat_entity) == RM_Size (gnat_entity)
1817 && esize == CHAR_TYPE_SIZE
1818 && flag_signed_char)
1819 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1820 else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity)))
1821 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1822 && Is_Unsigned_Type (gnat_entity))
1823 || Has_Biased_Representation (gnat_entity))
1824 gnu_type = make_unsigned_type (esize);
1825 else
1826 gnu_type = make_signed_type (esize);
1827 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1829 SET_TYPE_RM_MIN_VALUE
1830 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1831 gnat_entity, "L", definition, true,
1832 debug_info_p));
1834 SET_TYPE_RM_MAX_VALUE
1835 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1836 gnat_entity, "U", definition, true,
1837 debug_info_p));
1839 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1840 = Has_Biased_Representation (gnat_entity);
1842 /* Do the same processing for Character subtypes as for types. */
1843 if (TYPE_STRING_FLAG (TREE_TYPE (gnu_type)))
1845 TYPE_NAME (gnu_type) = gnu_entity_name;
1846 TYPE_STRING_FLAG (gnu_type) = 1;
1847 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1848 finish_character_type (gnu_type);
1851 /* Inherit our alias set from what we're a subtype of. Subtypes
1852 are not different types and a pointer can designate any instance
1853 within a subtype hierarchy. */
1854 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1856 /* One of the above calls might have caused us to be elaborated,
1857 so don't blow up if so. */
1858 if (present_gnu_tree (gnat_entity))
1860 maybe_present = true;
1861 break;
1864 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1865 TYPE_STUB_DECL (gnu_type)
1866 = create_type_stub_decl (gnu_entity_name, gnu_type);
1868 /* For a packed array, make the original array type a parallel/debug
1869 type. */
1870 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1871 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1873 discrete_type:
1875 /* We have to handle clauses that under-align the type specially. */
1876 if ((Present (Alignment_Clause (gnat_entity))
1877 || (Is_Packed_Array_Impl_Type (gnat_entity)
1878 && Present
1879 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1880 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1882 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1883 if (align >= TYPE_ALIGN (gnu_type))
1884 align = 0;
1887 /* If the type we are dealing with represents a bit-packed array,
1888 we need to have the bits left justified on big-endian targets
1889 and right justified on little-endian targets. We also need to
1890 ensure that when the value is read (e.g. for comparison of two
1891 such values), we only get the good bits, since the unused bits
1892 are uninitialized. Both goals are accomplished by wrapping up
1893 the modular type in an enclosing record type. */
1894 if (Is_Packed_Array_Impl_Type (gnat_entity)
1895 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1897 tree gnu_field_type, gnu_field;
1899 /* Set the RM size before wrapping up the original type. */
1900 SET_TYPE_RM_SIZE (gnu_type,
1901 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1902 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1904 /* Strip the ___XP suffix for standard DWARF. */
1905 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1906 gnu_entity_name = TYPE_NAME (gnu_type);
1908 /* Create a stripped-down declaration, mainly for debugging. */
1909 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1910 gnat_entity);
1912 /* Now save it and build the enclosing record type. */
1913 gnu_field_type = gnu_type;
1915 gnu_type = make_node (RECORD_TYPE);
1916 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1917 TYPE_PACKED (gnu_type) = 1;
1918 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1919 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1920 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1922 /* Propagate the alignment of the modular type to the record type,
1923 unless there is an alignment clause that under-aligns the type.
1924 This means that bit-packed arrays are given "ceil" alignment for
1925 their size by default, which may seem counter-intuitive but makes
1926 it possible to overlay them on modular types easily. */
1927 SET_TYPE_ALIGN (gnu_type,
1928 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1930 /* Propagate the reverse storage order flag to the record type so
1931 that the required byte swapping is performed when retrieving the
1932 enclosed modular value. */
1933 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1934 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1936 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1938 /* Don't declare the field as addressable since we won't be taking
1939 its address and this would prevent create_field_decl from making
1940 a bitfield. */
1941 gnu_field
1942 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1943 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1945 /* We will output additional debug info manually below. */
1946 finish_record_type (gnu_type, gnu_field, 2, false);
1947 compute_record_mode (gnu_type);
1948 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1950 if (debug_info_p)
1952 /* Make the original array type a parallel/debug type. */
1953 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1955 /* Since GNU_TYPE is a padding type around the packed array
1956 implementation type, the padded type is its debug type. */
1957 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1958 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1962 /* If the type we are dealing with has got a smaller alignment than the
1963 natural one, we need to wrap it up in a record type and misalign the
1964 latter; we reuse the padding machinery for this purpose. */
1965 else if (align > 0)
1967 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1969 /* Set the RM size before wrapping the type. */
1970 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
1972 gnu_type
1973 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
1974 gnat_entity, false, true, definition, false);
1976 TYPE_PACKED (gnu_type) = 1;
1977 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
1980 break;
1982 case E_Floating_Point_Type:
1983 /* The type of the Low and High bounds can be our type if this is
1984 a type from Standard, so set them at the end of the function. */
1985 gnu_type = make_node (REAL_TYPE);
1986 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1987 layout_type (gnu_type);
1988 break;
1990 case E_Floating_Point_Subtype:
1991 /* See the E_Signed_Integer_Subtype case for the rationale. */
1992 if (!definition
1993 && Present (Ancestor_Subtype (gnat_entity))
1994 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1995 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1996 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1997 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1999 gnu_type = make_node (REAL_TYPE);
2000 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2001 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2002 TYPE_GCC_MIN_VALUE (gnu_type)
2003 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2004 TYPE_GCC_MAX_VALUE (gnu_type)
2005 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2006 layout_type (gnu_type);
2008 SET_TYPE_RM_MIN_VALUE
2009 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2010 gnat_entity, "L", definition, true,
2011 debug_info_p));
2013 SET_TYPE_RM_MAX_VALUE
2014 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2015 gnat_entity, "U", definition, true,
2016 debug_info_p));
2018 /* Inherit our alias set from what we're a subtype of, as for
2019 integer subtypes. */
2020 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2022 /* One of the above calls might have caused us to be elaborated,
2023 so don't blow up if so. */
2024 maybe_present = true;
2025 break;
2027 /* Array Types and Subtypes
2029 Unconstrained array types are represented by E_Array_Type and
2030 constrained array types are represented by E_Array_Subtype. There
2031 are no actual objects of an unconstrained array type; all we have
2032 are pointers to that type.
2034 The following fields are defined on array types and subtypes:
2036 Component_Type Component type of the array.
2037 Number_Dimensions Number of dimensions (an int).
2038 First_Index Type of first index. */
2040 case E_Array_Type:
2042 const bool convention_fortran_p
2043 = (Convention (gnat_entity) == Convention_Fortran);
2044 const int ndim = Number_Dimensions (gnat_entity);
2045 tree gnu_template_type;
2046 tree gnu_ptr_template;
2047 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2048 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2049 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2050 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2051 Entity_Id gnat_index, gnat_name;
2052 int index;
2053 tree comp_type;
2055 /* Create the type for the component now, as it simplifies breaking
2056 type reference loops. */
2057 comp_type
2058 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2059 if (present_gnu_tree (gnat_entity))
2061 /* As a side effect, the type may have been translated. */
2062 maybe_present = true;
2063 break;
2066 /* We complete an existing dummy fat pointer type in place. This both
2067 avoids further complex adjustments in update_pointer_to and yields
2068 better debugging information in DWARF by leveraging the support for
2069 incomplete declarations of "tagged" types in the DWARF back-end. */
2070 gnu_type = get_dummy_type (gnat_entity);
2071 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2073 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2074 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2075 /* Save the contents of the dummy type for update_pointer_to. */
2076 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2077 gnu_ptr_template =
2078 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2079 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2081 else
2083 gnu_fat_type = make_node (RECORD_TYPE);
2084 gnu_template_type = make_node (RECORD_TYPE);
2085 gnu_ptr_template = build_pointer_type (gnu_template_type);
2088 /* Make a node for the array. If we are not defining the array
2089 suppress expanding incomplete types. */
2090 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2092 if (!definition)
2094 defer_incomplete_level++;
2095 this_deferred = true;
2098 /* Build the fat pointer type. Use a "void *" object instead of
2099 a pointer to the array type since we don't have the array type
2100 yet (it will reference the fat pointer via the bounds). */
2102 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2103 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2104 DECL_CHAIN (tem)
2105 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2106 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2108 if (COMPLETE_TYPE_P (gnu_fat_type))
2110 /* We are going to lay it out again so reset the alias set. */
2111 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2112 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2113 finish_fat_pointer_type (gnu_fat_type, tem);
2114 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2115 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2117 TYPE_FIELDS (t) = tem;
2118 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2121 else
2123 finish_fat_pointer_type (gnu_fat_type, tem);
2124 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2127 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2128 is the fat pointer. This will be used to access the individual
2129 fields once we build them. */
2130 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2131 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2132 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2133 gnu_template_reference
2134 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2135 TREE_READONLY (gnu_template_reference) = 1;
2136 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2138 /* Now create the GCC type for each index and add the fields for that
2139 index to the template. */
2140 for (index = (convention_fortran_p ? ndim - 1 : 0),
2141 gnat_index = First_Index (gnat_entity);
2142 0 <= index && index < ndim;
2143 index += (convention_fortran_p ? - 1 : 1),
2144 gnat_index = Next_Index (gnat_index))
2146 char field_name[16];
2147 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2148 tree gnu_index_base_type
2149 = maybe_character_type (get_base_type (gnu_index_type));
2150 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2151 tree gnu_min, gnu_max, gnu_high;
2153 /* Make the FIELD_DECLs for the low and high bounds of this
2154 type and then make extractions of these fields from the
2155 template. */
2156 sprintf (field_name, "LB%d", index);
2157 gnu_lb_field = create_field_decl (get_identifier (field_name),
2158 gnu_index_base_type,
2159 gnu_template_type, NULL_TREE,
2160 NULL_TREE, 0, 0);
2161 Sloc_to_locus (Sloc (gnat_entity),
2162 &DECL_SOURCE_LOCATION (gnu_lb_field));
2164 field_name[0] = 'U';
2165 gnu_hb_field = create_field_decl (get_identifier (field_name),
2166 gnu_index_base_type,
2167 gnu_template_type, NULL_TREE,
2168 NULL_TREE, 0, 0);
2169 Sloc_to_locus (Sloc (gnat_entity),
2170 &DECL_SOURCE_LOCATION (gnu_hb_field));
2172 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2174 /* We can't use build_component_ref here since the template type
2175 isn't complete yet. */
2176 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2177 gnu_template_reference, gnu_lb_field,
2178 NULL_TREE);
2179 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2180 gnu_template_reference, gnu_hb_field,
2181 NULL_TREE);
2182 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2184 gnu_min = convert (sizetype, gnu_orig_min);
2185 gnu_max = convert (sizetype, gnu_orig_max);
2187 /* Compute the size of this dimension. See the E_Array_Subtype
2188 case below for the rationale. */
2189 gnu_high
2190 = build3 (COND_EXPR, sizetype,
2191 build2 (GE_EXPR, boolean_type_node,
2192 gnu_orig_max, gnu_orig_min),
2193 gnu_max,
2194 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2196 /* Make a range type with the new range in the Ada base type.
2197 Then make an index type with the size range in sizetype. */
2198 gnu_index_types[index]
2199 = create_index_type (gnu_min, gnu_high,
2200 create_range_type (gnu_index_base_type,
2201 gnu_orig_min,
2202 gnu_orig_max),
2203 gnat_entity);
2205 /* Update the maximum size of the array in elements. */
2206 if (gnu_max_size)
2208 tree gnu_min
2209 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2210 tree gnu_max
2211 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2212 tree gnu_this_max
2213 = size_binop (PLUS_EXPR, size_one_node,
2214 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2216 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2217 && TREE_OVERFLOW (gnu_this_max))
2218 gnu_max_size = NULL_TREE;
2219 else
2220 gnu_max_size
2221 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2224 TYPE_NAME (gnu_index_types[index])
2225 = create_concat_name (gnat_entity, field_name);
2228 /* Install all the fields into the template. */
2229 TYPE_NAME (gnu_template_type)
2230 = create_concat_name (gnat_entity, "XUB");
2231 gnu_template_fields = NULL_TREE;
2232 for (index = 0; index < ndim; index++)
2233 gnu_template_fields
2234 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2235 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2236 debug_info_p);
2237 TYPE_READONLY (gnu_template_type) = 1;
2239 /* If Component_Size is not already specified, annotate it with the
2240 size of the component. */
2241 if (Unknown_Component_Size (gnat_entity))
2242 Set_Component_Size (gnat_entity,
2243 annotate_value (TYPE_SIZE (comp_type)));
2245 /* Compute the maximum size of the array in units and bits. */
2246 if (gnu_max_size)
2248 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2249 TYPE_SIZE_UNIT (comp_type));
2250 gnu_max_size = size_binop (MULT_EXPR,
2251 convert (bitsizetype, gnu_max_size),
2252 TYPE_SIZE (comp_type));
2254 else
2255 gnu_max_size_unit = NULL_TREE;
2257 /* Now build the array type. */
2258 tem = comp_type;
2259 for (index = ndim - 1; index >= 0; index--)
2261 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2262 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2263 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2264 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2265 set_reverse_storage_order_on_array_type (tem);
2266 if (array_type_has_nonaliased_component (tem, gnat_entity))
2267 set_nonaliased_component_on_array_type (tem);
2270 /* If an alignment is specified, use it if valid. But ignore it
2271 for the original type of packed array types. If the alignment
2272 was requested with an explicit alignment clause, state so. */
2273 if (No (Packed_Array_Impl_Type (gnat_entity))
2274 && Known_Alignment (gnat_entity))
2276 SET_TYPE_ALIGN (tem,
2277 validate_alignment (Alignment (gnat_entity),
2278 gnat_entity,
2279 TYPE_ALIGN (tem)));
2280 if (Present (Alignment_Clause (gnat_entity)))
2281 TYPE_USER_ALIGN (tem) = 1;
2284 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2285 implementation types as such so that the debug information back-end
2286 can output the appropriate description for them. */
2287 TYPE_PACKED (tem)
2288 = (Is_Packed (gnat_entity)
2289 || Is_Packed_Array_Impl_Type (gnat_entity));
2291 if (Treat_As_Volatile (gnat_entity))
2292 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2294 /* Adjust the type of the pointer-to-array field of the fat pointer
2295 and record the aliasing relationships if necessary. */
2296 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2297 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2298 record_component_aliases (gnu_fat_type);
2300 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2301 corresponding fat pointer. */
2302 TREE_TYPE (gnu_type) = gnu_fat_type;
2303 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2304 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2305 SET_TYPE_MODE (gnu_type, BLKmode);
2306 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2308 /* If the maximum size doesn't overflow, use it. */
2309 if (gnu_max_size
2310 && TREE_CODE (gnu_max_size) == INTEGER_CST
2311 && !TREE_OVERFLOW (gnu_max_size)
2312 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2313 && !TREE_OVERFLOW (gnu_max_size_unit))
2315 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2316 TYPE_SIZE (tem));
2317 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2318 TYPE_SIZE_UNIT (tem));
2321 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2322 artificial_p, debug_info_p, gnat_entity);
2324 /* If told to generate GNAT encodings for them (GDB rely on them at the
2325 moment): give the fat pointer type a name. If this is a packed
2326 array, tell the debugger how to interpret the underlying bits. */
2327 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2328 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2329 else
2330 gnat_name = gnat_entity;
2331 tree xup_name
2332 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2333 ? get_entity_name (gnat_name)
2334 : create_concat_name (gnat_name, "XUP");
2335 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2336 gnat_entity);
2338 /* Create the type to be designated by thin pointers: a record type for
2339 the array and its template. We used to shift the fields to have the
2340 template at a negative offset, but this was somewhat of a kludge; we
2341 now shift thin pointer values explicitly but only those which have a
2342 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2343 Note that GDB can handle standard DWARF information for them, so we
2344 don't have to name them as a GNAT encoding, except if specifically
2345 asked to. */
2346 tree xut_name
2347 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2348 ? get_entity_name (gnat_name)
2349 : create_concat_name (gnat_name, "XUT");
2350 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2351 debug_info_p);
2353 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2354 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2356 break;
2358 case E_Array_Subtype:
2360 /* This is the actual data type for array variables. Multidimensional
2361 arrays are implemented as arrays of arrays. Note that arrays which
2362 have sparse enumeration subtypes as index components create sparse
2363 arrays, which is obviously space inefficient but so much easier to
2364 code for now.
2366 Also note that the subtype never refers to the unconstrained array
2367 type, which is somewhat at variance with Ada semantics.
2369 First check to see if this is simply a renaming of the array type.
2370 If so, the result is the array type. */
2372 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2373 if (!Is_Constrained (gnat_entity))
2375 else
2377 Entity_Id gnat_index, gnat_base_index;
2378 const bool convention_fortran_p
2379 = (Convention (gnat_entity) == Convention_Fortran);
2380 const int ndim = Number_Dimensions (gnat_entity);
2381 tree gnu_base_type = gnu_type;
2382 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2383 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2384 bool need_index_type_struct = false;
2385 int index;
2387 /* First create the GCC type for each index and find out whether
2388 special types are needed for debugging information. */
2389 for (index = (convention_fortran_p ? ndim - 1 : 0),
2390 gnat_index = First_Index (gnat_entity),
2391 gnat_base_index
2392 = First_Index (Implementation_Base_Type (gnat_entity));
2393 0 <= index && index < ndim;
2394 index += (convention_fortran_p ? - 1 : 1),
2395 gnat_index = Next_Index (gnat_index),
2396 gnat_base_index = Next_Index (gnat_base_index))
2398 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2399 tree gnu_index_base_type
2400 = maybe_character_type (get_base_type (gnu_index_type));
2401 tree gnu_orig_min
2402 = convert (gnu_index_base_type,
2403 TYPE_MIN_VALUE (gnu_index_type));
2404 tree gnu_orig_max
2405 = convert (gnu_index_base_type,
2406 TYPE_MAX_VALUE (gnu_index_type));
2407 tree gnu_min = convert (sizetype, gnu_orig_min);
2408 tree gnu_max = convert (sizetype, gnu_orig_max);
2409 tree gnu_base_index_type
2410 = get_unpadded_type (Etype (gnat_base_index));
2411 tree gnu_base_index_base_type
2412 = maybe_character_type (get_base_type (gnu_base_index_type));
2413 tree gnu_base_orig_min
2414 = convert (gnu_base_index_base_type,
2415 TYPE_MIN_VALUE (gnu_base_index_type));
2416 tree gnu_base_orig_max
2417 = convert (gnu_base_index_base_type,
2418 TYPE_MAX_VALUE (gnu_base_index_type));
2419 tree gnu_high;
2421 /* See if the base array type is already flat. If it is, we
2422 are probably compiling an ACATS test but it will cause the
2423 code below to malfunction if we don't handle it specially. */
2424 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2425 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2426 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2428 gnu_min = size_one_node;
2429 gnu_max = size_zero_node;
2430 gnu_high = gnu_max;
2433 /* Similarly, if one of the values overflows in sizetype and the
2434 range is null, use 1..0 for the sizetype bounds. */
2435 else if (TREE_CODE (gnu_min) == INTEGER_CST
2436 && TREE_CODE (gnu_max) == INTEGER_CST
2437 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2438 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2440 gnu_min = size_one_node;
2441 gnu_max = size_zero_node;
2442 gnu_high = gnu_max;
2445 /* If the minimum and maximum values both overflow in sizetype,
2446 but the difference in the original type does not overflow in
2447 sizetype, ignore the overflow indication. */
2448 else if (TREE_CODE (gnu_min) == INTEGER_CST
2449 && TREE_CODE (gnu_max) == INTEGER_CST
2450 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2451 && !TREE_OVERFLOW
2452 (convert (sizetype,
2453 fold_build2 (MINUS_EXPR, gnu_index_type,
2454 gnu_orig_max,
2455 gnu_orig_min))))
2457 TREE_OVERFLOW (gnu_min) = 0;
2458 TREE_OVERFLOW (gnu_max) = 0;
2459 gnu_high = gnu_max;
2462 /* Compute the size of this dimension in the general case. We
2463 need to provide GCC with an upper bound to use but have to
2464 deal with the "superflat" case. There are three ways to do
2465 this. If we can prove that the array can never be superflat,
2466 we can just use the high bound of the index type. */
2467 else if ((Nkind (gnat_index) == N_Range
2468 && cannot_be_superflat (gnat_index))
2469 /* Bit-Packed Array Impl. Types are never superflat. */
2470 || (Is_Packed_Array_Impl_Type (gnat_entity)
2471 && Is_Bit_Packed_Array
2472 (Original_Array_Type (gnat_entity))))
2473 gnu_high = gnu_max;
2475 /* Otherwise, if the high bound is constant but the low bound is
2476 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2477 lower bound. Note that the comparison must be done in the
2478 original type to avoid any overflow during the conversion. */
2479 else if (TREE_CODE (gnu_max) == INTEGER_CST
2480 && TREE_CODE (gnu_min) != INTEGER_CST)
2482 gnu_high = gnu_max;
2483 gnu_min
2484 = build_cond_expr (sizetype,
2485 build_binary_op (GE_EXPR,
2486 boolean_type_node,
2487 gnu_orig_max,
2488 gnu_orig_min),
2489 gnu_min,
2490 int_const_binop (PLUS_EXPR, gnu_max,
2491 size_one_node));
2494 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2495 in all the other cases. Note that, here as well as above,
2496 the condition used in the comparison must be equivalent to
2497 the condition (length != 0). This is relied upon in order
2498 to optimize array comparisons in compare_arrays. Moreover
2499 we use int_const_binop for the shift by 1 if the bound is
2500 constant to avoid any unwanted overflow. */
2501 else
2502 gnu_high
2503 = build_cond_expr (sizetype,
2504 build_binary_op (GE_EXPR,
2505 boolean_type_node,
2506 gnu_orig_max,
2507 gnu_orig_min),
2508 gnu_max,
2509 TREE_CODE (gnu_min) == INTEGER_CST
2510 ? int_const_binop (MINUS_EXPR, gnu_min,
2511 size_one_node)
2512 : size_binop (MINUS_EXPR, gnu_min,
2513 size_one_node));
2515 /* Reuse the index type for the range type. Then make an index
2516 type with the size range in sizetype. */
2517 gnu_index_types[index]
2518 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2519 gnat_entity);
2521 /* Update the maximum size of the array in elements. Here we
2522 see if any constraint on the index type of the base type
2523 can be used in the case of self-referential bound on the
2524 index type of the subtype. We look for a non-"infinite"
2525 and non-self-referential bound from any type involved and
2526 handle each bound separately. */
2527 if (gnu_max_size)
2529 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2530 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2531 tree gnu_base_base_min
2532 = convert (sizetype,
2533 TYPE_MIN_VALUE (gnu_base_index_base_type));
2534 tree gnu_base_base_max
2535 = convert (sizetype,
2536 TYPE_MAX_VALUE (gnu_base_index_base_type));
2538 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2539 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2540 && !TREE_OVERFLOW (gnu_base_min)))
2541 gnu_base_min = gnu_min;
2543 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2544 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2545 && !TREE_OVERFLOW (gnu_base_max)))
2546 gnu_base_max = gnu_max;
2548 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2549 && TREE_OVERFLOW (gnu_base_min))
2550 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2551 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2552 && TREE_OVERFLOW (gnu_base_max))
2553 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2554 gnu_max_size = NULL_TREE;
2555 else
2557 tree gnu_this_max;
2559 /* Use int_const_binop if the bounds are constant to
2560 avoid any unwanted overflow. */
2561 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2562 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2563 gnu_this_max
2564 = int_const_binop (PLUS_EXPR, size_one_node,
2565 int_const_binop (MINUS_EXPR,
2566 gnu_base_max,
2567 gnu_base_min));
2568 else
2569 gnu_this_max
2570 = size_binop (PLUS_EXPR, size_one_node,
2571 size_binop (MINUS_EXPR,
2572 gnu_base_max,
2573 gnu_base_min));
2575 gnu_max_size
2576 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2580 /* We need special types for debugging information to point to
2581 the index types if they have variable bounds, are not integer
2582 types, are biased or are wider than sizetype. These are GNAT
2583 encodings, so we have to include them only when all encodings
2584 are requested. */
2585 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2586 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2587 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2588 || (TREE_TYPE (gnu_index_type)
2589 && TREE_CODE (TREE_TYPE (gnu_index_type))
2590 != INTEGER_TYPE)
2591 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2592 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2593 need_index_type_struct = true;
2596 /* Then flatten: create the array of arrays. For an array type
2597 used to implement a packed array, get the component type from
2598 the original array type since the representation clauses that
2599 can affect it are on the latter. */
2600 if (Is_Packed_Array_Impl_Type (gnat_entity)
2601 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2603 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2604 for (index = ndim - 1; index >= 0; index--)
2605 gnu_type = TREE_TYPE (gnu_type);
2607 /* One of the above calls might have caused us to be elaborated,
2608 so don't blow up if so. */
2609 if (present_gnu_tree (gnat_entity))
2611 maybe_present = true;
2612 break;
2615 else
2617 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2618 debug_info_p);
2620 /* One of the above calls might have caused us to be elaborated,
2621 so don't blow up if so. */
2622 if (present_gnu_tree (gnat_entity))
2624 maybe_present = true;
2625 break;
2629 /* Compute the maximum size of the array in units and bits. */
2630 if (gnu_max_size)
2632 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2633 TYPE_SIZE_UNIT (gnu_type));
2634 gnu_max_size = size_binop (MULT_EXPR,
2635 convert (bitsizetype, gnu_max_size),
2636 TYPE_SIZE (gnu_type));
2638 else
2639 gnu_max_size_unit = NULL_TREE;
2641 /* Now build the array type. */
2642 for (index = ndim - 1; index >= 0; index --)
2644 gnu_type = build_nonshared_array_type (gnu_type,
2645 gnu_index_types[index]);
2646 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2647 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2648 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2649 set_reverse_storage_order_on_array_type (gnu_type);
2650 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2651 set_nonaliased_component_on_array_type (gnu_type);
2654 /* Strip the ___XP suffix for standard DWARF. */
2655 if (Is_Packed_Array_Impl_Type (gnat_entity)
2656 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2658 Entity_Id gnat_original_array_type
2659 = Underlying_Type (Original_Array_Type (gnat_entity));
2661 gnu_entity_name
2662 = get_entity_name (gnat_original_array_type);
2665 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2666 TYPE_STUB_DECL (gnu_type)
2667 = create_type_stub_decl (gnu_entity_name, gnu_type);
2669 /* If this is a multi-dimensional array and we are at global level,
2670 we need to make a variable corresponding to the stride of the
2671 inner dimensions. */
2672 if (ndim > 1 && global_bindings_p ())
2674 tree gnu_arr_type;
2676 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2677 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2678 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2680 tree eltype = TREE_TYPE (gnu_arr_type);
2681 char stride_name[32];
2683 sprintf (stride_name, "ST%d", index);
2684 TYPE_SIZE (gnu_arr_type)
2685 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2686 gnat_entity, stride_name,
2687 definition, false);
2689 /* ??? For now, store the size as a multiple of the
2690 alignment of the element type in bytes so that we
2691 can see the alignment from the tree. */
2692 sprintf (stride_name, "ST%d_A_UNIT", index);
2693 TYPE_SIZE_UNIT (gnu_arr_type)
2694 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2695 gnat_entity, stride_name,
2696 definition, false,
2697 TYPE_ALIGN (eltype));
2699 /* ??? create_type_decl is not invoked on the inner types so
2700 the MULT_EXPR node built above will never be marked. */
2701 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2705 /* If we need to write out a record type giving the names of the
2706 bounds for debugging purposes, do it now and make the record
2707 type a parallel type. This is not needed for a packed array
2708 since the bounds are conveyed by the original array type. */
2709 if (need_index_type_struct
2710 && debug_info_p
2711 && !Is_Packed_Array_Impl_Type (gnat_entity))
2713 tree gnu_bound_rec = make_node (RECORD_TYPE);
2714 tree gnu_field_list = NULL_TREE;
2715 tree gnu_field;
2717 TYPE_NAME (gnu_bound_rec)
2718 = create_concat_name (gnat_entity, "XA");
2720 for (index = ndim - 1; index >= 0; index--)
2722 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2723 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2725 /* Make sure to reference the types themselves, and not just
2726 their names, as the debugger may fall back on them. */
2727 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2728 gnu_bound_rec, NULL_TREE,
2729 NULL_TREE, 0, 0);
2730 DECL_CHAIN (gnu_field) = gnu_field_list;
2731 gnu_field_list = gnu_field;
2734 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2735 add_parallel_type (gnu_type, gnu_bound_rec);
2738 /* If this is a packed array type, make the original array type a
2739 parallel/debug type. Otherwise, if such GNAT encodings are
2740 required, do it for the base array type if it isn't artificial to
2741 make sure it is kept in the debug info. */
2742 if (debug_info_p)
2744 if (Is_Packed_Array_Impl_Type (gnat_entity))
2745 associate_original_type_to_packed_array (gnu_type,
2746 gnat_entity);
2747 else
2749 tree gnu_base_decl
2750 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2751 false);
2752 if (!DECL_ARTIFICIAL (gnu_base_decl)
2753 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2754 add_parallel_type (gnu_type,
2755 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2759 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2760 = (Is_Packed_Array_Impl_Type (gnat_entity)
2761 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2763 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2764 implementation types as such so that the debug information back-end
2765 can output the appropriate description for them. */
2766 TYPE_PACKED (gnu_type)
2767 = (Is_Packed (gnat_entity)
2768 || Is_Packed_Array_Impl_Type (gnat_entity));
2770 /* If the size is self-referential and the maximum size doesn't
2771 overflow, use it. */
2772 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2773 && gnu_max_size
2774 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2775 && TREE_OVERFLOW (gnu_max_size))
2776 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2777 && TREE_OVERFLOW (gnu_max_size_unit)))
2779 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2780 TYPE_SIZE (gnu_type));
2781 TYPE_SIZE_UNIT (gnu_type)
2782 = size_binop (MIN_EXPR, gnu_max_size_unit,
2783 TYPE_SIZE_UNIT (gnu_type));
2786 /* Set our alias set to that of our base type. This gives all
2787 array subtypes the same alias set. */
2788 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2790 /* If this is a packed type, make this type the same as the packed
2791 array type, but do some adjusting in the type first. */
2792 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2794 Entity_Id gnat_index;
2795 tree gnu_inner;
2797 /* First finish the type we had been making so that we output
2798 debugging information for it. */
2799 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2800 if (Treat_As_Volatile (gnat_entity))
2802 const int quals
2803 = TYPE_QUAL_VOLATILE
2804 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2805 gnu_type = change_qualified_type (gnu_type, quals);
2807 /* Make it artificial only if the base type was artificial too.
2808 That's sort of "morally" true and will make it possible for
2809 the debugger to look it up by name in DWARF, which is needed
2810 in order to decode the packed array type. */
2811 gnu_decl
2812 = create_type_decl (gnu_entity_name, gnu_type,
2813 !Comes_From_Source (Etype (gnat_entity))
2814 && artificial_p, debug_info_p,
2815 gnat_entity);
2817 /* Save it as our equivalent in case the call below elaborates
2818 this type again. */
2819 save_gnu_tree (gnat_entity, gnu_decl, false);
2821 gnu_decl
2822 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2823 NULL_TREE, false);
2824 this_made_decl = true;
2825 gnu_type = TREE_TYPE (gnu_decl);
2826 save_gnu_tree (gnat_entity, NULL_TREE, false);
2827 save_gnu_tree (gnat_entity, gnu_decl, false);
2828 saved = true;
2830 gnu_inner = gnu_type;
2831 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2832 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2833 || TYPE_PADDING_P (gnu_inner)))
2834 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2836 /* We need to attach the index type to the type we just made so
2837 that the actual bounds can later be put into a template. */
2838 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2839 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2840 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2841 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2843 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2845 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2846 TYPE_MODULUS for modular types so we make an extra
2847 subtype if necessary. */
2848 if (TYPE_MODULAR_P (gnu_inner))
2850 tree gnu_subtype
2851 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2852 TREE_TYPE (gnu_subtype) = gnu_inner;
2853 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2854 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2855 TYPE_MIN_VALUE (gnu_inner));
2856 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2857 TYPE_MAX_VALUE (gnu_inner));
2858 gnu_inner = gnu_subtype;
2861 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2863 /* Check for other cases of overloading. */
2864 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2867 for (gnat_index = First_Index (gnat_entity);
2868 Present (gnat_index);
2869 gnat_index = Next_Index (gnat_index))
2870 SET_TYPE_ACTUAL_BOUNDS
2871 (gnu_inner,
2872 tree_cons (NULL_TREE,
2873 get_unpadded_type (Etype (gnat_index)),
2874 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2876 if (Convention (gnat_entity) != Convention_Fortran)
2877 SET_TYPE_ACTUAL_BOUNDS
2878 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2880 if (TREE_CODE (gnu_type) == RECORD_TYPE
2881 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2882 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2886 break;
2888 case E_String_Literal_Subtype:
2889 /* Create the type for a string literal. */
2891 Entity_Id gnat_full_type
2892 = (Is_Private_Type (Etype (gnat_entity))
2893 && Present (Full_View (Etype (gnat_entity)))
2894 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2895 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2896 tree gnu_string_array_type
2897 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2898 tree gnu_string_index_type
2899 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2900 (TYPE_DOMAIN (gnu_string_array_type))));
2901 tree gnu_lower_bound
2902 = convert (gnu_string_index_type,
2903 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2904 tree gnu_length
2905 = UI_To_gnu (String_Literal_Length (gnat_entity),
2906 gnu_string_index_type);
2907 tree gnu_upper_bound
2908 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2909 gnu_lower_bound,
2910 int_const_binop (MINUS_EXPR, gnu_length,
2911 convert (gnu_string_index_type,
2912 integer_one_node)));
2913 tree gnu_index_type
2914 = create_index_type (convert (sizetype, gnu_lower_bound),
2915 convert (sizetype, gnu_upper_bound),
2916 create_range_type (gnu_string_index_type,
2917 gnu_lower_bound,
2918 gnu_upper_bound),
2919 gnat_entity);
2921 gnu_type
2922 = build_nonshared_array_type (gnat_to_gnu_type
2923 (Component_Type (gnat_entity)),
2924 gnu_index_type);
2925 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2926 set_nonaliased_component_on_array_type (gnu_type);
2927 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2929 break;
2931 /* Record Types and Subtypes
2933 The following fields are defined on record types:
2935 Has_Discriminants True if the record has discriminants
2936 First_Discriminant Points to head of list of discriminants
2937 First_Entity Points to head of list of fields
2938 Is_Tagged_Type True if the record is tagged
2940 Implementation of Ada records and discriminated records:
2942 A record type definition is transformed into the equivalent of a C
2943 struct definition. The fields that are the discriminants which are
2944 found in the Full_Type_Declaration node and the elements of the
2945 Component_List found in the Record_Type_Definition node. The
2946 Component_List can be a recursive structure since each Variant of
2947 the Variant_Part of the Component_List has a Component_List.
2949 Processing of a record type definition comprises starting the list of
2950 field declarations here from the discriminants and the calling the
2951 function components_to_record to add the rest of the fields from the
2952 component list and return the gnu type node. The function
2953 components_to_record will call itself recursively as it traverses
2954 the tree. */
2956 case E_Record_Type:
2957 if (Has_Complex_Representation (gnat_entity))
2959 gnu_type
2960 = build_complex_type
2961 (get_unpadded_type
2962 (Etype (Defining_Entity
2963 (First (Component_Items
2964 (Component_List
2965 (Type_Definition
2966 (Declaration_Node (gnat_entity)))))))));
2968 break;
2972 Node_Id full_definition = Declaration_Node (gnat_entity);
2973 Node_Id record_definition = Type_Definition (full_definition);
2974 Node_Id gnat_constr;
2975 Entity_Id gnat_field, gnat_parent_type;
2976 tree gnu_field, gnu_field_list = NULL_TREE;
2977 tree gnu_get_parent;
2978 /* Set PACKED in keeping with gnat_to_gnu_field. */
2979 const int packed
2980 = Is_Packed (gnat_entity)
2982 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2983 ? -1
2984 : 0;
2985 const bool has_align = Known_Alignment (gnat_entity);
2986 const bool has_discr = Has_Discriminants (gnat_entity);
2987 const bool has_rep = Has_Specified_Layout (gnat_entity);
2988 const bool is_extension
2989 = (Is_Tagged_Type (gnat_entity)
2990 && Nkind (record_definition) == N_Derived_Type_Definition);
2991 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2992 bool all_rep = has_rep;
2994 /* See if all fields have a rep clause. Stop when we find one
2995 that doesn't. */
2996 if (all_rep)
2997 for (gnat_field = First_Entity (gnat_entity);
2998 Present (gnat_field);
2999 gnat_field = Next_Entity (gnat_field))
3000 if ((Ekind (gnat_field) == E_Component
3001 || Ekind (gnat_field) == E_Discriminant)
3002 && No (Component_Clause (gnat_field)))
3004 all_rep = false;
3005 break;
3008 /* If this is a record extension, go a level further to find the
3009 record definition. Also, verify we have a Parent_Subtype. */
3010 if (is_extension)
3012 if (!type_annotate_only
3013 || Present (Record_Extension_Part (record_definition)))
3014 record_definition = Record_Extension_Part (record_definition);
3016 gcc_assert (type_annotate_only
3017 || Present (Parent_Subtype (gnat_entity)));
3020 /* Make a node for the record. If we are not defining the record,
3021 suppress expanding incomplete types. */
3022 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3023 TYPE_NAME (gnu_type) = gnu_entity_name;
3024 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3025 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3026 = Reverse_Storage_Order (gnat_entity);
3027 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3029 if (!definition)
3031 defer_incomplete_level++;
3032 this_deferred = true;
3035 /* If both a size and rep clause were specified, put the size on
3036 the record type now so that it can get the proper layout. */
3037 if (has_rep && Known_RM_Size (gnat_entity))
3038 TYPE_SIZE (gnu_type)
3039 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3041 /* Always set the alignment on the record type here so that it can
3042 get the proper layout. */
3043 if (has_align)
3044 SET_TYPE_ALIGN (gnu_type,
3045 validate_alignment (Alignment (gnat_entity),
3046 gnat_entity, 0));
3047 else
3049 SET_TYPE_ALIGN (gnu_type, 0);
3051 /* If a type needs strict alignment, the minimum size will be the
3052 type size instead of the RM size (see validate_size). Cap the
3053 alignment lest it causes this type size to become too large. */
3054 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3056 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3057 unsigned int max_align = max_size & -max_size;
3058 if (max_align < BIGGEST_ALIGNMENT)
3059 TYPE_MAX_ALIGN (gnu_type) = max_align;
3063 /* If we have a Parent_Subtype, make a field for the parent. If
3064 this record has rep clauses, force the position to zero. */
3065 if (Present (Parent_Subtype (gnat_entity)))
3067 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3068 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3069 tree gnu_parent;
3070 int parent_packed = 0;
3072 /* A major complexity here is that the parent subtype will
3073 reference our discriminants in its Stored_Constraint list.
3074 But those must reference the parent component of this record
3075 which is precisely of the parent subtype we have not built yet!
3076 To break the circle we first build a dummy COMPONENT_REF which
3077 represents the "get to the parent" operation and initialize
3078 each of those discriminants to a COMPONENT_REF of the above
3079 dummy parent referencing the corresponding discriminant of the
3080 base type of the parent subtype. */
3081 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3082 build0 (PLACEHOLDER_EXPR, gnu_type),
3083 build_decl (input_location,
3084 FIELD_DECL, NULL_TREE,
3085 gnu_dummy_parent_type),
3086 NULL_TREE);
3088 if (has_discr)
3089 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3090 Present (gnat_field);
3091 gnat_field = Next_Stored_Discriminant (gnat_field))
3092 if (Present (Corresponding_Discriminant (gnat_field)))
3094 tree gnu_field
3095 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3096 (gnat_field));
3097 save_gnu_tree
3098 (gnat_field,
3099 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3100 gnu_get_parent, gnu_field, NULL_TREE),
3101 true);
3104 /* Then we build the parent subtype. If it has discriminants but
3105 the type itself has unknown discriminants, this means that it
3106 doesn't contain information about how the discriminants are
3107 derived from those of the ancestor type, so it cannot be used
3108 directly. Instead it is built by cloning the parent subtype
3109 of the underlying record view of the type, for which the above
3110 derivation of discriminants has been made explicit. */
3111 if (Has_Discriminants (gnat_parent)
3112 && Has_Unknown_Discriminants (gnat_entity))
3114 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3116 /* If we are defining the type, the underlying record
3117 view must already have been elaborated at this point.
3118 Otherwise do it now as its parent subtype cannot be
3119 technically elaborated on its own. */
3120 if (definition)
3121 gcc_assert (present_gnu_tree (gnat_uview));
3122 else
3123 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3125 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3127 /* Substitute the "get to the parent" of the type for that
3128 of its underlying record view in the cloned type. */
3129 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3130 Present (gnat_field);
3131 gnat_field = Next_Stored_Discriminant (gnat_field))
3132 if (Present (Corresponding_Discriminant (gnat_field)))
3134 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3135 tree gnu_ref
3136 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3137 gnu_get_parent, gnu_field, NULL_TREE);
3138 gnu_parent
3139 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3142 else
3143 gnu_parent = gnat_to_gnu_type (gnat_parent);
3145 /* The parent field needs strict alignment so, if it is to
3146 be created with a component clause below, then we need
3147 to apply the same adjustment as in gnat_to_gnu_field. */
3148 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3150 /* ??? For historical reasons, we do it on strict-alignment
3151 platforms only, where it is really required. This means
3152 that a confirming representation clause will change the
3153 behavior of the compiler on the other platforms. */
3154 if (STRICT_ALIGNMENT)
3155 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3156 else
3157 parent_packed
3158 = adjust_packed (gnu_parent, gnu_type, parent_packed);
3161 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3162 initially built. The discriminants must reference the fields
3163 of the parent subtype and not those of its base type for the
3164 placeholder machinery to properly work. */
3165 if (has_discr)
3167 /* The actual parent subtype is the full view. */
3168 if (Is_Private_Type (gnat_parent))
3170 if (Present (Full_View (gnat_parent)))
3171 gnat_parent = Full_View (gnat_parent);
3172 else
3173 gnat_parent = Underlying_Full_View (gnat_parent);
3176 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3177 Present (gnat_field);
3178 gnat_field = Next_Stored_Discriminant (gnat_field))
3179 if (Present (Corresponding_Discriminant (gnat_field)))
3181 Entity_Id field;
3182 for (field = First_Stored_Discriminant (gnat_parent);
3183 Present (field);
3184 field = Next_Stored_Discriminant (field))
3185 if (same_discriminant_p (gnat_field, field))
3186 break;
3187 gcc_assert (Present (field));
3188 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3189 = gnat_to_gnu_field_decl (field);
3193 /* The "get to the parent" COMPONENT_REF must be given its
3194 proper type... */
3195 TREE_TYPE (gnu_get_parent) = gnu_parent;
3197 /* ...and reference the _Parent field of this record. */
3198 gnu_field
3199 = create_field_decl (parent_name_id,
3200 gnu_parent, gnu_type,
3201 has_rep
3202 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3203 has_rep
3204 ? bitsize_zero_node : NULL_TREE,
3205 parent_packed, 1);
3206 DECL_INTERNAL_P (gnu_field) = 1;
3207 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3208 TYPE_FIELDS (gnu_type) = gnu_field;
3211 /* Make the fields for the discriminants and put them into the record
3212 unless it's an Unchecked_Union. */
3213 if (has_discr)
3214 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3215 Present (gnat_field);
3216 gnat_field = Next_Stored_Discriminant (gnat_field))
3218 /* If this is a record extension and this discriminant is the
3219 renaming of another discriminant, we've handled it above. */
3220 if (is_extension
3221 && Present (Corresponding_Discriminant (gnat_field)))
3222 continue;
3224 gnu_field
3225 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3226 debug_info_p);
3228 /* Make an expression using a PLACEHOLDER_EXPR from the
3229 FIELD_DECL node just created and link that with the
3230 corresponding GNAT defining identifier. */
3231 save_gnu_tree (gnat_field,
3232 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3233 build0 (PLACEHOLDER_EXPR, gnu_type),
3234 gnu_field, NULL_TREE),
3235 true);
3237 if (!is_unchecked_union)
3239 DECL_CHAIN (gnu_field) = gnu_field_list;
3240 gnu_field_list = gnu_field;
3244 /* If we have a derived untagged type that renames discriminants in
3245 the parent type, the (stored) discriminants are just a copy of the
3246 discriminants of the parent type. This means that any constraints
3247 added by the renaming in the derivation are disregarded as far as
3248 the layout of the derived type is concerned. To rescue them, we
3249 change the type of the (stored) discriminants to a subtype with
3250 the bounds of the type of the visible discriminants. */
3251 if (has_discr
3252 && !is_extension
3253 && Stored_Constraint (gnat_entity) != No_Elist)
3254 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3255 gnat_constr != No_Elmt;
3256 gnat_constr = Next_Elmt (gnat_constr))
3257 if (Nkind (Node (gnat_constr)) == N_Identifier
3258 /* Ignore access discriminants. */
3259 && !Is_Access_Type (Etype (Node (gnat_constr)))
3260 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3262 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3263 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3264 tree gnu_ref
3265 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3266 NULL_TREE, false);
3268 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3269 just above for one of the stored discriminants. */
3270 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3272 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3274 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3275 tree gnu_subtype
3276 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3277 ? make_unsigned_type (prec) : make_signed_type (prec);
3278 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3279 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3280 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3281 TYPE_MIN_VALUE (gnu_discr_type));
3282 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3283 TYPE_MAX_VALUE (gnu_discr_type));
3284 TREE_TYPE (gnu_ref)
3285 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3289 /* If this is a derived type with discriminants and these discriminants
3290 affect the initial shape it has inherited, factor them in. */
3291 if (has_discr
3292 && !is_extension
3293 && !Has_Record_Rep_Clause (gnat_entity)
3294 && Stored_Constraint (gnat_entity) != No_Elist
3295 && (gnat_parent_type = Underlying_Type (Etype (gnat_entity)))
3296 && Is_Record_Type (gnat_parent_type)
3297 && Is_Unchecked_Union (gnat_entity)
3298 == Is_Unchecked_Union (gnat_parent_type)
3299 && No_Reordering (gnat_entity) == No_Reordering (gnat_parent_type))
3301 tree gnu_parent_type
3302 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type));
3304 if (TYPE_IS_PADDING_P (gnu_parent_type))
3305 gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type));
3307 vec<subst_pair> gnu_subst_list
3308 = build_subst_list (gnat_entity, gnat_parent_type, definition);
3310 /* Set the layout of the type to match that of the parent type,
3311 doing required substitutions. If we are in minimal GNAT
3312 encodings mode, we don't need debug info for the inner record
3313 types, as they will be part of the embedding variant record's
3314 debug info. */
3315 copy_and_substitute_in_layout
3316 (gnat_entity, gnat_parent_type, gnu_type, gnu_parent_type,
3317 gnu_subst_list,
3318 debug_info_p && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL);
3320 else
3322 /* Add the fields into the record type and finish it up. */
3323 components_to_record (Component_List (record_definition),
3324 gnat_entity, gnu_field_list, gnu_type,
3325 packed, definition, false, all_rep,
3326 is_unchecked_union, artificial_p,
3327 debug_info_p, false,
3328 all_rep ? NULL_TREE : bitsize_zero_node,
3329 NULL);
3331 /* If there are entities in the chain corresponding to components
3332 that we did not elaborate, ensure we elaborate their types if
3333 they are Itypes. */
3334 for (gnat_temp = First_Entity (gnat_entity);
3335 Present (gnat_temp);
3336 gnat_temp = Next_Entity (gnat_temp))
3337 if ((Ekind (gnat_temp) == E_Component
3338 || Ekind (gnat_temp) == E_Discriminant)
3339 && Is_Itype (Etype (gnat_temp))
3340 && !present_gnu_tree (gnat_temp))
3341 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3344 /* Fill in locations of fields. */
3345 annotate_rep (gnat_entity, gnu_type);
3347 /* If this is a record type associated with an exception definition,
3348 equate its fields to those of the standard exception type. This
3349 will make it possible to convert between them. */
3350 if (gnu_entity_name == exception_data_name_id)
3352 tree gnu_std_field;
3353 for (gnu_field = TYPE_FIELDS (gnu_type),
3354 gnu_std_field = TYPE_FIELDS (except_type_node);
3355 gnu_field;
3356 gnu_field = DECL_CHAIN (gnu_field),
3357 gnu_std_field = DECL_CHAIN (gnu_std_field))
3358 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3359 gcc_assert (!gnu_std_field);
3362 break;
3364 case E_Class_Wide_Subtype:
3365 /* If an equivalent type is present, that is what we should use.
3366 Otherwise, fall through to handle this like a record subtype
3367 since it may have constraints. */
3368 if (gnat_equiv_type != gnat_entity)
3370 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3371 maybe_present = true;
3372 break;
3375 /* ... fall through ... */
3377 case E_Record_Subtype:
3378 /* If Cloned_Subtype is Present it means this record subtype has
3379 identical layout to that type or subtype and we should use
3380 that GCC type for this one. The front end guarantees that
3381 the component list is shared. */
3382 if (Present (Cloned_Subtype (gnat_entity)))
3384 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3385 NULL_TREE, false);
3386 saved = true;
3387 break;
3390 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3391 changing the type, make a new type with each field having the type of
3392 the field in the new subtype but the position computed by transforming
3393 every discriminant reference according to the constraints. We don't
3394 see any difference between private and non-private type here since
3395 derivations from types should have been deferred until the completion
3396 of the private type. */
3397 else
3399 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3401 if (!definition)
3403 defer_incomplete_level++;
3404 this_deferred = true;
3407 tree gnu_base_type
3408 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3410 if (present_gnu_tree (gnat_entity))
3412 maybe_present = true;
3413 break;
3416 /* If this is a record subtype associated with a dispatch table,
3417 strip the suffix. This is necessary to make sure 2 different
3418 subtypes associated with the imported and exported views of a
3419 dispatch table are properly merged in LTO mode. */
3420 if (Is_Dispatch_Table_Entity (gnat_entity))
3422 char *p;
3423 Get_Encoded_Name (gnat_entity);
3424 p = strchr (Name_Buffer, '_');
3425 gcc_assert (p);
3426 strcpy (p+2, "dtS");
3427 gnu_entity_name = get_identifier (Name_Buffer);
3430 /* When the subtype has discriminants and these discriminants affect
3431 the initial shape it has inherited, factor them in. But for an
3432 Unchecked_Union (it must be an Itype), just return the type. */
3433 if (Has_Discriminants (gnat_entity)
3434 && Stored_Constraint (gnat_entity) != No_Elist
3435 && !Is_For_Access_Subtype (gnat_entity)
3436 && Is_Record_Type (gnat_base_type)
3437 && !Is_Unchecked_Union (gnat_base_type))
3439 vec<subst_pair> gnu_subst_list
3440 = build_subst_list (gnat_entity, gnat_base_type, definition);
3441 tree gnu_unpad_base_type;
3443 gnu_type = make_node (RECORD_TYPE);
3444 TYPE_NAME (gnu_type) = gnu_entity_name;
3445 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3447 /* Use the ultimate base record type as the debug type.
3448 Subtypes and derived types bring no useful
3449 information. */
3450 Entity_Id gnat_debug_type = gnat_entity;
3451 while (Etype (gnat_debug_type) != gnat_debug_type)
3452 gnat_debug_type = Etype (gnat_debug_type);
3453 tree gnu_debug_type
3454 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_debug_type));
3455 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_debug_type);
3457 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3458 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3459 = Reverse_Storage_Order (gnat_entity);
3460 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3462 /* Set the size, alignment and alias set of the type to match
3463 those of the base type, doing required substitutions. */
3464 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3465 gnu_subst_list);
3467 if (TYPE_IS_PADDING_P (gnu_base_type))
3468 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3469 else
3470 gnu_unpad_base_type = gnu_base_type;
3472 /* Set the layout of the type to match that of the base type,
3473 doing required substitutions. We will output debug info
3474 manually below so pass false as last argument. */
3475 copy_and_substitute_in_layout (gnat_entity, gnat_base_type,
3476 gnu_type, gnu_unpad_base_type,
3477 gnu_subst_list, false);
3479 /* Fill in locations of fields. */
3480 annotate_rep (gnat_entity, gnu_type);
3482 /* If debugging information is being written for the type and if
3483 we are asked to output such encodings, write a record that
3484 shows what we are a subtype of and also make a variable that
3485 indicates our size, if still variable. */
3486 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3488 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3489 tree gnu_unpad_base_name
3490 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3491 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3493 TYPE_NAME (gnu_subtype_marker)
3494 = create_concat_name (gnat_entity, "XVS");
3495 finish_record_type (gnu_subtype_marker,
3496 create_field_decl (gnu_unpad_base_name,
3497 build_reference_type
3498 (gnu_unpad_base_type),
3499 gnu_subtype_marker,
3500 NULL_TREE, NULL_TREE,
3501 0, 0),
3502 0, true);
3504 add_parallel_type (gnu_type, gnu_subtype_marker);
3506 if (definition
3507 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3508 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3509 TYPE_SIZE_UNIT (gnu_subtype_marker)
3510 = create_var_decl (create_concat_name (gnat_entity,
3511 "XVZ"),
3512 NULL_TREE, sizetype, gnu_size_unit,
3513 false, false, false, false, false,
3514 true, debug_info_p,
3515 NULL, gnat_entity);
3519 /* Otherwise, go down all the components in the new type and make
3520 them equivalent to those in the base type. */
3521 else
3523 gnu_type = gnu_base_type;
3525 for (gnat_temp = First_Entity (gnat_entity);
3526 Present (gnat_temp);
3527 gnat_temp = Next_Entity (gnat_temp))
3528 if ((Ekind (gnat_temp) == E_Discriminant
3529 && !Is_Unchecked_Union (gnat_base_type))
3530 || Ekind (gnat_temp) == E_Component)
3531 save_gnu_tree (gnat_temp,
3532 gnat_to_gnu_field_decl
3533 (Original_Record_Component (gnat_temp)),
3534 false);
3537 break;
3539 case E_Access_Subprogram_Type:
3540 case E_Anonymous_Access_Subprogram_Type:
3541 /* Use the special descriptor type for dispatch tables if needed,
3542 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3543 Note that we are only required to do so for static tables in
3544 order to be compatible with the C++ ABI, but Ada 2005 allows
3545 to extend library level tagged types at the local level so
3546 we do it in the non-static case as well. */
3547 if (TARGET_VTABLE_USES_DESCRIPTORS
3548 && Is_Dispatch_Table_Entity (gnat_entity))
3550 gnu_type = fdesc_type_node;
3551 gnu_size = TYPE_SIZE (gnu_type);
3552 break;
3555 /* ... fall through ... */
3557 case E_Allocator_Type:
3558 case E_Access_Type:
3559 case E_Access_Attribute_Type:
3560 case E_Anonymous_Access_Type:
3561 case E_General_Access_Type:
3563 /* The designated type and its equivalent type for gigi. */
3564 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3565 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3566 /* Whether it comes from a limited with. */
3567 const bool is_from_limited_with
3568 = (Is_Incomplete_Type (gnat_desig_equiv)
3569 && From_Limited_With (gnat_desig_equiv));
3570 /* Whether it is a completed Taft Amendment type. Such a type is to
3571 be treated as coming from a limited with clause if it is not in
3572 the main unit, i.e. we break potential circularities here in case
3573 the body of an external unit is loaded for inter-unit inlining. */
3574 const bool is_completed_taft_type
3575 = (Is_Incomplete_Type (gnat_desig_equiv)
3576 && Has_Completion_In_Body (gnat_desig_equiv)
3577 && Present (Full_View (gnat_desig_equiv)));
3578 /* The "full view" of the designated type. If this is an incomplete
3579 entity from a limited with, treat its non-limited view as the full
3580 view. Otherwise, if this is an incomplete or private type, use the
3581 full view. In the former case, we might point to a private type,
3582 in which case, we need its full view. Also, we want to look at the
3583 actual type used for the representation, so this takes a total of
3584 three steps. */
3585 Entity_Id gnat_desig_full_direct_first
3586 = (is_from_limited_with
3587 ? Non_Limited_View (gnat_desig_equiv)
3588 : (Is_Incomplete_Or_Private_Type (gnat_desig_equiv)
3589 ? Full_View (gnat_desig_equiv) : Empty));
3590 Entity_Id gnat_desig_full_direct
3591 = ((is_from_limited_with
3592 && Present (gnat_desig_full_direct_first)
3593 && Is_Private_Type (gnat_desig_full_direct_first))
3594 ? Full_View (gnat_desig_full_direct_first)
3595 : gnat_desig_full_direct_first);
3596 Entity_Id gnat_desig_full
3597 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3598 /* The type actually used to represent the designated type, either
3599 gnat_desig_full or gnat_desig_equiv. */
3600 Entity_Id gnat_desig_rep;
3601 /* We want to know if we'll be seeing the freeze node for any
3602 incomplete type we may be pointing to. */
3603 const bool in_main_unit
3604 = (Present (gnat_desig_full)
3605 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3606 : In_Extended_Main_Code_Unit (gnat_desig_type));
3607 /* True if we make a dummy type here. */
3608 bool made_dummy = false;
3609 /* The mode to be used for the pointer type. */
3610 scalar_int_mode p_mode;
3611 /* The GCC type used for the designated type. */
3612 tree gnu_desig_type = NULL_TREE;
3614 if (!int_mode_for_size (esize, 0).exists (&p_mode)
3615 || !targetm.valid_pointer_mode (p_mode))
3616 p_mode = ptr_mode;
3618 /* If either the designated type or its full view is an unconstrained
3619 array subtype, replace it with the type it's a subtype of. This
3620 avoids problems with multiple copies of unconstrained array types.
3621 Likewise, if the designated type is a subtype of an incomplete
3622 record type, use the parent type to avoid order of elaboration
3623 issues. This can lose some code efficiency, but there is no
3624 alternative. */
3625 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3626 && !Is_Constrained (gnat_desig_equiv))
3627 gnat_desig_equiv = Etype (gnat_desig_equiv);
3628 if (Present (gnat_desig_full)
3629 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3630 && !Is_Constrained (gnat_desig_full))
3631 || (Ekind (gnat_desig_full) == E_Record_Subtype
3632 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3633 gnat_desig_full = Etype (gnat_desig_full);
3635 /* Set the type that's the representation of the designated type. */
3636 gnat_desig_rep
3637 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3639 /* If we already know what the full type is, use it. */
3640 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3641 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3643 /* Get the type of the thing we are to point to and build a pointer to
3644 it. If it is a reference to an incomplete or private type with a
3645 full view that is a record, an array or an access, make a dummy type
3646 and get the actual type later when we have verified it is safe. */
3647 else if ((!in_main_unit
3648 && !present_gnu_tree (gnat_desig_equiv)
3649 && Present (gnat_desig_full)
3650 && (Is_Record_Type (gnat_desig_full)
3651 || Is_Array_Type (gnat_desig_full)
3652 || Is_Access_Type (gnat_desig_full)))
3653 /* Likewise if this is a reference to a record, an array or a
3654 subprogram type and we are to defer elaborating incomplete
3655 types. We do this because this access type may be the full
3656 view of a private type. */
3657 || ((!in_main_unit || imported_p)
3658 && defer_incomplete_level != 0
3659 && !present_gnu_tree (gnat_desig_equiv)
3660 && (Is_Record_Type (gnat_desig_rep)
3661 || Is_Array_Type (gnat_desig_rep)
3662 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3663 /* If this is a reference from a limited_with type back to our
3664 main unit and there's a freeze node for it, either we have
3665 already processed the declaration and made the dummy type,
3666 in which case we just reuse the latter, or we have not yet,
3667 in which case we make the dummy type and it will be reused
3668 when the declaration is finally processed. In both cases,
3669 the pointer eventually created below will be automatically
3670 adjusted when the freeze node is processed. */
3671 || (in_main_unit
3672 && is_from_limited_with
3673 && Present (Freeze_Node (gnat_desig_rep))))
3675 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3676 made_dummy = true;
3679 /* Otherwise handle the case of a pointer to itself. */
3680 else if (gnat_desig_equiv == gnat_entity)
3682 gnu_type
3683 = build_pointer_type_for_mode (void_type_node, p_mode,
3684 No_Strict_Aliasing (gnat_entity));
3685 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3688 /* If expansion is disabled, the equivalent type of a concurrent type
3689 is absent, so we use the void pointer type. */
3690 else if (type_annotate_only && No (gnat_desig_equiv))
3691 gnu_type = ptr_type_node;
3693 /* If the ultimately designated type is an incomplete type with no full
3694 view, we use the void pointer type in LTO mode to avoid emitting a
3695 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3696 the name of the dummy type in used by GDB for a global lookup. */
3697 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3698 && No (Full_View (gnat_desig_rep))
3699 && flag_generate_lto)
3700 gnu_type = ptr_type_node;
3702 /* Finally, handle the default case where we can just elaborate our
3703 designated type. */
3704 else
3705 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3707 /* It is possible that a call to gnat_to_gnu_type above resolved our
3708 type. If so, just return it. */
3709 if (present_gnu_tree (gnat_entity))
3711 maybe_present = true;
3712 break;
3715 /* Access-to-unconstrained-array types need a special treatment. */
3716 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3718 /* If the processing above got something that has a pointer, then
3719 we are done. This could have happened either because the type
3720 was elaborated or because somebody else executed the code. */
3721 if (!TYPE_POINTER_TO (gnu_desig_type))
3722 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3724 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3727 /* If we haven't done it yet, build the pointer type the usual way. */
3728 else if (!gnu_type)
3730 /* Modify the designated type if we are pointing only to constant
3731 objects, but don't do it for a dummy type. */
3732 if (Is_Access_Constant (gnat_entity)
3733 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3734 gnu_desig_type
3735 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3737 gnu_type
3738 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3739 No_Strict_Aliasing (gnat_entity));
3742 /* If the designated type is not declared in the main unit and we made
3743 a dummy node for it, save our definition, elaborate the actual type
3744 and replace the dummy type we made with the actual one. But if we
3745 are to defer actually looking up the actual type, make an entry in
3746 the deferred list instead. If this is from a limited with, we may
3747 have to defer until the end of the current unit. */
3748 if (!in_main_unit && made_dummy)
3750 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3751 gnu_type
3752 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3754 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3755 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3756 artificial_p, debug_info_p,
3757 gnat_entity);
3758 this_made_decl = true;
3759 gnu_type = TREE_TYPE (gnu_decl);
3760 save_gnu_tree (gnat_entity, gnu_decl, false);
3761 saved = true;
3763 if (defer_incomplete_level == 0
3764 && !is_from_limited_with
3765 && !is_completed_taft_type)
3767 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3768 gnat_to_gnu_type (gnat_desig_equiv));
3770 else
3772 struct incomplete *p = XNEW (struct incomplete);
3773 struct incomplete **head
3774 = (is_from_limited_with || is_completed_taft_type
3775 ? &defer_limited_with_list : &defer_incomplete_list);
3777 p->old_type = gnu_desig_type;
3778 p->full_type = gnat_desig_equiv;
3779 p->next = *head;
3780 *head = p;
3784 break;
3786 case E_Access_Protected_Subprogram_Type:
3787 case E_Anonymous_Access_Protected_Subprogram_Type:
3788 /* If we are just annotating types and have no equivalent record type,
3789 just use the void pointer type. */
3790 if (type_annotate_only && gnat_equiv_type == gnat_entity)
3791 gnu_type = ptr_type_node;
3793 /* The run-time representation is the equivalent type. */
3794 else
3796 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3797 maybe_present = true;
3800 /* The designated subtype must be elaborated as well, if it does
3801 not have its own freeze node. */
3802 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3803 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3804 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3805 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3806 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3807 NULL_TREE, false);
3809 break;
3811 case E_Access_Subtype:
3812 /* We treat this as identical to its base type; any constraint is
3813 meaningful only to the front-end. */
3814 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
3815 saved = true;
3817 /* The designated subtype must be elaborated as well, if it does
3818 not have its own freeze node. But designated subtypes created
3819 for constrained components of records with discriminants are
3820 not frozen by the front-end and not elaborated here, because
3821 their use may appear before the base type is frozen and it is
3822 not clear that they are needed in gigi. With the current model,
3823 there is no correct place where they could be elaborated. */
3824 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3825 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3826 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3827 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3829 /* If we are to defer elaborating incomplete types, make a dummy
3830 type node and elaborate it later. */
3831 if (defer_incomplete_level != 0)
3833 struct incomplete *p = XNEW (struct incomplete);
3835 p->old_type
3836 = make_dummy_type (Directly_Designated_Type (gnat_entity));
3837 p->full_type = Directly_Designated_Type (gnat_entity);
3838 p->next = defer_incomplete_list;
3839 defer_incomplete_list = p;
3841 else if (!Is_Incomplete_Or_Private_Type
3842 (Base_Type (Directly_Designated_Type (gnat_entity))))
3843 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3844 NULL_TREE, false);
3846 break;
3848 /* Subprogram Entities
3850 The following access functions are defined for subprograms:
3852 Etype Return type or Standard_Void_Type.
3853 First_Formal The first formal parameter.
3854 Is_Imported Indicates that the subprogram has appeared in
3855 an INTERFACE or IMPORT pragma. For now we
3856 assume that the external language is C.
3857 Is_Exported Likewise but for an EXPORT pragma.
3858 Is_Inlined True if the subprogram is to be inlined.
3860 Each parameter is first checked by calling must_pass_by_ref on its
3861 type to determine if it is passed by reference. For parameters which
3862 are copied in, if they are Ada In Out or Out parameters, their return
3863 value becomes part of a record which becomes the return type of the
3864 function (C function - note that this applies only to Ada procedures
3865 so there is no Ada return type). Additional code to store back the
3866 parameters will be generated on the caller side. This transformation
3867 is done here, not in the front-end.
3869 The intended result of the transformation can be seen from the
3870 equivalent source rewritings that follow:
3872 struct temp {int a,b};
3873 procedure P (A,B: In Out ...) is temp P (int A,B)
3874 begin {
3875 .. ..
3876 end P; return {A,B};
3879 temp t;
3880 P(X,Y); t = P(X,Y);
3881 X = t.a , Y = t.b;
3883 For subprogram types we need to perform mainly the same conversions to
3884 GCC form that are needed for procedures and function declarations. The
3885 only difference is that at the end, we make a type declaration instead
3886 of a function declaration. */
3888 case E_Subprogram_Type:
3889 case E_Function:
3890 case E_Procedure:
3892 tree gnu_ext_name
3893 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
3894 enum inline_status_t inline_status
3895 = Has_Pragma_No_Inline (gnat_entity)
3896 ? is_suppressed
3897 : Has_Pragma_Inline_Always (gnat_entity)
3898 ? is_required
3899 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
3900 bool public_flag = Is_Public (gnat_entity) || imported_p;
3901 /* Subprograms marked both Intrinsic and Always_Inline need not
3902 have a body of their own. */
3903 bool extern_flag
3904 = ((Is_Public (gnat_entity) && !definition)
3905 || imported_p
3906 || (Convention (gnat_entity) == Convention_Intrinsic
3907 && Has_Pragma_Inline_Always (gnat_entity)));
3908 tree gnu_param_list;
3910 /* A parameter may refer to this type, so defer completion of any
3911 incomplete types. */
3912 if (kind == E_Subprogram_Type && !definition)
3914 defer_incomplete_level++;
3915 this_deferred = true;
3918 /* If the subprogram has an alias, it is probably inherited, so
3919 we can use the original one. If the original "subprogram"
3920 is actually an enumeration literal, it may be the first use
3921 of its type, so we must elaborate that type now. */
3922 if (Present (Alias (gnat_entity)))
3924 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
3926 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3927 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
3928 false);
3930 gnu_decl
3931 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
3933 /* Elaborate any Itypes in the parameters of this entity. */
3934 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3935 Present (gnat_temp);
3936 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3937 if (Is_Itype (Etype (gnat_temp)))
3938 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3940 /* Materialize renamed subprograms in the debugging information
3941 when the renamed object is compile time known. We can consider
3942 such renamings as imported declarations.
3944 Because the parameters in generics instantiation are generally
3945 materialized as renamings, we ofter end up having both the
3946 renamed subprogram and the renaming in the same context and with
3947 the same name: in this case, renaming is both useless debug-wise
3948 and potentially harmful as name resolution in the debugger could
3949 return twice the same entity! So avoid this case. */
3950 if (debug_info_p && !artificial_p
3951 && !(get_debug_scope (gnat_entity, NULL)
3952 == get_debug_scope (gnat_renamed, NULL)
3953 && Name_Equals (Chars (gnat_entity),
3954 Chars (gnat_renamed)))
3955 && Present (gnat_renamed)
3956 && (Ekind (gnat_renamed) == E_Function
3957 || Ekind (gnat_renamed) == E_Procedure)
3958 && gnu_decl
3959 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
3961 tree decl = build_decl (input_location, IMPORTED_DECL,
3962 gnu_entity_name, void_type_node);
3963 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
3964 gnat_pushdecl (decl, gnat_entity);
3967 break;
3970 /* Get the GCC tree for the (underlying) subprogram type. If the
3971 entity is an actual subprogram, also get the parameter list. */
3972 gnu_type
3973 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
3974 &gnu_param_list);
3975 if (DECL_P (gnu_type))
3977 gnu_decl = gnu_type;
3978 gnu_type = TREE_TYPE (gnu_decl);
3979 break;
3982 /* Deal with platform-specific calling conventions. */
3983 if (Has_Stdcall_Convention (gnat_entity))
3984 prepend_one_attribute
3985 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3986 get_identifier ("stdcall"), NULL_TREE,
3987 gnat_entity);
3988 else if (Has_Thiscall_Convention (gnat_entity))
3989 prepend_one_attribute
3990 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3991 get_identifier ("thiscall"), NULL_TREE,
3992 gnat_entity);
3994 /* If we should request stack realignment for a foreign convention
3995 subprogram, do so. Note that this applies to task entry points
3996 in particular. */
3997 if (FOREIGN_FORCE_REALIGN_STACK
3998 && Has_Foreign_Convention (gnat_entity))
3999 prepend_one_attribute
4000 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4001 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4002 gnat_entity);
4004 /* Deal with a pragma Linker_Section on a subprogram. */
4005 if ((kind == E_Function || kind == E_Procedure)
4006 && Present (Linker_Section_Pragma (gnat_entity)))
4007 prepend_one_attribute_pragma (&attr_list,
4008 Linker_Section_Pragma (gnat_entity));
4010 /* If we are defining the subprogram and it has an Address clause
4011 we must get the address expression from the saved GCC tree for the
4012 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4013 the address expression here since the front-end has guaranteed
4014 in that case that the elaboration has no effects. If there is
4015 an Address clause and we are not defining the object, just
4016 make it a constant. */
4017 if (Present (Address_Clause (gnat_entity)))
4019 tree gnu_address = NULL_TREE;
4021 if (definition)
4022 gnu_address
4023 = (present_gnu_tree (gnat_entity)
4024 ? get_gnu_tree (gnat_entity)
4025 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4027 save_gnu_tree (gnat_entity, NULL_TREE, false);
4029 /* Convert the type of the object to a reference type that can
4030 alias everything as per RM 13.3(19). */
4031 gnu_type
4032 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4033 if (gnu_address)
4034 gnu_address = convert (gnu_type, gnu_address);
4036 gnu_decl
4037 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4038 gnu_address, false, Is_Public (gnat_entity),
4039 extern_flag, false, false, artificial_p,
4040 debug_info_p, NULL, gnat_entity);
4041 DECL_BY_REF_P (gnu_decl) = 1;
4044 /* If this is a mere subprogram type, just create the declaration. */
4045 else if (kind == E_Subprogram_Type)
4047 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4049 gnu_decl
4050 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4051 debug_info_p, gnat_entity);
4054 /* Otherwise create the subprogram declaration with the external name,
4055 the type and the parameter list. However, if this a reference to
4056 the allocation routines, reuse the canonical declaration nodes as
4057 they come with special properties. */
4058 else
4060 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4061 gnu_decl = malloc_decl;
4062 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4063 gnu_decl = realloc_decl;
4064 else
4066 gnu_decl
4067 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4068 gnu_type, gnu_param_list,
4069 inline_status, public_flag,
4070 extern_flag, artificial_p,
4071 debug_info_p,
4072 definition && imported_p, attr_list,
4073 gnat_entity);
4075 DECL_STUBBED_P (gnu_decl)
4076 = (Convention (gnat_entity) == Convention_Stubbed);
4080 break;
4082 case E_Incomplete_Type:
4083 case E_Incomplete_Subtype:
4084 case E_Private_Type:
4085 case E_Private_Subtype:
4086 case E_Limited_Private_Type:
4087 case E_Limited_Private_Subtype:
4088 case E_Record_Type_With_Private:
4089 case E_Record_Subtype_With_Private:
4091 const bool is_from_limited_with
4092 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4093 /* Get the "full view" of this entity. If this is an incomplete
4094 entity from a limited with, treat its non-limited view as the
4095 full view. Otherwise, use either the full view or the underlying
4096 full view, whichever is present. This is used in all the tests
4097 below. */
4098 const Entity_Id full_view
4099 = is_from_limited_with
4100 ? Non_Limited_View (gnat_entity)
4101 : Present (Full_View (gnat_entity))
4102 ? Full_View (gnat_entity)
4103 : IN (kind, Private_Kind)
4104 ? Underlying_Full_View (gnat_entity)
4105 : Empty;
4107 /* If this is an incomplete type with no full view, it must be a Taft
4108 Amendment type or an incomplete type coming from a limited context,
4109 in which cases we return a dummy type. Otherwise, we just get the
4110 type from its Etype. */
4111 if (No (full_view))
4113 if (kind == E_Incomplete_Type)
4115 gnu_type = make_dummy_type (gnat_entity);
4116 gnu_decl = TYPE_STUB_DECL (gnu_type);
4118 else
4120 gnu_decl
4121 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4122 maybe_present = true;
4126 /* Or else, if we already made a type for the full view, reuse it. */
4127 else if (present_gnu_tree (full_view))
4128 gnu_decl = get_gnu_tree (full_view);
4130 /* Or else, if we are not defining the type or there is no freeze
4131 node on it, get the type for the full view. Likewise if this is
4132 a limited_with'ed type not declared in the main unit, which can
4133 happen for incomplete formal types instantiated on a type coming
4134 from a limited_with clause. */
4135 else if (!definition
4136 || No (Freeze_Node (full_view))
4137 || (is_from_limited_with
4138 && !In_Extended_Main_Code_Unit (full_view)))
4140 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4141 maybe_present = true;
4144 /* Otherwise, make a dummy type entry which will be replaced later.
4145 Save it as the full declaration's type so we can do any needed
4146 updates when we see it. */
4147 else
4149 gnu_type = make_dummy_type (gnat_entity);
4150 gnu_decl = TYPE_STUB_DECL (gnu_type);
4151 if (Has_Completion_In_Body (gnat_entity))
4152 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4153 save_gnu_tree (full_view, gnu_decl, false);
4156 break;
4158 case E_Class_Wide_Type:
4159 /* Class-wide types are always transformed into their root type. */
4160 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4161 maybe_present = true;
4162 break;
4164 case E_Protected_Type:
4165 case E_Protected_Subtype:
4166 case E_Task_Type:
4167 case E_Task_Subtype:
4168 /* If we are just annotating types and have no equivalent record type,
4169 just return void_type, except for root types that have discriminants
4170 because the discriminants will very likely be used in the declarative
4171 part of the associated body so they need to be translated. */
4172 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4174 if (Has_Discriminants (gnat_entity)
4175 && Root_Type (gnat_entity) == gnat_entity)
4177 tree gnu_field_list = NULL_TREE;
4178 Entity_Id gnat_field;
4180 /* This is a minimal version of the E_Record_Type handling. */
4181 gnu_type = make_node (RECORD_TYPE);
4182 TYPE_NAME (gnu_type) = gnu_entity_name;
4184 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4185 Present (gnat_field);
4186 gnat_field = Next_Stored_Discriminant (gnat_field))
4188 tree gnu_field
4189 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4190 definition, debug_info_p);
4192 save_gnu_tree (gnat_field,
4193 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4194 build0 (PLACEHOLDER_EXPR, gnu_type),
4195 gnu_field, NULL_TREE),
4196 true);
4198 DECL_CHAIN (gnu_field) = gnu_field_list;
4199 gnu_field_list = gnu_field;
4202 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4203 false);
4205 else
4206 gnu_type = void_type_node;
4209 /* Concurrent types are always transformed into their record type. */
4210 else
4211 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4212 maybe_present = true;
4213 break;
4215 case E_Label:
4216 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4217 break;
4219 case E_Block:
4220 case E_Loop:
4221 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4222 we've already saved it, so we don't try to. */
4223 gnu_decl = error_mark_node;
4224 saved = true;
4225 break;
4227 case E_Abstract_State:
4228 /* This is a SPARK annotation that only reaches here when compiling in
4229 ASIS mode. */
4230 gcc_assert (type_annotate_only);
4231 gnu_decl = error_mark_node;
4232 saved = true;
4233 break;
4235 default:
4236 gcc_unreachable ();
4239 /* If we had a case where we evaluated another type and it might have
4240 defined this one, handle it here. */
4241 if (maybe_present && present_gnu_tree (gnat_entity))
4243 gnu_decl = get_gnu_tree (gnat_entity);
4244 saved = true;
4247 /* If we are processing a type and there is either no decl for it or
4248 we just made one, do some common processing for the type, such as
4249 handling alignment and possible padding. */
4250 if (is_type && (!gnu_decl || this_made_decl))
4252 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4254 /* Process the attributes, if not already done. Note that the type is
4255 already defined so we cannot pass true for IN_PLACE here. */
4256 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4258 /* ??? Don't set the size for a String_Literal since it is either
4259 confirming or we don't handle it properly (if the low bound is
4260 non-constant). */
4261 if (!gnu_size && kind != E_String_Literal_Subtype)
4263 Uint gnat_size = Known_Esize (gnat_entity)
4264 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4265 gnu_size
4266 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4267 false, Has_Size_Clause (gnat_entity));
4270 /* If a size was specified, see if we can make a new type of that size
4271 by rearranging the type, for example from a fat to a thin pointer. */
4272 if (gnu_size)
4274 gnu_type
4275 = make_type_from_size (gnu_type, gnu_size,
4276 Has_Biased_Representation (gnat_entity));
4278 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4279 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4280 gnu_size = NULL_TREE;
4283 /* If the alignment has not already been processed and this is not
4284 an unconstrained array type, see if an alignment is specified.
4285 If not, we pick a default alignment for atomic objects. */
4286 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4288 else if (Known_Alignment (gnat_entity))
4290 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4291 TYPE_ALIGN (gnu_type));
4293 /* Warn on suspiciously large alignments. This should catch
4294 errors about the (alignment,byte)/(size,bit) discrepancy. */
4295 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4297 tree size;
4299 /* If a size was specified, take it into account. Otherwise
4300 use the RM size for records or unions as the type size has
4301 already been adjusted to the alignment. */
4302 if (gnu_size)
4303 size = gnu_size;
4304 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4305 && !TYPE_FAT_POINTER_P (gnu_type))
4306 size = rm_size (gnu_type);
4307 else
4308 size = TYPE_SIZE (gnu_type);
4310 /* Consider an alignment as suspicious if the alignment/size
4311 ratio is greater or equal to the byte/bit ratio. */
4312 if (tree_fits_uhwi_p (size)
4313 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4314 post_error_ne ("?suspiciously large alignment specified for&",
4315 Expression (Alignment_Clause (gnat_entity)),
4316 gnat_entity);
4319 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4320 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4321 && integer_pow2p (TYPE_SIZE (gnu_type)))
4322 align = MIN (BIGGEST_ALIGNMENT,
4323 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4324 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4325 && tree_fits_uhwi_p (gnu_size)
4326 && integer_pow2p (gnu_size))
4327 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4329 /* See if we need to pad the type. If we did, and made a record,
4330 the name of the new type may be changed. So get it back for
4331 us when we make the new TYPE_DECL below. */
4332 if (gnu_size || align > 0)
4333 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4334 false, !gnu_decl, definition, false);
4336 if (TYPE_IS_PADDING_P (gnu_type))
4337 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4339 /* Now set the RM size of the type. We cannot do it before padding
4340 because we need to accept arbitrary RM sizes on integral types. */
4341 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4343 /* If we are at global level, GCC will have applied variable_size to
4344 the type, but that won't have done anything. So, if it's not
4345 a constant or self-referential, call elaborate_expression_1 to
4346 make a variable for the size rather than calculating it each time.
4347 Handle both the RM size and the actual size. */
4348 if (TYPE_SIZE (gnu_type)
4349 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4350 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4351 && global_bindings_p ())
4353 tree size = TYPE_SIZE (gnu_type);
4355 TYPE_SIZE (gnu_type)
4356 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4357 false);
4359 /* ??? For now, store the size as a multiple of the alignment in
4360 bytes so that we can see the alignment from the tree. */
4361 TYPE_SIZE_UNIT (gnu_type)
4362 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4363 "SIZE_A_UNIT", definition, false,
4364 TYPE_ALIGN (gnu_type));
4366 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4367 may not be marked by the call to create_type_decl below. */
4368 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4370 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4372 tree variant_part = get_variant_part (gnu_type);
4373 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4375 if (variant_part)
4377 tree union_type = TREE_TYPE (variant_part);
4378 tree offset = DECL_FIELD_OFFSET (variant_part);
4380 /* If the position of the variant part is constant, subtract
4381 it from the size of the type of the parent to get the new
4382 size. This manual CSE reduces the data size. */
4383 if (TREE_CODE (offset) == INTEGER_CST)
4385 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4386 TYPE_SIZE (union_type)
4387 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4388 bit_from_pos (offset, bitpos));
4389 TYPE_SIZE_UNIT (union_type)
4390 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4391 byte_from_pos (offset, bitpos));
4393 else
4395 TYPE_SIZE (union_type)
4396 = elaborate_expression_1 (TYPE_SIZE (union_type),
4397 gnat_entity, "VSIZE",
4398 definition, false);
4400 /* ??? For now, store the size as a multiple of the
4401 alignment in bytes so that we can see the alignment
4402 from the tree. */
4403 TYPE_SIZE_UNIT (union_type)
4404 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4405 gnat_entity, "VSIZE_A_UNIT",
4406 definition, false,
4407 TYPE_ALIGN (union_type));
4409 /* ??? For now, store the offset as a multiple of the
4410 alignment in bytes so that we can see the alignment
4411 from the tree. */
4412 DECL_FIELD_OFFSET (variant_part)
4413 = elaborate_expression_2 (offset, gnat_entity,
4414 "VOFFSET", definition, false,
4415 DECL_OFFSET_ALIGN
4416 (variant_part));
4419 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4420 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4423 if (operand_equal_p (ada_size, size, 0))
4424 ada_size = TYPE_SIZE (gnu_type);
4425 else
4426 ada_size
4427 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4428 definition, false);
4429 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4433 /* Similarly, if this is a record type or subtype at global level, call
4434 elaborate_expression_2 on any field position. Skip any fields that
4435 we haven't made trees for to avoid problems with class-wide types. */
4436 if (IN (kind, Record_Kind) && global_bindings_p ())
4437 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4438 gnat_temp = Next_Entity (gnat_temp))
4439 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4441 tree gnu_field = get_gnu_tree (gnat_temp);
4443 /* ??? For now, store the offset as a multiple of the alignment
4444 in bytes so that we can see the alignment from the tree. */
4445 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4446 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4448 DECL_FIELD_OFFSET (gnu_field)
4449 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4450 gnat_temp, "OFFSET", definition,
4451 false,
4452 DECL_OFFSET_ALIGN (gnu_field));
4454 /* ??? The context of gnu_field is not necessarily gnu_type
4455 so the MULT_EXPR node built above may not be marked by
4456 the call to create_type_decl below. */
4457 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4461 if (Is_Atomic_Or_VFA (gnat_entity))
4462 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4464 /* If this is not an unconstrained array type, set some flags. */
4465 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4467 /* Tell the middle-end that objects of tagged types are guaranteed to
4468 be properly aligned. This is necessary because conversions to the
4469 class-wide type are translated into conversions to the root type,
4470 which can be less aligned than some of its derived types. */
4471 if (Is_Tagged_Type (gnat_entity)
4472 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4473 TYPE_ALIGN_OK (gnu_type) = 1;
4475 /* Record whether the type is passed by reference. */
4476 if (Is_By_Reference_Type (gnat_entity) && !VOID_TYPE_P (gnu_type))
4477 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4479 /* Record whether an alignment clause was specified. */
4480 if (Present (Alignment_Clause (gnat_entity)))
4481 TYPE_USER_ALIGN (gnu_type) = 1;
4483 /* Record whether a pragma Universal_Aliasing was specified. */
4484 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4485 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4487 /* If it is passed by reference, force BLKmode to ensure that
4488 objects of this type will always be put in memory. */
4489 if (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type))
4490 SET_TYPE_MODE (gnu_type, BLKmode);
4493 /* If this is a derived type, relate its alias set to that of its parent
4494 to avoid troubles when a call to an inherited primitive is inlined in
4495 a context where a derived object is accessed. The inlined code works
4496 on the parent view so the resulting code may access the same object
4497 using both the parent and the derived alias sets, which thus have to
4498 conflict. As the same issue arises with component references, the
4499 parent alias set also has to conflict with composite types enclosing
4500 derived components. For instance, if we have:
4502 type D is new T;
4503 type R is record
4504 Component : D;
4505 end record;
4507 we want T to conflict with both D and R, in addition to R being a
4508 superset of D by record/component construction.
4510 One way to achieve this is to perform an alias set copy from the
4511 parent to the derived type. This is not quite appropriate, though,
4512 as we don't want separate derived types to conflict with each other:
4514 type I1 is new Integer;
4515 type I2 is new Integer;
4517 We want I1 and I2 to both conflict with Integer but we do not want
4518 I1 to conflict with I2, and an alias set copy on derivation would
4519 have that effect.
4521 The option chosen is to make the alias set of the derived type a
4522 superset of that of its parent type. It trivially fulfills the
4523 simple requirement for the Integer derivation example above, and
4524 the component case as well by superset transitivity:
4526 superset superset
4527 R ----------> D ----------> T
4529 However, for composite types, conversions between derived types are
4530 translated into VIEW_CONVERT_EXPRs so a sequence like:
4532 type Comp1 is new Comp;
4533 type Comp2 is new Comp;
4534 procedure Proc (C : Comp1);
4536 C : Comp2;
4537 Proc (Comp1 (C));
4539 is translated into:
4541 C : Comp2;
4542 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4544 and gimplified into:
4546 C : Comp2;
4547 Comp1 *C.0;
4548 C.0 = (Comp1 *) &C;
4549 Proc (C.0);
4551 i.e. generates code involving type punning. Therefore, Comp1 needs
4552 to conflict with Comp2 and an alias set copy is required.
4554 The language rules ensure the parent type is already frozen here. */
4555 if (kind != E_Subprogram_Type
4556 && Is_Derived_Type (gnat_entity)
4557 && !type_annotate_only)
4559 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4560 /* For constrained packed array subtypes, the implementation type is
4561 used instead of the nominal type. */
4562 if (kind == E_Array_Subtype
4563 && Is_Constrained (gnat_entity)
4564 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4565 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4566 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4567 Is_Composite_Type (gnat_entity)
4568 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4571 /* Finally get to the appropriate variant, except for the implementation
4572 type of a packed array because the GNU type might be further adjusted
4573 when the original array type is itself processed. */
4574 if (Treat_As_Volatile (gnat_entity)
4575 && !Is_Packed_Array_Impl_Type (gnat_entity))
4577 const int quals
4578 = TYPE_QUAL_VOLATILE
4579 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4580 gnu_type = change_qualified_type (gnu_type, quals);
4583 if (!gnu_decl)
4584 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4585 artificial_p, debug_info_p,
4586 gnat_entity);
4587 else
4589 TREE_TYPE (gnu_decl) = gnu_type;
4590 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4594 /* If we got a type that is not dummy, back-annotate the alignment of the
4595 type if not already in the tree. Likewise for the size, if any. */
4596 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4598 gnu_type = TREE_TYPE (gnu_decl);
4600 if (Unknown_Alignment (gnat_entity))
4602 unsigned int double_align, align;
4603 bool is_capped_double, align_clause;
4605 /* If the default alignment of "double" or larger scalar types is
4606 specifically capped and this is not an array with an alignment
4607 clause on the component type, return the cap. */
4608 if ((double_align = double_float_alignment) > 0)
4609 is_capped_double
4610 = is_double_float_or_array (gnat_entity, &align_clause);
4611 else if ((double_align = double_scalar_alignment) > 0)
4612 is_capped_double
4613 = is_double_scalar_or_array (gnat_entity, &align_clause);
4614 else
4615 is_capped_double = align_clause = false;
4617 if (is_capped_double && !align_clause)
4618 align = double_align;
4619 else
4620 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4622 Set_Alignment (gnat_entity, UI_From_Int (align));
4625 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4627 tree gnu_size = TYPE_SIZE (gnu_type);
4629 /* If the size is self-referential, annotate the maximum value. */
4630 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4631 gnu_size = max_size (gnu_size, true);
4633 /* If we are just annotating types and the type is tagged, the tag
4634 and the parent components are not generated by the front-end so
4635 alignment and sizes must be adjusted if there is no rep clause. */
4636 if (type_annotate_only
4637 && Is_Tagged_Type (gnat_entity)
4638 && Unknown_RM_Size (gnat_entity)
4639 && !VOID_TYPE_P (gnu_type)
4640 && (!TYPE_FIELDS (gnu_type)
4641 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4643 tree offset;
4645 if (Is_Derived_Type (gnat_entity))
4647 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4648 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4649 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4651 else
4653 unsigned int align
4654 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4655 offset = bitsize_int (POINTER_SIZE);
4656 Set_Alignment (gnat_entity, UI_From_Int (align));
4659 if (TYPE_FIELDS (gnu_type))
4660 offset
4661 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4663 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4664 gnu_size = round_up (gnu_size, POINTER_SIZE);
4665 Uint uint_size = annotate_value (gnu_size);
4666 Set_RM_Size (gnat_entity, uint_size);
4667 Set_Esize (gnat_entity, uint_size);
4670 /* If there is a rep clause, only adjust alignment and Esize. */
4671 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4673 unsigned int align
4674 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4675 Set_Alignment (gnat_entity, UI_From_Int (align));
4676 gnu_size = round_up (gnu_size, POINTER_SIZE);
4677 Set_Esize (gnat_entity, annotate_value (gnu_size));
4680 /* Otherwise no adjustment is needed. */
4681 else
4682 Set_Esize (gnat_entity, annotate_value (gnu_size));
4685 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4686 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4689 /* If we haven't already, associate the ..._DECL node that we just made with
4690 the input GNAT entity node. */
4691 if (!saved)
4692 save_gnu_tree (gnat_entity, gnu_decl, false);
4694 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4695 eliminate as many deferred computations as possible. */
4696 process_deferred_decl_context (false);
4698 /* If this is an enumeration or floating-point type, we were not able to set
4699 the bounds since they refer to the type. These are always static. */
4700 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4701 || (kind == E_Floating_Point_Type))
4703 tree gnu_scalar_type = gnu_type;
4704 tree gnu_low_bound, gnu_high_bound;
4706 /* If this is a padded type, we need to use the underlying type. */
4707 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4708 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4710 /* If this is a floating point type and we haven't set a floating
4711 point type yet, use this in the evaluation of the bounds. */
4712 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4713 longest_float_type_node = gnu_scalar_type;
4715 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4716 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4718 if (kind == E_Enumeration_Type)
4720 /* Enumeration types have specific RM bounds. */
4721 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4722 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4724 else
4726 /* Floating-point types don't have specific RM bounds. */
4727 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4728 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4732 /* If we deferred processing of incomplete types, re-enable it. If there
4733 were no other disables and we have deferred types to process, do so. */
4734 if (this_deferred
4735 && --defer_incomplete_level == 0
4736 && defer_incomplete_list)
4738 struct incomplete *p, *next;
4740 /* We are back to level 0 for the deferring of incomplete types.
4741 But processing these incomplete types below may itself require
4742 deferring, so preserve what we have and restart from scratch. */
4743 p = defer_incomplete_list;
4744 defer_incomplete_list = NULL;
4746 for (; p; p = next)
4748 next = p->next;
4750 if (p->old_type)
4751 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4752 gnat_to_gnu_type (p->full_type));
4753 free (p);
4757 /* If we are not defining this type, see if it's on one of the lists of
4758 incomplete types. If so, handle the list entry now. */
4759 if (is_type && !definition)
4761 struct incomplete *p;
4763 for (p = defer_incomplete_list; p; p = p->next)
4764 if (p->old_type && p->full_type == gnat_entity)
4766 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4767 TREE_TYPE (gnu_decl));
4768 p->old_type = NULL_TREE;
4771 for (p = defer_limited_with_list; p; p = p->next)
4772 if (p->old_type
4773 && (Non_Limited_View (p->full_type) == gnat_entity
4774 || Full_View (p->full_type) == gnat_entity))
4776 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4777 TREE_TYPE (gnu_decl));
4778 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4779 update_profiles_with (p->old_type);
4780 p->old_type = NULL_TREE;
4784 if (this_global)
4785 force_global--;
4787 /* If this is a packed array type whose original array type is itself
4788 an Itype without freeze node, make sure the latter is processed. */
4789 if (Is_Packed_Array_Impl_Type (gnat_entity)
4790 && Is_Itype (Original_Array_Type (gnat_entity))
4791 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4792 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4793 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
4795 return gnu_decl;
4798 /* Similar, but if the returned value is a COMPONENT_REF, return the
4799 FIELD_DECL. */
4801 tree
4802 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4804 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4806 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4807 gnu_field = TREE_OPERAND (gnu_field, 1);
4809 return gnu_field;
4812 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4813 the GCC type corresponding to that entity. */
4815 tree
4816 gnat_to_gnu_type (Entity_Id gnat_entity)
4818 tree gnu_decl;
4820 /* The back end never attempts to annotate generic types. */
4821 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4822 return void_type_node;
4824 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4825 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4827 return TREE_TYPE (gnu_decl);
4830 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4831 the unpadded version of the GCC type corresponding to that entity. */
4833 tree
4834 get_unpadded_type (Entity_Id gnat_entity)
4836 tree type = gnat_to_gnu_type (gnat_entity);
4838 if (TYPE_IS_PADDING_P (type))
4839 type = TREE_TYPE (TYPE_FIELDS (type));
4841 return type;
4844 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
4845 a C++ imported method or equivalent.
4847 We use the predicate on 32-bit x86/Windows to find out whether we need to
4848 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
4849 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
4851 bool
4852 is_cplusplus_method (Entity_Id gnat_entity)
4854 /* A constructor is a method on the C++ side. We deal with it now because
4855 it is declared without the 'this' parameter in the sources and, although
4856 the front-end will create a version with the 'this' parameter for code
4857 generation purposes, we want to return true for both versions. */
4858 if (Is_Constructor (gnat_entity))
4859 return true;
4861 /* Check that the subprogram has C++ convention. */
4862 if (Convention (gnat_entity) != Convention_CPP)
4863 return false;
4865 /* And that the type of the first parameter (indirectly) has it too. */
4866 Entity_Id gnat_first = First_Formal (gnat_entity);
4867 if (No (gnat_first))
4868 return false;
4870 Entity_Id gnat_type = Etype (gnat_first);
4871 if (Is_Access_Type (gnat_type))
4872 gnat_type = Directly_Designated_Type (gnat_type);
4873 if (Convention (gnat_type) != Convention_CPP)
4874 return false;
4876 /* This is the main case: a C++ virtual method imported as a primitive
4877 operation of a tagged type. */
4878 if (Is_Dispatching_Operation (gnat_entity))
4879 return true;
4881 /* This is set on the E_Subprogram_Type built for a dispatching call. */
4882 if (Is_Dispatch_Table_Entity (gnat_entity))
4883 return true;
4885 /* A thunk needs to be handled like its associated primitive operation. */
4886 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
4887 return true;
4889 /* Now on to the annoying case: a C++ non-virtual method, imported either
4890 as a non-primitive operation of a tagged type or as a primitive operation
4891 of an untagged type. We cannot reliably differentiate these cases from
4892 their static member or regular function equivalents in Ada, so we ask
4893 the C++ side through the mangled name of the function, as the implicit
4894 'this' parameter is not encoded in the mangled name of a method. */
4895 if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity)))
4897 String_Pointer sp = { NULL, NULL };
4898 Get_External_Name (gnat_entity, false, sp);
4900 void *mem;
4901 struct demangle_component *cmp
4902 = cplus_demangle_v3_components (Name_Buffer,
4903 DMGL_GNU_V3
4904 | DMGL_TYPES
4905 | DMGL_PARAMS
4906 | DMGL_RET_DROP,
4907 &mem);
4908 if (!cmp)
4909 return false;
4911 /* We need to release MEM once we have a successful demangling. */
4912 bool ret = false;
4914 if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME
4915 && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE
4916 && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL
4917 && cmp->type == DEMANGLE_COMPONENT_ARGLIST)
4919 /* Make sure there is at least one parameter in C++ too. */
4920 if (cmp->u.s_binary.left)
4922 unsigned int n_ada_args = 0;
4923 do {
4924 n_ada_args++;
4925 gnat_first = Next_Formal (gnat_first);
4926 } while (Present (gnat_first));
4928 unsigned int n_cpp_args = 0;
4929 do {
4930 n_cpp_args++;
4931 cmp = cmp->u.s_binary.right;
4932 } while (cmp);
4934 if (n_cpp_args < n_ada_args)
4935 ret = true;
4937 else
4938 ret = true;
4941 free (mem);
4943 return ret;
4946 return false;
4949 /* Finalize the processing of From_Limited_With incomplete types. */
4951 void
4952 finalize_from_limited_with (void)
4954 struct incomplete *p, *next;
4956 p = defer_limited_with_list;
4957 defer_limited_with_list = NULL;
4959 for (; p; p = next)
4961 next = p->next;
4963 if (p->old_type)
4965 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4966 gnat_to_gnu_type (p->full_type));
4967 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4968 update_profiles_with (p->old_type);
4971 free (p);
4975 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind
4976 of type (such E_Task_Type) that has a different type which Gigi uses
4977 for its representation. If the type does not have a special type for
4978 its representation, return GNAT_ENTITY. */
4980 Entity_Id
4981 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4983 Entity_Id gnat_equiv = gnat_entity;
4985 if (No (gnat_entity))
4986 return gnat_entity;
4988 switch (Ekind (gnat_entity))
4990 case E_Class_Wide_Subtype:
4991 if (Present (Equivalent_Type (gnat_entity)))
4992 gnat_equiv = Equivalent_Type (gnat_entity);
4993 break;
4995 case E_Access_Protected_Subprogram_Type:
4996 case E_Anonymous_Access_Protected_Subprogram_Type:
4997 if (Present (Equivalent_Type (gnat_entity)))
4998 gnat_equiv = Equivalent_Type (gnat_entity);
4999 break;
5001 case E_Class_Wide_Type:
5002 gnat_equiv = Root_Type (gnat_entity);
5003 break;
5005 case E_Protected_Type:
5006 case E_Protected_Subtype:
5007 case E_Task_Type:
5008 case E_Task_Subtype:
5009 if (Present (Corresponding_Record_Type (gnat_entity)))
5010 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5011 break;
5013 default:
5014 break;
5017 return gnat_equiv;
5020 /* Return a GCC tree for a type corresponding to the component type of the
5021 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5022 is for an array being defined. DEBUG_INFO_P is true if we need to write
5023 debug information for other types that we may create in the process. */
5025 static tree
5026 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5027 bool debug_info_p)
5029 const Entity_Id gnat_type = Component_Type (gnat_array);
5030 tree gnu_type = gnat_to_gnu_type (gnat_type);
5031 tree gnu_comp_size;
5032 unsigned int max_align;
5034 /* If an alignment is specified, use it as a cap on the component type
5035 so that it can be honored for the whole type. But ignore it for the
5036 original type of packed array types. */
5037 if (No (Packed_Array_Impl_Type (gnat_array))
5038 && Known_Alignment (gnat_array))
5039 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5040 else
5041 max_align = 0;
5043 /* Try to get a smaller form of the component if needed. */
5044 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5045 && !Is_Bit_Packed_Array (gnat_array)
5046 && !Has_Aliased_Components (gnat_array)
5047 && !Strict_Alignment (gnat_type)
5048 && RECORD_OR_UNION_TYPE_P (gnu_type)
5049 && !TYPE_FAT_POINTER_P (gnu_type)
5050 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5051 gnu_type = make_packable_type (gnu_type, false, max_align);
5053 if (Has_Atomic_Components (gnat_array))
5054 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5056 /* Get and validate any specified Component_Size. */
5057 gnu_comp_size
5058 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5059 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5060 true, Has_Component_Size_Clause (gnat_array));
5062 /* If the array has aliased components and the component size can be zero,
5063 force at least unit size to ensure that the components have distinct
5064 addresses. */
5065 if (!gnu_comp_size
5066 && Has_Aliased_Components (gnat_array)
5067 && (integer_zerop (TYPE_SIZE (gnu_type))
5068 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5069 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5070 gnu_comp_size
5071 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5073 /* If the component type is a RECORD_TYPE that has a self-referential size,
5074 then use the maximum size for the component size. */
5075 if (!gnu_comp_size
5076 && TREE_CODE (gnu_type) == RECORD_TYPE
5077 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5078 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5080 /* Honor the component size. This is not needed for bit-packed arrays. */
5081 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5083 tree orig_type = gnu_type;
5085 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5086 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5087 gnu_type = orig_type;
5088 else
5089 orig_type = gnu_type;
5091 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5092 true, false, definition, true);
5094 /* If a padding record was made, declare it now since it will never be
5095 declared otherwise. This is necessary to ensure that its subtrees
5096 are properly marked. */
5097 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5098 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5099 gnat_array);
5102 /* If the component type is a padded type made for a non-bit-packed array
5103 of scalars with reverse storage order, we need to propagate the reverse
5104 storage order to the padding type since it is the innermost enclosing
5105 aggregate type around the scalar. */
5106 if (TYPE_IS_PADDING_P (gnu_type)
5107 && Reverse_Storage_Order (gnat_array)
5108 && !Is_Bit_Packed_Array (gnat_array)
5109 && Is_Scalar_Type (gnat_type))
5110 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5112 if (Has_Volatile_Components (gnat_array))
5114 const int quals
5115 = TYPE_QUAL_VOLATILE
5116 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5117 gnu_type = change_qualified_type (gnu_type, quals);
5120 return gnu_type;
5123 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5124 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5125 the type of the parameter. FIRST is true if this is the first parameter in
5126 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5127 the copy-in copy-out implementation mechanism.
5129 The returned tree is a PARM_DECL, except for the cases where no parameter
5130 needs to be actually passed to the subprogram; the type of this "shadow"
5131 parameter is then returned instead. */
5133 static tree
5134 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5135 Entity_Id gnat_subprog, bool *cico)
5137 Entity_Id gnat_param_type = Etype (gnat_param);
5138 Mechanism_Type mech = Mechanism (gnat_param);
5139 tree gnu_param_name = get_entity_name (gnat_param);
5140 bool foreign = Has_Foreign_Convention (gnat_subprog);
5141 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5142 /* The parameter can be indirectly modified if its address is taken. */
5143 bool ro_param = in_param && !Address_Taken (gnat_param);
5144 bool by_return = false, by_component_ptr = false;
5145 bool by_ref = false;
5146 bool restricted_aliasing_p = false;
5147 location_t saved_location = input_location;
5148 tree gnu_param;
5150 /* Make sure to use the proper SLOC for vector ABI warnings. */
5151 if (VECTOR_TYPE_P (gnu_param_type))
5152 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5154 /* Builtins are expanded inline and there is no real call sequence involved.
5155 So the type expected by the underlying expander is always the type of the
5156 argument "as is". */
5157 if (Convention (gnat_subprog) == Convention_Intrinsic
5158 && Present (Interface_Name (gnat_subprog)))
5159 mech = By_Copy;
5161 /* Handle the first parameter of a valued procedure specially: it's a copy
5162 mechanism for which the parameter is never allocated. */
5163 else if (first && Is_Valued_Procedure (gnat_subprog))
5165 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5166 mech = By_Copy;
5167 by_return = true;
5170 /* Or else, see if a Mechanism was supplied that forced this parameter
5171 to be passed one way or another. */
5172 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5175 /* Positive mechanism means by copy for sufficiently small parameters. */
5176 else if (mech > 0)
5178 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5179 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5180 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5181 mech = By_Reference;
5182 else
5183 mech = By_Copy;
5186 /* Otherwise, it's an unsupported mechanism so error out. */
5187 else
5189 post_error ("unsupported mechanism for&", gnat_param);
5190 mech = Default;
5193 /* If this is either a foreign function or if the underlying type won't
5194 be passed by reference and is as aligned as the original type, strip
5195 off possible padding type. */
5196 if (TYPE_IS_PADDING_P (gnu_param_type))
5198 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5200 if (foreign
5201 || (!must_pass_by_ref (unpadded_type)
5202 && mech != By_Reference
5203 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5204 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5205 gnu_param_type = unpadded_type;
5208 /* If this is a read-only parameter, make a variant of the type that is
5209 read-only. ??? However, if this is a self-referential type, the type
5210 can be very complex, so skip it for now. */
5211 if (ro_param && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5212 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5214 /* For foreign conventions, pass arrays as pointers to the element type.
5215 First check for unconstrained array and get the underlying array. */
5216 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5217 gnu_param_type
5218 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5220 /* Arrays are passed as pointers to element type for foreign conventions. */
5221 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5223 /* Strip off any multi-dimensional entries, then strip
5224 off the last array to get the component type. */
5225 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5226 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5227 gnu_param_type = TREE_TYPE (gnu_param_type);
5229 by_component_ptr = true;
5230 gnu_param_type = TREE_TYPE (gnu_param_type);
5232 if (ro_param)
5233 gnu_param_type
5234 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5236 gnu_param_type = build_pointer_type (gnu_param_type);
5239 /* Fat pointers are passed as thin pointers for foreign conventions. */
5240 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5241 gnu_param_type
5242 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5244 /* If we were requested or muss pass by reference, do so.
5245 If we were requested to pass by copy, do so.
5246 Otherwise, for foreign conventions, pass In Out or Out parameters
5247 or aggregates by reference. For COBOL and Fortran, pass all
5248 integer and FP types that way too. For Convention Ada, use
5249 the standard Ada default. */
5250 else if (mech == By_Reference
5251 || must_pass_by_ref (gnu_param_type)
5252 || (mech != By_Copy
5253 && ((foreign
5254 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5255 || (foreign
5256 && (Convention (gnat_subprog) == Convention_Fortran
5257 || Convention (gnat_subprog) == Convention_COBOL)
5258 && (INTEGRAL_TYPE_P (gnu_param_type)
5259 || FLOAT_TYPE_P (gnu_param_type)))
5260 || (!foreign
5261 && default_pass_by_ref (gnu_param_type)))))
5263 /* We take advantage of 6.2(12) by considering that references built for
5264 parameters whose type isn't by-ref and for which the mechanism hasn't
5265 been forced to by-ref allow only a restricted form of aliasing. */
5266 restricted_aliasing_p
5267 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5268 gnu_param_type = build_reference_type (gnu_param_type);
5269 by_ref = true;
5272 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5273 else if (!in_param)
5274 *cico = true;
5276 input_location = saved_location;
5278 if (mech == By_Copy && (by_ref || by_component_ptr))
5279 post_error ("?cannot pass & by copy", gnat_param);
5281 /* If this is an Out parameter that isn't passed by reference and isn't
5282 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5283 it will be a VAR_DECL created when we process the procedure, so just
5284 return its type. For the special parameter of a valued procedure,
5285 never pass it in.
5287 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5288 Out parameters with discriminants or implicit initial values to be
5289 handled like In Out parameters. These type are normally built as
5290 aggregates, hence passed by reference, except for some packed arrays
5291 which end up encoded in special integer types. Note that scalars can
5292 be given implicit initial values using the Default_Value aspect.
5294 The exception we need to make is then for packed arrays of records
5295 with discriminants or implicit initial values. We have no light/easy
5296 way to check for the latter case, so we merely check for packed arrays
5297 of records. This may lead to useless copy-in operations, but in very
5298 rare cases only, as these would be exceptions in a set of already
5299 exceptional situations. */
5300 if (Ekind (gnat_param) == E_Out_Parameter
5301 && !by_ref
5302 && (by_return
5303 || (!POINTER_TYPE_P (gnu_param_type)
5304 && !AGGREGATE_TYPE_P (gnu_param_type)
5305 && !Has_Default_Aspect (gnat_param_type)))
5306 && !(Is_Array_Type (gnat_param_type)
5307 && Is_Packed (gnat_param_type)
5308 && Is_Composite_Type (Component_Type (gnat_param_type))))
5309 return gnu_param_type;
5311 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5312 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5313 DECL_BY_REF_P (gnu_param) = by_ref;
5314 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5315 DECL_POINTS_TO_READONLY_P (gnu_param)
5316 = (ro_param && (by_ref || by_component_ptr));
5317 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5318 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5319 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5321 /* If no Mechanism was specified, indicate what we're using, then
5322 back-annotate it. */
5323 if (mech == Default)
5324 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5326 Set_Mechanism (gnat_param, mech);
5327 return gnu_param;
5330 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5331 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5333 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5334 the corresponding profile, which means that, by the time the freeze node
5335 of the subprogram is encountered, types involved in its profile may still
5336 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5337 the freeze node of types involved in its profile, either types of formal
5338 parameters or the return type. */
5340 static void
5341 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5343 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5345 struct tree_entity_vec_map in;
5346 in.base.from = gnu_type;
5347 struct tree_entity_vec_map **slot
5348 = dummy_to_subprog_map->find_slot (&in, INSERT);
5349 if (!*slot)
5351 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5352 e->base.from = gnu_type;
5353 e->to = NULL;
5354 *slot = e;
5357 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5358 because the vector might have been just emptied by update_profiles_with.
5359 This can happen when there are 2 freeze nodes associated with different
5360 views of the same type; the type will be really complete only after the
5361 second freeze node is encountered. */
5362 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5364 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5366 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5367 since this would mean updating twice its profile. */
5368 if (v)
5370 const unsigned len = v->length ();
5371 unsigned int l = 0, u = len;
5373 /* Entity_Id is a simple integer so we can implement a stable order on
5374 the vector with an ordered insertion scheme and binary search. */
5375 while (l < u)
5377 unsigned int m = (l + u) / 2;
5378 int diff = (int) (*v)[m] - (int) gnat_subprog;
5379 if (diff > 0)
5380 u = m;
5381 else if (diff < 0)
5382 l = m + 1;
5383 else
5384 return;
5387 /* l == u and therefore is the insertion point. */
5388 vec_safe_insert (v, l, gnat_subprog);
5390 else
5391 vec_safe_push (v, gnat_subprog);
5393 (*slot)->to = v;
5396 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5398 static void
5399 update_profile (Entity_Id gnat_subprog)
5401 tree gnu_param_list;
5402 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5403 Needs_Debug_Info (gnat_subprog),
5404 &gnu_param_list);
5405 if (DECL_P (gnu_type))
5407 /* Builtins cannot have their address taken so we can reset them. */
5408 gcc_assert (DECL_BUILT_IN (gnu_type));
5409 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5410 save_gnu_tree (gnat_subprog, gnu_type, false);
5411 return;
5414 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5416 TREE_TYPE (gnu_subprog) = gnu_type;
5418 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5419 and needs to be adjusted too. */
5420 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5422 tree gnu_entity_name = get_entity_name (gnat_subprog);
5423 tree gnu_ext_name
5424 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5426 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5427 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5431 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5432 a dummy type which appears in profiles. */
5434 void
5435 update_profiles_with (tree gnu_type)
5437 struct tree_entity_vec_map in;
5438 in.base.from = gnu_type;
5439 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5440 gcc_assert (e);
5441 vec<Entity_Id, va_gc_atomic> *v = e->to;
5442 e->to = NULL;
5444 /* The flag needs to be reset before calling update_profile, in case
5445 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5446 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5448 unsigned int i;
5449 Entity_Id *iter;
5450 FOR_EACH_VEC_ELT (*v, i, iter)
5451 update_profile (*iter);
5453 vec_free (v);
5456 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5458 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5459 context may now appear as parameter and result types. As a consequence,
5460 we may need to defer their translation until after a freeze node is seen
5461 or to the end of the current unit. We also aim at handling temporarily
5462 incomplete types created by the usual delayed elaboration scheme. */
5464 static tree
5465 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5467 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5468 so the rationale is exposed in that place. These processings probably
5469 ought to be merged at some point. */
5470 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5471 const bool is_from_limited_with
5472 = (Is_Incomplete_Type (gnat_equiv)
5473 && From_Limited_With (gnat_equiv));
5474 Entity_Id gnat_full_direct_first
5475 = (is_from_limited_with
5476 ? Non_Limited_View (gnat_equiv)
5477 : (Is_Incomplete_Or_Private_Type (gnat_equiv)
5478 ? Full_View (gnat_equiv) : Empty));
5479 Entity_Id gnat_full_direct
5480 = ((is_from_limited_with
5481 && Present (gnat_full_direct_first)
5482 && Is_Private_Type (gnat_full_direct_first))
5483 ? Full_View (gnat_full_direct_first)
5484 : gnat_full_direct_first);
5485 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5486 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5487 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5488 tree gnu_type;
5490 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5491 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5493 else if (is_from_limited_with
5494 && ((!in_main_unit
5495 && !present_gnu_tree (gnat_equiv)
5496 && Present (gnat_full)
5497 && (Is_Record_Type (gnat_full)
5498 || Is_Array_Type (gnat_full)
5499 || Is_Access_Type (gnat_full)))
5500 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5502 gnu_type = make_dummy_type (gnat_equiv);
5504 if (!in_main_unit)
5506 struct incomplete *p = XNEW (struct incomplete);
5508 p->old_type = gnu_type;
5509 p->full_type = gnat_equiv;
5510 p->next = defer_limited_with_list;
5511 defer_limited_with_list = p;
5515 else if (type_annotate_only && No (gnat_equiv))
5516 gnu_type = void_type_node;
5518 else
5519 gnu_type = gnat_to_gnu_type (gnat_equiv);
5521 /* Access-to-unconstrained-array types need a special treatment. */
5522 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5524 if (!TYPE_POINTER_TO (gnu_type))
5525 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5528 return gnu_type;
5531 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5532 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5533 is true if we need to write debug information for other types that we may
5534 create in the process. Also set PARAM_LIST to the list of parameters.
5535 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5536 directly instead of its type. */
5538 static tree
5539 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5540 bool debug_info_p, tree *param_list)
5542 const Entity_Kind kind = Ekind (gnat_subprog);
5543 Entity_Id gnat_return_type = Etype (gnat_subprog);
5544 Entity_Id gnat_param;
5545 tree gnu_type = present_gnu_tree (gnat_subprog)
5546 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5547 tree gnu_return_type;
5548 tree gnu_param_type_list = NULL_TREE;
5549 tree gnu_param_list = NULL_TREE;
5550 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5551 (In Out or Out parameters not passed by reference), in which case it is
5552 the list of nodes used to specify the values of the In Out/Out parameters
5553 that are returned as a record upon procedure return. The TREE_PURPOSE of
5554 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5555 is the PARM_DECL corresponding to that field. This list will be saved in
5556 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5557 tree gnu_cico_list = NULL_TREE;
5558 tree gnu_cico_return_type = NULL_TREE;
5559 /* Fields in return type of procedure with copy-in copy-out parameters. */
5560 tree gnu_field_list = NULL_TREE;
5561 /* The semantics of "pure" in Ada essentially matches that of "const"
5562 in the back-end. In particular, both properties are orthogonal to
5563 the "nothrow" property if the EH circuitry is explicit in the
5564 internal representation of the back-end. If we are to completely
5565 hide the EH circuitry from it, we need to declare that calls to pure
5566 Ada subprograms that can throw have side effects since they can
5567 trigger an "abnormal" transfer of control flow; thus they can be
5568 neither "const" nor "pure" in the back-end sense. */
5569 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5570 bool return_by_direct_ref_p = false;
5571 bool return_by_invisi_ref_p = false;
5572 bool return_unconstrained_p = false;
5573 bool incomplete_profile_p = false;
5574 unsigned int num;
5576 /* Look into the return type and get its associated GCC tree if it is not
5577 void, and then compute various flags for the subprogram type. But make
5578 sure not to do this processing multiple times. */
5579 if (Ekind (gnat_return_type) == E_Void)
5580 gnu_return_type = void_type_node;
5582 else if (gnu_type
5583 && TREE_CODE (gnu_type) == FUNCTION_TYPE
5584 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5586 gnu_return_type = TREE_TYPE (gnu_type);
5587 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5588 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5589 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5592 else
5594 /* For foreign convention subprograms, return System.Address as void *
5595 or equivalent. Note that this comprises GCC builtins. */
5596 if (Has_Foreign_Convention (gnat_subprog)
5597 && Is_Descendant_Of_Address (Underlying_Type (gnat_return_type)))
5598 gnu_return_type = ptr_type_node;
5599 else
5600 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5602 /* If this function returns by reference, make the actual return type
5603 the reference type and make a note of that. */
5604 if (Returns_By_Ref (gnat_subprog))
5606 gnu_return_type = build_reference_type (gnu_return_type);
5607 return_by_direct_ref_p = true;
5610 /* If the return type is an unconstrained array type, the return value
5611 will be allocated on the secondary stack so the actual return type
5612 is the fat pointer type. */
5613 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5615 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5616 return_unconstrained_p = true;
5619 /* This is the same unconstrained array case, but for a dummy type. */
5620 else if (TYPE_REFERENCE_TO (gnu_return_type)
5621 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5623 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5624 return_unconstrained_p = true;
5627 /* Likewise, if the return type requires a transient scope, the return
5628 value will also be allocated on the secondary stack so the actual
5629 return type is the reference type. */
5630 else if (Requires_Transient_Scope (gnat_return_type))
5632 gnu_return_type = build_reference_type (gnu_return_type);
5633 return_unconstrained_p = true;
5636 /* If the Mechanism is By_Reference, ensure this function uses the
5637 target's by-invisible-reference mechanism, which may not be the
5638 same as above (e.g. it might be passing an extra parameter). */
5639 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5640 return_by_invisi_ref_p = true;
5642 /* Likewise, if the return type is itself By_Reference. */
5643 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5644 return_by_invisi_ref_p = true;
5646 /* If the type is a padded type and the underlying type would not be
5647 passed by reference or the function has a foreign convention, return
5648 the underlying type. */
5649 else if (TYPE_IS_PADDING_P (gnu_return_type)
5650 && (!default_pass_by_ref
5651 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5652 || Has_Foreign_Convention (gnat_subprog)))
5653 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5655 /* If the return type is unconstrained, it must have a maximum size.
5656 Use the padded type as the effective return type. And ensure the
5657 function uses the target's by-invisible-reference mechanism to
5658 avoid copying too much data when it returns. */
5659 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5661 tree orig_type = gnu_return_type;
5662 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5664 /* If the size overflows to 0, set it to an arbitrary positive
5665 value so that assignments in the type are preserved. Their
5666 actual size is independent of this positive value. */
5667 if (TREE_CODE (max_return_size) == INTEGER_CST
5668 && TREE_OVERFLOW (max_return_size)
5669 && integer_zerop (max_return_size))
5671 max_return_size = copy_node (bitsize_unit_node);
5672 TREE_OVERFLOW (max_return_size) = 1;
5675 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5676 0, gnat_subprog, false, false,
5677 definition, true);
5679 /* Declare it now since it will never be declared otherwise. This
5680 is necessary to ensure that its subtrees are properly marked. */
5681 if (gnu_return_type != orig_type
5682 && !DECL_P (TYPE_NAME (gnu_return_type)))
5683 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5684 true, debug_info_p, gnat_subprog);
5686 return_by_invisi_ref_p = true;
5689 /* If the return type has a size that overflows, we usually cannot have
5690 a function that returns that type. This usage doesn't really make
5691 sense anyway, so issue an error here. */
5692 if (!return_by_invisi_ref_p
5693 && TYPE_SIZE_UNIT (gnu_return_type)
5694 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5695 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5697 post_error ("cannot return type whose size overflows", gnat_subprog);
5698 gnu_return_type = copy_type (gnu_return_type);
5699 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5700 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5703 /* If the return type is incomplete, there are 2 cases: if the function
5704 returns by reference, then the return type is only linked indirectly
5705 in the profile, so the profile can be seen as complete since it need
5706 not be further modified, only the reference types need be adjusted;
5707 otherwise the profile is incomplete and need be adjusted too. */
5708 if (TYPE_IS_DUMMY_P (gnu_return_type))
5710 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5711 incomplete_profile_p = true;
5714 if (kind == E_Function)
5715 Set_Mechanism (gnat_subprog, return_unconstrained_p
5716 || return_by_direct_ref_p
5717 || return_by_invisi_ref_p
5718 ? By_Reference : By_Copy);
5721 /* A procedure (something that doesn't return anything) shouldn't be
5722 considered const since there would be no reason for calling such a
5723 subprogram. Note that procedures with Out (or In Out) parameters
5724 have already been converted into a function with a return type.
5725 Similarly, if the function returns an unconstrained type, then the
5726 function will allocate the return value on the secondary stack and
5727 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5728 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5729 const_flag = false;
5731 /* Loop over the parameters and get their associated GCC tree. While doing
5732 this, build a copy-in copy-out structure if we need one. */
5733 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5734 Present (gnat_param);
5735 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5737 const bool mech_is_by_ref
5738 = Mechanism (gnat_param) == By_Reference
5739 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5740 tree gnu_param_name = get_entity_name (gnat_param);
5741 tree gnu_param, gnu_param_type;
5742 bool cico = false;
5744 /* Fetch an existing parameter with complete type and reuse it. But we
5745 didn't save the CICO property so we can only do it for In parameters
5746 or parameters passed by reference. */
5747 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5748 && present_gnu_tree (gnat_param)
5749 && (gnu_param = get_gnu_tree (gnat_param))
5750 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5752 DECL_CHAIN (gnu_param) = NULL_TREE;
5753 gnu_param_type = TREE_TYPE (gnu_param);
5756 /* Otherwise translate the parameter type and act accordingly. */
5757 else
5759 Entity_Id gnat_param_type = Etype (gnat_param);
5761 /* For foreign convention subprograms, pass System.Address as void *
5762 or equivalent. Note that this comprises GCC builtins. */
5763 if (Has_Foreign_Convention (gnat_subprog)
5764 && Is_Descendant_Of_Address (Underlying_Type (gnat_param_type)))
5765 gnu_param_type = ptr_type_node;
5766 else
5767 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5769 /* If the parameter type is incomplete, there are 2 cases: if it is
5770 passed by reference, then the type is only linked indirectly in
5771 the profile, so the profile can be seen as complete since it need
5772 not be further modified, only the reference type need be adjusted;
5773 otherwise the profile is incomplete and need be adjusted too. */
5774 if (TYPE_IS_DUMMY_P (gnu_param_type))
5776 Node_Id gnat_decl;
5778 if (mech_is_by_ref
5779 || (TYPE_REFERENCE_TO (gnu_param_type)
5780 && TYPE_IS_FAT_POINTER_P
5781 (TYPE_REFERENCE_TO (gnu_param_type)))
5782 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5784 gnu_param_type = build_reference_type (gnu_param_type);
5785 gnu_param
5786 = create_param_decl (gnu_param_name, gnu_param_type);
5787 TREE_READONLY (gnu_param) = 1;
5788 DECL_BY_REF_P (gnu_param) = 1;
5789 DECL_POINTS_TO_READONLY_P (gnu_param)
5790 = (Ekind (gnat_param) == E_In_Parameter
5791 && !Address_Taken (gnat_param));
5792 Set_Mechanism (gnat_param, By_Reference);
5793 Sloc_to_locus (Sloc (gnat_param),
5794 &DECL_SOURCE_LOCATION (gnu_param));
5797 /* ??? This is a kludge to support null procedures in spec taking
5798 a parameter with an untagged incomplete type coming from a
5799 limited context. The front-end creates a body without knowing
5800 anything about the non-limited view, which is illegal Ada and
5801 cannot be supported. Create a parameter with a fake type. */
5802 else if (kind == E_Procedure
5803 && (gnat_decl = Parent (gnat_subprog))
5804 && Nkind (gnat_decl) == N_Procedure_Specification
5805 && Null_Present (gnat_decl)
5806 && Is_Incomplete_Type (gnat_param_type))
5807 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5809 else
5811 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
5812 Call_to_gnu will stop if it encounters the PARM_DECL. */
5813 gnu_param
5814 = build_decl (input_location, PARM_DECL, gnu_param_name,
5815 gnu_param_type);
5816 associate_subprog_with_dummy_type (gnat_subprog,
5817 gnu_param_type);
5818 incomplete_profile_p = true;
5822 /* Otherwise build the parameter declaration normally. */
5823 else
5825 gnu_param
5826 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
5827 gnat_subprog, &cico);
5829 /* We are returned either a PARM_DECL or a type if no parameter
5830 needs to be passed; in either case, adjust the type. */
5831 if (DECL_P (gnu_param))
5832 gnu_param_type = TREE_TYPE (gnu_param);
5833 else
5835 gnu_param_type = gnu_param;
5836 gnu_param = NULL_TREE;
5841 /* If we have a GCC tree for the parameter, register it. */
5842 save_gnu_tree (gnat_param, NULL_TREE, false);
5843 if (gnu_param)
5845 gnu_param_type_list
5846 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
5847 gnu_param_list = chainon (gnu_param, gnu_param_list);
5848 save_gnu_tree (gnat_param, gnu_param, false);
5850 /* If a parameter is a pointer, a function may modify memory through
5851 it and thus shouldn't be considered a const function. Also, the
5852 memory may be modified between two calls, so they can't be CSE'ed.
5853 The latter case also handles by-ref parameters. */
5854 if (POINTER_TYPE_P (gnu_param_type)
5855 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
5856 const_flag = false;
5859 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5860 for it in the return type and register the association. */
5861 if (cico && !incomplete_profile_p)
5863 if (!gnu_cico_list)
5865 gnu_cico_return_type = make_node (RECORD_TYPE);
5867 /* If this is a function, we also need a field for the
5868 return value to be placed. */
5869 if (!VOID_TYPE_P (gnu_return_type))
5871 tree gnu_field
5872 = create_field_decl (get_identifier ("RETVAL"),
5873 gnu_return_type,
5874 gnu_cico_return_type, NULL_TREE,
5875 NULL_TREE, 0, 0);
5876 Sloc_to_locus (Sloc (gnat_subprog),
5877 &DECL_SOURCE_LOCATION (gnu_field));
5878 gnu_field_list = gnu_field;
5879 gnu_cico_list
5880 = tree_cons (gnu_field, void_type_node, NULL_TREE);
5883 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
5884 /* Set a default alignment to speed up accesses. But we should
5885 not increase the size of the structure too much, lest it does
5886 not fit in return registers anymore. */
5887 SET_TYPE_ALIGN (gnu_cico_return_type,
5888 get_mode_alignment (ptr_mode));
5891 tree gnu_field
5892 = create_field_decl (gnu_param_name, gnu_param_type,
5893 gnu_cico_return_type, NULL_TREE, NULL_TREE,
5894 0, 0);
5895 Sloc_to_locus (Sloc (gnat_param),
5896 &DECL_SOURCE_LOCATION (gnu_field));
5897 DECL_CHAIN (gnu_field) = gnu_field_list;
5898 gnu_field_list = gnu_field;
5899 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
5903 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5904 and finish up the return type. */
5905 if (gnu_cico_list && !incomplete_profile_p)
5907 /* If we have a CICO list but it has only one entry, we convert
5908 this function into a function that returns this object. */
5909 if (list_length (gnu_cico_list) == 1)
5910 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
5912 /* Do not finalize the return type if the subprogram is stubbed
5913 since structures are incomplete for the back-end. */
5914 else if (Convention (gnat_subprog) != Convention_Stubbed)
5916 finish_record_type (gnu_cico_return_type, nreverse (gnu_field_list),
5917 0, false);
5919 /* Try to promote the mode of the return type if it is passed
5920 in registers, again to speed up accesses. */
5921 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
5922 && !targetm.calls.return_in_memory (gnu_cico_return_type,
5923 NULL_TREE))
5925 unsigned int size
5926 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
5927 unsigned int i = BITS_PER_UNIT;
5928 scalar_int_mode mode;
5930 while (i < size)
5931 i <<= 1;
5932 if (int_mode_for_size (i, 0).exists (&mode))
5934 SET_TYPE_MODE (gnu_cico_return_type, mode);
5935 SET_TYPE_ALIGN (gnu_cico_return_type,
5936 GET_MODE_ALIGNMENT (mode));
5937 TYPE_SIZE (gnu_cico_return_type)
5938 = bitsize_int (GET_MODE_BITSIZE (mode));
5939 TYPE_SIZE_UNIT (gnu_cico_return_type)
5940 = size_int (GET_MODE_SIZE (mode));
5944 if (debug_info_p)
5945 rest_of_record_type_compilation (gnu_cico_return_type);
5948 gnu_return_type = gnu_cico_return_type;
5951 /* The lists have been built in reverse. */
5952 gnu_param_type_list = nreverse (gnu_param_type_list);
5953 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
5954 *param_list = nreverse (gnu_param_list);
5955 gnu_cico_list = nreverse (gnu_cico_list);
5957 /* If the profile is incomplete, we only set the (temporary) return and
5958 parameter types; otherwise, we build the full type. In either case,
5959 we reuse an already existing GCC tree that we built previously here. */
5960 if (incomplete_profile_p)
5962 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5964 else
5965 gnu_type = make_node (FUNCTION_TYPE);
5966 TREE_TYPE (gnu_type) = gnu_return_type;
5967 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5968 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5969 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5970 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5972 else
5974 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5976 TREE_TYPE (gnu_type) = gnu_return_type;
5977 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5978 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5979 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5980 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5981 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5982 TYPE_CANONICAL (gnu_type) = gnu_type;
5983 layout_type (gnu_type);
5985 else
5987 gnu_type
5988 = build_function_type (gnu_return_type, gnu_param_type_list);
5990 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
5991 has a different TYPE_CI_CO_LIST or flags. */
5992 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
5993 return_unconstrained_p,
5994 return_by_direct_ref_p,
5995 return_by_invisi_ref_p))
5997 gnu_type = copy_type (gnu_type);
5998 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5999 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6000 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6001 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6005 if (const_flag)
6006 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6008 if (No_Return (gnat_subprog))
6009 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6011 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6012 corresponding DECL node and check the parameter association. */
6013 if (Convention (gnat_subprog) == Convention_Intrinsic
6014 && Present (Interface_Name (gnat_subprog)))
6016 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6017 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6019 /* If we have a builtin DECL for that function, use it. Check if
6020 the profiles are compatible and warn if they are not. Note that
6021 the checker is expected to post diagnostics in this case. */
6022 if (gnu_builtin_decl)
6024 intrin_binding_t inb
6025 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6027 if (!intrin_profiles_compatible_p (&inb))
6028 post_error
6029 ("?profile of& doesn''t match the builtin it binds!",
6030 gnat_subprog);
6032 return gnu_builtin_decl;
6035 /* Inability to find the builtin DECL most often indicates a genuine
6036 mistake, but imports of unregistered intrinsics are sometimes used
6037 on purpose to allow hooking in alternate bodies; we post a warning
6038 conditioned on Wshadow in this case, to let developers be notified
6039 on demand without risking false positives with common default sets
6040 of options. */
6041 if (warn_shadow)
6042 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6046 return gnu_type;
6049 /* Return the external name for GNAT_SUBPROG given its entity name. */
6051 static tree
6052 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6054 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6056 /* If there was no specified Interface_Name and the external and
6057 internal names of the subprogram are the same, only use the
6058 internal name to allow disambiguation of nested subprograms. */
6059 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6060 gnu_ext_name = NULL_TREE;
6062 return gnu_ext_name;
6065 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6066 qualifiers on TYPE. */
6068 static tree
6069 change_qualified_type (tree type, int type_quals)
6071 /* Qualifiers must be put on the associated array type. */
6072 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
6073 return type;
6075 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6078 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6079 build_nonshared_array_type. */
6081 static void
6082 set_nonaliased_component_on_array_type (tree type)
6084 TYPE_NONALIASED_COMPONENT (type) = 1;
6085 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6088 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6089 build_nonshared_array_type. */
6091 static void
6092 set_reverse_storage_order_on_array_type (tree type)
6094 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6095 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6098 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6100 static bool
6101 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6103 while (Present (Corresponding_Discriminant (discr1)))
6104 discr1 = Corresponding_Discriminant (discr1);
6106 while (Present (Corresponding_Discriminant (discr2)))
6107 discr2 = Corresponding_Discriminant (discr2);
6109 return
6110 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6113 /* Return true if the array type GNU_TYPE, which represents a dimension of
6114 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6116 static bool
6117 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6119 /* If the array type is not the innermost dimension of the GNAT type,
6120 then it has a non-aliased component. */
6121 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6122 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6123 return true;
6125 /* If the array type has an aliased component in the front-end sense,
6126 then it also has an aliased component in the back-end sense. */
6127 if (Has_Aliased_Components (gnat_type))
6128 return false;
6130 /* If this is a derived type, then it has a non-aliased component if
6131 and only if its parent type also has one. */
6132 if (Is_Derived_Type (gnat_type))
6134 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6135 int index;
6136 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6137 gnu_parent_type
6138 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6139 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6140 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6141 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6144 /* Otherwise, rely exclusively on properties of the element type. */
6145 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6148 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6150 static bool
6151 compile_time_known_address_p (Node_Id gnat_address)
6153 /* Handle reference to a constant. */
6154 if (Is_Entity_Name (gnat_address)
6155 && Ekind (Entity (gnat_address)) == E_Constant)
6157 gnat_address = Constant_Value (Entity (gnat_address));
6158 if (No (gnat_address))
6159 return false;
6162 /* Catch System'To_Address. */
6163 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6164 gnat_address = Expression (gnat_address);
6166 return Compile_Time_Known_Value (gnat_address);
6169 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6170 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6172 static bool
6173 cannot_be_superflat (Node_Id gnat_range)
6175 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6176 Node_Id scalar_range;
6177 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6179 /* If the low bound is not constant, try to find an upper bound. */
6180 while (Nkind (gnat_lb) != N_Integer_Literal
6181 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6182 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6183 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6184 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6185 || Nkind (scalar_range) == N_Range))
6186 gnat_lb = High_Bound (scalar_range);
6188 /* If the high bound is not constant, try to find a lower bound. */
6189 while (Nkind (gnat_hb) != N_Integer_Literal
6190 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6191 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6192 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6193 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6194 || Nkind (scalar_range) == N_Range))
6195 gnat_hb = Low_Bound (scalar_range);
6197 /* If we have failed to find constant bounds, punt. */
6198 if (Nkind (gnat_lb) != N_Integer_Literal
6199 || Nkind (gnat_hb) != N_Integer_Literal)
6200 return false;
6202 /* We need at least a signed 64-bit type to catch most cases. */
6203 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6204 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6205 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6206 return false;
6208 /* If the low bound is the smallest integer, nothing can be smaller. */
6209 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6210 if (TREE_OVERFLOW (gnu_lb_minus_one))
6211 return true;
6213 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6216 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6218 static bool
6219 constructor_address_p (tree gnu_expr)
6221 while (TREE_CODE (gnu_expr) == NOP_EXPR
6222 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6223 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6224 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6226 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6227 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6230 /* Return true if the size in units represented by GNU_SIZE can be handled by
6231 an allocation. If STATIC_P is true, consider only what can be done with a
6232 static allocation. */
6234 static bool
6235 allocatable_size_p (tree gnu_size, bool static_p)
6237 /* We can allocate a fixed size if it is a valid for the middle-end. */
6238 if (TREE_CODE (gnu_size) == INTEGER_CST)
6239 return valid_constant_size_p (gnu_size);
6241 /* We can allocate a variable size if this isn't a static allocation. */
6242 else
6243 return !static_p;
6246 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6247 initial value of an object of GNU_TYPE. */
6249 static bool
6250 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6252 /* Do not convert if the object's type is unconstrained because this would
6253 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6254 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6255 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6256 return false;
6258 /* Do not convert if the object's type is a padding record whose field is of
6259 self-referential size because we want to copy only the actual data. */
6260 if (type_is_padding_self_referential (gnu_type))
6261 return false;
6263 /* Do not convert a call to a function that returns with variable size since
6264 we want to use the return slot optimization in this case. */
6265 if (TREE_CODE (gnu_expr) == CALL_EXPR
6266 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6267 return false;
6269 /* Do not convert to a record type with a variant part from a record type
6270 without one, to keep the object simpler. */
6271 if (TREE_CODE (gnu_type) == RECORD_TYPE
6272 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6273 && get_variant_part (gnu_type)
6274 && !get_variant_part (TREE_TYPE (gnu_expr)))
6275 return false;
6277 /* In all the other cases, convert the expression to the object's type. */
6278 return true;
6281 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6282 be elaborated at the point of its definition, but do nothing else. */
6284 void
6285 elaborate_entity (Entity_Id gnat_entity)
6287 switch (Ekind (gnat_entity))
6289 case E_Signed_Integer_Subtype:
6290 case E_Modular_Integer_Subtype:
6291 case E_Enumeration_Subtype:
6292 case E_Ordinary_Fixed_Point_Subtype:
6293 case E_Decimal_Fixed_Point_Subtype:
6294 case E_Floating_Point_Subtype:
6296 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6297 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6299 /* ??? Tests to avoid Constraint_Error in static expressions
6300 are needed until after the front stops generating bogus
6301 conversions on bounds of real types. */
6302 if (!Raises_Constraint_Error (gnat_lb))
6303 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6304 Needs_Debug_Info (gnat_entity));
6305 if (!Raises_Constraint_Error (gnat_hb))
6306 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6307 Needs_Debug_Info (gnat_entity));
6308 break;
6311 case E_Record_Subtype:
6312 case E_Private_Subtype:
6313 case E_Limited_Private_Subtype:
6314 case E_Record_Subtype_With_Private:
6315 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6317 Node_Id gnat_discriminant_expr;
6318 Entity_Id gnat_field;
6320 for (gnat_field
6321 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6322 gnat_discriminant_expr
6323 = First_Elmt (Discriminant_Constraint (gnat_entity));
6324 Present (gnat_field);
6325 gnat_field = Next_Discriminant (gnat_field),
6326 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6327 /* Ignore access discriminants. */
6328 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6329 elaborate_expression (Node (gnat_discriminant_expr),
6330 gnat_entity, get_entity_char (gnat_field),
6331 true, false, false);
6333 break;
6338 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6339 NAME, ARGS and ERROR_POINT. */
6341 static void
6342 prepend_one_attribute (struct attrib **attr_list,
6343 enum attrib_type attrib_type,
6344 tree attr_name,
6345 tree attr_args,
6346 Node_Id attr_error_point)
6348 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6350 attr->type = attrib_type;
6351 attr->name = attr_name;
6352 attr->args = attr_args;
6353 attr->error_point = attr_error_point;
6355 attr->next = *attr_list;
6356 *attr_list = attr;
6359 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6361 static void
6362 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6364 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6365 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6366 enum attrib_type etype;
6368 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6369 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6371 case Pragma_Machine_Attribute:
6372 etype = ATTR_MACHINE_ATTRIBUTE;
6373 break;
6375 case Pragma_Linker_Alias:
6376 etype = ATTR_LINK_ALIAS;
6377 break;
6379 case Pragma_Linker_Section:
6380 etype = ATTR_LINK_SECTION;
6381 break;
6383 case Pragma_Linker_Constructor:
6384 etype = ATTR_LINK_CONSTRUCTOR;
6385 break;
6387 case Pragma_Linker_Destructor:
6388 etype = ATTR_LINK_DESTRUCTOR;
6389 break;
6391 case Pragma_Weak_External:
6392 etype = ATTR_WEAK_EXTERNAL;
6393 break;
6395 case Pragma_Thread_Local_Storage:
6396 etype = ATTR_THREAD_LOCAL_STORAGE;
6397 break;
6399 default:
6400 return;
6403 /* See what arguments we have and turn them into GCC trees for attribute
6404 handlers. These expect identifier for strings. We handle at most two
6405 arguments and static expressions only. */
6406 if (Present (gnat_arg) && Present (First (gnat_arg)))
6408 Node_Id gnat_arg0 = Next (First (gnat_arg));
6409 Node_Id gnat_arg1 = Empty;
6411 if (Present (gnat_arg0)
6412 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6414 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6416 if (TREE_CODE (gnu_arg0) == STRING_CST)
6418 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6419 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6420 return;
6423 gnat_arg1 = Next (gnat_arg0);
6426 if (Present (gnat_arg1)
6427 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6429 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6431 if (TREE_CODE (gnu_arg1) == STRING_CST)
6432 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6436 /* Prepend to the list. Make a list of the argument we might have, as GCC
6437 expects it. */
6438 prepend_one_attribute (attr_list, etype, gnu_arg0,
6439 gnu_arg1
6440 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6441 Present (Next (First (gnat_arg)))
6442 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6445 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6447 static void
6448 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6450 Node_Id gnat_temp;
6452 /* Attributes are stored as Representation Item pragmas. */
6453 for (gnat_temp = First_Rep_Item (gnat_entity);
6454 Present (gnat_temp);
6455 gnat_temp = Next_Rep_Item (gnat_temp))
6456 if (Nkind (gnat_temp) == N_Pragma)
6457 prepend_one_attribute_pragma (attr_list, gnat_temp);
6460 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6461 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6462 return the GCC tree to use for that expression. S is the suffix to use
6463 if a variable needs to be created and DEFINITION is true if this is done
6464 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6465 otherwise, we are just elaborating the expression for side-effects. If
6466 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6467 isn't needed for code generation. */
6469 static tree
6470 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6471 bool definition, bool need_value, bool need_debug)
6473 tree gnu_expr;
6475 /* If we already elaborated this expression (e.g. it was involved
6476 in the definition of a private type), use the old value. */
6477 if (present_gnu_tree (gnat_expr))
6478 return get_gnu_tree (gnat_expr);
6480 /* If we don't need a value and this is static or a discriminant,
6481 we don't need to do anything. */
6482 if (!need_value
6483 && (Is_OK_Static_Expression (gnat_expr)
6484 || (Nkind (gnat_expr) == N_Identifier
6485 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6486 return NULL_TREE;
6488 /* If it's a static expression, we don't need a variable for debugging. */
6489 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6490 need_debug = false;
6492 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6493 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6494 definition, need_debug);
6496 /* Save the expression in case we try to elaborate this entity again. Since
6497 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6498 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6499 save_gnu_tree (gnat_expr, gnu_expr, true);
6501 return need_value ? gnu_expr : error_mark_node;
6504 /* Similar, but take a GNU expression and always return a result. */
6506 static tree
6507 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6508 bool definition, bool need_debug)
6510 const bool expr_public_p = Is_Public (gnat_entity);
6511 const bool expr_global_p = expr_public_p || global_bindings_p ();
6512 bool expr_variable_p, use_variable;
6514 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6515 that an expression cannot contain both a discriminant and a variable. */
6516 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6517 return gnu_expr;
6519 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6520 a variable that is initialized to contain the expression when the package
6521 containing the definition is elaborated. If this entity is defined at top
6522 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6523 if this is necessary. */
6524 if (TREE_CONSTANT (gnu_expr))
6525 expr_variable_p = false;
6526 else
6528 /* Skip any conversions and simple constant arithmetics to see if the
6529 expression is based on a read-only variable. */
6530 tree inner = remove_conversions (gnu_expr, true);
6532 inner = skip_simple_constant_arithmetic (inner);
6534 if (handled_component_p (inner))
6535 inner = get_inner_constant_reference (inner);
6537 expr_variable_p
6538 = !(inner
6539 && TREE_CODE (inner) == VAR_DECL
6540 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6543 /* We only need to use the variable if we are in a global context since GCC
6544 can do the right thing in the local case. However, when not optimizing,
6545 use it for bounds of loop iteration scheme to avoid code duplication. */
6546 use_variable = expr_variable_p
6547 && (expr_global_p
6548 || (!optimize
6549 && definition
6550 && Is_Itype (gnat_entity)
6551 && Nkind (Associated_Node_For_Itype (gnat_entity))
6552 == N_Loop_Parameter_Specification));
6554 /* Now create it, possibly only for debugging purposes. */
6555 if (use_variable || need_debug)
6557 /* The following variable creation can happen when processing the body
6558 of subprograms that are defined out of the extended main unit and
6559 inlined. In this case, we are not at the global scope, and thus the
6560 new variable must not be tagged "external", as we used to do here as
6561 soon as DEFINITION was false. */
6562 tree gnu_decl
6563 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6564 TREE_TYPE (gnu_expr), gnu_expr, true,
6565 expr_public_p, !definition && expr_global_p,
6566 expr_global_p, false, true, need_debug,
6567 NULL, gnat_entity);
6569 /* Using this variable at debug time (if need_debug is true) requires a
6570 proper location. The back-end will compute a location for this
6571 variable only if the variable is used by the generated code.
6572 Returning the variable ensures the caller will use it in generated
6573 code. Note that there is no need for a location if the debug info
6574 contains an integer constant.
6575 TODO: when the encoding-based debug scheme is dropped, move this
6576 condition to the top-level IF block: we will not need to create a
6577 variable anymore in such cases, then. */
6578 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6579 return gnu_decl;
6582 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6585 /* Similar, but take an alignment factor and make it explicit in the tree. */
6587 static tree
6588 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6589 bool definition, bool need_debug, unsigned int align)
6591 tree unit_align = size_int (align / BITS_PER_UNIT);
6592 return
6593 size_binop (MULT_EXPR,
6594 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6595 gnu_expr,
6596 unit_align),
6597 gnat_entity, s, definition,
6598 need_debug),
6599 unit_align);
6602 /* Structure to hold internal data for elaborate_reference. */
6604 struct er_data
6606 Entity_Id entity;
6607 bool definition;
6608 unsigned int n;
6611 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6613 static tree
6614 elaborate_reference_1 (tree ref, void *data)
6616 struct er_data *er = (struct er_data *)data;
6617 char suffix[16];
6619 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6620 if (TREE_CONSTANT (ref))
6621 return ref;
6623 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6624 pointer. This may be more efficient, but will also allow us to more
6625 easily find the match for the PLACEHOLDER_EXPR. */
6626 if (TREE_CODE (ref) == COMPONENT_REF
6627 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6628 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6629 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6630 TREE_OPERAND (ref, 1), NULL_TREE);
6632 sprintf (suffix, "EXP%d", ++er->n);
6633 return
6634 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6637 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6638 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6639 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6641 static tree
6642 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6643 tree *init)
6645 struct er_data er = { gnat_entity, definition, 0 };
6646 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6649 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6650 the value passed against the list of choices. */
6652 static tree
6653 choices_to_gnu (tree operand, Node_Id choices)
6655 Node_Id choice;
6656 Node_Id gnat_temp;
6657 tree result = boolean_false_node;
6658 tree this_test, low = 0, high = 0, single = 0;
6660 for (choice = First (choices); Present (choice); choice = Next (choice))
6662 switch (Nkind (choice))
6664 case N_Range:
6665 low = gnat_to_gnu (Low_Bound (choice));
6666 high = gnat_to_gnu (High_Bound (choice));
6668 this_test
6669 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6670 build_binary_op (GE_EXPR, boolean_type_node,
6671 operand, low, true),
6672 build_binary_op (LE_EXPR, boolean_type_node,
6673 operand, high, true),
6674 true);
6676 break;
6678 case N_Subtype_Indication:
6679 gnat_temp = Range_Expression (Constraint (choice));
6680 low = gnat_to_gnu (Low_Bound (gnat_temp));
6681 high = gnat_to_gnu (High_Bound (gnat_temp));
6683 this_test
6684 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6685 build_binary_op (GE_EXPR, boolean_type_node,
6686 operand, low, true),
6687 build_binary_op (LE_EXPR, boolean_type_node,
6688 operand, high, true),
6689 true);
6690 break;
6692 case N_Identifier:
6693 case N_Expanded_Name:
6694 /* This represents either a subtype range, an enumeration
6695 literal, or a constant Ekind says which. If an enumeration
6696 literal or constant, fall through to the next case. */
6697 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6698 && Ekind (Entity (choice)) != E_Constant)
6700 tree type = gnat_to_gnu_type (Entity (choice));
6702 low = TYPE_MIN_VALUE (type);
6703 high = TYPE_MAX_VALUE (type);
6705 this_test
6706 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6707 build_binary_op (GE_EXPR, boolean_type_node,
6708 operand, low, true),
6709 build_binary_op (LE_EXPR, boolean_type_node,
6710 operand, high, true),
6711 true);
6712 break;
6715 /* ... fall through ... */
6717 case N_Character_Literal:
6718 case N_Integer_Literal:
6719 single = gnat_to_gnu (choice);
6720 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6721 single, true);
6722 break;
6724 case N_Others_Choice:
6725 this_test = boolean_true_node;
6726 break;
6728 default:
6729 gcc_unreachable ();
6732 if (result == boolean_false_node)
6733 result = this_test;
6734 else
6735 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6736 this_test, true);
6739 return result;
6742 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6743 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6745 static int
6746 adjust_packed (tree field_type, tree record_type, int packed)
6748 /* If the field contains an item of variable size, we cannot pack it
6749 because we cannot create temporaries of non-fixed size in case
6750 we need to take the address of the field. See addressable_p and
6751 the notes on the addressability issues for further details. */
6752 if (type_has_variable_size (field_type))
6753 return 0;
6755 /* In the other cases, we can honor the packing. */
6756 if (packed)
6757 return packed;
6759 /* If the alignment of the record is specified and the field type
6760 is over-aligned, request Storage_Unit alignment for the field. */
6761 if (TYPE_ALIGN (record_type)
6762 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6763 return -1;
6765 /* Likewise if the maximum alignment of the record is specified. */
6766 if (TYPE_MAX_ALIGN (record_type)
6767 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6768 return -1;
6770 return 0;
6773 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6774 placed in GNU_RECORD_TYPE.
6776 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6777 record has Component_Alignment of Storage_Unit.
6779 DEFINITION is true if this field is for a record being defined.
6781 DEBUG_INFO_P is true if we need to write debug information for types
6782 that we may create in the process. */
6784 static tree
6785 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6786 bool definition, bool debug_info_p)
6788 const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field));
6789 const Entity_Id gnat_field_type = Etype (gnat_field);
6790 const bool is_atomic
6791 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6792 const bool is_aliased = Is_Aliased (gnat_field);
6793 const bool is_independent
6794 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6795 const bool is_volatile
6796 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6797 const bool is_strict_alignment = Strict_Alignment (gnat_field_type);
6798 /* We used to consider that volatile fields also require strict alignment,
6799 but that was an interpolation and would cause us to reject a pragma
6800 volatile on a packed record type containing boolean components, while
6801 there is no basis to do so in the RM. In such cases, the writes will
6802 involve load-modify-store sequences, but that's OK for volatile. The
6803 only constraint is the implementation advice whereby only the bits of
6804 the components should be accessed if they both start and end on byte
6805 boundaries, but that should be guaranteed by the GCC memory model. */
6806 const bool needs_strict_alignment
6807 = (is_atomic || is_aliased || is_independent || is_strict_alignment);
6808 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6809 tree gnu_field_id = get_entity_name (gnat_field);
6810 tree gnu_field, gnu_size, gnu_pos;
6812 /* If this field requires strict alignment, we cannot pack it because
6813 it would very likely be under-aligned in the record. */
6814 if (needs_strict_alignment)
6815 packed = 0;
6816 else
6817 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6819 /* If a size is specified, use it. Otherwise, if the record type is packed,
6820 use the official RM size. See "Handling of Type'Size Values" in Einfo
6821 for further details. */
6822 if (Known_Esize (gnat_field))
6823 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6824 gnat_field, FIELD_DECL, false, true);
6825 else if (packed == 1)
6826 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6827 gnat_field, FIELD_DECL, false, true);
6828 else
6829 gnu_size = NULL_TREE;
6831 /* If we have a specified size that is smaller than that of the field's type,
6832 or a position is specified, and the field's type is a record that doesn't
6833 require strict alignment, see if we can get either an integral mode form
6834 of the type or a smaller form. If we can, show a size was specified for
6835 the field if there wasn't one already, so we know to make this a bitfield
6836 and avoid making things wider.
6838 Changing to an integral mode form is useful when the record is packed as
6839 we can then place the field at a non-byte-aligned position and so achieve
6840 tighter packing. This is in addition required if the field shares a byte
6841 with another field and the front-end lets the back-end handle the access
6842 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6844 Changing to a smaller form is required if the specified size is smaller
6845 than that of the field's type and the type contains sub-fields that are
6846 padded, in order to avoid generating accesses to these sub-fields that
6847 are wider than the field.
6849 We avoid the transformation if it is not required or potentially useful,
6850 as it might entail an increase of the field's alignment and have ripple
6851 effects on the outer record type. A typical case is a field known to be
6852 byte-aligned and not to share a byte with another field. */
6853 if (!needs_strict_alignment
6854 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6855 && !TYPE_FAT_POINTER_P (gnu_field_type)
6856 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6857 && (packed == 1
6858 || (gnu_size
6859 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6860 || (Present (Component_Clause (gnat_field))
6861 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6862 % BITS_PER_UNIT == 0
6863 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6865 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6866 if (gnu_packable_type != gnu_field_type)
6868 gnu_field_type = gnu_packable_type;
6869 if (!gnu_size)
6870 gnu_size = rm_size (gnu_field_type);
6874 if (Is_Atomic_Or_VFA (gnat_field))
6876 const unsigned int align
6877 = promote_object_alignment (gnu_field_type, gnat_field);
6878 if (align > 0)
6879 gnu_field_type
6880 = maybe_pad_type (gnu_field_type, NULL_TREE, align, gnat_field,
6881 false, false, definition, true);
6882 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6885 if (Present (Component_Clause (gnat_field)))
6887 Node_Id gnat_clause = Component_Clause (gnat_field);
6888 Entity_Id gnat_parent = Parent_Subtype (gnat_record_type);
6890 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6891 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6892 gnat_field, FIELD_DECL, false, true);
6894 /* Ensure the position does not overlap with the parent subtype, if there
6895 is one. This test is omitted if the parent of the tagged type has a
6896 full rep clause since, in this case, component clauses are allowed to
6897 overlay the space allocated for the parent type and the front-end has
6898 checked that there are no overlapping components. */
6899 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6901 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6903 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6904 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6905 post_error_ne_tree
6906 ("offset of& must be beyond parent{, minimum allowed is ^}",
6907 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6910 /* If this field needs strict alignment, make sure that the record is
6911 sufficiently aligned and that the position and size are consistent
6912 with the type. But don't do it if we are just annotating types and
6913 the field's type is tagged, since tagged types aren't fully laid out
6914 in this mode. Also, note that atomic implies volatile so the inner
6915 test sequences ordering is significant here. */
6916 if (needs_strict_alignment
6917 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6919 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6921 if (TYPE_ALIGN (gnu_record_type) < type_align)
6922 SET_TYPE_ALIGN (gnu_record_type, type_align);
6924 /* If the position is not a multiple of the alignment of the type,
6925 then error out and reset the position. */
6926 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6927 bitsize_int (type_align))))
6929 const char *s;
6931 if (is_atomic)
6932 s = "position of atomic field& must be multiple of ^ bits";
6933 else if (is_aliased)
6934 s = "position of aliased field& must be multiple of ^ bits";
6935 else if (is_independent)
6936 s = "position of independent field& must be multiple of ^ bits";
6937 else if (is_strict_alignment)
6938 s = "position of & with aliased or tagged part must be"
6939 " multiple of ^ bits";
6940 else
6941 gcc_unreachable ();
6943 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6944 type_align);
6945 gnu_pos = NULL_TREE;
6948 if (gnu_size)
6950 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6951 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6953 /* If the size is lower than that of the type, or greater for
6954 atomic and aliased, then error out and reset the size. */
6955 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6957 const char *s;
6959 if (is_atomic)
6960 s = "size of atomic field& must be ^ bits";
6961 else if (is_aliased)
6962 s = "size of aliased field& must be ^ bits";
6963 else if (is_independent)
6964 s = "size of independent field& must be at least ^ bits";
6965 else if (is_strict_alignment)
6966 s = "size of & with aliased or tagged part must be"
6967 " at least ^ bits";
6968 else
6969 gcc_unreachable ();
6971 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6972 gnu_type_size);
6973 gnu_size = NULL_TREE;
6976 /* Likewise if the size is not a multiple of a byte, */
6977 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6978 bitsize_unit_node)))
6980 const char *s;
6982 if (is_independent)
6983 s = "size of independent field& must be multiple of"
6984 " Storage_Unit";
6985 else if (is_strict_alignment)
6986 s = "size of & with aliased or tagged part must be"
6987 " multiple of Storage_Unit";
6988 else
6989 gcc_unreachable ();
6991 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6992 gnu_size = NULL_TREE;
6998 /* If the record has rep clauses and this is the tag field, make a rep
6999 clause for it as well. */
7000 else if (Has_Specified_Layout (gnat_record_type)
7001 && Chars (gnat_field) == Name_uTag)
7003 gnu_pos = bitsize_zero_node;
7004 gnu_size = TYPE_SIZE (gnu_field_type);
7007 else
7009 gnu_pos = NULL_TREE;
7011 /* If we are packing the record and the field is BLKmode, round the
7012 size up to a byte boundary. */
7013 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7014 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7017 /* We need to make the size the maximum for the type if it is
7018 self-referential and an unconstrained type. In that case, we can't
7019 pack the field since we can't make a copy to align it. */
7020 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7021 && !gnu_size
7022 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7023 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7025 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7026 packed = 0;
7029 /* If a size is specified, adjust the field's type to it. */
7030 if (gnu_size)
7032 tree orig_field_type;
7034 /* If the field's type is justified modular, we would need to remove
7035 the wrapper to (better) meet the layout requirements. However we
7036 can do so only if the field is not aliased to preserve the unique
7037 layout, if it has the same storage order as the enclosing record
7038 and if the prescribed size is not greater than that of the packed
7039 array to preserve the justification. */
7040 if (!needs_strict_alignment
7041 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7042 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7043 && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type)
7044 == Reverse_Storage_Order (gnat_record_type)
7045 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7046 <= 0)
7047 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7049 /* Similarly if the field's type is a misaligned integral type, but
7050 there is no restriction on the size as there is no justification. */
7051 if (!needs_strict_alignment
7052 && TYPE_IS_PADDING_P (gnu_field_type)
7053 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7054 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7056 gnu_field_type
7057 = make_type_from_size (gnu_field_type, gnu_size,
7058 Has_Biased_Representation (gnat_field));
7060 orig_field_type = gnu_field_type;
7061 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7062 false, false, definition, true);
7064 /* If a padding record was made, declare it now since it will never be
7065 declared otherwise. This is necessary to ensure that its subtrees
7066 are properly marked. */
7067 if (gnu_field_type != orig_field_type
7068 && !DECL_P (TYPE_NAME (gnu_field_type)))
7069 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7070 debug_info_p, gnat_field);
7073 /* Otherwise (or if there was an error), don't specify a position. */
7074 else
7075 gnu_pos = NULL_TREE;
7077 /* If the field's type is a padded type made for a scalar field of a record
7078 type with reverse storage order, we need to propagate the reverse storage
7079 order to the padding type since it is the innermost enclosing aggregate
7080 type around the scalar. */
7081 if (TYPE_IS_PADDING_P (gnu_field_type)
7082 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7083 && Is_Scalar_Type (gnat_field_type))
7084 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7086 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7087 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7089 /* Now create the decl for the field. */
7090 gnu_field
7091 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7092 gnu_size, gnu_pos, packed, is_aliased);
7093 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7094 DECL_ALIASED_P (gnu_field) = is_aliased;
7095 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7097 if (Ekind (gnat_field) == E_Discriminant)
7099 DECL_INVARIANT_P (gnu_field)
7100 = No (Discriminant_Default_Value (gnat_field));
7101 DECL_DISCRIMINANT_NUMBER (gnu_field)
7102 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7105 return gnu_field;
7108 /* Return true if at least one member of COMPONENT_LIST needs strict
7109 alignment. */
7111 static bool
7112 components_need_strict_alignment (Node_Id component_list)
7114 Node_Id component_decl;
7116 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7117 Present (component_decl);
7118 component_decl = Next_Non_Pragma (component_decl))
7120 Entity_Id gnat_field = Defining_Entity (component_decl);
7122 if (Is_Aliased (gnat_field))
7123 return true;
7125 if (Strict_Alignment (Etype (gnat_field)))
7126 return true;
7129 return false;
7132 /* Return true if TYPE is a type with variable size or a padding type with a
7133 field of variable size or a record that has a field with such a type. */
7135 static bool
7136 type_has_variable_size (tree type)
7138 tree field;
7140 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7141 return true;
7143 if (TYPE_IS_PADDING_P (type)
7144 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7145 return true;
7147 if (!RECORD_OR_UNION_TYPE_P (type))
7148 return false;
7150 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7151 if (type_has_variable_size (TREE_TYPE (field)))
7152 return true;
7154 return false;
7157 /* Return true if FIELD is an artificial field. */
7159 static bool
7160 field_is_artificial (tree field)
7162 /* These fields are generated by the front-end proper. */
7163 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7164 return true;
7166 /* These fields are generated by gigi. */
7167 if (DECL_INTERNAL_P (field))
7168 return true;
7170 return false;
7173 /* Return true if FIELD is a non-artificial field with self-referential
7174 size. */
7176 static bool
7177 field_has_self_size (tree field)
7179 if (field_is_artificial (field))
7180 return false;
7182 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7183 return false;
7185 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7188 /* Return true if FIELD is a non-artificial field with variable size. */
7190 static bool
7191 field_has_variable_size (tree field)
7193 if (field_is_artificial (field))
7194 return false;
7196 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7197 return false;
7199 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7202 /* qsort comparer for the bit positions of two record components. */
7204 static int
7205 compare_field_bitpos (const PTR rt1, const PTR rt2)
7207 const_tree const field1 = * (const_tree const *) rt1;
7208 const_tree const field2 = * (const_tree const *) rt2;
7209 const int ret
7210 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7212 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7215 /* Reverse function from gnat_to_gnu_field: return the GNAT field present in
7216 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and
7217 corresponding to the GNU tree GNU_FIELD. */
7219 static Entity_Id
7220 gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list,
7221 Entity_Id gnat_record_type)
7223 Entity_Id gnat_component_decl, gnat_field;
7225 if (Present (Component_Items (gnat_component_list)))
7226 for (gnat_component_decl
7227 = First_Non_Pragma (Component_Items (gnat_component_list));
7228 Present (gnat_component_decl);
7229 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7231 gnat_field = Defining_Entity (gnat_component_decl);
7232 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7233 return gnat_field;
7236 if (Has_Discriminants (gnat_record_type))
7237 for (gnat_field = First_Stored_Discriminant (gnat_record_type);
7238 Present (gnat_field);
7239 gnat_field = Next_Stored_Discriminant (gnat_field))
7240 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7241 return gnat_field;
7243 return Empty;
7246 /* Issue a warning for the problematic placement of GNU_FIELD present in
7247 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE.
7248 IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant.
7249 DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */
7251 static void
7252 warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list,
7253 Entity_Id gnat_record_type, bool in_variant,
7254 bool do_reorder)
7256 if (!Comes_From_Source (gnat_record_type))
7257 return;
7259 const char *msg1
7260 = in_variant
7261 ? "?variant layout may cause performance issues"
7262 : "?record layout may cause performance issues";
7263 const char *msg2
7264 = field_has_self_size (gnu_field)
7265 ? "?component & whose length depends on a discriminant"
7266 : field_has_variable_size (gnu_field)
7267 ? "?component & whose length is not fixed"
7268 : "?component & whose length is not multiple of a byte";
7269 const char *msg3
7270 = do_reorder
7271 ? "?comes too early and was moved down"
7272 : "?comes too early and ought to be moved down";
7274 Entity_Id gnat_field
7275 = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type);
7277 gcc_assert (Present (gnat_field));
7279 post_error (msg1, gnat_field);
7280 post_error_ne (msg2, gnat_field, gnat_field);
7281 post_error (msg3, gnat_field);
7284 /* Structure holding information for a given variant. */
7285 typedef struct vinfo
7287 /* The record type of the variant. */
7288 tree type;
7290 /* The name of the variant. */
7291 tree name;
7293 /* The qualifier of the variant. */
7294 tree qual;
7296 /* Whether the variant has a rep clause. */
7297 bool has_rep;
7299 /* Whether the variant is packed. */
7300 bool packed;
7302 } vinfo_t;
7304 /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to
7305 GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and
7306 finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects
7307 the layout (see below). When called from gnat_to_gnu_entity during the
7308 processing of a record definition, the GCC node for the parent, if any,
7309 will be the single field of GNU_RECORD_TYPE and the GCC nodes for the
7310 discriminants will be on GNU_FIELD_LIST. The other call to this function
7311 is a recursive call for the component list of a variant and, in this case,
7312 GNU_FIELD_LIST is empty.
7314 PACKED is 1 if this is for a packed record or -1 if this is for a record
7315 with Component_Alignment of Storage_Unit.
7317 DEFINITION is true if we are defining this record type.
7319 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7320 out the record. This means the alignment only serves to force fields to
7321 be bitfields, but not to require the record to be that aligned. This is
7322 used for variants.
7324 ALL_REP is true if a rep clause is present for all the fields.
7326 UNCHECKED_UNION is true if we are building this type for a record with a
7327 Pragma Unchecked_Union.
7329 ARTIFICIAL is true if this is a type that was generated by the compiler.
7331 DEBUG_INFO is true if we need to write debug information about the type.
7333 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7334 mean that its contents may be unused as well, only the container itself.
7336 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7337 the outer record type down to this variant level. It is nonzero only if
7338 all the fields down to this level have a rep clause and ALL_REP is false.
7340 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7341 with a rep clause is to be added; in this case, that is all that should
7342 be done with such fields and the return value will be false. */
7344 static bool
7345 components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type,
7346 tree gnu_field_list, tree gnu_record_type, int packed,
7347 bool definition, bool cancel_alignment, bool all_rep,
7348 bool unchecked_union, bool artificial, bool debug_info,
7349 bool maybe_unused, tree first_free_pos,
7350 tree *p_gnu_rep_list)
7352 const bool needs_xv_encodings
7353 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7354 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7355 bool variants_have_rep = all_rep;
7356 bool layout_with_rep = false;
7357 bool has_self_field = false;
7358 bool has_aliased_after_self_field = false;
7359 Entity_Id gnat_component_decl, gnat_variant_part;
7360 tree gnu_field, gnu_next, gnu_last;
7361 tree gnu_variant_part = NULL_TREE;
7362 tree gnu_rep_list = NULL_TREE;
7364 /* For each component referenced in a component declaration create a GCC
7365 field and add it to the list, skipping pragmas in the GNAT list. */
7366 gnu_last = tree_last (gnu_field_list);
7367 if (Present (Component_Items (gnat_component_list)))
7368 for (gnat_component_decl
7369 = First_Non_Pragma (Component_Items (gnat_component_list));
7370 Present (gnat_component_decl);
7371 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7373 Entity_Id gnat_field = Defining_Entity (gnat_component_decl);
7374 Name_Id gnat_name = Chars (gnat_field);
7376 /* If present, the _Parent field must have been created as the single
7377 field of the record type. Put it before any other fields. */
7378 if (gnat_name == Name_uParent)
7380 gnu_field = TYPE_FIELDS (gnu_record_type);
7381 gnu_field_list = chainon (gnu_field_list, gnu_field);
7383 else
7385 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7386 definition, debug_info);
7388 /* If this is the _Tag field, put it before any other fields. */
7389 if (gnat_name == Name_uTag)
7390 gnu_field_list = chainon (gnu_field_list, gnu_field);
7392 /* If this is the _Controller field, put it before the other
7393 fields except for the _Tag or _Parent field. */
7394 else if (gnat_name == Name_uController && gnu_last)
7396 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7397 DECL_CHAIN (gnu_last) = gnu_field;
7400 /* If this is a regular field, put it after the other fields. */
7401 else
7403 DECL_CHAIN (gnu_field) = gnu_field_list;
7404 gnu_field_list = gnu_field;
7405 if (!gnu_last)
7406 gnu_last = gnu_field;
7408 /* And record information for the final layout. */
7409 if (field_has_self_size (gnu_field))
7410 has_self_field = true;
7411 else if (has_self_field && DECL_ALIASED_P (gnu_field))
7412 has_aliased_after_self_field = true;
7416 save_gnu_tree (gnat_field, gnu_field, false);
7419 /* At the end of the component list there may be a variant part. */
7420 gnat_variant_part = Variant_Part (gnat_component_list);
7422 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7423 mutually exclusive and should go in the same memory. To do this we need
7424 to treat each variant as a record whose elements are created from the
7425 component list for the variant. So here we create the records from the
7426 lists for the variants and put them all into the QUAL_UNION_TYPE.
7427 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7428 use GNU_RECORD_TYPE if there are no fields so far. */
7429 if (Present (gnat_variant_part))
7431 Node_Id gnat_discr = Name (gnat_variant_part), variant;
7432 tree gnu_discr = gnat_to_gnu (gnat_discr);
7433 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7434 tree gnu_var_name
7435 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7436 "XVN");
7437 tree gnu_union_type, gnu_union_name;
7438 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7439 bool union_field_needs_strict_alignment = false;
7440 auto_vec <vinfo_t, 16> variant_types;
7441 vinfo_t *gnu_variant;
7442 unsigned int variants_align = 0;
7443 unsigned int i;
7445 gnu_union_name
7446 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7448 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7449 are all in the variant part, to match the layout of C unions. There
7450 is an associated check below. */
7451 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7452 gnu_union_type = gnu_record_type;
7453 else
7455 gnu_union_type
7456 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7458 TYPE_NAME (gnu_union_type) = gnu_union_name;
7459 SET_TYPE_ALIGN (gnu_union_type, 0);
7460 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7461 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7462 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7465 /* If all the fields down to this level have a rep clause, find out
7466 whether all the fields at this level also have one. If so, then
7467 compute the new first free position to be passed downward. */
7468 this_first_free_pos = first_free_pos;
7469 if (this_first_free_pos)
7471 for (gnu_field = gnu_field_list;
7472 gnu_field;
7473 gnu_field = DECL_CHAIN (gnu_field))
7474 if (DECL_FIELD_OFFSET (gnu_field))
7476 tree pos = bit_position (gnu_field);
7477 if (!tree_int_cst_lt (pos, this_first_free_pos))
7478 this_first_free_pos
7479 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7481 else
7483 this_first_free_pos = NULL_TREE;
7484 break;
7488 /* We build the variants in two passes. The bulk of the work is done in
7489 the first pass, that is to say translating the GNAT nodes, building
7490 the container types and computing the associated properties. However
7491 we cannot finish up the container types during this pass because we
7492 don't know where the variant part will be placed until the end. */
7493 for (variant = First_Non_Pragma (Variants (gnat_variant_part));
7494 Present (variant);
7495 variant = Next_Non_Pragma (variant))
7497 tree gnu_variant_type = make_node (RECORD_TYPE);
7498 tree gnu_inner_name, gnu_qual;
7499 bool has_rep;
7500 int field_packed;
7501 vinfo_t vinfo;
7503 Get_Variant_Encoding (variant);
7504 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7505 TYPE_NAME (gnu_variant_type)
7506 = concat_name (gnu_union_name,
7507 IDENTIFIER_POINTER (gnu_inner_name));
7509 /* Set the alignment of the inner type in case we need to make
7510 inner objects into bitfields, but then clear it out so the
7511 record actually gets only the alignment required. */
7512 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7513 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7514 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7515 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7517 /* Similarly, if the outer record has a size specified and all
7518 the fields have a rep clause, we can propagate the size. */
7519 if (all_rep_and_size)
7521 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7522 TYPE_SIZE_UNIT (gnu_variant_type)
7523 = TYPE_SIZE_UNIT (gnu_record_type);
7526 /* Add the fields into the record type for the variant. Note that
7527 we aren't sure to really use it at this point, see below. */
7528 has_rep
7529 = components_to_record (Component_List (variant), gnat_record_type,
7530 NULL_TREE, gnu_variant_type, packed,
7531 definition, !all_rep_and_size, all_rep,
7532 unchecked_union, true, needs_xv_encodings,
7533 true, this_first_free_pos,
7534 all_rep || this_first_free_pos
7535 ? NULL : &gnu_rep_list);
7537 /* Translate the qualifier and annotate the GNAT node. */
7538 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7539 Set_Present_Expr (variant, annotate_value (gnu_qual));
7541 /* Deal with packedness like in gnat_to_gnu_field. */
7542 if (components_need_strict_alignment (Component_List (variant)))
7544 field_packed = 0;
7545 union_field_needs_strict_alignment = true;
7547 else
7548 field_packed
7549 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7551 /* Push this variant onto the stack for the second pass. */
7552 vinfo.type = gnu_variant_type;
7553 vinfo.name = gnu_inner_name;
7554 vinfo.qual = gnu_qual;
7555 vinfo.has_rep = has_rep;
7556 vinfo.packed = field_packed;
7557 variant_types.safe_push (vinfo);
7559 /* Compute the global properties that will determine the placement of
7560 the variant part. */
7561 variants_have_rep |= has_rep;
7562 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7563 variants_align = TYPE_ALIGN (gnu_variant_type);
7566 /* Round up the first free position to the alignment of the variant part
7567 for the variants without rep clause. This will guarantee a consistent
7568 layout independently of the placement of the variant part. */
7569 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7570 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7572 /* In the second pass, the container types are adjusted if necessary and
7573 finished up, then the corresponding fields of the variant part are
7574 built with their qualifier, unless this is an unchecked union. */
7575 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7577 tree gnu_variant_type = gnu_variant->type;
7578 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7580 /* If this is an Unchecked_Union whose fields are all in the variant
7581 part and we have a single field with no representation clause or
7582 placed at offset zero, use the field directly to match the layout
7583 of C unions. */
7584 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7585 && gnu_field_list
7586 && !DECL_CHAIN (gnu_field_list)
7587 && (!DECL_FIELD_OFFSET (gnu_field_list)
7588 || integer_zerop (bit_position (gnu_field_list))))
7590 gnu_field = gnu_field_list;
7591 DECL_CONTEXT (gnu_field) = gnu_record_type;
7593 else
7595 /* Finalize the variant type now. We used to throw away empty
7596 record types but we no longer do that because we need them to
7597 generate complete debug info for the variant; otherwise, the
7598 union type definition will be lacking the fields associated
7599 with these empty variants. */
7600 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7602 /* The variant part will be at offset 0 so we need to ensure
7603 that the fields are laid out starting from the first free
7604 position at this level. */
7605 tree gnu_rep_type = make_node (RECORD_TYPE);
7606 tree gnu_rep_part;
7607 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7608 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7609 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7610 gnu_rep_part
7611 = create_rep_part (gnu_rep_type, gnu_variant_type,
7612 this_first_free_pos);
7613 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7614 gnu_field_list = gnu_rep_part;
7615 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7616 false);
7619 if (debug_info)
7620 rest_of_record_type_compilation (gnu_variant_type);
7621 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7622 true, needs_xv_encodings, gnat_component_list);
7624 gnu_field
7625 = create_field_decl (gnu_variant->name, gnu_variant_type,
7626 gnu_union_type,
7627 all_rep_and_size
7628 ? TYPE_SIZE (gnu_variant_type) : 0,
7629 variants_have_rep ? bitsize_zero_node : 0,
7630 gnu_variant->packed, 0);
7632 DECL_INTERNAL_P (gnu_field) = 1;
7634 if (!unchecked_union)
7635 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7638 DECL_CHAIN (gnu_field) = gnu_variant_list;
7639 gnu_variant_list = gnu_field;
7642 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7643 if (gnu_variant_list)
7645 int union_field_packed;
7647 if (all_rep_and_size)
7649 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7650 TYPE_SIZE_UNIT (gnu_union_type)
7651 = TYPE_SIZE_UNIT (gnu_record_type);
7654 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7655 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7657 /* If GNU_UNION_TYPE is our record type, it means we must have an
7658 Unchecked_Union with no fields. Verify that and, if so, just
7659 return. */
7660 if (gnu_union_type == gnu_record_type)
7662 gcc_assert (unchecked_union
7663 && !gnu_field_list
7664 && !gnu_rep_list);
7665 return variants_have_rep;
7668 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7669 needs_xv_encodings, gnat_component_list);
7671 /* Deal with packedness like in gnat_to_gnu_field. */
7672 if (union_field_needs_strict_alignment)
7673 union_field_packed = 0;
7674 else
7675 union_field_packed
7676 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7678 gnu_variant_part
7679 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7680 all_rep_and_size
7681 ? TYPE_SIZE (gnu_union_type) : 0,
7682 variants_have_rep ? bitsize_zero_node : 0,
7683 union_field_packed, 0);
7685 DECL_INTERNAL_P (gnu_variant_part) = 1;
7689 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do,
7690 pull them out and put them onto the appropriate list.
7692 Similarly, pull out the fields with zero size and no rep clause, as they
7693 would otherwise modify the layout and thus very likely run afoul of the
7694 Ada semantics, which are different from those of C here.
7696 Finally, if there is an aliased field placed in the list after fields
7697 with self-referential size, pull out the latter in the same way.
7699 Optionally, if the reordering mechanism is enabled, pull out the fields
7700 with self-referential size, variable size and fixed size not a multiple
7701 of a byte, so that they don't cause the regular fields to be either at
7702 self-referential/variable offset or misaligned. Note, in the latter
7703 case, that this can only happen in packed record types so the alignment
7704 is effectively capped to the byte for the whole record. But we don't
7705 do it for non-packed record types if pragma Optimize_Alignment (Space)
7706 is specified because this can prevent alignment gaps from being filled.
7708 Optionally, if the layout warning is enabled, keep track of the above 4
7709 different kinds of fields and issue a warning if some of them would be
7710 (or are being) reordered by the reordering mechanism.
7712 ??? If we reorder fields, the debugging information will be affected and
7713 the debugger print fields in a different order from the source code. */
7714 const bool do_reorder
7715 = (Convention (gnat_record_type) == Convention_Ada
7716 && !No_Reordering (gnat_record_type)
7717 && (!Optimize_Alignment_Space (gnat_record_type)
7718 || Is_Packed (gnat_record_type))
7719 && !debug__debug_flag_dot_r);
7720 const bool w_reorder
7721 = (Convention (gnat_record_type) == Convention_Ada
7722 && Warn_On_Questionable_Layout
7723 && !(No_Reordering (gnat_record_type) && GNAT_Mode));
7724 const bool in_variant = (p_gnu_rep_list != NULL);
7725 tree gnu_zero_list = NULL_TREE;
7726 tree gnu_self_list = NULL_TREE;
7727 tree gnu_var_list = NULL_TREE;
7728 tree gnu_bitp_list = NULL_TREE;
7729 tree gnu_tmp_bitp_list = NULL_TREE;
7730 unsigned int tmp_bitp_size = 0;
7731 unsigned int last_reorder_field_type = -1;
7732 unsigned int tmp_last_reorder_field_type = -1;
7734 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7735 do { \
7736 if (gnu_last) \
7737 DECL_CHAIN (gnu_last) = gnu_next; \
7738 else \
7739 gnu_field_list = gnu_next; \
7741 DECL_CHAIN (gnu_field) = (LIST); \
7742 (LIST) = gnu_field; \
7743 } while (0)
7745 gnu_last = NULL_TREE;
7746 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7748 gnu_next = DECL_CHAIN (gnu_field);
7750 if (DECL_FIELD_OFFSET (gnu_field))
7752 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7753 continue;
7756 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7758 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7759 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7760 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7761 if (DECL_ALIASED_P (gnu_field))
7762 SET_TYPE_ALIGN (gnu_record_type,
7763 MAX (TYPE_ALIGN (gnu_record_type),
7764 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7765 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7766 continue;
7769 if (has_aliased_after_self_field && field_has_self_size (gnu_field))
7771 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7772 continue;
7775 /* We don't need further processing in default mode. */
7776 if (!w_reorder && !do_reorder)
7778 gnu_last = gnu_field;
7779 continue;
7782 if (field_has_self_size (gnu_field))
7784 if (w_reorder)
7786 if (last_reorder_field_type < 4)
7787 warn_on_field_placement (gnu_field, gnat_component_list,
7788 gnat_record_type, in_variant,
7789 do_reorder);
7790 else
7791 last_reorder_field_type = 4;
7794 if (do_reorder)
7796 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7797 continue;
7801 else if (field_has_variable_size (gnu_field))
7803 if (w_reorder)
7805 if (last_reorder_field_type < 3)
7806 warn_on_field_placement (gnu_field, gnat_component_list,
7807 gnat_record_type, in_variant,
7808 do_reorder);
7809 else
7810 last_reorder_field_type = 3;
7813 if (do_reorder)
7815 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7816 continue;
7820 else
7822 /* If the field has no size, then it cannot be bit-packed. */
7823 const unsigned int bitp_size
7824 = DECL_SIZE (gnu_field)
7825 ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT
7826 : 0;
7828 /* If the field is bit-packed, we move it to a temporary list that
7829 contains the contiguously preceding bit-packed fields, because
7830 we want to be able to put them back if the misalignment happens
7831 to cancel itself after several bit-packed fields. */
7832 if (bitp_size != 0)
7834 tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT;
7836 if (last_reorder_field_type != 2)
7838 tmp_last_reorder_field_type = last_reorder_field_type;
7839 last_reorder_field_type = 2;
7842 if (do_reorder)
7844 MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list);
7845 continue;
7849 /* No more bit-packed fields, move the existing ones to the end or
7850 put them back at their original location. */
7851 else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list)
7853 last_reorder_field_type = 1;
7855 if (tmp_bitp_size != 0)
7857 if (w_reorder && tmp_last_reorder_field_type < 2)
7858 warn_on_field_placement (gnu_tmp_bitp_list
7859 ? gnu_tmp_bitp_list : gnu_last,
7860 gnat_component_list,
7861 gnat_record_type, in_variant,
7862 do_reorder);
7864 if (do_reorder)
7865 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7867 gnu_tmp_bitp_list = NULL_TREE;
7868 tmp_bitp_size = 0;
7870 else
7872 /* Rechain the temporary list in front of GNU_FIELD. */
7873 tree gnu_bitp_field = gnu_field;
7874 while (gnu_tmp_bitp_list)
7876 tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list);
7877 DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field;
7878 if (gnu_last)
7879 DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list;
7880 else
7881 gnu_field_list = gnu_tmp_bitp_list;
7882 gnu_bitp_field = gnu_tmp_bitp_list;
7883 gnu_tmp_bitp_list = gnu_bitp_next;
7888 else
7889 last_reorder_field_type = 1;
7892 gnu_last = gnu_field;
7895 #undef MOVE_FROM_FIELD_LIST_TO
7897 gnu_field_list = nreverse (gnu_field_list);
7899 /* If permitted, we reorder the fields as follows:
7901 1) all (groups of) fields whose length is fixed and multiple of a byte,
7902 2) the remaining fields whose length is fixed and not multiple of a byte,
7903 3) the remaining fields whose length doesn't depend on discriminants,
7904 4) all fields whose length depends on discriminants,
7905 5) the variant part,
7907 within the record and within each variant recursively. */
7909 if (w_reorder)
7911 /* If we have pending bit-packed fields, warn if they would be moved
7912 to after regular fields. */
7913 if (last_reorder_field_type == 2
7914 && tmp_bitp_size != 0
7915 && tmp_last_reorder_field_type < 2)
7916 warn_on_field_placement (gnu_tmp_bitp_list
7917 ? gnu_tmp_bitp_list : gnu_field_list,
7918 gnat_component_list, gnat_record_type,
7919 in_variant, do_reorder);
7922 if (do_reorder)
7924 /* If we have pending bit-packed fields on the temporary list, we put
7925 them either on the bit-packed list or back on the regular list. */
7926 if (gnu_tmp_bitp_list)
7928 if (tmp_bitp_size != 0)
7929 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7930 else
7931 gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list);
7934 gnu_field_list
7935 = chainon (gnu_field_list,
7936 chainon (gnu_bitp_list,
7937 chainon (gnu_var_list, gnu_self_list)));
7940 /* Otherwise, if there is an aliased field placed after a field whose length
7941 depends on discriminants, we put all the fields of the latter sort, last.
7942 We need to do this in case an object of this record type is mutable. */
7943 else if (has_aliased_after_self_field)
7944 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7946 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7947 in our REP list to the previous level because this level needs them in
7948 order to do a correct layout, i.e. avoid having overlapping fields. */
7949 if (p_gnu_rep_list && gnu_rep_list)
7950 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7952 /* Deal with the annoying case of an extension of a record with variable size
7953 and partial rep clause, for which the _Parent field is forced at offset 0
7954 and has variable size, which we do not support below. Note that we cannot
7955 do it if the field has fixed size because we rely on the presence of the
7956 REP part built below to trigger the reordering of the fields in a derived
7957 record type when all the fields have a fixed position. */
7958 else if (gnu_rep_list
7959 && !DECL_CHAIN (gnu_rep_list)
7960 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7961 && !variants_have_rep
7962 && first_free_pos
7963 && integer_zerop (first_free_pos)
7964 && integer_zerop (bit_position (gnu_rep_list)))
7966 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7967 gnu_field_list = gnu_rep_list;
7968 gnu_rep_list = NULL_TREE;
7971 /* Otherwise, sort the fields by bit position and put them into their own
7972 record, before the others, if we also have fields without rep clause. */
7973 else if (gnu_rep_list)
7975 tree gnu_rep_type, gnu_rep_part;
7976 int i, len = list_length (gnu_rep_list);
7977 tree *gnu_arr = XALLOCAVEC (tree, len);
7979 /* If all the fields have a rep clause, we can do a flat layout. */
7980 layout_with_rep = !gnu_field_list
7981 && (!gnu_variant_part || variants_have_rep);
7982 gnu_rep_type
7983 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7985 for (gnu_field = gnu_rep_list, i = 0;
7986 gnu_field;
7987 gnu_field = DECL_CHAIN (gnu_field), i++)
7988 gnu_arr[i] = gnu_field;
7990 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7992 /* Put the fields in the list in order of increasing position, which
7993 means we start from the end. */
7994 gnu_rep_list = NULL_TREE;
7995 for (i = len - 1; i >= 0; i--)
7997 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7998 gnu_rep_list = gnu_arr[i];
7999 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
8002 if (layout_with_rep)
8003 gnu_field_list = gnu_rep_list;
8004 else
8006 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
8007 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
8008 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
8010 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
8011 without rep clause are laid out starting from this position.
8012 Therefore, we force it as a minimal size on the REP part. */
8013 gnu_rep_part
8014 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
8016 /* Chain the REP part at the beginning of the field list. */
8017 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
8018 gnu_field_list = gnu_rep_part;
8022 /* Chain the variant part at the end of the field list. */
8023 if (gnu_variant_part)
8024 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
8026 if (cancel_alignment)
8027 SET_TYPE_ALIGN (gnu_record_type, 0);
8029 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
8031 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
8032 debug_info && !maybe_unused);
8034 /* Chain the fields with zero size at the beginning of the field list. */
8035 if (gnu_zero_list)
8036 TYPE_FIELDS (gnu_record_type)
8037 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
8039 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
8042 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
8043 placed into an Esize, Component_Bit_Offset, or Component_Size value
8044 in the GNAT tree. */
8046 static Uint
8047 annotate_value (tree gnu_size)
8049 static int var_count = 0;
8050 TCode tcode;
8051 Node_Ref_Or_Val ops[3] = { No_Uint, No_Uint, No_Uint };
8052 struct tree_int_map in;
8054 /* See if we've already saved the value for this node. */
8055 if (EXPR_P (gnu_size) || DECL_P (gnu_size))
8057 struct tree_int_map *e;
8059 in.base.from = gnu_size;
8060 e = annotate_value_cache->find (&in);
8062 if (e)
8063 return (Node_Ref_Or_Val) e->to;
8065 else
8066 in.base.from = NULL_TREE;
8068 /* If we do not return inside this switch, TCODE will be set to the
8069 code to be used in a call to Create_Node. */
8070 switch (TREE_CODE (gnu_size))
8072 case INTEGER_CST:
8073 /* For negative values, build NEGATE_EXPR of the opposite. Such values
8074 can appear for discriminants in expressions for variants. */
8075 if (tree_int_cst_sgn (gnu_size) < 0)
8077 tree t = wide_int_to_tree (sizetype, -wi::to_wide (gnu_size));
8078 tcode = Negate_Expr;
8079 ops[0] = UI_From_gnu (t);
8081 else
8082 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8083 break;
8085 case COMPONENT_REF:
8086 /* The only case we handle here is a simple discriminant reference. */
8087 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8089 tree ref = gnu_size;
8090 gnu_size = TREE_OPERAND (ref, 1);
8092 /* Climb up the chain of successive extensions, if any. */
8093 while (TREE_CODE (TREE_OPERAND (ref, 0)) == COMPONENT_REF
8094 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (ref, 0), 1))
8095 == parent_name_id)
8096 ref = TREE_OPERAND (ref, 0);
8098 if (TREE_CODE (TREE_OPERAND (ref, 0)) == PLACEHOLDER_EXPR)
8100 /* Fall through to common processing as a FIELD_DECL. */
8101 tcode = Discrim_Val;
8102 ops[0] = UI_From_gnu (DECL_DISCRIMINANT_NUMBER (gnu_size));
8104 else
8105 return No_Uint;
8107 else
8108 return No_Uint;
8109 break;
8111 case VAR_DECL:
8112 tcode = Dynamic_Val;
8113 ops[0] = UI_From_Int (++var_count);
8114 break;
8116 CASE_CONVERT:
8117 case NON_LVALUE_EXPR:
8118 return annotate_value (TREE_OPERAND (gnu_size, 0));
8120 /* Now just list the operations we handle. */
8121 case COND_EXPR: tcode = Cond_Expr; break;
8122 case MINUS_EXPR: tcode = Minus_Expr; break;
8123 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8124 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8125 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8126 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8127 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8128 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8129 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8130 case NEGATE_EXPR: tcode = Negate_Expr; break;
8131 case MIN_EXPR: tcode = Min_Expr; break;
8132 case MAX_EXPR: tcode = Max_Expr; break;
8133 case ABS_EXPR: tcode = Abs_Expr; break;
8134 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
8135 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
8136 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8137 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8138 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8139 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8140 case LT_EXPR: tcode = Lt_Expr; break;
8141 case LE_EXPR: tcode = Le_Expr; break;
8142 case GT_EXPR: tcode = Gt_Expr; break;
8143 case GE_EXPR: tcode = Ge_Expr; break;
8144 case EQ_EXPR: tcode = Eq_Expr; break;
8145 case NE_EXPR: tcode = Ne_Expr; break;
8147 case MULT_EXPR:
8148 case PLUS_EXPR:
8149 tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr);
8150 /* Fold conversions from bytes to bits into inner operations. */
8151 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST
8152 && CONVERT_EXPR_P (TREE_OPERAND (gnu_size, 0)))
8154 tree inner_op = TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 0);
8155 if (TREE_CODE (inner_op) == TREE_CODE (gnu_size)
8156 && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST)
8158 tree inner_op_op1 = TREE_OPERAND (inner_op, 1);
8159 tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1);
8160 widest_int op1;
8161 if (TREE_CODE (gnu_size) == MULT_EXPR)
8162 op1 = (wi::to_widest (inner_op_op1)
8163 * wi::to_widest (gnu_size_op1));
8164 else
8165 op1 = (wi::to_widest (inner_op_op1)
8166 + wi::to_widest (gnu_size_op1));
8167 ops[1] = UI_From_gnu (wide_int_to_tree (sizetype, op1));
8168 ops[0] = annotate_value (TREE_OPERAND (inner_op, 0));
8171 break;
8173 case BIT_AND_EXPR:
8174 tcode = Bit_And_Expr;
8175 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8176 Such values appear in expressions with aligning patterns. Note that,
8177 since sizetype is unsigned, we have to jump through some hoops. */
8178 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8180 tree op1 = TREE_OPERAND (gnu_size, 1);
8181 wide_int signed_op1 = wi::sext (wi::to_wide (op1),
8182 TYPE_PRECISION (sizetype));
8183 if (wi::neg_p (signed_op1))
8185 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
8186 ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8189 break;
8191 case CALL_EXPR:
8192 /* In regular mode, inline back only if symbolic annotation is requested
8193 in order to avoid memory explosion on big discriminated record types.
8194 But not in ASIS mode, as symbolic annotation is required for DDA. */
8195 if (List_Representation_Info == 3 || type_annotate_only)
8197 tree t = maybe_inline_call_in_expr (gnu_size);
8198 return t ? annotate_value (t) : No_Uint;
8200 else
8201 return Uint_Minus_1;
8203 default:
8204 return No_Uint;
8207 /* Now get each of the operands that's relevant for this code. If any
8208 cannot be expressed as a repinfo node, say we can't. */
8209 for (int i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8210 if (ops[i] == No_Uint)
8212 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8213 if (ops[i] == No_Uint)
8214 return No_Uint;
8217 Node_Ref_Or_Val ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8219 /* Save the result in the cache. */
8220 if (in.base.from)
8222 struct tree_int_map **h;
8223 /* We can't assume the hash table data hasn't moved since the initial
8224 look up, so we have to search again. Allocating and inserting an
8225 entry at that point would be an alternative, but then we'd better
8226 discard the entry if we decided not to cache it. */
8227 h = annotate_value_cache->find_slot (&in, INSERT);
8228 gcc_assert (!*h);
8229 *h = ggc_alloc<tree_int_map> ();
8230 (*h)->base.from = in.base.from;
8231 (*h)->to = ret;
8234 return ret;
8237 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8238 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8239 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8240 BY_REF is true if the object is used by reference. */
8242 void
8243 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8245 if (by_ref)
8247 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8248 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8249 else
8250 gnu_type = TREE_TYPE (gnu_type);
8253 if (Unknown_Esize (gnat_entity))
8255 if (TREE_CODE (gnu_type) == RECORD_TYPE
8256 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8257 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8258 else if (!size)
8259 size = TYPE_SIZE (gnu_type);
8261 if (size)
8262 Set_Esize (gnat_entity, annotate_value (size));
8265 if (Unknown_Alignment (gnat_entity))
8266 Set_Alignment (gnat_entity,
8267 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8270 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8271 Return NULL_TREE if there is no such element in the list. */
8273 static tree
8274 purpose_member_field (const_tree elem, tree list)
8276 while (list)
8278 tree field = TREE_PURPOSE (list);
8279 if (SAME_FIELD_P (field, elem))
8280 return list;
8281 list = TREE_CHAIN (list);
8283 return NULL_TREE;
8286 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8287 set Component_Bit_Offset and Esize of the components to the position and
8288 size used by Gigi. */
8290 static void
8291 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8293 /* For an extension, the inherited components have not been translated because
8294 they are fetched from the _Parent component on the fly. */
8295 const bool is_extension
8296 = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity);
8298 /* We operate by first making a list of all fields and their position (we
8299 can get the size easily) and then update all the sizes in the tree. */
8300 tree gnu_list
8301 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8302 BIGGEST_ALIGNMENT, NULL_TREE);
8304 for (Entity_Id gnat_field = First_Entity (gnat_entity);
8305 Present (gnat_field);
8306 gnat_field = Next_Entity (gnat_field))
8307 if ((Ekind (gnat_field) == E_Component
8308 && (is_extension || present_gnu_tree (gnat_field)))
8309 || (Ekind (gnat_field) == E_Discriminant
8310 && !Is_Unchecked_Union (Scope (gnat_field))))
8312 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8313 gnu_list);
8314 if (t)
8316 tree parent_offset;
8318 /* If we are just annotating types and the type is tagged, the tag
8319 and the parent components are not generated by the front-end so
8320 we need to add the appropriate offset to each component without
8321 representation clause. */
8322 if (type_annotate_only
8323 && Is_Tagged_Type (gnat_entity)
8324 && No (Component_Clause (gnat_field)))
8326 /* For a component appearing in the current extension, the
8327 offset is the size of the parent. */
8328 if (Is_Derived_Type (gnat_entity)
8329 && Original_Record_Component (gnat_field) == gnat_field)
8330 parent_offset
8331 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8332 bitsizetype);
8333 else
8334 parent_offset = bitsize_int (POINTER_SIZE);
8336 if (TYPE_FIELDS (gnu_type))
8337 parent_offset
8338 = round_up (parent_offset,
8339 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8341 else
8342 parent_offset = bitsize_zero_node;
8344 Set_Component_Bit_Offset
8345 (gnat_field,
8346 annotate_value
8347 (size_binop (PLUS_EXPR,
8348 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
8349 TREE_VEC_ELT (TREE_VALUE (t), 2)),
8350 parent_offset)));
8352 Set_Esize (gnat_field,
8353 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8355 else if (is_extension)
8357 /* If there is no entry, this is an inherited component whose
8358 position is the same as in the parent type. */
8359 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
8361 /* If we are just annotating types, discriminants renaming those of
8362 the parent have no entry so deal with them specifically. */
8363 if (type_annotate_only
8364 && gnat_orig_field == gnat_field
8365 && Ekind (gnat_field) == E_Discriminant)
8366 gnat_orig_field = Corresponding_Discriminant (gnat_field);
8368 Set_Component_Bit_Offset (gnat_field,
8369 Component_Bit_Offset (gnat_orig_field));
8371 Set_Esize (gnat_field, Esize (gnat_orig_field));
8376 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8377 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8378 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8379 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8380 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8381 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8382 pre-existing list to be chained to the newly created entries. */
8384 static tree
8385 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8386 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8388 tree gnu_field;
8390 for (gnu_field = TYPE_FIELDS (gnu_type);
8391 gnu_field;
8392 gnu_field = DECL_CHAIN (gnu_field))
8394 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8395 DECL_FIELD_BIT_OFFSET (gnu_field));
8396 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8397 DECL_FIELD_OFFSET (gnu_field));
8398 unsigned int our_offset_align
8399 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8400 tree v = make_tree_vec (3);
8402 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8403 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8404 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8405 gnu_list = tree_cons (gnu_field, v, gnu_list);
8407 /* Recurse on internal fields, flattening the nested fields except for
8408 those in the variant part, if requested. */
8409 if (DECL_INTERNAL_P (gnu_field))
8411 tree gnu_field_type = TREE_TYPE (gnu_field);
8412 if (do_not_flatten_variant
8413 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8414 gnu_list
8415 = build_position_list (gnu_field_type, do_not_flatten_variant,
8416 size_zero_node, bitsize_zero_node,
8417 BIGGEST_ALIGNMENT, gnu_list);
8418 else
8419 gnu_list
8420 = build_position_list (gnu_field_type, do_not_flatten_variant,
8421 gnu_our_offset, gnu_our_bitpos,
8422 our_offset_align, gnu_list);
8426 return gnu_list;
8429 /* Return a list describing the substitutions needed to reflect the
8430 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8431 be in any order. The values in an element of the list are in the form
8432 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8433 a definition of GNAT_SUBTYPE. */
8435 static vec<subst_pair>
8436 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8438 vec<subst_pair> gnu_list = vNULL;
8439 Entity_Id gnat_discrim;
8440 Node_Id gnat_constr;
8442 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8443 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8444 Present (gnat_discrim);
8445 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8446 gnat_constr = Next_Elmt (gnat_constr))
8447 /* Ignore access discriminants. */
8448 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8450 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8451 tree replacement = convert (TREE_TYPE (gnu_field),
8452 elaborate_expression
8453 (Node (gnat_constr), gnat_subtype,
8454 get_entity_char (gnat_discrim),
8455 definition, true, false));
8456 subst_pair s = { gnu_field, replacement };
8457 gnu_list.safe_push (s);
8460 return gnu_list;
8463 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8464 variants of QUAL_UNION_TYPE that are still relevant after applying
8465 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8466 list to be prepended to the newly created entries. */
8468 static vec<variant_desc>
8469 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8470 vec<variant_desc> gnu_list)
8472 tree gnu_field;
8474 for (gnu_field = TYPE_FIELDS (qual_union_type);
8475 gnu_field;
8476 gnu_field = DECL_CHAIN (gnu_field))
8478 tree qual = DECL_QUALIFIER (gnu_field);
8479 unsigned int i;
8480 subst_pair *s;
8482 FOR_EACH_VEC_ELT (subst_list, i, s)
8483 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8485 /* If the new qualifier is not unconditionally false, its variant may
8486 still be accessed. */
8487 if (!integer_zerop (qual))
8489 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8490 variant_desc v = { variant_type, gnu_field, qual, NULL_TREE };
8492 gnu_list.safe_push (v);
8494 /* Recurse on the variant subpart of the variant, if any. */
8495 variant_subpart = get_variant_part (variant_type);
8496 if (variant_subpart)
8497 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8498 subst_list, gnu_list);
8500 /* If the new qualifier is unconditionally true, the subsequent
8501 variants cannot be accessed. */
8502 if (integer_onep (qual))
8503 break;
8507 return gnu_list;
8510 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8511 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8512 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8513 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8514 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8515 true if we are being called to process the Component_Size of GNAT_OBJECT;
8516 this is used only for error messages. ZERO_OK is true if a size of zero
8517 is permitted; if ZERO_OK is false, it means that a size of zero should be
8518 treated as an unspecified size. */
8520 static tree
8521 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8522 enum tree_code kind, bool component_p, bool zero_ok)
8524 Node_Id gnat_error_node;
8525 tree type_size, size;
8527 /* Return 0 if no size was specified. */
8528 if (uint_size == No_Uint)
8529 return NULL_TREE;
8531 /* Ignore a negative size since that corresponds to our back-annotation. */
8532 if (UI_Lt (uint_size, Uint_0))
8533 return NULL_TREE;
8535 /* Find the node to use for error messages. */
8536 if ((Ekind (gnat_object) == E_Component
8537 || Ekind (gnat_object) == E_Discriminant)
8538 && Present (Component_Clause (gnat_object)))
8539 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8540 else if (Present (Size_Clause (gnat_object)))
8541 gnat_error_node = Expression (Size_Clause (gnat_object));
8542 else
8543 gnat_error_node = gnat_object;
8545 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8546 but cannot be represented in bitsizetype. */
8547 size = UI_To_gnu (uint_size, bitsizetype);
8548 if (TREE_OVERFLOW (size))
8550 if (component_p)
8551 post_error_ne ("component size for& is too large", gnat_error_node,
8552 gnat_object);
8553 else
8554 post_error_ne ("size for& is too large", gnat_error_node,
8555 gnat_object);
8556 return NULL_TREE;
8559 /* Ignore a zero size if it is not permitted. */
8560 if (!zero_ok && integer_zerop (size))
8561 return NULL_TREE;
8563 /* The size of objects is always a multiple of a byte. */
8564 if (kind == VAR_DECL
8565 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8567 if (component_p)
8568 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8569 gnat_error_node, gnat_object);
8570 else
8571 post_error_ne ("size for& is not a multiple of Storage_Unit",
8572 gnat_error_node, gnat_object);
8573 return NULL_TREE;
8576 /* If this is an integral type or a packed array type, the front-end has
8577 already verified the size, so we need not do it here (which would mean
8578 checking against the bounds). However, if this is an aliased object,
8579 it may not be smaller than the type of the object. */
8580 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8581 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8582 return size;
8584 /* If the object is a record that contains a template, add the size of the
8585 template to the specified size. */
8586 if (TREE_CODE (gnu_type) == RECORD_TYPE
8587 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8588 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8590 if (kind == VAR_DECL
8591 /* If a type needs strict alignment, a component of this type in
8592 a packed record cannot be packed and thus uses the type size. */
8593 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8594 type_size = TYPE_SIZE (gnu_type);
8595 else
8596 type_size = rm_size (gnu_type);
8598 /* Modify the size of a discriminated type to be the maximum size. */
8599 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8600 type_size = max_size (type_size, true);
8602 /* If this is an access type or a fat pointer, the minimum size is that given
8603 by the smallest integral mode that's valid for pointers. */
8604 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8606 scalar_int_mode p_mode = NARROWEST_INT_MODE;
8607 while (!targetm.valid_pointer_mode (p_mode))
8608 p_mode = GET_MODE_WIDER_MODE (p_mode).require ();
8609 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8612 /* Issue an error either if the default size of the object isn't a constant
8613 or if the new size is smaller than it. */
8614 if (TREE_CODE (type_size) != INTEGER_CST
8615 || TREE_OVERFLOW (type_size)
8616 || tree_int_cst_lt (size, type_size))
8618 if (component_p)
8619 post_error_ne_tree
8620 ("component size for& too small{, minimum allowed is ^}",
8621 gnat_error_node, gnat_object, type_size);
8622 else
8623 post_error_ne_tree
8624 ("size for& too small{, minimum allowed is ^}",
8625 gnat_error_node, gnat_object, type_size);
8626 return NULL_TREE;
8629 return size;
8632 /* Similarly, but both validate and process a value of RM size. This routine
8633 is only called for types. */
8635 static void
8636 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8638 Node_Id gnat_attr_node;
8639 tree old_size, size;
8641 /* Do nothing if no size was specified. */
8642 if (uint_size == No_Uint)
8643 return;
8645 /* Ignore a negative size since that corresponds to our back-annotation. */
8646 if (UI_Lt (uint_size, Uint_0))
8647 return;
8649 /* Only issue an error if a Value_Size clause was explicitly given.
8650 Otherwise, we'd be duplicating an error on the Size clause. */
8651 gnat_attr_node
8652 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8654 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8655 but cannot be represented in bitsizetype. */
8656 size = UI_To_gnu (uint_size, bitsizetype);
8657 if (TREE_OVERFLOW (size))
8659 if (Present (gnat_attr_node))
8660 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8661 gnat_entity);
8662 return;
8665 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8666 exists, or this is an integer type, in which case the front-end will
8667 have always set it. */
8668 if (No (gnat_attr_node)
8669 && integer_zerop (size)
8670 && !Has_Size_Clause (gnat_entity)
8671 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8672 return;
8674 old_size = rm_size (gnu_type);
8676 /* If the old size is self-referential, get the maximum size. */
8677 if (CONTAINS_PLACEHOLDER_P (old_size))
8678 old_size = max_size (old_size, true);
8680 /* Issue an error either if the old size of the object isn't a constant or
8681 if the new size is smaller than it. The front-end has already verified
8682 this for scalar and packed array types. */
8683 if (TREE_CODE (old_size) != INTEGER_CST
8684 || TREE_OVERFLOW (old_size)
8685 || (AGGREGATE_TYPE_P (gnu_type)
8686 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8687 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8688 && !(TYPE_IS_PADDING_P (gnu_type)
8689 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8690 && TYPE_PACKED_ARRAY_TYPE_P
8691 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8692 && tree_int_cst_lt (size, old_size)))
8694 if (Present (gnat_attr_node))
8695 post_error_ne_tree
8696 ("Value_Size for& too small{, minimum allowed is ^}",
8697 gnat_attr_node, gnat_entity, old_size);
8698 return;
8701 /* Otherwise, set the RM size proper for integral types... */
8702 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8703 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8704 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8705 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8706 SET_TYPE_RM_SIZE (gnu_type, size);
8708 /* ...or the Ada size for record and union types. */
8709 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8710 && !TYPE_FAT_POINTER_P (gnu_type))
8711 SET_TYPE_ADA_SIZE (gnu_type, size);
8714 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8715 a type or object whose present alignment is ALIGN. If this alignment is
8716 valid, return it. Otherwise, give an error and return ALIGN. */
8718 static unsigned int
8719 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8721 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8722 unsigned int new_align;
8723 Node_Id gnat_error_node;
8725 /* Don't worry about checking alignment if alignment was not specified
8726 by the source program and we already posted an error for this entity. */
8727 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8728 return align;
8730 /* Post the error on the alignment clause if any. Note, for the implicit
8731 base type of an array type, the alignment clause is on the first
8732 subtype. */
8733 if (Present (Alignment_Clause (gnat_entity)))
8734 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8736 else if (Is_Itype (gnat_entity)
8737 && Is_Array_Type (gnat_entity)
8738 && Etype (gnat_entity) == gnat_entity
8739 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8740 gnat_error_node =
8741 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8743 else
8744 gnat_error_node = gnat_entity;
8746 /* Within GCC, an alignment is an integer, so we must make sure a value is
8747 specified that fits in that range. Also, there is an upper bound to
8748 alignments we can support/allow. */
8749 if (!UI_Is_In_Int_Range (alignment)
8750 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8751 post_error_ne_num ("largest supported alignment for& is ^",
8752 gnat_error_node, gnat_entity, max_allowed_alignment);
8753 else if (!(Present (Alignment_Clause (gnat_entity))
8754 && From_At_Mod (Alignment_Clause (gnat_entity)))
8755 && new_align * BITS_PER_UNIT < align)
8757 unsigned int double_align;
8758 bool is_capped_double, align_clause;
8760 /* If the default alignment of "double" or larger scalar types is
8761 specifically capped and the new alignment is above the cap, do
8762 not post an error and change the alignment only if there is an
8763 alignment clause; this makes it possible to have the associated
8764 GCC type overaligned by default for performance reasons. */
8765 if ((double_align = double_float_alignment) > 0)
8767 Entity_Id gnat_type
8768 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8769 is_capped_double
8770 = is_double_float_or_array (gnat_type, &align_clause);
8772 else if ((double_align = double_scalar_alignment) > 0)
8774 Entity_Id gnat_type
8775 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8776 is_capped_double
8777 = is_double_scalar_or_array (gnat_type, &align_clause);
8779 else
8780 is_capped_double = align_clause = false;
8782 if (is_capped_double && new_align >= double_align)
8784 if (align_clause)
8785 align = new_align * BITS_PER_UNIT;
8787 else
8789 if (is_capped_double)
8790 align = double_align * BITS_PER_UNIT;
8792 post_error_ne_num ("alignment for& must be at least ^",
8793 gnat_error_node, gnat_entity,
8794 align / BITS_PER_UNIT);
8797 else
8799 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8800 if (new_align > align)
8801 align = new_align;
8804 return align;
8807 /* Promote the alignment of GNU_TYPE corresponding to GNAT_ENTITY. Return
8808 a positive value on success or zero on failure. */
8810 static unsigned int
8811 promote_object_alignment (tree gnu_type, Entity_Id gnat_entity)
8813 unsigned int align, size_cap, align_cap;
8815 /* No point in promoting the alignment if this doesn't prevent BLKmode access
8816 to the object, in particular block copy, as this will for example disable
8817 the NRV optimization for it. No point in jumping through all the hoops
8818 needed in order to support BIGGEST_ALIGNMENT if we don't really have to.
8819 So we cap to the smallest alignment that corresponds to a known efficient
8820 memory access pattern, except for Atomic and Volatile_Full_Access. */
8821 if (Is_Atomic_Or_VFA (gnat_entity))
8823 size_cap = UINT_MAX;
8824 align_cap = BIGGEST_ALIGNMENT;
8826 else
8828 size_cap = MAX_FIXED_MODE_SIZE;
8829 align_cap = get_mode_alignment (ptr_mode);
8832 /* Do the promotion within the above limits. */
8833 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
8834 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
8835 align = 0;
8836 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
8837 align = align_cap;
8838 else
8839 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
8841 /* But make sure not to under-align the object. */
8842 if (align <= TYPE_ALIGN (gnu_type))
8843 align = 0;
8845 /* And honor the minimum valid atomic alignment, if any. */
8846 #ifdef MINIMUM_ATOMIC_ALIGNMENT
8847 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
8848 align = MINIMUM_ATOMIC_ALIGNMENT;
8849 #endif
8851 return align;
8854 /* Verify that TYPE is something we can implement atomically. If not, issue
8855 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8856 process a component type. */
8858 static void
8859 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8861 Node_Id gnat_error_point = gnat_entity;
8862 Node_Id gnat_node;
8863 machine_mode mode;
8864 enum mode_class mclass;
8865 unsigned int align;
8866 tree size;
8868 /* If this is an anonymous base type, nothing to check, the error will be
8869 reported on the source type if need be. */
8870 if (!Comes_From_Source (gnat_entity))
8871 return;
8873 mode = TYPE_MODE (type);
8874 mclass = GET_MODE_CLASS (mode);
8875 align = TYPE_ALIGN (type);
8876 size = TYPE_SIZE (type);
8878 /* Consider all aligned floating-point types atomic and any aligned types
8879 that are represented by integers no wider than a machine word. */
8880 scalar_int_mode int_mode;
8881 if ((mclass == MODE_FLOAT
8882 || (is_a <scalar_int_mode> (mode, &int_mode)
8883 && GET_MODE_BITSIZE (int_mode) <= BITS_PER_WORD))
8884 && align >= GET_MODE_ALIGNMENT (mode))
8885 return;
8887 /* For the moment, also allow anything that has an alignment equal to its
8888 size and which is smaller than a word. */
8889 if (size
8890 && TREE_CODE (size) == INTEGER_CST
8891 && compare_tree_int (size, align) == 0
8892 && align <= BITS_PER_WORD)
8893 return;
8895 for (gnat_node = First_Rep_Item (gnat_entity);
8896 Present (gnat_node);
8897 gnat_node = Next_Rep_Item (gnat_node))
8898 if (Nkind (gnat_node) == N_Pragma)
8900 unsigned char pragma_id
8901 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8903 if ((pragma_id == Pragma_Atomic && !component_p)
8904 || (pragma_id == Pragma_Atomic_Components && component_p))
8906 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8907 break;
8911 if (component_p)
8912 post_error_ne ("atomic access to component of & cannot be guaranteed",
8913 gnat_error_point, gnat_entity);
8914 else if (Is_Volatile_Full_Access (gnat_entity))
8915 post_error_ne ("volatile full access to & cannot be guaranteed",
8916 gnat_error_point, gnat_entity);
8917 else
8918 post_error_ne ("atomic access to & cannot be guaranteed",
8919 gnat_error_point, gnat_entity);
8923 /* Helper for the intrin compatibility checks family. Evaluate whether
8924 two types are definitely incompatible. */
8926 static bool
8927 intrin_types_incompatible_p (tree t1, tree t2)
8929 enum tree_code code;
8931 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8932 return false;
8934 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8935 return true;
8937 if (TREE_CODE (t1) != TREE_CODE (t2))
8938 return true;
8940 code = TREE_CODE (t1);
8942 switch (code)
8944 case INTEGER_TYPE:
8945 case REAL_TYPE:
8946 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8948 case POINTER_TYPE:
8949 case REFERENCE_TYPE:
8950 /* Assume designated types are ok. We'd need to account for char * and
8951 void * variants to do better, which could rapidly get messy and isn't
8952 clearly worth the effort. */
8953 return false;
8955 default:
8956 break;
8959 return false;
8962 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8963 on the Ada/builtin argument lists for the INB binding. */
8965 static bool
8966 intrin_arglists_compatible_p (intrin_binding_t * inb)
8968 function_args_iterator ada_iter, btin_iter;
8970 function_args_iter_init (&ada_iter, inb->ada_fntype);
8971 function_args_iter_init (&btin_iter, inb->btin_fntype);
8973 /* Sequence position of the last argument we checked. */
8974 int argpos = 0;
8976 while (true)
8978 tree ada_type = function_args_iter_cond (&ada_iter);
8979 tree btin_type = function_args_iter_cond (&btin_iter);
8981 /* If we've exhausted both lists simultaneously, we're done. */
8982 if (!ada_type && !btin_type)
8983 break;
8985 /* If one list is shorter than the other, they fail to match. */
8986 if (!ada_type || !btin_type)
8987 return false;
8989 /* If we're done with the Ada args and not with the internal builtin
8990 args, or the other way around, complain. */
8991 if (ada_type == void_type_node
8992 && btin_type != void_type_node)
8994 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8995 return false;
8998 if (btin_type == void_type_node
8999 && ada_type != void_type_node)
9001 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
9002 inb->gnat_entity, inb->gnat_entity, argpos);
9003 return false;
9006 /* Otherwise, check that types match for the current argument. */
9007 argpos ++;
9008 if (intrin_types_incompatible_p (ada_type, btin_type))
9010 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
9011 inb->gnat_entity, inb->gnat_entity, argpos);
9012 return false;
9016 function_args_iter_next (&ada_iter);
9017 function_args_iter_next (&btin_iter);
9020 return true;
9023 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
9024 on the Ada/builtin return values for the INB binding. */
9026 static bool
9027 intrin_return_compatible_p (intrin_binding_t * inb)
9029 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
9030 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
9032 /* Accept function imported as procedure, common and convenient. */
9033 if (VOID_TYPE_P (ada_return_type)
9034 && !VOID_TYPE_P (btin_return_type))
9035 return true;
9037 /* Check return types compatibility otherwise. Note that this
9038 handles void/void as well. */
9039 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
9041 post_error ("?intrinsic binding type mismatch on return value!",
9042 inb->gnat_entity);
9043 return false;
9046 return true;
9049 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
9050 compatible. Issue relevant warnings when they are not.
9052 This is intended as a light check to diagnose the most obvious cases, not
9053 as a full fledged type compatibility predicate. It is the programmer's
9054 responsibility to ensure correctness of the Ada declarations in Imports,
9055 especially when binding straight to a compiler internal. */
9057 static bool
9058 intrin_profiles_compatible_p (intrin_binding_t * inb)
9060 /* Check compatibility on return values and argument lists, each responsible
9061 for posting warnings as appropriate. Ensure use of the proper sloc for
9062 this purpose. */
9064 bool arglists_compatible_p, return_compatible_p;
9065 location_t saved_location = input_location;
9067 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
9069 return_compatible_p = intrin_return_compatible_p (inb);
9070 arglists_compatible_p = intrin_arglists_compatible_p (inb);
9072 input_location = saved_location;
9074 return return_compatible_p && arglists_compatible_p;
9077 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
9078 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
9079 specified size for this field. POS_LIST is a position list describing
9080 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
9081 to this layout. */
9083 static tree
9084 create_field_decl_from (tree old_field, tree field_type, tree record_type,
9085 tree size, tree pos_list,
9086 vec<subst_pair> subst_list)
9088 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
9089 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
9090 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
9091 tree new_pos, new_field;
9092 unsigned int i;
9093 subst_pair *s;
9095 if (CONTAINS_PLACEHOLDER_P (pos))
9096 FOR_EACH_VEC_ELT (subst_list, i, s)
9097 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
9099 /* If the position is now a constant, we can set it as the position of the
9100 field when we make it. Otherwise, we need to deal with it specially. */
9101 if (TREE_CONSTANT (pos))
9102 new_pos = bit_from_pos (pos, bitpos);
9103 else
9104 new_pos = NULL_TREE;
9106 new_field
9107 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
9108 size, new_pos, DECL_PACKED (old_field),
9109 !DECL_NONADDRESSABLE_P (old_field));
9111 if (!new_pos)
9113 normalize_offset (&pos, &bitpos, offset_align);
9114 /* Finalize the position. */
9115 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
9116 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
9117 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
9118 DECL_SIZE (new_field) = size;
9119 DECL_SIZE_UNIT (new_field)
9120 = convert (sizetype,
9121 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
9122 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
9125 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
9126 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
9127 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
9128 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
9130 return new_field;
9133 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
9134 it is the minimal size the REP_PART must have. */
9136 static tree
9137 create_rep_part (tree rep_type, tree record_type, tree min_size)
9139 tree field;
9141 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
9142 min_size = NULL_TREE;
9144 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
9145 min_size, NULL_TREE, 0, 1);
9146 DECL_INTERNAL_P (field) = 1;
9148 return field;
9151 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9153 static tree
9154 get_rep_part (tree record_type)
9156 tree field = TYPE_FIELDS (record_type);
9158 /* The REP part is the first field, internal, another record, and its name
9159 starts with an 'R'. */
9160 if (field
9161 && DECL_INTERNAL_P (field)
9162 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9163 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9164 return field;
9166 return NULL_TREE;
9169 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9171 tree
9172 get_variant_part (tree record_type)
9174 tree field;
9176 /* The variant part is the only internal field that is a qualified union. */
9177 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9178 if (DECL_INTERNAL_P (field)
9179 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9180 return field;
9182 return NULL_TREE;
9185 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9186 the list of variants to be used and RECORD_TYPE is the type of the parent.
9187 POS_LIST is a position list describing the layout of fields present in
9188 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9189 layout. DEBUG_INFO_P is true if we need to write debug information. */
9191 static tree
9192 create_variant_part_from (tree old_variant_part,
9193 vec<variant_desc> variant_list,
9194 tree record_type, tree pos_list,
9195 vec<subst_pair> subst_list,
9196 bool debug_info_p)
9198 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9199 tree old_union_type = TREE_TYPE (old_variant_part);
9200 tree new_union_type, new_variant_part;
9201 tree union_field_list = NULL_TREE;
9202 variant_desc *v;
9203 unsigned int i;
9205 /* First create the type of the variant part from that of the old one. */
9206 new_union_type = make_node (QUAL_UNION_TYPE);
9207 TYPE_NAME (new_union_type)
9208 = concat_name (TYPE_NAME (record_type),
9209 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9211 /* If the position of the variant part is constant, subtract it from the
9212 size of the type of the parent to get the new size. This manual CSE
9213 reduces the code size when not optimizing. */
9214 if (TREE_CODE (offset) == INTEGER_CST
9215 && TYPE_SIZE (record_type)
9216 && TYPE_SIZE_UNIT (record_type))
9218 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9219 tree first_bit = bit_from_pos (offset, bitpos);
9220 TYPE_SIZE (new_union_type)
9221 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9222 TYPE_SIZE_UNIT (new_union_type)
9223 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9224 byte_from_pos (offset, bitpos));
9225 SET_TYPE_ADA_SIZE (new_union_type,
9226 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9227 first_bit));
9228 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9229 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9231 else
9232 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9234 /* Now finish up the new variants and populate the union type. */
9235 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9237 tree old_field = v->field, new_field;
9238 tree old_variant, old_variant_subpart, new_variant, field_list;
9240 /* Skip variants that don't belong to this nesting level. */
9241 if (DECL_CONTEXT (old_field) != old_union_type)
9242 continue;
9244 /* Retrieve the list of fields already added to the new variant. */
9245 new_variant = v->new_type;
9246 field_list = TYPE_FIELDS (new_variant);
9248 /* If the old variant had a variant subpart, we need to create a new
9249 variant subpart and add it to the field list. */
9250 old_variant = v->type;
9251 old_variant_subpart = get_variant_part (old_variant);
9252 if (old_variant_subpart)
9254 tree new_variant_subpart
9255 = create_variant_part_from (old_variant_subpart, variant_list,
9256 new_variant, pos_list, subst_list,
9257 debug_info_p);
9258 DECL_CHAIN (new_variant_subpart) = field_list;
9259 field_list = new_variant_subpart;
9262 /* Finish up the new variant and create the field. */
9263 finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p);
9264 compute_record_mode (new_variant);
9265 create_type_decl (TYPE_NAME (new_variant), new_variant, true,
9266 debug_info_p, Empty);
9268 new_field
9269 = create_field_decl_from (old_field, new_variant, new_union_type,
9270 TYPE_SIZE (new_variant),
9271 pos_list, subst_list);
9272 DECL_QUALIFIER (new_field) = v->qual;
9273 DECL_INTERNAL_P (new_field) = 1;
9274 DECL_CHAIN (new_field) = union_field_list;
9275 union_field_list = new_field;
9278 /* Finish up the union type and create the variant part. Note that we don't
9279 reverse the field list because VARIANT_LIST has been traversed in reverse
9280 order. */
9281 finish_record_type (new_union_type, union_field_list, 2, debug_info_p);
9282 compute_record_mode (new_union_type);
9283 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true,
9284 debug_info_p, Empty);
9286 new_variant_part
9287 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9288 TYPE_SIZE (new_union_type),
9289 pos_list, subst_list);
9290 DECL_INTERNAL_P (new_variant_part) = 1;
9292 /* With multiple discriminants it is possible for an inner variant to be
9293 statically selected while outer ones are not; in this case, the list
9294 of fields of the inner variant is not flattened and we end up with a
9295 qualified union with a single member. Drop the useless container. */
9296 if (!DECL_CHAIN (union_field_list))
9298 DECL_CONTEXT (union_field_list) = record_type;
9299 DECL_FIELD_OFFSET (union_field_list)
9300 = DECL_FIELD_OFFSET (new_variant_part);
9301 DECL_FIELD_BIT_OFFSET (union_field_list)
9302 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9303 SET_DECL_OFFSET_ALIGN (union_field_list,
9304 DECL_OFFSET_ALIGN (new_variant_part));
9305 new_variant_part = union_field_list;
9308 return new_variant_part;
9311 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9312 which are both RECORD_TYPE, after applying the substitutions described
9313 in SUBST_LIST. */
9315 static void
9316 copy_and_substitute_in_size (tree new_type, tree old_type,
9317 vec<subst_pair> subst_list)
9319 unsigned int i;
9320 subst_pair *s;
9322 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9323 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9324 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9325 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9326 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9328 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9329 FOR_EACH_VEC_ELT (subst_list, i, s)
9330 TYPE_SIZE (new_type)
9331 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9332 s->discriminant, s->replacement);
9334 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9335 FOR_EACH_VEC_ELT (subst_list, i, s)
9336 TYPE_SIZE_UNIT (new_type)
9337 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9338 s->discriminant, s->replacement);
9340 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9341 FOR_EACH_VEC_ELT (subst_list, i, s)
9342 SET_TYPE_ADA_SIZE
9343 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9344 s->discriminant, s->replacement));
9346 /* Finalize the size. */
9347 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9348 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9351 /* Return true if DISC is a stored discriminant of RECORD_TYPE. */
9353 static inline bool
9354 is_stored_discriminant (Entity_Id discr, Entity_Id record_type)
9356 if (Is_Unchecked_Union (record_type))
9357 return false;
9358 else if (Is_Tagged_Type (record_type))
9359 return No (Corresponding_Discriminant (discr));
9360 else if (Ekind (record_type) == E_Record_Type)
9361 return Original_Record_Component (discr) == discr;
9362 else
9363 return true;
9366 /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are
9367 both record types, after applying the substitutions described in SUBST_LIST.
9368 DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */
9370 static void
9371 copy_and_substitute_in_layout (Entity_Id gnat_new_type,
9372 Entity_Id gnat_old_type,
9373 tree gnu_new_type,
9374 tree gnu_old_type,
9375 vec<subst_pair> gnu_subst_list,
9376 bool debug_info_p)
9378 const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype);
9379 tree gnu_field_list = NULL_TREE;
9380 bool selected_variant, all_constant_pos = true;
9381 vec<variant_desc> gnu_variant_list;
9383 /* Look for REP and variant parts in the old type. */
9384 tree gnu_rep_part = get_rep_part (gnu_old_type);
9385 tree gnu_variant_part = get_variant_part (gnu_old_type);
9387 /* If there is a variant part, we must compute whether the constraints
9388 statically select a particular variant. If so, we simply drop the
9389 qualified union and flatten the list of fields. Otherwise we will
9390 build a new qualified union for the variants that are still relevant. */
9391 if (gnu_variant_part)
9393 variant_desc *v;
9394 unsigned int i;
9396 gnu_variant_list = build_variant_list (TREE_TYPE (gnu_variant_part),
9397 gnu_subst_list, vNULL);
9399 /* If all the qualifiers are unconditionally true, the innermost variant
9400 is statically selected. */
9401 selected_variant = true;
9402 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9403 if (!integer_onep (v->qual))
9405 selected_variant = false;
9406 break;
9409 /* Otherwise, create the new variants. */
9410 if (!selected_variant)
9411 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9413 tree old_variant = v->type;
9414 tree new_variant = make_node (RECORD_TYPE);
9415 tree suffix
9416 = concat_name (DECL_NAME (gnu_variant_part),
9417 IDENTIFIER_POINTER (DECL_NAME (v->field)));
9418 TYPE_NAME (new_variant)
9419 = concat_name (TYPE_NAME (gnu_new_type),
9420 IDENTIFIER_POINTER (suffix));
9421 TYPE_REVERSE_STORAGE_ORDER (new_variant)
9422 = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type);
9423 copy_and_substitute_in_size (new_variant, old_variant,
9424 gnu_subst_list);
9425 v->new_type = new_variant;
9428 else
9430 gnu_variant_list.create (0);
9431 selected_variant = false;
9434 /* Make a list of fields and their position in the old type. */
9435 tree gnu_pos_list
9436 = build_position_list (gnu_old_type,
9437 gnu_variant_list.exists () && !selected_variant,
9438 size_zero_node, bitsize_zero_node,
9439 BIGGEST_ALIGNMENT, NULL_TREE);
9441 /* Now go down every component in the new type and compute its size and
9442 position from those of the component in the old type and the stored
9443 constraints of the new type. */
9444 Entity_Id gnat_field, gnat_old_field;
9445 for (gnat_field = First_Entity (gnat_new_type);
9446 Present (gnat_field);
9447 gnat_field = Next_Entity (gnat_field))
9448 if ((Ekind (gnat_field) == E_Component
9449 || (Ekind (gnat_field) == E_Discriminant
9450 && is_stored_discriminant (gnat_field, gnat_new_type)))
9451 && (gnat_old_field = is_subtype
9452 ? Original_Record_Component (gnat_field)
9453 : Corresponding_Record_Component (gnat_field))
9454 && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type
9455 && present_gnu_tree (gnat_old_field))
9457 Name_Id gnat_name = Chars (gnat_field);
9458 tree gnu_old_field = get_gnu_tree (gnat_old_field);
9459 if (TREE_CODE (gnu_old_field) == COMPONENT_REF)
9460 gnu_old_field = TREE_OPERAND (gnu_old_field, 1);
9461 tree gnu_context = DECL_CONTEXT (gnu_old_field);
9462 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
9463 tree gnu_cont_type, gnu_last = NULL_TREE;
9465 /* If the type is the same, retrieve the GCC type from the
9466 old field to take into account possible adjustments. */
9467 if (Etype (gnat_field) == Etype (gnat_old_field))
9468 gnu_field_type = TREE_TYPE (gnu_old_field);
9469 else
9470 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
9472 /* If there was a component clause, the field types must be the same
9473 for the old and new types, so copy the data from the old field to
9474 avoid recomputation here. Also if the field is justified modular
9475 and the optimization in gnat_to_gnu_field was applied. */
9476 if (Present (Component_Clause (gnat_old_field))
9477 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
9478 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
9479 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
9480 == TREE_TYPE (gnu_old_field)))
9482 gnu_size = DECL_SIZE (gnu_old_field);
9483 gnu_field_type = TREE_TYPE (gnu_old_field);
9486 /* If the old field was packed and of constant size, we have to get the
9487 old size here as it might differ from what the Etype conveys and the
9488 latter might overlap with the following field. Try to arrange the
9489 type for possible better packing along the way. */
9490 else if (DECL_PACKED (gnu_old_field)
9491 && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST)
9493 gnu_size = DECL_SIZE (gnu_old_field);
9494 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
9495 && !TYPE_FAT_POINTER_P (gnu_field_type)
9496 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
9497 gnu_field_type = make_packable_type (gnu_field_type, true);
9500 else
9501 gnu_size = TYPE_SIZE (gnu_field_type);
9503 /* If the context of the old field is the old type or its REP part,
9504 put the field directly in the new type; otherwise look up the
9505 context in the variant list and put the field either in the new
9506 type if there is a selected variant or in one new variant. */
9507 if (gnu_context == gnu_old_type
9508 || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part)))
9509 gnu_cont_type = gnu_new_type;
9510 else
9512 variant_desc *v;
9513 unsigned int i;
9514 tree rep_part;
9516 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9517 if (gnu_context == v->type
9518 || ((rep_part = get_rep_part (v->type))
9519 && gnu_context == TREE_TYPE (rep_part)))
9520 break;
9522 if (v)
9523 gnu_cont_type = selected_variant ? gnu_new_type : v->new_type;
9524 else
9525 /* The front-end may pass us "ghost" components if it fails to
9526 recognize that a constrain statically selects a particular
9527 variant. Discard them. */
9528 continue;
9531 /* Now create the new field modeled on the old one. */
9532 gnu_field
9533 = create_field_decl_from (gnu_old_field, gnu_field_type,
9534 gnu_cont_type, gnu_size,
9535 gnu_pos_list, gnu_subst_list);
9536 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
9538 /* If the context is a variant, put it in the new variant directly. */
9539 if (gnu_cont_type != gnu_new_type)
9541 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
9542 TYPE_FIELDS (gnu_cont_type) = gnu_field;
9545 /* To match the layout crafted in components_to_record, if this is
9546 the _Tag or _Parent field, put it before any other fields. */
9547 else if (gnat_name == Name_uTag || gnat_name == Name_uParent)
9548 gnu_field_list = chainon (gnu_field_list, gnu_field);
9550 /* Similarly, if this is the _Controller field, put it before the
9551 other fields except for the _Tag or _Parent field. */
9552 else if (gnat_name == Name_uController && gnu_last)
9554 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
9555 DECL_CHAIN (gnu_last) = gnu_field;
9558 /* Otherwise, put it after the other fields. */
9559 else
9561 DECL_CHAIN (gnu_field) = gnu_field_list;
9562 gnu_field_list = gnu_field;
9563 if (!gnu_last)
9564 gnu_last = gnu_field;
9565 if (TREE_CODE (gnu_pos) != INTEGER_CST)
9566 all_constant_pos = false;
9569 /* For a stored discriminant in a derived type, replace the field. */
9570 if (!is_subtype && Ekind (gnat_field) == E_Discriminant)
9572 tree gnu_ref = get_gnu_tree (gnat_field);
9573 TREE_OPERAND (gnu_ref, 1) = gnu_field;
9575 else
9576 save_gnu_tree (gnat_field, gnu_field, false);
9579 /* If there is no variant list or a selected variant and the fields all have
9580 constant position, put them in order of increasing position to match that
9581 of constant CONSTRUCTORs. */
9582 if ((!gnu_variant_list.exists () || selected_variant) && all_constant_pos)
9584 const int len = list_length (gnu_field_list);
9585 tree *field_arr = XALLOCAVEC (tree, len), t = gnu_field_list;
9587 for (int i = 0; t; t = DECL_CHAIN (t), i++)
9588 field_arr[i] = t;
9590 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
9592 gnu_field_list = NULL_TREE;
9593 for (int i = 0; i < len; i++)
9595 DECL_CHAIN (field_arr[i]) = gnu_field_list;
9596 gnu_field_list = field_arr[i];
9600 /* If there is a variant list and no selected variant, we need to create the
9601 nest of variant parts from the old nest. */
9602 else if (gnu_variant_list.exists () && !selected_variant)
9604 tree new_variant_part
9605 = create_variant_part_from (gnu_variant_part, gnu_variant_list,
9606 gnu_new_type, gnu_pos_list,
9607 gnu_subst_list, debug_info_p);
9608 DECL_CHAIN (new_variant_part) = gnu_field_list;
9609 gnu_field_list = new_variant_part;
9612 gnu_variant_list.release ();
9613 gnu_subst_list.release ();
9615 gnu_field_list = nreverse (gnu_field_list);
9617 /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE.
9618 Otherwise sizes and alignment must be computed independently. */
9619 if (is_subtype)
9621 finish_record_type (gnu_new_type, gnu_field_list, 2, debug_info_p);
9622 compute_record_mode (gnu_new_type);
9624 else
9625 finish_record_type (gnu_new_type, gnu_field_list, 1, debug_info_p);
9627 /* Now go through the entities again looking for Itypes that we have not yet
9628 elaborated (e.g. Etypes of fields that have Original_Components). */
9629 for (Entity_Id gnat_field = First_Entity (gnat_new_type);
9630 Present (gnat_field);
9631 gnat_field = Next_Entity (gnat_field))
9632 if ((Ekind (gnat_field) == E_Component
9633 || Ekind (gnat_field) == E_Discriminant)
9634 && Is_Itype (Etype (gnat_field))
9635 && !present_gnu_tree (Etype (gnat_field)))
9636 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
9639 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9640 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9641 the original array type if it has been translated. This association is a
9642 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9643 that for standard DWARF, we also want to get the original type name. */
9645 static void
9646 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9648 Entity_Id gnat_original_array_type
9649 = Underlying_Type (Original_Array_Type (gnat_entity));
9650 tree gnu_original_array_type;
9652 if (!present_gnu_tree (gnat_original_array_type))
9653 return;
9655 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9657 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9658 return;
9660 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9662 tree original_name = TYPE_NAME (gnu_original_array_type);
9664 if (TREE_CODE (original_name) == TYPE_DECL)
9665 original_name = DECL_NAME (original_name);
9667 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9668 TYPE_NAME (gnu_type) = original_name;
9670 else
9671 add_parallel_type (gnu_type, gnu_original_array_type);
9674 /* Given a type T, a FIELD_DECL F, and a replacement value R, return an
9675 equivalent type with adjusted size expressions where all occurrences
9676 of references to F in a PLACEHOLDER_EXPR have been replaced by R.
9678 The function doesn't update the layout of the type, i.e. it assumes
9679 that the substitution is purely formal. That's why the replacement
9680 value R must itself contain a PLACEHOLDER_EXPR. */
9682 tree
9683 substitute_in_type (tree t, tree f, tree r)
9685 tree nt;
9687 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9689 switch (TREE_CODE (t))
9691 case INTEGER_TYPE:
9692 case ENUMERAL_TYPE:
9693 case BOOLEAN_TYPE:
9694 case REAL_TYPE:
9696 /* First the domain types of arrays. */
9697 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9698 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9700 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9701 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9703 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9704 return t;
9706 nt = copy_type (t);
9707 TYPE_GCC_MIN_VALUE (nt) = low;
9708 TYPE_GCC_MAX_VALUE (nt) = high;
9710 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9711 SET_TYPE_INDEX_TYPE
9712 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9714 return nt;
9717 /* Then the subtypes. */
9718 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9719 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9721 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9722 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9724 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9725 return t;
9727 nt = copy_type (t);
9728 SET_TYPE_RM_MIN_VALUE (nt, low);
9729 SET_TYPE_RM_MAX_VALUE (nt, high);
9731 return nt;
9734 return t;
9736 case COMPLEX_TYPE:
9737 nt = substitute_in_type (TREE_TYPE (t), f, r);
9738 if (nt == TREE_TYPE (t))
9739 return t;
9741 return build_complex_type (nt);
9743 case FUNCTION_TYPE:
9744 /* These should never show up here. */
9745 gcc_unreachable ();
9747 case ARRAY_TYPE:
9749 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9750 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9752 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9753 return t;
9755 nt = build_nonshared_array_type (component, domain);
9756 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9757 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9758 SET_TYPE_MODE (nt, TYPE_MODE (t));
9759 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9760 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9761 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9762 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9763 if (TYPE_REVERSE_STORAGE_ORDER (t))
9764 set_reverse_storage_order_on_array_type (nt);
9765 if (TYPE_NONALIASED_COMPONENT (t))
9766 set_nonaliased_component_on_array_type (nt);
9767 return nt;
9770 case RECORD_TYPE:
9771 case UNION_TYPE:
9772 case QUAL_UNION_TYPE:
9774 bool changed_field = false;
9775 tree field;
9777 /* Start out with no fields, make new fields, and chain them
9778 in. If we haven't actually changed the type of any field,
9779 discard everything we've done and return the old type. */
9780 nt = copy_type (t);
9781 TYPE_FIELDS (nt) = NULL_TREE;
9783 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9785 tree new_field = copy_node (field), new_n;
9787 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9788 if (new_n != TREE_TYPE (field))
9790 TREE_TYPE (new_field) = new_n;
9791 changed_field = true;
9794 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9795 if (new_n != DECL_FIELD_OFFSET (field))
9797 DECL_FIELD_OFFSET (new_field) = new_n;
9798 changed_field = true;
9801 /* Do the substitution inside the qualifier, if any. */
9802 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9804 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9805 if (new_n != DECL_QUALIFIER (field))
9807 DECL_QUALIFIER (new_field) = new_n;
9808 changed_field = true;
9812 DECL_CONTEXT (new_field) = nt;
9813 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9815 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9816 TYPE_FIELDS (nt) = new_field;
9819 if (!changed_field)
9820 return t;
9822 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9823 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9824 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9825 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9826 return nt;
9829 default:
9830 return t;
9834 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9835 needed to represent the object. */
9837 tree
9838 rm_size (tree gnu_type)
9840 /* For integral types, we store the RM size explicitly. */
9841 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9842 return TYPE_RM_SIZE (gnu_type);
9844 /* Return the RM size of the actual data plus the size of the template. */
9845 if (TREE_CODE (gnu_type) == RECORD_TYPE
9846 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9847 return
9848 size_binop (PLUS_EXPR,
9849 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9850 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9852 /* For record or union types, we store the size explicitly. */
9853 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9854 && !TYPE_FAT_POINTER_P (gnu_type)
9855 && TYPE_ADA_SIZE (gnu_type))
9856 return TYPE_ADA_SIZE (gnu_type);
9858 /* For other types, this is just the size. */
9859 return TYPE_SIZE (gnu_type);
9862 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9863 fully-qualified name, possibly with type information encoding.
9864 Otherwise, return the name. */
9866 static const char *
9867 get_entity_char (Entity_Id gnat_entity)
9869 Get_Encoded_Name (gnat_entity);
9870 return ggc_strdup (Name_Buffer);
9873 tree
9874 get_entity_name (Entity_Id gnat_entity)
9876 Get_Encoded_Name (gnat_entity);
9877 return get_identifier_with_length (Name_Buffer, Name_Len);
9880 /* Return an identifier representing the external name to be used for
9881 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9882 and the specified suffix. */
9884 tree
9885 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9887 const Entity_Kind kind = Ekind (gnat_entity);
9888 const bool has_suffix = (suffix != NULL);
9889 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9890 String_Pointer sp = {suffix, &temp};
9892 Get_External_Name (gnat_entity, has_suffix, sp);
9894 /* A variable using the Stdcall convention lives in a DLL. We adjust
9895 its name to use the jump table, the _imp__NAME contains the address
9896 for the NAME variable. */
9897 if ((kind == E_Variable || kind == E_Constant)
9898 && Has_Stdcall_Convention (gnat_entity))
9900 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9901 char *new_name = (char *) alloca (len + 1);
9902 strcpy (new_name, STDCALL_PREFIX);
9903 strcat (new_name, Name_Buffer);
9904 return get_identifier_with_length (new_name, len);
9907 return get_identifier_with_length (Name_Buffer, Name_Len);
9910 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9911 string, return a new IDENTIFIER_NODE that is the concatenation of
9912 the name followed by "___" and the specified suffix. */
9914 tree
9915 concat_name (tree gnu_name, const char *suffix)
9917 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9918 char *new_name = (char *) alloca (len + 1);
9919 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9920 strcat (new_name, "___");
9921 strcat (new_name, suffix);
9922 return get_identifier_with_length (new_name, len);
9925 /* Initialize data structures of the decl.c module. */
9927 void
9928 init_gnat_decl (void)
9930 /* Initialize the cache of annotated values. */
9931 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9933 /* Initialize the association of dummy types with subprograms. */
9934 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
9937 /* Destroy data structures of the decl.c module. */
9939 void
9940 destroy_gnat_decl (void)
9942 /* Destroy the cache of annotated values. */
9943 annotate_value_cache->empty ();
9944 annotate_value_cache = NULL;
9946 /* Destroy the association of dummy types with subprograms. */
9947 dummy_to_subprog_map->empty ();
9948 dummy_to_subprog_map = NULL;
9951 #include "gt-ada-decl.h"