Fix ChangeLog entry for r240730
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob60dc32c9cd040964f49b639f32bf7e86b2354ff6
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2016, 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"
38 #include "ada.h"
39 #include "types.h"
40 #include "atree.h"
41 #include "elists.h"
42 #include "namet.h"
43 #include "nlists.h"
44 #include "repinfo.h"
45 #include "snames.h"
46 #include "uintp.h"
47 #include "urealp.h"
48 #include "fe.h"
49 #include "sinfo.h"
50 #include "einfo.h"
51 #include "ada-tree.h"
52 #include "gigi.h"
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
59 #ifdef TARGET_64BIT
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
64 #else
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
67 #endif
68 #else
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
71 #endif
73 #define STDCALL_PREFIX "_imp__"
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
87 #else
88 #define FOREIGN_FORCE_REALIGN_STACK 0
89 #endif
91 struct incomplete
93 struct incomplete *next;
94 tree old_type;
95 Entity_Id full_type;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing a record, an array or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_Limited_With types until the
104 end of the spec. */
105 static struct incomplete *defer_limited_with_list;
107 typedef struct subst_pair_d {
108 tree discriminant;
109 tree replacement;
110 } subst_pair;
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
115 tree type;
117 /* The associated field. */
118 tree field;
120 /* The value of the qualifier. */
121 tree qual;
123 /* The type of the variant after transformation. */
124 tree new_type;
125 } variant_desc;
128 /* A map used to cache the result of annotate_value. */
129 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
131 static inline hashval_t
132 hash (tree_int_map *m)
134 return htab_hash_pointer (m->base.from);
137 static inline bool
138 equal (tree_int_map *a, tree_int_map *b)
140 return a->base.from == b->base.from;
143 static int
144 keep_cache_entry (tree_int_map *&m)
146 return ggc_marked_p (m->base.from);
150 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
152 /* A map used to associate a dummy type with a list of subprogram entities. */
153 struct GTY((for_user)) tree_entity_vec_map
155 struct tree_map_base base;
156 vec<Entity_Id, va_gc_atomic> *to;
159 void
160 gt_pch_nx (Entity_Id &)
164 void
165 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
167 op (x, cookie);
170 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
172 static inline hashval_t
173 hash (tree_entity_vec_map *m)
175 return htab_hash_pointer (m->base.from);
178 static inline bool
179 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
181 return a->base.from == b->base.from;
184 static int
185 keep_cache_entry (tree_entity_vec_map *&m)
187 return ggc_marked_p (m->base.from);
191 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
193 static void prepend_one_attribute (struct attrib **,
194 enum attrib_type, tree, tree, Node_Id);
195 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
196 static void prepend_attributes (struct attrib **, Entity_Id);
197 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
198 bool);
199 static bool type_has_variable_size (tree);
200 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
201 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
202 unsigned int);
203 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
204 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
205 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
206 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
207 static tree gnu_ext_name_for_subprog (Entity_Id, tree);
208 static tree change_qualified_type (tree, int);
209 static void set_nonaliased_component_on_array_type (tree);
210 static void set_reverse_storage_order_on_array_type (tree);
211 static bool same_discriminant_p (Entity_Id, Entity_Id);
212 static bool array_type_has_nonaliased_component (tree, Entity_Id);
213 static bool compile_time_known_address_p (Node_Id);
214 static bool cannot_be_superflat (Node_Id);
215 static bool constructor_address_p (tree);
216 static bool allocatable_size_p (tree, bool);
217 static bool initial_value_needs_conversion (tree, tree);
218 static int compare_field_bitpos (const PTR, const PTR);
219 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
220 bool, bool, bool, bool, bool, tree, tree *);
221 static Uint annotate_value (tree);
222 static void annotate_rep (Entity_Id, tree);
223 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
224 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
225 static vec<variant_desc> build_variant_list (tree,
226 vec<subst_pair> ,
227 vec<variant_desc> );
228 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
229 static void set_rm_size (Uint, tree, Entity_Id);
230 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
231 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
232 static tree create_field_decl_from (tree, tree, tree, tree, tree,
233 vec<subst_pair> );
234 static tree create_rep_part (tree, tree, tree);
235 static tree get_rep_part (tree);
236 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
237 tree, vec<subst_pair> );
238 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
239 static void associate_original_type_to_packed_array (tree, Entity_Id);
240 static const char *get_entity_char (Entity_Id);
242 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
243 to pass around calls performing profile compatibility checks. */
245 typedef struct {
246 Entity_Id gnat_entity; /* The Ada subprogram entity. */
247 tree ada_fntype; /* The corresponding GCC type node. */
248 tree btin_fntype; /* The GCC builtin function type node. */
249 } intrin_binding_t;
251 static bool intrin_profiles_compatible_p (intrin_binding_t *);
253 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
254 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
255 and associate the ..._DECL node with the input GNAT defining identifier.
257 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
258 initial value (in GCC tree form). This is optional for a variable. For
259 a renamed entity, GNU_EXPR gives the object being renamed.
261 DEFINITION is true if this call is intended for a definition. This is used
262 for separate compilation where it is necessary to know whether an external
263 declaration or a definition must be created if the GCC equivalent was not
264 created previously. */
266 tree
267 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
269 /* Contains the kind of the input GNAT node. */
270 const Entity_Kind kind = Ekind (gnat_entity);
271 /* True if this is a type. */
272 const bool is_type = IN (kind, Type_Kind);
273 /* True if this is an artificial entity. */
274 const bool artificial_p = !Comes_From_Source (gnat_entity);
275 /* True if debug info is requested for this entity. */
276 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
277 /* True if this entity is to be considered as imported. */
278 const bool imported_p
279 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
280 /* For a type, contains the equivalent GNAT node to be used in gigi. */
281 Entity_Id gnat_equiv_type = Empty;
282 /* Temporary used to walk the GNAT tree. */
283 Entity_Id gnat_temp;
284 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
285 This node will be associated with the GNAT node by calling at the end
286 of the `switch' statement. */
287 tree gnu_decl = NULL_TREE;
288 /* Contains the GCC type to be used for the GCC node. */
289 tree gnu_type = NULL_TREE;
290 /* Contains the GCC size tree to be used for the GCC node. */
291 tree gnu_size = NULL_TREE;
292 /* Contains the GCC name to be used for the GCC node. */
293 tree gnu_entity_name;
294 /* True if we have already saved gnu_decl as a GNAT association. */
295 bool saved = false;
296 /* True if we incremented defer_incomplete_level. */
297 bool this_deferred = false;
298 /* True if we incremented force_global. */
299 bool this_global = false;
300 /* True if we should check to see if elaborated during processing. */
301 bool maybe_present = false;
302 /* True if we made GNU_DECL and its type here. */
303 bool this_made_decl = false;
304 /* Size and alignment of the GCC node, if meaningful. */
305 unsigned int esize = 0, align = 0;
306 /* Contains the list of attributes directly attached to the entity. */
307 struct attrib *attr_list = NULL;
309 /* Since a use of an Itype is a definition, process it as such if it
310 is not in a with'ed unit. */
311 if (!definition
312 && is_type
313 && Is_Itype (gnat_entity)
314 && !present_gnu_tree (gnat_entity)
315 && In_Extended_Main_Code_Unit (gnat_entity))
317 /* Ensure that we are in a subprogram mentioned in the Scope chain of
318 this entity, our current scope is global, or we encountered a task
319 or entry (where we can't currently accurately check scoping). */
320 if (!current_function_decl
321 || DECL_ELABORATION_PROC_P (current_function_decl))
323 process_type (gnat_entity);
324 return get_gnu_tree (gnat_entity);
327 for (gnat_temp = Scope (gnat_entity);
328 Present (gnat_temp);
329 gnat_temp = Scope (gnat_temp))
331 if (Is_Type (gnat_temp))
332 gnat_temp = Underlying_Type (gnat_temp);
334 if (Ekind (gnat_temp) == E_Subprogram_Body)
335 gnat_temp
336 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
338 if (IN (Ekind (gnat_temp), Subprogram_Kind)
339 && Present (Protected_Body_Subprogram (gnat_temp)))
340 gnat_temp = Protected_Body_Subprogram (gnat_temp);
342 if (Ekind (gnat_temp) == E_Entry
343 || Ekind (gnat_temp) == E_Entry_Family
344 || Ekind (gnat_temp) == E_Task_Type
345 || (IN (Ekind (gnat_temp), Subprogram_Kind)
346 && present_gnu_tree (gnat_temp)
347 && (current_function_decl
348 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
350 process_type (gnat_entity);
351 return get_gnu_tree (gnat_entity);
355 /* This abort means the Itype has an incorrect scope, i.e. that its
356 scope does not correspond to the subprogram it is declared in. */
357 gcc_unreachable ();
360 /* If we've already processed this entity, return what we got last time.
361 If we are defining the node, we should not have already processed it.
362 In that case, we will abort below when we try to save a new GCC tree
363 for this object. We also need to handle the case of getting a dummy
364 type when a Full_View exists but be careful so as not to trigger its
365 premature elaboration. */
366 if ((!definition || (is_type && imported_p))
367 && present_gnu_tree (gnat_entity))
369 gnu_decl = get_gnu_tree (gnat_entity);
371 if (TREE_CODE (gnu_decl) == TYPE_DECL
372 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
373 && IN (kind, Incomplete_Or_Private_Kind)
374 && Present (Full_View (gnat_entity))
375 && (present_gnu_tree (Full_View (gnat_entity))
376 || No (Freeze_Node (Full_View (gnat_entity)))))
378 gnu_decl
379 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, false);
380 save_gnu_tree (gnat_entity, NULL_TREE, false);
381 save_gnu_tree (gnat_entity, gnu_decl, false);
384 return gnu_decl;
387 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
388 must be specified unless it was specified by the programmer. Exceptions
389 are for access-to-protected-subprogram types and all access subtypes, as
390 another GNAT type is used to lay out the GCC type for them. */
391 gcc_assert (!Unknown_Esize (gnat_entity)
392 || Has_Size_Clause (gnat_entity)
393 || (!IN (kind, Numeric_Kind)
394 && !IN (kind, Enumeration_Kind)
395 && (!IN (kind, Access_Kind)
396 || kind == E_Access_Protected_Subprogram_Type
397 || kind == E_Anonymous_Access_Protected_Subprogram_Type
398 || kind == E_Access_Subtype
399 || type_annotate_only)));
401 /* The RM size must be specified for all discrete and fixed-point types. */
402 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
403 && Unknown_RM_Size (gnat_entity)));
405 /* If we get here, it means we have not yet done anything with this entity.
406 If we are not defining it, it must be a type or an entity that is defined
407 elsewhere or externally, otherwise we should have defined it already. */
408 gcc_assert (definition
409 || type_annotate_only
410 || is_type
411 || kind == E_Discriminant
412 || kind == E_Component
413 || kind == E_Label
414 || (kind == E_Constant && Present (Full_View (gnat_entity)))
415 || Is_Public (gnat_entity));
417 /* Get the name of the entity and set up the line number and filename of
418 the original definition for use in any decl we make. Make sure we do not
419 inherit another source location. */
420 gnu_entity_name = get_entity_name (gnat_entity);
421 if (Sloc (gnat_entity) != No_Location
422 && !renaming_from_generic_instantiation_p (gnat_entity))
423 Sloc_to_locus (Sloc (gnat_entity), &input_location);
425 /* For cases when we are not defining (i.e., we are referencing from
426 another compilation unit) public entities, show we are at global level
427 for the purpose of computing scopes. Don't do this for components or
428 discriminants since the relevant test is whether or not the record is
429 being defined. */
430 if (!definition
431 && kind != E_Component
432 && kind != E_Discriminant
433 && Is_Public (gnat_entity)
434 && !Is_Statically_Allocated (gnat_entity))
435 force_global++, this_global = true;
437 /* Handle any attributes directly attached to the entity. */
438 if (Has_Gigi_Rep_Item (gnat_entity))
439 prepend_attributes (&attr_list, gnat_entity);
441 /* Do some common processing for types. */
442 if (is_type)
444 /* Compute the equivalent type to be used in gigi. */
445 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
447 /* Machine_Attributes on types are expected to be propagated to
448 subtypes. The corresponding Gigi_Rep_Items are only attached
449 to the first subtype though, so we handle the propagation here. */
450 if (Base_Type (gnat_entity) != gnat_entity
451 && !Is_First_Subtype (gnat_entity)
452 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
453 prepend_attributes (&attr_list,
454 First_Subtype (Base_Type (gnat_entity)));
456 /* Compute a default value for the size of an elementary type. */
457 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
459 unsigned int max_esize;
461 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
462 esize = UI_To_Int (Esize (gnat_entity));
464 if (IN (kind, Float_Kind))
465 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
466 else if (IN (kind, Access_Kind))
467 max_esize = POINTER_SIZE * 2;
468 else
469 max_esize = LONG_LONG_TYPE_SIZE;
471 if (esize > max_esize)
472 esize = max_esize;
476 switch (kind)
478 case E_Component:
479 case E_Discriminant:
481 /* The GNAT record where the component was defined. */
482 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
484 /* If the entity is a discriminant of an extended tagged type used to
485 rename a discriminant of the parent type, return the latter. */
486 if (Is_Tagged_Type (gnat_record)
487 && Present (Corresponding_Discriminant (gnat_entity)))
489 gnu_decl
490 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
491 gnu_expr, definition);
492 saved = true;
493 break;
496 /* If the entity is an inherited component (in the case of extended
497 tagged record types), just return the original entity, which must
498 be a FIELD_DECL. Likewise for discriminants. If the entity is a
499 non-girder discriminant (in the case of derived untagged record
500 types), return the stored discriminant it renames. */
501 if (Present (Original_Record_Component (gnat_entity))
502 && Original_Record_Component (gnat_entity) != gnat_entity)
504 gnu_decl
505 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
506 gnu_expr, definition);
507 saved = true;
508 break;
511 /* Otherwise, if we are not defining this and we have no GCC type
512 for the containing record, make one for it. Then we should
513 have made our own equivalent. */
514 if (!definition && !present_gnu_tree (gnat_record))
516 /* ??? If this is in a record whose scope is a protected
517 type and we have an Original_Record_Component, use it.
518 This is a workaround for major problems in protected type
519 handling. */
520 Entity_Id Scop = Scope (Scope (gnat_entity));
521 if (Is_Protected_Type (Underlying_Type (Scop))
522 && Present (Original_Record_Component (gnat_entity)))
524 gnu_decl
525 = gnat_to_gnu_entity (Original_Record_Component
526 (gnat_entity),
527 gnu_expr, false);
529 else
531 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
532 gnu_decl = get_gnu_tree (gnat_entity);
535 saved = true;
536 break;
539 /* Here we have no GCC type and this is a reference rather than a
540 definition. This should never happen. Most likely the cause is
541 reference before declaration in the GNAT tree for gnat_entity. */
542 gcc_unreachable ();
545 case E_Constant:
546 /* Ignore constant definitions already marked with the error node. See
547 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
548 if (definition
549 && present_gnu_tree (gnat_entity)
550 && get_gnu_tree (gnat_entity) == error_mark_node)
552 maybe_present = true;
553 break;
556 /* Ignore deferred constant definitions without address clause since
557 they are processed fully in the front-end. If No_Initialization
558 is set, this is not a deferred constant but a constant whose value
559 is built manually. And constants that are renamings are handled
560 like variables. */
561 if (definition
562 && !gnu_expr
563 && No (Address_Clause (gnat_entity))
564 && !No_Initialization (Declaration_Node (gnat_entity))
565 && No (Renamed_Object (gnat_entity)))
567 gnu_decl = error_mark_node;
568 saved = true;
569 break;
572 /* If this is a use of a deferred constant without address clause,
573 get its full definition. */
574 if (!definition
575 && No (Address_Clause (gnat_entity))
576 && Present (Full_View (gnat_entity)))
578 gnu_decl
579 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
580 saved = true;
581 break;
584 /* If we have a constant that we are not defining, get the expression it
585 was defined to represent. This is necessary to avoid generating dumb
586 elaboration code in simple cases, but we may throw it away later if it
587 is not a constant. But do not retrieve it if it is an allocator since
588 the designated type might still be dummy at this point. */
589 if (!definition
590 && !No_Initialization (Declaration_Node (gnat_entity))
591 && Present (Expression (Declaration_Node (gnat_entity)))
592 && Nkind (Expression (Declaration_Node (gnat_entity)))
593 != N_Allocator)
594 /* The expression may contain N_Expression_With_Actions nodes and
595 thus object declarations from other units. Discard them. */
596 gnu_expr
597 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
599 /* fall through */
601 case E_Exception:
602 case E_Loop_Parameter:
603 case E_Out_Parameter:
604 case E_Variable:
606 const Entity_Id gnat_type = Etype (gnat_entity);
607 /* Always create a variable for volatile objects and variables seen
608 constant but with a Linker_Section pragma. */
609 bool const_flag
610 = ((kind == E_Constant || kind == E_Variable)
611 && Is_True_Constant (gnat_entity)
612 && !(kind == E_Variable
613 && Present (Linker_Section_Pragma (gnat_entity)))
614 && !Treat_As_Volatile (gnat_entity)
615 && (((Nkind (Declaration_Node (gnat_entity))
616 == N_Object_Declaration)
617 && Present (Expression (Declaration_Node (gnat_entity))))
618 || Present (Renamed_Object (gnat_entity))
619 || imported_p));
620 bool inner_const_flag = const_flag;
621 bool static_flag = Is_Statically_Allocated (gnat_entity);
622 /* We implement RM 13.3(19) for exported and imported (non-constant)
623 objects by making them volatile. */
624 bool volatile_flag
625 = (Treat_As_Volatile (gnat_entity)
626 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
627 bool mutable_p = false;
628 bool used_by_ref = false;
629 tree gnu_ext_name = NULL_TREE;
630 tree renamed_obj = NULL_TREE;
631 tree gnu_object_size;
633 /* We need to translate the renamed object even though we are only
634 referencing the renaming. But it may contain a call for which
635 we'll generate a temporary to hold the return value and which
636 is part of the definition of the renaming, so discard it. */
637 if (Present (Renamed_Object (gnat_entity)) && !definition)
639 if (kind == E_Exception)
640 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
641 NULL_TREE, false);
642 else
643 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
646 /* Get the type after elaborating the renamed object. */
647 if (Convention (gnat_entity) == Convention_C
648 && Is_Descendant_Of_Address (gnat_type))
649 gnu_type = ptr_type_node;
650 else
652 gnu_type = gnat_to_gnu_type (gnat_type);
654 /* If this is a standard exception definition, use the standard
655 exception type. This is necessary to make sure that imported
656 and exported views of exceptions are merged in LTO mode. */
657 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
658 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
659 gnu_type = except_type_node;
662 /* For a debug renaming declaration, build a debug-only entity. */
663 if (Present (Debug_Renaming_Link (gnat_entity)))
665 /* Force a non-null value to make sure the symbol is retained. */
666 tree value = build1 (INDIRECT_REF, gnu_type,
667 build1 (NOP_EXPR,
668 build_pointer_type (gnu_type),
669 integer_minus_one_node));
670 gnu_decl = build_decl (input_location,
671 VAR_DECL, gnu_entity_name, gnu_type);
672 SET_DECL_VALUE_EXPR (gnu_decl, value);
673 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
674 gnat_pushdecl (gnu_decl, gnat_entity);
675 break;
678 /* If this is a loop variable, its type should be the base type.
679 This is because the code for processing a loop determines whether
680 a normal loop end test can be done by comparing the bounds of the
681 loop against those of the base type, which is presumed to be the
682 size used for computation. But this is not correct when the size
683 of the subtype is smaller than the type. */
684 if (kind == E_Loop_Parameter)
685 gnu_type = get_base_type (gnu_type);
687 /* Reject non-renamed objects whose type is an unconstrained array or
688 any object whose type is a dummy type or void. */
689 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
690 && No (Renamed_Object (gnat_entity)))
691 || TYPE_IS_DUMMY_P (gnu_type)
692 || TREE_CODE (gnu_type) == VOID_TYPE)
694 gcc_assert (type_annotate_only);
695 if (this_global)
696 force_global--;
697 return error_mark_node;
700 /* If an alignment is specified, use it if valid. Note that exceptions
701 are objects but don't have an alignment. We must do this before we
702 validate the size, since the alignment can affect the size. */
703 if (kind != E_Exception && Known_Alignment (gnat_entity))
705 gcc_assert (Present (Alignment (gnat_entity)));
707 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
708 TYPE_ALIGN (gnu_type));
710 /* No point in changing the type if there is an address clause
711 as the final type of the object will be a reference type. */
712 if (Present (Address_Clause (gnat_entity)))
713 align = 0;
714 else
716 tree orig_type = gnu_type;
718 gnu_type
719 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
720 false, false, definition, true);
722 /* If a padding record was made, declare it now since it will
723 never be declared otherwise. This is necessary to ensure
724 that its subtrees are properly marked. */
725 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
726 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
727 debug_info_p, gnat_entity);
731 /* If we are defining the object, see if it has a Size and validate it
732 if so. If we are not defining the object and a Size clause applies,
733 simply retrieve the value. We don't want to ignore the clause and
734 it is expected to have been validated already. Then get the new
735 type, if any. */
736 if (definition)
737 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
738 gnat_entity, VAR_DECL, false,
739 Has_Size_Clause (gnat_entity));
740 else if (Has_Size_Clause (gnat_entity))
741 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
743 if (gnu_size)
745 gnu_type
746 = make_type_from_size (gnu_type, gnu_size,
747 Has_Biased_Representation (gnat_entity));
749 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
750 gnu_size = NULL_TREE;
753 /* If this object has self-referential size, it must be a record with
754 a default discriminant. We are supposed to allocate an object of
755 the maximum size in this case, unless it is a constant with an
756 initializing expression, in which case we can get the size from
757 that. Note that the resulting size may still be a variable, so
758 this may end up with an indirect allocation. */
759 if (No (Renamed_Object (gnat_entity))
760 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
762 if (gnu_expr && kind == E_Constant)
764 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
765 if (CONTAINS_PLACEHOLDER_P (size))
767 /* If the initializing expression is itself a constant,
768 despite having a nominal type with self-referential
769 size, we can get the size directly from it. */
770 if (TREE_CODE (gnu_expr) == COMPONENT_REF
771 && TYPE_IS_PADDING_P
772 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
773 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
774 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
775 || DECL_READONLY_ONCE_ELAB
776 (TREE_OPERAND (gnu_expr, 0))))
777 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
778 else
779 gnu_size
780 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
782 else
783 gnu_size = size;
785 /* We may have no GNU_EXPR because No_Initialization is
786 set even though there's an Expression. */
787 else if (kind == E_Constant
788 && (Nkind (Declaration_Node (gnat_entity))
789 == N_Object_Declaration)
790 && Present (Expression (Declaration_Node (gnat_entity))))
791 gnu_size
792 = TYPE_SIZE (gnat_to_gnu_type
793 (Etype
794 (Expression (Declaration_Node (gnat_entity)))));
795 else
797 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
798 mutable_p = true;
801 /* If the size isn't constant and we are at global level, call
802 elaborate_expression_1 to make a variable for it rather than
803 calculating it each time. */
804 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
805 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
806 "SIZE", definition, false);
809 /* If the size is zero byte, make it one byte since some linkers have
810 troubles with zero-sized objects. If the object will have a
811 template, that will make it nonzero so don't bother. Also avoid
812 doing that for an object renaming or an object with an address
813 clause, as we would lose useful information on the view size
814 (e.g. for null array slices) and we are not allocating the object
815 here anyway. */
816 if (((gnu_size
817 && integer_zerop (gnu_size)
818 && !TREE_OVERFLOW (gnu_size))
819 || (TYPE_SIZE (gnu_type)
820 && integer_zerop (TYPE_SIZE (gnu_type))
821 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
822 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
823 && No (Renamed_Object (gnat_entity))
824 && No (Address_Clause (gnat_entity)))
825 gnu_size = bitsize_unit_node;
827 /* If this is an object with no specified size and alignment, and
828 if either it is atomic or we are not optimizing alignment for
829 space and it is composite and not an exception, an Out parameter
830 or a reference to another object, and the size of its type is a
831 constant, set the alignment to the smallest one which is not
832 smaller than the size, with an appropriate cap. */
833 if (!gnu_size && align == 0
834 && (Is_Atomic_Or_VFA (gnat_entity)
835 || (!Optimize_Alignment_Space (gnat_entity)
836 && kind != E_Exception
837 && kind != E_Out_Parameter
838 && Is_Composite_Type (gnat_type)
839 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
840 && !Is_Exported (gnat_entity)
841 && !imported_p
842 && No (Renamed_Object (gnat_entity))
843 && No (Address_Clause (gnat_entity))))
844 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
846 unsigned int size_cap, align_cap;
848 /* No point in promoting the alignment if this doesn't prevent
849 BLKmode access to the object, in particular block copy, as
850 this will for example disable the NRV optimization for it.
851 No point in jumping through all the hoops needed in order
852 to support BIGGEST_ALIGNMENT if we don't really have to.
853 So we cap to the smallest alignment that corresponds to
854 a known efficient memory access pattern of the target. */
855 if (Is_Atomic_Or_VFA (gnat_entity))
857 size_cap = UINT_MAX;
858 align_cap = BIGGEST_ALIGNMENT;
860 else
862 size_cap = MAX_FIXED_MODE_SIZE;
863 align_cap = get_mode_alignment (ptr_mode);
866 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
867 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
868 align = 0;
869 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
870 align = align_cap;
871 else
872 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
874 /* But make sure not to under-align the object. */
875 if (align <= TYPE_ALIGN (gnu_type))
876 align = 0;
878 /* And honor the minimum valid atomic alignment, if any. */
879 #ifdef MINIMUM_ATOMIC_ALIGNMENT
880 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
881 align = MINIMUM_ATOMIC_ALIGNMENT;
882 #endif
885 /* If the object is set to have atomic components, find the component
886 type and validate it.
888 ??? Note that we ignore Has_Volatile_Components on objects; it's
889 not at all clear what to do in that case. */
890 if (Has_Atomic_Components (gnat_entity))
892 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
893 ? TREE_TYPE (gnu_type) : gnu_type);
895 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
896 && TYPE_MULTI_ARRAY_P (gnu_inner))
897 gnu_inner = TREE_TYPE (gnu_inner);
899 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
902 /* If this is an aliased object with an unconstrained array nominal
903 subtype, make a type that includes the template. We will either
904 allocate or create a variable of that type, see below. */
905 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
906 && Is_Array_Type (Underlying_Type (gnat_type))
907 && !type_annotate_only)
909 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
910 gnu_type
911 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
912 gnu_type,
913 concat_name (gnu_entity_name,
914 "UNC"),
915 debug_info_p);
918 /* ??? If this is an object of CW type initialized to a value, try to
919 ensure that the object is sufficient aligned for this value, but
920 without pessimizing the allocation. This is a kludge necessary
921 because we don't support dynamic alignment. */
922 if (align == 0
923 && Ekind (gnat_type) == E_Class_Wide_Subtype
924 && No (Renamed_Object (gnat_entity))
925 && No (Address_Clause (gnat_entity)))
926 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
928 #ifdef MINIMUM_ATOMIC_ALIGNMENT
929 /* If the size is a constant and no alignment is specified, force
930 the alignment to be the minimum valid atomic alignment. The
931 restriction on constant size avoids problems with variable-size
932 temporaries; if the size is variable, there's no issue with
933 atomic access. Also don't do this for a constant, since it isn't
934 necessary and can interfere with constant replacement. Finally,
935 do not do it for Out parameters since that creates an
936 size inconsistency with In parameters. */
937 if (align == 0
938 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
939 && !FLOAT_TYPE_P (gnu_type)
940 && !const_flag && No (Renamed_Object (gnat_entity))
941 && !imported_p && No (Address_Clause (gnat_entity))
942 && kind != E_Out_Parameter
943 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
944 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
945 align = MINIMUM_ATOMIC_ALIGNMENT;
946 #endif
948 /* Make a new type with the desired size and alignment, if needed.
949 But do not take into account alignment promotions to compute the
950 size of the object. */
951 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
952 if (gnu_size || align > 0)
954 tree orig_type = gnu_type;
956 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
957 false, false, definition, true);
959 /* If a padding record was made, declare it now since it will
960 never be declared otherwise. This is necessary to ensure
961 that its subtrees are properly marked. */
962 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
963 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
964 debug_info_p, gnat_entity);
967 /* Now check if the type of the object allows atomic access. */
968 if (Is_Atomic_Or_VFA (gnat_entity))
969 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
971 /* If this is a renaming, avoid as much as possible to create a new
972 object. However, in some cases, creating it is required because
973 renaming can be applied to objects that are not names in Ada.
974 This processing needs to be applied to the raw expression so as
975 to make it more likely to rename the underlying object. */
976 if (Present (Renamed_Object (gnat_entity)))
978 /* If the renamed object had padding, strip off the reference to
979 the inner object and reset our type. */
980 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
981 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
982 /* Strip useless conversions around the object. */
983 || gnat_useless_type_conversion (gnu_expr))
985 gnu_expr = TREE_OPERAND (gnu_expr, 0);
986 gnu_type = TREE_TYPE (gnu_expr);
989 /* Or else, if the renamed object has an unconstrained type with
990 default discriminant, use the padded type. */
991 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
992 gnu_type = TREE_TYPE (gnu_expr);
994 /* Case 1: if this is a constant renaming stemming from a function
995 call, treat it as a normal object whose initial value is what
996 is being renamed. RM 3.3 says that the result of evaluating a
997 function call is a constant object. Therefore, it can be the
998 inner object of a constant renaming and the renaming must be
999 fully instantiated, i.e. it cannot be a reference to (part of)
1000 an existing object. And treat other rvalues (addresses, null
1001 expressions, constructors and literals) the same way. */
1002 tree inner = gnu_expr;
1003 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
1004 inner = TREE_OPERAND (inner, 0);
1005 /* Expand_Dispatching_Call can prepend a comparison of the tags
1006 before the call to "=". */
1007 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
1008 || TREE_CODE (inner) == COMPOUND_EXPR)
1009 inner = TREE_OPERAND (inner, 1);
1010 if ((TREE_CODE (inner) == CALL_EXPR
1011 && !call_is_atomic_load (inner))
1012 || TREE_CODE (inner) == ADDR_EXPR
1013 || TREE_CODE (inner) == NULL_EXPR
1014 || TREE_CODE (inner) == PLUS_EXPR
1015 || TREE_CODE (inner) == CONSTRUCTOR
1016 || CONSTANT_CLASS_P (inner)
1017 /* We need to detect the case where a temporary is created to
1018 hold the return value, since we cannot safely rename it at
1019 top level as it lives only in the elaboration routine. */
1020 || (TREE_CODE (inner) == VAR_DECL
1021 && DECL_RETURN_VALUE_P (inner))
1022 /* We also need to detect the case where the front-end creates
1023 a dangling 'reference to a function call at top level and
1024 substitutes it in the renaming, for example:
1026 q__b : boolean renames r__f.e (1);
1028 can be rewritten into:
1030 q__R1s : constant q__A2s := r__f'reference;
1031 [...]
1032 q__b : boolean renames q__R1s.all.e (1);
1034 We cannot safely rename the rewritten expression since the
1035 underlying object lives only in the elaboration routine. */
1036 || (TREE_CODE (inner) == INDIRECT_REF
1037 && (inner
1038 = remove_conversions (TREE_OPERAND (inner, 0), true))
1039 && TREE_CODE (inner) == VAR_DECL
1040 && DECL_RETURN_VALUE_P (inner)))
1043 /* Case 2: if the renaming entity need not be materialized, use
1044 the elaborated renamed expression for the renaming. But this
1045 means that the caller is responsible for evaluating the address
1046 of the renaming in the correct place for the definition case to
1047 instantiate the SAVE_EXPRs. */
1048 else if (!Materialize_Entity (gnat_entity))
1050 tree init = NULL_TREE;
1052 gnu_decl
1053 = elaborate_reference (gnu_expr, gnat_entity, definition,
1054 &init);
1056 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1057 correct place for this case. */
1058 gcc_assert (!init);
1060 /* No DECL_EXPR will be created so the expression needs to be
1061 marked manually because it will likely be shared. */
1062 if (global_bindings_p ())
1063 MARK_VISITED (gnu_decl);
1065 /* This assertion will fail if the renamed object isn't aligned
1066 enough as to make it possible to honor the alignment set on
1067 the renaming. */
1068 if (align)
1070 unsigned int ralign = DECL_P (gnu_decl)
1071 ? DECL_ALIGN (gnu_decl)
1072 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1073 gcc_assert (ralign >= align);
1076 /* The expression might not be a DECL so save it manually. */
1077 save_gnu_tree (gnat_entity, gnu_decl, true);
1078 saved = true;
1079 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1080 break;
1083 /* Case 3: otherwise, make a constant pointer to the object we
1084 are renaming and attach the object to the pointer after it is
1085 elaborated. The object will be referenced directly instead
1086 of indirectly via the pointer to avoid aliasing problems with
1087 non-addressable entities. The pointer is called a "renaming"
1088 pointer in this case. Note that we also need to preserve the
1089 volatility of the renamed object through the indirection. */
1090 else
1092 tree init = NULL_TREE;
1094 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1095 gnu_type
1096 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1097 gnu_type = build_reference_type (gnu_type);
1098 used_by_ref = true;
1099 const_flag = true;
1100 volatile_flag = false;
1101 inner_const_flag = TREE_READONLY (gnu_expr);
1102 gnu_size = NULL_TREE;
1104 renamed_obj
1105 = elaborate_reference (gnu_expr, gnat_entity, definition,
1106 &init);
1108 /* The expression needs to be marked manually because it will
1109 likely be shared, even for a definition since the ADDR_EXPR
1110 built below can cause the first few nodes to be folded. */
1111 if (global_bindings_p ())
1112 MARK_VISITED (renamed_obj);
1114 if (type_annotate_only
1115 && TREE_CODE (renamed_obj) == ERROR_MARK)
1116 gnu_expr = NULL_TREE;
1117 else
1119 gnu_expr
1120 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1121 if (init)
1122 gnu_expr
1123 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1124 gnu_expr);
1129 /* If we are defining an aliased object whose nominal subtype is
1130 unconstrained, the object is a record that contains both the
1131 template and the object. If there is an initializer, it will
1132 have already been converted to the right type, but we need to
1133 create the template if there is no initializer. */
1134 if (definition
1135 && !gnu_expr
1136 && TREE_CODE (gnu_type) == RECORD_TYPE
1137 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1138 /* Beware that padding might have been introduced above. */
1139 || (TYPE_PADDING_P (gnu_type)
1140 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1141 == RECORD_TYPE
1142 && TYPE_CONTAINS_TEMPLATE_P
1143 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1145 tree template_field
1146 = TYPE_PADDING_P (gnu_type)
1147 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1148 : TYPE_FIELDS (gnu_type);
1149 vec<constructor_elt, va_gc> *v;
1150 vec_alloc (v, 1);
1151 tree t = build_template (TREE_TYPE (template_field),
1152 TREE_TYPE (DECL_CHAIN (template_field)),
1153 NULL_TREE);
1154 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1155 gnu_expr = gnat_build_constructor (gnu_type, v);
1158 /* Convert the expression to the type of the object if need be. */
1159 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1160 gnu_expr = convert (gnu_type, gnu_expr);
1162 /* If this is a pointer that doesn't have an initializing expression,
1163 initialize it to NULL, unless the object is declared imported as
1164 per RM B.1(24). */
1165 if (definition
1166 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1167 && !gnu_expr
1168 && !Is_Imported (gnat_entity))
1169 gnu_expr = integer_zero_node;
1171 /* If we are defining the object and it has an Address clause, we must
1172 either get the address expression from the saved GCC tree for the
1173 object if it has a Freeze node, or elaborate the address expression
1174 here since the front-end has guaranteed that the elaboration has no
1175 effects in this case. */
1176 if (definition && Present (Address_Clause (gnat_entity)))
1178 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1179 Node_Id gnat_address = Expression (gnat_clause);
1180 tree gnu_address
1181 = present_gnu_tree (gnat_entity)
1182 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1184 save_gnu_tree (gnat_entity, NULL_TREE, false);
1186 /* Convert the type of the object to a reference type that can
1187 alias everything as per RM 13.3(19). */
1188 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1189 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1190 gnu_type
1191 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1192 gnu_address = convert (gnu_type, gnu_address);
1193 used_by_ref = true;
1194 const_flag
1195 = (!Is_Public (gnat_entity)
1196 || compile_time_known_address_p (gnat_address));
1197 volatile_flag = false;
1198 gnu_size = NULL_TREE;
1200 /* If this is an aliased object with an unconstrained array nominal
1201 subtype, then it can overlay only another aliased object with an
1202 unconstrained array nominal subtype and compatible template. */
1203 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1204 && Is_Array_Type (Underlying_Type (gnat_type))
1205 && !type_annotate_only)
1207 tree rec_type = TREE_TYPE (gnu_type);
1208 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1210 /* This is the pattern built for a regular object. */
1211 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1212 && TREE_OPERAND (gnu_address, 1) == off)
1213 gnu_address = TREE_OPERAND (gnu_address, 0);
1214 /* This is the pattern built for an overaligned object. */
1215 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1216 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1217 == PLUS_EXPR
1218 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1219 == off)
1220 gnu_address
1221 = build2 (POINTER_PLUS_EXPR, gnu_type,
1222 TREE_OPERAND (gnu_address, 0),
1223 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1224 else
1226 post_error_ne ("aliased object& with unconstrained array "
1227 "nominal subtype", gnat_clause,
1228 gnat_entity);
1229 post_error ("\\can overlay only aliased object with "
1230 "compatible subtype", gnat_clause);
1234 /* If we don't have an initializing expression for the underlying
1235 variable, the initializing expression for the pointer is the
1236 specified address. Otherwise, we have to make a COMPOUND_EXPR
1237 to assign both the address and the initial value. */
1238 if (!gnu_expr)
1239 gnu_expr = gnu_address;
1240 else
1241 gnu_expr
1242 = build2 (COMPOUND_EXPR, gnu_type,
1243 build_binary_op (INIT_EXPR, NULL_TREE,
1244 build_unary_op (INDIRECT_REF,
1245 NULL_TREE,
1246 gnu_address),
1247 gnu_expr),
1248 gnu_address);
1251 /* If it has an address clause and we are not defining it, mark it
1252 as an indirect object. Likewise for Stdcall objects that are
1253 imported. */
1254 if ((!definition && Present (Address_Clause (gnat_entity)))
1255 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1257 /* Convert the type of the object to a reference type that can
1258 alias everything as per RM 13.3(19). */
1259 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1260 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1261 gnu_type
1262 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1263 used_by_ref = true;
1264 const_flag = false;
1265 volatile_flag = false;
1266 gnu_size = NULL_TREE;
1268 /* No point in taking the address of an initializing expression
1269 that isn't going to be used. */
1270 gnu_expr = NULL_TREE;
1272 /* If it has an address clause whose value is known at compile
1273 time, make the object a CONST_DECL. This will avoid a
1274 useless dereference. */
1275 if (Present (Address_Clause (gnat_entity)))
1277 Node_Id gnat_address
1278 = Expression (Address_Clause (gnat_entity));
1280 if (compile_time_known_address_p (gnat_address))
1282 gnu_expr = gnat_to_gnu (gnat_address);
1283 const_flag = true;
1288 /* If we are at top level and this object is of variable size,
1289 make the actual type a hidden pointer to the real type and
1290 make the initializer be a memory allocation and initialization.
1291 Likewise for objects we aren't defining (presumed to be
1292 external references from other packages), but there we do
1293 not set up an initialization.
1295 If the object's size overflows, make an allocator too, so that
1296 Storage_Error gets raised. Note that we will never free
1297 such memory, so we presume it never will get allocated. */
1298 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1299 global_bindings_p ()
1300 || !definition
1301 || static_flag)
1302 || (gnu_size
1303 && !allocatable_size_p (convert (sizetype,
1304 size_binop
1305 (CEIL_DIV_EXPR, gnu_size,
1306 bitsize_unit_node)),
1307 global_bindings_p ()
1308 || !definition
1309 || static_flag)))
1311 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1312 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1313 gnu_type = build_reference_type (gnu_type);
1314 used_by_ref = true;
1315 const_flag = true;
1316 volatile_flag = false;
1317 gnu_size = NULL_TREE;
1319 /* In case this was a aliased object whose nominal subtype is
1320 unconstrained, the pointer above will be a thin pointer and
1321 build_allocator will automatically make the template.
1323 If we have a template initializer only (that we made above),
1324 pretend there is none and rely on what build_allocator creates
1325 again anyway. Otherwise (if we have a full initializer), get
1326 the data part and feed that to build_allocator.
1328 If we are elaborating a mutable object, tell build_allocator to
1329 ignore a possibly simpler size from the initializer, if any, as
1330 we must allocate the maximum possible size in this case. */
1331 if (definition && !imported_p)
1333 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1335 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1336 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1338 gnu_alloc_type
1339 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1341 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1342 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1343 gnu_expr = NULL_TREE;
1344 else
1345 gnu_expr
1346 = build_component_ref
1347 (gnu_expr,
1348 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1349 false);
1352 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1353 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1354 post_error ("?`Storage_Error` will be raised at run time!",
1355 gnat_entity);
1357 gnu_expr
1358 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1359 Empty, Empty, gnat_entity, mutable_p);
1361 else
1362 gnu_expr = NULL_TREE;
1365 /* If this object would go into the stack and has an alignment larger
1366 than the largest stack alignment the back-end can honor, resort to
1367 a variable of "aligning type". */
1368 if (definition
1369 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1370 && !imported_p
1371 && !static_flag
1372 && !global_bindings_p ())
1374 /* Create the new variable. No need for extra room before the
1375 aligned field as this is in automatic storage. */
1376 tree gnu_new_type
1377 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1378 TYPE_SIZE_UNIT (gnu_type),
1379 BIGGEST_ALIGNMENT, 0, gnat_entity);
1380 tree gnu_new_var
1381 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1382 NULL_TREE, gnu_new_type, NULL_TREE,
1383 false, false, false, false, false,
1384 true, debug_info_p, NULL, gnat_entity);
1386 /* Initialize the aligned field if we have an initializer. */
1387 if (gnu_expr)
1388 add_stmt_with_node
1389 (build_binary_op (INIT_EXPR, NULL_TREE,
1390 build_component_ref
1391 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1392 false),
1393 gnu_expr),
1394 gnat_entity);
1396 /* And setup this entity as a reference to the aligned field. */
1397 gnu_type = build_reference_type (gnu_type);
1398 gnu_expr
1399 = build_unary_op
1400 (ADDR_EXPR, NULL_TREE,
1401 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1402 false));
1403 TREE_CONSTANT (gnu_expr) = 1;
1405 used_by_ref = true;
1406 const_flag = true;
1407 volatile_flag = false;
1408 gnu_size = NULL_TREE;
1411 /* If this is an aliased object with an unconstrained array nominal
1412 subtype, we make its type a thin reference, i.e. the reference
1413 counterpart of a thin pointer, so it points to the array part.
1414 This is aimed to make it easier for the debugger to decode the
1415 object. Note that we have to do it this late because of the
1416 couple of allocation adjustments that might be made above. */
1417 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1418 && Is_Array_Type (Underlying_Type (gnat_type))
1419 && !type_annotate_only)
1421 /* In case the object with the template has already been allocated
1422 just above, we have nothing to do here. */
1423 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1425 /* This variable is a GNAT encoding used by Workbench: let it
1426 go through the debugging information but mark it as
1427 artificial: users are not interested in it. */
1428 tree gnu_unc_var
1429 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1430 NULL_TREE, gnu_type, gnu_expr,
1431 const_flag, Is_Public (gnat_entity),
1432 imported_p || !definition, static_flag,
1433 volatile_flag, true, debug_info_p,
1434 NULL, gnat_entity);
1435 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1436 TREE_CONSTANT (gnu_expr) = 1;
1438 used_by_ref = true;
1439 const_flag = true;
1440 volatile_flag = false;
1441 inner_const_flag = TREE_READONLY (gnu_unc_var);
1442 gnu_size = NULL_TREE;
1445 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1446 gnu_type
1447 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1450 /* Convert the expression to the type of the object if need be. */
1451 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1452 gnu_expr = convert (gnu_type, gnu_expr);
1454 /* If this name is external or a name was specified, use it, but don't
1455 use the Interface_Name with an address clause (see cd30005). */
1456 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1457 || (Present (Interface_Name (gnat_entity))
1458 && No (Address_Clause (gnat_entity))))
1459 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1461 /* If this is an aggregate constant initialized to a constant, force it
1462 to be statically allocated. This saves an initialization copy. */
1463 if (!static_flag
1464 && const_flag
1465 && gnu_expr && TREE_CONSTANT (gnu_expr)
1466 && AGGREGATE_TYPE_P (gnu_type)
1467 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1468 && !(TYPE_IS_PADDING_P (gnu_type)
1469 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1470 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1471 static_flag = true;
1473 /* Deal with a pragma Linker_Section on a constant or variable. */
1474 if ((kind == E_Constant || kind == E_Variable)
1475 && Present (Linker_Section_Pragma (gnat_entity)))
1476 prepend_one_attribute_pragma (&attr_list,
1477 Linker_Section_Pragma (gnat_entity));
1479 /* Now create the variable or the constant and set various flags. */
1480 gnu_decl
1481 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1482 gnu_expr, const_flag, Is_Public (gnat_entity),
1483 imported_p || !definition, static_flag,
1484 volatile_flag, artificial_p, debug_info_p,
1485 attr_list, gnat_entity, !renamed_obj);
1486 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1487 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1488 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1490 /* If we are defining an Out parameter and optimization isn't enabled,
1491 create a fake PARM_DECL for debugging purposes and make it point to
1492 the VAR_DECL. Suppress debug info for the latter but make sure it
1493 will live in memory so that it can be accessed from within the
1494 debugger through the PARM_DECL. */
1495 if (kind == E_Out_Parameter
1496 && definition
1497 && debug_info_p
1498 && !optimize
1499 && !flag_generate_lto)
1501 tree param = create_param_decl (gnu_entity_name, gnu_type);
1502 gnat_pushdecl (param, gnat_entity);
1503 SET_DECL_VALUE_EXPR (param, gnu_decl);
1504 DECL_HAS_VALUE_EXPR_P (param) = 1;
1505 DECL_IGNORED_P (gnu_decl) = 1;
1506 TREE_ADDRESSABLE (gnu_decl) = 1;
1509 /* If this is a loop parameter, set the corresponding flag. */
1510 else if (kind == E_Loop_Parameter)
1511 DECL_LOOP_PARM_P (gnu_decl) = 1;
1513 /* If this is a renaming pointer, attach the renamed object to it. */
1514 if (renamed_obj)
1515 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1517 /* If this is a constant and we are defining it or it generates a real
1518 symbol at the object level and we are referencing it, we may want
1519 or need to have a true variable to represent it:
1520 - if optimization isn't enabled, for debugging purposes,
1521 - if the constant is public and not overlaid on something else,
1522 - if its address is taken,
1523 - if either itself or its type is aliased. */
1524 if (TREE_CODE (gnu_decl) == CONST_DECL
1525 && (definition || Sloc (gnat_entity) > Standard_Location)
1526 && ((!optimize && debug_info_p)
1527 || (Is_Public (gnat_entity)
1528 && No (Address_Clause (gnat_entity)))
1529 || Address_Taken (gnat_entity)
1530 || Is_Aliased (gnat_entity)
1531 || Is_Aliased (gnat_type)))
1533 tree gnu_corr_var
1534 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1535 gnu_expr, true, Is_Public (gnat_entity),
1536 !definition, static_flag, volatile_flag,
1537 artificial_p, debug_info_p, attr_list,
1538 gnat_entity, false);
1540 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1543 /* If this is a constant, even if we don't need a true variable, we
1544 may need to avoid returning the initializer in every case. That
1545 can happen for the address of a (constant) constructor because,
1546 upon dereferencing it, the constructor will be reinjected in the
1547 tree, which may not be valid in every case; see lvalue_required_p
1548 for more details. */
1549 if (TREE_CODE (gnu_decl) == CONST_DECL)
1550 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1552 /* If this object is declared in a block that contains a block with an
1553 exception handler, and we aren't using the GCC exception mechanism,
1554 we must force this variable in memory in order to avoid an invalid
1555 optimization. */
1556 if (Front_End_Exceptions ()
1557 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1558 TREE_ADDRESSABLE (gnu_decl) = 1;
1560 /* If this is a local variable with non-BLKmode and aggregate type,
1561 and optimization isn't enabled, then force it in memory so that
1562 a register won't be allocated to it with possible subparts left
1563 uninitialized and reaching the register allocator. */
1564 else if (TREE_CODE (gnu_decl) == VAR_DECL
1565 && !DECL_EXTERNAL (gnu_decl)
1566 && !TREE_STATIC (gnu_decl)
1567 && DECL_MODE (gnu_decl) != BLKmode
1568 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1569 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1570 && !optimize)
1571 TREE_ADDRESSABLE (gnu_decl) = 1;
1573 /* If we are defining an object with variable size or an object with
1574 fixed size that will be dynamically allocated, and we are using the
1575 front-end setjmp/longjmp exception mechanism, update the setjmp
1576 buffer. */
1577 if (definition
1578 && Exception_Mechanism == Front_End_SJLJ
1579 && get_block_jmpbuf_decl ()
1580 && DECL_SIZE_UNIT (gnu_decl)
1581 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1582 || (flag_stack_check == GENERIC_STACK_CHECK
1583 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1584 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1585 add_stmt_with_node (build_call_n_expr
1586 (update_setjmp_buf_decl, 1,
1587 build_unary_op (ADDR_EXPR, NULL_TREE,
1588 get_block_jmpbuf_decl ())),
1589 gnat_entity);
1591 /* Back-annotate Esize and Alignment of the object if not already
1592 known. Note that we pick the values of the type, not those of
1593 the object, to shield ourselves from low-level platform-dependent
1594 adjustments like alignment promotion. This is both consistent with
1595 all the treatment above, where alignment and size are set on the
1596 type of the object and not on the object directly, and makes it
1597 possible to support all confirming representation clauses. */
1598 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1599 used_by_ref);
1601 break;
1603 case E_Void:
1604 /* Return a TYPE_DECL for "void" that we previously made. */
1605 gnu_decl = TYPE_NAME (void_type_node);
1606 break;
1608 case E_Enumeration_Type:
1609 /* A special case: for the types Character and Wide_Character in
1610 Standard, we do not list all the literals. So if the literals
1611 are not specified, make this an integer type. */
1612 if (No (First_Literal (gnat_entity)))
1614 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1615 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1616 else
1617 gnu_type = make_unsigned_type (esize);
1618 TYPE_NAME (gnu_type) = gnu_entity_name;
1620 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1621 This is needed by the DWARF-2 back-end to distinguish between
1622 unsigned integer types and character types. */
1623 TYPE_STRING_FLAG (gnu_type) = 1;
1625 /* This flag is needed by the call just below. */
1626 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1628 finish_character_type (gnu_type);
1630 else
1632 /* We have a list of enumeral constants in First_Literal. We make a
1633 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1634 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1635 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1636 value of the literal. But when we have a regular boolean type, we
1637 simplify this a little by using a BOOLEAN_TYPE. */
1638 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1639 && !Has_Non_Standard_Rep (gnat_entity);
1640 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1641 tree gnu_list = NULL_TREE;
1642 Entity_Id gnat_literal;
1644 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1645 TYPE_PRECISION (gnu_type) = esize;
1646 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1647 set_min_and_max_values_for_integral_type (gnu_type, esize,
1648 TYPE_SIGN (gnu_type));
1649 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1650 layout_type (gnu_type);
1652 for (gnat_literal = First_Literal (gnat_entity);
1653 Present (gnat_literal);
1654 gnat_literal = Next_Literal (gnat_literal))
1656 tree gnu_value
1657 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1658 /* Do not generate debug info for individual enumerators. */
1659 tree gnu_literal
1660 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1661 gnu_type, gnu_value, true, false, false,
1662 false, false, artificial_p, false,
1663 NULL, gnat_literal);
1664 save_gnu_tree (gnat_literal, gnu_literal, false);
1665 gnu_list
1666 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1669 if (!is_boolean)
1670 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1672 /* Note that the bounds are updated at the end of this function
1673 to avoid an infinite recursion since they refer to the type. */
1674 goto discrete_type;
1676 break;
1678 case E_Signed_Integer_Type:
1679 /* For integer types, just make a signed type the appropriate number
1680 of bits. */
1681 gnu_type = make_signed_type (esize);
1682 goto discrete_type;
1684 case E_Ordinary_Fixed_Point_Type:
1685 case E_Decimal_Fixed_Point_Type:
1687 /* Small_Value is the scale factor. */
1688 const Ureal gnat_small_value = Small_Value (gnat_entity);
1689 tree scale_factor = NULL_TREE;
1691 gnu_type = make_signed_type (esize);
1693 /* Try to decode the scale factor and to save it for the fixed-point
1694 types debug hook. */
1696 /* There are various ways to describe the scale factor, however there
1697 are cases where back-end internals cannot hold it. In such cases,
1698 we output invalid scale factor for such cases (i.e. the 0/0
1699 rational constant) but we expect GNAT to output GNAT encodings,
1700 then. Thus, keep this in sync with
1701 Exp_Dbug.Is_Handled_Scale_Factor. */
1703 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1704 binary or decimal scale: it is easier to read for humans. */
1705 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1706 && (Rbase (gnat_small_value) == 2
1707 || Rbase (gnat_small_value) == 10))
1709 /* Given RM restrictions on 'Small values, we assume here that
1710 the denominator fits in an int. */
1711 const tree base = build_int_cst (integer_type_node,
1712 Rbase (gnat_small_value));
1713 const tree exponent
1714 = build_int_cst (integer_type_node,
1715 UI_To_Int (Denominator (gnat_small_value)));
1716 scale_factor
1717 = build2 (RDIV_EXPR, integer_type_node,
1718 integer_one_node,
1719 build2 (POWER_EXPR, integer_type_node,
1720 base, exponent));
1723 /* Default to arbitrary scale factors descriptions. */
1724 else
1726 const Uint num = Norm_Num (gnat_small_value);
1727 const Uint den = Norm_Den (gnat_small_value);
1729 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1731 const tree gnu_num
1732 = build_int_cst (integer_type_node,
1733 UI_To_Int (Norm_Num (gnat_small_value)));
1734 const tree gnu_den
1735 = build_int_cst (integer_type_node,
1736 UI_To_Int (Norm_Den (gnat_small_value)));
1737 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1738 gnu_num, gnu_den);
1740 else
1741 /* If compiler internals cannot represent arbitrary scale
1742 factors, output an invalid scale factor so that debugger
1743 don't try to handle them but so that we still have a type
1744 in the output. Note that GNAT */
1745 scale_factor = integer_zero_node;
1748 TYPE_FIXED_POINT_P (gnu_type) = 1;
1749 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1751 goto discrete_type;
1753 case E_Modular_Integer_Type:
1755 /* For modular types, make the unsigned type of the proper number
1756 of bits and then set up the modulus, if required. */
1757 tree gnu_modulus, gnu_high = NULL_TREE;
1759 /* Packed Array Impl. Types are supposed to be subtypes only. */
1760 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1762 gnu_type = make_unsigned_type (esize);
1764 /* Get the modulus in this type. If it overflows, assume it is because
1765 it is equal to 2**Esize. Note that there is no overflow checking
1766 done on unsigned type, so we detect the overflow by looking for
1767 a modulus of zero, which is otherwise invalid. */
1768 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1770 if (!integer_zerop (gnu_modulus))
1772 TYPE_MODULAR_P (gnu_type) = 1;
1773 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1774 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1775 build_int_cst (gnu_type, 1));
1778 /* If the upper bound is not maximal, make an extra subtype. */
1779 if (gnu_high
1780 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1782 tree gnu_subtype = make_unsigned_type (esize);
1783 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1784 TREE_TYPE (gnu_subtype) = gnu_type;
1785 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1786 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1787 gnu_type = gnu_subtype;
1790 goto discrete_type;
1792 case E_Signed_Integer_Subtype:
1793 case E_Enumeration_Subtype:
1794 case E_Modular_Integer_Subtype:
1795 case E_Ordinary_Fixed_Point_Subtype:
1796 case E_Decimal_Fixed_Point_Subtype:
1798 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1799 not want to call create_range_type since we would like each subtype
1800 node to be distinct. ??? Historically this was in preparation for
1801 when memory aliasing is implemented, but that's obsolete now given
1802 the call to relate_alias_sets below.
1804 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1805 this fact is used by the arithmetic conversion functions.
1807 We elaborate the Ancestor_Subtype if it is not in the current unit
1808 and one of our bounds is non-static. We do this to ensure consistent
1809 naming in the case where several subtypes share the same bounds, by
1810 elaborating the first such subtype first, thus using its name. */
1812 if (!definition
1813 && Present (Ancestor_Subtype (gnat_entity))
1814 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1815 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1816 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1817 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1819 /* Set the precision to the Esize except for bit-packed arrays. */
1820 if (Is_Packed_Array_Impl_Type (gnat_entity)
1821 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1822 esize = UI_To_Int (RM_Size (gnat_entity));
1824 /* First subtypes of Character are treated as Character; otherwise
1825 this should be an unsigned type if the base type is unsigned or
1826 if the lower bound is constant and non-negative or if the type
1827 is biased. However, even if the lower bound is constant and
1828 non-negative, we use a signed type for a subtype with the same
1829 size as its signed base type, because this eliminates useless
1830 conversions to it and gives more leeway to the optimizer; but
1831 this means that we will need to explicitly test for this case
1832 when we change the representation based on the RM size. */
1833 if (kind == E_Enumeration_Subtype
1834 && No (First_Literal (Etype (gnat_entity)))
1835 && Esize (gnat_entity) == RM_Size (gnat_entity)
1836 && esize == CHAR_TYPE_SIZE
1837 && flag_signed_char)
1838 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1839 else if (Is_Unsigned_Type (Etype (gnat_entity))
1840 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1841 && Is_Unsigned_Type (gnat_entity))
1842 || Has_Biased_Representation (gnat_entity))
1843 gnu_type = make_unsigned_type (esize);
1844 else
1845 gnu_type = make_signed_type (esize);
1846 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1848 SET_TYPE_RM_MIN_VALUE
1849 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1850 gnat_entity, "L", definition, true,
1851 debug_info_p));
1853 SET_TYPE_RM_MAX_VALUE
1854 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1855 gnat_entity, "U", definition, true,
1856 debug_info_p));
1858 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1859 = Has_Biased_Representation (gnat_entity);
1861 /* Set TYPE_STRING_FLAG for Character and Wide_Character subtypes. */
1862 TYPE_STRING_FLAG (gnu_type) = TYPE_STRING_FLAG (TREE_TYPE (gnu_type));
1864 /* Inherit our alias set from what we're a subtype of. Subtypes
1865 are not different types and a pointer can designate any instance
1866 within a subtype hierarchy. */
1867 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1869 /* One of the above calls might have caused us to be elaborated,
1870 so don't blow up if so. */
1871 if (present_gnu_tree (gnat_entity))
1873 maybe_present = true;
1874 break;
1877 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1878 TYPE_STUB_DECL (gnu_type)
1879 = create_type_stub_decl (gnu_entity_name, gnu_type);
1881 /* For a packed array, make the original array type a parallel/debug
1882 type. */
1883 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1884 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1886 discrete_type:
1888 /* We have to handle clauses that under-align the type specially. */
1889 if ((Present (Alignment_Clause (gnat_entity))
1890 || (Is_Packed_Array_Impl_Type (gnat_entity)
1891 && Present
1892 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1893 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1895 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1896 if (align >= TYPE_ALIGN (gnu_type))
1897 align = 0;
1900 /* If the type we are dealing with represents a bit-packed array,
1901 we need to have the bits left justified on big-endian targets
1902 and right justified on little-endian targets. We also need to
1903 ensure that when the value is read (e.g. for comparison of two
1904 such values), we only get the good bits, since the unused bits
1905 are uninitialized. Both goals are accomplished by wrapping up
1906 the modular type in an enclosing record type. */
1907 if (Is_Packed_Array_Impl_Type (gnat_entity)
1908 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1910 tree gnu_field_type, gnu_field;
1912 /* Set the RM size before wrapping up the original type. */
1913 SET_TYPE_RM_SIZE (gnu_type,
1914 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1915 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1917 /* Strip the ___XP suffix for standard DWARF. */
1918 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1919 gnu_entity_name = TYPE_NAME (gnu_type);
1921 /* Create a stripped-down declaration, mainly for debugging. */
1922 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1923 gnat_entity);
1925 /* Now save it and build the enclosing record type. */
1926 gnu_field_type = gnu_type;
1928 gnu_type = make_node (RECORD_TYPE);
1929 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1930 TYPE_PACKED (gnu_type) = 1;
1931 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1932 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1933 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1935 /* Propagate the alignment of the modular type to the record type,
1936 unless there is an alignment clause that under-aligns the type.
1937 This means that bit-packed arrays are given "ceil" alignment for
1938 their size by default, which may seem counter-intuitive but makes
1939 it possible to overlay them on modular types easily. */
1940 SET_TYPE_ALIGN (gnu_type,
1941 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1943 /* Propagate the reverse storage order flag to the record type so
1944 that the required byte swapping is performed when retrieving the
1945 enclosed modular value. */
1946 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1947 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1949 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1951 /* Don't declare the field as addressable since we won't be taking
1952 its address and this would prevent create_field_decl from making
1953 a bitfield. */
1954 gnu_field
1955 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1956 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1958 /* We will output additional debug info manually below. */
1959 finish_record_type (gnu_type, gnu_field, 2, false);
1960 compute_record_mode (gnu_type);
1961 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1963 if (debug_info_p)
1965 /* Make the original array type a parallel/debug type. */
1966 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1968 /* Since GNU_TYPE is a padding type around the packed array
1969 implementation type, the padded type is its debug type. */
1970 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1971 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1975 /* If the type we are dealing with has got a smaller alignment than the
1976 natural one, we need to wrap it up in a record type and misalign the
1977 latter; we reuse the padding machinery for this purpose. */
1978 else if (align > 0)
1980 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1982 /* Set the RM size before wrapping the type. */
1983 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
1985 gnu_type
1986 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
1987 gnat_entity, false, true, definition, false);
1989 TYPE_PACKED (gnu_type) = 1;
1990 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
1993 break;
1995 case E_Floating_Point_Type:
1996 /* The type of the Low and High bounds can be our type if this is
1997 a type from Standard, so set them at the end of the function. */
1998 gnu_type = make_node (REAL_TYPE);
1999 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2000 layout_type (gnu_type);
2001 break;
2003 case E_Floating_Point_Subtype:
2004 /* See the E_Signed_Integer_Subtype case for the rationale. */
2005 if (!definition
2006 && Present (Ancestor_Subtype (gnat_entity))
2007 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
2008 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
2009 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
2010 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
2012 gnu_type = make_node (REAL_TYPE);
2013 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2014 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2015 TYPE_GCC_MIN_VALUE (gnu_type)
2016 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2017 TYPE_GCC_MAX_VALUE (gnu_type)
2018 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2019 layout_type (gnu_type);
2021 SET_TYPE_RM_MIN_VALUE
2022 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2023 gnat_entity, "L", definition, true,
2024 debug_info_p));
2026 SET_TYPE_RM_MAX_VALUE
2027 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2028 gnat_entity, "U", definition, true,
2029 debug_info_p));
2031 /* Inherit our alias set from what we're a subtype of, as for
2032 integer subtypes. */
2033 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2035 /* One of the above calls might have caused us to be elaborated,
2036 so don't blow up if so. */
2037 maybe_present = true;
2038 break;
2040 /* Array Types and Subtypes
2042 Unconstrained array types are represented by E_Array_Type and
2043 constrained array types are represented by E_Array_Subtype. There
2044 are no actual objects of an unconstrained array type; all we have
2045 are pointers to that type.
2047 The following fields are defined on array types and subtypes:
2049 Component_Type Component type of the array.
2050 Number_Dimensions Number of dimensions (an int).
2051 First_Index Type of first index. */
2053 case E_Array_Type:
2055 const bool convention_fortran_p
2056 = (Convention (gnat_entity) == Convention_Fortran);
2057 const int ndim = Number_Dimensions (gnat_entity);
2058 tree gnu_template_type;
2059 tree gnu_ptr_template;
2060 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2061 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2062 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2063 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2064 Entity_Id gnat_index, gnat_name;
2065 int index;
2066 tree comp_type;
2068 /* Create the type for the component now, as it simplifies breaking
2069 type reference loops. */
2070 comp_type
2071 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2072 if (present_gnu_tree (gnat_entity))
2074 /* As a side effect, the type may have been translated. */
2075 maybe_present = true;
2076 break;
2079 /* We complete an existing dummy fat pointer type in place. This both
2080 avoids further complex adjustments in update_pointer_to and yields
2081 better debugging information in DWARF by leveraging the support for
2082 incomplete declarations of "tagged" types in the DWARF back-end. */
2083 gnu_type = get_dummy_type (gnat_entity);
2084 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2086 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2087 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2088 /* Save the contents of the dummy type for update_pointer_to. */
2089 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2090 gnu_ptr_template =
2091 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2092 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2094 else
2096 gnu_fat_type = make_node (RECORD_TYPE);
2097 gnu_template_type = make_node (RECORD_TYPE);
2098 gnu_ptr_template = build_pointer_type (gnu_template_type);
2101 /* Make a node for the array. If we are not defining the array
2102 suppress expanding incomplete types. */
2103 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2105 if (!definition)
2107 defer_incomplete_level++;
2108 this_deferred = true;
2111 /* Build the fat pointer type. Use a "void *" object instead of
2112 a pointer to the array type since we don't have the array type
2113 yet (it will reference the fat pointer via the bounds). */
2115 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2116 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2117 DECL_CHAIN (tem)
2118 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2119 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2121 if (COMPLETE_TYPE_P (gnu_fat_type))
2123 /* We are going to lay it out again so reset the alias set. */
2124 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2125 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2126 finish_fat_pointer_type (gnu_fat_type, tem);
2127 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2128 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2130 TYPE_FIELDS (t) = tem;
2131 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2134 else
2136 finish_fat_pointer_type (gnu_fat_type, tem);
2137 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2140 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2141 is the fat pointer. This will be used to access the individual
2142 fields once we build them. */
2143 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2144 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2145 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2146 gnu_template_reference
2147 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2148 TREE_READONLY (gnu_template_reference) = 1;
2149 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2151 /* Now create the GCC type for each index and add the fields for that
2152 index to the template. */
2153 for (index = (convention_fortran_p ? ndim - 1 : 0),
2154 gnat_index = First_Index (gnat_entity);
2155 0 <= index && index < ndim;
2156 index += (convention_fortran_p ? - 1 : 1),
2157 gnat_index = Next_Index (gnat_index))
2159 char field_name[16];
2160 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2161 tree gnu_index_base_type
2162 = maybe_character_type (get_base_type (gnu_index_type));
2163 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2164 tree gnu_min, gnu_max, gnu_high;
2166 /* Make the FIELD_DECLs for the low and high bounds of this
2167 type and then make extractions of these fields from the
2168 template. */
2169 sprintf (field_name, "LB%d", index);
2170 gnu_lb_field = create_field_decl (get_identifier (field_name),
2171 gnu_index_base_type,
2172 gnu_template_type, NULL_TREE,
2173 NULL_TREE, 0, 0);
2174 Sloc_to_locus (Sloc (gnat_entity),
2175 &DECL_SOURCE_LOCATION (gnu_lb_field));
2177 field_name[0] = 'U';
2178 gnu_hb_field = create_field_decl (get_identifier (field_name),
2179 gnu_index_base_type,
2180 gnu_template_type, NULL_TREE,
2181 NULL_TREE, 0, 0);
2182 Sloc_to_locus (Sloc (gnat_entity),
2183 &DECL_SOURCE_LOCATION (gnu_hb_field));
2185 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2187 /* We can't use build_component_ref here since the template type
2188 isn't complete yet. */
2189 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2190 gnu_template_reference, gnu_lb_field,
2191 NULL_TREE);
2192 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2193 gnu_template_reference, gnu_hb_field,
2194 NULL_TREE);
2195 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2197 gnu_min = convert (sizetype, gnu_orig_min);
2198 gnu_max = convert (sizetype, gnu_orig_max);
2200 /* Compute the size of this dimension. See the E_Array_Subtype
2201 case below for the rationale. */
2202 gnu_high
2203 = build3 (COND_EXPR, sizetype,
2204 build2 (GE_EXPR, boolean_type_node,
2205 gnu_orig_max, gnu_orig_min),
2206 gnu_max,
2207 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2209 /* Make a range type with the new range in the Ada base type.
2210 Then make an index type with the size range in sizetype. */
2211 gnu_index_types[index]
2212 = create_index_type (gnu_min, gnu_high,
2213 create_range_type (gnu_index_base_type,
2214 gnu_orig_min,
2215 gnu_orig_max),
2216 gnat_entity);
2218 /* Update the maximum size of the array in elements. */
2219 if (gnu_max_size)
2221 tree gnu_min
2222 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2223 tree gnu_max
2224 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2225 tree gnu_this_max
2226 = size_binop (PLUS_EXPR, size_one_node,
2227 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2229 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2230 && TREE_OVERFLOW (gnu_this_max))
2231 gnu_max_size = NULL_TREE;
2232 else
2233 gnu_max_size
2234 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2237 TYPE_NAME (gnu_index_types[index])
2238 = create_concat_name (gnat_entity, field_name);
2241 /* Install all the fields into the template. */
2242 TYPE_NAME (gnu_template_type)
2243 = create_concat_name (gnat_entity, "XUB");
2244 gnu_template_fields = NULL_TREE;
2245 for (index = 0; index < ndim; index++)
2246 gnu_template_fields
2247 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2248 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2249 debug_info_p);
2250 TYPE_READONLY (gnu_template_type) = 1;
2252 /* If Component_Size is not already specified, annotate it with the
2253 size of the component. */
2254 if (Unknown_Component_Size (gnat_entity))
2255 Set_Component_Size (gnat_entity,
2256 annotate_value (TYPE_SIZE (comp_type)));
2258 /* Compute the maximum size of the array in units and bits. */
2259 if (gnu_max_size)
2261 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2262 TYPE_SIZE_UNIT (comp_type));
2263 gnu_max_size = size_binop (MULT_EXPR,
2264 convert (bitsizetype, gnu_max_size),
2265 TYPE_SIZE (comp_type));
2267 else
2268 gnu_max_size_unit = NULL_TREE;
2270 /* Now build the array type. */
2271 tem = comp_type;
2272 for (index = ndim - 1; index >= 0; index--)
2274 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2275 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2276 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2277 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2278 set_reverse_storage_order_on_array_type (tem);
2279 if (array_type_has_nonaliased_component (tem, gnat_entity))
2280 set_nonaliased_component_on_array_type (tem);
2283 /* If an alignment is specified, use it if valid. But ignore it
2284 for the original type of packed array types. If the alignment
2285 was requested with an explicit alignment clause, state so. */
2286 if (No (Packed_Array_Impl_Type (gnat_entity))
2287 && Known_Alignment (gnat_entity))
2289 SET_TYPE_ALIGN (tem,
2290 validate_alignment (Alignment (gnat_entity),
2291 gnat_entity,
2292 TYPE_ALIGN (tem)));
2293 if (Present (Alignment_Clause (gnat_entity)))
2294 TYPE_USER_ALIGN (tem) = 1;
2297 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2298 implementation types as such so that the debug information back-end
2299 can output the appropriate description for them. */
2300 TYPE_PACKED (tem)
2301 = (Is_Packed (gnat_entity)
2302 || Is_Packed_Array_Impl_Type (gnat_entity));
2304 if (Treat_As_Volatile (gnat_entity))
2305 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2307 /* Adjust the type of the pointer-to-array field of the fat pointer
2308 and record the aliasing relationships if necessary. */
2309 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2310 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2311 record_component_aliases (gnu_fat_type);
2313 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2314 corresponding fat pointer. */
2315 TREE_TYPE (gnu_type) = gnu_fat_type;
2316 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2317 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2318 SET_TYPE_MODE (gnu_type, BLKmode);
2319 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2321 /* If the maximum size doesn't overflow, use it. */
2322 if (gnu_max_size
2323 && TREE_CODE (gnu_max_size) == INTEGER_CST
2324 && !TREE_OVERFLOW (gnu_max_size)
2325 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2326 && !TREE_OVERFLOW (gnu_max_size_unit))
2328 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2329 TYPE_SIZE (tem));
2330 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2331 TYPE_SIZE_UNIT (tem));
2334 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2335 artificial_p, debug_info_p, gnat_entity);
2337 /* If told to generate GNAT encodings for them (GDB rely on them at the
2338 moment): give the fat pointer type a name. If this is a packed
2339 array, tell the debugger how to interpret the underlying bits. */
2340 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2341 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2342 else
2343 gnat_name = gnat_entity;
2344 tree xup_name
2345 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2346 ? get_entity_name (gnat_name)
2347 : create_concat_name (gnat_name, "XUP");
2348 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2349 gnat_entity);
2351 /* Create the type to be designated by thin pointers: a record type for
2352 the array and its template. We used to shift the fields to have the
2353 template at a negative offset, but this was somewhat of a kludge; we
2354 now shift thin pointer values explicitly but only those which have a
2355 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2356 Note that GDB can handle standard DWARF information for them, so we
2357 don't have to name them as a GNAT encoding, except if specifically
2358 asked to. */
2359 tree xut_name
2360 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2361 ? get_entity_name (gnat_name)
2362 : create_concat_name (gnat_name, "XUT");
2363 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2364 debug_info_p);
2366 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2367 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2369 break;
2371 case E_Array_Subtype:
2373 /* This is the actual data type for array variables. Multidimensional
2374 arrays are implemented as arrays of arrays. Note that arrays which
2375 have sparse enumeration subtypes as index components create sparse
2376 arrays, which is obviously space inefficient but so much easier to
2377 code for now.
2379 Also note that the subtype never refers to the unconstrained array
2380 type, which is somewhat at variance with Ada semantics.
2382 First check to see if this is simply a renaming of the array type.
2383 If so, the result is the array type. */
2385 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2386 if (!Is_Constrained (gnat_entity))
2388 else
2390 Entity_Id gnat_index, gnat_base_index;
2391 const bool convention_fortran_p
2392 = (Convention (gnat_entity) == Convention_Fortran);
2393 const int ndim = Number_Dimensions (gnat_entity);
2394 tree gnu_base_type = gnu_type;
2395 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2396 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2397 bool need_index_type_struct = false;
2398 int index;
2400 /* First create the GCC type for each index and find out whether
2401 special types are needed for debugging information. */
2402 for (index = (convention_fortran_p ? ndim - 1 : 0),
2403 gnat_index = First_Index (gnat_entity),
2404 gnat_base_index
2405 = First_Index (Implementation_Base_Type (gnat_entity));
2406 0 <= index && index < ndim;
2407 index += (convention_fortran_p ? - 1 : 1),
2408 gnat_index = Next_Index (gnat_index),
2409 gnat_base_index = Next_Index (gnat_base_index))
2411 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2412 tree gnu_index_base_type
2413 = maybe_character_type (get_base_type (gnu_index_type));
2414 tree gnu_orig_min
2415 = convert (gnu_index_base_type,
2416 TYPE_MIN_VALUE (gnu_index_type));
2417 tree gnu_orig_max
2418 = convert (gnu_index_base_type,
2419 TYPE_MAX_VALUE (gnu_index_type));
2420 tree gnu_min = convert (sizetype, gnu_orig_min);
2421 tree gnu_max = convert (sizetype, gnu_orig_max);
2422 tree gnu_base_index_type
2423 = get_unpadded_type (Etype (gnat_base_index));
2424 tree gnu_base_index_base_type
2425 = maybe_character_type (get_base_type (gnu_base_index_type));
2426 tree gnu_base_orig_min
2427 = convert (gnu_base_index_base_type,
2428 TYPE_MIN_VALUE (gnu_base_index_type));
2429 tree gnu_base_orig_max
2430 = convert (gnu_base_index_base_type,
2431 TYPE_MAX_VALUE (gnu_base_index_type));
2432 tree gnu_high;
2434 /* See if the base array type is already flat. If it is, we
2435 are probably compiling an ACATS test but it will cause the
2436 code below to malfunction if we don't handle it specially. */
2437 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2438 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2439 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2441 gnu_min = size_one_node;
2442 gnu_max = size_zero_node;
2443 gnu_high = gnu_max;
2446 /* Similarly, if one of the values overflows in sizetype and the
2447 range is null, use 1..0 for the sizetype bounds. */
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_int_cst_lt (gnu_orig_max, gnu_orig_min))
2453 gnu_min = size_one_node;
2454 gnu_max = size_zero_node;
2455 gnu_high = gnu_max;
2458 /* If the minimum and maximum values both overflow in sizetype,
2459 but the difference in the original type does not overflow in
2460 sizetype, ignore the overflow indication. */
2461 else if (TREE_CODE (gnu_min) == INTEGER_CST
2462 && TREE_CODE (gnu_max) == INTEGER_CST
2463 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2464 && !TREE_OVERFLOW
2465 (convert (sizetype,
2466 fold_build2 (MINUS_EXPR, gnu_index_type,
2467 gnu_orig_max,
2468 gnu_orig_min))))
2470 TREE_OVERFLOW (gnu_min) = 0;
2471 TREE_OVERFLOW (gnu_max) = 0;
2472 gnu_high = gnu_max;
2475 /* Compute the size of this dimension in the general case. We
2476 need to provide GCC with an upper bound to use but have to
2477 deal with the "superflat" case. There are three ways to do
2478 this. If we can prove that the array can never be superflat,
2479 we can just use the high bound of the index type. */
2480 else if ((Nkind (gnat_index) == N_Range
2481 && cannot_be_superflat (gnat_index))
2482 /* Bit-Packed Array Impl. Types are never superflat. */
2483 || (Is_Packed_Array_Impl_Type (gnat_entity)
2484 && Is_Bit_Packed_Array
2485 (Original_Array_Type (gnat_entity))))
2486 gnu_high = gnu_max;
2488 /* Otherwise, if the high bound is constant but the low bound is
2489 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2490 lower bound. Note that the comparison must be done in the
2491 original type to avoid any overflow during the conversion. */
2492 else if (TREE_CODE (gnu_max) == INTEGER_CST
2493 && TREE_CODE (gnu_min) != INTEGER_CST)
2495 gnu_high = gnu_max;
2496 gnu_min
2497 = build_cond_expr (sizetype,
2498 build_binary_op (GE_EXPR,
2499 boolean_type_node,
2500 gnu_orig_max,
2501 gnu_orig_min),
2502 gnu_min,
2503 int_const_binop (PLUS_EXPR, gnu_max,
2504 size_one_node));
2507 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2508 in all the other cases. Note that, here as well as above,
2509 the condition used in the comparison must be equivalent to
2510 the condition (length != 0). This is relied upon in order
2511 to optimize array comparisons in compare_arrays. Moreover
2512 we use int_const_binop for the shift by 1 if the bound is
2513 constant to avoid any unwanted overflow. */
2514 else
2515 gnu_high
2516 = build_cond_expr (sizetype,
2517 build_binary_op (GE_EXPR,
2518 boolean_type_node,
2519 gnu_orig_max,
2520 gnu_orig_min),
2521 gnu_max,
2522 TREE_CODE (gnu_min) == INTEGER_CST
2523 ? int_const_binop (MINUS_EXPR, gnu_min,
2524 size_one_node)
2525 : size_binop (MINUS_EXPR, gnu_min,
2526 size_one_node));
2528 /* Reuse the index type for the range type. Then make an index
2529 type with the size range in sizetype. */
2530 gnu_index_types[index]
2531 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2532 gnat_entity);
2534 /* Update the maximum size of the array in elements. Here we
2535 see if any constraint on the index type of the base type
2536 can be used in the case of self-referential bound on the
2537 index type of the subtype. We look for a non-"infinite"
2538 and non-self-referential bound from any type involved and
2539 handle each bound separately. */
2540 if (gnu_max_size)
2542 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2543 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2544 tree gnu_base_base_min
2545 = convert (sizetype,
2546 TYPE_MIN_VALUE (gnu_base_index_base_type));
2547 tree gnu_base_base_max
2548 = convert (sizetype,
2549 TYPE_MAX_VALUE (gnu_base_index_base_type));
2551 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2552 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2553 && !TREE_OVERFLOW (gnu_base_min)))
2554 gnu_base_min = gnu_min;
2556 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2557 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2558 && !TREE_OVERFLOW (gnu_base_max)))
2559 gnu_base_max = gnu_max;
2561 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2562 && TREE_OVERFLOW (gnu_base_min))
2563 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2564 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2565 && TREE_OVERFLOW (gnu_base_max))
2566 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2567 gnu_max_size = NULL_TREE;
2568 else
2570 tree gnu_this_max;
2572 /* Use int_const_binop if the bounds are constant to
2573 avoid any unwanted overflow. */
2574 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2575 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2576 gnu_this_max
2577 = int_const_binop (PLUS_EXPR, size_one_node,
2578 int_const_binop (MINUS_EXPR,
2579 gnu_base_max,
2580 gnu_base_min));
2581 else
2582 gnu_this_max
2583 = size_binop (PLUS_EXPR, size_one_node,
2584 size_binop (MINUS_EXPR,
2585 gnu_base_max,
2586 gnu_base_min));
2588 gnu_max_size
2589 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2593 /* We need special types for debugging information to point to
2594 the index types if they have variable bounds, are not integer
2595 types, are biased or are wider than sizetype. These are GNAT
2596 encodings, so we have to include them only when all encodings
2597 are requested. */
2598 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2599 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2600 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2601 || (TREE_TYPE (gnu_index_type)
2602 && TREE_CODE (TREE_TYPE (gnu_index_type))
2603 != INTEGER_TYPE)
2604 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2605 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2606 need_index_type_struct = true;
2609 /* Then flatten: create the array of arrays. For an array type
2610 used to implement a packed array, get the component type from
2611 the original array type since the representation clauses that
2612 can affect it are on the latter. */
2613 if (Is_Packed_Array_Impl_Type (gnat_entity)
2614 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2616 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2617 for (index = ndim - 1; index >= 0; index--)
2618 gnu_type = TREE_TYPE (gnu_type);
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;
2628 else
2630 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2631 debug_info_p);
2633 /* One of the above calls might have caused us to be elaborated,
2634 so don't blow up if so. */
2635 if (present_gnu_tree (gnat_entity))
2637 maybe_present = true;
2638 break;
2642 /* Compute the maximum size of the array in units and bits. */
2643 if (gnu_max_size)
2645 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2646 TYPE_SIZE_UNIT (gnu_type));
2647 gnu_max_size = size_binop (MULT_EXPR,
2648 convert (bitsizetype, gnu_max_size),
2649 TYPE_SIZE (gnu_type));
2651 else
2652 gnu_max_size_unit = NULL_TREE;
2654 /* Now build the array type. */
2655 for (index = ndim - 1; index >= 0; index --)
2657 gnu_type = build_nonshared_array_type (gnu_type,
2658 gnu_index_types[index]);
2659 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2660 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2661 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2662 set_reverse_storage_order_on_array_type (gnu_type);
2663 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2664 set_nonaliased_component_on_array_type (gnu_type);
2667 /* Strip the ___XP suffix for standard DWARF. */
2668 if (Is_Packed_Array_Impl_Type (gnat_entity)
2669 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2671 Entity_Id gnat_original_array_type
2672 = Underlying_Type (Original_Array_Type (gnat_entity));
2674 gnu_entity_name
2675 = get_entity_name (gnat_original_array_type);
2678 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2679 TYPE_STUB_DECL (gnu_type)
2680 = create_type_stub_decl (gnu_entity_name, gnu_type);
2682 /* If this is a multi-dimensional array and we are at global level,
2683 we need to make a variable corresponding to the stride of the
2684 inner dimensions. */
2685 if (ndim > 1 && global_bindings_p ())
2687 tree gnu_arr_type;
2689 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2690 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2691 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2693 tree eltype = TREE_TYPE (gnu_arr_type);
2694 char stride_name[32];
2696 sprintf (stride_name, "ST%d", index);
2697 TYPE_SIZE (gnu_arr_type)
2698 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2699 gnat_entity, stride_name,
2700 definition, false);
2702 /* ??? For now, store the size as a multiple of the
2703 alignment of the element type in bytes so that we
2704 can see the alignment from the tree. */
2705 sprintf (stride_name, "ST%d_A_UNIT", index);
2706 TYPE_SIZE_UNIT (gnu_arr_type)
2707 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2708 gnat_entity, stride_name,
2709 definition, false,
2710 TYPE_ALIGN (eltype));
2712 /* ??? create_type_decl is not invoked on the inner types so
2713 the MULT_EXPR node built above will never be marked. */
2714 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2718 /* If we need to write out a record type giving the names of the
2719 bounds for debugging purposes, do it now and make the record
2720 type a parallel type. This is not needed for a packed array
2721 since the bounds are conveyed by the original array type. */
2722 if (need_index_type_struct
2723 && debug_info_p
2724 && !Is_Packed_Array_Impl_Type (gnat_entity))
2726 tree gnu_bound_rec = make_node (RECORD_TYPE);
2727 tree gnu_field_list = NULL_TREE;
2728 tree gnu_field;
2730 TYPE_NAME (gnu_bound_rec)
2731 = create_concat_name (gnat_entity, "XA");
2733 for (index = ndim - 1; index >= 0; index--)
2735 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2736 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2738 /* Make sure to reference the types themselves, and not just
2739 their names, as the debugger may fall back on them. */
2740 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2741 gnu_bound_rec, NULL_TREE,
2742 NULL_TREE, 0, 0);
2743 DECL_CHAIN (gnu_field) = gnu_field_list;
2744 gnu_field_list = gnu_field;
2747 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2748 add_parallel_type (gnu_type, gnu_bound_rec);
2751 /* If this is a packed array type, make the original array type a
2752 parallel/debug type. Otherwise, if such GNAT encodings are
2753 required, do it for the base array type if it isn't artificial to
2754 make sure it is kept in the debug info. */
2755 if (debug_info_p)
2757 if (Is_Packed_Array_Impl_Type (gnat_entity))
2758 associate_original_type_to_packed_array (gnu_type,
2759 gnat_entity);
2760 else
2762 tree gnu_base_decl
2763 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2764 false);
2765 if (!DECL_ARTIFICIAL (gnu_base_decl)
2766 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2767 add_parallel_type (gnu_type,
2768 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2772 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2773 = (Is_Packed_Array_Impl_Type (gnat_entity)
2774 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2776 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2777 implementation types as such so that the debug information back-end
2778 can output the appropriate description for them. */
2779 TYPE_PACKED (gnu_type)
2780 = (Is_Packed (gnat_entity)
2781 || Is_Packed_Array_Impl_Type (gnat_entity));
2783 /* If the size is self-referential and the maximum size doesn't
2784 overflow, use it. */
2785 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2786 && gnu_max_size
2787 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2788 && TREE_OVERFLOW (gnu_max_size))
2789 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2790 && TREE_OVERFLOW (gnu_max_size_unit)))
2792 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2793 TYPE_SIZE (gnu_type));
2794 TYPE_SIZE_UNIT (gnu_type)
2795 = size_binop (MIN_EXPR, gnu_max_size_unit,
2796 TYPE_SIZE_UNIT (gnu_type));
2799 /* Set our alias set to that of our base type. This gives all
2800 array subtypes the same alias set. */
2801 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2803 /* If this is a packed type, make this type the same as the packed
2804 array type, but do some adjusting in the type first. */
2805 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2807 Entity_Id gnat_index;
2808 tree gnu_inner;
2810 /* First finish the type we had been making so that we output
2811 debugging information for it. */
2812 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2813 if (Treat_As_Volatile (gnat_entity))
2815 const int quals
2816 = TYPE_QUAL_VOLATILE
2817 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2818 gnu_type = change_qualified_type (gnu_type, quals);
2820 /* Make it artificial only if the base type was artificial too.
2821 That's sort of "morally" true and will make it possible for
2822 the debugger to look it up by name in DWARF, which is needed
2823 in order to decode the packed array type. */
2824 gnu_decl
2825 = create_type_decl (gnu_entity_name, gnu_type,
2826 !Comes_From_Source (Etype (gnat_entity))
2827 && artificial_p, debug_info_p,
2828 gnat_entity);
2830 /* Save it as our equivalent in case the call below elaborates
2831 this type again. */
2832 save_gnu_tree (gnat_entity, gnu_decl, false);
2834 gnu_decl
2835 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2836 NULL_TREE, false);
2837 this_made_decl = true;
2838 gnu_type = TREE_TYPE (gnu_decl);
2839 save_gnu_tree (gnat_entity, NULL_TREE, false);
2840 save_gnu_tree (gnat_entity, gnu_decl, false);
2841 saved = true;
2843 gnu_inner = gnu_type;
2844 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2845 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2846 || TYPE_PADDING_P (gnu_inner)))
2847 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2849 /* We need to attach the index type to the type we just made so
2850 that the actual bounds can later be put into a template. */
2851 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2852 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2853 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2854 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2856 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2858 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2859 TYPE_MODULUS for modular types so we make an extra
2860 subtype if necessary. */
2861 if (TYPE_MODULAR_P (gnu_inner))
2863 tree gnu_subtype
2864 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2865 TREE_TYPE (gnu_subtype) = gnu_inner;
2866 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2867 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2868 TYPE_MIN_VALUE (gnu_inner));
2869 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2870 TYPE_MAX_VALUE (gnu_inner));
2871 gnu_inner = gnu_subtype;
2874 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2876 /* Check for other cases of overloading. */
2877 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2880 for (gnat_index = First_Index (gnat_entity);
2881 Present (gnat_index);
2882 gnat_index = Next_Index (gnat_index))
2883 SET_TYPE_ACTUAL_BOUNDS
2884 (gnu_inner,
2885 tree_cons (NULL_TREE,
2886 get_unpadded_type (Etype (gnat_index)),
2887 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2889 if (Convention (gnat_entity) != Convention_Fortran)
2890 SET_TYPE_ACTUAL_BOUNDS
2891 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2893 if (TREE_CODE (gnu_type) == RECORD_TYPE
2894 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2895 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2899 break;
2901 case E_String_Literal_Subtype:
2902 /* Create the type for a string literal. */
2904 Entity_Id gnat_full_type
2905 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2906 && Present (Full_View (Etype (gnat_entity)))
2907 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2908 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2909 tree gnu_string_array_type
2910 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2911 tree gnu_string_index_type
2912 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2913 (TYPE_DOMAIN (gnu_string_array_type))));
2914 tree gnu_lower_bound
2915 = convert (gnu_string_index_type,
2916 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2917 tree gnu_length
2918 = UI_To_gnu (String_Literal_Length (gnat_entity),
2919 gnu_string_index_type);
2920 tree gnu_upper_bound
2921 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2922 gnu_lower_bound,
2923 int_const_binop (MINUS_EXPR, gnu_length,
2924 convert (gnu_string_index_type,
2925 integer_one_node)));
2926 tree gnu_index_type
2927 = create_index_type (convert (sizetype, gnu_lower_bound),
2928 convert (sizetype, gnu_upper_bound),
2929 create_range_type (gnu_string_index_type,
2930 gnu_lower_bound,
2931 gnu_upper_bound),
2932 gnat_entity);
2934 gnu_type
2935 = build_nonshared_array_type (gnat_to_gnu_type
2936 (Component_Type (gnat_entity)),
2937 gnu_index_type);
2938 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2939 set_nonaliased_component_on_array_type (gnu_type);
2940 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2942 break;
2944 /* Record Types and Subtypes
2946 The following fields are defined on record types:
2948 Has_Discriminants True if the record has discriminants
2949 First_Discriminant Points to head of list of discriminants
2950 First_Entity Points to head of list of fields
2951 Is_Tagged_Type True if the record is tagged
2953 Implementation of Ada records and discriminated records:
2955 A record type definition is transformed into the equivalent of a C
2956 struct definition. The fields that are the discriminants which are
2957 found in the Full_Type_Declaration node and the elements of the
2958 Component_List found in the Record_Type_Definition node. The
2959 Component_List can be a recursive structure since each Variant of
2960 the Variant_Part of the Component_List has a Component_List.
2962 Processing of a record type definition comprises starting the list of
2963 field declarations here from the discriminants and the calling the
2964 function components_to_record to add the rest of the fields from the
2965 component list and return the gnu type node. The function
2966 components_to_record will call itself recursively as it traverses
2967 the tree. */
2969 case E_Record_Type:
2970 if (Has_Complex_Representation (gnat_entity))
2972 gnu_type
2973 = build_complex_type
2974 (get_unpadded_type
2975 (Etype (Defining_Entity
2976 (First (Component_Items
2977 (Component_List
2978 (Type_Definition
2979 (Declaration_Node (gnat_entity)))))))));
2981 break;
2985 Node_Id full_definition = Declaration_Node (gnat_entity);
2986 Node_Id record_definition = Type_Definition (full_definition);
2987 Node_Id gnat_constr;
2988 Entity_Id gnat_field;
2989 tree gnu_field, gnu_field_list = NULL_TREE;
2990 tree gnu_get_parent;
2991 /* Set PACKED in keeping with gnat_to_gnu_field. */
2992 const int packed
2993 = Is_Packed (gnat_entity)
2995 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2996 ? -1
2997 : 0;
2998 const bool has_align = Known_Alignment (gnat_entity);
2999 const bool has_discr = Has_Discriminants (gnat_entity);
3000 const bool has_rep = Has_Specified_Layout (gnat_entity);
3001 const bool is_extension
3002 = (Is_Tagged_Type (gnat_entity)
3003 && Nkind (record_definition) == N_Derived_Type_Definition);
3004 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
3005 bool all_rep = has_rep;
3007 /* See if all fields have a rep clause. Stop when we find one
3008 that doesn't. */
3009 if (all_rep)
3010 for (gnat_field = First_Entity (gnat_entity);
3011 Present (gnat_field);
3012 gnat_field = Next_Entity (gnat_field))
3013 if ((Ekind (gnat_field) == E_Component
3014 || Ekind (gnat_field) == E_Discriminant)
3015 && No (Component_Clause (gnat_field)))
3017 all_rep = false;
3018 break;
3021 /* If this is a record extension, go a level further to find the
3022 record definition. Also, verify we have a Parent_Subtype. */
3023 if (is_extension)
3025 if (!type_annotate_only
3026 || Present (Record_Extension_Part (record_definition)))
3027 record_definition = Record_Extension_Part (record_definition);
3029 gcc_assert (type_annotate_only
3030 || Present (Parent_Subtype (gnat_entity)));
3033 /* Make a node for the record. If we are not defining the record,
3034 suppress expanding incomplete types. */
3035 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3036 TYPE_NAME (gnu_type) = gnu_entity_name;
3037 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3038 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3039 = Reverse_Storage_Order (gnat_entity);
3040 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3042 if (!definition)
3044 defer_incomplete_level++;
3045 this_deferred = true;
3048 /* If both a size and rep clause were specified, put the size on
3049 the record type now so that it can get the proper layout. */
3050 if (has_rep && Known_RM_Size (gnat_entity))
3051 TYPE_SIZE (gnu_type)
3052 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3054 /* Always set the alignment on the record type here so that it can
3055 get the proper layout. */
3056 if (has_align)
3057 SET_TYPE_ALIGN (gnu_type,
3058 validate_alignment (Alignment (gnat_entity),
3059 gnat_entity, 0));
3060 else
3062 SET_TYPE_ALIGN (gnu_type, 0);
3064 /* If a type needs strict alignment, the minimum size will be the
3065 type size instead of the RM size (see validate_size). Cap the
3066 alignment lest it causes this type size to become too large. */
3067 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3069 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3070 unsigned int max_align = max_size & -max_size;
3071 if (max_align < BIGGEST_ALIGNMENT)
3072 TYPE_MAX_ALIGN (gnu_type) = max_align;
3076 /* If we have a Parent_Subtype, make a field for the parent. If
3077 this record has rep clauses, force the position to zero. */
3078 if (Present (Parent_Subtype (gnat_entity)))
3080 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3081 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3082 tree gnu_parent;
3084 /* A major complexity here is that the parent subtype will
3085 reference our discriminants in its Stored_Constraint list.
3086 But those must reference the parent component of this record
3087 which is precisely of the parent subtype we have not built yet!
3088 To break the circle we first build a dummy COMPONENT_REF which
3089 represents the "get to the parent" operation and initialize
3090 each of those discriminants to a COMPONENT_REF of the above
3091 dummy parent referencing the corresponding discriminant of the
3092 base type of the parent subtype. */
3093 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3094 build0 (PLACEHOLDER_EXPR, gnu_type),
3095 build_decl (input_location,
3096 FIELD_DECL, NULL_TREE,
3097 gnu_dummy_parent_type),
3098 NULL_TREE);
3100 if (has_discr)
3101 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3102 Present (gnat_field);
3103 gnat_field = Next_Stored_Discriminant (gnat_field))
3104 if (Present (Corresponding_Discriminant (gnat_field)))
3106 tree gnu_field
3107 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3108 (gnat_field));
3109 save_gnu_tree
3110 (gnat_field,
3111 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3112 gnu_get_parent, gnu_field, NULL_TREE),
3113 true);
3116 /* Then we build the parent subtype. If it has discriminants but
3117 the type itself has unknown discriminants, this means that it
3118 doesn't contain information about how the discriminants are
3119 derived from those of the ancestor type, so it cannot be used
3120 directly. Instead it is built by cloning the parent subtype
3121 of the underlying record view of the type, for which the above
3122 derivation of discriminants has been made explicit. */
3123 if (Has_Discriminants (gnat_parent)
3124 && Has_Unknown_Discriminants (gnat_entity))
3126 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3128 /* If we are defining the type, the underlying record
3129 view must already have been elaborated at this point.
3130 Otherwise do it now as its parent subtype cannot be
3131 technically elaborated on its own. */
3132 if (definition)
3133 gcc_assert (present_gnu_tree (gnat_uview));
3134 else
3135 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3137 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3139 /* Substitute the "get to the parent" of the type for that
3140 of its underlying record view in the cloned type. */
3141 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3142 Present (gnat_field);
3143 gnat_field = Next_Stored_Discriminant (gnat_field))
3144 if (Present (Corresponding_Discriminant (gnat_field)))
3146 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3147 tree gnu_ref
3148 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3149 gnu_get_parent, gnu_field, NULL_TREE);
3150 gnu_parent
3151 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3154 else
3155 gnu_parent = gnat_to_gnu_type (gnat_parent);
3157 /* The parent field needs strict alignment so, if it is to
3158 be created with a component clause below, then we need
3159 to apply the same adjustment as in gnat_to_gnu_field. */
3160 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3161 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3163 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3164 initially built. The discriminants must reference the fields
3165 of the parent subtype and not those of its base type for the
3166 placeholder machinery to properly work. */
3167 if (has_discr)
3169 /* The actual parent subtype is the full view. */
3170 if (IN (Ekind (gnat_parent), Private_Kind))
3172 if (Present (Full_View (gnat_parent)))
3173 gnat_parent = Full_View (gnat_parent);
3174 else
3175 gnat_parent = Underlying_Full_View (gnat_parent);
3178 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3179 Present (gnat_field);
3180 gnat_field = Next_Stored_Discriminant (gnat_field))
3181 if (Present (Corresponding_Discriminant (gnat_field)))
3183 Entity_Id field;
3184 for (field = First_Stored_Discriminant (gnat_parent);
3185 Present (field);
3186 field = Next_Stored_Discriminant (field))
3187 if (same_discriminant_p (gnat_field, field))
3188 break;
3189 gcc_assert (Present (field));
3190 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3191 = gnat_to_gnu_field_decl (field);
3195 /* The "get to the parent" COMPONENT_REF must be given its
3196 proper type... */
3197 TREE_TYPE (gnu_get_parent) = gnu_parent;
3199 /* ...and reference the _Parent field of this record. */
3200 gnu_field
3201 = create_field_decl (parent_name_id,
3202 gnu_parent, gnu_type,
3203 has_rep
3204 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3205 has_rep
3206 ? bitsize_zero_node : NULL_TREE,
3207 0, 1);
3208 DECL_INTERNAL_P (gnu_field) = 1;
3209 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3210 TYPE_FIELDS (gnu_type) = gnu_field;
3213 /* Make the fields for the discriminants and put them into the record
3214 unless it's an Unchecked_Union. */
3215 if (has_discr)
3216 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3217 Present (gnat_field);
3218 gnat_field = Next_Stored_Discriminant (gnat_field))
3220 /* If this is a record extension and this discriminant is the
3221 renaming of another discriminant, we've handled it above. */
3222 if (Present (Parent_Subtype (gnat_entity))
3223 && Present (Corresponding_Discriminant (gnat_field)))
3224 continue;
3226 /* However, if we are just annotating types, the Parent_Subtype
3227 doesn't exist so we need skip the discriminant altogether. */
3228 if (type_annotate_only
3229 && Is_Tagged_Type (gnat_entity)
3230 && Is_Derived_Type (gnat_entity)
3231 && Present (Corresponding_Discriminant (gnat_field)))
3232 continue;
3234 gnu_field
3235 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3236 debug_info_p);
3238 /* Make an expression using a PLACEHOLDER_EXPR from the
3239 FIELD_DECL node just created and link that with the
3240 corresponding GNAT defining identifier. */
3241 save_gnu_tree (gnat_field,
3242 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3243 build0 (PLACEHOLDER_EXPR, gnu_type),
3244 gnu_field, NULL_TREE),
3245 true);
3247 if (!is_unchecked_union)
3249 DECL_CHAIN (gnu_field) = gnu_field_list;
3250 gnu_field_list = gnu_field;
3254 /* If we have a derived untagged type that renames discriminants in
3255 the root type, the (stored) discriminants are a just copy of the
3256 discriminants of the root type. This means that any constraints
3257 added by the renaming in the derivation are disregarded as far
3258 as the layout of the derived type is concerned. To rescue them,
3259 we change the type of the (stored) discriminants to a subtype
3260 with the bounds of the type of the visible discriminants. */
3261 if (has_discr
3262 && !is_extension
3263 && Stored_Constraint (gnat_entity) != No_Elist)
3264 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3265 gnat_constr != No_Elmt;
3266 gnat_constr = Next_Elmt (gnat_constr))
3267 if (Nkind (Node (gnat_constr)) == N_Identifier
3268 /* Ignore access discriminants. */
3269 && !Is_Access_Type (Etype (Node (gnat_constr)))
3270 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3272 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3273 tree gnu_discr_type, gnu_ref;
3275 /* If the scope of the discriminant is not the record type,
3276 this means that we're processing the implicit full view
3277 of a type derived from a private discriminated type: in
3278 this case, the Stored_Constraint list is simply copied
3279 from the partial view, see Build_Derived_Private_Type.
3280 So we need to retrieve the corresponding discriminant
3281 of the implicit full view, otherwise we will abort. */
3282 if (Scope (gnat_discr) != gnat_entity)
3284 Entity_Id field;
3285 for (field = First_Entity (gnat_entity);
3286 Present (field);
3287 field = Next_Entity (field))
3288 if (Ekind (field) == E_Discriminant
3289 && same_discriminant_p (gnat_discr, field))
3290 break;
3291 gcc_assert (Present (field));
3292 gnat_discr = field;
3295 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3296 gnu_ref
3297 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3298 NULL_TREE, false);
3300 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3301 just above for one of the stored discriminants. */
3302 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3304 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3306 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3307 tree gnu_subtype
3308 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3309 ? make_unsigned_type (prec) : make_signed_type (prec);
3310 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3311 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3312 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3313 TYPE_MIN_VALUE (gnu_discr_type));
3314 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3315 TYPE_MAX_VALUE (gnu_discr_type));
3316 TREE_TYPE (gnu_ref)
3317 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3321 /* Add the fields into the record type and finish it up. */
3322 components_to_record (gnu_type, Component_List (record_definition),
3323 gnu_field_list, packed, definition, false,
3324 all_rep, is_unchecked_union,
3325 artificial_p, debug_info_p,
3326 false, OK_To_Reorder_Components (gnat_entity),
3327 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3329 /* Fill in locations of fields. */
3330 annotate_rep (gnat_entity, gnu_type);
3332 /* If there are any entities in the chain corresponding to components
3333 that we did not elaborate, ensure we elaborate their types if they
3334 are Itypes. */
3335 for (gnat_temp = First_Entity (gnat_entity);
3336 Present (gnat_temp);
3337 gnat_temp = Next_Entity (gnat_temp))
3338 if ((Ekind (gnat_temp) == E_Component
3339 || Ekind (gnat_temp) == E_Discriminant)
3340 && Is_Itype (Etype (gnat_temp))
3341 && !present_gnu_tree (gnat_temp))
3342 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3344 /* If this is a record type associated with an exception definition,
3345 equate its fields to those of the standard exception type. This
3346 will make it possible to convert between them. */
3347 if (gnu_entity_name == exception_data_name_id)
3349 tree gnu_std_field;
3350 for (gnu_field = TYPE_FIELDS (gnu_type),
3351 gnu_std_field = TYPE_FIELDS (except_type_node);
3352 gnu_field;
3353 gnu_field = DECL_CHAIN (gnu_field),
3354 gnu_std_field = DECL_CHAIN (gnu_std_field))
3355 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3356 gcc_assert (!gnu_std_field);
3359 break;
3361 case E_Class_Wide_Subtype:
3362 /* If an equivalent type is present, that is what we should use.
3363 Otherwise, fall through to handle this like a record subtype
3364 since it may have constraints. */
3365 if (gnat_equiv_type != gnat_entity)
3367 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3368 maybe_present = true;
3369 break;
3372 /* fall through */
3374 case E_Record_Subtype:
3375 /* If Cloned_Subtype is Present it means this record subtype has
3376 identical layout to that type or subtype and we should use
3377 that GCC type for this one. The front end guarantees that
3378 the component list is shared. */
3379 if (Present (Cloned_Subtype (gnat_entity)))
3381 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3382 NULL_TREE, false);
3383 maybe_present = true;
3384 break;
3387 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3388 changing the type, make a new type with each field having the type of
3389 the field in the new subtype but the position computed by transforming
3390 every discriminant reference according to the constraints. We don't
3391 see any difference between private and non-private type here since
3392 derivations from types should have been deferred until the completion
3393 of the private type. */
3394 else
3396 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3397 tree gnu_base_type;
3399 if (!definition)
3401 defer_incomplete_level++;
3402 this_deferred = true;
3405 gnu_base_type
3406 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3408 if (present_gnu_tree (gnat_entity))
3410 maybe_present = true;
3411 break;
3414 /* If this is a record subtype associated with a dispatch table,
3415 strip the suffix. This is necessary to make sure 2 different
3416 subtypes associated with the imported and exported views of a
3417 dispatch table are properly merged in LTO mode. */
3418 if (Is_Dispatch_Table_Entity (gnat_entity))
3420 char *p;
3421 Get_Encoded_Name (gnat_entity);
3422 p = strchr (Name_Buffer, '_');
3423 gcc_assert (p);
3424 strcpy (p+2, "dtS");
3425 gnu_entity_name = get_identifier (Name_Buffer);
3428 /* When the subtype has discriminants and these discriminants affect
3429 the initial shape it has inherited, factor them in. But for an
3430 Unchecked_Union (it must be an Itype), just return the type.
3431 We can't just test Is_Constrained because private subtypes without
3432 discriminants of types with discriminants with default expressions
3433 are Is_Constrained but aren't constrained! */
3434 if (IN (Ekind (gnat_base_type), Record_Kind)
3435 && !Is_Unchecked_Union (gnat_base_type)
3436 && !Is_For_Access_Subtype (gnat_entity)
3437 && Has_Discriminants (gnat_entity)
3438 && Is_Constrained (gnat_entity)
3439 && Stored_Constraint (gnat_entity) != No_Elist)
3441 vec<subst_pair> gnu_subst_list
3442 = build_subst_list (gnat_entity, gnat_base_type, definition);
3443 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3444 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3445 bool selected_variant = false, all_constant_pos = true;
3446 Entity_Id gnat_field;
3447 vec<variant_desc> gnu_variant_list;
3449 gnu_type = make_node (RECORD_TYPE);
3450 TYPE_NAME (gnu_type) = gnu_entity_name;
3451 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3452 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3453 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3454 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3455 = Reverse_Storage_Order (gnat_entity);
3456 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3458 /* Set the size, alignment and alias set of the new type to
3459 match that of the old one, doing required substitutions. */
3460 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3461 gnu_subst_list);
3463 if (TYPE_IS_PADDING_P (gnu_base_type))
3464 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3465 else
3466 gnu_unpad_base_type = gnu_base_type;
3468 /* Look for REP and variant parts in the base type. */
3469 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3470 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3472 /* If there is a variant part, we must compute whether the
3473 constraints statically select a particular variant. If
3474 so, we simply drop the qualified union and flatten the
3475 list of fields. Otherwise we'll build a new qualified
3476 union for the variants that are still relevant. */
3477 if (gnu_variant_part)
3479 variant_desc *v;
3480 unsigned int i;
3482 gnu_variant_list
3483 = build_variant_list (TREE_TYPE (gnu_variant_part),
3484 gnu_subst_list,
3485 vNULL);
3487 /* If all the qualifiers are unconditionally true, the
3488 innermost variant is statically selected. */
3489 selected_variant = true;
3490 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3491 if (!integer_onep (v->qual))
3493 selected_variant = false;
3494 break;
3497 /* Otherwise, create the new variants. */
3498 if (!selected_variant)
3499 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3501 tree old_variant = v->type;
3502 tree new_variant = make_node (RECORD_TYPE);
3503 tree suffix
3504 = concat_name (DECL_NAME (gnu_variant_part),
3505 IDENTIFIER_POINTER
3506 (DECL_NAME (v->field)));
3507 TYPE_NAME (new_variant)
3508 = concat_name (TYPE_NAME (gnu_type),
3509 IDENTIFIER_POINTER (suffix));
3510 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3511 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3512 copy_and_substitute_in_size (new_variant, old_variant,
3513 gnu_subst_list);
3514 v->new_type = new_variant;
3517 else
3519 gnu_variant_list.create (0);
3520 selected_variant = false;
3523 /* Make a list of fields and their position in the base type. */
3524 gnu_pos_list
3525 = build_position_list (gnu_unpad_base_type,
3526 gnu_variant_list.exists ()
3527 && !selected_variant,
3528 size_zero_node, bitsize_zero_node,
3529 BIGGEST_ALIGNMENT, NULL_TREE);
3531 /* Now go down every component in the subtype and compute its
3532 size and position from those of the component in the base
3533 type and from the constraints of the subtype. */
3534 for (gnat_field = First_Entity (gnat_entity);
3535 Present (gnat_field);
3536 gnat_field = Next_Entity (gnat_field))
3537 if ((Ekind (gnat_field) == E_Component
3538 || Ekind (gnat_field) == E_Discriminant)
3539 && !(Present (Corresponding_Discriminant (gnat_field))
3540 && Is_Tagged_Type (gnat_base_type))
3541 && Underlying_Type
3542 (Scope (Original_Record_Component (gnat_field)))
3543 == gnat_base_type)
3545 Name_Id gnat_name = Chars (gnat_field);
3546 Entity_Id gnat_old_field
3547 = Original_Record_Component (gnat_field);
3548 tree gnu_old_field
3549 = gnat_to_gnu_field_decl (gnat_old_field);
3550 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3551 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3552 tree gnu_cont_type, gnu_last = NULL_TREE;
3554 /* If the type is the same, retrieve the GCC type from the
3555 old field to take into account possible adjustments. */
3556 if (Etype (gnat_field) == Etype (gnat_old_field))
3557 gnu_field_type = TREE_TYPE (gnu_old_field);
3558 else
3559 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3561 /* If there was a component clause, the field types must be
3562 the same for the type and subtype, so copy the data from
3563 the old field to avoid recomputation here. Also if the
3564 field is justified modular and the optimization in
3565 gnat_to_gnu_field was applied. */
3566 if (Present (Component_Clause (gnat_old_field))
3567 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3568 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3569 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3570 == TREE_TYPE (gnu_old_field)))
3572 gnu_size = DECL_SIZE (gnu_old_field);
3573 gnu_field_type = TREE_TYPE (gnu_old_field);
3576 /* If the old field was packed and of constant size, we
3577 have to get the old size here, as it might differ from
3578 what the Etype conveys and the latter might overlap
3579 onto the following field. Try to arrange the type for
3580 possible better packing along the way. */
3581 else if (DECL_PACKED (gnu_old_field)
3582 && TREE_CODE (DECL_SIZE (gnu_old_field))
3583 == INTEGER_CST)
3585 gnu_size = DECL_SIZE (gnu_old_field);
3586 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3587 && !TYPE_FAT_POINTER_P (gnu_field_type)
3588 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3589 gnu_field_type
3590 = make_packable_type (gnu_field_type, true);
3593 else
3594 gnu_size = TYPE_SIZE (gnu_field_type);
3596 /* If the context of the old field is the base type or its
3597 REP part (if any), put the field directly in the new
3598 type; otherwise look up the context in the variant list
3599 and put the field either in the new type if there is a
3600 selected variant or in one of the new variants. */
3601 if (gnu_context == gnu_unpad_base_type
3602 || (gnu_rep_part
3603 && gnu_context == TREE_TYPE (gnu_rep_part)))
3604 gnu_cont_type = gnu_type;
3605 else
3607 variant_desc *v;
3608 unsigned int i;
3609 tree rep_part;
3611 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3612 if (gnu_context == v->type
3613 || ((rep_part = get_rep_part (v->type))
3614 && gnu_context == TREE_TYPE (rep_part)))
3615 break;
3616 if (v)
3618 if (selected_variant)
3619 gnu_cont_type = gnu_type;
3620 else
3621 gnu_cont_type = v->new_type;
3623 else
3624 /* The front-end may pass us "ghost" components if
3625 it fails to recognize that a constrained subtype
3626 is statically constrained. Discard them. */
3627 continue;
3630 /* Now create the new field modeled on the old one. */
3631 gnu_field
3632 = create_field_decl_from (gnu_old_field, gnu_field_type,
3633 gnu_cont_type, gnu_size,
3634 gnu_pos_list, gnu_subst_list);
3635 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3637 /* Put it in one of the new variants directly. */
3638 if (gnu_cont_type != gnu_type)
3640 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3641 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3644 /* To match the layout crafted in components_to_record,
3645 if this is the _Tag or _Parent field, put it before
3646 any other fields. */
3647 else if (gnat_name == Name_uTag
3648 || gnat_name == Name_uParent)
3649 gnu_field_list = chainon (gnu_field_list, gnu_field);
3651 /* Similarly, if this is the _Controller field, put
3652 it before the other fields except for the _Tag or
3653 _Parent field. */
3654 else if (gnat_name == Name_uController && gnu_last)
3656 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3657 DECL_CHAIN (gnu_last) = gnu_field;
3660 /* Otherwise, if this is a regular field, put it after
3661 the other fields. */
3662 else
3664 DECL_CHAIN (gnu_field) = gnu_field_list;
3665 gnu_field_list = gnu_field;
3666 if (!gnu_last)
3667 gnu_last = gnu_field;
3668 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3669 all_constant_pos = false;
3672 save_gnu_tree (gnat_field, gnu_field, false);
3675 /* If there is a variant list, a selected variant and the fields
3676 all have a constant position, put them in order of increasing
3677 position to match that of constant CONSTRUCTORs. Likewise if
3678 there is no variant list but a REP part, since the latter has
3679 been flattened in the process. */
3680 if (((gnu_variant_list.exists () && selected_variant)
3681 || (!gnu_variant_list.exists () && gnu_rep_part))
3682 && all_constant_pos)
3684 const int len = list_length (gnu_field_list);
3685 tree *field_arr = XALLOCAVEC (tree, len), t;
3686 int i;
3688 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3689 field_arr[i] = t;
3691 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3693 gnu_field_list = NULL_TREE;
3694 for (i = 0; i < len; i++)
3696 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3697 gnu_field_list = field_arr[i];
3701 /* If there is a variant list and no selected variant, we need
3702 to create the nest of variant parts from the old nest. */
3703 else if (gnu_variant_list.exists () && !selected_variant)
3705 tree new_variant_part
3706 = create_variant_part_from (gnu_variant_part,
3707 gnu_variant_list, gnu_type,
3708 gnu_pos_list, gnu_subst_list);
3709 DECL_CHAIN (new_variant_part) = gnu_field_list;
3710 gnu_field_list = new_variant_part;
3713 /* Now go through the entities again looking for Itypes that
3714 we have not elaborated but should (e.g., Etypes of fields
3715 that have Original_Components). */
3716 for (gnat_field = First_Entity (gnat_entity);
3717 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3718 if ((Ekind (gnat_field) == E_Discriminant
3719 || Ekind (gnat_field) == E_Component)
3720 && !present_gnu_tree (Etype (gnat_field)))
3721 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
3723 /* We will output additional debug info manually below. */
3724 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3725 false);
3726 compute_record_mode (gnu_type);
3728 /* Fill in locations of fields. */
3729 annotate_rep (gnat_entity, gnu_type);
3731 /* If debugging information is being written for the type and if
3732 we are asked to output such encodings, write a record that
3733 shows what we are a subtype of and also make a variable that
3734 indicates our size, if still variable. */
3735 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3737 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3738 tree gnu_unpad_base_name
3739 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3740 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3742 TYPE_NAME (gnu_subtype_marker)
3743 = create_concat_name (gnat_entity, "XVS");
3744 finish_record_type (gnu_subtype_marker,
3745 create_field_decl (gnu_unpad_base_name,
3746 build_reference_type
3747 (gnu_unpad_base_type),
3748 gnu_subtype_marker,
3749 NULL_TREE, NULL_TREE,
3750 0, 0),
3751 0, true);
3753 add_parallel_type (gnu_type, gnu_subtype_marker);
3755 if (definition
3756 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3757 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3758 TYPE_SIZE_UNIT (gnu_subtype_marker)
3759 = create_var_decl (create_concat_name (gnat_entity,
3760 "XVZ"),
3761 NULL_TREE, sizetype, gnu_size_unit,
3762 false, false, false, false, false,
3763 true, debug_info_p,
3764 NULL, gnat_entity);
3767 gnu_variant_list.release ();
3768 gnu_subst_list.release ();
3771 /* Otherwise, go down all the components in the new type and make
3772 them equivalent to those in the base type. */
3773 else
3775 gnu_type = gnu_base_type;
3777 for (gnat_temp = First_Entity (gnat_entity);
3778 Present (gnat_temp);
3779 gnat_temp = Next_Entity (gnat_temp))
3780 if ((Ekind (gnat_temp) == E_Discriminant
3781 && !Is_Unchecked_Union (gnat_base_type))
3782 || Ekind (gnat_temp) == E_Component)
3783 save_gnu_tree (gnat_temp,
3784 gnat_to_gnu_field_decl
3785 (Original_Record_Component (gnat_temp)),
3786 false);
3789 break;
3791 case E_Access_Subprogram_Type:
3792 case E_Anonymous_Access_Subprogram_Type:
3793 /* Use the special descriptor type for dispatch tables if needed,
3794 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3795 Note that we are only required to do so for static tables in
3796 order to be compatible with the C++ ABI, but Ada 2005 allows
3797 to extend library level tagged types at the local level so
3798 we do it in the non-static case as well. */
3799 if (TARGET_VTABLE_USES_DESCRIPTORS
3800 && Is_Dispatch_Table_Entity (gnat_entity))
3802 gnu_type = fdesc_type_node;
3803 gnu_size = TYPE_SIZE (gnu_type);
3804 break;
3807 /* fall through */
3809 case E_Allocator_Type:
3810 case E_Access_Type:
3811 case E_Access_Attribute_Type:
3812 case E_Anonymous_Access_Type:
3813 case E_General_Access_Type:
3815 /* The designated type and its equivalent type for gigi. */
3816 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3817 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3818 /* Whether it comes from a limited with. */
3819 const bool is_from_limited_with
3820 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3821 && From_Limited_With (gnat_desig_equiv));
3822 /* The "full view" of the designated type. If this is an incomplete
3823 entity from a limited with, treat its non-limited view as the full
3824 view. Otherwise, if this is an incomplete or private type, use the
3825 full view. In the former case, we might point to a private type,
3826 in which case, we need its full view. Also, we want to look at the
3827 actual type used for the representation, so this takes a total of
3828 three steps. */
3829 Entity_Id gnat_desig_full_direct_first
3830 = (is_from_limited_with
3831 ? Non_Limited_View (gnat_desig_equiv)
3832 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3833 ? Full_View (gnat_desig_equiv) : Empty));
3834 Entity_Id gnat_desig_full_direct
3835 = ((is_from_limited_with
3836 && Present (gnat_desig_full_direct_first)
3837 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3838 ? Full_View (gnat_desig_full_direct_first)
3839 : gnat_desig_full_direct_first);
3840 Entity_Id gnat_desig_full
3841 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3842 /* The type actually used to represent the designated type, either
3843 gnat_desig_full or gnat_desig_equiv. */
3844 Entity_Id gnat_desig_rep;
3845 /* We want to know if we'll be seeing the freeze node for any
3846 incomplete type we may be pointing to. */
3847 const bool in_main_unit
3848 = (Present (gnat_desig_full)
3849 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3850 : In_Extended_Main_Code_Unit (gnat_desig_type));
3851 /* True if we make a dummy type here. */
3852 bool made_dummy = false;
3853 /* The mode to be used for the pointer type. */
3854 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3855 /* The GCC type used for the designated type. */
3856 tree gnu_desig_type = NULL_TREE;
3858 if (!targetm.valid_pointer_mode (p_mode))
3859 p_mode = ptr_mode;
3861 /* If either the designated type or its full view is an unconstrained
3862 array subtype, replace it with the type it's a subtype of. This
3863 avoids problems with multiple copies of unconstrained array types.
3864 Likewise, if the designated type is a subtype of an incomplete
3865 record type, use the parent type to avoid order of elaboration
3866 issues. This can lose some code efficiency, but there is no
3867 alternative. */
3868 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3869 && !Is_Constrained (gnat_desig_equiv))
3870 gnat_desig_equiv = Etype (gnat_desig_equiv);
3871 if (Present (gnat_desig_full)
3872 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3873 && !Is_Constrained (gnat_desig_full))
3874 || (Ekind (gnat_desig_full) == E_Record_Subtype
3875 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3876 gnat_desig_full = Etype (gnat_desig_full);
3878 /* Set the type that's the representation of the designated type. */
3879 gnat_desig_rep
3880 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3882 /* If we already know what the full type is, use it. */
3883 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3884 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3886 /* Get the type of the thing we are to point to and build a pointer to
3887 it. If it is a reference to an incomplete or private type with a
3888 full view that is a record or an array, make a dummy type node and
3889 get the actual type later when we have verified it is safe. */
3890 else if ((!in_main_unit
3891 && !present_gnu_tree (gnat_desig_equiv)
3892 && Present (gnat_desig_full)
3893 && (Is_Record_Type (gnat_desig_full)
3894 || Is_Array_Type (gnat_desig_full)))
3895 /* Likewise if this is a reference to a record, an array or a
3896 subprogram type and we are to defer elaborating incomplete
3897 types. We do this because this access type may be the full
3898 view of a private type. */
3899 || ((!in_main_unit || imported_p)
3900 && defer_incomplete_level != 0
3901 && !present_gnu_tree (gnat_desig_equiv)
3902 && (Is_Record_Type (gnat_desig_rep)
3903 || Is_Array_Type (gnat_desig_rep)
3904 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3905 /* If this is a reference from a limited_with type back to our
3906 main unit and there's a freeze node for it, either we have
3907 already processed the declaration and made the dummy type,
3908 in which case we just reuse the latter, or we have not yet,
3909 in which case we make the dummy type and it will be reused
3910 when the declaration is finally processed. In both cases,
3911 the pointer eventually created below will be automatically
3912 adjusted when the freeze node is processed. */
3913 || (in_main_unit
3914 && is_from_limited_with
3915 && Present (Freeze_Node (gnat_desig_rep))))
3917 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3918 made_dummy = true;
3921 /* Otherwise handle the case of a pointer to itself. */
3922 else if (gnat_desig_equiv == gnat_entity)
3924 gnu_type
3925 = build_pointer_type_for_mode (void_type_node, p_mode,
3926 No_Strict_Aliasing (gnat_entity));
3927 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3930 /* If expansion is disabled, the equivalent type of a concurrent type
3931 is absent, so we use the void pointer type. */
3932 else if (type_annotate_only && No (gnat_desig_equiv))
3933 gnu_type = ptr_type_node;
3935 /* If the ultimately designated type is an incomplete type with no full
3936 view, we use the void pointer type in LTO mode to avoid emitting a
3937 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3938 the name of the dummy type in used by GDB for a global lookup. */
3939 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3940 && No (Full_View (gnat_desig_rep))
3941 && flag_generate_lto)
3942 gnu_type = ptr_type_node;
3944 /* Finally, handle the default case where we can just elaborate our
3945 designated type. */
3946 else
3947 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3949 /* It is possible that a call to gnat_to_gnu_type above resolved our
3950 type. If so, just return it. */
3951 if (present_gnu_tree (gnat_entity))
3953 maybe_present = true;
3954 break;
3957 /* Access-to-unconstrained-array types need a special treatment. */
3958 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3960 /* If the processing above got something that has a pointer, then
3961 we are done. This could have happened either because the type
3962 was elaborated or because somebody else executed the code. */
3963 if (!TYPE_POINTER_TO (gnu_desig_type))
3964 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3966 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3969 /* If we haven't done it yet, build the pointer type the usual way. */
3970 else if (!gnu_type)
3972 /* Modify the designated type if we are pointing only to constant
3973 objects, but don't do it for a dummy type. */
3974 if (Is_Access_Constant (gnat_entity)
3975 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3976 gnu_desig_type
3977 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3979 gnu_type
3980 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3981 No_Strict_Aliasing (gnat_entity));
3984 /* If the designated type is not declared in the main unit and we made
3985 a dummy node for it, save our definition, elaborate the actual type
3986 and replace the dummy type we made with the actual one. But if we
3987 are to defer actually looking up the actual type, make an entry in
3988 the deferred list instead. If this is from a limited with, we may
3989 have to defer until the end of the current unit. */
3990 if (!in_main_unit && made_dummy)
3992 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3993 gnu_type
3994 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3996 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3997 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3998 artificial_p, debug_info_p,
3999 gnat_entity);
4000 this_made_decl = true;
4001 gnu_type = TREE_TYPE (gnu_decl);
4002 save_gnu_tree (gnat_entity, gnu_decl, false);
4003 saved = true;
4005 if (defer_incomplete_level == 0 && !is_from_limited_with)
4007 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
4008 gnat_to_gnu_type (gnat_desig_equiv));
4010 else
4012 struct incomplete *p = XNEW (struct incomplete);
4013 struct incomplete **head
4014 = (is_from_limited_with
4015 ? &defer_limited_with_list : &defer_incomplete_list);
4017 p->old_type = gnu_desig_type;
4018 p->full_type = gnat_desig_equiv;
4019 p->next = *head;
4020 *head = p;
4024 break;
4026 case E_Access_Protected_Subprogram_Type:
4027 case E_Anonymous_Access_Protected_Subprogram_Type:
4028 /* If we are just annotating types and have no equivalent record type,
4029 just use the void pointer type. */
4030 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4031 gnu_type = ptr_type_node;
4033 /* The run-time representation is the equivalent type. */
4034 else
4036 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4037 maybe_present = true;
4040 /* The designated subtype must be elaborated as well, if it does
4041 not have its own freeze node. */
4042 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4043 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4044 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4045 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4046 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4047 NULL_TREE, false);
4049 break;
4051 case E_Access_Subtype:
4052 /* We treat this as identical to its base type; any constraint is
4053 meaningful only to the front-end. */
4054 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4056 /* The designated subtype must be elaborated as well, if it does
4057 not have its own freeze node. But designated subtypes created
4058 for constrained components of records with discriminants are
4059 not frozen by the front-end and not elaborated here, because
4060 their use may appear before the base type is frozen and it is
4061 not clear that they are needed in gigi. With the current model,
4062 there is no correct place where they could be elaborated. */
4063 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4064 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4065 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4066 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4068 /* If we are to defer elaborating incomplete types, make a dummy
4069 type node and elaborate it later. */
4070 if (defer_incomplete_level != 0)
4072 struct incomplete *p = XNEW (struct incomplete);
4074 p->old_type
4075 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4076 p->full_type = Directly_Designated_Type (gnat_entity);
4077 p->next = defer_incomplete_list;
4078 defer_incomplete_list = p;
4080 else if (!IN (Ekind (Base_Type
4081 (Directly_Designated_Type (gnat_entity))),
4082 Incomplete_Or_Private_Kind))
4083 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4084 NULL_TREE, false);
4087 maybe_present = true;
4088 break;
4090 /* Subprogram Entities
4092 The following access functions are defined for subprograms:
4094 Etype Return type or Standard_Void_Type.
4095 First_Formal The first formal parameter.
4096 Is_Imported Indicates that the subprogram has appeared in
4097 an INTERFACE or IMPORT pragma. For now we
4098 assume that the external language is C.
4099 Is_Exported Likewise but for an EXPORT pragma.
4100 Is_Inlined True if the subprogram is to be inlined.
4102 Each parameter is first checked by calling must_pass_by_ref on its
4103 type to determine if it is passed by reference. For parameters which
4104 are copied in, if they are Ada In Out or Out parameters, their return
4105 value becomes part of a record which becomes the return type of the
4106 function (C function - note that this applies only to Ada procedures
4107 so there is no Ada return type). Additional code to store back the
4108 parameters will be generated on the caller side. This transformation
4109 is done here, not in the front-end.
4111 The intended result of the transformation can be seen from the
4112 equivalent source rewritings that follow:
4114 struct temp {int a,b};
4115 procedure P (A,B: In Out ...) is temp P (int A,B)
4116 begin {
4117 .. ..
4118 end P; return {A,B};
4121 temp t;
4122 P(X,Y); t = P(X,Y);
4123 X = t.a , Y = t.b;
4125 For subprogram types we need to perform mainly the same conversions to
4126 GCC form that are needed for procedures and function declarations. The
4127 only difference is that at the end, we make a type declaration instead
4128 of a function declaration. */
4130 case E_Subprogram_Type:
4131 case E_Function:
4132 case E_Procedure:
4134 tree gnu_ext_name
4135 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
4136 enum inline_status_t inline_status
4137 = Has_Pragma_No_Inline (gnat_entity)
4138 ? is_suppressed
4139 : Has_Pragma_Inline_Always (gnat_entity)
4140 ? is_required
4141 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4142 bool public_flag = Is_Public (gnat_entity) || imported_p;
4143 /* Subprograms marked both Intrinsic and Always_Inline need not
4144 have a body of their own. */
4145 bool extern_flag
4146 = ((Is_Public (gnat_entity) && !definition)
4147 || imported_p
4148 || (Convention (gnat_entity) == Convention_Intrinsic
4149 && Has_Pragma_Inline_Always (gnat_entity)));
4150 tree gnu_param_list;
4152 /* A parameter may refer to this type, so defer completion of any
4153 incomplete types. */
4154 if (kind == E_Subprogram_Type && !definition)
4156 defer_incomplete_level++;
4157 this_deferred = true;
4160 /* If the subprogram has an alias, it is probably inherited, so
4161 we can use the original one. If the original "subprogram"
4162 is actually an enumeration literal, it may be the first use
4163 of its type, so we must elaborate that type now. */
4164 if (Present (Alias (gnat_entity)))
4166 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
4168 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4169 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
4170 false);
4172 gnu_decl
4173 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
4175 /* Elaborate any Itypes in the parameters of this entity. */
4176 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4177 Present (gnat_temp);
4178 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4179 if (Is_Itype (Etype (gnat_temp)))
4180 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
4182 /* Materialize renamed subprograms in the debugging information
4183 when the renamed object is compile time known. We can consider
4184 such renamings as imported declarations.
4186 Because the parameters in generics instantiation are generally
4187 materialized as renamings, we ofter end up having both the
4188 renamed subprogram and the renaming in the same context and with
4189 the same name: in this case, renaming is both useless debug-wise
4190 and potentially harmful as name resolution in the debugger could
4191 return twice the same entity! So avoid this case. */
4192 if (debug_info_p && !artificial_p
4193 && !(get_debug_scope (gnat_entity, NULL)
4194 == get_debug_scope (gnat_renamed, NULL)
4195 && Name_Equals (Chars (gnat_entity),
4196 Chars (gnat_renamed)))
4197 && Present (gnat_renamed)
4198 && (Ekind (gnat_renamed) == E_Function
4199 || Ekind (gnat_renamed) == E_Procedure)
4200 && gnu_decl
4201 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
4203 tree decl = build_decl (input_location, IMPORTED_DECL,
4204 gnu_entity_name, void_type_node);
4205 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
4206 gnat_pushdecl (decl, gnat_entity);
4209 break;
4212 /* Get the GCC tree for the (underlying) subprogram type. If the
4213 entity is an actual subprogram, also get the parameter list. */
4214 gnu_type
4215 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
4216 &gnu_param_list);
4217 if (DECL_P (gnu_type))
4219 gnu_decl = gnu_type;
4220 gnu_type = TREE_TYPE (gnu_decl);
4221 break;
4224 /* Deal with platform-specific calling conventions. */
4225 if (Has_Stdcall_Convention (gnat_entity))
4226 prepend_one_attribute
4227 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4228 get_identifier ("stdcall"), NULL_TREE,
4229 gnat_entity);
4230 else if (Has_Thiscall_Convention (gnat_entity))
4231 prepend_one_attribute
4232 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4233 get_identifier ("thiscall"), NULL_TREE,
4234 gnat_entity);
4236 /* If we should request stack realignment for a foreign convention
4237 subprogram, do so. Note that this applies to task entry points
4238 in particular. */
4239 if (FOREIGN_FORCE_REALIGN_STACK
4240 && Has_Foreign_Convention (gnat_entity))
4241 prepend_one_attribute
4242 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4243 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4244 gnat_entity);
4246 /* Deal with a pragma Linker_Section on a subprogram. */
4247 if ((kind == E_Function || kind == E_Procedure)
4248 && Present (Linker_Section_Pragma (gnat_entity)))
4249 prepend_one_attribute_pragma (&attr_list,
4250 Linker_Section_Pragma (gnat_entity));
4252 /* If we are defining the subprogram and it has an Address clause
4253 we must get the address expression from the saved GCC tree for the
4254 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4255 the address expression here since the front-end has guaranteed
4256 in that case that the elaboration has no effects. If there is
4257 an Address clause and we are not defining the object, just
4258 make it a constant. */
4259 if (Present (Address_Clause (gnat_entity)))
4261 tree gnu_address = NULL_TREE;
4263 if (definition)
4264 gnu_address
4265 = (present_gnu_tree (gnat_entity)
4266 ? get_gnu_tree (gnat_entity)
4267 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4269 save_gnu_tree (gnat_entity, NULL_TREE, false);
4271 /* Convert the type of the object to a reference type that can
4272 alias everything as per RM 13.3(19). */
4273 gnu_type
4274 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4275 if (gnu_address)
4276 gnu_address = convert (gnu_type, gnu_address);
4278 gnu_decl
4279 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4280 gnu_address, false, Is_Public (gnat_entity),
4281 extern_flag, false, false, artificial_p,
4282 debug_info_p, NULL, gnat_entity);
4283 DECL_BY_REF_P (gnu_decl) = 1;
4286 /* If this is a mere subprogram type, just create the declaration. */
4287 else if (kind == E_Subprogram_Type)
4289 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4291 gnu_decl
4292 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4293 debug_info_p, gnat_entity);
4296 /* Otherwise create the subprogram declaration with the external name,
4297 the type and the parameter list. However, if this a reference to
4298 the allocation routines, reuse the canonical declaration nodes as
4299 they come with special properties. */
4300 else
4302 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4303 gnu_decl = malloc_decl;
4304 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4305 gnu_decl = realloc_decl;
4306 else
4308 gnu_decl
4309 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4310 gnu_type, gnu_param_list,
4311 inline_status, public_flag,
4312 extern_flag, artificial_p,
4313 debug_info_p, attr_list, gnat_entity);
4315 DECL_STUBBED_P (gnu_decl)
4316 = (Convention (gnat_entity) == Convention_Stubbed);
4320 break;
4322 case E_Incomplete_Type:
4323 case E_Incomplete_Subtype:
4324 case E_Private_Type:
4325 case E_Private_Subtype:
4326 case E_Limited_Private_Type:
4327 case E_Limited_Private_Subtype:
4328 case E_Record_Type_With_Private:
4329 case E_Record_Subtype_With_Private:
4331 const bool is_from_limited_with
4332 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4333 /* Get the "full view" of this entity. If this is an incomplete
4334 entity from a limited with, treat its non-limited view as the
4335 full view. Otherwise, use either the full view or the underlying
4336 full view, whichever is present. This is used in all the tests
4337 below. */
4338 const Entity_Id full_view
4339 = is_from_limited_with
4340 ? Non_Limited_View (gnat_entity)
4341 : Present (Full_View (gnat_entity))
4342 ? Full_View (gnat_entity)
4343 : IN (kind, Private_Kind)
4344 ? Underlying_Full_View (gnat_entity)
4345 : Empty;
4347 /* If this is an incomplete type with no full view, it must be a Taft
4348 Amendment type or an incomplete type coming from a limited context,
4349 in which cases we return a dummy type. Otherwise, we just get the
4350 type from its Etype. */
4351 if (No (full_view))
4353 if (kind == E_Incomplete_Type)
4355 gnu_type = make_dummy_type (gnat_entity);
4356 gnu_decl = TYPE_STUB_DECL (gnu_type);
4358 else
4360 gnu_decl
4361 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4362 maybe_present = true;
4366 /* Or else, if we already made a type for the full view, reuse it. */
4367 else if (present_gnu_tree (full_view))
4368 gnu_decl = get_gnu_tree (full_view);
4370 /* Or else, if we are not defining the type or there is no freeze
4371 node on it, get the type for the full view. Likewise if this is
4372 a limited_with'ed type not declared in the main unit, which can
4373 happen for incomplete formal types instantiated on a type coming
4374 from a limited_with clause. */
4375 else if (!definition
4376 || No (Freeze_Node (full_view))
4377 || (is_from_limited_with
4378 && !In_Extended_Main_Code_Unit (full_view)))
4380 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4381 maybe_present = true;
4384 /* Otherwise, make a dummy type entry which will be replaced later.
4385 Save it as the full declaration's type so we can do any needed
4386 updates when we see it. */
4387 else
4389 gnu_type = make_dummy_type (gnat_entity);
4390 gnu_decl = TYPE_STUB_DECL (gnu_type);
4391 if (Has_Completion_In_Body (gnat_entity))
4392 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4393 save_gnu_tree (full_view, gnu_decl, false);
4396 break;
4398 case E_Class_Wide_Type:
4399 /* Class-wide types are always transformed into their root type. */
4400 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4401 maybe_present = true;
4402 break;
4404 case E_Protected_Type:
4405 case E_Protected_Subtype:
4406 case E_Task_Type:
4407 case E_Task_Subtype:
4408 /* If we are just annotating types and have no equivalent record type,
4409 just return void_type, except for root types that have discriminants
4410 because the discriminants will very likely be used in the declarative
4411 part of the associated body so they need to be translated. */
4412 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4414 if (Has_Discriminants (gnat_entity)
4415 && Root_Type (gnat_entity) == gnat_entity)
4417 tree gnu_field_list = NULL_TREE;
4418 Entity_Id gnat_field;
4420 /* This is a minimal version of the E_Record_Type handling. */
4421 gnu_type = make_node (RECORD_TYPE);
4422 TYPE_NAME (gnu_type) = gnu_entity_name;
4424 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4425 Present (gnat_field);
4426 gnat_field = Next_Stored_Discriminant (gnat_field))
4428 tree gnu_field
4429 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4430 definition, debug_info_p);
4432 save_gnu_tree (gnat_field,
4433 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4434 build0 (PLACEHOLDER_EXPR, gnu_type),
4435 gnu_field, NULL_TREE),
4436 true);
4438 DECL_CHAIN (gnu_field) = gnu_field_list;
4439 gnu_field_list = gnu_field;
4442 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4443 false);
4445 else
4446 gnu_type = void_type_node;
4449 /* Concurrent types are always transformed into their record type. */
4450 else
4451 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4452 maybe_present = true;
4453 break;
4455 case E_Label:
4456 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4457 break;
4459 case E_Block:
4460 case E_Loop:
4461 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4462 we've already saved it, so we don't try to. */
4463 gnu_decl = error_mark_node;
4464 saved = true;
4465 break;
4467 case E_Abstract_State:
4468 /* This is a SPARK annotation that only reaches here when compiling in
4469 ASIS mode. */
4470 gcc_assert (type_annotate_only);
4471 gnu_decl = error_mark_node;
4472 saved = true;
4473 break;
4475 default:
4476 gcc_unreachable ();
4479 /* If we had a case where we evaluated another type and it might have
4480 defined this one, handle it here. */
4481 if (maybe_present && present_gnu_tree (gnat_entity))
4483 gnu_decl = get_gnu_tree (gnat_entity);
4484 saved = true;
4487 /* If we are processing a type and there is either no decl for it or
4488 we just made one, do some common processing for the type, such as
4489 handling alignment and possible padding. */
4490 if (is_type && (!gnu_decl || this_made_decl))
4492 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4494 /* Process the attributes, if not already done. Note that the type is
4495 already defined so we cannot pass true for IN_PLACE here. */
4496 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4498 /* Tell the middle-end that objects of tagged types are guaranteed to
4499 be properly aligned. This is necessary because conversions to the
4500 class-wide type are translated into conversions to the root type,
4501 which can be less aligned than some of its derived types. */
4502 if (Is_Tagged_Type (gnat_entity)
4503 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4504 TYPE_ALIGN_OK (gnu_type) = 1;
4506 /* Record whether the type is passed by reference. */
4507 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4508 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4510 /* ??? Don't set the size for a String_Literal since it is either
4511 confirming or we don't handle it properly (if the low bound is
4512 non-constant). */
4513 if (!gnu_size && kind != E_String_Literal_Subtype)
4515 Uint gnat_size = Known_Esize (gnat_entity)
4516 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4517 gnu_size
4518 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4519 false, Has_Size_Clause (gnat_entity));
4522 /* If a size was specified, see if we can make a new type of that size
4523 by rearranging the type, for example from a fat to a thin pointer. */
4524 if (gnu_size)
4526 gnu_type
4527 = make_type_from_size (gnu_type, gnu_size,
4528 Has_Biased_Representation (gnat_entity));
4530 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4531 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4532 gnu_size = NULL_TREE;
4535 /* If the alignment has not already been processed and this is not
4536 an unconstrained array type, see if an alignment is specified.
4537 If not, we pick a default alignment for atomic objects. */
4538 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4540 else if (Known_Alignment (gnat_entity))
4542 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4543 TYPE_ALIGN (gnu_type));
4545 /* Warn on suspiciously large alignments. This should catch
4546 errors about the (alignment,byte)/(size,bit) discrepancy. */
4547 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4549 tree size;
4551 /* If a size was specified, take it into account. Otherwise
4552 use the RM size for records or unions as the type size has
4553 already been adjusted to the alignment. */
4554 if (gnu_size)
4555 size = gnu_size;
4556 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4557 && !TYPE_FAT_POINTER_P (gnu_type))
4558 size = rm_size (gnu_type);
4559 else
4560 size = TYPE_SIZE (gnu_type);
4562 /* Consider an alignment as suspicious if the alignment/size
4563 ratio is greater or equal to the byte/bit ratio. */
4564 if (tree_fits_uhwi_p (size)
4565 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4566 post_error_ne ("?suspiciously large alignment specified for&",
4567 Expression (Alignment_Clause (gnat_entity)),
4568 gnat_entity);
4571 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4572 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4573 && integer_pow2p (TYPE_SIZE (gnu_type)))
4574 align = MIN (BIGGEST_ALIGNMENT,
4575 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4576 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4577 && tree_fits_uhwi_p (gnu_size)
4578 && integer_pow2p (gnu_size))
4579 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4581 /* See if we need to pad the type. If we did, and made a record,
4582 the name of the new type may be changed. So get it back for
4583 us when we make the new TYPE_DECL below. */
4584 if (gnu_size || align > 0)
4585 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4586 false, !gnu_decl, definition, false);
4588 if (TYPE_IS_PADDING_P (gnu_type))
4589 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4591 /* Now set the RM size of the type. We cannot do it before padding
4592 because we need to accept arbitrary RM sizes on integral types. */
4593 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4595 /* If we are at global level, GCC will have applied variable_size to
4596 the type, but that won't have done anything. So, if it's not
4597 a constant or self-referential, call elaborate_expression_1 to
4598 make a variable for the size rather than calculating it each time.
4599 Handle both the RM size and the actual size. */
4600 if (TYPE_SIZE (gnu_type)
4601 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4602 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4603 && global_bindings_p ())
4605 tree size = TYPE_SIZE (gnu_type);
4607 TYPE_SIZE (gnu_type)
4608 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4609 false);
4611 /* ??? For now, store the size as a multiple of the alignment in
4612 bytes so that we can see the alignment from the tree. */
4613 TYPE_SIZE_UNIT (gnu_type)
4614 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4615 "SIZE_A_UNIT", definition, false,
4616 TYPE_ALIGN (gnu_type));
4618 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4619 may not be marked by the call to create_type_decl below. */
4620 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4622 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4624 tree variant_part = get_variant_part (gnu_type);
4625 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4627 if (variant_part)
4629 tree union_type = TREE_TYPE (variant_part);
4630 tree offset = DECL_FIELD_OFFSET (variant_part);
4632 /* If the position of the variant part is constant, subtract
4633 it from the size of the type of the parent to get the new
4634 size. This manual CSE reduces the data size. */
4635 if (TREE_CODE (offset) == INTEGER_CST)
4637 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4638 TYPE_SIZE (union_type)
4639 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4640 bit_from_pos (offset, bitpos));
4641 TYPE_SIZE_UNIT (union_type)
4642 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4643 byte_from_pos (offset, bitpos));
4645 else
4647 TYPE_SIZE (union_type)
4648 = elaborate_expression_1 (TYPE_SIZE (union_type),
4649 gnat_entity, "VSIZE",
4650 definition, false);
4652 /* ??? For now, store the size as a multiple of the
4653 alignment in bytes so that we can see the alignment
4654 from the tree. */
4655 TYPE_SIZE_UNIT (union_type)
4656 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4657 gnat_entity, "VSIZE_A_UNIT",
4658 definition, false,
4659 TYPE_ALIGN (union_type));
4661 /* ??? For now, store the offset as a multiple of the
4662 alignment in bytes so that we can see the alignment
4663 from the tree. */
4664 DECL_FIELD_OFFSET (variant_part)
4665 = elaborate_expression_2 (offset, gnat_entity,
4666 "VOFFSET", definition, false,
4667 DECL_OFFSET_ALIGN
4668 (variant_part));
4671 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4672 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4675 if (operand_equal_p (ada_size, size, 0))
4676 ada_size = TYPE_SIZE (gnu_type);
4677 else
4678 ada_size
4679 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4680 definition, false);
4681 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4685 /* Similarly, if this is a record type or subtype at global level, call
4686 elaborate_expression_2 on any field position. Skip any fields that
4687 we haven't made trees for to avoid problems with class-wide types. */
4688 if (IN (kind, Record_Kind) && global_bindings_p ())
4689 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4690 gnat_temp = Next_Entity (gnat_temp))
4691 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4693 tree gnu_field = get_gnu_tree (gnat_temp);
4695 /* ??? For now, store the offset as a multiple of the alignment
4696 in bytes so that we can see the alignment from the tree. */
4697 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4698 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4700 DECL_FIELD_OFFSET (gnu_field)
4701 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4702 gnat_temp, "OFFSET", definition,
4703 false,
4704 DECL_OFFSET_ALIGN (gnu_field));
4706 /* ??? The context of gnu_field is not necessarily gnu_type
4707 so the MULT_EXPR node built above may not be marked by
4708 the call to create_type_decl below. */
4709 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4713 if (Is_Atomic_Or_VFA (gnat_entity))
4714 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4716 /* If this is not an unconstrained array type, set some flags. */
4717 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4719 if (Present (Alignment_Clause (gnat_entity)))
4720 TYPE_USER_ALIGN (gnu_type) = 1;
4722 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4723 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4725 /* If it is passed by reference, force BLKmode to ensure that
4726 objects of this type will always be put in memory. */
4727 if (TYPE_MODE (gnu_type) != BLKmode
4728 && AGGREGATE_TYPE_P (gnu_type)
4729 && TYPE_BY_REFERENCE_P (gnu_type))
4730 SET_TYPE_MODE (gnu_type, BLKmode);
4732 if (Treat_As_Volatile (gnat_entity))
4734 const int quals
4735 = TYPE_QUAL_VOLATILE
4736 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4737 gnu_type = change_qualified_type (gnu_type, quals);
4741 /* If this is a derived type, relate its alias set to that of its parent
4742 to avoid troubles when a call to an inherited primitive is inlined in
4743 a context where a derived object is accessed. The inlined code works
4744 on the parent view so the resulting code may access the same object
4745 using both the parent and the derived alias sets, which thus have to
4746 conflict. As the same issue arises with component references, the
4747 parent alias set also has to conflict with composite types enclosing
4748 derived components. For instance, if we have:
4750 type D is new T;
4751 type R is record
4752 Component : D;
4753 end record;
4755 we want T to conflict with both D and R, in addition to R being a
4756 superset of D by record/component construction.
4758 One way to achieve this is to perform an alias set copy from the
4759 parent to the derived type. This is not quite appropriate, though,
4760 as we don't want separate derived types to conflict with each other:
4762 type I1 is new Integer;
4763 type I2 is new Integer;
4765 We want I1 and I2 to both conflict with Integer but we do not want
4766 I1 to conflict with I2, and an alias set copy on derivation would
4767 have that effect.
4769 The option chosen is to make the alias set of the derived type a
4770 superset of that of its parent type. It trivially fulfills the
4771 simple requirement for the Integer derivation example above, and
4772 the component case as well by superset transitivity:
4774 superset superset
4775 R ----------> D ----------> T
4777 However, for composite types, conversions between derived types are
4778 translated into VIEW_CONVERT_EXPRs so a sequence like:
4780 type Comp1 is new Comp;
4781 type Comp2 is new Comp;
4782 procedure Proc (C : Comp1);
4784 C : Comp2;
4785 Proc (Comp1 (C));
4787 is translated into:
4789 C : Comp2;
4790 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4792 and gimplified into:
4794 C : Comp2;
4795 Comp1 *C.0;
4796 C.0 = (Comp1 *) &C;
4797 Proc (C.0);
4799 i.e. generates code involving type punning. Therefore, Comp1 needs
4800 to conflict with Comp2 and an alias set copy is required.
4802 The language rules ensure the parent type is already frozen here. */
4803 if (kind != E_Subprogram_Type
4804 && Is_Derived_Type (gnat_entity)
4805 && !type_annotate_only)
4807 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4808 /* For constrained packed array subtypes, the implementation type is
4809 used instead of the nominal type. */
4810 if (kind == E_Array_Subtype
4811 && Is_Constrained (gnat_entity)
4812 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4813 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4814 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4815 Is_Composite_Type (gnat_entity)
4816 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4819 if (!gnu_decl)
4820 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4821 artificial_p, debug_info_p,
4822 gnat_entity);
4823 else
4825 TREE_TYPE (gnu_decl) = gnu_type;
4826 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4830 /* If we got a type that is not dummy, back-annotate the alignment of the
4831 type if not already in the tree. Likewise for the size, if any. */
4832 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4834 gnu_type = TREE_TYPE (gnu_decl);
4836 if (Unknown_Alignment (gnat_entity))
4838 unsigned int double_align, align;
4839 bool is_capped_double, align_clause;
4841 /* If the default alignment of "double" or larger scalar types is
4842 specifically capped and this is not an array with an alignment
4843 clause on the component type, return the cap. */
4844 if ((double_align = double_float_alignment) > 0)
4845 is_capped_double
4846 = is_double_float_or_array (gnat_entity, &align_clause);
4847 else if ((double_align = double_scalar_alignment) > 0)
4848 is_capped_double
4849 = is_double_scalar_or_array (gnat_entity, &align_clause);
4850 else
4851 is_capped_double = align_clause = false;
4853 if (is_capped_double && !align_clause)
4854 align = double_align;
4855 else
4856 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4858 Set_Alignment (gnat_entity, UI_From_Int (align));
4861 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4863 tree gnu_size = TYPE_SIZE (gnu_type);
4865 /* If the size is self-referential, annotate the maximum value. */
4866 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4867 gnu_size = max_size (gnu_size, true);
4869 /* If we are just annotating types and the type is tagged, the tag
4870 and the parent components are not generated by the front-end so
4871 alignment and sizes must be adjusted if there is no rep clause. */
4872 if (type_annotate_only
4873 && Is_Tagged_Type (gnat_entity)
4874 && Unknown_RM_Size (gnat_entity)
4875 && !VOID_TYPE_P (gnu_type)
4876 && (!TYPE_FIELDS (gnu_type)
4877 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4879 tree offset;
4881 if (Is_Derived_Type (gnat_entity))
4883 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4884 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4885 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4887 else
4889 unsigned int align
4890 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4891 offset = bitsize_int (POINTER_SIZE);
4892 Set_Alignment (gnat_entity, UI_From_Int (align));
4895 if (TYPE_FIELDS (gnu_type))
4896 offset
4897 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4899 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4900 gnu_size = round_up (gnu_size, POINTER_SIZE);
4901 Uint uint_size = annotate_value (gnu_size);
4902 Set_RM_Size (gnat_entity, uint_size);
4903 Set_Esize (gnat_entity, uint_size);
4906 /* If there is a rep clause, only adjust alignment and Esize. */
4907 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4909 unsigned int align
4910 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4911 Set_Alignment (gnat_entity, UI_From_Int (align));
4912 gnu_size = round_up (gnu_size, POINTER_SIZE);
4913 Set_Esize (gnat_entity, annotate_value (gnu_size));
4916 /* Otherwise no adjustment is needed. */
4917 else
4918 Set_Esize (gnat_entity, annotate_value (gnu_size));
4921 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4922 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4925 /* If we haven't already, associate the ..._DECL node that we just made with
4926 the input GNAT entity node. */
4927 if (!saved)
4928 save_gnu_tree (gnat_entity, gnu_decl, false);
4930 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4931 eliminate as many deferred computations as possible. */
4932 process_deferred_decl_context (false);
4934 /* If this is an enumeration or floating-point type, we were not able to set
4935 the bounds since they refer to the type. These are always static. */
4936 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4937 || (kind == E_Floating_Point_Type))
4939 tree gnu_scalar_type = gnu_type;
4940 tree gnu_low_bound, gnu_high_bound;
4942 /* If this is a padded type, we need to use the underlying type. */
4943 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4944 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4946 /* If this is a floating point type and we haven't set a floating
4947 point type yet, use this in the evaluation of the bounds. */
4948 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4949 longest_float_type_node = gnu_scalar_type;
4951 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4952 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4954 if (kind == E_Enumeration_Type)
4956 /* Enumeration types have specific RM bounds. */
4957 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4958 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4960 else
4962 /* Floating-point types don't have specific RM bounds. */
4963 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4964 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4968 /* If we deferred processing of incomplete types, re-enable it. If there
4969 were no other disables and we have deferred types to process, do so. */
4970 if (this_deferred
4971 && --defer_incomplete_level == 0
4972 && defer_incomplete_list)
4974 struct incomplete *p, *next;
4976 /* We are back to level 0 for the deferring of incomplete types.
4977 But processing these incomplete types below may itself require
4978 deferring, so preserve what we have and restart from scratch. */
4979 p = defer_incomplete_list;
4980 defer_incomplete_list = NULL;
4982 for (; p; p = next)
4984 next = p->next;
4986 if (p->old_type)
4987 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4988 gnat_to_gnu_type (p->full_type));
4989 free (p);
4993 /* If we are not defining this type, see if it's on one of the lists of
4994 incomplete types. If so, handle the list entry now. */
4995 if (is_type && !definition)
4997 struct incomplete *p;
4999 for (p = defer_incomplete_list; p; p = p->next)
5000 if (p->old_type && p->full_type == gnat_entity)
5002 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5003 TREE_TYPE (gnu_decl));
5004 p->old_type = NULL_TREE;
5007 for (p = defer_limited_with_list; p; p = p->next)
5008 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5010 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5011 TREE_TYPE (gnu_decl));
5012 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
5013 update_profiles_with (p->old_type);
5014 p->old_type = NULL_TREE;
5018 if (this_global)
5019 force_global--;
5021 /* If this is a packed array type whose original array type is itself
5022 an Itype without freeze node, make sure the latter is processed. */
5023 if (Is_Packed_Array_Impl_Type (gnat_entity)
5024 && Is_Itype (Original_Array_Type (gnat_entity))
5025 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5026 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5027 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
5029 return gnu_decl;
5032 /* Similar, but if the returned value is a COMPONENT_REF, return the
5033 FIELD_DECL. */
5035 tree
5036 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5038 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5040 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5041 gnu_field = TREE_OPERAND (gnu_field, 1);
5043 return gnu_field;
5046 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5047 the GCC type corresponding to that entity. */
5049 tree
5050 gnat_to_gnu_type (Entity_Id gnat_entity)
5052 tree gnu_decl;
5054 /* The back end never attempts to annotate generic types. */
5055 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5056 return void_type_node;
5058 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5059 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5061 return TREE_TYPE (gnu_decl);
5064 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5065 the unpadded version of the GCC type corresponding to that entity. */
5067 tree
5068 get_unpadded_type (Entity_Id gnat_entity)
5070 tree type = gnat_to_gnu_type (gnat_entity);
5072 if (TYPE_IS_PADDING_P (type))
5073 type = TREE_TYPE (TYPE_FIELDS (type));
5075 return type;
5078 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5079 a C++ imported method or equivalent.
5081 We use the predicate on 32-bit x86/Windows to find out whether we need to
5082 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5083 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5085 bool
5086 is_cplusplus_method (Entity_Id gnat_entity)
5088 /* Check that the subprogram has C++ convention. */
5089 if (Convention (gnat_entity) != Convention_CPP)
5090 return false;
5092 /* A constructor is a method on the C++ side. We deal with it now because
5093 it is declared without the 'this' parameter in the sources and, although
5094 the front-end will create a version with the 'this' parameter for code
5095 generation purposes, we want to return true for both versions. */
5096 if (Is_Constructor (gnat_entity))
5097 return true;
5099 /* And that the type of the first parameter (indirectly) has it too. */
5100 Entity_Id gnat_first = First_Formal (gnat_entity);
5101 if (No (gnat_first))
5102 return false;
5104 Entity_Id gnat_type = Etype (gnat_first);
5105 if (Is_Access_Type (gnat_type))
5106 gnat_type = Directly_Designated_Type (gnat_type);
5107 if (Convention (gnat_type) != Convention_CPP)
5108 return false;
5110 /* This is the main case: C++ method imported as a primitive operation.
5111 Note that a C++ class with no virtual functions can be imported as a
5112 limited record type so the operation is not necessarily dispatching. */
5113 if (Is_Primitive (gnat_entity))
5114 return true;
5116 /* A thunk needs to be handled like its associated primitive operation. */
5117 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5118 return true;
5120 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5121 if (Is_Dispatch_Table_Entity (gnat_entity))
5122 return true;
5124 return false;
5127 /* Finalize the processing of From_Limited_With incomplete types. */
5129 void
5130 finalize_from_limited_with (void)
5132 struct incomplete *p, *next;
5134 p = defer_limited_with_list;
5135 defer_limited_with_list = NULL;
5137 for (; p; p = next)
5139 next = p->next;
5141 if (p->old_type)
5143 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5144 gnat_to_gnu_type (p->full_type));
5145 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
5146 update_profiles_with (p->old_type);
5149 free (p);
5153 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5154 kind of type (such E_Task_Type) that has a different type which Gigi
5155 uses for its representation. If the type does not have a special type
5156 for its representation, return GNAT_ENTITY. If a type is supposed to
5157 exist, but does not, abort unless annotating types, in which case
5158 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5160 Entity_Id
5161 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5163 Entity_Id gnat_equiv = gnat_entity;
5165 if (No (gnat_entity))
5166 return gnat_entity;
5168 switch (Ekind (gnat_entity))
5170 case E_Class_Wide_Subtype:
5171 if (Present (Equivalent_Type (gnat_entity)))
5172 gnat_equiv = Equivalent_Type (gnat_entity);
5173 break;
5175 case E_Access_Protected_Subprogram_Type:
5176 case E_Anonymous_Access_Protected_Subprogram_Type:
5177 if (Present (Equivalent_Type (gnat_entity)))
5178 gnat_equiv = Equivalent_Type (gnat_entity);
5179 break;
5181 case E_Class_Wide_Type:
5182 gnat_equiv = Root_Type (gnat_entity);
5183 break;
5185 case E_Protected_Type:
5186 case E_Protected_Subtype:
5187 case E_Task_Type:
5188 case E_Task_Subtype:
5189 if (Present (Corresponding_Record_Type (gnat_entity)))
5190 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5191 break;
5193 default:
5194 break;
5197 return gnat_equiv;
5200 /* Return a GCC tree for a type corresponding to the component type of the
5201 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5202 is for an array being defined. DEBUG_INFO_P is true if we need to write
5203 debug information for other types that we may create in the process. */
5205 static tree
5206 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5207 bool debug_info_p)
5209 const Entity_Id gnat_type = Component_Type (gnat_array);
5210 tree gnu_type = gnat_to_gnu_type (gnat_type);
5211 tree gnu_comp_size;
5212 unsigned int max_align;
5214 /* If an alignment is specified, use it as a cap on the component type
5215 so that it can be honored for the whole type. But ignore it for the
5216 original type of packed array types. */
5217 if (No (Packed_Array_Impl_Type (gnat_array))
5218 && Known_Alignment (gnat_array))
5219 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5220 else
5221 max_align = 0;
5223 /* Try to get a smaller form of the component if needed. */
5224 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5225 && !Is_Bit_Packed_Array (gnat_array)
5226 && !Has_Aliased_Components (gnat_array)
5227 && !Strict_Alignment (gnat_type)
5228 && RECORD_OR_UNION_TYPE_P (gnu_type)
5229 && !TYPE_FAT_POINTER_P (gnu_type)
5230 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5231 gnu_type = make_packable_type (gnu_type, false, max_align);
5233 if (Has_Atomic_Components (gnat_array))
5234 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5236 /* Get and validate any specified Component_Size. */
5237 gnu_comp_size
5238 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5239 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5240 true, Has_Component_Size_Clause (gnat_array));
5242 /* If the array has aliased components and the component size can be zero,
5243 force at least unit size to ensure that the components have distinct
5244 addresses. */
5245 if (!gnu_comp_size
5246 && Has_Aliased_Components (gnat_array)
5247 && (integer_zerop (TYPE_SIZE (gnu_type))
5248 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5249 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5250 gnu_comp_size
5251 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5253 /* If the component type is a RECORD_TYPE that has a self-referential size,
5254 then use the maximum size for the component size. */
5255 if (!gnu_comp_size
5256 && TREE_CODE (gnu_type) == RECORD_TYPE
5257 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5258 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5260 /* Honor the component size. This is not needed for bit-packed arrays. */
5261 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5263 tree orig_type = gnu_type;
5265 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5266 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5267 gnu_type = orig_type;
5268 else
5269 orig_type = gnu_type;
5271 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5272 true, false, definition, true);
5274 /* If a padding record was made, declare it now since it will never be
5275 declared otherwise. This is necessary to ensure that its subtrees
5276 are properly marked. */
5277 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5278 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5279 gnat_array);
5282 /* If the component type is a padded type made for a non-bit-packed array
5283 of scalars with reverse storage order, we need to propagate the reverse
5284 storage order to the padding type since it is the innermost enclosing
5285 aggregate type around the scalar. */
5286 if (TYPE_IS_PADDING_P (gnu_type)
5287 && Reverse_Storage_Order (gnat_array)
5288 && !Is_Bit_Packed_Array (gnat_array)
5289 && Is_Scalar_Type (gnat_type))
5290 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5292 if (Has_Volatile_Components (gnat_array))
5294 const int quals
5295 = TYPE_QUAL_VOLATILE
5296 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5297 gnu_type = change_qualified_type (gnu_type, quals);
5300 return gnu_type;
5303 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5304 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5305 the type of the parameter. FIRST is true if this is the first parameter in
5306 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5307 the copy-in copy-out implementation mechanism.
5309 The returned tree is a PARM_DECL, except for the cases where no parameter
5310 needs to be actually passed to the subprogram; the type of this "shadow"
5311 parameter is then returned instead. */
5313 static tree
5314 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5315 Entity_Id gnat_subprog, bool *cico)
5317 Entity_Id gnat_param_type = Etype (gnat_param);
5318 Mechanism_Type mech = Mechanism (gnat_param);
5319 tree gnu_param_name = get_entity_name (gnat_param);
5320 bool foreign = Has_Foreign_Convention (gnat_subprog);
5321 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5322 /* The parameter can be indirectly modified if its address is taken. */
5323 bool ro_param = in_param && !Address_Taken (gnat_param);
5324 bool by_return = false, by_component_ptr = false;
5325 bool by_ref = false;
5326 bool restricted_aliasing_p = false;
5327 location_t saved_location = input_location;
5328 tree gnu_param;
5330 /* Make sure to use the proper SLOC for vector ABI warnings. */
5331 if (VECTOR_TYPE_P (gnu_param_type))
5332 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5334 /* Builtins are expanded inline and there is no real call sequence involved.
5335 So the type expected by the underlying expander is always the type of the
5336 argument "as is". */
5337 if (Convention (gnat_subprog) == Convention_Intrinsic
5338 && Present (Interface_Name (gnat_subprog)))
5339 mech = By_Copy;
5341 /* Handle the first parameter of a valued procedure specially: it's a copy
5342 mechanism for which the parameter is never allocated. */
5343 else if (first && Is_Valued_Procedure (gnat_subprog))
5345 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5346 mech = By_Copy;
5347 by_return = true;
5350 /* Or else, see if a Mechanism was supplied that forced this parameter
5351 to be passed one way or another. */
5352 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5355 /* Positive mechanism means by copy for sufficiently small parameters. */
5356 else if (mech > 0)
5358 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5359 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5360 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5361 mech = By_Reference;
5362 else
5363 mech = By_Copy;
5366 /* Otherwise, it's an unsupported mechanism so error out. */
5367 else
5369 post_error ("unsupported mechanism for&", gnat_param);
5370 mech = Default;
5373 /* If this is either a foreign function or if the underlying type won't
5374 be passed by reference and is as aligned as the original type, strip
5375 off possible padding type. */
5376 if (TYPE_IS_PADDING_P (gnu_param_type))
5378 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5380 if (foreign
5381 || (!must_pass_by_ref (unpadded_type)
5382 && mech != By_Reference
5383 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5384 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5385 gnu_param_type = unpadded_type;
5388 /* If this is a read-only parameter, make a variant of the type that is
5389 read-only. ??? However, if this is an unconstrained array, that type
5390 can be very complex, so skip it for now. Likewise for any other
5391 self-referential type. */
5392 if (ro_param
5393 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5394 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5395 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5397 /* For foreign conventions, pass arrays as pointers to the element type.
5398 First check for unconstrained array and get the underlying array. */
5399 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5400 gnu_param_type
5401 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5403 /* For GCC builtins, pass Address integer types as (void *) */
5404 if (Convention (gnat_subprog) == Convention_Intrinsic
5405 && Present (Interface_Name (gnat_subprog))
5406 && Is_Descendant_Of_Address (gnat_param_type))
5407 gnu_param_type = ptr_type_node;
5409 /* Arrays are passed as pointers to element type for foreign conventions. */
5410 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5412 /* Strip off any multi-dimensional entries, then strip
5413 off the last array to get the component type. */
5414 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5415 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5416 gnu_param_type = TREE_TYPE (gnu_param_type);
5418 by_component_ptr = true;
5419 gnu_param_type = TREE_TYPE (gnu_param_type);
5421 if (ro_param)
5422 gnu_param_type
5423 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5425 gnu_param_type = build_pointer_type (gnu_param_type);
5428 /* Fat pointers are passed as thin pointers for foreign conventions. */
5429 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5430 gnu_param_type
5431 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5433 /* If we were requested or muss pass by reference, do so.
5434 If we were requested to pass by copy, do so.
5435 Otherwise, for foreign conventions, pass In Out or Out parameters
5436 or aggregates by reference. For COBOL and Fortran, pass all
5437 integer and FP types that way too. For Convention Ada, use
5438 the standard Ada default. */
5439 else if (mech == By_Reference
5440 || must_pass_by_ref (gnu_param_type)
5441 || (mech != By_Copy
5442 && ((foreign
5443 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5444 || (foreign
5445 && (Convention (gnat_subprog) == Convention_Fortran
5446 || Convention (gnat_subprog) == Convention_COBOL)
5447 && (INTEGRAL_TYPE_P (gnu_param_type)
5448 || FLOAT_TYPE_P (gnu_param_type)))
5449 || (!foreign
5450 && default_pass_by_ref (gnu_param_type)))))
5452 /* We take advantage of 6.2(12) by considering that references built for
5453 parameters whose type isn't by-ref and for which the mechanism hasn't
5454 been forced to by-ref allow only a restricted form of aliasing. */
5455 restricted_aliasing_p
5456 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5457 gnu_param_type = build_reference_type (gnu_param_type);
5458 by_ref = true;
5461 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5462 else if (!in_param)
5463 *cico = true;
5465 input_location = saved_location;
5467 if (mech == By_Copy && (by_ref || by_component_ptr))
5468 post_error ("?cannot pass & by copy", gnat_param);
5470 /* If this is an Out parameter that isn't passed by reference and isn't
5471 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5472 it will be a VAR_DECL created when we process the procedure, so just
5473 return its type. For the special parameter of a valued procedure,
5474 never pass it in.
5476 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5477 Out parameters with discriminants or implicit initial values to be
5478 handled like In Out parameters. These type are normally built as
5479 aggregates, hence passed by reference, except for some packed arrays
5480 which end up encoded in special integer types. Note that scalars can
5481 be given implicit initial values using the Default_Value aspect.
5483 The exception we need to make is then for packed arrays of records
5484 with discriminants or implicit initial values. We have no light/easy
5485 way to check for the latter case, so we merely check for packed arrays
5486 of records. This may lead to useless copy-in operations, but in very
5487 rare cases only, as these would be exceptions in a set of already
5488 exceptional situations. */
5489 if (Ekind (gnat_param) == E_Out_Parameter
5490 && !by_ref
5491 && (by_return
5492 || (!POINTER_TYPE_P (gnu_param_type)
5493 && !AGGREGATE_TYPE_P (gnu_param_type)
5494 && !Has_Default_Aspect (gnat_param_type)))
5495 && !(Is_Array_Type (gnat_param_type)
5496 && Is_Packed (gnat_param_type)
5497 && Is_Composite_Type (Component_Type (gnat_param_type))))
5498 return gnu_param_type;
5500 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5501 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5502 DECL_BY_REF_P (gnu_param) = by_ref;
5503 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5504 DECL_POINTS_TO_READONLY_P (gnu_param)
5505 = (ro_param && (by_ref || by_component_ptr));
5506 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5507 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5508 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5510 /* If no Mechanism was specified, indicate what we're using, then
5511 back-annotate it. */
5512 if (mech == Default)
5513 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5515 Set_Mechanism (gnat_param, mech);
5516 return gnu_param;
5519 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5520 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5522 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5523 the corresponding profile, which means that, by the time the freeze node
5524 of the subprogram is encountered, types involved in its profile may still
5525 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5526 the freeze node of types involved in its profile, either types of formal
5527 parameters or the return type. */
5529 static void
5530 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5532 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5534 struct tree_entity_vec_map in;
5535 in.base.from = gnu_type;
5536 struct tree_entity_vec_map **slot
5537 = dummy_to_subprog_map->find_slot (&in, INSERT);
5538 if (!*slot)
5540 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5541 e->base.from = gnu_type;
5542 e->to = NULL;
5543 *slot = e;
5546 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5547 because the vector might have been just emptied by update_profiles_with.
5548 This can happen when there are 2 freeze nodes associated with different
5549 views of the same type; the type will be really complete only after the
5550 second freeze node is encountered. */
5551 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5553 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5555 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5556 since this would mean updating twice its profile. */
5557 if (v)
5559 const unsigned len = v->length ();
5560 unsigned int l = 0, u = len;
5562 /* Entity_Id is a simple integer so we can implement a stable order on
5563 the vector with an ordered insertion scheme and binary search. */
5564 while (l < u)
5566 unsigned int m = (l + u) / 2;
5567 int diff = (int) (*v)[m] - (int) gnat_subprog;
5568 if (diff > 0)
5569 u = m;
5570 else if (diff < 0)
5571 l = m + 1;
5572 else
5573 return;
5576 /* l == u and therefore is the insertion point. */
5577 vec_safe_insert (v, l, gnat_subprog);
5579 else
5580 vec_safe_push (v, gnat_subprog);
5582 (*slot)->to = v;
5585 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5587 static void
5588 update_profile (Entity_Id gnat_subprog)
5590 tree gnu_param_list;
5591 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5592 Needs_Debug_Info (gnat_subprog),
5593 &gnu_param_list);
5594 if (DECL_P (gnu_type))
5596 /* Builtins cannot have their address taken so we can reset them. */
5597 gcc_assert (DECL_BUILT_IN (gnu_type));
5598 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5599 save_gnu_tree (gnat_subprog, gnu_type, false);
5600 return;
5603 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5605 TREE_TYPE (gnu_subprog) = gnu_type;
5607 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5608 and needs to be adjusted too. */
5609 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5611 tree gnu_entity_name = get_entity_name (gnat_subprog);
5612 tree gnu_ext_name
5613 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5615 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5616 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5620 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5621 a dummy type which appears in profiles. */
5623 void
5624 update_profiles_with (tree gnu_type)
5626 struct tree_entity_vec_map in;
5627 in.base.from = gnu_type;
5628 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5629 gcc_assert (e);
5630 vec<Entity_Id, va_gc_atomic> *v = e->to;
5631 e->to = NULL;
5633 /* The flag needs to be reset before calling update_profile, in case
5634 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5635 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5637 unsigned int i;
5638 Entity_Id *iter;
5639 FOR_EACH_VEC_ELT (*v, i, iter)
5640 update_profile (*iter);
5642 vec_free (v);
5645 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5647 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5648 context may now appear as parameter and result types. As a consequence,
5649 we may need to defer their translation until after a freeze node is seen
5650 or to the end of the current unit. We also aim at handling temporarily
5651 incomplete types created by the usual delayed elaboration scheme. */
5653 static tree
5654 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5656 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5657 so the rationale is exposed in that place. These processings probably
5658 ought to be merged at some point. */
5659 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5660 const bool is_from_limited_with
5661 = (IN (Ekind (gnat_equiv), Incomplete_Kind)
5662 && From_Limited_With (gnat_equiv));
5663 Entity_Id gnat_full_direct_first
5664 = (is_from_limited_with
5665 ? Non_Limited_View (gnat_equiv)
5666 : (IN (Ekind (gnat_equiv), Incomplete_Or_Private_Kind)
5667 ? Full_View (gnat_equiv) : Empty));
5668 Entity_Id gnat_full_direct
5669 = ((is_from_limited_with
5670 && Present (gnat_full_direct_first)
5671 && IN (Ekind (gnat_full_direct_first), Private_Kind))
5672 ? Full_View (gnat_full_direct_first)
5673 : gnat_full_direct_first);
5674 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5675 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5676 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5677 tree gnu_type;
5679 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5680 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5682 else if (is_from_limited_with
5683 && ((!in_main_unit
5684 && !present_gnu_tree (gnat_equiv)
5685 && Present (gnat_full)
5686 && (Is_Record_Type (gnat_full)
5687 || Is_Array_Type (gnat_full)
5688 || Is_Access_Type (gnat_full)))
5689 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5691 gnu_type = make_dummy_type (gnat_equiv);
5693 if (!in_main_unit)
5695 struct incomplete *p = XNEW (struct incomplete);
5697 p->old_type = gnu_type;
5698 p->full_type = gnat_equiv;
5699 p->next = defer_limited_with_list;
5700 defer_limited_with_list = p;
5704 else if (type_annotate_only && No (gnat_equiv))
5705 gnu_type = void_type_node;
5707 else
5708 gnu_type = gnat_to_gnu_type (gnat_equiv);
5710 /* Access-to-unconstrained-array types need a special treatment. */
5711 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5713 if (!TYPE_POINTER_TO (gnu_type))
5714 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5717 return gnu_type;
5720 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5721 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5722 is true if we need to write debug information for other types that we may
5723 create in the process. Also set PARAM_LIST to the list of parameters.
5724 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5725 directly instead of its type. */
5727 static tree
5728 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5729 bool debug_info_p, tree *param_list)
5731 const Entity_Kind kind = Ekind (gnat_subprog);
5732 Entity_Id gnat_return_type = Etype (gnat_subprog);
5733 Entity_Id gnat_param;
5734 tree gnu_type = present_gnu_tree (gnat_subprog)
5735 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5736 tree gnu_return_type;
5737 tree gnu_param_type_list = NULL_TREE;
5738 tree gnu_param_list = NULL_TREE;
5739 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5740 (In Out or Out parameters not passed by reference), in which case it is
5741 the list of nodes used to specify the values of the In Out/Out parameters
5742 that are returned as a record upon procedure return. The TREE_PURPOSE of
5743 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5744 is the PARM_DECL corresponding to that field. This list will be saved in
5745 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5746 tree gnu_cico_list = NULL_TREE;
5747 tree gnu_cico_return_type = NULL_TREE;
5748 /* Fields in return type of procedure with copy-in copy-out parameters. */
5749 tree gnu_field_list = NULL_TREE;
5750 /* The semantics of "pure" in Ada essentially matches that of "const"
5751 in the back-end. In particular, both properties are orthogonal to
5752 the "nothrow" property if the EH circuitry is explicit in the
5753 internal representation of the back-end. If we are to completely
5754 hide the EH circuitry from it, we need to declare that calls to pure
5755 Ada subprograms that can throw have side effects since they can
5756 trigger an "abnormal" transfer of control flow; thus they can be
5757 neither "const" nor "pure" in the back-end sense. */
5758 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5759 bool return_by_direct_ref_p = false;
5760 bool return_by_invisi_ref_p = false;
5761 bool return_unconstrained_p = false;
5762 bool incomplete_profile_p = false;
5763 unsigned int num;
5765 /* Look into the return type and get its associated GCC tree if it is not
5766 void, and then compute various flags for the subprogram type. But make
5767 sure not to do this processing multiple times. */
5768 if (Ekind (gnat_return_type) == E_Void)
5769 gnu_return_type = void_type_node;
5771 else if (gnu_type
5772 && TREE_CODE (gnu_type) == FUNCTION_TYPE
5773 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5775 gnu_return_type = TREE_TYPE (gnu_type);
5776 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5777 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5778 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5781 else
5783 if (Convention (gnat_subprog) == Convention_C
5784 && Is_Descendant_Of_Address (gnat_return_type))
5785 gnu_return_type = ptr_type_node;
5786 else
5787 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5789 /* If this function returns by reference, make the actual return type
5790 the reference type and make a note of that. */
5791 if (Returns_By_Ref (gnat_subprog))
5793 gnu_return_type = build_reference_type (gnu_return_type);
5794 return_by_direct_ref_p = true;
5797 /* If the return type is an unconstrained array type, the return value
5798 will be allocated on the secondary stack so the actual return type
5799 is the fat pointer type. */
5800 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5802 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5803 return_unconstrained_p = true;
5806 /* This is the same unconstrained array case, but for a dummy type. */
5807 else if (TYPE_REFERENCE_TO (gnu_return_type)
5808 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5810 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5811 return_unconstrained_p = true;
5814 /* Likewise, if the return type requires a transient scope, the return
5815 value will also be allocated on the secondary stack so the actual
5816 return type is the reference type. */
5817 else if (Requires_Transient_Scope (gnat_return_type))
5819 gnu_return_type = build_reference_type (gnu_return_type);
5820 return_unconstrained_p = true;
5823 /* If the Mechanism is By_Reference, ensure this function uses the
5824 target's by-invisible-reference mechanism, which may not be the
5825 same as above (e.g. it might be passing an extra parameter). */
5826 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5827 return_by_invisi_ref_p = true;
5829 /* Likewise, if the return type is itself By_Reference. */
5830 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5831 return_by_invisi_ref_p = true;
5833 /* If the type is a padded type and the underlying type would not be
5834 passed by reference or the function has a foreign convention, return
5835 the underlying type. */
5836 else if (TYPE_IS_PADDING_P (gnu_return_type)
5837 && (!default_pass_by_ref
5838 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5839 || Has_Foreign_Convention (gnat_subprog)))
5840 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5842 /* If the return type is unconstrained, it must have a maximum size.
5843 Use the padded type as the effective return type. And ensure the
5844 function uses the target's by-invisible-reference mechanism to
5845 avoid copying too much data when it returns. */
5846 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5848 tree orig_type = gnu_return_type;
5849 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5851 /* If the size overflows to 0, set it to an arbitrary positive
5852 value so that assignments in the type are preserved. Their
5853 actual size is independent of this positive value. */
5854 if (TREE_CODE (max_return_size) == INTEGER_CST
5855 && TREE_OVERFLOW (max_return_size)
5856 && integer_zerop (max_return_size))
5858 max_return_size = copy_node (bitsize_unit_node);
5859 TREE_OVERFLOW (max_return_size) = 1;
5862 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5863 0, gnat_subprog, false, false,
5864 definition, true);
5866 /* Declare it now since it will never be declared otherwise. This
5867 is necessary to ensure that its subtrees are properly marked. */
5868 if (gnu_return_type != orig_type
5869 && !DECL_P (TYPE_NAME (gnu_return_type)))
5870 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5871 true, debug_info_p, gnat_subprog);
5873 return_by_invisi_ref_p = true;
5876 /* If the return type has a size that overflows, we usually cannot have
5877 a function that returns that type. This usage doesn't really make
5878 sense anyway, so issue an error here. */
5879 if (!return_by_invisi_ref_p
5880 && TYPE_SIZE_UNIT (gnu_return_type)
5881 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5882 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5884 post_error ("cannot return type whose size overflows", gnat_subprog);
5885 gnu_return_type = copy_type (gnu_return_type);
5886 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5887 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5890 /* If the return type is incomplete, there are 2 cases: if the function
5891 returns by reference, then the return type is only linked indirectly
5892 in the profile, so the profile can be seen as complete since it need
5893 not be further modified, only the reference types need be adjusted;
5894 otherwise the profile is incomplete and need be adjusted too. */
5895 if (TYPE_IS_DUMMY_P (gnu_return_type))
5897 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5898 incomplete_profile_p = true;
5901 if (kind == E_Function)
5902 Set_Mechanism (gnat_subprog, return_unconstrained_p
5903 || return_by_direct_ref_p
5904 || return_by_invisi_ref_p
5905 ? By_Reference : By_Copy);
5908 /* A procedure (something that doesn't return anything) shouldn't be
5909 considered const since there would be no reason for calling such a
5910 subprogram. Note that procedures with Out (or In Out) parameters
5911 have already been converted into a function with a return type.
5912 Similarly, if the function returns an unconstrained type, then the
5913 function will allocate the return value on the secondary stack and
5914 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5915 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5916 const_flag = false;
5918 /* Loop over the parameters and get their associated GCC tree. While doing
5919 this, build a copy-in copy-out structure if we need one. */
5920 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5921 Present (gnat_param);
5922 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5924 const bool mech_is_by_ref
5925 = Mechanism (gnat_param) == By_Reference
5926 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5927 tree gnu_param_name = get_entity_name (gnat_param);
5928 tree gnu_param, gnu_param_type;
5929 bool cico = false;
5931 /* Fetch an existing parameter with complete type and reuse it. But we
5932 didn't save the CICO property so we can only do it for In parameters
5933 or parameters passed by reference. */
5934 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5935 && present_gnu_tree (gnat_param)
5936 && (gnu_param = get_gnu_tree (gnat_param))
5937 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5939 DECL_CHAIN (gnu_param) = NULL_TREE;
5940 gnu_param_type = TREE_TYPE (gnu_param);
5943 /* Otherwise translate the parameter type and act accordingly. */
5944 else
5946 Entity_Id gnat_param_type = Etype (gnat_param);
5948 if (Convention (gnat_subprog) == Convention_C
5949 && Is_Descendant_Of_Address (gnat_param_type))
5950 gnu_param_type = ptr_type_node;
5951 else
5952 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5954 /* If the parameter type is incomplete, there are 2 cases: if it is
5955 passed by reference, then the type is only linked indirectly in
5956 the profile, so the profile can be seen as complete since it need
5957 not be further modified, only the reference type need be adjusted;
5958 otherwise the profile is incomplete and need be adjusted too. */
5959 if (TYPE_IS_DUMMY_P (gnu_param_type))
5961 Node_Id gnat_decl;
5963 if (mech_is_by_ref
5964 || (TYPE_REFERENCE_TO (gnu_param_type)
5965 && TYPE_IS_FAT_POINTER_P
5966 (TYPE_REFERENCE_TO (gnu_param_type)))
5967 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5969 gnu_param_type = build_reference_type (gnu_param_type);
5970 gnu_param
5971 = create_param_decl (gnu_param_name, gnu_param_type);
5972 TREE_READONLY (gnu_param) = 1;
5973 DECL_BY_REF_P (gnu_param) = 1;
5974 DECL_POINTS_TO_READONLY_P (gnu_param)
5975 = (Ekind (gnat_param) == E_In_Parameter
5976 && !Address_Taken (gnat_param));
5977 Set_Mechanism (gnat_param, By_Reference);
5978 Sloc_to_locus (Sloc (gnat_param),
5979 &DECL_SOURCE_LOCATION (gnu_param));
5982 /* ??? This is a kludge to support null procedures in spec taking
5983 a parameter with an untagged incomplete type coming from a
5984 limited context. The front-end creates a body without knowing
5985 anything about the non-limited view, which is illegal Ada and
5986 cannot be supported. Create a parameter with a fake type. */
5987 else if (kind == E_Procedure
5988 && (gnat_decl = Parent (gnat_subprog))
5989 && Nkind (gnat_decl) == N_Procedure_Specification
5990 && Null_Present (gnat_decl)
5991 && IN (Ekind (gnat_param_type), Incomplete_Kind))
5992 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5994 else
5996 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
5997 Call_to_gnu will stop if it encounters the PARM_DECL. */
5998 gnu_param
5999 = build_decl (input_location, PARM_DECL, gnu_param_name,
6000 gnu_param_type);
6001 associate_subprog_with_dummy_type (gnat_subprog,
6002 gnu_param_type);
6003 incomplete_profile_p = true;
6007 /* Otherwise build the parameter declaration normally. */
6008 else
6010 gnu_param
6011 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
6012 gnat_subprog, &cico);
6014 /* We are returned either a PARM_DECL or a type if no parameter
6015 needs to be passed; in either case, adjust the type. */
6016 if (DECL_P (gnu_param))
6017 gnu_param_type = TREE_TYPE (gnu_param);
6018 else
6020 gnu_param_type = gnu_param;
6021 gnu_param = NULL_TREE;
6026 /* If we have a GCC tree for the parameter, register it. */
6027 save_gnu_tree (gnat_param, NULL_TREE, false);
6028 if (gnu_param)
6030 gnu_param_type_list
6031 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
6032 gnu_param_list = chainon (gnu_param, gnu_param_list);
6033 save_gnu_tree (gnat_param, gnu_param, false);
6035 /* If a parameter is a pointer, a function may modify memory through
6036 it and thus shouldn't be considered a const function. Also, the
6037 memory may be modified between two calls, so they can't be CSE'ed.
6038 The latter case also handles by-ref parameters. */
6039 if (POINTER_TYPE_P (gnu_param_type)
6040 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
6041 const_flag = false;
6044 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
6045 for it in the return type and register the association. */
6046 if (cico && !incomplete_profile_p)
6048 if (!gnu_cico_list)
6050 gnu_cico_return_type = make_node (RECORD_TYPE);
6052 /* If this is a function, we also need a field for the
6053 return value to be placed. */
6054 if (!VOID_TYPE_P (gnu_return_type))
6056 tree gnu_field
6057 = create_field_decl (get_identifier ("RETVAL"),
6058 gnu_return_type,
6059 gnu_cico_return_type, NULL_TREE,
6060 NULL_TREE, 0, 0);
6061 Sloc_to_locus (Sloc (gnat_subprog),
6062 &DECL_SOURCE_LOCATION (gnu_field));
6063 gnu_field_list = gnu_field;
6064 gnu_cico_list
6065 = tree_cons (gnu_field, void_type_node, NULL_TREE);
6068 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
6069 /* Set a default alignment to speed up accesses. But we should
6070 not increase the size of the structure too much, lest it does
6071 not fit in return registers anymore. */
6072 SET_TYPE_ALIGN (gnu_cico_return_type,
6073 get_mode_alignment (ptr_mode));
6076 tree gnu_field
6077 = create_field_decl (gnu_param_name, gnu_param_type,
6078 gnu_cico_return_type, NULL_TREE, NULL_TREE,
6079 0, 0);
6080 Sloc_to_locus (Sloc (gnat_param),
6081 &DECL_SOURCE_LOCATION (gnu_field));
6082 DECL_CHAIN (gnu_field) = gnu_field_list;
6083 gnu_field_list = gnu_field;
6084 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
6088 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
6089 and finish up the return type. */
6090 if (gnu_cico_list && !incomplete_profile_p)
6092 /* If we have a CICO list but it has only one entry, we convert
6093 this function into a function that returns this object. */
6094 if (list_length (gnu_cico_list) == 1)
6095 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
6097 /* Do not finalize the return type if the subprogram is stubbed
6098 since structures are incomplete for the back-end. */
6099 else if (Convention (gnat_subprog) != Convention_Stubbed)
6101 finish_record_type (gnu_cico_return_type, nreverse (gnu_field_list),
6102 0, false);
6104 /* Try to promote the mode of the return type if it is passed
6105 in registers, again to speed up accesses. */
6106 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
6107 && !targetm.calls.return_in_memory (gnu_cico_return_type,
6108 NULL_TREE))
6110 unsigned int size
6111 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
6112 unsigned int i = BITS_PER_UNIT;
6113 machine_mode mode;
6115 while (i < size)
6116 i <<= 1;
6117 mode = mode_for_size (i, MODE_INT, 0);
6118 if (mode != BLKmode)
6120 SET_TYPE_MODE (gnu_cico_return_type, mode);
6121 SET_TYPE_ALIGN (gnu_cico_return_type,
6122 GET_MODE_ALIGNMENT (mode));
6123 TYPE_SIZE (gnu_cico_return_type)
6124 = bitsize_int (GET_MODE_BITSIZE (mode));
6125 TYPE_SIZE_UNIT (gnu_cico_return_type)
6126 = size_int (GET_MODE_SIZE (mode));
6130 if (debug_info_p)
6131 rest_of_record_type_compilation (gnu_cico_return_type);
6134 gnu_return_type = gnu_cico_return_type;
6137 /* The lists have been built in reverse. */
6138 gnu_param_type_list = nreverse (gnu_param_type_list);
6139 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
6140 *param_list = nreverse (gnu_param_list);
6141 gnu_cico_list = nreverse (gnu_cico_list);
6143 /* If the profile is incomplete, we only set the (temporary) return and
6144 parameter types; otherwise, we build the full type. In either case,
6145 we reuse an already existing GCC tree that we built previously here. */
6146 if (incomplete_profile_p)
6148 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
6150 else
6151 gnu_type = make_node (FUNCTION_TYPE);
6152 TREE_TYPE (gnu_type) = gnu_return_type;
6153 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6154 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6155 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6156 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6158 else
6160 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
6162 TREE_TYPE (gnu_type) = gnu_return_type;
6163 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6164 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6165 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6166 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6167 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6168 TYPE_CANONICAL (gnu_type) = gnu_type;
6169 layout_type (gnu_type);
6171 else
6173 gnu_type
6174 = build_function_type (gnu_return_type, gnu_param_type_list);
6176 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
6177 has a different TYPE_CI_CO_LIST or flags. */
6178 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
6179 return_unconstrained_p,
6180 return_by_direct_ref_p,
6181 return_by_invisi_ref_p))
6183 gnu_type = copy_type (gnu_type);
6184 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6185 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6186 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6187 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6191 if (const_flag)
6192 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6194 if (No_Return (gnat_subprog))
6195 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6197 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6198 corresponding DECL node and check the parameter association. */
6199 if (Convention (gnat_subprog) == Convention_Intrinsic
6200 && Present (Interface_Name (gnat_subprog)))
6202 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6203 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6205 /* If we have a builtin DECL for that function, use it. Check if
6206 the profiles are compatible and warn if they are not. Note that
6207 the checker is expected to post diagnostics in this case. */
6208 if (gnu_builtin_decl)
6210 intrin_binding_t inb
6211 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6213 if (!intrin_profiles_compatible_p (&inb))
6214 post_error
6215 ("?profile of& doesn''t match the builtin it binds!",
6216 gnat_subprog);
6218 return gnu_builtin_decl;
6221 /* Inability to find the builtin DECL most often indicates a genuine
6222 mistake, but imports of unregistered intrinsics are sometimes used
6223 on purpose to allow hooking in alternate bodies; we post a warning
6224 conditioned on Wshadow in this case, to let developers be notified
6225 on demand without risking false positives with common default sets
6226 of options. */
6227 if (warn_shadow)
6228 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6232 return gnu_type;
6235 /* Return the external name for GNAT_SUBPROG given its entity name. */
6237 static tree
6238 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6240 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6242 /* If there was no specified Interface_Name and the external and
6243 internal names of the subprogram are the same, only use the
6244 internal name to allow disambiguation of nested subprograms. */
6245 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6246 gnu_ext_name = NULL_TREE;
6248 return gnu_ext_name;
6251 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6252 qualifiers on TYPE. */
6254 static tree
6255 change_qualified_type (tree type, int type_quals)
6257 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6260 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6261 build_nonshared_array_type. */
6263 static void
6264 set_nonaliased_component_on_array_type (tree type)
6266 TYPE_NONALIASED_COMPONENT (type) = 1;
6267 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6270 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6271 build_nonshared_array_type. */
6273 static void
6274 set_reverse_storage_order_on_array_type (tree type)
6276 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6277 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6280 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6282 static bool
6283 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6285 while (Present (Corresponding_Discriminant (discr1)))
6286 discr1 = Corresponding_Discriminant (discr1);
6288 while (Present (Corresponding_Discriminant (discr2)))
6289 discr2 = Corresponding_Discriminant (discr2);
6291 return
6292 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6295 /* Return true if the array type GNU_TYPE, which represents a dimension of
6296 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6298 static bool
6299 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6301 /* If the array type is not the innermost dimension of the GNAT type,
6302 then it has a non-aliased component. */
6303 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6304 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6305 return true;
6307 /* If the array type has an aliased component in the front-end sense,
6308 then it also has an aliased component in the back-end sense. */
6309 if (Has_Aliased_Components (gnat_type))
6310 return false;
6312 /* If this is a derived type, then it has a non-aliased component if
6313 and only if its parent type also has one. */
6314 if (Is_Derived_Type (gnat_type))
6316 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6317 int index;
6318 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6319 gnu_parent_type
6320 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6321 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6322 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6323 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6326 /* Otherwise, rely exclusively on properties of the element type. */
6327 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6330 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6332 static bool
6333 compile_time_known_address_p (Node_Id gnat_address)
6335 /* Catch System'To_Address. */
6336 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6337 gnat_address = Expression (gnat_address);
6339 return Compile_Time_Known_Value (gnat_address);
6342 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6343 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6345 static bool
6346 cannot_be_superflat (Node_Id gnat_range)
6348 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6349 Node_Id scalar_range;
6350 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6352 /* If the low bound is not constant, try to find an upper bound. */
6353 while (Nkind (gnat_lb) != N_Integer_Literal
6354 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6355 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6356 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6357 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6358 || Nkind (scalar_range) == N_Range))
6359 gnat_lb = High_Bound (scalar_range);
6361 /* If the high bound is not constant, try to find a lower bound. */
6362 while (Nkind (gnat_hb) != N_Integer_Literal
6363 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6364 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6365 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6366 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6367 || Nkind (scalar_range) == N_Range))
6368 gnat_hb = Low_Bound (scalar_range);
6370 /* If we have failed to find constant bounds, punt. */
6371 if (Nkind (gnat_lb) != N_Integer_Literal
6372 || Nkind (gnat_hb) != N_Integer_Literal)
6373 return false;
6375 /* We need at least a signed 64-bit type to catch most cases. */
6376 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6377 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6378 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6379 return false;
6381 /* If the low bound is the smallest integer, nothing can be smaller. */
6382 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6383 if (TREE_OVERFLOW (gnu_lb_minus_one))
6384 return true;
6386 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6389 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6391 static bool
6392 constructor_address_p (tree gnu_expr)
6394 while (TREE_CODE (gnu_expr) == NOP_EXPR
6395 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6396 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6397 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6399 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6400 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6403 /* Return true if the size in units represented by GNU_SIZE can be handled by
6404 an allocation. If STATIC_P is true, consider only what can be done with a
6405 static allocation. */
6407 static bool
6408 allocatable_size_p (tree gnu_size, bool static_p)
6410 /* We can allocate a fixed size if it is a valid for the middle-end. */
6411 if (TREE_CODE (gnu_size) == INTEGER_CST)
6412 return valid_constant_size_p (gnu_size);
6414 /* We can allocate a variable size if this isn't a static allocation. */
6415 else
6416 return !static_p;
6419 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6420 initial value of an object of GNU_TYPE. */
6422 static bool
6423 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6425 /* Do not convert if the object's type is unconstrained because this would
6426 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6427 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6428 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6429 return false;
6431 /* Do not convert if the object's type is a padding record whose field is of
6432 self-referential size because we want to copy only the actual data. */
6433 if (type_is_padding_self_referential (gnu_type))
6434 return false;
6436 /* Do not convert a call to a function that returns with variable size since
6437 we want to use the return slot optimization in this case. */
6438 if (TREE_CODE (gnu_expr) == CALL_EXPR
6439 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6440 return false;
6442 /* Do not convert to a record type with a variant part from a record type
6443 without one, to keep the object simpler. */
6444 if (TREE_CODE (gnu_type) == RECORD_TYPE
6445 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6446 && get_variant_part (gnu_type)
6447 && !get_variant_part (TREE_TYPE (gnu_expr)))
6448 return false;
6450 /* In all the other cases, convert the expression to the object's type. */
6451 return true;
6454 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6455 be elaborated at the point of its definition, but do nothing else. */
6457 void
6458 elaborate_entity (Entity_Id gnat_entity)
6460 switch (Ekind (gnat_entity))
6462 case E_Signed_Integer_Subtype:
6463 case E_Modular_Integer_Subtype:
6464 case E_Enumeration_Subtype:
6465 case E_Ordinary_Fixed_Point_Subtype:
6466 case E_Decimal_Fixed_Point_Subtype:
6467 case E_Floating_Point_Subtype:
6469 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6470 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6472 /* ??? Tests to avoid Constraint_Error in static expressions
6473 are needed until after the front stops generating bogus
6474 conversions on bounds of real types. */
6475 if (!Raises_Constraint_Error (gnat_lb))
6476 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6477 Needs_Debug_Info (gnat_entity));
6478 if (!Raises_Constraint_Error (gnat_hb))
6479 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6480 Needs_Debug_Info (gnat_entity));
6481 break;
6484 case E_Record_Subtype:
6485 case E_Private_Subtype:
6486 case E_Limited_Private_Subtype:
6487 case E_Record_Subtype_With_Private:
6488 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6490 Node_Id gnat_discriminant_expr;
6491 Entity_Id gnat_field;
6493 for (gnat_field
6494 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6495 gnat_discriminant_expr
6496 = First_Elmt (Discriminant_Constraint (gnat_entity));
6497 Present (gnat_field);
6498 gnat_field = Next_Discriminant (gnat_field),
6499 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6500 /* Ignore access discriminants. */
6501 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6502 elaborate_expression (Node (gnat_discriminant_expr),
6503 gnat_entity, get_entity_char (gnat_field),
6504 true, false, false);
6506 break;
6511 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6512 NAME, ARGS and ERROR_POINT. */
6514 static void
6515 prepend_one_attribute (struct attrib **attr_list,
6516 enum attrib_type attrib_type,
6517 tree attr_name,
6518 tree attr_args,
6519 Node_Id attr_error_point)
6521 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6523 attr->type = attrib_type;
6524 attr->name = attr_name;
6525 attr->args = attr_args;
6526 attr->error_point = attr_error_point;
6528 attr->next = *attr_list;
6529 *attr_list = attr;
6532 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6534 static void
6535 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6537 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6538 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6539 enum attrib_type etype;
6541 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6542 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6544 case Pragma_Machine_Attribute:
6545 etype = ATTR_MACHINE_ATTRIBUTE;
6546 break;
6548 case Pragma_Linker_Alias:
6549 etype = ATTR_LINK_ALIAS;
6550 break;
6552 case Pragma_Linker_Section:
6553 etype = ATTR_LINK_SECTION;
6554 break;
6556 case Pragma_Linker_Constructor:
6557 etype = ATTR_LINK_CONSTRUCTOR;
6558 break;
6560 case Pragma_Linker_Destructor:
6561 etype = ATTR_LINK_DESTRUCTOR;
6562 break;
6564 case Pragma_Weak_External:
6565 etype = ATTR_WEAK_EXTERNAL;
6566 break;
6568 case Pragma_Thread_Local_Storage:
6569 etype = ATTR_THREAD_LOCAL_STORAGE;
6570 break;
6572 default:
6573 return;
6576 /* See what arguments we have and turn them into GCC trees for attribute
6577 handlers. These expect identifier for strings. We handle at most two
6578 arguments and static expressions only. */
6579 if (Present (gnat_arg) && Present (First (gnat_arg)))
6581 Node_Id gnat_arg0 = Next (First (gnat_arg));
6582 Node_Id gnat_arg1 = Empty;
6584 if (Present (gnat_arg0)
6585 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6587 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6589 if (TREE_CODE (gnu_arg0) == STRING_CST)
6591 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6592 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6593 return;
6596 gnat_arg1 = Next (gnat_arg0);
6599 if (Present (gnat_arg1)
6600 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6602 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6604 if (TREE_CODE (gnu_arg1) == STRING_CST)
6605 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6609 /* Prepend to the list. Make a list of the argument we might have, as GCC
6610 expects it. */
6611 prepend_one_attribute (attr_list, etype, gnu_arg0,
6612 gnu_arg1
6613 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6614 Present (Next (First (gnat_arg)))
6615 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6618 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6620 static void
6621 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6623 Node_Id gnat_temp;
6625 /* Attributes are stored as Representation Item pragmas. */
6626 for (gnat_temp = First_Rep_Item (gnat_entity);
6627 Present (gnat_temp);
6628 gnat_temp = Next_Rep_Item (gnat_temp))
6629 if (Nkind (gnat_temp) == N_Pragma)
6630 prepend_one_attribute_pragma (attr_list, gnat_temp);
6633 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6634 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6635 return the GCC tree to use for that expression. S is the suffix to use
6636 if a variable needs to be created and DEFINITION is true if this is done
6637 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6638 otherwise, we are just elaborating the expression for side-effects. If
6639 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6640 isn't needed for code generation. */
6642 static tree
6643 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6644 bool definition, bool need_value, bool need_debug)
6646 tree gnu_expr;
6648 /* If we already elaborated this expression (e.g. it was involved
6649 in the definition of a private type), use the old value. */
6650 if (present_gnu_tree (gnat_expr))
6651 return get_gnu_tree (gnat_expr);
6653 /* If we don't need a value and this is static or a discriminant,
6654 we don't need to do anything. */
6655 if (!need_value
6656 && (Is_OK_Static_Expression (gnat_expr)
6657 || (Nkind (gnat_expr) == N_Identifier
6658 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6659 return NULL_TREE;
6661 /* If it's a static expression, we don't need a variable for debugging. */
6662 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6663 need_debug = false;
6665 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6666 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6667 definition, need_debug);
6669 /* Save the expression in case we try to elaborate this entity again. Since
6670 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6671 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6672 save_gnu_tree (gnat_expr, gnu_expr, true);
6674 return need_value ? gnu_expr : error_mark_node;
6677 /* Similar, but take a GNU expression and always return a result. */
6679 static tree
6680 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6681 bool definition, bool need_debug)
6683 const bool expr_public_p = Is_Public (gnat_entity);
6684 const bool expr_global_p = expr_public_p || global_bindings_p ();
6685 bool expr_variable_p, use_variable;
6687 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6688 that an expression cannot contain both a discriminant and a variable. */
6689 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6690 return gnu_expr;
6692 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6693 a variable that is initialized to contain the expression when the package
6694 containing the definition is elaborated. If this entity is defined at top
6695 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6696 if this is necessary. */
6697 if (TREE_CONSTANT (gnu_expr))
6698 expr_variable_p = false;
6699 else
6701 /* Skip any conversions and simple constant arithmetics to see if the
6702 expression is based on a read-only variable. */
6703 tree inner = remove_conversions (gnu_expr, true);
6705 inner = skip_simple_constant_arithmetic (inner);
6707 if (handled_component_p (inner))
6708 inner = get_inner_constant_reference (inner);
6710 expr_variable_p
6711 = !(inner
6712 && TREE_CODE (inner) == VAR_DECL
6713 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6716 /* We only need to use the variable if we are in a global context since GCC
6717 can do the right thing in the local case. However, when not optimizing,
6718 use it for bounds of loop iteration scheme to avoid code duplication. */
6719 use_variable = expr_variable_p
6720 && (expr_global_p
6721 || (!optimize
6722 && definition
6723 && Is_Itype (gnat_entity)
6724 && Nkind (Associated_Node_For_Itype (gnat_entity))
6725 == N_Loop_Parameter_Specification));
6727 /* Now create it, possibly only for debugging purposes. */
6728 if (use_variable || need_debug)
6730 /* The following variable creation can happen when processing the body
6731 of subprograms that are defined out of the extended main unit and
6732 inlined. In this case, we are not at the global scope, and thus the
6733 new variable must not be tagged "external", as we used to do here as
6734 soon as DEFINITION was false. */
6735 tree gnu_decl
6736 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6737 TREE_TYPE (gnu_expr), gnu_expr, true,
6738 expr_public_p, !definition && expr_global_p,
6739 expr_global_p, false, true, need_debug,
6740 NULL, gnat_entity);
6742 /* Using this variable at debug time (if need_debug is true) requires a
6743 proper location. The back-end will compute a location for this
6744 variable only if the variable is used by the generated code.
6745 Returning the variable ensures the caller will use it in generated
6746 code. Note that there is no need for a location if the debug info
6747 contains an integer constant.
6748 TODO: when the encoding-based debug scheme is dropped, move this
6749 condition to the top-level IF block: we will not need to create a
6750 variable anymore in such cases, then. */
6751 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6752 return gnu_decl;
6755 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6758 /* Similar, but take an alignment factor and make it explicit in the tree. */
6760 static tree
6761 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6762 bool definition, bool need_debug, unsigned int align)
6764 tree unit_align = size_int (align / BITS_PER_UNIT);
6765 return
6766 size_binop (MULT_EXPR,
6767 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6768 gnu_expr,
6769 unit_align),
6770 gnat_entity, s, definition,
6771 need_debug),
6772 unit_align);
6775 /* Structure to hold internal data for elaborate_reference. */
6777 struct er_data
6779 Entity_Id entity;
6780 bool definition;
6781 unsigned int n;
6784 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6786 static tree
6787 elaborate_reference_1 (tree ref, void *data)
6789 struct er_data *er = (struct er_data *)data;
6790 char suffix[16];
6792 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6793 if (TREE_CONSTANT (ref))
6794 return ref;
6796 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6797 pointer. This may be more efficient, but will also allow us to more
6798 easily find the match for the PLACEHOLDER_EXPR. */
6799 if (TREE_CODE (ref) == COMPONENT_REF
6800 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6801 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6802 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6803 TREE_OPERAND (ref, 1), NULL_TREE);
6805 sprintf (suffix, "EXP%d", ++er->n);
6806 return
6807 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6810 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6811 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6812 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6814 static tree
6815 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6816 tree *init)
6818 struct er_data er = { gnat_entity, definition, 0 };
6819 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6822 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6823 the value passed against the list of choices. */
6825 tree
6826 choices_to_gnu (tree operand, Node_Id choices)
6828 Node_Id choice;
6829 Node_Id gnat_temp;
6830 tree result = boolean_false_node;
6831 tree this_test, low = 0, high = 0, single = 0;
6833 for (choice = First (choices); Present (choice); choice = Next (choice))
6835 switch (Nkind (choice))
6837 case N_Range:
6838 low = gnat_to_gnu (Low_Bound (choice));
6839 high = gnat_to_gnu (High_Bound (choice));
6841 this_test
6842 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6843 build_binary_op (GE_EXPR, boolean_type_node,
6844 operand, low),
6845 build_binary_op (LE_EXPR, boolean_type_node,
6846 operand, high));
6848 break;
6850 case N_Subtype_Indication:
6851 gnat_temp = Range_Expression (Constraint (choice));
6852 low = gnat_to_gnu (Low_Bound (gnat_temp));
6853 high = gnat_to_gnu (High_Bound (gnat_temp));
6855 this_test
6856 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6857 build_binary_op (GE_EXPR, boolean_type_node,
6858 operand, low),
6859 build_binary_op (LE_EXPR, boolean_type_node,
6860 operand, high));
6861 break;
6863 case N_Identifier:
6864 case N_Expanded_Name:
6865 /* This represents either a subtype range, an enumeration
6866 literal, or a constant Ekind says which. If an enumeration
6867 literal or constant, fall through to the next case. */
6868 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6869 && Ekind (Entity (choice)) != E_Constant)
6871 tree type = gnat_to_gnu_type (Entity (choice));
6873 low = TYPE_MIN_VALUE (type);
6874 high = TYPE_MAX_VALUE (type);
6876 this_test
6877 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6878 build_binary_op (GE_EXPR, boolean_type_node,
6879 operand, low),
6880 build_binary_op (LE_EXPR, boolean_type_node,
6881 operand, high));
6882 break;
6885 /* fall through */
6887 case N_Character_Literal:
6888 case N_Integer_Literal:
6889 single = gnat_to_gnu (choice);
6890 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6891 single);
6892 break;
6894 case N_Others_Choice:
6895 this_test = boolean_true_node;
6896 break;
6898 default:
6899 gcc_unreachable ();
6902 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6903 this_test);
6906 return result;
6909 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6910 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6912 static int
6913 adjust_packed (tree field_type, tree record_type, int packed)
6915 /* If the field contains an item of variable size, we cannot pack it
6916 because we cannot create temporaries of non-fixed size in case
6917 we need to take the address of the field. See addressable_p and
6918 the notes on the addressability issues for further details. */
6919 if (type_has_variable_size (field_type))
6920 return 0;
6922 /* In the other cases, we can honor the packing. */
6923 if (packed)
6924 return packed;
6926 /* If the alignment of the record is specified and the field type
6927 is over-aligned, request Storage_Unit alignment for the field. */
6928 if (TYPE_ALIGN (record_type)
6929 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6930 return -1;
6932 /* Likewise if the maximum alignment of the record is specified. */
6933 if (TYPE_MAX_ALIGN (record_type)
6934 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6935 return -1;
6937 return 0;
6940 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6941 placed in GNU_RECORD_TYPE.
6943 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6944 record has Component_Alignment of Storage_Unit.
6946 DEFINITION is true if this field is for a record being defined.
6948 DEBUG_INFO_P is true if we need to write debug information for types
6949 that we may create in the process. */
6951 static tree
6952 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6953 bool definition, bool debug_info_p)
6955 const Entity_Id gnat_field_type = Etype (gnat_field);
6956 const bool is_aliased
6957 = Is_Aliased (gnat_field);
6958 const bool is_atomic
6959 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6960 const bool is_independent
6961 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6962 const bool is_volatile
6963 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6964 const bool needs_strict_alignment
6965 = (is_aliased
6966 || is_independent
6967 || is_volatile
6968 || Strict_Alignment (gnat_field_type));
6969 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6970 tree gnu_field_id = get_entity_name (gnat_field);
6971 tree gnu_field, gnu_size, gnu_pos;
6973 /* If this field requires strict alignment, we cannot pack it because
6974 it would very likely be under-aligned in the record. */
6975 if (needs_strict_alignment)
6976 packed = 0;
6977 else
6978 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6980 /* If a size is specified, use it. Otherwise, if the record type is packed,
6981 use the official RM size. See "Handling of Type'Size Values" in Einfo
6982 for further details. */
6983 if (Known_Esize (gnat_field))
6984 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6985 gnat_field, FIELD_DECL, false, true);
6986 else if (packed == 1)
6987 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6988 gnat_field, FIELD_DECL, false, true);
6989 else
6990 gnu_size = NULL_TREE;
6992 /* If we have a specified size that is smaller than that of the field's type,
6993 or a position is specified, and the field's type is a record that doesn't
6994 require strict alignment, see if we can get either an integral mode form
6995 of the type or a smaller form. If we can, show a size was specified for
6996 the field if there wasn't one already, so we know to make this a bitfield
6997 and avoid making things wider.
6999 Changing to an integral mode form is useful when the record is packed as
7000 we can then place the field at a non-byte-aligned position and so achieve
7001 tighter packing. This is in addition required if the field shares a byte
7002 with another field and the front-end lets the back-end handle the access
7003 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
7005 Changing to a smaller form is required if the specified size is smaller
7006 than that of the field's type and the type contains sub-fields that are
7007 padded, in order to avoid generating accesses to these sub-fields that
7008 are wider than the field.
7010 We avoid the transformation if it is not required or potentially useful,
7011 as it might entail an increase of the field's alignment and have ripple
7012 effects on the outer record type. A typical case is a field known to be
7013 byte-aligned and not to share a byte with another field. */
7014 if (!needs_strict_alignment
7015 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
7016 && !TYPE_FAT_POINTER_P (gnu_field_type)
7017 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
7018 && (packed == 1
7019 || (gnu_size
7020 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
7021 || (Present (Component_Clause (gnat_field))
7022 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
7023 % BITS_PER_UNIT == 0
7024 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
7026 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
7027 if (gnu_packable_type != gnu_field_type)
7029 gnu_field_type = gnu_packable_type;
7030 if (!gnu_size)
7031 gnu_size = rm_size (gnu_field_type);
7035 if (Is_Atomic_Or_VFA (gnat_field))
7036 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
7038 if (Present (Component_Clause (gnat_field)))
7040 Node_Id gnat_clause = Component_Clause (gnat_field);
7041 Entity_Id gnat_parent
7042 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
7044 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
7045 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
7046 gnat_field, FIELD_DECL, false, true);
7048 /* Ensure the position does not overlap with the parent subtype, if there
7049 is one. This test is omitted if the parent of the tagged type has a
7050 full rep clause since, in this case, component clauses are allowed to
7051 overlay the space allocated for the parent type and the front-end has
7052 checked that there are no overlapping components. */
7053 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
7055 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
7057 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
7058 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
7059 post_error_ne_tree
7060 ("offset of& must be beyond parent{, minimum allowed is ^}",
7061 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
7064 /* If this field needs strict alignment, make sure that the record is
7065 sufficiently aligned and that the position and size are consistent
7066 with the type. But don't do it if we are just annotating types and
7067 the field's type is tagged, since tagged types aren't fully laid out
7068 in this mode. Also, note that atomic implies volatile so the inner
7069 test sequences ordering is significant here. */
7070 if (needs_strict_alignment
7071 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
7073 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
7075 if (TYPE_ALIGN (gnu_record_type) < type_align)
7076 SET_TYPE_ALIGN (gnu_record_type, type_align);
7078 /* If the position is not a multiple of the alignment of the type,
7079 then error out and reset the position. */
7080 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
7081 bitsize_int (type_align))))
7083 const char *s;
7085 if (is_atomic)
7086 s = "position of atomic field& must be multiple of ^ bits";
7087 else if (is_aliased)
7088 s = "position of aliased field& must be multiple of ^ bits";
7089 else if (is_independent)
7090 s = "position of independent field& must be multiple of ^ bits";
7091 else if (is_volatile)
7092 s = "position of volatile field& must be multiple of ^ bits";
7093 else if (Strict_Alignment (gnat_field_type))
7094 s = "position of & with aliased or tagged part must be"
7095 " multiple of ^ bits";
7096 else
7097 gcc_unreachable ();
7099 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
7100 type_align);
7101 gnu_pos = NULL_TREE;
7104 if (gnu_size)
7106 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
7107 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
7109 /* If the size is lower than that of the type, or greater for
7110 atomic and aliased, then error out and reset the size. */
7111 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
7113 const char *s;
7115 if (is_atomic)
7116 s = "size of atomic field& must be ^ bits";
7117 else if (is_aliased)
7118 s = "size of aliased field& must be ^ bits";
7119 else if (is_independent)
7120 s = "size of independent field& must be at least ^ bits";
7121 else if (is_volatile)
7122 s = "size of volatile field& must be at least ^ bits";
7123 else if (Strict_Alignment (gnat_field_type))
7124 s = "size of & with aliased or tagged part must be"
7125 " at least ^ bits";
7126 else
7127 gcc_unreachable ();
7129 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
7130 gnu_type_size);
7131 gnu_size = NULL_TREE;
7134 /* Likewise if the size is not a multiple of a byte, */
7135 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
7136 bitsize_unit_node)))
7138 const char *s;
7140 if (is_independent)
7141 s = "size of independent field& must be multiple of"
7142 " Storage_Unit";
7143 else if (is_volatile)
7144 s = "size of volatile field& must be multiple of"
7145 " Storage_Unit";
7146 else if (Strict_Alignment (gnat_field_type))
7147 s = "size of & with aliased or tagged part must be"
7148 " multiple of Storage_Unit";
7149 else
7150 gcc_unreachable ();
7152 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
7153 gnu_size = NULL_TREE;
7159 /* If the record has rep clauses and this is the tag field, make a rep
7160 clause for it as well. */
7161 else if (Has_Specified_Layout (Scope (gnat_field))
7162 && Chars (gnat_field) == Name_uTag)
7164 gnu_pos = bitsize_zero_node;
7165 gnu_size = TYPE_SIZE (gnu_field_type);
7168 else
7170 gnu_pos = NULL_TREE;
7172 /* If we are packing the record and the field is BLKmode, round the
7173 size up to a byte boundary. */
7174 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7175 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7178 /* We need to make the size the maximum for the type if it is
7179 self-referential and an unconstrained type. In that case, we can't
7180 pack the field since we can't make a copy to align it. */
7181 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7182 && !gnu_size
7183 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7184 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7186 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7187 packed = 0;
7190 /* If a size is specified, adjust the field's type to it. */
7191 if (gnu_size)
7193 tree orig_field_type;
7195 /* If the field's type is justified modular, we would need to remove
7196 the wrapper to (better) meet the layout requirements. However we
7197 can do so only if the field is not aliased to preserve the unique
7198 layout and if the prescribed size is not greater than that of the
7199 packed array to preserve the justification. */
7200 if (!needs_strict_alignment
7201 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7202 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7203 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7204 <= 0)
7205 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7207 /* Similarly if the field's type is a misaligned integral type, but
7208 there is no restriction on the size as there is no justification. */
7209 if (!needs_strict_alignment
7210 && TYPE_IS_PADDING_P (gnu_field_type)
7211 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7212 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7214 gnu_field_type
7215 = make_type_from_size (gnu_field_type, gnu_size,
7216 Has_Biased_Representation (gnat_field));
7218 orig_field_type = gnu_field_type;
7219 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7220 false, false, definition, true);
7222 /* If a padding record was made, declare it now since it will never be
7223 declared otherwise. This is necessary to ensure that its subtrees
7224 are properly marked. */
7225 if (gnu_field_type != orig_field_type
7226 && !DECL_P (TYPE_NAME (gnu_field_type)))
7227 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7228 debug_info_p, gnat_field);
7231 /* Otherwise (or if there was an error), don't specify a position. */
7232 else
7233 gnu_pos = NULL_TREE;
7235 /* If the field's type is a padded type made for a scalar field of a record
7236 type with reverse storage order, we need to propagate the reverse storage
7237 order to the padding type since it is the innermost enclosing aggregate
7238 type around the scalar. */
7239 if (TYPE_IS_PADDING_P (gnu_field_type)
7240 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7241 && Is_Scalar_Type (gnat_field_type))
7242 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7244 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7245 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7247 /* Now create the decl for the field. */
7248 gnu_field
7249 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7250 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
7251 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7252 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
7253 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7255 if (Ekind (gnat_field) == E_Discriminant)
7257 DECL_INVARIANT_P (gnu_field)
7258 = No (Discriminant_Default_Value (gnat_field));
7259 DECL_DISCRIMINANT_NUMBER (gnu_field)
7260 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7263 return gnu_field;
7266 /* Return true if at least one member of COMPONENT_LIST needs strict
7267 alignment. */
7269 static bool
7270 components_need_strict_alignment (Node_Id component_list)
7272 Node_Id component_decl;
7274 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7275 Present (component_decl);
7276 component_decl = Next_Non_Pragma (component_decl))
7278 Entity_Id gnat_field = Defining_Entity (component_decl);
7280 if (Is_Aliased (gnat_field))
7281 return true;
7283 if (Strict_Alignment (Etype (gnat_field)))
7284 return true;
7287 return false;
7290 /* Return true if TYPE is a type with variable size or a padding type with a
7291 field of variable size or a record that has a field with such a type. */
7293 static bool
7294 type_has_variable_size (tree type)
7296 tree field;
7298 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7299 return true;
7301 if (TYPE_IS_PADDING_P (type)
7302 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7303 return true;
7305 if (!RECORD_OR_UNION_TYPE_P (type))
7306 return false;
7308 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7309 if (type_has_variable_size (TREE_TYPE (field)))
7310 return true;
7312 return false;
7315 /* Return true if FIELD is an artificial field. */
7317 static bool
7318 field_is_artificial (tree field)
7320 /* These fields are generated by the front-end proper. */
7321 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7322 return true;
7324 /* These fields are generated by gigi. */
7325 if (DECL_INTERNAL_P (field))
7326 return true;
7328 return false;
7331 /* Return true if FIELD is a non-artificial aliased field. */
7333 static bool
7334 field_is_aliased (tree field)
7336 if (field_is_artificial (field))
7337 return false;
7339 return DECL_ALIASED_P (field);
7342 /* Return true if FIELD is a non-artificial field with self-referential
7343 size. */
7345 static bool
7346 field_has_self_size (tree field)
7348 if (field_is_artificial (field))
7349 return false;
7351 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7352 return false;
7354 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7357 /* Return true if FIELD is a non-artificial field with variable size. */
7359 static bool
7360 field_has_variable_size (tree field)
7362 if (field_is_artificial (field))
7363 return false;
7365 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7366 return false;
7368 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7371 /* qsort comparer for the bit positions of two record components. */
7373 static int
7374 compare_field_bitpos (const PTR rt1, const PTR rt2)
7376 const_tree const field1 = * (const_tree const *) rt1;
7377 const_tree const field2 = * (const_tree const *) rt2;
7378 const int ret
7379 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7381 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7384 /* Structure holding information for a given variant. */
7385 typedef struct vinfo
7387 /* The record type of the variant. */
7388 tree type;
7390 /* The name of the variant. */
7391 tree name;
7393 /* The qualifier of the variant. */
7394 tree qual;
7396 /* Whether the variant has a rep clause. */
7397 bool has_rep;
7399 /* Whether the variant is packed. */
7400 bool packed;
7402 } vinfo_t;
7404 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
7405 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
7406 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
7407 When called from gnat_to_gnu_entity during the processing of a record type
7408 definition, the GCC node for the parent, if any, will be the single field
7409 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7410 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7411 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7413 PACKED is 1 if this is for a packed record or -1 if this is for a record
7414 with Component_Alignment of Storage_Unit.
7416 DEFINITION is true if we are defining this record type.
7418 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7419 out the record. This means the alignment only serves to force fields to
7420 be bitfields, but not to require the record to be that aligned. This is
7421 used for variants.
7423 ALL_REP is true if a rep clause is present for all the fields.
7425 UNCHECKED_UNION is true if we are building this type for a record with a
7426 Pragma Unchecked_Union.
7428 ARTIFICIAL is true if this is a type that was generated by the compiler.
7430 DEBUG_INFO is true if we need to write debug information about the type.
7432 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7433 mean that its contents may be unused as well, only the container itself.
7435 REORDER is true if we are permitted to reorder components of this type.
7437 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7438 the outer record type down to this variant level. It is nonzero only if
7439 all the fields down to this level have a rep clause and ALL_REP is false.
7441 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7442 with a rep clause is to be added; in this case, that is all that should
7443 be done with such fields and the return value will be false. */
7445 static bool
7446 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7447 tree gnu_field_list, int packed, bool definition,
7448 bool cancel_alignment, bool all_rep,
7449 bool unchecked_union, bool artificial,
7450 bool debug_info, bool maybe_unused, bool reorder,
7451 tree first_free_pos, tree *p_gnu_rep_list)
7453 const bool needs_xv_encodings
7454 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7455 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7456 bool variants_have_rep = all_rep;
7457 bool layout_with_rep = false;
7458 bool has_self_field = false;
7459 bool has_aliased_after_self_field = false;
7460 Node_Id component_decl, variant_part;
7461 tree gnu_field, gnu_next, gnu_last;
7462 tree gnu_variant_part = NULL_TREE;
7463 tree gnu_rep_list = NULL_TREE;
7464 tree gnu_var_list = NULL_TREE;
7465 tree gnu_self_list = NULL_TREE;
7466 tree gnu_zero_list = NULL_TREE;
7468 /* For each component referenced in a component declaration create a GCC
7469 field and add it to the list, skipping pragmas in the GNAT list. */
7470 gnu_last = tree_last (gnu_field_list);
7471 if (Present (Component_Items (gnat_component_list)))
7472 for (component_decl
7473 = First_Non_Pragma (Component_Items (gnat_component_list));
7474 Present (component_decl);
7475 component_decl = Next_Non_Pragma (component_decl))
7477 Entity_Id gnat_field = Defining_Entity (component_decl);
7478 Name_Id gnat_name = Chars (gnat_field);
7480 /* If present, the _Parent field must have been created as the single
7481 field of the record type. Put it before any other fields. */
7482 if (gnat_name == Name_uParent)
7484 gnu_field = TYPE_FIELDS (gnu_record_type);
7485 gnu_field_list = chainon (gnu_field_list, gnu_field);
7487 else
7489 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7490 definition, debug_info);
7492 /* If this is the _Tag field, put it before any other fields. */
7493 if (gnat_name == Name_uTag)
7494 gnu_field_list = chainon (gnu_field_list, gnu_field);
7496 /* If this is the _Controller field, put it before the other
7497 fields except for the _Tag or _Parent field. */
7498 else if (gnat_name == Name_uController && gnu_last)
7500 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7501 DECL_CHAIN (gnu_last) = gnu_field;
7504 /* If this is a regular field, put it after the other fields. */
7505 else
7507 DECL_CHAIN (gnu_field) = gnu_field_list;
7508 gnu_field_list = gnu_field;
7509 if (!gnu_last)
7510 gnu_last = gnu_field;
7512 /* And record information for the final layout. */
7513 if (field_has_self_size (gnu_field))
7514 has_self_field = true;
7515 else if (has_self_field && field_is_aliased (gnu_field))
7516 has_aliased_after_self_field = true;
7520 save_gnu_tree (gnat_field, gnu_field, false);
7523 /* At the end of the component list there may be a variant part. */
7524 variant_part = Variant_Part (gnat_component_list);
7526 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7527 mutually exclusive and should go in the same memory. To do this we need
7528 to treat each variant as a record whose elements are created from the
7529 component list for the variant. So here we create the records from the
7530 lists for the variants and put them all into the QUAL_UNION_TYPE.
7531 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7532 use GNU_RECORD_TYPE if there are no fields so far. */
7533 if (Present (variant_part))
7535 Node_Id gnat_discr = Name (variant_part), variant;
7536 tree gnu_discr = gnat_to_gnu (gnat_discr);
7537 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7538 tree gnu_var_name
7539 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7540 "XVN");
7541 tree gnu_union_type, gnu_union_name;
7542 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7543 bool union_field_needs_strict_alignment = false;
7544 auto_vec <vinfo_t, 16> variant_types;
7545 vinfo_t *gnu_variant;
7546 unsigned int variants_align = 0;
7547 unsigned int i;
7549 gnu_union_name
7550 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7552 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7553 are all in the variant part, to match the layout of C unions. There
7554 is an associated check below. */
7555 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7556 gnu_union_type = gnu_record_type;
7557 else
7559 gnu_union_type
7560 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7562 TYPE_NAME (gnu_union_type) = gnu_union_name;
7563 SET_TYPE_ALIGN (gnu_union_type, 0);
7564 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7565 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7566 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7569 /* If all the fields down to this level have a rep clause, find out
7570 whether all the fields at this level also have one. If so, then
7571 compute the new first free position to be passed downward. */
7572 this_first_free_pos = first_free_pos;
7573 if (this_first_free_pos)
7575 for (gnu_field = gnu_field_list;
7576 gnu_field;
7577 gnu_field = DECL_CHAIN (gnu_field))
7578 if (DECL_FIELD_OFFSET (gnu_field))
7580 tree pos = bit_position (gnu_field);
7581 if (!tree_int_cst_lt (pos, this_first_free_pos))
7582 this_first_free_pos
7583 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7585 else
7587 this_first_free_pos = NULL_TREE;
7588 break;
7592 /* We build the variants in two passes. The bulk of the work is done in
7593 the first pass, that is to say translating the GNAT nodes, building
7594 the container types and computing the associated properties. However
7595 we cannot finish up the container types during this pass because we
7596 don't know where the variant part will be placed until the end. */
7597 for (variant = First_Non_Pragma (Variants (variant_part));
7598 Present (variant);
7599 variant = Next_Non_Pragma (variant))
7601 tree gnu_variant_type = make_node (RECORD_TYPE);
7602 tree gnu_inner_name, gnu_qual;
7603 bool has_rep;
7604 int field_packed;
7605 vinfo_t vinfo;
7607 Get_Variant_Encoding (variant);
7608 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7609 TYPE_NAME (gnu_variant_type)
7610 = concat_name (gnu_union_name,
7611 IDENTIFIER_POINTER (gnu_inner_name));
7613 /* Set the alignment of the inner type in case we need to make
7614 inner objects into bitfields, but then clear it out so the
7615 record actually gets only the alignment required. */
7616 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7617 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7618 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7619 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7621 /* Similarly, if the outer record has a size specified and all
7622 the fields have a rep clause, we can propagate the size. */
7623 if (all_rep_and_size)
7625 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7626 TYPE_SIZE_UNIT (gnu_variant_type)
7627 = TYPE_SIZE_UNIT (gnu_record_type);
7630 /* Add the fields into the record type for the variant. Note that
7631 we aren't sure to really use it at this point, see below. */
7632 has_rep
7633 = components_to_record (gnu_variant_type, Component_List (variant),
7634 NULL_TREE, packed, definition,
7635 !all_rep_and_size, all_rep,
7636 unchecked_union,
7637 true, needs_xv_encodings, true, reorder,
7638 this_first_free_pos,
7639 all_rep || this_first_free_pos
7640 ? NULL : &gnu_rep_list);
7642 /* Translate the qualifier and annotate the GNAT node. */
7643 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7644 Set_Present_Expr (variant, annotate_value (gnu_qual));
7646 /* Deal with packedness like in gnat_to_gnu_field. */
7647 if (components_need_strict_alignment (Component_List (variant)))
7649 field_packed = 0;
7650 union_field_needs_strict_alignment = true;
7652 else
7653 field_packed
7654 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7656 /* Push this variant onto the stack for the second pass. */
7657 vinfo.type = gnu_variant_type;
7658 vinfo.name = gnu_inner_name;
7659 vinfo.qual = gnu_qual;
7660 vinfo.has_rep = has_rep;
7661 vinfo.packed = field_packed;
7662 variant_types.safe_push (vinfo);
7664 /* Compute the global properties that will determine the placement of
7665 the variant part. */
7666 variants_have_rep |= has_rep;
7667 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7668 variants_align = TYPE_ALIGN (gnu_variant_type);
7671 /* Round up the first free position to the alignment of the variant part
7672 for the variants without rep clause. This will guarantee a consistent
7673 layout independently of the placement of the variant part. */
7674 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7675 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7677 /* In the second pass, the container types are adjusted if necessary and
7678 finished up, then the corresponding fields of the variant part are
7679 built with their qualifier, unless this is an unchecked union. */
7680 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7682 tree gnu_variant_type = gnu_variant->type;
7683 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7685 /* If this is an Unchecked_Union whose fields are all in the variant
7686 part and we have a single field with no representation clause or
7687 placed at offset zero, use the field directly to match the layout
7688 of C unions. */
7689 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7690 && gnu_field_list
7691 && !DECL_CHAIN (gnu_field_list)
7692 && (!DECL_FIELD_OFFSET (gnu_field_list)
7693 || integer_zerop (bit_position (gnu_field_list))))
7695 gnu_field = gnu_field_list;
7696 DECL_CONTEXT (gnu_field) = gnu_record_type;
7698 else
7700 /* Finalize the variant type now. We used to throw away empty
7701 record types but we no longer do that because we need them to
7702 generate complete debug info for the variant; otherwise, the
7703 union type definition will be lacking the fields associated
7704 with these empty variants. */
7705 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7707 /* The variant part will be at offset 0 so we need to ensure
7708 that the fields are laid out starting from the first free
7709 position at this level. */
7710 tree gnu_rep_type = make_node (RECORD_TYPE);
7711 tree gnu_rep_part;
7712 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7713 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7714 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7715 gnu_rep_part
7716 = create_rep_part (gnu_rep_type, gnu_variant_type,
7717 this_first_free_pos);
7718 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7719 gnu_field_list = gnu_rep_part;
7720 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7721 false);
7724 if (debug_info)
7725 rest_of_record_type_compilation (gnu_variant_type);
7726 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7727 true, needs_xv_encodings, gnat_component_list);
7729 gnu_field
7730 = create_field_decl (gnu_variant->name, gnu_variant_type,
7731 gnu_union_type,
7732 all_rep_and_size
7733 ? TYPE_SIZE (gnu_variant_type) : 0,
7734 variants_have_rep ? bitsize_zero_node : 0,
7735 gnu_variant->packed, 0);
7737 DECL_INTERNAL_P (gnu_field) = 1;
7739 if (!unchecked_union)
7740 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7743 DECL_CHAIN (gnu_field) = gnu_variant_list;
7744 gnu_variant_list = gnu_field;
7747 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7748 if (gnu_variant_list)
7750 int union_field_packed;
7752 if (all_rep_and_size)
7754 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7755 TYPE_SIZE_UNIT (gnu_union_type)
7756 = TYPE_SIZE_UNIT (gnu_record_type);
7759 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7760 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7762 /* If GNU_UNION_TYPE is our record type, it means we must have an
7763 Unchecked_Union with no fields. Verify that and, if so, just
7764 return. */
7765 if (gnu_union_type == gnu_record_type)
7767 gcc_assert (unchecked_union
7768 && !gnu_field_list
7769 && !gnu_rep_list);
7770 return variants_have_rep;
7773 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7774 needs_xv_encodings, gnat_component_list);
7776 /* Deal with packedness like in gnat_to_gnu_field. */
7777 if (union_field_needs_strict_alignment)
7778 union_field_packed = 0;
7779 else
7780 union_field_packed
7781 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7783 gnu_variant_part
7784 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7785 all_rep_and_size
7786 ? TYPE_SIZE (gnu_union_type) : 0,
7787 variants_have_rep ? bitsize_zero_node : 0,
7788 union_field_packed, 0);
7790 DECL_INTERNAL_P (gnu_variant_part) = 1;
7794 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7795 permitted to reorder components, self-referential sizes or variable sizes.
7796 If they do, pull them out and put them onto the appropriate list. We have
7797 to do this in a separate pass since we want to handle the discriminants
7798 but can't play with them until we've used them in debugging data above.
7800 Similarly, pull out the fields with zero size and no rep clause, as they
7801 would otherwise modify the layout and thus very likely run afoul of the
7802 Ada semantics, which are different from those of C here.
7804 ??? If we reorder them, debugging information will be wrong but there is
7805 nothing that can be done about this at the moment. */
7806 gnu_last = NULL_TREE;
7808 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7809 do { \
7810 if (gnu_last) \
7811 DECL_CHAIN (gnu_last) = gnu_next; \
7812 else \
7813 gnu_field_list = gnu_next; \
7815 DECL_CHAIN (gnu_field) = (LIST); \
7816 (LIST) = gnu_field; \
7817 } while (0)
7819 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7821 gnu_next = DECL_CHAIN (gnu_field);
7823 if (DECL_FIELD_OFFSET (gnu_field))
7825 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7826 continue;
7829 if ((reorder || has_aliased_after_self_field)
7830 && field_has_self_size (gnu_field))
7832 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7833 continue;
7836 if (reorder && field_has_variable_size (gnu_field))
7838 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7839 continue;
7842 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7844 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7845 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7846 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7847 if (field_is_aliased (gnu_field))
7848 SET_TYPE_ALIGN (gnu_record_type,
7849 MAX (TYPE_ALIGN (gnu_record_type),
7850 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7851 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7852 continue;
7855 gnu_last = gnu_field;
7858 #undef MOVE_FROM_FIELD_LIST_TO
7860 gnu_field_list = nreverse (gnu_field_list);
7862 /* If permitted, we reorder the fields as follows:
7864 1) all fixed length fields,
7865 2) all fields whose length doesn't depend on discriminants,
7866 3) all fields whose length depends on discriminants,
7867 4) the variant part,
7869 within the record and within each variant recursively. */
7870 if (reorder)
7871 gnu_field_list
7872 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7874 /* Otherwise, if there is an aliased field placed after a field whose length
7875 depends on discriminants, we put all the fields of the latter sort, last.
7876 We need to do this in case an object of this record type is mutable. */
7877 else if (has_aliased_after_self_field)
7878 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7880 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7881 in our REP list to the previous level because this level needs them in
7882 order to do a correct layout, i.e. avoid having overlapping fields. */
7883 if (p_gnu_rep_list && gnu_rep_list)
7884 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7886 /* Deal with the annoying case of an extension of a record with variable size
7887 and partial rep clause, for which the _Parent field is forced at offset 0
7888 and has variable size, which we do not support below. Note that we cannot
7889 do it if the field has fixed size because we rely on the presence of the
7890 REP part built below to trigger the reordering of the fields in a derived
7891 record type when all the fields have a fixed position. */
7892 else if (gnu_rep_list
7893 && !DECL_CHAIN (gnu_rep_list)
7894 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7895 && !variants_have_rep
7896 && first_free_pos
7897 && integer_zerop (first_free_pos)
7898 && integer_zerop (bit_position (gnu_rep_list)))
7900 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7901 gnu_field_list = gnu_rep_list;
7902 gnu_rep_list = NULL_TREE;
7905 /* Otherwise, sort the fields by bit position and put them into their own
7906 record, before the others, if we also have fields without rep clause. */
7907 else if (gnu_rep_list)
7909 tree gnu_rep_type, gnu_rep_part;
7910 int i, len = list_length (gnu_rep_list);
7911 tree *gnu_arr = XALLOCAVEC (tree, len);
7913 /* If all the fields have a rep clause, we can do a flat layout. */
7914 layout_with_rep = !gnu_field_list
7915 && (!gnu_variant_part || variants_have_rep);
7916 gnu_rep_type
7917 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7919 for (gnu_field = gnu_rep_list, i = 0;
7920 gnu_field;
7921 gnu_field = DECL_CHAIN (gnu_field), i++)
7922 gnu_arr[i] = gnu_field;
7924 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7926 /* Put the fields in the list in order of increasing position, which
7927 means we start from the end. */
7928 gnu_rep_list = NULL_TREE;
7929 for (i = len - 1; i >= 0; i--)
7931 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7932 gnu_rep_list = gnu_arr[i];
7933 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7936 if (layout_with_rep)
7937 gnu_field_list = gnu_rep_list;
7938 else
7940 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7941 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7942 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7944 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7945 without rep clause are laid out starting from this position.
7946 Therefore, we force it as a minimal size on the REP part. */
7947 gnu_rep_part
7948 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7950 /* Chain the REP part at the beginning of the field list. */
7951 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7952 gnu_field_list = gnu_rep_part;
7956 /* Chain the variant part at the end of the field list. */
7957 if (gnu_variant_part)
7958 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7960 if (cancel_alignment)
7961 SET_TYPE_ALIGN (gnu_record_type, 0);
7963 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7965 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7966 debug_info && !maybe_unused);
7968 /* Chain the fields with zero size at the beginning of the field list. */
7969 if (gnu_zero_list)
7970 TYPE_FIELDS (gnu_record_type)
7971 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7973 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7976 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7977 placed into an Esize, Component_Bit_Offset, or Component_Size value
7978 in the GNAT tree. */
7980 static Uint
7981 annotate_value (tree gnu_size)
7983 TCode tcode;
7984 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7985 struct tree_int_map in;
7986 int i;
7988 /* See if we've already saved the value for this node. */
7989 if (EXPR_P (gnu_size))
7991 struct tree_int_map *e;
7993 in.base.from = gnu_size;
7994 e = annotate_value_cache->find (&in);
7996 if (e)
7997 return (Node_Ref_Or_Val) e->to;
7999 else
8000 in.base.from = NULL_TREE;
8002 /* If we do not return inside this switch, TCODE will be set to the
8003 code to use for a Create_Node operand and LEN (set above) will be
8004 the number of recursive calls for us to make. */
8006 switch (TREE_CODE (gnu_size))
8008 case INTEGER_CST:
8009 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8011 case COMPONENT_REF:
8012 /* The only case we handle here is a simple discriminant reference. */
8013 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8015 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
8017 /* Climb up the chain of successive extensions, if any. */
8018 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
8019 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
8020 == parent_name_id)
8021 gnu_size = TREE_OPERAND (gnu_size, 0);
8023 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
8024 return
8025 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
8028 return No_Uint;
8030 CASE_CONVERT: case NON_LVALUE_EXPR:
8031 return annotate_value (TREE_OPERAND (gnu_size, 0));
8033 /* Now just list the operations we handle. */
8034 case COND_EXPR: tcode = Cond_Expr; break;
8035 case PLUS_EXPR: tcode = Plus_Expr; break;
8036 case MINUS_EXPR: tcode = Minus_Expr; break;
8037 case MULT_EXPR: tcode = Mult_Expr; break;
8038 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8039 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8040 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8041 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8042 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8043 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8044 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8045 case NEGATE_EXPR: tcode = Negate_Expr; break;
8046 case MIN_EXPR: tcode = Min_Expr; break;
8047 case MAX_EXPR: tcode = Max_Expr; break;
8048 case ABS_EXPR: tcode = Abs_Expr; break;
8049 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
8050 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
8051 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8052 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8053 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8054 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8055 case LT_EXPR: tcode = Lt_Expr; break;
8056 case LE_EXPR: tcode = Le_Expr; break;
8057 case GT_EXPR: tcode = Gt_Expr; break;
8058 case GE_EXPR: tcode = Ge_Expr; break;
8059 case EQ_EXPR: tcode = Eq_Expr; break;
8060 case NE_EXPR: tcode = Ne_Expr; break;
8062 case BIT_AND_EXPR:
8063 tcode = Bit_And_Expr;
8064 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8065 Such values appear in expressions with aligning patterns. Note that,
8066 since sizetype is unsigned, we have to jump through some hoops. */
8067 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8069 tree op1 = TREE_OPERAND (gnu_size, 1);
8070 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
8071 if (wi::neg_p (signed_op1))
8073 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
8074 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8077 break;
8079 case CALL_EXPR:
8080 /* In regular mode, inline back only if symbolic annotation is requested
8081 in order to avoid memory explosion on big discriminated record types.
8082 But not in ASIS mode, as symbolic annotation is required for DDA. */
8083 if (List_Representation_Info == 3 || type_annotate_only)
8085 tree t = maybe_inline_call_in_expr (gnu_size);
8086 if (t)
8087 return annotate_value (t);
8089 else
8090 return Uint_Minus_1;
8092 /* fall through */
8094 default:
8095 return No_Uint;
8098 /* Now get each of the operands that's relevant for this code. If any
8099 cannot be expressed as a repinfo node, say we can't. */
8100 for (i = 0; i < 3; i++)
8101 ops[i] = No_Uint;
8103 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8105 if (i == 1 && pre_op1 != No_Uint)
8106 ops[i] = pre_op1;
8107 else
8108 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8109 if (ops[i] == No_Uint)
8110 return No_Uint;
8113 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8115 /* Save the result in the cache. */
8116 if (in.base.from)
8118 struct tree_int_map **h;
8119 /* We can't assume the hash table data hasn't moved since the initial
8120 look up, so we have to search again. Allocating and inserting an
8121 entry at that point would be an alternative, but then we'd better
8122 discard the entry if we decided not to cache it. */
8123 h = annotate_value_cache->find_slot (&in, INSERT);
8124 gcc_assert (!*h);
8125 *h = ggc_alloc<tree_int_map> ();
8126 (*h)->base.from = gnu_size;
8127 (*h)->to = ret;
8130 return ret;
8133 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8134 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8135 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8136 BY_REF is true if the object is used by reference. */
8138 void
8139 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8141 if (by_ref)
8143 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8144 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8145 else
8146 gnu_type = TREE_TYPE (gnu_type);
8149 if (Unknown_Esize (gnat_entity))
8151 if (TREE_CODE (gnu_type) == RECORD_TYPE
8152 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8153 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8154 else if (!size)
8155 size = TYPE_SIZE (gnu_type);
8157 if (size)
8158 Set_Esize (gnat_entity, annotate_value (size));
8161 if (Unknown_Alignment (gnat_entity))
8162 Set_Alignment (gnat_entity,
8163 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8166 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8167 Return NULL_TREE if there is no such element in the list. */
8169 static tree
8170 purpose_member_field (const_tree elem, tree list)
8172 while (list)
8174 tree field = TREE_PURPOSE (list);
8175 if (SAME_FIELD_P (field, elem))
8176 return list;
8177 list = TREE_CHAIN (list);
8179 return NULL_TREE;
8182 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8183 set Component_Bit_Offset and Esize of the components to the position and
8184 size used by Gigi. */
8186 static void
8187 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8189 Entity_Id gnat_field;
8190 tree gnu_list;
8192 /* We operate by first making a list of all fields and their position (we
8193 can get the size easily) and then update all the sizes in the tree. */
8194 gnu_list
8195 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8196 BIGGEST_ALIGNMENT, NULL_TREE);
8198 for (gnat_field = First_Entity (gnat_entity);
8199 Present (gnat_field);
8200 gnat_field = Next_Entity (gnat_field))
8201 if (Ekind (gnat_field) == E_Component
8202 || (Ekind (gnat_field) == E_Discriminant
8203 && !Is_Unchecked_Union (Scope (gnat_field))))
8205 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8206 gnu_list);
8207 if (t)
8209 tree parent_offset;
8211 /* If we are just annotating types and the type is tagged, the tag
8212 and the parent components are not generated by the front-end so
8213 we need to add the appropriate offset to each component without
8214 representation clause. */
8215 if (type_annotate_only
8216 && Is_Tagged_Type (gnat_entity)
8217 && No (Component_Clause (gnat_field)))
8219 /* For a component appearing in the current extension, the
8220 offset is the size of the parent. */
8221 if (Is_Derived_Type (gnat_entity)
8222 && Original_Record_Component (gnat_field) == gnat_field)
8223 parent_offset
8224 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8225 bitsizetype);
8226 else
8227 parent_offset = bitsize_int (POINTER_SIZE);
8229 if (TYPE_FIELDS (gnu_type))
8230 parent_offset
8231 = round_up (parent_offset,
8232 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8234 else
8235 parent_offset = bitsize_zero_node;
8237 Set_Component_Bit_Offset
8238 (gnat_field,
8239 annotate_value
8240 (size_binop (PLUS_EXPR,
8241 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
8242 TREE_VEC_ELT (TREE_VALUE (t), 2)),
8243 parent_offset)));
8245 Set_Esize (gnat_field,
8246 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8248 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
8250 /* If there is no entry, this is an inherited component whose
8251 position is the same as in the parent type. */
8252 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
8254 /* If we are just annotating types, discriminants renaming those of
8255 the parent have no entry so deal with them specifically. */
8256 if (type_annotate_only
8257 && gnat_orig_field == gnat_field
8258 && Ekind (gnat_field) == E_Discriminant)
8259 gnat_orig_field = Corresponding_Discriminant (gnat_field);
8261 Set_Component_Bit_Offset (gnat_field,
8262 Component_Bit_Offset (gnat_orig_field));
8264 Set_Esize (gnat_field, Esize (gnat_orig_field));
8269 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8270 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8271 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8272 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8273 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8274 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8275 pre-existing list to be chained to the newly created entries. */
8277 static tree
8278 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8279 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8281 tree gnu_field;
8283 for (gnu_field = TYPE_FIELDS (gnu_type);
8284 gnu_field;
8285 gnu_field = DECL_CHAIN (gnu_field))
8287 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8288 DECL_FIELD_BIT_OFFSET (gnu_field));
8289 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8290 DECL_FIELD_OFFSET (gnu_field));
8291 unsigned int our_offset_align
8292 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8293 tree v = make_tree_vec (3);
8295 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8296 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8297 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8298 gnu_list = tree_cons (gnu_field, v, gnu_list);
8300 /* Recurse on internal fields, flattening the nested fields except for
8301 those in the variant part, if requested. */
8302 if (DECL_INTERNAL_P (gnu_field))
8304 tree gnu_field_type = TREE_TYPE (gnu_field);
8305 if (do_not_flatten_variant
8306 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8307 gnu_list
8308 = build_position_list (gnu_field_type, do_not_flatten_variant,
8309 size_zero_node, bitsize_zero_node,
8310 BIGGEST_ALIGNMENT, gnu_list);
8311 else
8312 gnu_list
8313 = build_position_list (gnu_field_type, do_not_flatten_variant,
8314 gnu_our_offset, gnu_our_bitpos,
8315 our_offset_align, gnu_list);
8319 return gnu_list;
8322 /* Return a list describing the substitutions needed to reflect the
8323 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8324 be in any order. The values in an element of the list are in the form
8325 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8326 a definition of GNAT_SUBTYPE. */
8328 static vec<subst_pair>
8329 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8331 vec<subst_pair> gnu_list = vNULL;
8332 Entity_Id gnat_discrim;
8333 Node_Id gnat_constr;
8335 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8336 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8337 Present (gnat_discrim);
8338 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8339 gnat_constr = Next_Elmt (gnat_constr))
8340 /* Ignore access discriminants. */
8341 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8343 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8344 tree replacement = convert (TREE_TYPE (gnu_field),
8345 elaborate_expression
8346 (Node (gnat_constr), gnat_subtype,
8347 get_entity_char (gnat_discrim),
8348 definition, true, false));
8349 subst_pair s = {gnu_field, replacement};
8350 gnu_list.safe_push (s);
8353 return gnu_list;
8356 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8357 variants of QUAL_UNION_TYPE that are still relevant after applying
8358 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8359 list to be prepended to the newly created entries. */
8361 static vec<variant_desc>
8362 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8363 vec<variant_desc> gnu_list)
8365 tree gnu_field;
8367 for (gnu_field = TYPE_FIELDS (qual_union_type);
8368 gnu_field;
8369 gnu_field = DECL_CHAIN (gnu_field))
8371 tree qual = DECL_QUALIFIER (gnu_field);
8372 unsigned int i;
8373 subst_pair *s;
8375 FOR_EACH_VEC_ELT (subst_list, i, s)
8376 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8378 /* If the new qualifier is not unconditionally false, its variant may
8379 still be accessed. */
8380 if (!integer_zerop (qual))
8382 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8383 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
8385 gnu_list.safe_push (v);
8387 /* Recurse on the variant subpart of the variant, if any. */
8388 variant_subpart = get_variant_part (variant_type);
8389 if (variant_subpart)
8390 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8391 subst_list, gnu_list);
8393 /* If the new qualifier is unconditionally true, the subsequent
8394 variants cannot be accessed. */
8395 if (integer_onep (qual))
8396 break;
8400 return gnu_list;
8403 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8404 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8405 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8406 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8407 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8408 true if we are being called to process the Component_Size of GNAT_OBJECT;
8409 this is used only for error messages. ZERO_OK is true if a size of zero
8410 is permitted; if ZERO_OK is false, it means that a size of zero should be
8411 treated as an unspecified size. */
8413 static tree
8414 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8415 enum tree_code kind, bool component_p, bool zero_ok)
8417 Node_Id gnat_error_node;
8418 tree type_size, size;
8420 /* Return 0 if no size was specified. */
8421 if (uint_size == No_Uint)
8422 return NULL_TREE;
8424 /* Ignore a negative size since that corresponds to our back-annotation. */
8425 if (UI_Lt (uint_size, Uint_0))
8426 return NULL_TREE;
8428 /* Find the node to use for error messages. */
8429 if ((Ekind (gnat_object) == E_Component
8430 || Ekind (gnat_object) == E_Discriminant)
8431 && Present (Component_Clause (gnat_object)))
8432 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8433 else if (Present (Size_Clause (gnat_object)))
8434 gnat_error_node = Expression (Size_Clause (gnat_object));
8435 else
8436 gnat_error_node = gnat_object;
8438 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8439 but cannot be represented in bitsizetype. */
8440 size = UI_To_gnu (uint_size, bitsizetype);
8441 if (TREE_OVERFLOW (size))
8443 if (component_p)
8444 post_error_ne ("component size for& is too large", gnat_error_node,
8445 gnat_object);
8446 else
8447 post_error_ne ("size for& is too large", gnat_error_node,
8448 gnat_object);
8449 return NULL_TREE;
8452 /* Ignore a zero size if it is not permitted. */
8453 if (!zero_ok && integer_zerop (size))
8454 return NULL_TREE;
8456 /* The size of objects is always a multiple of a byte. */
8457 if (kind == VAR_DECL
8458 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8460 if (component_p)
8461 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8462 gnat_error_node, gnat_object);
8463 else
8464 post_error_ne ("size for& is not a multiple of Storage_Unit",
8465 gnat_error_node, gnat_object);
8466 return NULL_TREE;
8469 /* If this is an integral type or a packed array type, the front-end has
8470 already verified the size, so we need not do it here (which would mean
8471 checking against the bounds). However, if this is an aliased object,
8472 it may not be smaller than the type of the object. */
8473 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8474 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8475 return size;
8477 /* If the object is a record that contains a template, add the size of the
8478 template to the specified size. */
8479 if (TREE_CODE (gnu_type) == RECORD_TYPE
8480 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8481 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8483 if (kind == VAR_DECL
8484 /* If a type needs strict alignment, a component of this type in
8485 a packed record cannot be packed and thus uses the type size. */
8486 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8487 type_size = TYPE_SIZE (gnu_type);
8488 else
8489 type_size = rm_size (gnu_type);
8491 /* Modify the size of a discriminated type to be the maximum size. */
8492 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8493 type_size = max_size (type_size, true);
8495 /* If this is an access type or a fat pointer, the minimum size is that given
8496 by the smallest integral mode that's valid for pointers. */
8497 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8499 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8500 while (!targetm.valid_pointer_mode (p_mode))
8501 p_mode = GET_MODE_WIDER_MODE (p_mode);
8502 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8505 /* Issue an error either if the default size of the object isn't a constant
8506 or if the new size is smaller than it. */
8507 if (TREE_CODE (type_size) != INTEGER_CST
8508 || TREE_OVERFLOW (type_size)
8509 || tree_int_cst_lt (size, type_size))
8511 if (component_p)
8512 post_error_ne_tree
8513 ("component size for& too small{, minimum allowed is ^}",
8514 gnat_error_node, gnat_object, type_size);
8515 else
8516 post_error_ne_tree
8517 ("size for& too small{, minimum allowed is ^}",
8518 gnat_error_node, gnat_object, type_size);
8519 return NULL_TREE;
8522 return size;
8525 /* Similarly, but both validate and process a value of RM size. This routine
8526 is only called for types. */
8528 static void
8529 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8531 Node_Id gnat_attr_node;
8532 tree old_size, size;
8534 /* Do nothing if no size was specified. */
8535 if (uint_size == No_Uint)
8536 return;
8538 /* Ignore a negative size since that corresponds to our back-annotation. */
8539 if (UI_Lt (uint_size, Uint_0))
8540 return;
8542 /* Only issue an error if a Value_Size clause was explicitly given.
8543 Otherwise, we'd be duplicating an error on the Size clause. */
8544 gnat_attr_node
8545 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8547 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8548 but cannot be represented in bitsizetype. */
8549 size = UI_To_gnu (uint_size, bitsizetype);
8550 if (TREE_OVERFLOW (size))
8552 if (Present (gnat_attr_node))
8553 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8554 gnat_entity);
8555 return;
8558 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8559 exists, or this is an integer type, in which case the front-end will
8560 have always set it. */
8561 if (No (gnat_attr_node)
8562 && integer_zerop (size)
8563 && !Has_Size_Clause (gnat_entity)
8564 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8565 return;
8567 old_size = rm_size (gnu_type);
8569 /* If the old size is self-referential, get the maximum size. */
8570 if (CONTAINS_PLACEHOLDER_P (old_size))
8571 old_size = max_size (old_size, true);
8573 /* Issue an error either if the old size of the object isn't a constant or
8574 if the new size is smaller than it. The front-end has already verified
8575 this for scalar and packed array types. */
8576 if (TREE_CODE (old_size) != INTEGER_CST
8577 || TREE_OVERFLOW (old_size)
8578 || (AGGREGATE_TYPE_P (gnu_type)
8579 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8580 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8581 && !(TYPE_IS_PADDING_P (gnu_type)
8582 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8583 && TYPE_PACKED_ARRAY_TYPE_P
8584 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8585 && tree_int_cst_lt (size, old_size)))
8587 if (Present (gnat_attr_node))
8588 post_error_ne_tree
8589 ("Value_Size for& too small{, minimum allowed is ^}",
8590 gnat_attr_node, gnat_entity, old_size);
8591 return;
8594 /* Otherwise, set the RM size proper for integral types... */
8595 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8596 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8597 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8598 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8599 SET_TYPE_RM_SIZE (gnu_type, size);
8601 /* ...or the Ada size for record and union types. */
8602 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8603 && !TYPE_FAT_POINTER_P (gnu_type))
8604 SET_TYPE_ADA_SIZE (gnu_type, size);
8607 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8608 a type or object whose present alignment is ALIGN. If this alignment is
8609 valid, return it. Otherwise, give an error and return ALIGN. */
8611 static unsigned int
8612 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8614 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8615 unsigned int new_align;
8616 Node_Id gnat_error_node;
8618 /* Don't worry about checking alignment if alignment was not specified
8619 by the source program and we already posted an error for this entity. */
8620 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8621 return align;
8623 /* Post the error on the alignment clause if any. Note, for the implicit
8624 base type of an array type, the alignment clause is on the first
8625 subtype. */
8626 if (Present (Alignment_Clause (gnat_entity)))
8627 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8629 else if (Is_Itype (gnat_entity)
8630 && Is_Array_Type (gnat_entity)
8631 && Etype (gnat_entity) == gnat_entity
8632 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8633 gnat_error_node =
8634 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8636 else
8637 gnat_error_node = gnat_entity;
8639 /* Within GCC, an alignment is an integer, so we must make sure a value is
8640 specified that fits in that range. Also, there is an upper bound to
8641 alignments we can support/allow. */
8642 if (!UI_Is_In_Int_Range (alignment)
8643 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8644 post_error_ne_num ("largest supported alignment for& is ^",
8645 gnat_error_node, gnat_entity, max_allowed_alignment);
8646 else if (!(Present (Alignment_Clause (gnat_entity))
8647 && From_At_Mod (Alignment_Clause (gnat_entity)))
8648 && new_align * BITS_PER_UNIT < align)
8650 unsigned int double_align;
8651 bool is_capped_double, align_clause;
8653 /* If the default alignment of "double" or larger scalar types is
8654 specifically capped and the new alignment is above the cap, do
8655 not post an error and change the alignment only if there is an
8656 alignment clause; this makes it possible to have the associated
8657 GCC type overaligned by default for performance reasons. */
8658 if ((double_align = double_float_alignment) > 0)
8660 Entity_Id gnat_type
8661 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8662 is_capped_double
8663 = is_double_float_or_array (gnat_type, &align_clause);
8665 else if ((double_align = double_scalar_alignment) > 0)
8667 Entity_Id gnat_type
8668 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8669 is_capped_double
8670 = is_double_scalar_or_array (gnat_type, &align_clause);
8672 else
8673 is_capped_double = align_clause = false;
8675 if (is_capped_double && new_align >= double_align)
8677 if (align_clause)
8678 align = new_align * BITS_PER_UNIT;
8680 else
8682 if (is_capped_double)
8683 align = double_align * BITS_PER_UNIT;
8685 post_error_ne_num ("alignment for& must be at least ^",
8686 gnat_error_node, gnat_entity,
8687 align / BITS_PER_UNIT);
8690 else
8692 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8693 if (new_align > align)
8694 align = new_align;
8697 return align;
8700 /* Verify that TYPE is something we can implement atomically. If not, issue
8701 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8702 process a component type. */
8704 static void
8705 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8707 Node_Id gnat_error_point = gnat_entity;
8708 Node_Id gnat_node;
8709 machine_mode mode;
8710 enum mode_class mclass;
8711 unsigned int align;
8712 tree size;
8714 /* If this is an anonymous base type, nothing to check, the error will be
8715 reported on the source type if need be. */
8716 if (!Comes_From_Source (gnat_entity))
8717 return;
8719 mode = TYPE_MODE (type);
8720 mclass = GET_MODE_CLASS (mode);
8721 align = TYPE_ALIGN (type);
8722 size = TYPE_SIZE (type);
8724 /* Consider all aligned floating-point types atomic and any aligned types
8725 that are represented by integers no wider than a machine word. */
8726 if ((mclass == MODE_FLOAT
8727 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8728 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8729 && align >= GET_MODE_ALIGNMENT (mode))
8730 return;
8732 /* For the moment, also allow anything that has an alignment equal to its
8733 size and which is smaller than a word. */
8734 if (size
8735 && TREE_CODE (size) == INTEGER_CST
8736 && compare_tree_int (size, align) == 0
8737 && align <= BITS_PER_WORD)
8738 return;
8740 for (gnat_node = First_Rep_Item (gnat_entity);
8741 Present (gnat_node);
8742 gnat_node = Next_Rep_Item (gnat_node))
8743 if (Nkind (gnat_node) == N_Pragma)
8745 unsigned char pragma_id
8746 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8748 if ((pragma_id == Pragma_Atomic && !component_p)
8749 || (pragma_id == Pragma_Atomic_Components && component_p))
8751 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8752 break;
8756 if (component_p)
8757 post_error_ne ("atomic access to component of & cannot be guaranteed",
8758 gnat_error_point, gnat_entity);
8759 else if (Is_Volatile_Full_Access (gnat_entity))
8760 post_error_ne ("volatile full access to & cannot be guaranteed",
8761 gnat_error_point, gnat_entity);
8762 else
8763 post_error_ne ("atomic access to & cannot be guaranteed",
8764 gnat_error_point, gnat_entity);
8768 /* Helper for the intrin compatibility checks family. Evaluate whether
8769 two types are definitely incompatible. */
8771 static bool
8772 intrin_types_incompatible_p (tree t1, tree t2)
8774 enum tree_code code;
8776 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8777 return false;
8779 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8780 return true;
8782 if (TREE_CODE (t1) != TREE_CODE (t2))
8783 return true;
8785 code = TREE_CODE (t1);
8787 switch (code)
8789 case INTEGER_TYPE:
8790 case REAL_TYPE:
8791 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8793 case POINTER_TYPE:
8794 case REFERENCE_TYPE:
8795 /* Assume designated types are ok. We'd need to account for char * and
8796 void * variants to do better, which could rapidly get messy and isn't
8797 clearly worth the effort. */
8798 return false;
8800 default:
8801 break;
8804 return false;
8807 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8808 on the Ada/builtin argument lists for the INB binding. */
8810 static bool
8811 intrin_arglists_compatible_p (intrin_binding_t * inb)
8813 function_args_iterator ada_iter, btin_iter;
8815 function_args_iter_init (&ada_iter, inb->ada_fntype);
8816 function_args_iter_init (&btin_iter, inb->btin_fntype);
8818 /* Sequence position of the last argument we checked. */
8819 int argpos = 0;
8821 while (true)
8823 tree ada_type = function_args_iter_cond (&ada_iter);
8824 tree btin_type = function_args_iter_cond (&btin_iter);
8826 /* If we've exhausted both lists simultaneously, we're done. */
8827 if (!ada_type && !btin_type)
8828 break;
8830 /* If one list is shorter than the other, they fail to match. */
8831 if (!ada_type || !btin_type)
8832 return false;
8834 /* If we're done with the Ada args and not with the internal builtin
8835 args, or the other way around, complain. */
8836 if (ada_type == void_type_node
8837 && btin_type != void_type_node)
8839 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8840 return false;
8843 if (btin_type == void_type_node
8844 && ada_type != void_type_node)
8846 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8847 inb->gnat_entity, inb->gnat_entity, argpos);
8848 return false;
8851 /* Otherwise, check that types match for the current argument. */
8852 argpos ++;
8853 if (intrin_types_incompatible_p (ada_type, btin_type))
8855 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8856 inb->gnat_entity, inb->gnat_entity, argpos);
8857 return false;
8861 function_args_iter_next (&ada_iter);
8862 function_args_iter_next (&btin_iter);
8865 return true;
8868 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8869 on the Ada/builtin return values for the INB binding. */
8871 static bool
8872 intrin_return_compatible_p (intrin_binding_t * inb)
8874 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8875 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8877 /* Accept function imported as procedure, common and convenient. */
8878 if (VOID_TYPE_P (ada_return_type)
8879 && !VOID_TYPE_P (btin_return_type))
8880 return true;
8882 /* If return type is Address (integer type), map it to void *. */
8883 if (Is_Descendant_Of_Address (Etype (inb->gnat_entity)))
8884 ada_return_type = ptr_type_node;
8886 /* Check return types compatibility otherwise. Note that this
8887 handles void/void as well. */
8888 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8890 post_error ("?intrinsic binding type mismatch on return value!",
8891 inb->gnat_entity);
8892 return false;
8895 return true;
8898 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8899 compatible. Issue relevant warnings when they are not.
8901 This is intended as a light check to diagnose the most obvious cases, not
8902 as a full fledged type compatibility predicate. It is the programmer's
8903 responsibility to ensure correctness of the Ada declarations in Imports,
8904 especially when binding straight to a compiler internal. */
8906 static bool
8907 intrin_profiles_compatible_p (intrin_binding_t * inb)
8909 /* Check compatibility on return values and argument lists, each responsible
8910 for posting warnings as appropriate. Ensure use of the proper sloc for
8911 this purpose. */
8913 bool arglists_compatible_p, return_compatible_p;
8914 location_t saved_location = input_location;
8916 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8918 return_compatible_p = intrin_return_compatible_p (inb);
8919 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8921 input_location = saved_location;
8923 return return_compatible_p && arglists_compatible_p;
8926 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8927 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8928 specified size for this field. POS_LIST is a position list describing
8929 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8930 to this layout. */
8932 static tree
8933 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8934 tree size, tree pos_list,
8935 vec<subst_pair> subst_list)
8937 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8938 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8939 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8940 tree new_pos, new_field;
8941 unsigned int i;
8942 subst_pair *s;
8944 if (CONTAINS_PLACEHOLDER_P (pos))
8945 FOR_EACH_VEC_ELT (subst_list, i, s)
8946 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8948 /* If the position is now a constant, we can set it as the position of the
8949 field when we make it. Otherwise, we need to deal with it specially. */
8950 if (TREE_CONSTANT (pos))
8951 new_pos = bit_from_pos (pos, bitpos);
8952 else
8953 new_pos = NULL_TREE;
8955 new_field
8956 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8957 size, new_pos, DECL_PACKED (old_field),
8958 !DECL_NONADDRESSABLE_P (old_field));
8960 if (!new_pos)
8962 normalize_offset (&pos, &bitpos, offset_align);
8963 /* Finalize the position. */
8964 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8965 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8966 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8967 DECL_SIZE (new_field) = size;
8968 DECL_SIZE_UNIT (new_field)
8969 = convert (sizetype,
8970 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8971 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8974 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8975 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8976 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8977 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8979 return new_field;
8982 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8983 it is the minimal size the REP_PART must have. */
8985 static tree
8986 create_rep_part (tree rep_type, tree record_type, tree min_size)
8988 tree field;
8990 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8991 min_size = NULL_TREE;
8993 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8994 min_size, NULL_TREE, 0, 1);
8995 DECL_INTERNAL_P (field) = 1;
8997 return field;
9000 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9002 static tree
9003 get_rep_part (tree record_type)
9005 tree field = TYPE_FIELDS (record_type);
9007 /* The REP part is the first field, internal, another record, and its name
9008 starts with an 'R'. */
9009 if (field
9010 && DECL_INTERNAL_P (field)
9011 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9012 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9013 return field;
9015 return NULL_TREE;
9018 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9020 tree
9021 get_variant_part (tree record_type)
9023 tree field;
9025 /* The variant part is the only internal field that is a qualified union. */
9026 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9027 if (DECL_INTERNAL_P (field)
9028 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9029 return field;
9031 return NULL_TREE;
9034 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9035 the list of variants to be used and RECORD_TYPE is the type of the parent.
9036 POS_LIST is a position list describing the layout of fields present in
9037 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9038 layout. */
9040 static tree
9041 create_variant_part_from (tree old_variant_part,
9042 vec<variant_desc> variant_list,
9043 tree record_type, tree pos_list,
9044 vec<subst_pair> subst_list)
9046 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9047 tree old_union_type = TREE_TYPE (old_variant_part);
9048 tree new_union_type, new_variant_part;
9049 tree union_field_list = NULL_TREE;
9050 variant_desc *v;
9051 unsigned int i;
9053 /* First create the type of the variant part from that of the old one. */
9054 new_union_type = make_node (QUAL_UNION_TYPE);
9055 TYPE_NAME (new_union_type)
9056 = concat_name (TYPE_NAME (record_type),
9057 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9059 /* If the position of the variant part is constant, subtract it from the
9060 size of the type of the parent to get the new size. This manual CSE
9061 reduces the code size when not optimizing. */
9062 if (TREE_CODE (offset) == INTEGER_CST)
9064 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9065 tree first_bit = bit_from_pos (offset, bitpos);
9066 TYPE_SIZE (new_union_type)
9067 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9068 TYPE_SIZE_UNIT (new_union_type)
9069 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9070 byte_from_pos (offset, bitpos));
9071 SET_TYPE_ADA_SIZE (new_union_type,
9072 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9073 first_bit));
9074 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9075 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9077 else
9078 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9080 /* Now finish up the new variants and populate the union type. */
9081 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9083 tree old_field = v->field, new_field;
9084 tree old_variant, old_variant_subpart, new_variant, field_list;
9086 /* Skip variants that don't belong to this nesting level. */
9087 if (DECL_CONTEXT (old_field) != old_union_type)
9088 continue;
9090 /* Retrieve the list of fields already added to the new variant. */
9091 new_variant = v->new_type;
9092 field_list = TYPE_FIELDS (new_variant);
9094 /* If the old variant had a variant subpart, we need to create a new
9095 variant subpart and add it to the field list. */
9096 old_variant = v->type;
9097 old_variant_subpart = get_variant_part (old_variant);
9098 if (old_variant_subpart)
9100 tree new_variant_subpart
9101 = create_variant_part_from (old_variant_subpart, variant_list,
9102 new_variant, pos_list, subst_list);
9103 DECL_CHAIN (new_variant_subpart) = field_list;
9104 field_list = new_variant_subpart;
9107 /* Finish up the new variant and create the field. No need for debug
9108 info thanks to the XVS type. */
9109 finish_record_type (new_variant, nreverse (field_list), 2, false);
9110 compute_record_mode (new_variant);
9111 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
9112 Empty);
9114 new_field
9115 = create_field_decl_from (old_field, new_variant, new_union_type,
9116 TYPE_SIZE (new_variant),
9117 pos_list, subst_list);
9118 DECL_QUALIFIER (new_field) = v->qual;
9119 DECL_INTERNAL_P (new_field) = 1;
9120 DECL_CHAIN (new_field) = union_field_list;
9121 union_field_list = new_field;
9124 /* Finish up the union type and create the variant part. No need for debug
9125 info thanks to the XVS type. Note that we don't reverse the field list
9126 because VARIANT_LIST has been traversed in reverse order. */
9127 finish_record_type (new_union_type, union_field_list, 2, false);
9128 compute_record_mode (new_union_type);
9129 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
9130 Empty);
9132 new_variant_part
9133 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9134 TYPE_SIZE (new_union_type),
9135 pos_list, subst_list);
9136 DECL_INTERNAL_P (new_variant_part) = 1;
9138 /* With multiple discriminants it is possible for an inner variant to be
9139 statically selected while outer ones are not; in this case, the list
9140 of fields of the inner variant is not flattened and we end up with a
9141 qualified union with a single member. Drop the useless container. */
9142 if (!DECL_CHAIN (union_field_list))
9144 DECL_CONTEXT (union_field_list) = record_type;
9145 DECL_FIELD_OFFSET (union_field_list)
9146 = DECL_FIELD_OFFSET (new_variant_part);
9147 DECL_FIELD_BIT_OFFSET (union_field_list)
9148 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9149 SET_DECL_OFFSET_ALIGN (union_field_list,
9150 DECL_OFFSET_ALIGN (new_variant_part));
9151 new_variant_part = union_field_list;
9154 return new_variant_part;
9157 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9158 which are both RECORD_TYPE, after applying the substitutions described
9159 in SUBST_LIST. */
9161 static void
9162 copy_and_substitute_in_size (tree new_type, tree old_type,
9163 vec<subst_pair> subst_list)
9165 unsigned int i;
9166 subst_pair *s;
9168 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9169 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9170 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9171 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9172 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9174 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9175 FOR_EACH_VEC_ELT (subst_list, i, s)
9176 TYPE_SIZE (new_type)
9177 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9178 s->discriminant, s->replacement);
9180 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9181 FOR_EACH_VEC_ELT (subst_list, i, s)
9182 TYPE_SIZE_UNIT (new_type)
9183 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9184 s->discriminant, s->replacement);
9186 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9187 FOR_EACH_VEC_ELT (subst_list, i, s)
9188 SET_TYPE_ADA_SIZE
9189 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9190 s->discriminant, s->replacement));
9192 /* Finalize the size. */
9193 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9194 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9197 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9198 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9199 the original array type if it has been translated. This association is a
9200 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9201 that for standard DWARF, we also want to get the original type name. */
9203 static void
9204 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9206 Entity_Id gnat_original_array_type
9207 = Underlying_Type (Original_Array_Type (gnat_entity));
9208 tree gnu_original_array_type;
9210 if (!present_gnu_tree (gnat_original_array_type))
9211 return;
9213 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9215 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9216 return;
9218 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9220 tree original_name = TYPE_NAME (gnu_original_array_type);
9222 if (TREE_CODE (original_name) == TYPE_DECL)
9223 original_name = DECL_NAME (original_name);
9225 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9226 TYPE_NAME (gnu_type) = original_name;
9228 else
9229 add_parallel_type (gnu_type, gnu_original_array_type);
9232 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
9233 type with all size expressions that contain F in a PLACEHOLDER_EXPR
9234 updated by replacing F with R.
9236 The function doesn't update the layout of the type, i.e. it assumes
9237 that the substitution is purely formal. That's why the replacement
9238 value R must itself contain a PLACEHOLDER_EXPR. */
9240 tree
9241 substitute_in_type (tree t, tree f, tree r)
9243 tree nt;
9245 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9247 switch (TREE_CODE (t))
9249 case INTEGER_TYPE:
9250 case ENUMERAL_TYPE:
9251 case BOOLEAN_TYPE:
9252 case REAL_TYPE:
9254 /* First the domain types of arrays. */
9255 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9256 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9258 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9259 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9261 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9262 return t;
9264 nt = copy_type (t);
9265 TYPE_GCC_MIN_VALUE (nt) = low;
9266 TYPE_GCC_MAX_VALUE (nt) = high;
9268 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9269 SET_TYPE_INDEX_TYPE
9270 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9272 return nt;
9275 /* Then the subtypes. */
9276 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9277 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9279 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9280 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9282 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9283 return t;
9285 nt = copy_type (t);
9286 SET_TYPE_RM_MIN_VALUE (nt, low);
9287 SET_TYPE_RM_MAX_VALUE (nt, high);
9289 return nt;
9292 return t;
9294 case COMPLEX_TYPE:
9295 nt = substitute_in_type (TREE_TYPE (t), f, r);
9296 if (nt == TREE_TYPE (t))
9297 return t;
9299 return build_complex_type (nt);
9301 case FUNCTION_TYPE:
9302 /* These should never show up here. */
9303 gcc_unreachable ();
9305 case ARRAY_TYPE:
9307 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9308 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9310 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9311 return t;
9313 nt = build_nonshared_array_type (component, domain);
9314 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9315 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9316 SET_TYPE_MODE (nt, TYPE_MODE (t));
9317 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9318 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9319 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9320 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9321 if (TYPE_REVERSE_STORAGE_ORDER (t))
9322 set_reverse_storage_order_on_array_type (nt);
9323 if (TYPE_NONALIASED_COMPONENT (t))
9324 set_nonaliased_component_on_array_type (nt);
9325 return nt;
9328 case RECORD_TYPE:
9329 case UNION_TYPE:
9330 case QUAL_UNION_TYPE:
9332 bool changed_field = false;
9333 tree field;
9335 /* Start out with no fields, make new fields, and chain them
9336 in. If we haven't actually changed the type of any field,
9337 discard everything we've done and return the old type. */
9338 nt = copy_type (t);
9339 TYPE_FIELDS (nt) = NULL_TREE;
9341 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9343 tree new_field = copy_node (field), new_n;
9345 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9346 if (new_n != TREE_TYPE (field))
9348 TREE_TYPE (new_field) = new_n;
9349 changed_field = true;
9352 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9353 if (new_n != DECL_FIELD_OFFSET (field))
9355 DECL_FIELD_OFFSET (new_field) = new_n;
9356 changed_field = true;
9359 /* Do the substitution inside the qualifier, if any. */
9360 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9362 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9363 if (new_n != DECL_QUALIFIER (field))
9365 DECL_QUALIFIER (new_field) = new_n;
9366 changed_field = true;
9370 DECL_CONTEXT (new_field) = nt;
9371 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9373 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9374 TYPE_FIELDS (nt) = new_field;
9377 if (!changed_field)
9378 return t;
9380 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9381 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9382 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9383 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9384 return nt;
9387 default:
9388 return t;
9392 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9393 needed to represent the object. */
9395 tree
9396 rm_size (tree gnu_type)
9398 /* For integral types, we store the RM size explicitly. */
9399 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9400 return TYPE_RM_SIZE (gnu_type);
9402 /* Return the RM size of the actual data plus the size of the template. */
9403 if (TREE_CODE (gnu_type) == RECORD_TYPE
9404 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9405 return
9406 size_binop (PLUS_EXPR,
9407 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9408 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9410 /* For record or union types, we store the size explicitly. */
9411 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9412 && !TYPE_FAT_POINTER_P (gnu_type)
9413 && TYPE_ADA_SIZE (gnu_type))
9414 return TYPE_ADA_SIZE (gnu_type);
9416 /* For other types, this is just the size. */
9417 return TYPE_SIZE (gnu_type);
9420 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9421 fully-qualified name, possibly with type information encoding.
9422 Otherwise, return the name. */
9424 static const char *
9425 get_entity_char (Entity_Id gnat_entity)
9427 Get_Encoded_Name (gnat_entity);
9428 return ggc_strdup (Name_Buffer);
9431 tree
9432 get_entity_name (Entity_Id gnat_entity)
9434 Get_Encoded_Name (gnat_entity);
9435 return get_identifier_with_length (Name_Buffer, Name_Len);
9438 /* Return an identifier representing the external name to be used for
9439 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9440 and the specified suffix. */
9442 tree
9443 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9445 const Entity_Kind kind = Ekind (gnat_entity);
9446 const bool has_suffix = (suffix != NULL);
9447 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9448 String_Pointer sp = {suffix, &temp};
9450 Get_External_Name (gnat_entity, has_suffix, sp);
9452 /* A variable using the Stdcall convention lives in a DLL. We adjust
9453 its name to use the jump table, the _imp__NAME contains the address
9454 for the NAME variable. */
9455 if ((kind == E_Variable || kind == E_Constant)
9456 && Has_Stdcall_Convention (gnat_entity))
9458 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9459 char *new_name = (char *) alloca (len + 1);
9460 strcpy (new_name, STDCALL_PREFIX);
9461 strcat (new_name, Name_Buffer);
9462 return get_identifier_with_length (new_name, len);
9465 return get_identifier_with_length (Name_Buffer, Name_Len);
9468 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9469 string, return a new IDENTIFIER_NODE that is the concatenation of
9470 the name followed by "___" and the specified suffix. */
9472 tree
9473 concat_name (tree gnu_name, const char *suffix)
9475 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9476 char *new_name = (char *) alloca (len + 1);
9477 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9478 strcat (new_name, "___");
9479 strcat (new_name, suffix);
9480 return get_identifier_with_length (new_name, len);
9483 /* Initialize data structures of the decl.c module. */
9485 void
9486 init_gnat_decl (void)
9488 /* Initialize the cache of annotated values. */
9489 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9491 /* Initialize the association of dummy types with subprograms. */
9492 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
9495 /* Destroy data structures of the decl.c module. */
9497 void
9498 destroy_gnat_decl (void)
9500 /* Destroy the cache of annotated values. */
9501 annotate_value_cache->empty ();
9502 annotate_value_cache = NULL;
9504 /* Destroy the association of dummy types with subprograms. */
9505 dummy_to_subprog_map->empty ();
9506 dummy_to_subprog_map = NULL;
9509 #include "gt-ada-decl.h"