* gcc-interface/decl.c (gnat_to_gnu_field): Do not enforce strict
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob4bf14649d97459ac89e6738a9caf3267b2309fad
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2017, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
37 #include "demangle.h"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "uintp.h"
48 #include "urealp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
55 /* "stdcall" and "thiscall" conventions should be processed in a specific way
56 on 32-bit x86/Windows only. The macros below are helpers to avoid having
57 to check for a Windows specific attribute throughout this unit. */
59 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #ifdef TARGET_64BIT
61 #define Has_Stdcall_Convention(E) \
62 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
63 #define Has_Thiscall_Convention(E) \
64 (!TARGET_64BIT && is_cplusplus_method (E))
65 #else
66 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
67 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
68 #endif
69 #else
70 #define Has_Stdcall_Convention(E) 0
71 #define Has_Thiscall_Convention(E) 0
72 #endif
74 #define STDCALL_PREFIX "_imp__"
76 /* Stack realignment is necessary for functions with foreign conventions when
77 the ABI doesn't mandate as much as what the compiler assumes - that is, up
78 to PREFERRED_STACK_BOUNDARY.
80 Such realignment can be requested with a dedicated function type attribute
81 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
82 characterize the situations where the attribute should be set. We rely on
83 compiler configuration settings for 'main' to decide. */
85 #ifdef MAIN_STACK_BOUNDARY
86 #define FOREIGN_FORCE_REALIGN_STACK \
87 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
88 #else
89 #define FOREIGN_FORCE_REALIGN_STACK 0
90 #endif
92 struct incomplete
94 struct incomplete *next;
95 tree old_type;
96 Entity_Id full_type;
99 /* These variables are used to defer recursively expanding incomplete types
100 while we are processing a record, an array or a subprogram type. */
101 static int defer_incomplete_level = 0;
102 static struct incomplete *defer_incomplete_list;
104 /* This variable is used to delay expanding From_Limited_With types until the
105 end of the spec. */
106 static struct incomplete *defer_limited_with_list;
108 typedef struct subst_pair_d {
109 tree discriminant;
110 tree replacement;
111 } subst_pair;
114 typedef struct variant_desc_d {
115 /* The type of the variant. */
116 tree type;
118 /* The associated field. */
119 tree field;
121 /* The value of the qualifier. */
122 tree qual;
124 /* The type of the variant after transformation. */
125 tree new_type;
126 } variant_desc;
129 /* A map used to cache the result of annotate_value. */
130 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
132 static inline hashval_t
133 hash (tree_int_map *m)
135 return htab_hash_pointer (m->base.from);
138 static inline bool
139 equal (tree_int_map *a, tree_int_map *b)
141 return a->base.from == b->base.from;
144 static int
145 keep_cache_entry (tree_int_map *&m)
147 return ggc_marked_p (m->base.from);
151 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
153 /* A map used to associate a dummy type with a list of subprogram entities. */
154 struct GTY((for_user)) tree_entity_vec_map
156 struct tree_map_base base;
157 vec<Entity_Id, va_gc_atomic> *to;
160 void
161 gt_pch_nx (Entity_Id &)
165 void
166 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
168 op (x, cookie);
171 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
173 static inline hashval_t
174 hash (tree_entity_vec_map *m)
176 return htab_hash_pointer (m->base.from);
179 static inline bool
180 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
182 return a->base.from == b->base.from;
185 static int
186 keep_cache_entry (tree_entity_vec_map *&m)
188 return ggc_marked_p (m->base.from);
192 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
194 static void prepend_one_attribute (struct attrib **,
195 enum attrib_type, tree, tree, Node_Id);
196 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
197 static void prepend_attributes (struct attrib **, Entity_Id);
198 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
199 bool);
200 static bool type_has_variable_size (tree);
201 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
202 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
203 unsigned int);
204 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
205 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
206 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
207 static int adjust_packed (tree, tree, int);
208 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
209 static tree gnu_ext_name_for_subprog (Entity_Id, tree);
210 static tree change_qualified_type (tree, int);
211 static void set_nonaliased_component_on_array_type (tree);
212 static void set_reverse_storage_order_on_array_type (tree);
213 static bool same_discriminant_p (Entity_Id, Entity_Id);
214 static bool array_type_has_nonaliased_component (tree, Entity_Id);
215 static bool compile_time_known_address_p (Node_Id);
216 static bool cannot_be_superflat (Node_Id);
217 static bool constructor_address_p (tree);
218 static bool allocatable_size_p (tree, bool);
219 static bool initial_value_needs_conversion (tree, tree);
220 static int compare_field_bitpos (const PTR, const PTR);
221 static bool components_to_record (Node_Id, Entity_Id, tree, tree, int, bool,
222 bool, bool, bool, bool, bool, bool, tree,
223 tree *);
224 static Uint annotate_value (tree);
225 static void annotate_rep (Entity_Id, tree);
226 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
227 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
228 static vec<variant_desc> build_variant_list (tree, vec<subst_pair>,
229 vec<variant_desc>);
230 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
231 static void set_rm_size (Uint, tree, Entity_Id);
232 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
233 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
234 static tree create_field_decl_from (tree, tree, tree, tree, tree,
235 vec<subst_pair>);
236 static tree create_rep_part (tree, tree, tree);
237 static tree get_rep_part (tree);
238 static tree create_variant_part_from (tree, vec<variant_desc>, tree,
239 tree, vec<subst_pair>, bool);
240 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair>);
241 static void copy_and_substitute_in_layout (Entity_Id, Entity_Id, tree, tree,
242 vec<subst_pair>, bool);
243 static void associate_original_type_to_packed_array (tree, Entity_Id);
244 static const char *get_entity_char (Entity_Id);
246 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
247 to pass around calls performing profile compatibility checks. */
249 typedef struct {
250 Entity_Id gnat_entity; /* The Ada subprogram entity. */
251 tree ada_fntype; /* The corresponding GCC type node. */
252 tree btin_fntype; /* The GCC builtin function type node. */
253 } intrin_binding_t;
255 static bool intrin_profiles_compatible_p (intrin_binding_t *);
257 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
258 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
259 and associate the ..._DECL node with the input GNAT defining identifier.
261 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
262 initial value (in GCC tree form). This is optional for a variable. For
263 a renamed entity, GNU_EXPR gives the object being renamed.
265 DEFINITION is true if this call is intended for a definition. This is used
266 for separate compilation where it is necessary to know whether an external
267 declaration or a definition must be created if the GCC equivalent was not
268 created previously. */
270 tree
271 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
273 /* Contains the kind of the input GNAT node. */
274 const Entity_Kind kind = Ekind (gnat_entity);
275 /* True if this is a type. */
276 const bool is_type = IN (kind, Type_Kind);
277 /* True if this is an artificial entity. */
278 const bool artificial_p = !Comes_From_Source (gnat_entity);
279 /* True if debug info is requested for this entity. */
280 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
281 /* True if this entity is to be considered as imported. */
282 const bool imported_p
283 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
284 /* For a type, contains the equivalent GNAT node to be used in gigi. */
285 Entity_Id gnat_equiv_type = Empty;
286 /* Temporary used to walk the GNAT tree. */
287 Entity_Id gnat_temp;
288 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
289 This node will be associated with the GNAT node by calling at the end
290 of the `switch' statement. */
291 tree gnu_decl = NULL_TREE;
292 /* Contains the GCC type to be used for the GCC node. */
293 tree gnu_type = NULL_TREE;
294 /* Contains the GCC size tree to be used for the GCC node. */
295 tree gnu_size = NULL_TREE;
296 /* Contains the GCC name to be used for the GCC node. */
297 tree gnu_entity_name;
298 /* True if we have already saved gnu_decl as a GNAT association. */
299 bool saved = false;
300 /* True if we incremented defer_incomplete_level. */
301 bool this_deferred = false;
302 /* True if we incremented force_global. */
303 bool this_global = false;
304 /* True if we should check to see if elaborated during processing. */
305 bool maybe_present = false;
306 /* True if we made GNU_DECL and its type here. */
307 bool this_made_decl = false;
308 /* Size and alignment of the GCC node, if meaningful. */
309 unsigned int esize = 0, align = 0;
310 /* Contains the list of attributes directly attached to the entity. */
311 struct attrib *attr_list = NULL;
313 /* Since a use of an Itype is a definition, process it as such if it
314 is not in a with'ed unit. */
315 if (!definition
316 && is_type
317 && Is_Itype (gnat_entity)
318 && !present_gnu_tree (gnat_entity)
319 && In_Extended_Main_Code_Unit (gnat_entity))
321 /* Ensure that we are in a subprogram mentioned in the Scope chain of
322 this entity, our current scope is global, or we encountered a task
323 or entry (where we can't currently accurately check scoping). */
324 if (!current_function_decl
325 || DECL_ELABORATION_PROC_P (current_function_decl))
327 process_type (gnat_entity);
328 return get_gnu_tree (gnat_entity);
331 for (gnat_temp = Scope (gnat_entity);
332 Present (gnat_temp);
333 gnat_temp = Scope (gnat_temp))
335 if (Is_Type (gnat_temp))
336 gnat_temp = Underlying_Type (gnat_temp);
338 if (Ekind (gnat_temp) == E_Subprogram_Body)
339 gnat_temp
340 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
342 if (IN (Ekind (gnat_temp), Subprogram_Kind)
343 && Present (Protected_Body_Subprogram (gnat_temp)))
344 gnat_temp = Protected_Body_Subprogram (gnat_temp);
346 if (Ekind (gnat_temp) == E_Entry
347 || Ekind (gnat_temp) == E_Entry_Family
348 || Ekind (gnat_temp) == E_Task_Type
349 || (IN (Ekind (gnat_temp), Subprogram_Kind)
350 && present_gnu_tree (gnat_temp)
351 && (current_function_decl
352 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
354 process_type (gnat_entity);
355 return get_gnu_tree (gnat_entity);
359 /* This abort means the Itype has an incorrect scope, i.e. that its
360 scope does not correspond to the subprogram it is declared in. */
361 gcc_unreachable ();
364 /* If we've already processed this entity, return what we got last time.
365 If we are defining the node, we should not have already processed it.
366 In that case, we will abort below when we try to save a new GCC tree
367 for this object. We also need to handle the case of getting a dummy
368 type when a Full_View exists but be careful so as not to trigger its
369 premature elaboration. */
370 if ((!definition || (is_type && imported_p))
371 && present_gnu_tree (gnat_entity))
373 gnu_decl = get_gnu_tree (gnat_entity);
375 if (TREE_CODE (gnu_decl) == TYPE_DECL
376 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
377 && IN (kind, Incomplete_Or_Private_Kind)
378 && Present (Full_View (gnat_entity))
379 && (present_gnu_tree (Full_View (gnat_entity))
380 || No (Freeze_Node (Full_View (gnat_entity)))))
382 gnu_decl
383 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, false);
384 save_gnu_tree (gnat_entity, NULL_TREE, false);
385 save_gnu_tree (gnat_entity, gnu_decl, false);
388 return gnu_decl;
391 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
392 must be specified unless it was specified by the programmer. Exceptions
393 are for access-to-protected-subprogram types and all access subtypes, as
394 another GNAT type is used to lay out the GCC type for them. */
395 gcc_assert (!is_type
396 || Known_Esize (gnat_entity)
397 || Has_Size_Clause (gnat_entity)
398 || (!IN (kind, Numeric_Kind)
399 && !IN (kind, Enumeration_Kind)
400 && (!IN (kind, Access_Kind)
401 || kind == E_Access_Protected_Subprogram_Type
402 || kind == E_Anonymous_Access_Protected_Subprogram_Type
403 || kind == E_Access_Subtype
404 || type_annotate_only)));
406 /* The RM size must be specified for all discrete and fixed-point types. */
407 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
408 && Unknown_RM_Size (gnat_entity)));
410 /* If we get here, it means we have not yet done anything with this entity.
411 If we are not defining it, it must be a type or an entity that is defined
412 elsewhere or externally, otherwise we should have defined it already. */
413 gcc_assert (definition
414 || type_annotate_only
415 || is_type
416 || kind == E_Discriminant
417 || kind == E_Component
418 || kind == E_Label
419 || (kind == E_Constant && Present (Full_View (gnat_entity)))
420 || Is_Public (gnat_entity));
422 /* Get the name of the entity and set up the line number and filename of
423 the original definition for use in any decl we make. Make sure we do not
424 inherit another source location. */
425 gnu_entity_name = get_entity_name (gnat_entity);
426 if (Sloc (gnat_entity) != No_Location
427 && !renaming_from_generic_instantiation_p (gnat_entity))
428 Sloc_to_locus (Sloc (gnat_entity), &input_location);
430 /* For cases when we are not defining (i.e., we are referencing from
431 another compilation unit) public entities, show we are at global level
432 for the purpose of computing scopes. Don't do this for components or
433 discriminants since the relevant test is whether or not the record is
434 being defined. */
435 if (!definition
436 && kind != E_Component
437 && kind != E_Discriminant
438 && Is_Public (gnat_entity)
439 && !Is_Statically_Allocated (gnat_entity))
440 force_global++, this_global = true;
442 /* Handle any attributes directly attached to the entity. */
443 if (Has_Gigi_Rep_Item (gnat_entity))
444 prepend_attributes (&attr_list, gnat_entity);
446 /* Do some common processing for types. */
447 if (is_type)
449 /* Compute the equivalent type to be used in gigi. */
450 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
452 /* Machine_Attributes on types are expected to be propagated to
453 subtypes. The corresponding Gigi_Rep_Items are only attached
454 to the first subtype though, so we handle the propagation here. */
455 if (Base_Type (gnat_entity) != gnat_entity
456 && !Is_First_Subtype (gnat_entity)
457 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
458 prepend_attributes (&attr_list,
459 First_Subtype (Base_Type (gnat_entity)));
461 /* Compute a default value for the size of an elementary type. */
462 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
464 unsigned int max_esize;
466 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
467 esize = UI_To_Int (Esize (gnat_entity));
469 if (IN (kind, Float_Kind))
470 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
471 else if (IN (kind, Access_Kind))
472 max_esize = POINTER_SIZE * 2;
473 else
474 max_esize = LONG_LONG_TYPE_SIZE;
476 if (esize > max_esize)
477 esize = max_esize;
481 switch (kind)
483 case E_Component:
484 case E_Discriminant:
486 /* The GNAT record where the component was defined. */
487 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
489 /* If the entity is a discriminant of an extended tagged type used to
490 rename a discriminant of the parent type, return the latter. */
491 if (kind == E_Discriminant
492 && Present (Corresponding_Discriminant (gnat_entity))
493 && Is_Tagged_Type (gnat_record))
495 gnu_decl
496 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
497 gnu_expr, definition);
498 saved = true;
499 break;
502 /* If the entity is an inherited component (in the case of extended
503 tagged record types), just return the original entity, which must
504 be a FIELD_DECL. Likewise for discriminants. If the entity is a
505 non-girder discriminant (in the case of derived untagged record
506 types), return the stored discriminant it renames. */
507 if (Present (Original_Record_Component (gnat_entity))
508 && Original_Record_Component (gnat_entity) != gnat_entity)
510 gnu_decl
511 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
512 gnu_expr, definition);
513 /* GNU_DECL contains a PLACEHOLDER_EXPR for discriminants. */
514 if (kind == E_Discriminant)
515 saved = true;
516 break;
519 /* Otherwise, if we are not defining this and we have no GCC type
520 for the containing record, make one for it. Then we should
521 have made our own equivalent. */
522 if (!definition && !present_gnu_tree (gnat_record))
524 /* ??? If this is in a record whose scope is a protected
525 type and we have an Original_Record_Component, use it.
526 This is a workaround for major problems in protected type
527 handling. */
528 Entity_Id Scop = Scope (Scope (gnat_entity));
529 if (Is_Protected_Type (Underlying_Type (Scop))
530 && Present (Original_Record_Component (gnat_entity)))
532 gnu_decl
533 = gnat_to_gnu_entity (Original_Record_Component
534 (gnat_entity),
535 gnu_expr, false);
537 else
539 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
540 gnu_decl = get_gnu_tree (gnat_entity);
543 saved = true;
544 break;
547 /* Here we have no GCC type and this is a reference rather than a
548 definition. This should never happen. Most likely the cause is
549 reference before declaration in the GNAT tree for gnat_entity. */
550 gcc_unreachable ();
553 case E_Constant:
554 /* Ignore constant definitions already marked with the error node. See
555 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
556 if (definition
557 && present_gnu_tree (gnat_entity)
558 && get_gnu_tree (gnat_entity) == error_mark_node)
560 maybe_present = true;
561 break;
564 /* Ignore deferred constant definitions without address clause since
565 they are processed fully in the front-end. If No_Initialization
566 is set, this is not a deferred constant but a constant whose value
567 is built manually. And constants that are renamings are handled
568 like variables. */
569 if (definition
570 && !gnu_expr
571 && No (Address_Clause (gnat_entity))
572 && !No_Initialization (Declaration_Node (gnat_entity))
573 && No (Renamed_Object (gnat_entity)))
575 gnu_decl = error_mark_node;
576 saved = true;
577 break;
580 /* If this is a use of a deferred constant without address clause,
581 get its full definition. */
582 if (!definition
583 && No (Address_Clause (gnat_entity))
584 && Present (Full_View (gnat_entity)))
586 gnu_decl
587 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
588 saved = true;
589 break;
592 /* If we have a constant that we are not defining, get the expression it
593 was defined to represent. This is necessary to avoid generating dumb
594 elaboration code in simple cases, but we may throw it away later if it
595 is not a constant. But do not retrieve it if it is an allocator since
596 the designated type might still be dummy at this point. */
597 if (!definition
598 && !No_Initialization (Declaration_Node (gnat_entity))
599 && Present (Expression (Declaration_Node (gnat_entity)))
600 && Nkind (Expression (Declaration_Node (gnat_entity)))
601 != N_Allocator)
602 /* The expression may contain N_Expression_With_Actions nodes and
603 thus object declarations from other units. Discard them. */
604 gnu_expr
605 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
607 /* ... fall through ... */
609 case E_Exception:
610 case E_Loop_Parameter:
611 case E_Out_Parameter:
612 case E_Variable:
614 const Entity_Id gnat_type = Etype (gnat_entity);
615 /* Always create a variable for volatile objects and variables seen
616 constant but with a Linker_Section pragma. */
617 bool const_flag
618 = ((kind == E_Constant || kind == E_Variable)
619 && Is_True_Constant (gnat_entity)
620 && !(kind == E_Variable
621 && Present (Linker_Section_Pragma (gnat_entity)))
622 && !Treat_As_Volatile (gnat_entity)
623 && (((Nkind (Declaration_Node (gnat_entity))
624 == N_Object_Declaration)
625 && Present (Expression (Declaration_Node (gnat_entity))))
626 || Present (Renamed_Object (gnat_entity))
627 || imported_p));
628 bool inner_const_flag = const_flag;
629 bool static_flag = Is_Statically_Allocated (gnat_entity);
630 /* We implement RM 13.3(19) for exported and imported (non-constant)
631 objects by making them volatile. */
632 bool volatile_flag
633 = (Treat_As_Volatile (gnat_entity)
634 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
635 bool mutable_p = false;
636 bool used_by_ref = false;
637 tree gnu_ext_name = NULL_TREE;
638 tree renamed_obj = NULL_TREE;
639 tree gnu_object_size;
641 /* We need to translate the renamed object even though we are only
642 referencing the renaming. But it may contain a call for which
643 we'll generate a temporary to hold the return value and which
644 is part of the definition of the renaming, so discard it. */
645 if (Present (Renamed_Object (gnat_entity)) && !definition)
647 if (kind == E_Exception)
648 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
649 NULL_TREE, false);
650 else
651 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
654 /* Get the type after elaborating the renamed object. */
655 if (Has_Foreign_Convention (gnat_entity)
656 && Is_Descendant_Of_Address (gnat_type))
657 gnu_type = ptr_type_node;
658 else
660 gnu_type = gnat_to_gnu_type (gnat_type);
662 /* If this is a standard exception definition, use the standard
663 exception type. This is necessary to make sure that imported
664 and exported views of exceptions are merged in LTO mode. */
665 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
666 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
667 gnu_type = except_type_node;
670 /* For a debug renaming declaration, build a debug-only entity. */
671 if (Present (Debug_Renaming_Link (gnat_entity)))
673 /* Force a non-null value to make sure the symbol is retained. */
674 tree value = build1 (INDIRECT_REF, gnu_type,
675 build1 (NOP_EXPR,
676 build_pointer_type (gnu_type),
677 integer_minus_one_node));
678 gnu_decl = build_decl (input_location,
679 VAR_DECL, gnu_entity_name, gnu_type);
680 SET_DECL_VALUE_EXPR (gnu_decl, value);
681 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
682 TREE_STATIC (gnu_decl) = global_bindings_p ();
683 gnat_pushdecl (gnu_decl, gnat_entity);
684 break;
687 /* If this is a loop variable, its type should be the base type.
688 This is because the code for processing a loop determines whether
689 a normal loop end test can be done by comparing the bounds of the
690 loop against those of the base type, which is presumed to be the
691 size used for computation. But this is not correct when the size
692 of the subtype is smaller than the type. */
693 if (kind == E_Loop_Parameter)
694 gnu_type = get_base_type (gnu_type);
696 /* Reject non-renamed objects whose type is an unconstrained array or
697 any object whose type is a dummy type or void. */
698 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
699 && No (Renamed_Object (gnat_entity)))
700 || TYPE_IS_DUMMY_P (gnu_type)
701 || TREE_CODE (gnu_type) == VOID_TYPE)
703 gcc_assert (type_annotate_only);
704 if (this_global)
705 force_global--;
706 return error_mark_node;
709 /* If an alignment is specified, use it if valid. Note that exceptions
710 are objects but don't have an alignment. We must do this before we
711 validate the size, since the alignment can affect the size. */
712 if (kind != E_Exception && Known_Alignment (gnat_entity))
714 gcc_assert (Present (Alignment (gnat_entity)));
716 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
717 TYPE_ALIGN (gnu_type));
719 /* No point in changing the type if there is an address clause
720 as the final type of the object will be a reference type. */
721 if (Present (Address_Clause (gnat_entity)))
722 align = 0;
723 else
725 tree orig_type = gnu_type;
727 gnu_type
728 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
729 false, false, definition, true);
731 /* If a padding record was made, declare it now since it will
732 never be declared otherwise. This is necessary to ensure
733 that its subtrees are properly marked. */
734 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
735 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
736 debug_info_p, gnat_entity);
740 /* If we are defining the object, see if it has a Size and validate it
741 if so. If we are not defining the object and a Size clause applies,
742 simply retrieve the value. We don't want to ignore the clause and
743 it is expected to have been validated already. Then get the new
744 type, if any. */
745 if (definition)
746 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
747 gnat_entity, VAR_DECL, false,
748 Has_Size_Clause (gnat_entity));
749 else if (Has_Size_Clause (gnat_entity))
750 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
752 if (gnu_size)
754 gnu_type
755 = make_type_from_size (gnu_type, gnu_size,
756 Has_Biased_Representation (gnat_entity));
758 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
759 gnu_size = NULL_TREE;
762 /* If this object has self-referential size, it must be a record with
763 a default discriminant. We are supposed to allocate an object of
764 the maximum size in this case, unless it is a constant with an
765 initializing expression, in which case we can get the size from
766 that. Note that the resulting size may still be a variable, so
767 this may end up with an indirect allocation. */
768 if (No (Renamed_Object (gnat_entity))
769 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
771 if (gnu_expr && kind == E_Constant)
773 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
774 if (CONTAINS_PLACEHOLDER_P (size))
776 /* If the initializing expression is itself a constant,
777 despite having a nominal type with self-referential
778 size, we can get the size directly from it. */
779 if (TREE_CODE (gnu_expr) == COMPONENT_REF
780 && TYPE_IS_PADDING_P
781 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
782 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
783 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
784 || DECL_READONLY_ONCE_ELAB
785 (TREE_OPERAND (gnu_expr, 0))))
786 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
787 else
788 gnu_size
789 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
791 else
792 gnu_size = size;
794 /* We may have no GNU_EXPR because No_Initialization is
795 set even though there's an Expression. */
796 else if (kind == E_Constant
797 && (Nkind (Declaration_Node (gnat_entity))
798 == N_Object_Declaration)
799 && Present (Expression (Declaration_Node (gnat_entity))))
800 gnu_size
801 = TYPE_SIZE (gnat_to_gnu_type
802 (Etype
803 (Expression (Declaration_Node (gnat_entity)))));
804 else
806 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
807 mutable_p = true;
810 /* If the size isn't constant and we are at global level, call
811 elaborate_expression_1 to make a variable for it rather than
812 calculating it each time. */
813 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
814 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
815 "SIZE", definition, false);
818 /* If the size is zero byte, make it one byte since some linkers have
819 troubles with zero-sized objects. If the object will have a
820 template, that will make it nonzero so don't bother. Also avoid
821 doing that for an object renaming or an object with an address
822 clause, as we would lose useful information on the view size
823 (e.g. for null array slices) and we are not allocating the object
824 here anyway. */
825 if (((gnu_size
826 && integer_zerop (gnu_size)
827 && !TREE_OVERFLOW (gnu_size))
828 || (TYPE_SIZE (gnu_type)
829 && integer_zerop (TYPE_SIZE (gnu_type))
830 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
831 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
832 && No (Renamed_Object (gnat_entity))
833 && No (Address_Clause (gnat_entity)))
834 gnu_size = bitsize_unit_node;
836 /* If this is an object with no specified size and alignment, and
837 if either it is atomic or we are not optimizing alignment for
838 space and it is composite and not an exception, an Out parameter
839 or a reference to another object, and the size of its type is a
840 constant, set the alignment to the smallest one which is not
841 smaller than the size, with an appropriate cap. */
842 if (!gnu_size && align == 0
843 && (Is_Atomic_Or_VFA (gnat_entity)
844 || (!Optimize_Alignment_Space (gnat_entity)
845 && kind != E_Exception
846 && kind != E_Out_Parameter
847 && Is_Composite_Type (gnat_type)
848 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
849 && !Is_Exported (gnat_entity)
850 && !imported_p
851 && No (Renamed_Object (gnat_entity))
852 && No (Address_Clause (gnat_entity))))
853 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
855 unsigned int size_cap, align_cap;
857 /* No point in promoting the alignment if this doesn't prevent
858 BLKmode access to the object, in particular block copy, as
859 this will for example disable the NRV optimization for it.
860 No point in jumping through all the hoops needed in order
861 to support BIGGEST_ALIGNMENT if we don't really have to.
862 So we cap to the smallest alignment that corresponds to
863 a known efficient memory access pattern of the target. */
864 if (Is_Atomic_Or_VFA (gnat_entity))
866 size_cap = UINT_MAX;
867 align_cap = BIGGEST_ALIGNMENT;
869 else
871 size_cap = MAX_FIXED_MODE_SIZE;
872 align_cap = get_mode_alignment (ptr_mode);
875 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
876 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
877 align = 0;
878 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
879 align = align_cap;
880 else
881 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
883 /* But make sure not to under-align the object. */
884 if (align <= TYPE_ALIGN (gnu_type))
885 align = 0;
887 /* And honor the minimum valid atomic alignment, if any. */
888 #ifdef MINIMUM_ATOMIC_ALIGNMENT
889 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
890 align = MINIMUM_ATOMIC_ALIGNMENT;
891 #endif
894 /* If the object is set to have atomic components, find the component
895 type and validate it.
897 ??? Note that we ignore Has_Volatile_Components on objects; it's
898 not at all clear what to do in that case. */
899 if (Has_Atomic_Components (gnat_entity))
901 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
902 ? TREE_TYPE (gnu_type) : gnu_type);
904 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
905 && TYPE_MULTI_ARRAY_P (gnu_inner))
906 gnu_inner = TREE_TYPE (gnu_inner);
908 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
911 /* If this is an aliased object with an unconstrained array nominal
912 subtype, make a type that includes the template. We will either
913 allocate or create a variable of that type, see below. */
914 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
915 && Is_Array_Type (Underlying_Type (gnat_type))
916 && !type_annotate_only)
918 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
919 gnu_type
920 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
921 gnu_type,
922 concat_name (gnu_entity_name,
923 "UNC"),
924 debug_info_p);
927 /* ??? If this is an object of CW type initialized to a value, try to
928 ensure that the object is sufficient aligned for this value, but
929 without pessimizing the allocation. This is a kludge necessary
930 because we don't support dynamic alignment. */
931 if (align == 0
932 && Ekind (gnat_type) == E_Class_Wide_Subtype
933 && No (Renamed_Object (gnat_entity))
934 && No (Address_Clause (gnat_entity)))
935 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
937 #ifdef MINIMUM_ATOMIC_ALIGNMENT
938 /* If the size is a constant and no alignment is specified, force
939 the alignment to be the minimum valid atomic alignment. The
940 restriction on constant size avoids problems with variable-size
941 temporaries; if the size is variable, there's no issue with
942 atomic access. Also don't do this for a constant, since it isn't
943 necessary and can interfere with constant replacement. Finally,
944 do not do it for Out parameters since that creates an
945 size inconsistency with In parameters. */
946 if (align == 0
947 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
948 && !FLOAT_TYPE_P (gnu_type)
949 && !const_flag && No (Renamed_Object (gnat_entity))
950 && !imported_p && No (Address_Clause (gnat_entity))
951 && kind != E_Out_Parameter
952 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
953 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
954 align = MINIMUM_ATOMIC_ALIGNMENT;
955 #endif
957 /* Make a new type with the desired size and alignment, if needed.
958 But do not take into account alignment promotions to compute the
959 size of the object. */
960 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
961 if (gnu_size || align > 0)
963 tree orig_type = gnu_type;
965 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
966 false, false, definition, true);
968 /* If a padding record was made, declare it now since it will
969 never be declared otherwise. This is necessary to ensure
970 that its subtrees are properly marked. */
971 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
972 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
973 debug_info_p, gnat_entity);
976 /* Now check if the type of the object allows atomic access. */
977 if (Is_Atomic_Or_VFA (gnat_entity))
978 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
980 /* If this is a renaming, avoid as much as possible to create a new
981 object. However, in some cases, creating it is required because
982 renaming can be applied to objects that are not names in Ada.
983 This processing needs to be applied to the raw expression so as
984 to make it more likely to rename the underlying object. */
985 if (Present (Renamed_Object (gnat_entity)))
987 /* If the renamed object had padding, strip off the reference to
988 the inner object and reset our type. */
989 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
990 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
991 /* Strip useless conversions around the object. */
992 || gnat_useless_type_conversion (gnu_expr))
994 gnu_expr = TREE_OPERAND (gnu_expr, 0);
995 gnu_type = TREE_TYPE (gnu_expr);
998 /* Or else, if the renamed object has an unconstrained type with
999 default discriminant, use the padded type. */
1000 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
1001 gnu_type = TREE_TYPE (gnu_expr);
1003 /* Case 1: if this is a constant renaming stemming from a function
1004 call, treat it as a normal object whose initial value is what
1005 is being renamed. RM 3.3 says that the result of evaluating a
1006 function call is a constant object. Therefore, it can be the
1007 inner object of a constant renaming and the renaming must be
1008 fully instantiated, i.e. it cannot be a reference to (part of)
1009 an existing object. And treat other rvalues (addresses, null
1010 expressions, constructors and literals) the same way. */
1011 tree inner = gnu_expr;
1012 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
1013 inner = TREE_OPERAND (inner, 0);
1014 /* Expand_Dispatching_Call can prepend a comparison of the tags
1015 before the call to "=". */
1016 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
1017 || TREE_CODE (inner) == COMPOUND_EXPR)
1018 inner = TREE_OPERAND (inner, 1);
1019 if ((TREE_CODE (inner) == CALL_EXPR
1020 && !call_is_atomic_load (inner))
1021 || TREE_CODE (inner) == ADDR_EXPR
1022 || TREE_CODE (inner) == NULL_EXPR
1023 || TREE_CODE (inner) == PLUS_EXPR
1024 || TREE_CODE (inner) == CONSTRUCTOR
1025 || CONSTANT_CLASS_P (inner)
1026 /* We need to detect the case where a temporary is created to
1027 hold the return value, since we cannot safely rename it at
1028 top level as it lives only in the elaboration routine. */
1029 || (TREE_CODE (inner) == VAR_DECL
1030 && DECL_RETURN_VALUE_P (inner))
1031 /* We also need to detect the case where the front-end creates
1032 a dangling 'reference to a function call at top level and
1033 substitutes it in the renaming, for example:
1035 q__b : boolean renames r__f.e (1);
1037 can be rewritten into:
1039 q__R1s : constant q__A2s := r__f'reference;
1040 [...]
1041 q__b : boolean renames q__R1s.all.e (1);
1043 We cannot safely rename the rewritten expression since the
1044 underlying object lives only in the elaboration routine. */
1045 || (TREE_CODE (inner) == INDIRECT_REF
1046 && (inner
1047 = remove_conversions (TREE_OPERAND (inner, 0), true))
1048 && TREE_CODE (inner) == VAR_DECL
1049 && DECL_RETURN_VALUE_P (inner)))
1052 /* Case 2: if the renaming entity need not be materialized, use
1053 the elaborated renamed expression for the renaming. But this
1054 means that the caller is responsible for evaluating the address
1055 of the renaming in the correct place for the definition case to
1056 instantiate the SAVE_EXPRs. */
1057 else if (!Materialize_Entity (gnat_entity))
1059 tree init = NULL_TREE;
1061 gnu_decl
1062 = elaborate_reference (gnu_expr, gnat_entity, definition,
1063 &init);
1065 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1066 correct place for this case. */
1067 gcc_assert (!init);
1069 /* No DECL_EXPR will be created so the expression needs to be
1070 marked manually because it will likely be shared. */
1071 if (global_bindings_p ())
1072 MARK_VISITED (gnu_decl);
1074 /* This assertion will fail if the renamed object isn't aligned
1075 enough as to make it possible to honor the alignment set on
1076 the renaming. */
1077 if (align)
1079 unsigned int ralign = DECL_P (gnu_decl)
1080 ? DECL_ALIGN (gnu_decl)
1081 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1082 gcc_assert (ralign >= align);
1085 /* The expression might not be a DECL so save it manually. */
1086 save_gnu_tree (gnat_entity, gnu_decl, true);
1087 saved = true;
1088 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1089 break;
1092 /* Case 3: otherwise, make a constant pointer to the object we
1093 are renaming and attach the object to the pointer after it is
1094 elaborated. The object will be referenced directly instead
1095 of indirectly via the pointer to avoid aliasing problems with
1096 non-addressable entities. The pointer is called a "renaming"
1097 pointer in this case. Note that we also need to preserve the
1098 volatility of the renamed object through the indirection. */
1099 else
1101 tree init = NULL_TREE;
1103 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1104 gnu_type
1105 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1106 gnu_type = build_reference_type (gnu_type);
1107 used_by_ref = true;
1108 const_flag = true;
1109 volatile_flag = false;
1110 inner_const_flag = TREE_READONLY (gnu_expr);
1111 gnu_size = NULL_TREE;
1113 renamed_obj
1114 = elaborate_reference (gnu_expr, gnat_entity, definition,
1115 &init);
1117 /* The expression needs to be marked manually because it will
1118 likely be shared, even for a definition since the ADDR_EXPR
1119 built below can cause the first few nodes to be folded. */
1120 if (global_bindings_p ())
1121 MARK_VISITED (renamed_obj);
1123 if (type_annotate_only
1124 && TREE_CODE (renamed_obj) == ERROR_MARK)
1125 gnu_expr = NULL_TREE;
1126 else
1128 gnu_expr
1129 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1130 if (init)
1131 gnu_expr
1132 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1133 gnu_expr);
1138 /* If we are defining an aliased object whose nominal subtype is
1139 unconstrained, the object is a record that contains both the
1140 template and the object. If there is an initializer, it will
1141 have already been converted to the right type, but we need to
1142 create the template if there is no initializer. */
1143 if (definition
1144 && !gnu_expr
1145 && TREE_CODE (gnu_type) == RECORD_TYPE
1146 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1147 /* Beware that padding might have been introduced above. */
1148 || (TYPE_PADDING_P (gnu_type)
1149 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1150 == RECORD_TYPE
1151 && TYPE_CONTAINS_TEMPLATE_P
1152 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1154 tree template_field
1155 = TYPE_PADDING_P (gnu_type)
1156 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1157 : TYPE_FIELDS (gnu_type);
1158 vec<constructor_elt, va_gc> *v;
1159 vec_alloc (v, 1);
1160 tree t = build_template (TREE_TYPE (template_field),
1161 TREE_TYPE (DECL_CHAIN (template_field)),
1162 NULL_TREE);
1163 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1164 gnu_expr = gnat_build_constructor (gnu_type, v);
1167 /* Convert the expression to the type of the object if need be. */
1168 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1169 gnu_expr = convert (gnu_type, gnu_expr);
1171 /* If this is a pointer that doesn't have an initializing expression,
1172 initialize it to NULL, unless the object is declared imported as
1173 per RM B.1(24). */
1174 if (definition
1175 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1176 && !gnu_expr
1177 && !Is_Imported (gnat_entity))
1178 gnu_expr = integer_zero_node;
1180 /* If we are defining the object and it has an Address clause, we must
1181 either get the address expression from the saved GCC tree for the
1182 object if it has a Freeze node, or elaborate the address expression
1183 here since the front-end has guaranteed that the elaboration has no
1184 effects in this case. */
1185 if (definition && Present (Address_Clause (gnat_entity)))
1187 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1188 Node_Id gnat_address = Expression (gnat_clause);
1189 tree gnu_address
1190 = present_gnu_tree (gnat_entity)
1191 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1193 save_gnu_tree (gnat_entity, NULL_TREE, false);
1195 /* Convert the type of the object to a reference type that can
1196 alias everything as per RM 13.3(19). */
1197 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1198 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1199 gnu_type
1200 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1201 gnu_address = convert (gnu_type, gnu_address);
1202 used_by_ref = true;
1203 const_flag
1204 = (!Is_Public (gnat_entity)
1205 || compile_time_known_address_p (gnat_address));
1206 volatile_flag = false;
1207 gnu_size = NULL_TREE;
1209 /* If this is an aliased object with an unconstrained array nominal
1210 subtype, then it can overlay only another aliased object with an
1211 unconstrained array nominal subtype and compatible template. */
1212 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1213 && Is_Array_Type (Underlying_Type (gnat_type))
1214 && !type_annotate_only)
1216 tree rec_type = TREE_TYPE (gnu_type);
1217 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1219 /* This is the pattern built for a regular object. */
1220 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1221 && TREE_OPERAND (gnu_address, 1) == off)
1222 gnu_address = TREE_OPERAND (gnu_address, 0);
1223 /* This is the pattern built for an overaligned object. */
1224 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1225 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1226 == PLUS_EXPR
1227 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1228 == off)
1229 gnu_address
1230 = build2 (POINTER_PLUS_EXPR, gnu_type,
1231 TREE_OPERAND (gnu_address, 0),
1232 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1233 else
1235 post_error_ne ("aliased object& with unconstrained array "
1236 "nominal subtype", gnat_clause,
1237 gnat_entity);
1238 post_error ("\\can overlay only aliased object with "
1239 "compatible subtype", gnat_clause);
1243 /* If we don't have an initializing expression for the underlying
1244 variable, the initializing expression for the pointer is the
1245 specified address. Otherwise, we have to make a COMPOUND_EXPR
1246 to assign both the address and the initial value. */
1247 if (!gnu_expr)
1248 gnu_expr = gnu_address;
1249 else
1250 gnu_expr
1251 = build2 (COMPOUND_EXPR, gnu_type,
1252 build_binary_op (INIT_EXPR, NULL_TREE,
1253 build_unary_op (INDIRECT_REF,
1254 NULL_TREE,
1255 gnu_address),
1256 gnu_expr),
1257 gnu_address);
1260 /* If it has an address clause and we are not defining it, mark it
1261 as an indirect object. Likewise for Stdcall objects that are
1262 imported. */
1263 if ((!definition && Present (Address_Clause (gnat_entity)))
1264 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1266 /* Convert the type of the object to a reference type that can
1267 alias everything as per RM 13.3(19). */
1268 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1269 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1270 gnu_type
1271 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1272 used_by_ref = true;
1273 const_flag = false;
1274 volatile_flag = false;
1275 gnu_size = NULL_TREE;
1277 /* No point in taking the address of an initializing expression
1278 that isn't going to be used. */
1279 gnu_expr = NULL_TREE;
1281 /* If it has an address clause whose value is known at compile
1282 time, make the object a CONST_DECL. This will avoid a
1283 useless dereference. */
1284 if (Present (Address_Clause (gnat_entity)))
1286 Node_Id gnat_address
1287 = Expression (Address_Clause (gnat_entity));
1289 if (compile_time_known_address_p (gnat_address))
1291 gnu_expr = gnat_to_gnu (gnat_address);
1292 const_flag = true;
1297 /* If we are at top level and this object is of variable size,
1298 make the actual type a hidden pointer to the real type and
1299 make the initializer be a memory allocation and initialization.
1300 Likewise for objects we aren't defining (presumed to be
1301 external references from other packages), but there we do
1302 not set up an initialization.
1304 If the object's size overflows, make an allocator too, so that
1305 Storage_Error gets raised. Note that we will never free
1306 such memory, so we presume it never will get allocated. */
1307 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1308 global_bindings_p ()
1309 || !definition
1310 || static_flag)
1311 || (gnu_size
1312 && !allocatable_size_p (convert (sizetype,
1313 size_binop
1314 (CEIL_DIV_EXPR, gnu_size,
1315 bitsize_unit_node)),
1316 global_bindings_p ()
1317 || !definition
1318 || static_flag)))
1320 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1321 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1322 gnu_type = build_reference_type (gnu_type);
1323 used_by_ref = true;
1324 const_flag = true;
1325 volatile_flag = false;
1326 gnu_size = NULL_TREE;
1328 /* In case this was a aliased object whose nominal subtype is
1329 unconstrained, the pointer above will be a thin pointer and
1330 build_allocator will automatically make the template.
1332 If we have a template initializer only (that we made above),
1333 pretend there is none and rely on what build_allocator creates
1334 again anyway. Otherwise (if we have a full initializer), get
1335 the data part and feed that to build_allocator.
1337 If we are elaborating a mutable object, tell build_allocator to
1338 ignore a possibly simpler size from the initializer, if any, as
1339 we must allocate the maximum possible size in this case. */
1340 if (definition && !imported_p)
1342 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1344 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1345 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1347 gnu_alloc_type
1348 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1350 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1351 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1352 gnu_expr = NULL_TREE;
1353 else
1354 gnu_expr
1355 = build_component_ref
1356 (gnu_expr,
1357 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1358 false);
1361 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1362 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1363 post_error ("?`Storage_Error` will be raised at run time!",
1364 gnat_entity);
1366 gnu_expr
1367 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1368 Empty, Empty, gnat_entity, mutable_p);
1370 else
1371 gnu_expr = NULL_TREE;
1374 /* If this object would go into the stack and has an alignment larger
1375 than the largest stack alignment the back-end can honor, resort to
1376 a variable of "aligning type". */
1377 if (definition
1378 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1379 && !imported_p
1380 && !static_flag
1381 && !global_bindings_p ())
1383 /* Create the new variable. No need for extra room before the
1384 aligned field as this is in automatic storage. */
1385 tree gnu_new_type
1386 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1387 TYPE_SIZE_UNIT (gnu_type),
1388 BIGGEST_ALIGNMENT, 0, gnat_entity);
1389 tree gnu_new_var
1390 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1391 NULL_TREE, gnu_new_type, NULL_TREE,
1392 false, false, false, false, false,
1393 true, debug_info_p, NULL, gnat_entity);
1395 /* Initialize the aligned field if we have an initializer. */
1396 if (gnu_expr)
1397 add_stmt_with_node
1398 (build_binary_op (INIT_EXPR, NULL_TREE,
1399 build_component_ref
1400 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1401 false),
1402 gnu_expr),
1403 gnat_entity);
1405 /* And setup this entity as a reference to the aligned field. */
1406 gnu_type = build_reference_type (gnu_type);
1407 gnu_expr
1408 = build_unary_op
1409 (ADDR_EXPR, NULL_TREE,
1410 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1411 false));
1412 TREE_CONSTANT (gnu_expr) = 1;
1414 used_by_ref = true;
1415 const_flag = true;
1416 volatile_flag = false;
1417 gnu_size = NULL_TREE;
1420 /* If this is an aliased object with an unconstrained array nominal
1421 subtype, we make its type a thin reference, i.e. the reference
1422 counterpart of a thin pointer, so it points to the array part.
1423 This is aimed to make it easier for the debugger to decode the
1424 object. Note that we have to do it this late because of the
1425 couple of allocation adjustments that might be made above. */
1426 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1427 && Is_Array_Type (Underlying_Type (gnat_type))
1428 && !type_annotate_only)
1430 /* In case the object with the template has already been allocated
1431 just above, we have nothing to do here. */
1432 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1434 /* This variable is a GNAT encoding used by Workbench: let it
1435 go through the debugging information but mark it as
1436 artificial: users are not interested in it. */
1437 tree gnu_unc_var
1438 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1439 NULL_TREE, gnu_type, gnu_expr,
1440 const_flag, Is_Public (gnat_entity),
1441 imported_p || !definition, static_flag,
1442 volatile_flag, true, debug_info_p,
1443 NULL, gnat_entity);
1444 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1445 TREE_CONSTANT (gnu_expr) = 1;
1447 used_by_ref = true;
1448 const_flag = true;
1449 volatile_flag = false;
1450 inner_const_flag = TREE_READONLY (gnu_unc_var);
1451 gnu_size = NULL_TREE;
1454 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1455 gnu_type
1456 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1459 /* Convert the expression to the type of the object if need be. */
1460 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1461 gnu_expr = convert (gnu_type, gnu_expr);
1463 /* If this name is external or a name was specified, use it, but don't
1464 use the Interface_Name with an address clause (see cd30005). */
1465 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1466 || (Present (Interface_Name (gnat_entity))
1467 && No (Address_Clause (gnat_entity))))
1468 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1470 /* If this is an aggregate constant initialized to a constant, force it
1471 to be statically allocated. This saves an initialization copy. */
1472 if (!static_flag
1473 && const_flag
1474 && gnu_expr && TREE_CONSTANT (gnu_expr)
1475 && AGGREGATE_TYPE_P (gnu_type)
1476 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1477 && !(TYPE_IS_PADDING_P (gnu_type)
1478 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1479 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1480 static_flag = true;
1482 /* Deal with a pragma Linker_Section on a constant or variable. */
1483 if ((kind == E_Constant || kind == E_Variable)
1484 && Present (Linker_Section_Pragma (gnat_entity)))
1485 prepend_one_attribute_pragma (&attr_list,
1486 Linker_Section_Pragma (gnat_entity));
1488 /* Now create the variable or the constant and set various flags. */
1489 gnu_decl
1490 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1491 gnu_expr, const_flag, Is_Public (gnat_entity),
1492 imported_p || !definition, static_flag,
1493 volatile_flag, artificial_p, debug_info_p,
1494 attr_list, gnat_entity, !renamed_obj);
1495 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1496 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1497 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1499 /* If we are defining an Out parameter and optimization isn't enabled,
1500 create a fake PARM_DECL for debugging purposes and make it point to
1501 the VAR_DECL. Suppress debug info for the latter but make sure it
1502 will live in memory so that it can be accessed from within the
1503 debugger through the PARM_DECL. */
1504 if (kind == E_Out_Parameter
1505 && definition
1506 && debug_info_p
1507 && !optimize
1508 && !flag_generate_lto)
1510 tree param = create_param_decl (gnu_entity_name, gnu_type);
1511 gnat_pushdecl (param, gnat_entity);
1512 SET_DECL_VALUE_EXPR (param, gnu_decl);
1513 DECL_HAS_VALUE_EXPR_P (param) = 1;
1514 DECL_IGNORED_P (gnu_decl) = 1;
1515 TREE_ADDRESSABLE (gnu_decl) = 1;
1518 /* If this is a loop parameter, set the corresponding flag. */
1519 else if (kind == E_Loop_Parameter)
1520 DECL_LOOP_PARM_P (gnu_decl) = 1;
1522 /* If this is a renaming pointer, attach the renamed object to it. */
1523 if (renamed_obj)
1524 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1526 /* If this is a constant and we are defining it or it generates a real
1527 symbol at the object level and we are referencing it, we may want
1528 or need to have a true variable to represent it:
1529 - if optimization isn't enabled, for debugging purposes,
1530 - if the constant is public and not overlaid on something else,
1531 - if its address is taken,
1532 - if either itself or its type is aliased. */
1533 if (TREE_CODE (gnu_decl) == CONST_DECL
1534 && (definition || Sloc (gnat_entity) > Standard_Location)
1535 && ((!optimize && debug_info_p)
1536 || (Is_Public (gnat_entity)
1537 && No (Address_Clause (gnat_entity)))
1538 || Address_Taken (gnat_entity)
1539 || Is_Aliased (gnat_entity)
1540 || Is_Aliased (gnat_type)))
1542 tree gnu_corr_var
1543 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1544 gnu_expr, true, Is_Public (gnat_entity),
1545 !definition, static_flag, volatile_flag,
1546 artificial_p, debug_info_p, attr_list,
1547 gnat_entity, false);
1549 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1552 /* If this is a constant, even if we don't need a true variable, we
1553 may need to avoid returning the initializer in every case. That
1554 can happen for the address of a (constant) constructor because,
1555 upon dereferencing it, the constructor will be reinjected in the
1556 tree, which may not be valid in every case; see lvalue_required_p
1557 for more details. */
1558 if (TREE_CODE (gnu_decl) == CONST_DECL)
1559 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1561 /* If this object is declared in a block that contains a block with an
1562 exception handler, and we aren't using the GCC exception mechanism,
1563 we must force this variable in memory in order to avoid an invalid
1564 optimization. */
1565 if (Front_End_Exceptions ()
1566 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1567 TREE_ADDRESSABLE (gnu_decl) = 1;
1569 /* If this is a local variable with non-BLKmode and aggregate type,
1570 and optimization isn't enabled, then force it in memory so that
1571 a register won't be allocated to it with possible subparts left
1572 uninitialized and reaching the register allocator. */
1573 else if (TREE_CODE (gnu_decl) == VAR_DECL
1574 && !DECL_EXTERNAL (gnu_decl)
1575 && !TREE_STATIC (gnu_decl)
1576 && DECL_MODE (gnu_decl) != BLKmode
1577 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1578 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1579 && !optimize)
1580 TREE_ADDRESSABLE (gnu_decl) = 1;
1582 /* If we are defining an object with variable size or an object with
1583 fixed size that will be dynamically allocated, and we are using the
1584 front-end setjmp/longjmp exception mechanism, update the setjmp
1585 buffer. */
1586 if (definition
1587 && Exception_Mechanism == Front_End_SJLJ
1588 && get_block_jmpbuf_decl ()
1589 && DECL_SIZE_UNIT (gnu_decl)
1590 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1591 || (flag_stack_check == GENERIC_STACK_CHECK
1592 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1593 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1594 add_stmt_with_node (build_call_n_expr
1595 (update_setjmp_buf_decl, 1,
1596 build_unary_op (ADDR_EXPR, NULL_TREE,
1597 get_block_jmpbuf_decl ())),
1598 gnat_entity);
1600 /* Back-annotate Esize and Alignment of the object if not already
1601 known. Note that we pick the values of the type, not those of
1602 the object, to shield ourselves from low-level platform-dependent
1603 adjustments like alignment promotion. This is both consistent with
1604 all the treatment above, where alignment and size are set on the
1605 type of the object and not on the object directly, and makes it
1606 possible to support all confirming representation clauses. */
1607 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1608 used_by_ref);
1610 break;
1612 case E_Void:
1613 /* Return a TYPE_DECL for "void" that we previously made. */
1614 gnu_decl = TYPE_NAME (void_type_node);
1615 break;
1617 case E_Enumeration_Type:
1618 /* A special case: for the types Character and Wide_Character in
1619 Standard, we do not list all the literals. So if the literals
1620 are not specified, make this an integer type. */
1621 if (No (First_Literal (gnat_entity)))
1623 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1624 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1625 else
1626 gnu_type = make_unsigned_type (esize);
1627 TYPE_NAME (gnu_type) = gnu_entity_name;
1629 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1630 This is needed by the DWARF-2 back-end to distinguish between
1631 unsigned integer types and character types. */
1632 TYPE_STRING_FLAG (gnu_type) = 1;
1634 /* This flag is needed by the call just below. */
1635 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1637 finish_character_type (gnu_type);
1639 else
1641 /* We have a list of enumeral constants in First_Literal. We make a
1642 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1643 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1644 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1645 value of the literal. But when we have a regular boolean type, we
1646 simplify this a little by using a BOOLEAN_TYPE. */
1647 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1648 && !Has_Non_Standard_Rep (gnat_entity);
1649 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1650 tree gnu_list = NULL_TREE;
1651 Entity_Id gnat_literal;
1653 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1654 TYPE_PRECISION (gnu_type) = esize;
1655 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1656 set_min_and_max_values_for_integral_type (gnu_type, esize,
1657 TYPE_SIGN (gnu_type));
1658 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1659 layout_type (gnu_type);
1661 for (gnat_literal = First_Literal (gnat_entity);
1662 Present (gnat_literal);
1663 gnat_literal = Next_Literal (gnat_literal))
1665 tree gnu_value
1666 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1667 /* Do not generate debug info for individual enumerators. */
1668 tree gnu_literal
1669 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1670 gnu_type, gnu_value, true, false, false,
1671 false, false, artificial_p, false,
1672 NULL, gnat_literal);
1673 save_gnu_tree (gnat_literal, gnu_literal, false);
1674 gnu_list
1675 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1678 if (!is_boolean)
1679 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1681 /* Note that the bounds are updated at the end of this function
1682 to avoid an infinite recursion since they refer to the type. */
1683 goto discrete_type;
1685 break;
1687 case E_Signed_Integer_Type:
1688 /* For integer types, just make a signed type the appropriate number
1689 of bits. */
1690 gnu_type = make_signed_type (esize);
1691 goto discrete_type;
1693 case E_Ordinary_Fixed_Point_Type:
1694 case E_Decimal_Fixed_Point_Type:
1696 /* Small_Value is the scale factor. */
1697 const Ureal gnat_small_value = Small_Value (gnat_entity);
1698 tree scale_factor = NULL_TREE;
1700 gnu_type = make_signed_type (esize);
1702 /* Try to decode the scale factor and to save it for the fixed-point
1703 types debug hook. */
1705 /* There are various ways to describe the scale factor, however there
1706 are cases where back-end internals cannot hold it. In such cases,
1707 we output invalid scale factor for such cases (i.e. the 0/0
1708 rational constant) but we expect GNAT to output GNAT encodings,
1709 then. Thus, keep this in sync with
1710 Exp_Dbug.Is_Handled_Scale_Factor. */
1712 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1713 binary or decimal scale: it is easier to read for humans. */
1714 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1715 && (Rbase (gnat_small_value) == 2
1716 || Rbase (gnat_small_value) == 10))
1718 /* Given RM restrictions on 'Small values, we assume here that
1719 the denominator fits in an int. */
1720 const tree base = build_int_cst (integer_type_node,
1721 Rbase (gnat_small_value));
1722 const tree exponent
1723 = build_int_cst (integer_type_node,
1724 UI_To_Int (Denominator (gnat_small_value)));
1725 scale_factor
1726 = build2 (RDIV_EXPR, integer_type_node,
1727 integer_one_node,
1728 build2 (POWER_EXPR, integer_type_node,
1729 base, exponent));
1732 /* Default to arbitrary scale factors descriptions. */
1733 else
1735 const Uint num = Norm_Num (gnat_small_value);
1736 const Uint den = Norm_Den (gnat_small_value);
1738 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1740 const tree gnu_num
1741 = build_int_cst (integer_type_node,
1742 UI_To_Int (Norm_Num (gnat_small_value)));
1743 const tree gnu_den
1744 = build_int_cst (integer_type_node,
1745 UI_To_Int (Norm_Den (gnat_small_value)));
1746 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1747 gnu_num, gnu_den);
1749 else
1750 /* If compiler internals cannot represent arbitrary scale
1751 factors, output an invalid scale factor so that debugger
1752 don't try to handle them but so that we still have a type
1753 in the output. Note that GNAT */
1754 scale_factor = integer_zero_node;
1757 TYPE_FIXED_POINT_P (gnu_type) = 1;
1758 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1760 goto discrete_type;
1762 case E_Modular_Integer_Type:
1764 /* For modular types, make the unsigned type of the proper number
1765 of bits and then set up the modulus, if required. */
1766 tree gnu_modulus, gnu_high = NULL_TREE;
1768 /* Packed Array Impl. Types are supposed to be subtypes only. */
1769 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1771 gnu_type = make_unsigned_type (esize);
1773 /* Get the modulus in this type. If it overflows, assume it is because
1774 it is equal to 2**Esize. Note that there is no overflow checking
1775 done on unsigned type, so we detect the overflow by looking for
1776 a modulus of zero, which is otherwise invalid. */
1777 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1779 if (!integer_zerop (gnu_modulus))
1781 TYPE_MODULAR_P (gnu_type) = 1;
1782 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1783 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1784 build_int_cst (gnu_type, 1));
1787 /* If the upper bound is not maximal, make an extra subtype. */
1788 if (gnu_high
1789 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1791 tree gnu_subtype = make_unsigned_type (esize);
1792 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1793 TREE_TYPE (gnu_subtype) = gnu_type;
1794 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1795 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1796 gnu_type = gnu_subtype;
1799 goto discrete_type;
1801 case E_Signed_Integer_Subtype:
1802 case E_Enumeration_Subtype:
1803 case E_Modular_Integer_Subtype:
1804 case E_Ordinary_Fixed_Point_Subtype:
1805 case E_Decimal_Fixed_Point_Subtype:
1807 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1808 not want to call create_range_type since we would like each subtype
1809 node to be distinct. ??? Historically this was in preparation for
1810 when memory aliasing is implemented, but that's obsolete now given
1811 the call to relate_alias_sets below.
1813 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1814 this fact is used by the arithmetic conversion functions.
1816 We elaborate the Ancestor_Subtype if it is not in the current unit
1817 and one of our bounds is non-static. We do this to ensure consistent
1818 naming in the case where several subtypes share the same bounds, by
1819 elaborating the first such subtype first, thus using its name. */
1821 if (!definition
1822 && Present (Ancestor_Subtype (gnat_entity))
1823 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1824 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1825 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1826 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1828 /* Set the precision to the Esize except for bit-packed arrays. */
1829 if (Is_Packed_Array_Impl_Type (gnat_entity)
1830 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1831 esize = UI_To_Int (RM_Size (gnat_entity));
1833 /* First subtypes of Character are treated as Character; otherwise
1834 this should be an unsigned type if the base type is unsigned or
1835 if the lower bound is constant and non-negative or if the type
1836 is biased. However, even if the lower bound is constant and
1837 non-negative, we use a signed type for a subtype with the same
1838 size as its signed base type, because this eliminates useless
1839 conversions to it and gives more leeway to the optimizer; but
1840 this means that we will need to explicitly test for this case
1841 when we change the representation based on the RM size. */
1842 if (kind == E_Enumeration_Subtype
1843 && No (First_Literal (Etype (gnat_entity)))
1844 && Esize (gnat_entity) == RM_Size (gnat_entity)
1845 && esize == CHAR_TYPE_SIZE
1846 && flag_signed_char)
1847 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1848 else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity)))
1849 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1850 && Is_Unsigned_Type (gnat_entity))
1851 || Has_Biased_Representation (gnat_entity))
1852 gnu_type = make_unsigned_type (esize);
1853 else
1854 gnu_type = make_signed_type (esize);
1855 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1857 SET_TYPE_RM_MIN_VALUE
1858 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1859 gnat_entity, "L", definition, true,
1860 debug_info_p));
1862 SET_TYPE_RM_MAX_VALUE
1863 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1864 gnat_entity, "U", definition, true,
1865 debug_info_p));
1867 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1868 = Has_Biased_Representation (gnat_entity);
1870 /* Do the same processing for Character subtypes as for types. */
1871 if (TYPE_STRING_FLAG (TREE_TYPE (gnu_type)))
1873 TYPE_NAME (gnu_type) = gnu_entity_name;
1874 TYPE_STRING_FLAG (gnu_type) = 1;
1875 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1876 finish_character_type (gnu_type);
1879 /* Inherit our alias set from what we're a subtype of. Subtypes
1880 are not different types and a pointer can designate any instance
1881 within a subtype hierarchy. */
1882 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1884 /* One of the above calls might have caused us to be elaborated,
1885 so don't blow up if so. */
1886 if (present_gnu_tree (gnat_entity))
1888 maybe_present = true;
1889 break;
1892 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1893 TYPE_STUB_DECL (gnu_type)
1894 = create_type_stub_decl (gnu_entity_name, gnu_type);
1896 /* For a packed array, make the original array type a parallel/debug
1897 type. */
1898 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1899 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1901 discrete_type:
1903 /* We have to handle clauses that under-align the type specially. */
1904 if ((Present (Alignment_Clause (gnat_entity))
1905 || (Is_Packed_Array_Impl_Type (gnat_entity)
1906 && Present
1907 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1908 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1910 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1911 if (align >= TYPE_ALIGN (gnu_type))
1912 align = 0;
1915 /* If the type we are dealing with represents a bit-packed array,
1916 we need to have the bits left justified on big-endian targets
1917 and right justified on little-endian targets. We also need to
1918 ensure that when the value is read (e.g. for comparison of two
1919 such values), we only get the good bits, since the unused bits
1920 are uninitialized. Both goals are accomplished by wrapping up
1921 the modular type in an enclosing record type. */
1922 if (Is_Packed_Array_Impl_Type (gnat_entity)
1923 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1925 tree gnu_field_type, gnu_field;
1927 /* Set the RM size before wrapping up the original type. */
1928 SET_TYPE_RM_SIZE (gnu_type,
1929 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1930 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1932 /* Strip the ___XP suffix for standard DWARF. */
1933 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1934 gnu_entity_name = TYPE_NAME (gnu_type);
1936 /* Create a stripped-down declaration, mainly for debugging. */
1937 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1938 gnat_entity);
1940 /* Now save it and build the enclosing record type. */
1941 gnu_field_type = gnu_type;
1943 gnu_type = make_node (RECORD_TYPE);
1944 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1945 TYPE_PACKED (gnu_type) = 1;
1946 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1947 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1948 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1950 /* Propagate the alignment of the modular type to the record type,
1951 unless there is an alignment clause that under-aligns the type.
1952 This means that bit-packed arrays are given "ceil" alignment for
1953 their size by default, which may seem counter-intuitive but makes
1954 it possible to overlay them on modular types easily. */
1955 SET_TYPE_ALIGN (gnu_type,
1956 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1958 /* Propagate the reverse storage order flag to the record type so
1959 that the required byte swapping is performed when retrieving the
1960 enclosed modular value. */
1961 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1962 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1964 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1966 /* Don't declare the field as addressable since we won't be taking
1967 its address and this would prevent create_field_decl from making
1968 a bitfield. */
1969 gnu_field
1970 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1971 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1973 /* We will output additional debug info manually below. */
1974 finish_record_type (gnu_type, gnu_field, 2, false);
1975 compute_record_mode (gnu_type);
1976 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1978 if (debug_info_p)
1980 /* Make the original array type a parallel/debug type. */
1981 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1983 /* Since GNU_TYPE is a padding type around the packed array
1984 implementation type, the padded type is its debug type. */
1985 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1986 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1990 /* If the type we are dealing with has got a smaller alignment than the
1991 natural one, we need to wrap it up in a record type and misalign the
1992 latter; we reuse the padding machinery for this purpose. */
1993 else if (align > 0)
1995 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1997 /* Set the RM size before wrapping the type. */
1998 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
2000 gnu_type
2001 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
2002 gnat_entity, false, true, definition, false);
2004 TYPE_PACKED (gnu_type) = 1;
2005 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
2008 break;
2010 case E_Floating_Point_Type:
2011 /* The type of the Low and High bounds can be our type if this is
2012 a type from Standard, so set them at the end of the function. */
2013 gnu_type = make_node (REAL_TYPE);
2014 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2015 layout_type (gnu_type);
2016 break;
2018 case E_Floating_Point_Subtype:
2019 /* See the E_Signed_Integer_Subtype case for the rationale. */
2020 if (!definition
2021 && Present (Ancestor_Subtype (gnat_entity))
2022 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
2023 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
2024 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
2025 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
2027 gnu_type = make_node (REAL_TYPE);
2028 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2029 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2030 TYPE_GCC_MIN_VALUE (gnu_type)
2031 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2032 TYPE_GCC_MAX_VALUE (gnu_type)
2033 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2034 layout_type (gnu_type);
2036 SET_TYPE_RM_MIN_VALUE
2037 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2038 gnat_entity, "L", definition, true,
2039 debug_info_p));
2041 SET_TYPE_RM_MAX_VALUE
2042 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2043 gnat_entity, "U", definition, true,
2044 debug_info_p));
2046 /* Inherit our alias set from what we're a subtype of, as for
2047 integer subtypes. */
2048 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2050 /* One of the above calls might have caused us to be elaborated,
2051 so don't blow up if so. */
2052 maybe_present = true;
2053 break;
2055 /* Array Types and Subtypes
2057 Unconstrained array types are represented by E_Array_Type and
2058 constrained array types are represented by E_Array_Subtype. There
2059 are no actual objects of an unconstrained array type; all we have
2060 are pointers to that type.
2062 The following fields are defined on array types and subtypes:
2064 Component_Type Component type of the array.
2065 Number_Dimensions Number of dimensions (an int).
2066 First_Index Type of first index. */
2068 case E_Array_Type:
2070 const bool convention_fortran_p
2071 = (Convention (gnat_entity) == Convention_Fortran);
2072 const int ndim = Number_Dimensions (gnat_entity);
2073 tree gnu_template_type;
2074 tree gnu_ptr_template;
2075 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2076 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2077 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2078 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2079 Entity_Id gnat_index, gnat_name;
2080 int index;
2081 tree comp_type;
2083 /* Create the type for the component now, as it simplifies breaking
2084 type reference loops. */
2085 comp_type
2086 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2087 if (present_gnu_tree (gnat_entity))
2089 /* As a side effect, the type may have been translated. */
2090 maybe_present = true;
2091 break;
2094 /* We complete an existing dummy fat pointer type in place. This both
2095 avoids further complex adjustments in update_pointer_to and yields
2096 better debugging information in DWARF by leveraging the support for
2097 incomplete declarations of "tagged" types in the DWARF back-end. */
2098 gnu_type = get_dummy_type (gnat_entity);
2099 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2101 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2102 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2103 /* Save the contents of the dummy type for update_pointer_to. */
2104 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2105 gnu_ptr_template =
2106 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2107 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2109 else
2111 gnu_fat_type = make_node (RECORD_TYPE);
2112 gnu_template_type = make_node (RECORD_TYPE);
2113 gnu_ptr_template = build_pointer_type (gnu_template_type);
2116 /* Make a node for the array. If we are not defining the array
2117 suppress expanding incomplete types. */
2118 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2120 if (!definition)
2122 defer_incomplete_level++;
2123 this_deferred = true;
2126 /* Build the fat pointer type. Use a "void *" object instead of
2127 a pointer to the array type since we don't have the array type
2128 yet (it will reference the fat pointer via the bounds). */
2130 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2131 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2132 DECL_CHAIN (tem)
2133 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2134 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2136 if (COMPLETE_TYPE_P (gnu_fat_type))
2138 /* We are going to lay it out again so reset the alias set. */
2139 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2140 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2141 finish_fat_pointer_type (gnu_fat_type, tem);
2142 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2143 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2145 TYPE_FIELDS (t) = tem;
2146 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2149 else
2151 finish_fat_pointer_type (gnu_fat_type, tem);
2152 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2155 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2156 is the fat pointer. This will be used to access the individual
2157 fields once we build them. */
2158 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2159 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2160 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2161 gnu_template_reference
2162 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2163 TREE_READONLY (gnu_template_reference) = 1;
2164 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2166 /* Now create the GCC type for each index and add the fields for that
2167 index to the template. */
2168 for (index = (convention_fortran_p ? ndim - 1 : 0),
2169 gnat_index = First_Index (gnat_entity);
2170 0 <= index && index < ndim;
2171 index += (convention_fortran_p ? - 1 : 1),
2172 gnat_index = Next_Index (gnat_index))
2174 char field_name[16];
2175 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2176 tree gnu_index_base_type
2177 = maybe_character_type (get_base_type (gnu_index_type));
2178 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2179 tree gnu_min, gnu_max, gnu_high;
2181 /* Make the FIELD_DECLs for the low and high bounds of this
2182 type and then make extractions of these fields from the
2183 template. */
2184 sprintf (field_name, "LB%d", index);
2185 gnu_lb_field = create_field_decl (get_identifier (field_name),
2186 gnu_index_base_type,
2187 gnu_template_type, NULL_TREE,
2188 NULL_TREE, 0, 0);
2189 Sloc_to_locus (Sloc (gnat_entity),
2190 &DECL_SOURCE_LOCATION (gnu_lb_field));
2192 field_name[0] = 'U';
2193 gnu_hb_field = create_field_decl (get_identifier (field_name),
2194 gnu_index_base_type,
2195 gnu_template_type, NULL_TREE,
2196 NULL_TREE, 0, 0);
2197 Sloc_to_locus (Sloc (gnat_entity),
2198 &DECL_SOURCE_LOCATION (gnu_hb_field));
2200 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2202 /* We can't use build_component_ref here since the template type
2203 isn't complete yet. */
2204 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2205 gnu_template_reference, gnu_lb_field,
2206 NULL_TREE);
2207 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2208 gnu_template_reference, gnu_hb_field,
2209 NULL_TREE);
2210 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2212 gnu_min = convert (sizetype, gnu_orig_min);
2213 gnu_max = convert (sizetype, gnu_orig_max);
2215 /* Compute the size of this dimension. See the E_Array_Subtype
2216 case below for the rationale. */
2217 gnu_high
2218 = build3 (COND_EXPR, sizetype,
2219 build2 (GE_EXPR, boolean_type_node,
2220 gnu_orig_max, gnu_orig_min),
2221 gnu_max,
2222 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2224 /* Make a range type with the new range in the Ada base type.
2225 Then make an index type with the size range in sizetype. */
2226 gnu_index_types[index]
2227 = create_index_type (gnu_min, gnu_high,
2228 create_range_type (gnu_index_base_type,
2229 gnu_orig_min,
2230 gnu_orig_max),
2231 gnat_entity);
2233 /* Update the maximum size of the array in elements. */
2234 if (gnu_max_size)
2236 tree gnu_min
2237 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2238 tree gnu_max
2239 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2240 tree gnu_this_max
2241 = size_binop (PLUS_EXPR, size_one_node,
2242 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2244 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2245 && TREE_OVERFLOW (gnu_this_max))
2246 gnu_max_size = NULL_TREE;
2247 else
2248 gnu_max_size
2249 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2252 TYPE_NAME (gnu_index_types[index])
2253 = create_concat_name (gnat_entity, field_name);
2256 /* Install all the fields into the template. */
2257 TYPE_NAME (gnu_template_type)
2258 = create_concat_name (gnat_entity, "XUB");
2259 gnu_template_fields = NULL_TREE;
2260 for (index = 0; index < ndim; index++)
2261 gnu_template_fields
2262 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2263 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2264 debug_info_p);
2265 TYPE_READONLY (gnu_template_type) = 1;
2267 /* If Component_Size is not already specified, annotate it with the
2268 size of the component. */
2269 if (Unknown_Component_Size (gnat_entity))
2270 Set_Component_Size (gnat_entity,
2271 annotate_value (TYPE_SIZE (comp_type)));
2273 /* Compute the maximum size of the array in units and bits. */
2274 if (gnu_max_size)
2276 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2277 TYPE_SIZE_UNIT (comp_type));
2278 gnu_max_size = size_binop (MULT_EXPR,
2279 convert (bitsizetype, gnu_max_size),
2280 TYPE_SIZE (comp_type));
2282 else
2283 gnu_max_size_unit = NULL_TREE;
2285 /* Now build the array type. */
2286 tem = comp_type;
2287 for (index = ndim - 1; index >= 0; index--)
2289 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2290 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2291 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2292 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2293 set_reverse_storage_order_on_array_type (tem);
2294 if (array_type_has_nonaliased_component (tem, gnat_entity))
2295 set_nonaliased_component_on_array_type (tem);
2298 /* If an alignment is specified, use it if valid. But ignore it
2299 for the original type of packed array types. If the alignment
2300 was requested with an explicit alignment clause, state so. */
2301 if (No (Packed_Array_Impl_Type (gnat_entity))
2302 && Known_Alignment (gnat_entity))
2304 SET_TYPE_ALIGN (tem,
2305 validate_alignment (Alignment (gnat_entity),
2306 gnat_entity,
2307 TYPE_ALIGN (tem)));
2308 if (Present (Alignment_Clause (gnat_entity)))
2309 TYPE_USER_ALIGN (tem) = 1;
2312 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2313 implementation types as such so that the debug information back-end
2314 can output the appropriate description for them. */
2315 TYPE_PACKED (tem)
2316 = (Is_Packed (gnat_entity)
2317 || Is_Packed_Array_Impl_Type (gnat_entity));
2319 if (Treat_As_Volatile (gnat_entity))
2320 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2322 /* Adjust the type of the pointer-to-array field of the fat pointer
2323 and record the aliasing relationships if necessary. */
2324 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2325 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2326 record_component_aliases (gnu_fat_type);
2328 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2329 corresponding fat pointer. */
2330 TREE_TYPE (gnu_type) = gnu_fat_type;
2331 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2332 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2333 SET_TYPE_MODE (gnu_type, BLKmode);
2334 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2336 /* If the maximum size doesn't overflow, use it. */
2337 if (gnu_max_size
2338 && TREE_CODE (gnu_max_size) == INTEGER_CST
2339 && !TREE_OVERFLOW (gnu_max_size)
2340 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2341 && !TREE_OVERFLOW (gnu_max_size_unit))
2343 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2344 TYPE_SIZE (tem));
2345 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2346 TYPE_SIZE_UNIT (tem));
2349 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2350 artificial_p, debug_info_p, gnat_entity);
2352 /* If told to generate GNAT encodings for them (GDB rely on them at the
2353 moment): give the fat pointer type a name. If this is a packed
2354 array, tell the debugger how to interpret the underlying bits. */
2355 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2356 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2357 else
2358 gnat_name = gnat_entity;
2359 tree xup_name
2360 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2361 ? get_entity_name (gnat_name)
2362 : create_concat_name (gnat_name, "XUP");
2363 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2364 gnat_entity);
2366 /* Create the type to be designated by thin pointers: a record type for
2367 the array and its template. We used to shift the fields to have the
2368 template at a negative offset, but this was somewhat of a kludge; we
2369 now shift thin pointer values explicitly but only those which have a
2370 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2371 Note that GDB can handle standard DWARF information for them, so we
2372 don't have to name them as a GNAT encoding, except if specifically
2373 asked to. */
2374 tree xut_name
2375 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2376 ? get_entity_name (gnat_name)
2377 : create_concat_name (gnat_name, "XUT");
2378 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2379 debug_info_p);
2381 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2382 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2384 break;
2386 case E_Array_Subtype:
2388 /* This is the actual data type for array variables. Multidimensional
2389 arrays are implemented as arrays of arrays. Note that arrays which
2390 have sparse enumeration subtypes as index components create sparse
2391 arrays, which is obviously space inefficient but so much easier to
2392 code for now.
2394 Also note that the subtype never refers to the unconstrained array
2395 type, which is somewhat at variance with Ada semantics.
2397 First check to see if this is simply a renaming of the array type.
2398 If so, the result is the array type. */
2400 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2401 if (!Is_Constrained (gnat_entity))
2403 else
2405 Entity_Id gnat_index, gnat_base_index;
2406 const bool convention_fortran_p
2407 = (Convention (gnat_entity) == Convention_Fortran);
2408 const int ndim = Number_Dimensions (gnat_entity);
2409 tree gnu_base_type = gnu_type;
2410 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2411 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2412 bool need_index_type_struct = false;
2413 int index;
2415 /* First create the GCC type for each index and find out whether
2416 special types are needed for debugging information. */
2417 for (index = (convention_fortran_p ? ndim - 1 : 0),
2418 gnat_index = First_Index (gnat_entity),
2419 gnat_base_index
2420 = First_Index (Implementation_Base_Type (gnat_entity));
2421 0 <= index && index < ndim;
2422 index += (convention_fortran_p ? - 1 : 1),
2423 gnat_index = Next_Index (gnat_index),
2424 gnat_base_index = Next_Index (gnat_base_index))
2426 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2427 tree gnu_index_base_type
2428 = maybe_character_type (get_base_type (gnu_index_type));
2429 tree gnu_orig_min
2430 = convert (gnu_index_base_type,
2431 TYPE_MIN_VALUE (gnu_index_type));
2432 tree gnu_orig_max
2433 = convert (gnu_index_base_type,
2434 TYPE_MAX_VALUE (gnu_index_type));
2435 tree gnu_min = convert (sizetype, gnu_orig_min);
2436 tree gnu_max = convert (sizetype, gnu_orig_max);
2437 tree gnu_base_index_type
2438 = get_unpadded_type (Etype (gnat_base_index));
2439 tree gnu_base_index_base_type
2440 = maybe_character_type (get_base_type (gnu_base_index_type));
2441 tree gnu_base_orig_min
2442 = convert (gnu_base_index_base_type,
2443 TYPE_MIN_VALUE (gnu_base_index_type));
2444 tree gnu_base_orig_max
2445 = convert (gnu_base_index_base_type,
2446 TYPE_MAX_VALUE (gnu_base_index_type));
2447 tree gnu_high;
2449 /* See if the base array type is already flat. If it is, we
2450 are probably compiling an ACATS test but it will cause the
2451 code below to malfunction if we don't handle it specially. */
2452 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2453 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2454 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2456 gnu_min = size_one_node;
2457 gnu_max = size_zero_node;
2458 gnu_high = gnu_max;
2461 /* Similarly, if one of the values overflows in sizetype and the
2462 range is null, use 1..0 for the sizetype bounds. */
2463 else if (TREE_CODE (gnu_min) == INTEGER_CST
2464 && TREE_CODE (gnu_max) == INTEGER_CST
2465 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2466 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2468 gnu_min = size_one_node;
2469 gnu_max = size_zero_node;
2470 gnu_high = gnu_max;
2473 /* If the minimum and maximum values both overflow in sizetype,
2474 but the difference in the original type does not overflow in
2475 sizetype, ignore the overflow indication. */
2476 else if (TREE_CODE (gnu_min) == INTEGER_CST
2477 && TREE_CODE (gnu_max) == INTEGER_CST
2478 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2479 && !TREE_OVERFLOW
2480 (convert (sizetype,
2481 fold_build2 (MINUS_EXPR, gnu_index_type,
2482 gnu_orig_max,
2483 gnu_orig_min))))
2485 TREE_OVERFLOW (gnu_min) = 0;
2486 TREE_OVERFLOW (gnu_max) = 0;
2487 gnu_high = gnu_max;
2490 /* Compute the size of this dimension in the general case. We
2491 need to provide GCC with an upper bound to use but have to
2492 deal with the "superflat" case. There are three ways to do
2493 this. If we can prove that the array can never be superflat,
2494 we can just use the high bound of the index type. */
2495 else if ((Nkind (gnat_index) == N_Range
2496 && cannot_be_superflat (gnat_index))
2497 /* Bit-Packed Array Impl. Types are never superflat. */
2498 || (Is_Packed_Array_Impl_Type (gnat_entity)
2499 && Is_Bit_Packed_Array
2500 (Original_Array_Type (gnat_entity))))
2501 gnu_high = gnu_max;
2503 /* Otherwise, if the high bound is constant but the low bound is
2504 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2505 lower bound. Note that the comparison must be done in the
2506 original type to avoid any overflow during the conversion. */
2507 else if (TREE_CODE (gnu_max) == INTEGER_CST
2508 && TREE_CODE (gnu_min) != INTEGER_CST)
2510 gnu_high = gnu_max;
2511 gnu_min
2512 = build_cond_expr (sizetype,
2513 build_binary_op (GE_EXPR,
2514 boolean_type_node,
2515 gnu_orig_max,
2516 gnu_orig_min),
2517 gnu_min,
2518 int_const_binop (PLUS_EXPR, gnu_max,
2519 size_one_node));
2522 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2523 in all the other cases. Note that, here as well as above,
2524 the condition used in the comparison must be equivalent to
2525 the condition (length != 0). This is relied upon in order
2526 to optimize array comparisons in compare_arrays. Moreover
2527 we use int_const_binop for the shift by 1 if the bound is
2528 constant to avoid any unwanted overflow. */
2529 else
2530 gnu_high
2531 = build_cond_expr (sizetype,
2532 build_binary_op (GE_EXPR,
2533 boolean_type_node,
2534 gnu_orig_max,
2535 gnu_orig_min),
2536 gnu_max,
2537 TREE_CODE (gnu_min) == INTEGER_CST
2538 ? int_const_binop (MINUS_EXPR, gnu_min,
2539 size_one_node)
2540 : size_binop (MINUS_EXPR, gnu_min,
2541 size_one_node));
2543 /* Reuse the index type for the range type. Then make an index
2544 type with the size range in sizetype. */
2545 gnu_index_types[index]
2546 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2547 gnat_entity);
2549 /* Update the maximum size of the array in elements. Here we
2550 see if any constraint on the index type of the base type
2551 can be used in the case of self-referential bound on the
2552 index type of the subtype. We look for a non-"infinite"
2553 and non-self-referential bound from any type involved and
2554 handle each bound separately. */
2555 if (gnu_max_size)
2557 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2558 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2559 tree gnu_base_base_min
2560 = convert (sizetype,
2561 TYPE_MIN_VALUE (gnu_base_index_base_type));
2562 tree gnu_base_base_max
2563 = convert (sizetype,
2564 TYPE_MAX_VALUE (gnu_base_index_base_type));
2566 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2567 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2568 && !TREE_OVERFLOW (gnu_base_min)))
2569 gnu_base_min = gnu_min;
2571 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2572 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2573 && !TREE_OVERFLOW (gnu_base_max)))
2574 gnu_base_max = gnu_max;
2576 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2577 && TREE_OVERFLOW (gnu_base_min))
2578 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2579 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2580 && TREE_OVERFLOW (gnu_base_max))
2581 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2582 gnu_max_size = NULL_TREE;
2583 else
2585 tree gnu_this_max;
2587 /* Use int_const_binop if the bounds are constant to
2588 avoid any unwanted overflow. */
2589 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2590 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2591 gnu_this_max
2592 = int_const_binop (PLUS_EXPR, size_one_node,
2593 int_const_binop (MINUS_EXPR,
2594 gnu_base_max,
2595 gnu_base_min));
2596 else
2597 gnu_this_max
2598 = size_binop (PLUS_EXPR, size_one_node,
2599 size_binop (MINUS_EXPR,
2600 gnu_base_max,
2601 gnu_base_min));
2603 gnu_max_size
2604 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2608 /* We need special types for debugging information to point to
2609 the index types if they have variable bounds, are not integer
2610 types, are biased or are wider than sizetype. These are GNAT
2611 encodings, so we have to include them only when all encodings
2612 are requested. */
2613 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2614 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2615 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2616 || (TREE_TYPE (gnu_index_type)
2617 && TREE_CODE (TREE_TYPE (gnu_index_type))
2618 != INTEGER_TYPE)
2619 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2620 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2621 need_index_type_struct = true;
2624 /* Then flatten: create the array of arrays. For an array type
2625 used to implement a packed array, get the component type from
2626 the original array type since the representation clauses that
2627 can affect it are on the latter. */
2628 if (Is_Packed_Array_Impl_Type (gnat_entity)
2629 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2631 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2632 for (index = ndim - 1; index >= 0; index--)
2633 gnu_type = TREE_TYPE (gnu_type);
2635 /* One of the above calls might have caused us to be elaborated,
2636 so don't blow up if so. */
2637 if (present_gnu_tree (gnat_entity))
2639 maybe_present = true;
2640 break;
2643 else
2645 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2646 debug_info_p);
2648 /* One of the above calls might have caused us to be elaborated,
2649 so don't blow up if so. */
2650 if (present_gnu_tree (gnat_entity))
2652 maybe_present = true;
2653 break;
2657 /* Compute the maximum size of the array in units and bits. */
2658 if (gnu_max_size)
2660 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2661 TYPE_SIZE_UNIT (gnu_type));
2662 gnu_max_size = size_binop (MULT_EXPR,
2663 convert (bitsizetype, gnu_max_size),
2664 TYPE_SIZE (gnu_type));
2666 else
2667 gnu_max_size_unit = NULL_TREE;
2669 /* Now build the array type. */
2670 for (index = ndim - 1; index >= 0; index --)
2672 gnu_type = build_nonshared_array_type (gnu_type,
2673 gnu_index_types[index]);
2674 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2675 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2676 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2677 set_reverse_storage_order_on_array_type (gnu_type);
2678 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2679 set_nonaliased_component_on_array_type (gnu_type);
2682 /* Strip the ___XP suffix for standard DWARF. */
2683 if (Is_Packed_Array_Impl_Type (gnat_entity)
2684 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2686 Entity_Id gnat_original_array_type
2687 = Underlying_Type (Original_Array_Type (gnat_entity));
2689 gnu_entity_name
2690 = get_entity_name (gnat_original_array_type);
2693 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2694 TYPE_STUB_DECL (gnu_type)
2695 = create_type_stub_decl (gnu_entity_name, gnu_type);
2697 /* If this is a multi-dimensional array and we are at global level,
2698 we need to make a variable corresponding to the stride of the
2699 inner dimensions. */
2700 if (ndim > 1 && global_bindings_p ())
2702 tree gnu_arr_type;
2704 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2705 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2706 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2708 tree eltype = TREE_TYPE (gnu_arr_type);
2709 char stride_name[32];
2711 sprintf (stride_name, "ST%d", index);
2712 TYPE_SIZE (gnu_arr_type)
2713 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2714 gnat_entity, stride_name,
2715 definition, false);
2717 /* ??? For now, store the size as a multiple of the
2718 alignment of the element type in bytes so that we
2719 can see the alignment from the tree. */
2720 sprintf (stride_name, "ST%d_A_UNIT", index);
2721 TYPE_SIZE_UNIT (gnu_arr_type)
2722 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2723 gnat_entity, stride_name,
2724 definition, false,
2725 TYPE_ALIGN (eltype));
2727 /* ??? create_type_decl is not invoked on the inner types so
2728 the MULT_EXPR node built above will never be marked. */
2729 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2733 /* If we need to write out a record type giving the names of the
2734 bounds for debugging purposes, do it now and make the record
2735 type a parallel type. This is not needed for a packed array
2736 since the bounds are conveyed by the original array type. */
2737 if (need_index_type_struct
2738 && debug_info_p
2739 && !Is_Packed_Array_Impl_Type (gnat_entity))
2741 tree gnu_bound_rec = make_node (RECORD_TYPE);
2742 tree gnu_field_list = NULL_TREE;
2743 tree gnu_field;
2745 TYPE_NAME (gnu_bound_rec)
2746 = create_concat_name (gnat_entity, "XA");
2748 for (index = ndim - 1; index >= 0; index--)
2750 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2751 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2753 /* Make sure to reference the types themselves, and not just
2754 their names, as the debugger may fall back on them. */
2755 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2756 gnu_bound_rec, NULL_TREE,
2757 NULL_TREE, 0, 0);
2758 DECL_CHAIN (gnu_field) = gnu_field_list;
2759 gnu_field_list = gnu_field;
2762 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2763 add_parallel_type (gnu_type, gnu_bound_rec);
2766 /* If this is a packed array type, make the original array type a
2767 parallel/debug type. Otherwise, if such GNAT encodings are
2768 required, do it for the base array type if it isn't artificial to
2769 make sure it is kept in the debug info. */
2770 if (debug_info_p)
2772 if (Is_Packed_Array_Impl_Type (gnat_entity))
2773 associate_original_type_to_packed_array (gnu_type,
2774 gnat_entity);
2775 else
2777 tree gnu_base_decl
2778 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2779 false);
2780 if (!DECL_ARTIFICIAL (gnu_base_decl)
2781 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2782 add_parallel_type (gnu_type,
2783 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2787 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2788 = (Is_Packed_Array_Impl_Type (gnat_entity)
2789 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2791 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2792 implementation types as such so that the debug information back-end
2793 can output the appropriate description for them. */
2794 TYPE_PACKED (gnu_type)
2795 = (Is_Packed (gnat_entity)
2796 || Is_Packed_Array_Impl_Type (gnat_entity));
2798 /* If the size is self-referential and the maximum size doesn't
2799 overflow, use it. */
2800 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2801 && gnu_max_size
2802 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2803 && TREE_OVERFLOW (gnu_max_size))
2804 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2805 && TREE_OVERFLOW (gnu_max_size_unit)))
2807 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2808 TYPE_SIZE (gnu_type));
2809 TYPE_SIZE_UNIT (gnu_type)
2810 = size_binop (MIN_EXPR, gnu_max_size_unit,
2811 TYPE_SIZE_UNIT (gnu_type));
2814 /* Set our alias set to that of our base type. This gives all
2815 array subtypes the same alias set. */
2816 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2818 /* If this is a packed type, make this type the same as the packed
2819 array type, but do some adjusting in the type first. */
2820 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2822 Entity_Id gnat_index;
2823 tree gnu_inner;
2825 /* First finish the type we had been making so that we output
2826 debugging information for it. */
2827 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2828 if (Treat_As_Volatile (gnat_entity))
2830 const int quals
2831 = TYPE_QUAL_VOLATILE
2832 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2833 gnu_type = change_qualified_type (gnu_type, quals);
2835 /* Make it artificial only if the base type was artificial too.
2836 That's sort of "morally" true and will make it possible for
2837 the debugger to look it up by name in DWARF, which is needed
2838 in order to decode the packed array type. */
2839 gnu_decl
2840 = create_type_decl (gnu_entity_name, gnu_type,
2841 !Comes_From_Source (Etype (gnat_entity))
2842 && artificial_p, debug_info_p,
2843 gnat_entity);
2845 /* Save it as our equivalent in case the call below elaborates
2846 this type again. */
2847 save_gnu_tree (gnat_entity, gnu_decl, false);
2849 gnu_decl
2850 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2851 NULL_TREE, false);
2852 this_made_decl = true;
2853 gnu_type = TREE_TYPE (gnu_decl);
2854 save_gnu_tree (gnat_entity, NULL_TREE, false);
2855 save_gnu_tree (gnat_entity, gnu_decl, false);
2856 saved = true;
2858 gnu_inner = gnu_type;
2859 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2860 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2861 || TYPE_PADDING_P (gnu_inner)))
2862 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2864 /* We need to attach the index type to the type we just made so
2865 that the actual bounds can later be put into a template. */
2866 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2867 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2868 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2869 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2871 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2873 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2874 TYPE_MODULUS for modular types so we make an extra
2875 subtype if necessary. */
2876 if (TYPE_MODULAR_P (gnu_inner))
2878 tree gnu_subtype
2879 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2880 TREE_TYPE (gnu_subtype) = gnu_inner;
2881 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2882 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2883 TYPE_MIN_VALUE (gnu_inner));
2884 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2885 TYPE_MAX_VALUE (gnu_inner));
2886 gnu_inner = gnu_subtype;
2889 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2891 /* Check for other cases of overloading. */
2892 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2895 for (gnat_index = First_Index (gnat_entity);
2896 Present (gnat_index);
2897 gnat_index = Next_Index (gnat_index))
2898 SET_TYPE_ACTUAL_BOUNDS
2899 (gnu_inner,
2900 tree_cons (NULL_TREE,
2901 get_unpadded_type (Etype (gnat_index)),
2902 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2904 if (Convention (gnat_entity) != Convention_Fortran)
2905 SET_TYPE_ACTUAL_BOUNDS
2906 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2908 if (TREE_CODE (gnu_type) == RECORD_TYPE
2909 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2910 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2914 break;
2916 case E_String_Literal_Subtype:
2917 /* Create the type for a string literal. */
2919 Entity_Id gnat_full_type
2920 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2921 && Present (Full_View (Etype (gnat_entity)))
2922 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2923 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2924 tree gnu_string_array_type
2925 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2926 tree gnu_string_index_type
2927 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2928 (TYPE_DOMAIN (gnu_string_array_type))));
2929 tree gnu_lower_bound
2930 = convert (gnu_string_index_type,
2931 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2932 tree gnu_length
2933 = UI_To_gnu (String_Literal_Length (gnat_entity),
2934 gnu_string_index_type);
2935 tree gnu_upper_bound
2936 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2937 gnu_lower_bound,
2938 int_const_binop (MINUS_EXPR, gnu_length,
2939 convert (gnu_string_index_type,
2940 integer_one_node)));
2941 tree gnu_index_type
2942 = create_index_type (convert (sizetype, gnu_lower_bound),
2943 convert (sizetype, gnu_upper_bound),
2944 create_range_type (gnu_string_index_type,
2945 gnu_lower_bound,
2946 gnu_upper_bound),
2947 gnat_entity);
2949 gnu_type
2950 = build_nonshared_array_type (gnat_to_gnu_type
2951 (Component_Type (gnat_entity)),
2952 gnu_index_type);
2953 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2954 set_nonaliased_component_on_array_type (gnu_type);
2955 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2957 break;
2959 /* Record Types and Subtypes
2961 The following fields are defined on record types:
2963 Has_Discriminants True if the record has discriminants
2964 First_Discriminant Points to head of list of discriminants
2965 First_Entity Points to head of list of fields
2966 Is_Tagged_Type True if the record is tagged
2968 Implementation of Ada records and discriminated records:
2970 A record type definition is transformed into the equivalent of a C
2971 struct definition. The fields that are the discriminants which are
2972 found in the Full_Type_Declaration node and the elements of the
2973 Component_List found in the Record_Type_Definition node. The
2974 Component_List can be a recursive structure since each Variant of
2975 the Variant_Part of the Component_List has a Component_List.
2977 Processing of a record type definition comprises starting the list of
2978 field declarations here from the discriminants and the calling the
2979 function components_to_record to add the rest of the fields from the
2980 component list and return the gnu type node. The function
2981 components_to_record will call itself recursively as it traverses
2982 the tree. */
2984 case E_Record_Type:
2985 if (Has_Complex_Representation (gnat_entity))
2987 gnu_type
2988 = build_complex_type
2989 (get_unpadded_type
2990 (Etype (Defining_Entity
2991 (First (Component_Items
2992 (Component_List
2993 (Type_Definition
2994 (Declaration_Node (gnat_entity)))))))));
2996 break;
3000 Node_Id full_definition = Declaration_Node (gnat_entity);
3001 Node_Id record_definition = Type_Definition (full_definition);
3002 Node_Id gnat_constr;
3003 Entity_Id gnat_field, gnat_parent_type;
3004 tree gnu_field, gnu_field_list = NULL_TREE;
3005 tree gnu_get_parent;
3006 /* Set PACKED in keeping with gnat_to_gnu_field. */
3007 const int packed
3008 = Is_Packed (gnat_entity)
3010 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
3011 ? -1
3012 : 0;
3013 const bool has_align = Known_Alignment (gnat_entity);
3014 const bool has_discr = Has_Discriminants (gnat_entity);
3015 const bool has_rep = Has_Specified_Layout (gnat_entity);
3016 const bool is_extension
3017 = (Is_Tagged_Type (gnat_entity)
3018 && Nkind (record_definition) == N_Derived_Type_Definition);
3019 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
3020 bool all_rep = has_rep;
3022 /* See if all fields have a rep clause. Stop when we find one
3023 that doesn't. */
3024 if (all_rep)
3025 for (gnat_field = First_Entity (gnat_entity);
3026 Present (gnat_field);
3027 gnat_field = Next_Entity (gnat_field))
3028 if ((Ekind (gnat_field) == E_Component
3029 || Ekind (gnat_field) == E_Discriminant)
3030 && No (Component_Clause (gnat_field)))
3032 all_rep = false;
3033 break;
3036 /* If this is a record extension, go a level further to find the
3037 record definition. Also, verify we have a Parent_Subtype. */
3038 if (is_extension)
3040 if (!type_annotate_only
3041 || Present (Record_Extension_Part (record_definition)))
3042 record_definition = Record_Extension_Part (record_definition);
3044 gcc_assert (type_annotate_only
3045 || Present (Parent_Subtype (gnat_entity)));
3048 /* Make a node for the record. If we are not defining the record,
3049 suppress expanding incomplete types. */
3050 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3051 TYPE_NAME (gnu_type) = gnu_entity_name;
3052 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3053 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3054 = Reverse_Storage_Order (gnat_entity);
3055 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3057 if (!definition)
3059 defer_incomplete_level++;
3060 this_deferred = true;
3063 /* If both a size and rep clause were specified, put the size on
3064 the record type now so that it can get the proper layout. */
3065 if (has_rep && Known_RM_Size (gnat_entity))
3066 TYPE_SIZE (gnu_type)
3067 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3069 /* Always set the alignment on the record type here so that it can
3070 get the proper layout. */
3071 if (has_align)
3072 SET_TYPE_ALIGN (gnu_type,
3073 validate_alignment (Alignment (gnat_entity),
3074 gnat_entity, 0));
3075 else
3077 SET_TYPE_ALIGN (gnu_type, 0);
3079 /* If a type needs strict alignment, the minimum size will be the
3080 type size instead of the RM size (see validate_size). Cap the
3081 alignment lest it causes this type size to become too large. */
3082 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3084 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3085 unsigned int max_align = max_size & -max_size;
3086 if (max_align < BIGGEST_ALIGNMENT)
3087 TYPE_MAX_ALIGN (gnu_type) = max_align;
3091 /* If we have a Parent_Subtype, make a field for the parent. If
3092 this record has rep clauses, force the position to zero. */
3093 if (Present (Parent_Subtype (gnat_entity)))
3095 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3096 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3097 tree gnu_parent;
3098 int parent_packed = 0;
3100 /* A major complexity here is that the parent subtype will
3101 reference our discriminants in its Stored_Constraint list.
3102 But those must reference the parent component of this record
3103 which is precisely of the parent subtype we have not built yet!
3104 To break the circle we first build a dummy COMPONENT_REF which
3105 represents the "get to the parent" operation and initialize
3106 each of those discriminants to a COMPONENT_REF of the above
3107 dummy parent referencing the corresponding discriminant of the
3108 base type of the parent subtype. */
3109 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3110 build0 (PLACEHOLDER_EXPR, gnu_type),
3111 build_decl (input_location,
3112 FIELD_DECL, NULL_TREE,
3113 gnu_dummy_parent_type),
3114 NULL_TREE);
3116 if (has_discr)
3117 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3118 Present (gnat_field);
3119 gnat_field = Next_Stored_Discriminant (gnat_field))
3120 if (Present (Corresponding_Discriminant (gnat_field)))
3122 tree gnu_field
3123 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3124 (gnat_field));
3125 save_gnu_tree
3126 (gnat_field,
3127 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3128 gnu_get_parent, gnu_field, NULL_TREE),
3129 true);
3132 /* Then we build the parent subtype. If it has discriminants but
3133 the type itself has unknown discriminants, this means that it
3134 doesn't contain information about how the discriminants are
3135 derived from those of the ancestor type, so it cannot be used
3136 directly. Instead it is built by cloning the parent subtype
3137 of the underlying record view of the type, for which the above
3138 derivation of discriminants has been made explicit. */
3139 if (Has_Discriminants (gnat_parent)
3140 && Has_Unknown_Discriminants (gnat_entity))
3142 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3144 /* If we are defining the type, the underlying record
3145 view must already have been elaborated at this point.
3146 Otherwise do it now as its parent subtype cannot be
3147 technically elaborated on its own. */
3148 if (definition)
3149 gcc_assert (present_gnu_tree (gnat_uview));
3150 else
3151 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3153 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3155 /* Substitute the "get to the parent" of the type for that
3156 of its underlying record view in the cloned type. */
3157 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3158 Present (gnat_field);
3159 gnat_field = Next_Stored_Discriminant (gnat_field))
3160 if (Present (Corresponding_Discriminant (gnat_field)))
3162 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3163 tree gnu_ref
3164 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3165 gnu_get_parent, gnu_field, NULL_TREE);
3166 gnu_parent
3167 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3170 else
3171 gnu_parent = gnat_to_gnu_type (gnat_parent);
3173 /* The parent field needs strict alignment so, if it is to
3174 be created with a component clause below, then we need
3175 to apply the same adjustment as in gnat_to_gnu_field. */
3176 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3178 /* ??? For historical reasons, we do it on strict-alignment
3179 platforms only, where it is really required. This means
3180 that a confirming representation clause will change the
3181 behavior of the compiler on the other platforms. */
3182 if (STRICT_ALIGNMENT)
3183 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3184 else
3185 parent_packed
3186 = adjust_packed (gnu_parent, gnu_type, parent_packed);
3189 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3190 initially built. The discriminants must reference the fields
3191 of the parent subtype and not those of its base type for the
3192 placeholder machinery to properly work. */
3193 if (has_discr)
3195 /* The actual parent subtype is the full view. */
3196 if (IN (Ekind (gnat_parent), Private_Kind))
3198 if (Present (Full_View (gnat_parent)))
3199 gnat_parent = Full_View (gnat_parent);
3200 else
3201 gnat_parent = Underlying_Full_View (gnat_parent);
3204 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3205 Present (gnat_field);
3206 gnat_field = Next_Stored_Discriminant (gnat_field))
3207 if (Present (Corresponding_Discriminant (gnat_field)))
3209 Entity_Id field;
3210 for (field = First_Stored_Discriminant (gnat_parent);
3211 Present (field);
3212 field = Next_Stored_Discriminant (field))
3213 if (same_discriminant_p (gnat_field, field))
3214 break;
3215 gcc_assert (Present (field));
3216 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3217 = gnat_to_gnu_field_decl (field);
3221 /* The "get to the parent" COMPONENT_REF must be given its
3222 proper type... */
3223 TREE_TYPE (gnu_get_parent) = gnu_parent;
3225 /* ...and reference the _Parent field of this record. */
3226 gnu_field
3227 = create_field_decl (parent_name_id,
3228 gnu_parent, gnu_type,
3229 has_rep
3230 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3231 has_rep
3232 ? bitsize_zero_node : NULL_TREE,
3233 parent_packed, 1);
3234 DECL_INTERNAL_P (gnu_field) = 1;
3235 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3236 TYPE_FIELDS (gnu_type) = gnu_field;
3239 /* Make the fields for the discriminants and put them into the record
3240 unless it's an Unchecked_Union. */
3241 if (has_discr)
3242 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3243 Present (gnat_field);
3244 gnat_field = Next_Stored_Discriminant (gnat_field))
3246 /* If this is a record extension and this discriminant is the
3247 renaming of another discriminant, we've handled it above. */
3248 if (is_extension
3249 && Present (Corresponding_Discriminant (gnat_field)))
3250 continue;
3252 gnu_field
3253 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3254 debug_info_p);
3256 /* Make an expression using a PLACEHOLDER_EXPR from the
3257 FIELD_DECL node just created and link that with the
3258 corresponding GNAT defining identifier. */
3259 save_gnu_tree (gnat_field,
3260 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3261 build0 (PLACEHOLDER_EXPR, gnu_type),
3262 gnu_field, NULL_TREE),
3263 true);
3265 if (!is_unchecked_union)
3267 DECL_CHAIN (gnu_field) = gnu_field_list;
3268 gnu_field_list = gnu_field;
3272 /* If we have a derived untagged type that renames discriminants in
3273 the parent type, the (stored) discriminants are just a copy of the
3274 discriminants of the parent type. This means that any constraints
3275 added by the renaming in the derivation are disregarded as far as
3276 the layout of the derived type is concerned. To rescue them, we
3277 change the type of the (stored) discriminants to a subtype with
3278 the bounds of the type of the visible discriminants. */
3279 if (has_discr
3280 && !is_extension
3281 && Stored_Constraint (gnat_entity) != No_Elist)
3282 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3283 gnat_constr != No_Elmt;
3284 gnat_constr = Next_Elmt (gnat_constr))
3285 if (Nkind (Node (gnat_constr)) == N_Identifier
3286 /* Ignore access discriminants. */
3287 && !Is_Access_Type (Etype (Node (gnat_constr)))
3288 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3290 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3291 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3292 tree gnu_ref
3293 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3294 NULL_TREE, false);
3296 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3297 just above for one of the stored discriminants. */
3298 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3300 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3302 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3303 tree gnu_subtype
3304 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3305 ? make_unsigned_type (prec) : make_signed_type (prec);
3306 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3307 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3308 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3309 TYPE_MIN_VALUE (gnu_discr_type));
3310 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3311 TYPE_MAX_VALUE (gnu_discr_type));
3312 TREE_TYPE (gnu_ref)
3313 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3317 /* If this is a derived type with discriminants and these discriminants
3318 affect the initial shape it has inherited, factor them in. But for
3319 an Unchecked_Union (it must be an Itype), just process the type. */
3320 if (has_discr
3321 && !is_extension
3322 && !Has_Record_Rep_Clause (gnat_entity)
3323 && Stored_Constraint (gnat_entity) != No_Elist
3324 && (gnat_parent_type = Underlying_Type (Etype (gnat_entity)))
3325 && Is_Record_Type (gnat_parent_type)
3326 && !Is_Unchecked_Union (gnat_parent_type))
3328 tree gnu_parent_type
3329 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type));
3331 if (TYPE_IS_PADDING_P (gnu_parent_type))
3332 gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type));
3334 vec<subst_pair> gnu_subst_list
3335 = build_subst_list (gnat_entity, gnat_parent_type, definition);
3337 /* Set the layout of the type to match that of the parent type,
3338 doing required substitutions. */
3339 copy_and_substitute_in_layout (gnat_entity, gnat_parent_type,
3340 gnu_type, gnu_parent_type,
3341 gnu_subst_list, debug_info_p);
3343 else
3345 /* Add the fields into the record type and finish it up. */
3346 components_to_record (Component_List (record_definition),
3347 gnat_entity, gnu_field_list, gnu_type,
3348 packed, definition, false, all_rep,
3349 is_unchecked_union, artificial_p,
3350 debug_info_p, false,
3351 all_rep ? NULL_TREE : bitsize_zero_node,
3352 NULL);
3354 /* If there are entities in the chain corresponding to components
3355 that we did not elaborate, ensure we elaborate their types if
3356 they are Itypes. */
3357 for (gnat_temp = First_Entity (gnat_entity);
3358 Present (gnat_temp);
3359 gnat_temp = Next_Entity (gnat_temp))
3360 if ((Ekind (gnat_temp) == E_Component
3361 || Ekind (gnat_temp) == E_Discriminant)
3362 && Is_Itype (Etype (gnat_temp))
3363 && !present_gnu_tree (gnat_temp))
3364 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3367 /* Fill in locations of fields. */
3368 annotate_rep (gnat_entity, gnu_type);
3370 /* If this is a record type associated with an exception definition,
3371 equate its fields to those of the standard exception type. This
3372 will make it possible to convert between them. */
3373 if (gnu_entity_name == exception_data_name_id)
3375 tree gnu_std_field;
3376 for (gnu_field = TYPE_FIELDS (gnu_type),
3377 gnu_std_field = TYPE_FIELDS (except_type_node);
3378 gnu_field;
3379 gnu_field = DECL_CHAIN (gnu_field),
3380 gnu_std_field = DECL_CHAIN (gnu_std_field))
3381 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3382 gcc_assert (!gnu_std_field);
3385 break;
3387 case E_Class_Wide_Subtype:
3388 /* If an equivalent type is present, that is what we should use.
3389 Otherwise, fall through to handle this like a record subtype
3390 since it may have constraints. */
3391 if (gnat_equiv_type != gnat_entity)
3393 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3394 maybe_present = true;
3395 break;
3398 /* ... fall through ... */
3400 case E_Record_Subtype:
3401 /* If Cloned_Subtype is Present it means this record subtype has
3402 identical layout to that type or subtype and we should use
3403 that GCC type for this one. The front end guarantees that
3404 the component list is shared. */
3405 if (Present (Cloned_Subtype (gnat_entity)))
3407 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3408 NULL_TREE, false);
3409 maybe_present = true;
3410 break;
3413 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3414 changing the type, make a new type with each field having the type of
3415 the field in the new subtype but the position computed by transforming
3416 every discriminant reference according to the constraints. We don't
3417 see any difference between private and non-private type here since
3418 derivations from types should have been deferred until the completion
3419 of the private type. */
3420 else
3422 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3424 if (!definition)
3426 defer_incomplete_level++;
3427 this_deferred = true;
3430 tree gnu_base_type
3431 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3433 if (present_gnu_tree (gnat_entity))
3435 maybe_present = true;
3436 break;
3439 /* If this is a record subtype associated with a dispatch table,
3440 strip the suffix. This is necessary to make sure 2 different
3441 subtypes associated with the imported and exported views of a
3442 dispatch table are properly merged in LTO mode. */
3443 if (Is_Dispatch_Table_Entity (gnat_entity))
3445 char *p;
3446 Get_Encoded_Name (gnat_entity);
3447 p = strchr (Name_Buffer, '_');
3448 gcc_assert (p);
3449 strcpy (p+2, "dtS");
3450 gnu_entity_name = get_identifier (Name_Buffer);
3453 /* When the subtype has discriminants and these discriminants affect
3454 the initial shape it has inherited, factor them in. But for an
3455 Unchecked_Union (it must be an Itype), just return the type. */
3456 if (Has_Discriminants (gnat_entity)
3457 && Stored_Constraint (gnat_entity) != No_Elist
3458 && !Is_For_Access_Subtype (gnat_entity)
3459 && Is_Record_Type (gnat_base_type)
3460 && !Is_Unchecked_Union (gnat_base_type))
3462 vec<subst_pair> gnu_subst_list
3463 = build_subst_list (gnat_entity, gnat_base_type, definition);
3464 tree gnu_unpad_base_type;
3466 gnu_type = make_node (RECORD_TYPE);
3467 TYPE_NAME (gnu_type) = gnu_entity_name;
3468 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3469 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3470 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3471 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3472 = Reverse_Storage_Order (gnat_entity);
3473 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3475 /* Set the size, alignment and alias set of the type to match
3476 those of the base type, doing required substitutions. */
3477 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3478 gnu_subst_list);
3480 if (TYPE_IS_PADDING_P (gnu_base_type))
3481 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3482 else
3483 gnu_unpad_base_type = gnu_base_type;
3485 /* Set the layout of the type to match that of the base type,
3486 doing required substitutions. We will output debug info
3487 manually below so pass false as last argument. */
3488 copy_and_substitute_in_layout (gnat_entity, gnat_base_type,
3489 gnu_type, gnu_unpad_base_type,
3490 gnu_subst_list, false);
3492 /* Fill in locations of fields. */
3493 annotate_rep (gnat_entity, gnu_type);
3495 /* If debugging information is being written for the type and if
3496 we are asked to output such encodings, write a record that
3497 shows what we are a subtype of and also make a variable that
3498 indicates our size, if still variable. */
3499 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3501 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3502 tree gnu_unpad_base_name
3503 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3504 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3506 TYPE_NAME (gnu_subtype_marker)
3507 = create_concat_name (gnat_entity, "XVS");
3508 finish_record_type (gnu_subtype_marker,
3509 create_field_decl (gnu_unpad_base_name,
3510 build_reference_type
3511 (gnu_unpad_base_type),
3512 gnu_subtype_marker,
3513 NULL_TREE, NULL_TREE,
3514 0, 0),
3515 0, true);
3517 add_parallel_type (gnu_type, gnu_subtype_marker);
3519 if (definition
3520 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3521 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3522 TYPE_SIZE_UNIT (gnu_subtype_marker)
3523 = create_var_decl (create_concat_name (gnat_entity,
3524 "XVZ"),
3525 NULL_TREE, sizetype, gnu_size_unit,
3526 false, false, false, false, false,
3527 true, debug_info_p,
3528 NULL, gnat_entity);
3532 /* Otherwise, go down all the components in the new type and make
3533 them equivalent to those in the base type. */
3534 else
3536 gnu_type = gnu_base_type;
3538 for (gnat_temp = First_Entity (gnat_entity);
3539 Present (gnat_temp);
3540 gnat_temp = Next_Entity (gnat_temp))
3541 if ((Ekind (gnat_temp) == E_Discriminant
3542 && !Is_Unchecked_Union (gnat_base_type))
3543 || Ekind (gnat_temp) == E_Component)
3544 save_gnu_tree (gnat_temp,
3545 gnat_to_gnu_field_decl
3546 (Original_Record_Component (gnat_temp)),
3547 false);
3550 break;
3552 case E_Access_Subprogram_Type:
3553 case E_Anonymous_Access_Subprogram_Type:
3554 /* Use the special descriptor type for dispatch tables if needed,
3555 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3556 Note that we are only required to do so for static tables in
3557 order to be compatible with the C++ ABI, but Ada 2005 allows
3558 to extend library level tagged types at the local level so
3559 we do it in the non-static case as well. */
3560 if (TARGET_VTABLE_USES_DESCRIPTORS
3561 && Is_Dispatch_Table_Entity (gnat_entity))
3563 gnu_type = fdesc_type_node;
3564 gnu_size = TYPE_SIZE (gnu_type);
3565 break;
3568 /* ... fall through ... */
3570 case E_Allocator_Type:
3571 case E_Access_Type:
3572 case E_Access_Attribute_Type:
3573 case E_Anonymous_Access_Type:
3574 case E_General_Access_Type:
3576 /* The designated type and its equivalent type for gigi. */
3577 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3578 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3579 /* Whether it comes from a limited with. */
3580 const bool is_from_limited_with
3581 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3582 && From_Limited_With (gnat_desig_equiv));
3583 /* The "full view" of the designated type. If this is an incomplete
3584 entity from a limited with, treat its non-limited view as the full
3585 view. Otherwise, if this is an incomplete or private type, use the
3586 full view. In the former case, we might point to a private type,
3587 in which case, we need its full view. Also, we want to look at the
3588 actual type used for the representation, so this takes a total of
3589 three steps. */
3590 Entity_Id gnat_desig_full_direct_first
3591 = (is_from_limited_with
3592 ? Non_Limited_View (gnat_desig_equiv)
3593 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3594 ? Full_View (gnat_desig_equiv) : Empty));
3595 Entity_Id gnat_desig_full_direct
3596 = ((is_from_limited_with
3597 && Present (gnat_desig_full_direct_first)
3598 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3599 ? Full_View (gnat_desig_full_direct_first)
3600 : gnat_desig_full_direct_first);
3601 Entity_Id gnat_desig_full
3602 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3603 /* The type actually used to represent the designated type, either
3604 gnat_desig_full or gnat_desig_equiv. */
3605 Entity_Id gnat_desig_rep;
3606 /* We want to know if we'll be seeing the freeze node for any
3607 incomplete type we may be pointing to. */
3608 const bool in_main_unit
3609 = (Present (gnat_desig_full)
3610 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3611 : In_Extended_Main_Code_Unit (gnat_desig_type));
3612 /* True if we make a dummy type here. */
3613 bool made_dummy = false;
3614 /* The mode to be used for the pointer type. */
3615 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3616 /* The GCC type used for the designated type. */
3617 tree gnu_desig_type = NULL_TREE;
3619 if (!targetm.valid_pointer_mode (p_mode))
3620 p_mode = ptr_mode;
3622 /* If either the designated type or its full view is an unconstrained
3623 array subtype, replace it with the type it's a subtype of. This
3624 avoids problems with multiple copies of unconstrained array types.
3625 Likewise, if the designated type is a subtype of an incomplete
3626 record type, use the parent type to avoid order of elaboration
3627 issues. This can lose some code efficiency, but there is no
3628 alternative. */
3629 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3630 && !Is_Constrained (gnat_desig_equiv))
3631 gnat_desig_equiv = Etype (gnat_desig_equiv);
3632 if (Present (gnat_desig_full)
3633 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3634 && !Is_Constrained (gnat_desig_full))
3635 || (Ekind (gnat_desig_full) == E_Record_Subtype
3636 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3637 gnat_desig_full = Etype (gnat_desig_full);
3639 /* Set the type that's the representation of the designated type. */
3640 gnat_desig_rep
3641 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3643 /* If we already know what the full type is, use it. */
3644 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3645 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3647 /* Get the type of the thing we are to point to and build a pointer to
3648 it. If it is a reference to an incomplete or private type with a
3649 full view that is a record or an array, make a dummy type node and
3650 get the actual type later when we have verified it is safe. */
3651 else if ((!in_main_unit
3652 && !present_gnu_tree (gnat_desig_equiv)
3653 && Present (gnat_desig_full)
3654 && (Is_Record_Type (gnat_desig_full)
3655 || Is_Array_Type (gnat_desig_full)))
3656 /* Likewise if this is a reference to a record, an array or a
3657 subprogram type and we are to defer elaborating incomplete
3658 types. We do this because this access type may be the full
3659 view of a private type. */
3660 || ((!in_main_unit || imported_p)
3661 && defer_incomplete_level != 0
3662 && !present_gnu_tree (gnat_desig_equiv)
3663 && (Is_Record_Type (gnat_desig_rep)
3664 || Is_Array_Type (gnat_desig_rep)
3665 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3666 /* If this is a reference from a limited_with type back to our
3667 main unit and there's a freeze node for it, either we have
3668 already processed the declaration and made the dummy type,
3669 in which case we just reuse the latter, or we have not yet,
3670 in which case we make the dummy type and it will be reused
3671 when the declaration is finally processed. In both cases,
3672 the pointer eventually created below will be automatically
3673 adjusted when the freeze node is processed. */
3674 || (in_main_unit
3675 && is_from_limited_with
3676 && Present (Freeze_Node (gnat_desig_rep))))
3678 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3679 made_dummy = true;
3682 /* Otherwise handle the case of a pointer to itself. */
3683 else if (gnat_desig_equiv == gnat_entity)
3685 gnu_type
3686 = build_pointer_type_for_mode (void_type_node, p_mode,
3687 No_Strict_Aliasing (gnat_entity));
3688 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3691 /* If expansion is disabled, the equivalent type of a concurrent type
3692 is absent, so we use the void pointer type. */
3693 else if (type_annotate_only && No (gnat_desig_equiv))
3694 gnu_type = ptr_type_node;
3696 /* If the ultimately designated type is an incomplete type with no full
3697 view, we use the void pointer type in LTO mode to avoid emitting a
3698 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3699 the name of the dummy type in used by GDB for a global lookup. */
3700 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3701 && No (Full_View (gnat_desig_rep))
3702 && flag_generate_lto)
3703 gnu_type = ptr_type_node;
3705 /* Finally, handle the default case where we can just elaborate our
3706 designated type. */
3707 else
3708 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3710 /* It is possible that a call to gnat_to_gnu_type above resolved our
3711 type. If so, just return it. */
3712 if (present_gnu_tree (gnat_entity))
3714 maybe_present = true;
3715 break;
3718 /* Access-to-unconstrained-array types need a special treatment. */
3719 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3721 /* If the processing above got something that has a pointer, then
3722 we are done. This could have happened either because the type
3723 was elaborated or because somebody else executed the code. */
3724 if (!TYPE_POINTER_TO (gnu_desig_type))
3725 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3727 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3730 /* If we haven't done it yet, build the pointer type the usual way. */
3731 else if (!gnu_type)
3733 /* Modify the designated type if we are pointing only to constant
3734 objects, but don't do it for a dummy type. */
3735 if (Is_Access_Constant (gnat_entity)
3736 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3737 gnu_desig_type
3738 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3740 gnu_type
3741 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3742 No_Strict_Aliasing (gnat_entity));
3745 /* If the designated type is not declared in the main unit and we made
3746 a dummy node for it, save our definition, elaborate the actual type
3747 and replace the dummy type we made with the actual one. But if we
3748 are to defer actually looking up the actual type, make an entry in
3749 the deferred list instead. If this is from a limited with, we may
3750 have to defer until the end of the current unit. */
3751 if (!in_main_unit && made_dummy)
3753 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3754 gnu_type
3755 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3757 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3758 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3759 artificial_p, debug_info_p,
3760 gnat_entity);
3761 this_made_decl = true;
3762 gnu_type = TREE_TYPE (gnu_decl);
3763 save_gnu_tree (gnat_entity, gnu_decl, false);
3764 saved = true;
3766 if (defer_incomplete_level == 0 && !is_from_limited_with)
3768 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3769 gnat_to_gnu_type (gnat_desig_equiv));
3771 else
3773 struct incomplete *p = XNEW (struct incomplete);
3774 struct incomplete **head
3775 = (is_from_limited_with
3776 ? &defer_limited_with_list : &defer_incomplete_list);
3778 p->old_type = gnu_desig_type;
3779 p->full_type = gnat_desig_equiv;
3780 p->next = *head;
3781 *head = p;
3785 break;
3787 case E_Access_Protected_Subprogram_Type:
3788 case E_Anonymous_Access_Protected_Subprogram_Type:
3789 /* If we are just annotating types and have no equivalent record type,
3790 just use the void pointer type. */
3791 if (type_annotate_only && gnat_equiv_type == gnat_entity)
3792 gnu_type = ptr_type_node;
3794 /* The run-time representation is the equivalent type. */
3795 else
3797 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3798 maybe_present = true;
3801 /* The designated subtype must be elaborated as well, if it does
3802 not have its own freeze node. */
3803 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3804 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3805 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3806 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3807 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3808 NULL_TREE, false);
3810 break;
3812 case E_Access_Subtype:
3813 /* We treat this as identical to its base type; any constraint is
3814 meaningful only to the front-end. */
3815 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3817 /* The designated subtype must be elaborated as well, if it does
3818 not have its own freeze node. But designated subtypes created
3819 for constrained components of records with discriminants are
3820 not frozen by the front-end and not elaborated here, because
3821 their use may appear before the base type is frozen and it is
3822 not clear that they are needed in gigi. With the current model,
3823 there is no correct place where they could be elaborated. */
3824 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3825 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3826 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3827 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3829 /* If we are to defer elaborating incomplete types, make a dummy
3830 type node and elaborate it later. */
3831 if (defer_incomplete_level != 0)
3833 struct incomplete *p = XNEW (struct incomplete);
3835 p->old_type
3836 = make_dummy_type (Directly_Designated_Type (gnat_entity));
3837 p->full_type = Directly_Designated_Type (gnat_entity);
3838 p->next = defer_incomplete_list;
3839 defer_incomplete_list = p;
3841 else if (!IN (Ekind (Base_Type
3842 (Directly_Designated_Type (gnat_entity))),
3843 Incomplete_Or_Private_Kind))
3844 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3845 NULL_TREE, false);
3848 maybe_present = true;
3849 break;
3851 /* Subprogram Entities
3853 The following access functions are defined for subprograms:
3855 Etype Return type or Standard_Void_Type.
3856 First_Formal The first formal parameter.
3857 Is_Imported Indicates that the subprogram has appeared in
3858 an INTERFACE or IMPORT pragma. For now we
3859 assume that the external language is C.
3860 Is_Exported Likewise but for an EXPORT pragma.
3861 Is_Inlined True if the subprogram is to be inlined.
3863 Each parameter is first checked by calling must_pass_by_ref on its
3864 type to determine if it is passed by reference. For parameters which
3865 are copied in, if they are Ada In Out or Out parameters, their return
3866 value becomes part of a record which becomes the return type of the
3867 function (C function - note that this applies only to Ada procedures
3868 so there is no Ada return type). Additional code to store back the
3869 parameters will be generated on the caller side. This transformation
3870 is done here, not in the front-end.
3872 The intended result of the transformation can be seen from the
3873 equivalent source rewritings that follow:
3875 struct temp {int a,b};
3876 procedure P (A,B: In Out ...) is temp P (int A,B)
3877 begin {
3878 .. ..
3879 end P; return {A,B};
3882 temp t;
3883 P(X,Y); t = P(X,Y);
3884 X = t.a , Y = t.b;
3886 For subprogram types we need to perform mainly the same conversions to
3887 GCC form that are needed for procedures and function declarations. The
3888 only difference is that at the end, we make a type declaration instead
3889 of a function declaration. */
3891 case E_Subprogram_Type:
3892 case E_Function:
3893 case E_Procedure:
3895 tree gnu_ext_name
3896 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
3897 enum inline_status_t inline_status
3898 = Has_Pragma_No_Inline (gnat_entity)
3899 ? is_suppressed
3900 : Has_Pragma_Inline_Always (gnat_entity)
3901 ? is_required
3902 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
3903 bool public_flag = Is_Public (gnat_entity) || imported_p;
3904 /* Subprograms marked both Intrinsic and Always_Inline need not
3905 have a body of their own. */
3906 bool extern_flag
3907 = ((Is_Public (gnat_entity) && !definition)
3908 || imported_p
3909 || (Convention (gnat_entity) == Convention_Intrinsic
3910 && Has_Pragma_Inline_Always (gnat_entity)));
3911 tree gnu_param_list;
3913 /* A parameter may refer to this type, so defer completion of any
3914 incomplete types. */
3915 if (kind == E_Subprogram_Type && !definition)
3917 defer_incomplete_level++;
3918 this_deferred = true;
3921 /* If the subprogram has an alias, it is probably inherited, so
3922 we can use the original one. If the original "subprogram"
3923 is actually an enumeration literal, it may be the first use
3924 of its type, so we must elaborate that type now. */
3925 if (Present (Alias (gnat_entity)))
3927 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
3929 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3930 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
3931 false);
3933 gnu_decl
3934 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
3936 /* Elaborate any Itypes in the parameters of this entity. */
3937 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3938 Present (gnat_temp);
3939 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3940 if (Is_Itype (Etype (gnat_temp)))
3941 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3943 /* Materialize renamed subprograms in the debugging information
3944 when the renamed object is compile time known. We can consider
3945 such renamings as imported declarations.
3947 Because the parameters in generics instantiation are generally
3948 materialized as renamings, we ofter end up having both the
3949 renamed subprogram and the renaming in the same context and with
3950 the same name: in this case, renaming is both useless debug-wise
3951 and potentially harmful as name resolution in the debugger could
3952 return twice the same entity! So avoid this case. */
3953 if (debug_info_p && !artificial_p
3954 && !(get_debug_scope (gnat_entity, NULL)
3955 == get_debug_scope (gnat_renamed, NULL)
3956 && Name_Equals (Chars (gnat_entity),
3957 Chars (gnat_renamed)))
3958 && Present (gnat_renamed)
3959 && (Ekind (gnat_renamed) == E_Function
3960 || Ekind (gnat_renamed) == E_Procedure)
3961 && gnu_decl
3962 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
3964 tree decl = build_decl (input_location, IMPORTED_DECL,
3965 gnu_entity_name, void_type_node);
3966 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
3967 gnat_pushdecl (decl, gnat_entity);
3970 break;
3973 /* Get the GCC tree for the (underlying) subprogram type. If the
3974 entity is an actual subprogram, also get the parameter list. */
3975 gnu_type
3976 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
3977 &gnu_param_list);
3978 if (DECL_P (gnu_type))
3980 gnu_decl = gnu_type;
3981 gnu_type = TREE_TYPE (gnu_decl);
3982 break;
3985 /* Deal with platform-specific calling conventions. */
3986 if (Has_Stdcall_Convention (gnat_entity))
3987 prepend_one_attribute
3988 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3989 get_identifier ("stdcall"), NULL_TREE,
3990 gnat_entity);
3991 else if (Has_Thiscall_Convention (gnat_entity))
3992 prepend_one_attribute
3993 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3994 get_identifier ("thiscall"), NULL_TREE,
3995 gnat_entity);
3997 /* If we should request stack realignment for a foreign convention
3998 subprogram, do so. Note that this applies to task entry points
3999 in particular. */
4000 if (FOREIGN_FORCE_REALIGN_STACK
4001 && Has_Foreign_Convention (gnat_entity))
4002 prepend_one_attribute
4003 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4004 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4005 gnat_entity);
4007 /* Deal with a pragma Linker_Section on a subprogram. */
4008 if ((kind == E_Function || kind == E_Procedure)
4009 && Present (Linker_Section_Pragma (gnat_entity)))
4010 prepend_one_attribute_pragma (&attr_list,
4011 Linker_Section_Pragma (gnat_entity));
4013 /* If we are defining the subprogram and it has an Address clause
4014 we must get the address expression from the saved GCC tree for the
4015 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4016 the address expression here since the front-end has guaranteed
4017 in that case that the elaboration has no effects. If there is
4018 an Address clause and we are not defining the object, just
4019 make it a constant. */
4020 if (Present (Address_Clause (gnat_entity)))
4022 tree gnu_address = NULL_TREE;
4024 if (definition)
4025 gnu_address
4026 = (present_gnu_tree (gnat_entity)
4027 ? get_gnu_tree (gnat_entity)
4028 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4030 save_gnu_tree (gnat_entity, NULL_TREE, false);
4032 /* Convert the type of the object to a reference type that can
4033 alias everything as per RM 13.3(19). */
4034 gnu_type
4035 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4036 if (gnu_address)
4037 gnu_address = convert (gnu_type, gnu_address);
4039 gnu_decl
4040 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4041 gnu_address, false, Is_Public (gnat_entity),
4042 extern_flag, false, false, artificial_p,
4043 debug_info_p, NULL, gnat_entity);
4044 DECL_BY_REF_P (gnu_decl) = 1;
4047 /* If this is a mere subprogram type, just create the declaration. */
4048 else if (kind == E_Subprogram_Type)
4050 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4052 gnu_decl
4053 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4054 debug_info_p, gnat_entity);
4057 /* Otherwise create the subprogram declaration with the external name,
4058 the type and the parameter list. However, if this a reference to
4059 the allocation routines, reuse the canonical declaration nodes as
4060 they come with special properties. */
4061 else
4063 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4064 gnu_decl = malloc_decl;
4065 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4066 gnu_decl = realloc_decl;
4067 else
4069 gnu_decl
4070 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4071 gnu_type, gnu_param_list,
4072 inline_status, public_flag,
4073 extern_flag, artificial_p,
4074 debug_info_p, attr_list, gnat_entity);
4076 DECL_STUBBED_P (gnu_decl)
4077 = (Convention (gnat_entity) == Convention_Stubbed);
4081 break;
4083 case E_Incomplete_Type:
4084 case E_Incomplete_Subtype:
4085 case E_Private_Type:
4086 case E_Private_Subtype:
4087 case E_Limited_Private_Type:
4088 case E_Limited_Private_Subtype:
4089 case E_Record_Type_With_Private:
4090 case E_Record_Subtype_With_Private:
4092 const bool is_from_limited_with
4093 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4094 /* Get the "full view" of this entity. If this is an incomplete
4095 entity from a limited with, treat its non-limited view as the
4096 full view. Otherwise, use either the full view or the underlying
4097 full view, whichever is present. This is used in all the tests
4098 below. */
4099 const Entity_Id full_view
4100 = is_from_limited_with
4101 ? Non_Limited_View (gnat_entity)
4102 : Present (Full_View (gnat_entity))
4103 ? Full_View (gnat_entity)
4104 : IN (kind, Private_Kind)
4105 ? Underlying_Full_View (gnat_entity)
4106 : Empty;
4108 /* If this is an incomplete type with no full view, it must be a Taft
4109 Amendment type or an incomplete type coming from a limited context,
4110 in which cases we return a dummy type. Otherwise, we just get the
4111 type from its Etype. */
4112 if (No (full_view))
4114 if (kind == E_Incomplete_Type)
4116 gnu_type = make_dummy_type (gnat_entity);
4117 gnu_decl = TYPE_STUB_DECL (gnu_type);
4119 else
4121 gnu_decl
4122 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4123 maybe_present = true;
4127 /* Or else, if we already made a type for the full view, reuse it. */
4128 else if (present_gnu_tree (full_view))
4129 gnu_decl = get_gnu_tree (full_view);
4131 /* Or else, if we are not defining the type or there is no freeze
4132 node on it, get the type for the full view. Likewise if this is
4133 a limited_with'ed type not declared in the main unit, which can
4134 happen for incomplete formal types instantiated on a type coming
4135 from a limited_with clause. */
4136 else if (!definition
4137 || No (Freeze_Node (full_view))
4138 || (is_from_limited_with
4139 && !In_Extended_Main_Code_Unit (full_view)))
4141 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4142 maybe_present = true;
4145 /* Otherwise, make a dummy type entry which will be replaced later.
4146 Save it as the full declaration's type so we can do any needed
4147 updates when we see it. */
4148 else
4150 gnu_type = make_dummy_type (gnat_entity);
4151 gnu_decl = TYPE_STUB_DECL (gnu_type);
4152 if (Has_Completion_In_Body (gnat_entity))
4153 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4154 save_gnu_tree (full_view, gnu_decl, false);
4157 break;
4159 case E_Class_Wide_Type:
4160 /* Class-wide types are always transformed into their root type. */
4161 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4162 maybe_present = true;
4163 break;
4165 case E_Protected_Type:
4166 case E_Protected_Subtype:
4167 case E_Task_Type:
4168 case E_Task_Subtype:
4169 /* If we are just annotating types and have no equivalent record type,
4170 just return void_type, except for root types that have discriminants
4171 because the discriminants will very likely be used in the declarative
4172 part of the associated body so they need to be translated. */
4173 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4175 if (Has_Discriminants (gnat_entity)
4176 && Root_Type (gnat_entity) == gnat_entity)
4178 tree gnu_field_list = NULL_TREE;
4179 Entity_Id gnat_field;
4181 /* This is a minimal version of the E_Record_Type handling. */
4182 gnu_type = make_node (RECORD_TYPE);
4183 TYPE_NAME (gnu_type) = gnu_entity_name;
4185 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4186 Present (gnat_field);
4187 gnat_field = Next_Stored_Discriminant (gnat_field))
4189 tree gnu_field
4190 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4191 definition, debug_info_p);
4193 save_gnu_tree (gnat_field,
4194 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4195 build0 (PLACEHOLDER_EXPR, gnu_type),
4196 gnu_field, NULL_TREE),
4197 true);
4199 DECL_CHAIN (gnu_field) = gnu_field_list;
4200 gnu_field_list = gnu_field;
4203 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4204 false);
4206 else
4207 gnu_type = void_type_node;
4210 /* Concurrent types are always transformed into their record type. */
4211 else
4212 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4213 maybe_present = true;
4214 break;
4216 case E_Label:
4217 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4218 break;
4220 case E_Block:
4221 case E_Loop:
4222 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4223 we've already saved it, so we don't try to. */
4224 gnu_decl = error_mark_node;
4225 saved = true;
4226 break;
4228 case E_Abstract_State:
4229 /* This is a SPARK annotation that only reaches here when compiling in
4230 ASIS mode. */
4231 gcc_assert (type_annotate_only);
4232 gnu_decl = error_mark_node;
4233 saved = true;
4234 break;
4236 default:
4237 gcc_unreachable ();
4240 /* If we had a case where we evaluated another type and it might have
4241 defined this one, handle it here. */
4242 if (maybe_present && present_gnu_tree (gnat_entity))
4244 gnu_decl = get_gnu_tree (gnat_entity);
4245 saved = true;
4248 /* If we are processing a type and there is either no decl for it or
4249 we just made one, do some common processing for the type, such as
4250 handling alignment and possible padding. */
4251 if (is_type && (!gnu_decl || this_made_decl))
4253 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4255 /* Process the attributes, if not already done. Note that the type is
4256 already defined so we cannot pass true for IN_PLACE here. */
4257 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4259 /* Tell the middle-end that objects of tagged types are guaranteed to
4260 be properly aligned. This is necessary because conversions to the
4261 class-wide type are translated into conversions to the root type,
4262 which can be less aligned than some of its derived types. */
4263 if (Is_Tagged_Type (gnat_entity)
4264 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4265 TYPE_ALIGN_OK (gnu_type) = 1;
4267 /* Record whether the type is passed by reference. */
4268 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4269 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4271 /* ??? Don't set the size for a String_Literal since it is either
4272 confirming or we don't handle it properly (if the low bound is
4273 non-constant). */
4274 if (!gnu_size && kind != E_String_Literal_Subtype)
4276 Uint gnat_size = Known_Esize (gnat_entity)
4277 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4278 gnu_size
4279 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4280 false, Has_Size_Clause (gnat_entity));
4283 /* If a size was specified, see if we can make a new type of that size
4284 by rearranging the type, for example from a fat to a thin pointer. */
4285 if (gnu_size)
4287 gnu_type
4288 = make_type_from_size (gnu_type, gnu_size,
4289 Has_Biased_Representation (gnat_entity));
4291 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4292 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4293 gnu_size = NULL_TREE;
4296 /* If the alignment has not already been processed and this is not
4297 an unconstrained array type, see if an alignment is specified.
4298 If not, we pick a default alignment for atomic objects. */
4299 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4301 else if (Known_Alignment (gnat_entity))
4303 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4304 TYPE_ALIGN (gnu_type));
4306 /* Warn on suspiciously large alignments. This should catch
4307 errors about the (alignment,byte)/(size,bit) discrepancy. */
4308 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4310 tree size;
4312 /* If a size was specified, take it into account. Otherwise
4313 use the RM size for records or unions as the type size has
4314 already been adjusted to the alignment. */
4315 if (gnu_size)
4316 size = gnu_size;
4317 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4318 && !TYPE_FAT_POINTER_P (gnu_type))
4319 size = rm_size (gnu_type);
4320 else
4321 size = TYPE_SIZE (gnu_type);
4323 /* Consider an alignment as suspicious if the alignment/size
4324 ratio is greater or equal to the byte/bit ratio. */
4325 if (tree_fits_uhwi_p (size)
4326 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4327 post_error_ne ("?suspiciously large alignment specified for&",
4328 Expression (Alignment_Clause (gnat_entity)),
4329 gnat_entity);
4332 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4333 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4334 && integer_pow2p (TYPE_SIZE (gnu_type)))
4335 align = MIN (BIGGEST_ALIGNMENT,
4336 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4337 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4338 && tree_fits_uhwi_p (gnu_size)
4339 && integer_pow2p (gnu_size))
4340 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4342 /* See if we need to pad the type. If we did, and made a record,
4343 the name of the new type may be changed. So get it back for
4344 us when we make the new TYPE_DECL below. */
4345 if (gnu_size || align > 0)
4346 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4347 false, !gnu_decl, definition, false);
4349 if (TYPE_IS_PADDING_P (gnu_type))
4350 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4352 /* Now set the RM size of the type. We cannot do it before padding
4353 because we need to accept arbitrary RM sizes on integral types. */
4354 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4356 /* If we are at global level, GCC will have applied variable_size to
4357 the type, but that won't have done anything. So, if it's not
4358 a constant or self-referential, call elaborate_expression_1 to
4359 make a variable for the size rather than calculating it each time.
4360 Handle both the RM size and the actual size. */
4361 if (TYPE_SIZE (gnu_type)
4362 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4363 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4364 && global_bindings_p ())
4366 tree size = TYPE_SIZE (gnu_type);
4368 TYPE_SIZE (gnu_type)
4369 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4370 false);
4372 /* ??? For now, store the size as a multiple of the alignment in
4373 bytes so that we can see the alignment from the tree. */
4374 TYPE_SIZE_UNIT (gnu_type)
4375 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4376 "SIZE_A_UNIT", definition, false,
4377 TYPE_ALIGN (gnu_type));
4379 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4380 may not be marked by the call to create_type_decl below. */
4381 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4383 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4385 tree variant_part = get_variant_part (gnu_type);
4386 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4388 if (variant_part)
4390 tree union_type = TREE_TYPE (variant_part);
4391 tree offset = DECL_FIELD_OFFSET (variant_part);
4393 /* If the position of the variant part is constant, subtract
4394 it from the size of the type of the parent to get the new
4395 size. This manual CSE reduces the data size. */
4396 if (TREE_CODE (offset) == INTEGER_CST)
4398 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4399 TYPE_SIZE (union_type)
4400 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4401 bit_from_pos (offset, bitpos));
4402 TYPE_SIZE_UNIT (union_type)
4403 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4404 byte_from_pos (offset, bitpos));
4406 else
4408 TYPE_SIZE (union_type)
4409 = elaborate_expression_1 (TYPE_SIZE (union_type),
4410 gnat_entity, "VSIZE",
4411 definition, false);
4413 /* ??? For now, store the size as a multiple of the
4414 alignment in bytes so that we can see the alignment
4415 from the tree. */
4416 TYPE_SIZE_UNIT (union_type)
4417 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4418 gnat_entity, "VSIZE_A_UNIT",
4419 definition, false,
4420 TYPE_ALIGN (union_type));
4422 /* ??? For now, store the offset as a multiple of the
4423 alignment in bytes so that we can see the alignment
4424 from the tree. */
4425 DECL_FIELD_OFFSET (variant_part)
4426 = elaborate_expression_2 (offset, gnat_entity,
4427 "VOFFSET", definition, false,
4428 DECL_OFFSET_ALIGN
4429 (variant_part));
4432 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4433 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4436 if (operand_equal_p (ada_size, size, 0))
4437 ada_size = TYPE_SIZE (gnu_type);
4438 else
4439 ada_size
4440 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4441 definition, false);
4442 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4446 /* Similarly, if this is a record type or subtype at global level, call
4447 elaborate_expression_2 on any field position. Skip any fields that
4448 we haven't made trees for to avoid problems with class-wide types. */
4449 if (IN (kind, Record_Kind) && global_bindings_p ())
4450 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4451 gnat_temp = Next_Entity (gnat_temp))
4452 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4454 tree gnu_field = get_gnu_tree (gnat_temp);
4456 /* ??? For now, store the offset as a multiple of the alignment
4457 in bytes so that we can see the alignment from the tree. */
4458 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4459 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4461 DECL_FIELD_OFFSET (gnu_field)
4462 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4463 gnat_temp, "OFFSET", definition,
4464 false,
4465 DECL_OFFSET_ALIGN (gnu_field));
4467 /* ??? The context of gnu_field is not necessarily gnu_type
4468 so the MULT_EXPR node built above may not be marked by
4469 the call to create_type_decl below. */
4470 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4474 if (Is_Atomic_Or_VFA (gnat_entity))
4475 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4477 /* If this is not an unconstrained array type, set some flags. */
4478 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4480 if (Present (Alignment_Clause (gnat_entity)))
4481 TYPE_USER_ALIGN (gnu_type) = 1;
4483 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4484 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4486 /* If it is passed by reference, force BLKmode to ensure that
4487 objects of this type will always be put in memory. */
4488 if (TYPE_MODE (gnu_type) != BLKmode
4489 && AGGREGATE_TYPE_P (gnu_type)
4490 && TYPE_BY_REFERENCE_P (gnu_type))
4491 SET_TYPE_MODE (gnu_type, BLKmode);
4494 /* If this is a derived type, relate its alias set to that of its parent
4495 to avoid troubles when a call to an inherited primitive is inlined in
4496 a context where a derived object is accessed. The inlined code works
4497 on the parent view so the resulting code may access the same object
4498 using both the parent and the derived alias sets, which thus have to
4499 conflict. As the same issue arises with component references, the
4500 parent alias set also has to conflict with composite types enclosing
4501 derived components. For instance, if we have:
4503 type D is new T;
4504 type R is record
4505 Component : D;
4506 end record;
4508 we want T to conflict with both D and R, in addition to R being a
4509 superset of D by record/component construction.
4511 One way to achieve this is to perform an alias set copy from the
4512 parent to the derived type. This is not quite appropriate, though,
4513 as we don't want separate derived types to conflict with each other:
4515 type I1 is new Integer;
4516 type I2 is new Integer;
4518 We want I1 and I2 to both conflict with Integer but we do not want
4519 I1 to conflict with I2, and an alias set copy on derivation would
4520 have that effect.
4522 The option chosen is to make the alias set of the derived type a
4523 superset of that of its parent type. It trivially fulfills the
4524 simple requirement for the Integer derivation example above, and
4525 the component case as well by superset transitivity:
4527 superset superset
4528 R ----------> D ----------> T
4530 However, for composite types, conversions between derived types are
4531 translated into VIEW_CONVERT_EXPRs so a sequence like:
4533 type Comp1 is new Comp;
4534 type Comp2 is new Comp;
4535 procedure Proc (C : Comp1);
4537 C : Comp2;
4538 Proc (Comp1 (C));
4540 is translated into:
4542 C : Comp2;
4543 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4545 and gimplified into:
4547 C : Comp2;
4548 Comp1 *C.0;
4549 C.0 = (Comp1 *) &C;
4550 Proc (C.0);
4552 i.e. generates code involving type punning. Therefore, Comp1 needs
4553 to conflict with Comp2 and an alias set copy is required.
4555 The language rules ensure the parent type is already frozen here. */
4556 if (kind != E_Subprogram_Type
4557 && Is_Derived_Type (gnat_entity)
4558 && !type_annotate_only)
4560 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4561 /* For constrained packed array subtypes, the implementation type is
4562 used instead of the nominal type. */
4563 if (kind == E_Array_Subtype
4564 && Is_Constrained (gnat_entity)
4565 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4566 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4567 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4568 Is_Composite_Type (gnat_entity)
4569 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4572 if (Treat_As_Volatile (gnat_entity))
4574 const int quals
4575 = TYPE_QUAL_VOLATILE
4576 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4577 gnu_type = change_qualified_type (gnu_type, quals);
4580 if (!gnu_decl)
4581 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4582 artificial_p, debug_info_p,
4583 gnat_entity);
4584 else
4586 TREE_TYPE (gnu_decl) = gnu_type;
4587 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4591 /* If we got a type that is not dummy, back-annotate the alignment of the
4592 type if not already in the tree. Likewise for the size, if any. */
4593 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4595 gnu_type = TREE_TYPE (gnu_decl);
4597 if (Unknown_Alignment (gnat_entity))
4599 unsigned int double_align, align;
4600 bool is_capped_double, align_clause;
4602 /* If the default alignment of "double" or larger scalar types is
4603 specifically capped and this is not an array with an alignment
4604 clause on the component type, return the cap. */
4605 if ((double_align = double_float_alignment) > 0)
4606 is_capped_double
4607 = is_double_float_or_array (gnat_entity, &align_clause);
4608 else if ((double_align = double_scalar_alignment) > 0)
4609 is_capped_double
4610 = is_double_scalar_or_array (gnat_entity, &align_clause);
4611 else
4612 is_capped_double = align_clause = false;
4614 if (is_capped_double && !align_clause)
4615 align = double_align;
4616 else
4617 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4619 Set_Alignment (gnat_entity, UI_From_Int (align));
4622 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4624 tree gnu_size = TYPE_SIZE (gnu_type);
4626 /* If the size is self-referential, annotate the maximum value. */
4627 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4628 gnu_size = max_size (gnu_size, true);
4630 /* If we are just annotating types and the type is tagged, the tag
4631 and the parent components are not generated by the front-end so
4632 alignment and sizes must be adjusted if there is no rep clause. */
4633 if (type_annotate_only
4634 && Is_Tagged_Type (gnat_entity)
4635 && Unknown_RM_Size (gnat_entity)
4636 && !VOID_TYPE_P (gnu_type)
4637 && (!TYPE_FIELDS (gnu_type)
4638 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4640 tree offset;
4642 if (Is_Derived_Type (gnat_entity))
4644 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4645 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4646 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4648 else
4650 unsigned int align
4651 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4652 offset = bitsize_int (POINTER_SIZE);
4653 Set_Alignment (gnat_entity, UI_From_Int (align));
4656 if (TYPE_FIELDS (gnu_type))
4657 offset
4658 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4660 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4661 gnu_size = round_up (gnu_size, POINTER_SIZE);
4662 Uint uint_size = annotate_value (gnu_size);
4663 Set_RM_Size (gnat_entity, uint_size);
4664 Set_Esize (gnat_entity, uint_size);
4667 /* If there is a rep clause, only adjust alignment and Esize. */
4668 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4670 unsigned int align
4671 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4672 Set_Alignment (gnat_entity, UI_From_Int (align));
4673 gnu_size = round_up (gnu_size, POINTER_SIZE);
4674 Set_Esize (gnat_entity, annotate_value (gnu_size));
4677 /* Otherwise no adjustment is needed. */
4678 else
4679 Set_Esize (gnat_entity, annotate_value (gnu_size));
4682 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4683 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4686 /* If we haven't already, associate the ..._DECL node that we just made with
4687 the input GNAT entity node. */
4688 if (!saved)
4689 save_gnu_tree (gnat_entity, gnu_decl, false);
4691 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4692 eliminate as many deferred computations as possible. */
4693 process_deferred_decl_context (false);
4695 /* If this is an enumeration or floating-point type, we were not able to set
4696 the bounds since they refer to the type. These are always static. */
4697 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4698 || (kind == E_Floating_Point_Type))
4700 tree gnu_scalar_type = gnu_type;
4701 tree gnu_low_bound, gnu_high_bound;
4703 /* If this is a padded type, we need to use the underlying type. */
4704 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4705 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4707 /* If this is a floating point type and we haven't set a floating
4708 point type yet, use this in the evaluation of the bounds. */
4709 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4710 longest_float_type_node = gnu_scalar_type;
4712 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4713 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4715 if (kind == E_Enumeration_Type)
4717 /* Enumeration types have specific RM bounds. */
4718 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4719 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4721 else
4723 /* Floating-point types don't have specific RM bounds. */
4724 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4725 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4729 /* If we deferred processing of incomplete types, re-enable it. If there
4730 were no other disables and we have deferred types to process, do so. */
4731 if (this_deferred
4732 && --defer_incomplete_level == 0
4733 && defer_incomplete_list)
4735 struct incomplete *p, *next;
4737 /* We are back to level 0 for the deferring of incomplete types.
4738 But processing these incomplete types below may itself require
4739 deferring, so preserve what we have and restart from scratch. */
4740 p = defer_incomplete_list;
4741 defer_incomplete_list = NULL;
4743 for (; p; p = next)
4745 next = p->next;
4747 if (p->old_type)
4748 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4749 gnat_to_gnu_type (p->full_type));
4750 free (p);
4754 /* If we are not defining this type, see if it's on one of the lists of
4755 incomplete types. If so, handle the list entry now. */
4756 if (is_type && !definition)
4758 struct incomplete *p;
4760 for (p = defer_incomplete_list; p; p = p->next)
4761 if (p->old_type && p->full_type == gnat_entity)
4763 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4764 TREE_TYPE (gnu_decl));
4765 p->old_type = NULL_TREE;
4768 for (p = defer_limited_with_list; p; p = p->next)
4769 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
4771 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4772 TREE_TYPE (gnu_decl));
4773 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4774 update_profiles_with (p->old_type);
4775 p->old_type = NULL_TREE;
4779 if (this_global)
4780 force_global--;
4782 /* If this is a packed array type whose original array type is itself
4783 an Itype without freeze node, make sure the latter is processed. */
4784 if (Is_Packed_Array_Impl_Type (gnat_entity)
4785 && Is_Itype (Original_Array_Type (gnat_entity))
4786 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4787 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4788 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
4790 return gnu_decl;
4793 /* Similar, but if the returned value is a COMPONENT_REF, return the
4794 FIELD_DECL. */
4796 tree
4797 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4799 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4801 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4802 gnu_field = TREE_OPERAND (gnu_field, 1);
4804 return gnu_field;
4807 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4808 the GCC type corresponding to that entity. */
4810 tree
4811 gnat_to_gnu_type (Entity_Id gnat_entity)
4813 tree gnu_decl;
4815 /* The back end never attempts to annotate generic types. */
4816 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4817 return void_type_node;
4819 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4820 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4822 return TREE_TYPE (gnu_decl);
4825 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4826 the unpadded version of the GCC type corresponding to that entity. */
4828 tree
4829 get_unpadded_type (Entity_Id gnat_entity)
4831 tree type = gnat_to_gnu_type (gnat_entity);
4833 if (TYPE_IS_PADDING_P (type))
4834 type = TREE_TYPE (TYPE_FIELDS (type));
4836 return type;
4839 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
4840 a C++ imported method or equivalent.
4842 We use the predicate on 32-bit x86/Windows to find out whether we need to
4843 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
4844 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
4846 bool
4847 is_cplusplus_method (Entity_Id gnat_entity)
4849 /* A constructor is a method on the C++ side. We deal with it now because
4850 it is declared without the 'this' parameter in the sources and, although
4851 the front-end will create a version with the 'this' parameter for code
4852 generation purposes, we want to return true for both versions. */
4853 if (Is_Constructor (gnat_entity))
4854 return true;
4856 /* Check that the subprogram has C++ convention. */
4857 if (Convention (gnat_entity) != Convention_CPP)
4858 return false;
4860 /* And that the type of the first parameter (indirectly) has it too. */
4861 Entity_Id gnat_first = First_Formal (gnat_entity);
4862 if (No (gnat_first))
4863 return false;
4865 Entity_Id gnat_type = Etype (gnat_first);
4866 if (Is_Access_Type (gnat_type))
4867 gnat_type = Directly_Designated_Type (gnat_type);
4868 if (Convention (gnat_type) != Convention_CPP)
4869 return false;
4871 /* This is the main case: a C++ virtual method imported as a primitive
4872 operation of a tagged type. */
4873 if (Is_Dispatching_Operation (gnat_entity))
4874 return true;
4876 /* This is set on the E_Subprogram_Type built for a dispatching call. */
4877 if (Is_Dispatch_Table_Entity (gnat_entity))
4878 return true;
4880 /* A thunk needs to be handled like its associated primitive operation. */
4881 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
4882 return true;
4884 /* Now on to the annoying case: a C++ non-virtual method, imported either
4885 as a non-primitive operation of a tagged type or as a primitive operation
4886 of an untagged type. We cannot reliably differentiate these cases from
4887 their static member or regular function equivalents in Ada, so we ask
4888 the C++ side through the mangled name of the function, as the implicit
4889 'this' parameter is not encoded in the mangled name of a method. */
4890 if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity)))
4892 String_Pointer sp = { NULL, NULL };
4893 Get_External_Name (gnat_entity, false, sp);
4895 void *mem;
4896 struct demangle_component *cmp
4897 = cplus_demangle_v3_components (Name_Buffer,
4898 DMGL_GNU_V3
4899 | DMGL_TYPES
4900 | DMGL_PARAMS
4901 | DMGL_RET_DROP,
4902 &mem);
4903 if (!cmp)
4904 return false;
4906 /* We need to release MEM once we have a successful demangling. */
4907 bool ret = false;
4909 if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME
4910 && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE
4911 && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL
4912 && cmp->type == DEMANGLE_COMPONENT_ARGLIST)
4914 /* Make sure there is at least one parameter in C++ too. */
4915 if (cmp->u.s_binary.left)
4917 unsigned int n_ada_args = 0;
4918 do {
4919 n_ada_args++;
4920 gnat_first = Next_Formal (gnat_first);
4921 } while (Present (gnat_first));
4923 unsigned int n_cpp_args = 0;
4924 do {
4925 n_cpp_args++;
4926 cmp = cmp->u.s_binary.right;
4927 } while (cmp);
4929 if (n_cpp_args < n_ada_args)
4930 ret = true;
4932 else
4933 ret = true;
4936 free (mem);
4938 return ret;
4941 return false;
4944 /* Finalize the processing of From_Limited_With incomplete types. */
4946 void
4947 finalize_from_limited_with (void)
4949 struct incomplete *p, *next;
4951 p = defer_limited_with_list;
4952 defer_limited_with_list = NULL;
4954 for (; p; p = next)
4956 next = p->next;
4958 if (p->old_type)
4960 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4961 gnat_to_gnu_type (p->full_type));
4962 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4963 update_profiles_with (p->old_type);
4966 free (p);
4970 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind
4971 of type (such E_Task_Type) that has a different type which Gigi uses
4972 for its representation. If the type does not have a special type for
4973 its representation, return GNAT_ENTITY. */
4975 Entity_Id
4976 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4978 Entity_Id gnat_equiv = gnat_entity;
4980 if (No (gnat_entity))
4981 return gnat_entity;
4983 switch (Ekind (gnat_entity))
4985 case E_Class_Wide_Subtype:
4986 if (Present (Equivalent_Type (gnat_entity)))
4987 gnat_equiv = Equivalent_Type (gnat_entity);
4988 break;
4990 case E_Access_Protected_Subprogram_Type:
4991 case E_Anonymous_Access_Protected_Subprogram_Type:
4992 if (Present (Equivalent_Type (gnat_entity)))
4993 gnat_equiv = Equivalent_Type (gnat_entity);
4994 break;
4996 case E_Class_Wide_Type:
4997 gnat_equiv = Root_Type (gnat_entity);
4998 break;
5000 case E_Protected_Type:
5001 case E_Protected_Subtype:
5002 case E_Task_Type:
5003 case E_Task_Subtype:
5004 if (Present (Corresponding_Record_Type (gnat_entity)))
5005 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5006 break;
5008 default:
5009 break;
5012 return gnat_equiv;
5015 /* Return a GCC tree for a type corresponding to the component type of the
5016 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5017 is for an array being defined. DEBUG_INFO_P is true if we need to write
5018 debug information for other types that we may create in the process. */
5020 static tree
5021 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5022 bool debug_info_p)
5024 const Entity_Id gnat_type = Component_Type (gnat_array);
5025 tree gnu_type = gnat_to_gnu_type (gnat_type);
5026 tree gnu_comp_size;
5027 unsigned int max_align;
5029 /* If an alignment is specified, use it as a cap on the component type
5030 so that it can be honored for the whole type. But ignore it for the
5031 original type of packed array types. */
5032 if (No (Packed_Array_Impl_Type (gnat_array))
5033 && Known_Alignment (gnat_array))
5034 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5035 else
5036 max_align = 0;
5038 /* Try to get a smaller form of the component if needed. */
5039 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5040 && !Is_Bit_Packed_Array (gnat_array)
5041 && !Has_Aliased_Components (gnat_array)
5042 && !Strict_Alignment (gnat_type)
5043 && RECORD_OR_UNION_TYPE_P (gnu_type)
5044 && !TYPE_FAT_POINTER_P (gnu_type)
5045 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5046 gnu_type = make_packable_type (gnu_type, false, max_align);
5048 if (Has_Atomic_Components (gnat_array))
5049 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5051 /* Get and validate any specified Component_Size. */
5052 gnu_comp_size
5053 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5054 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5055 true, Has_Component_Size_Clause (gnat_array));
5057 /* If the array has aliased components and the component size can be zero,
5058 force at least unit size to ensure that the components have distinct
5059 addresses. */
5060 if (!gnu_comp_size
5061 && Has_Aliased_Components (gnat_array)
5062 && (integer_zerop (TYPE_SIZE (gnu_type))
5063 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5064 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5065 gnu_comp_size
5066 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5068 /* If the component type is a RECORD_TYPE that has a self-referential size,
5069 then use the maximum size for the component size. */
5070 if (!gnu_comp_size
5071 && TREE_CODE (gnu_type) == RECORD_TYPE
5072 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5073 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5075 /* Honor the component size. This is not needed for bit-packed arrays. */
5076 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5078 tree orig_type = gnu_type;
5080 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5081 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5082 gnu_type = orig_type;
5083 else
5084 orig_type = gnu_type;
5086 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5087 true, false, definition, true);
5089 /* If a padding record was made, declare it now since it will never be
5090 declared otherwise. This is necessary to ensure that its subtrees
5091 are properly marked. */
5092 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5093 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5094 gnat_array);
5097 /* If the component type is a padded type made for a non-bit-packed array
5098 of scalars with reverse storage order, we need to propagate the reverse
5099 storage order to the padding type since it is the innermost enclosing
5100 aggregate type around the scalar. */
5101 if (TYPE_IS_PADDING_P (gnu_type)
5102 && Reverse_Storage_Order (gnat_array)
5103 && !Is_Bit_Packed_Array (gnat_array)
5104 && Is_Scalar_Type (gnat_type))
5105 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5107 if (Has_Volatile_Components (gnat_array))
5109 const int quals
5110 = TYPE_QUAL_VOLATILE
5111 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5112 gnu_type = change_qualified_type (gnu_type, quals);
5115 return gnu_type;
5118 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5119 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5120 the type of the parameter. FIRST is true if this is the first parameter in
5121 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5122 the copy-in copy-out implementation mechanism.
5124 The returned tree is a PARM_DECL, except for the cases where no parameter
5125 needs to be actually passed to the subprogram; the type of this "shadow"
5126 parameter is then returned instead. */
5128 static tree
5129 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5130 Entity_Id gnat_subprog, bool *cico)
5132 Entity_Id gnat_param_type = Etype (gnat_param);
5133 Mechanism_Type mech = Mechanism (gnat_param);
5134 tree gnu_param_name = get_entity_name (gnat_param);
5135 bool foreign = Has_Foreign_Convention (gnat_subprog);
5136 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5137 /* The parameter can be indirectly modified if its address is taken. */
5138 bool ro_param = in_param && !Address_Taken (gnat_param);
5139 bool by_return = false, by_component_ptr = false;
5140 bool by_ref = false;
5141 bool restricted_aliasing_p = false;
5142 location_t saved_location = input_location;
5143 tree gnu_param;
5145 /* Make sure to use the proper SLOC for vector ABI warnings. */
5146 if (VECTOR_TYPE_P (gnu_param_type))
5147 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5149 /* Builtins are expanded inline and there is no real call sequence involved.
5150 So the type expected by the underlying expander is always the type of the
5151 argument "as is". */
5152 if (Convention (gnat_subprog) == Convention_Intrinsic
5153 && Present (Interface_Name (gnat_subprog)))
5154 mech = By_Copy;
5156 /* Handle the first parameter of a valued procedure specially: it's a copy
5157 mechanism for which the parameter is never allocated. */
5158 else if (first && Is_Valued_Procedure (gnat_subprog))
5160 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5161 mech = By_Copy;
5162 by_return = true;
5165 /* Or else, see if a Mechanism was supplied that forced this parameter
5166 to be passed one way or another. */
5167 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5170 /* Positive mechanism means by copy for sufficiently small parameters. */
5171 else if (mech > 0)
5173 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5174 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5175 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5176 mech = By_Reference;
5177 else
5178 mech = By_Copy;
5181 /* Otherwise, it's an unsupported mechanism so error out. */
5182 else
5184 post_error ("unsupported mechanism for&", gnat_param);
5185 mech = Default;
5188 /* If this is either a foreign function or if the underlying type won't
5189 be passed by reference and is as aligned as the original type, strip
5190 off possible padding type. */
5191 if (TYPE_IS_PADDING_P (gnu_param_type))
5193 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5195 if (foreign
5196 || (!must_pass_by_ref (unpadded_type)
5197 && mech != By_Reference
5198 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5199 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5200 gnu_param_type = unpadded_type;
5203 /* If this is a read-only parameter, make a variant of the type that is
5204 read-only. ??? However, if this is a self-referential type, the type
5205 can be very complex, so skip it for now. */
5206 if (ro_param && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5207 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5209 /* For foreign conventions, pass arrays as pointers to the element type.
5210 First check for unconstrained array and get the underlying array. */
5211 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5212 gnu_param_type
5213 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5215 /* Arrays are passed as pointers to element type for foreign conventions. */
5216 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5218 /* Strip off any multi-dimensional entries, then strip
5219 off the last array to get the component type. */
5220 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5221 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5222 gnu_param_type = TREE_TYPE (gnu_param_type);
5224 by_component_ptr = true;
5225 gnu_param_type = TREE_TYPE (gnu_param_type);
5227 if (ro_param)
5228 gnu_param_type
5229 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5231 gnu_param_type = build_pointer_type (gnu_param_type);
5234 /* Fat pointers are passed as thin pointers for foreign conventions. */
5235 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5236 gnu_param_type
5237 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5239 /* If we were requested or muss pass by reference, do so.
5240 If we were requested to pass by copy, do so.
5241 Otherwise, for foreign conventions, pass In Out or Out parameters
5242 or aggregates by reference. For COBOL and Fortran, pass all
5243 integer and FP types that way too. For Convention Ada, use
5244 the standard Ada default. */
5245 else if (mech == By_Reference
5246 || must_pass_by_ref (gnu_param_type)
5247 || (mech != By_Copy
5248 && ((foreign
5249 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5250 || (foreign
5251 && (Convention (gnat_subprog) == Convention_Fortran
5252 || Convention (gnat_subprog) == Convention_COBOL)
5253 && (INTEGRAL_TYPE_P (gnu_param_type)
5254 || FLOAT_TYPE_P (gnu_param_type)))
5255 || (!foreign
5256 && default_pass_by_ref (gnu_param_type)))))
5258 /* We take advantage of 6.2(12) by considering that references built for
5259 parameters whose type isn't by-ref and for which the mechanism hasn't
5260 been forced to by-ref allow only a restricted form of aliasing. */
5261 restricted_aliasing_p
5262 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5263 gnu_param_type = build_reference_type (gnu_param_type);
5264 by_ref = true;
5267 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5268 else if (!in_param)
5269 *cico = true;
5271 input_location = saved_location;
5273 if (mech == By_Copy && (by_ref || by_component_ptr))
5274 post_error ("?cannot pass & by copy", gnat_param);
5276 /* If this is an Out parameter that isn't passed by reference and isn't
5277 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5278 it will be a VAR_DECL created when we process the procedure, so just
5279 return its type. For the special parameter of a valued procedure,
5280 never pass it in.
5282 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5283 Out parameters with discriminants or implicit initial values to be
5284 handled like In Out parameters. These type are normally built as
5285 aggregates, hence passed by reference, except for some packed arrays
5286 which end up encoded in special integer types. Note that scalars can
5287 be given implicit initial values using the Default_Value aspect.
5289 The exception we need to make is then for packed arrays of records
5290 with discriminants or implicit initial values. We have no light/easy
5291 way to check for the latter case, so we merely check for packed arrays
5292 of records. This may lead to useless copy-in operations, but in very
5293 rare cases only, as these would be exceptions in a set of already
5294 exceptional situations. */
5295 if (Ekind (gnat_param) == E_Out_Parameter
5296 && !by_ref
5297 && (by_return
5298 || (!POINTER_TYPE_P (gnu_param_type)
5299 && !AGGREGATE_TYPE_P (gnu_param_type)
5300 && !Has_Default_Aspect (gnat_param_type)))
5301 && !(Is_Array_Type (gnat_param_type)
5302 && Is_Packed (gnat_param_type)
5303 && Is_Composite_Type (Component_Type (gnat_param_type))))
5304 return gnu_param_type;
5306 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5307 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5308 DECL_BY_REF_P (gnu_param) = by_ref;
5309 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5310 DECL_POINTS_TO_READONLY_P (gnu_param)
5311 = (ro_param && (by_ref || by_component_ptr));
5312 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5313 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5314 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5316 /* If no Mechanism was specified, indicate what we're using, then
5317 back-annotate it. */
5318 if (mech == Default)
5319 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5321 Set_Mechanism (gnat_param, mech);
5322 return gnu_param;
5325 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5326 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5328 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5329 the corresponding profile, which means that, by the time the freeze node
5330 of the subprogram is encountered, types involved in its profile may still
5331 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5332 the freeze node of types involved in its profile, either types of formal
5333 parameters or the return type. */
5335 static void
5336 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5338 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5340 struct tree_entity_vec_map in;
5341 in.base.from = gnu_type;
5342 struct tree_entity_vec_map **slot
5343 = dummy_to_subprog_map->find_slot (&in, INSERT);
5344 if (!*slot)
5346 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5347 e->base.from = gnu_type;
5348 e->to = NULL;
5349 *slot = e;
5352 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5353 because the vector might have been just emptied by update_profiles_with.
5354 This can happen when there are 2 freeze nodes associated with different
5355 views of the same type; the type will be really complete only after the
5356 second freeze node is encountered. */
5357 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5359 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5361 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5362 since this would mean updating twice its profile. */
5363 if (v)
5365 const unsigned len = v->length ();
5366 unsigned int l = 0, u = len;
5368 /* Entity_Id is a simple integer so we can implement a stable order on
5369 the vector with an ordered insertion scheme and binary search. */
5370 while (l < u)
5372 unsigned int m = (l + u) / 2;
5373 int diff = (int) (*v)[m] - (int) gnat_subprog;
5374 if (diff > 0)
5375 u = m;
5376 else if (diff < 0)
5377 l = m + 1;
5378 else
5379 return;
5382 /* l == u and therefore is the insertion point. */
5383 vec_safe_insert (v, l, gnat_subprog);
5385 else
5386 vec_safe_push (v, gnat_subprog);
5388 (*slot)->to = v;
5391 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5393 static void
5394 update_profile (Entity_Id gnat_subprog)
5396 tree gnu_param_list;
5397 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5398 Needs_Debug_Info (gnat_subprog),
5399 &gnu_param_list);
5400 if (DECL_P (gnu_type))
5402 /* Builtins cannot have their address taken so we can reset them. */
5403 gcc_assert (DECL_BUILT_IN (gnu_type));
5404 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5405 save_gnu_tree (gnat_subprog, gnu_type, false);
5406 return;
5409 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5411 TREE_TYPE (gnu_subprog) = gnu_type;
5413 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5414 and needs to be adjusted too. */
5415 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5417 tree gnu_entity_name = get_entity_name (gnat_subprog);
5418 tree gnu_ext_name
5419 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5421 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5422 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5426 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5427 a dummy type which appears in profiles. */
5429 void
5430 update_profiles_with (tree gnu_type)
5432 struct tree_entity_vec_map in;
5433 in.base.from = gnu_type;
5434 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5435 gcc_assert (e);
5436 vec<Entity_Id, va_gc_atomic> *v = e->to;
5437 e->to = NULL;
5439 /* The flag needs to be reset before calling update_profile, in case
5440 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5441 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5443 unsigned int i;
5444 Entity_Id *iter;
5445 FOR_EACH_VEC_ELT (*v, i, iter)
5446 update_profile (*iter);
5448 vec_free (v);
5451 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5453 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5454 context may now appear as parameter and result types. As a consequence,
5455 we may need to defer their translation until after a freeze node is seen
5456 or to the end of the current unit. We also aim at handling temporarily
5457 incomplete types created by the usual delayed elaboration scheme. */
5459 static tree
5460 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5462 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5463 so the rationale is exposed in that place. These processings probably
5464 ought to be merged at some point. */
5465 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5466 const bool is_from_limited_with
5467 = (IN (Ekind (gnat_equiv), Incomplete_Kind)
5468 && From_Limited_With (gnat_equiv));
5469 Entity_Id gnat_full_direct_first
5470 = (is_from_limited_with
5471 ? Non_Limited_View (gnat_equiv)
5472 : (IN (Ekind (gnat_equiv), Incomplete_Or_Private_Kind)
5473 ? Full_View (gnat_equiv) : Empty));
5474 Entity_Id gnat_full_direct
5475 = ((is_from_limited_with
5476 && Present (gnat_full_direct_first)
5477 && IN (Ekind (gnat_full_direct_first), Private_Kind))
5478 ? Full_View (gnat_full_direct_first)
5479 : gnat_full_direct_first);
5480 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5481 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5482 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5483 tree gnu_type;
5485 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5486 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5488 else if (is_from_limited_with
5489 && ((!in_main_unit
5490 && !present_gnu_tree (gnat_equiv)
5491 && Present (gnat_full)
5492 && (Is_Record_Type (gnat_full)
5493 || Is_Array_Type (gnat_full)
5494 || Is_Access_Type (gnat_full)))
5495 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5497 gnu_type = make_dummy_type (gnat_equiv);
5499 if (!in_main_unit)
5501 struct incomplete *p = XNEW (struct incomplete);
5503 p->old_type = gnu_type;
5504 p->full_type = gnat_equiv;
5505 p->next = defer_limited_with_list;
5506 defer_limited_with_list = p;
5510 else if (type_annotate_only && No (gnat_equiv))
5511 gnu_type = void_type_node;
5513 else
5514 gnu_type = gnat_to_gnu_type (gnat_equiv);
5516 /* Access-to-unconstrained-array types need a special treatment. */
5517 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5519 if (!TYPE_POINTER_TO (gnu_type))
5520 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5523 return gnu_type;
5526 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5527 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5528 is true if we need to write debug information for other types that we may
5529 create in the process. Also set PARAM_LIST to the list of parameters.
5530 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5531 directly instead of its type. */
5533 static tree
5534 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5535 bool debug_info_p, tree *param_list)
5537 const Entity_Kind kind = Ekind (gnat_subprog);
5538 Entity_Id gnat_return_type = Etype (gnat_subprog);
5539 Entity_Id gnat_param;
5540 tree gnu_type = present_gnu_tree (gnat_subprog)
5541 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5542 tree gnu_return_type;
5543 tree gnu_param_type_list = NULL_TREE;
5544 tree gnu_param_list = NULL_TREE;
5545 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5546 (In Out or Out parameters not passed by reference), in which case it is
5547 the list of nodes used to specify the values of the In Out/Out parameters
5548 that are returned as a record upon procedure return. The TREE_PURPOSE of
5549 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5550 is the PARM_DECL corresponding to that field. This list will be saved in
5551 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5552 tree gnu_cico_list = NULL_TREE;
5553 tree gnu_cico_return_type = NULL_TREE;
5554 /* Fields in return type of procedure with copy-in copy-out parameters. */
5555 tree gnu_field_list = NULL_TREE;
5556 /* The semantics of "pure" in Ada essentially matches that of "const"
5557 in the back-end. In particular, both properties are orthogonal to
5558 the "nothrow" property if the EH circuitry is explicit in the
5559 internal representation of the back-end. If we are to completely
5560 hide the EH circuitry from it, we need to declare that calls to pure
5561 Ada subprograms that can throw have side effects since they can
5562 trigger an "abnormal" transfer of control flow; thus they can be
5563 neither "const" nor "pure" in the back-end sense. */
5564 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5565 bool return_by_direct_ref_p = false;
5566 bool return_by_invisi_ref_p = false;
5567 bool return_unconstrained_p = false;
5568 bool incomplete_profile_p = false;
5569 unsigned int num;
5571 /* Look into the return type and get its associated GCC tree if it is not
5572 void, and then compute various flags for the subprogram type. But make
5573 sure not to do this processing multiple times. */
5574 if (Ekind (gnat_return_type) == E_Void)
5575 gnu_return_type = void_type_node;
5577 else if (gnu_type
5578 && TREE_CODE (gnu_type) == FUNCTION_TYPE
5579 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5581 gnu_return_type = TREE_TYPE (gnu_type);
5582 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5583 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5584 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5587 else
5589 /* For foreign convention subprograms, return System.Address as void *
5590 or equivalent. Note that this comprises GCC builtins. */
5591 if (Has_Foreign_Convention (gnat_subprog)
5592 && Is_Descendant_Of_Address (gnat_return_type))
5593 gnu_return_type = ptr_type_node;
5594 else
5595 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5597 /* If this function returns by reference, make the actual return type
5598 the reference type and make a note of that. */
5599 if (Returns_By_Ref (gnat_subprog))
5601 gnu_return_type = build_reference_type (gnu_return_type);
5602 return_by_direct_ref_p = true;
5605 /* If the return type is an unconstrained array type, the return value
5606 will be allocated on the secondary stack so the actual return type
5607 is the fat pointer type. */
5608 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5610 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5611 return_unconstrained_p = true;
5614 /* This is the same unconstrained array case, but for a dummy type. */
5615 else if (TYPE_REFERENCE_TO (gnu_return_type)
5616 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5618 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5619 return_unconstrained_p = true;
5622 /* Likewise, if the return type requires a transient scope, the return
5623 value will also be allocated on the secondary stack so the actual
5624 return type is the reference type. */
5625 else if (Requires_Transient_Scope (gnat_return_type))
5627 gnu_return_type = build_reference_type (gnu_return_type);
5628 return_unconstrained_p = true;
5631 /* If the Mechanism is By_Reference, ensure this function uses the
5632 target's by-invisible-reference mechanism, which may not be the
5633 same as above (e.g. it might be passing an extra parameter). */
5634 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5635 return_by_invisi_ref_p = true;
5637 /* Likewise, if the return type is itself By_Reference. */
5638 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5639 return_by_invisi_ref_p = true;
5641 /* If the type is a padded type and the underlying type would not be
5642 passed by reference or the function has a foreign convention, return
5643 the underlying type. */
5644 else if (TYPE_IS_PADDING_P (gnu_return_type)
5645 && (!default_pass_by_ref
5646 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5647 || Has_Foreign_Convention (gnat_subprog)))
5648 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5650 /* If the return type is unconstrained, it must have a maximum size.
5651 Use the padded type as the effective return type. And ensure the
5652 function uses the target's by-invisible-reference mechanism to
5653 avoid copying too much data when it returns. */
5654 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5656 tree orig_type = gnu_return_type;
5657 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5659 /* If the size overflows to 0, set it to an arbitrary positive
5660 value so that assignments in the type are preserved. Their
5661 actual size is independent of this positive value. */
5662 if (TREE_CODE (max_return_size) == INTEGER_CST
5663 && TREE_OVERFLOW (max_return_size)
5664 && integer_zerop (max_return_size))
5666 max_return_size = copy_node (bitsize_unit_node);
5667 TREE_OVERFLOW (max_return_size) = 1;
5670 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5671 0, gnat_subprog, false, false,
5672 definition, true);
5674 /* Declare it now since it will never be declared otherwise. This
5675 is necessary to ensure that its subtrees are properly marked. */
5676 if (gnu_return_type != orig_type
5677 && !DECL_P (TYPE_NAME (gnu_return_type)))
5678 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5679 true, debug_info_p, gnat_subprog);
5681 return_by_invisi_ref_p = true;
5684 /* If the return type has a size that overflows, we usually cannot have
5685 a function that returns that type. This usage doesn't really make
5686 sense anyway, so issue an error here. */
5687 if (!return_by_invisi_ref_p
5688 && TYPE_SIZE_UNIT (gnu_return_type)
5689 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5690 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5692 post_error ("cannot return type whose size overflows", gnat_subprog);
5693 gnu_return_type = copy_type (gnu_return_type);
5694 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5695 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5698 /* If the return type is incomplete, there are 2 cases: if the function
5699 returns by reference, then the return type is only linked indirectly
5700 in the profile, so the profile can be seen as complete since it need
5701 not be further modified, only the reference types need be adjusted;
5702 otherwise the profile is incomplete and need be adjusted too. */
5703 if (TYPE_IS_DUMMY_P (gnu_return_type))
5705 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5706 incomplete_profile_p = true;
5709 if (kind == E_Function)
5710 Set_Mechanism (gnat_subprog, return_unconstrained_p
5711 || return_by_direct_ref_p
5712 || return_by_invisi_ref_p
5713 ? By_Reference : By_Copy);
5716 /* A procedure (something that doesn't return anything) shouldn't be
5717 considered const since there would be no reason for calling such a
5718 subprogram. Note that procedures with Out (or In Out) parameters
5719 have already been converted into a function with a return type.
5720 Similarly, if the function returns an unconstrained type, then the
5721 function will allocate the return value on the secondary stack and
5722 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5723 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5724 const_flag = false;
5726 /* Loop over the parameters and get their associated GCC tree. While doing
5727 this, build a copy-in copy-out structure if we need one. */
5728 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5729 Present (gnat_param);
5730 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5732 const bool mech_is_by_ref
5733 = Mechanism (gnat_param) == By_Reference
5734 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5735 tree gnu_param_name = get_entity_name (gnat_param);
5736 tree gnu_param, gnu_param_type;
5737 bool cico = false;
5739 /* Fetch an existing parameter with complete type and reuse it. But we
5740 didn't save the CICO property so we can only do it for In parameters
5741 or parameters passed by reference. */
5742 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5743 && present_gnu_tree (gnat_param)
5744 && (gnu_param = get_gnu_tree (gnat_param))
5745 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5747 DECL_CHAIN (gnu_param) = NULL_TREE;
5748 gnu_param_type = TREE_TYPE (gnu_param);
5751 /* Otherwise translate the parameter type and act accordingly. */
5752 else
5754 Entity_Id gnat_param_type = Etype (gnat_param);
5756 /* For foreign convention subprograms, pass System.Address as void *
5757 or equivalent. Note that this comprises GCC builtins. */
5758 if (Has_Foreign_Convention (gnat_subprog)
5759 && Is_Descendant_Of_Address (gnat_param_type))
5760 gnu_param_type = ptr_type_node;
5761 else
5762 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5764 /* If the parameter type is incomplete, there are 2 cases: if it is
5765 passed by reference, then the type is only linked indirectly in
5766 the profile, so the profile can be seen as complete since it need
5767 not be further modified, only the reference type need be adjusted;
5768 otherwise the profile is incomplete and need be adjusted too. */
5769 if (TYPE_IS_DUMMY_P (gnu_param_type))
5771 Node_Id gnat_decl;
5773 if (mech_is_by_ref
5774 || (TYPE_REFERENCE_TO (gnu_param_type)
5775 && TYPE_IS_FAT_POINTER_P
5776 (TYPE_REFERENCE_TO (gnu_param_type)))
5777 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5779 gnu_param_type = build_reference_type (gnu_param_type);
5780 gnu_param
5781 = create_param_decl (gnu_param_name, gnu_param_type);
5782 TREE_READONLY (gnu_param) = 1;
5783 DECL_BY_REF_P (gnu_param) = 1;
5784 DECL_POINTS_TO_READONLY_P (gnu_param)
5785 = (Ekind (gnat_param) == E_In_Parameter
5786 && !Address_Taken (gnat_param));
5787 Set_Mechanism (gnat_param, By_Reference);
5788 Sloc_to_locus (Sloc (gnat_param),
5789 &DECL_SOURCE_LOCATION (gnu_param));
5792 /* ??? This is a kludge to support null procedures in spec taking
5793 a parameter with an untagged incomplete type coming from a
5794 limited context. The front-end creates a body without knowing
5795 anything about the non-limited view, which is illegal Ada and
5796 cannot be supported. Create a parameter with a fake type. */
5797 else if (kind == E_Procedure
5798 && (gnat_decl = Parent (gnat_subprog))
5799 && Nkind (gnat_decl) == N_Procedure_Specification
5800 && Null_Present (gnat_decl)
5801 && IN (Ekind (gnat_param_type), Incomplete_Kind))
5802 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5804 else
5806 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
5807 Call_to_gnu will stop if it encounters the PARM_DECL. */
5808 gnu_param
5809 = build_decl (input_location, PARM_DECL, gnu_param_name,
5810 gnu_param_type);
5811 associate_subprog_with_dummy_type (gnat_subprog,
5812 gnu_param_type);
5813 incomplete_profile_p = true;
5817 /* Otherwise build the parameter declaration normally. */
5818 else
5820 gnu_param
5821 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
5822 gnat_subprog, &cico);
5824 /* We are returned either a PARM_DECL or a type if no parameter
5825 needs to be passed; in either case, adjust the type. */
5826 if (DECL_P (gnu_param))
5827 gnu_param_type = TREE_TYPE (gnu_param);
5828 else
5830 gnu_param_type = gnu_param;
5831 gnu_param = NULL_TREE;
5836 /* If we have a GCC tree for the parameter, register it. */
5837 save_gnu_tree (gnat_param, NULL_TREE, false);
5838 if (gnu_param)
5840 gnu_param_type_list
5841 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
5842 gnu_param_list = chainon (gnu_param, gnu_param_list);
5843 save_gnu_tree (gnat_param, gnu_param, false);
5845 /* If a parameter is a pointer, a function may modify memory through
5846 it and thus shouldn't be considered a const function. Also, the
5847 memory may be modified between two calls, so they can't be CSE'ed.
5848 The latter case also handles by-ref parameters. */
5849 if (POINTER_TYPE_P (gnu_param_type)
5850 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
5851 const_flag = false;
5854 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5855 for it in the return type and register the association. */
5856 if (cico && !incomplete_profile_p)
5858 if (!gnu_cico_list)
5860 gnu_cico_return_type = make_node (RECORD_TYPE);
5862 /* If this is a function, we also need a field for the
5863 return value to be placed. */
5864 if (!VOID_TYPE_P (gnu_return_type))
5866 tree gnu_field
5867 = create_field_decl (get_identifier ("RETVAL"),
5868 gnu_return_type,
5869 gnu_cico_return_type, NULL_TREE,
5870 NULL_TREE, 0, 0);
5871 Sloc_to_locus (Sloc (gnat_subprog),
5872 &DECL_SOURCE_LOCATION (gnu_field));
5873 gnu_field_list = gnu_field;
5874 gnu_cico_list
5875 = tree_cons (gnu_field, void_type_node, NULL_TREE);
5878 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
5879 /* Set a default alignment to speed up accesses. But we should
5880 not increase the size of the structure too much, lest it does
5881 not fit in return registers anymore. */
5882 SET_TYPE_ALIGN (gnu_cico_return_type,
5883 get_mode_alignment (ptr_mode));
5886 tree gnu_field
5887 = create_field_decl (gnu_param_name, gnu_param_type,
5888 gnu_cico_return_type, NULL_TREE, NULL_TREE,
5889 0, 0);
5890 Sloc_to_locus (Sloc (gnat_param),
5891 &DECL_SOURCE_LOCATION (gnu_field));
5892 DECL_CHAIN (gnu_field) = gnu_field_list;
5893 gnu_field_list = gnu_field;
5894 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
5898 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5899 and finish up the return type. */
5900 if (gnu_cico_list && !incomplete_profile_p)
5902 /* If we have a CICO list but it has only one entry, we convert
5903 this function into a function that returns this object. */
5904 if (list_length (gnu_cico_list) == 1)
5905 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
5907 /* Do not finalize the return type if the subprogram is stubbed
5908 since structures are incomplete for the back-end. */
5909 else if (Convention (gnat_subprog) != Convention_Stubbed)
5911 finish_record_type (gnu_cico_return_type, nreverse (gnu_field_list),
5912 0, false);
5914 /* Try to promote the mode of the return type if it is passed
5915 in registers, again to speed up accesses. */
5916 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
5917 && !targetm.calls.return_in_memory (gnu_cico_return_type,
5918 NULL_TREE))
5920 unsigned int size
5921 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
5922 unsigned int i = BITS_PER_UNIT;
5923 machine_mode mode;
5925 while (i < size)
5926 i <<= 1;
5927 mode = mode_for_size (i, MODE_INT, 0);
5928 if (mode != BLKmode)
5930 SET_TYPE_MODE (gnu_cico_return_type, mode);
5931 SET_TYPE_ALIGN (gnu_cico_return_type,
5932 GET_MODE_ALIGNMENT (mode));
5933 TYPE_SIZE (gnu_cico_return_type)
5934 = bitsize_int (GET_MODE_BITSIZE (mode));
5935 TYPE_SIZE_UNIT (gnu_cico_return_type)
5936 = size_int (GET_MODE_SIZE (mode));
5940 if (debug_info_p)
5941 rest_of_record_type_compilation (gnu_cico_return_type);
5944 gnu_return_type = gnu_cico_return_type;
5947 /* The lists have been built in reverse. */
5948 gnu_param_type_list = nreverse (gnu_param_type_list);
5949 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
5950 *param_list = nreverse (gnu_param_list);
5951 gnu_cico_list = nreverse (gnu_cico_list);
5953 /* If the profile is incomplete, we only set the (temporary) return and
5954 parameter types; otherwise, we build the full type. In either case,
5955 we reuse an already existing GCC tree that we built previously here. */
5956 if (incomplete_profile_p)
5958 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5960 else
5961 gnu_type = make_node (FUNCTION_TYPE);
5962 TREE_TYPE (gnu_type) = gnu_return_type;
5963 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5964 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5965 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5966 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5968 else
5970 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5972 TREE_TYPE (gnu_type) = gnu_return_type;
5973 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5974 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5975 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5976 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5977 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5978 TYPE_CANONICAL (gnu_type) = gnu_type;
5979 layout_type (gnu_type);
5981 else
5983 gnu_type
5984 = build_function_type (gnu_return_type, gnu_param_type_list);
5986 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
5987 has a different TYPE_CI_CO_LIST or flags. */
5988 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
5989 return_unconstrained_p,
5990 return_by_direct_ref_p,
5991 return_by_invisi_ref_p))
5993 gnu_type = copy_type (gnu_type);
5994 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5995 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5996 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5997 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6001 if (const_flag)
6002 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6004 if (No_Return (gnat_subprog))
6005 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6007 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6008 corresponding DECL node and check the parameter association. */
6009 if (Convention (gnat_subprog) == Convention_Intrinsic
6010 && Present (Interface_Name (gnat_subprog)))
6012 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6013 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6015 /* If we have a builtin DECL for that function, use it. Check if
6016 the profiles are compatible and warn if they are not. Note that
6017 the checker is expected to post diagnostics in this case. */
6018 if (gnu_builtin_decl)
6020 intrin_binding_t inb
6021 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6023 if (!intrin_profiles_compatible_p (&inb))
6024 post_error
6025 ("?profile of& doesn''t match the builtin it binds!",
6026 gnat_subprog);
6028 return gnu_builtin_decl;
6031 /* Inability to find the builtin DECL most often indicates a genuine
6032 mistake, but imports of unregistered intrinsics are sometimes used
6033 on purpose to allow hooking in alternate bodies; we post a warning
6034 conditioned on Wshadow in this case, to let developers be notified
6035 on demand without risking false positives with common default sets
6036 of options. */
6037 if (warn_shadow)
6038 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6042 return gnu_type;
6045 /* Return the external name for GNAT_SUBPROG given its entity name. */
6047 static tree
6048 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6050 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6052 /* If there was no specified Interface_Name and the external and
6053 internal names of the subprogram are the same, only use the
6054 internal name to allow disambiguation of nested subprograms. */
6055 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6056 gnu_ext_name = NULL_TREE;
6058 return gnu_ext_name;
6061 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6062 qualifiers on TYPE. */
6064 static tree
6065 change_qualified_type (tree type, int type_quals)
6067 /* Qualifiers must be put on the associated array type. */
6068 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
6069 return type;
6071 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6074 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6075 build_nonshared_array_type. */
6077 static void
6078 set_nonaliased_component_on_array_type (tree type)
6080 TYPE_NONALIASED_COMPONENT (type) = 1;
6081 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6084 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6085 build_nonshared_array_type. */
6087 static void
6088 set_reverse_storage_order_on_array_type (tree type)
6090 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6091 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6094 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6096 static bool
6097 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6099 while (Present (Corresponding_Discriminant (discr1)))
6100 discr1 = Corresponding_Discriminant (discr1);
6102 while (Present (Corresponding_Discriminant (discr2)))
6103 discr2 = Corresponding_Discriminant (discr2);
6105 return
6106 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6109 /* Return true if the array type GNU_TYPE, which represents a dimension of
6110 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6112 static bool
6113 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6115 /* If the array type is not the innermost dimension of the GNAT type,
6116 then it has a non-aliased component. */
6117 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6118 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6119 return true;
6121 /* If the array type has an aliased component in the front-end sense,
6122 then it also has an aliased component in the back-end sense. */
6123 if (Has_Aliased_Components (gnat_type))
6124 return false;
6126 /* If this is a derived type, then it has a non-aliased component if
6127 and only if its parent type also has one. */
6128 if (Is_Derived_Type (gnat_type))
6130 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6131 int index;
6132 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6133 gnu_parent_type
6134 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6135 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6136 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6137 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6140 /* Otherwise, rely exclusively on properties of the element type. */
6141 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6144 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6146 static bool
6147 compile_time_known_address_p (Node_Id gnat_address)
6149 /* Handle reference to a constant. */
6150 if (Is_Entity_Name (gnat_address)
6151 && Ekind (Entity (gnat_address)) == E_Constant)
6153 gnat_address = Constant_Value (Entity (gnat_address));
6154 if (No (gnat_address))
6155 return false;
6158 /* Catch System'To_Address. */
6159 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6160 gnat_address = Expression (gnat_address);
6162 return Compile_Time_Known_Value (gnat_address);
6165 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6166 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6168 static bool
6169 cannot_be_superflat (Node_Id gnat_range)
6171 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6172 Node_Id scalar_range;
6173 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6175 /* If the low bound is not constant, try to find an upper bound. */
6176 while (Nkind (gnat_lb) != N_Integer_Literal
6177 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6178 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6179 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6180 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6181 || Nkind (scalar_range) == N_Range))
6182 gnat_lb = High_Bound (scalar_range);
6184 /* If the high bound is not constant, try to find a lower bound. */
6185 while (Nkind (gnat_hb) != N_Integer_Literal
6186 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6187 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6188 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6189 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6190 || Nkind (scalar_range) == N_Range))
6191 gnat_hb = Low_Bound (scalar_range);
6193 /* If we have failed to find constant bounds, punt. */
6194 if (Nkind (gnat_lb) != N_Integer_Literal
6195 || Nkind (gnat_hb) != N_Integer_Literal)
6196 return false;
6198 /* We need at least a signed 64-bit type to catch most cases. */
6199 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6200 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6201 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6202 return false;
6204 /* If the low bound is the smallest integer, nothing can be smaller. */
6205 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6206 if (TREE_OVERFLOW (gnu_lb_minus_one))
6207 return true;
6209 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6212 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6214 static bool
6215 constructor_address_p (tree gnu_expr)
6217 while (TREE_CODE (gnu_expr) == NOP_EXPR
6218 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6219 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6220 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6222 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6223 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6226 /* Return true if the size in units represented by GNU_SIZE can be handled by
6227 an allocation. If STATIC_P is true, consider only what can be done with a
6228 static allocation. */
6230 static bool
6231 allocatable_size_p (tree gnu_size, bool static_p)
6233 /* We can allocate a fixed size if it is a valid for the middle-end. */
6234 if (TREE_CODE (gnu_size) == INTEGER_CST)
6235 return valid_constant_size_p (gnu_size);
6237 /* We can allocate a variable size if this isn't a static allocation. */
6238 else
6239 return !static_p;
6242 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6243 initial value of an object of GNU_TYPE. */
6245 static bool
6246 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6248 /* Do not convert if the object's type is unconstrained because this would
6249 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6250 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6251 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6252 return false;
6254 /* Do not convert if the object's type is a padding record whose field is of
6255 self-referential size because we want to copy only the actual data. */
6256 if (type_is_padding_self_referential (gnu_type))
6257 return false;
6259 /* Do not convert a call to a function that returns with variable size since
6260 we want to use the return slot optimization in this case. */
6261 if (TREE_CODE (gnu_expr) == CALL_EXPR
6262 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6263 return false;
6265 /* Do not convert to a record type with a variant part from a record type
6266 without one, to keep the object simpler. */
6267 if (TREE_CODE (gnu_type) == RECORD_TYPE
6268 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6269 && get_variant_part (gnu_type)
6270 && !get_variant_part (TREE_TYPE (gnu_expr)))
6271 return false;
6273 /* In all the other cases, convert the expression to the object's type. */
6274 return true;
6277 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6278 be elaborated at the point of its definition, but do nothing else. */
6280 void
6281 elaborate_entity (Entity_Id gnat_entity)
6283 switch (Ekind (gnat_entity))
6285 case E_Signed_Integer_Subtype:
6286 case E_Modular_Integer_Subtype:
6287 case E_Enumeration_Subtype:
6288 case E_Ordinary_Fixed_Point_Subtype:
6289 case E_Decimal_Fixed_Point_Subtype:
6290 case E_Floating_Point_Subtype:
6292 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6293 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6295 /* ??? Tests to avoid Constraint_Error in static expressions
6296 are needed until after the front stops generating bogus
6297 conversions on bounds of real types. */
6298 if (!Raises_Constraint_Error (gnat_lb))
6299 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6300 Needs_Debug_Info (gnat_entity));
6301 if (!Raises_Constraint_Error (gnat_hb))
6302 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6303 Needs_Debug_Info (gnat_entity));
6304 break;
6307 case E_Record_Subtype:
6308 case E_Private_Subtype:
6309 case E_Limited_Private_Subtype:
6310 case E_Record_Subtype_With_Private:
6311 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6313 Node_Id gnat_discriminant_expr;
6314 Entity_Id gnat_field;
6316 for (gnat_field
6317 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6318 gnat_discriminant_expr
6319 = First_Elmt (Discriminant_Constraint (gnat_entity));
6320 Present (gnat_field);
6321 gnat_field = Next_Discriminant (gnat_field),
6322 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6323 /* Ignore access discriminants. */
6324 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6325 elaborate_expression (Node (gnat_discriminant_expr),
6326 gnat_entity, get_entity_char (gnat_field),
6327 true, false, false);
6329 break;
6334 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6335 NAME, ARGS and ERROR_POINT. */
6337 static void
6338 prepend_one_attribute (struct attrib **attr_list,
6339 enum attrib_type attrib_type,
6340 tree attr_name,
6341 tree attr_args,
6342 Node_Id attr_error_point)
6344 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6346 attr->type = attrib_type;
6347 attr->name = attr_name;
6348 attr->args = attr_args;
6349 attr->error_point = attr_error_point;
6351 attr->next = *attr_list;
6352 *attr_list = attr;
6355 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6357 static void
6358 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6360 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6361 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6362 enum attrib_type etype;
6364 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6365 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6367 case Pragma_Machine_Attribute:
6368 etype = ATTR_MACHINE_ATTRIBUTE;
6369 break;
6371 case Pragma_Linker_Alias:
6372 etype = ATTR_LINK_ALIAS;
6373 break;
6375 case Pragma_Linker_Section:
6376 etype = ATTR_LINK_SECTION;
6377 break;
6379 case Pragma_Linker_Constructor:
6380 etype = ATTR_LINK_CONSTRUCTOR;
6381 break;
6383 case Pragma_Linker_Destructor:
6384 etype = ATTR_LINK_DESTRUCTOR;
6385 break;
6387 case Pragma_Weak_External:
6388 etype = ATTR_WEAK_EXTERNAL;
6389 break;
6391 case Pragma_Thread_Local_Storage:
6392 etype = ATTR_THREAD_LOCAL_STORAGE;
6393 break;
6395 default:
6396 return;
6399 /* See what arguments we have and turn them into GCC trees for attribute
6400 handlers. These expect identifier for strings. We handle at most two
6401 arguments and static expressions only. */
6402 if (Present (gnat_arg) && Present (First (gnat_arg)))
6404 Node_Id gnat_arg0 = Next (First (gnat_arg));
6405 Node_Id gnat_arg1 = Empty;
6407 if (Present (gnat_arg0)
6408 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6410 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6412 if (TREE_CODE (gnu_arg0) == STRING_CST)
6414 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6415 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6416 return;
6419 gnat_arg1 = Next (gnat_arg0);
6422 if (Present (gnat_arg1)
6423 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6425 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6427 if (TREE_CODE (gnu_arg1) == STRING_CST)
6428 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6432 /* Prepend to the list. Make a list of the argument we might have, as GCC
6433 expects it. */
6434 prepend_one_attribute (attr_list, etype, gnu_arg0,
6435 gnu_arg1
6436 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6437 Present (Next (First (gnat_arg)))
6438 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6441 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6443 static void
6444 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6446 Node_Id gnat_temp;
6448 /* Attributes are stored as Representation Item pragmas. */
6449 for (gnat_temp = First_Rep_Item (gnat_entity);
6450 Present (gnat_temp);
6451 gnat_temp = Next_Rep_Item (gnat_temp))
6452 if (Nkind (gnat_temp) == N_Pragma)
6453 prepend_one_attribute_pragma (attr_list, gnat_temp);
6456 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6457 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6458 return the GCC tree to use for that expression. S is the suffix to use
6459 if a variable needs to be created and DEFINITION is true if this is done
6460 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6461 otherwise, we are just elaborating the expression for side-effects. If
6462 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6463 isn't needed for code generation. */
6465 static tree
6466 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6467 bool definition, bool need_value, bool need_debug)
6469 tree gnu_expr;
6471 /* If we already elaborated this expression (e.g. it was involved
6472 in the definition of a private type), use the old value. */
6473 if (present_gnu_tree (gnat_expr))
6474 return get_gnu_tree (gnat_expr);
6476 /* If we don't need a value and this is static or a discriminant,
6477 we don't need to do anything. */
6478 if (!need_value
6479 && (Is_OK_Static_Expression (gnat_expr)
6480 || (Nkind (gnat_expr) == N_Identifier
6481 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6482 return NULL_TREE;
6484 /* If it's a static expression, we don't need a variable for debugging. */
6485 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6486 need_debug = false;
6488 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6489 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6490 definition, need_debug);
6492 /* Save the expression in case we try to elaborate this entity again. Since
6493 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6494 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6495 save_gnu_tree (gnat_expr, gnu_expr, true);
6497 return need_value ? gnu_expr : error_mark_node;
6500 /* Similar, but take a GNU expression and always return a result. */
6502 static tree
6503 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6504 bool definition, bool need_debug)
6506 const bool expr_public_p = Is_Public (gnat_entity);
6507 const bool expr_global_p = expr_public_p || global_bindings_p ();
6508 bool expr_variable_p, use_variable;
6510 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6511 that an expression cannot contain both a discriminant and a variable. */
6512 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6513 return gnu_expr;
6515 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6516 a variable that is initialized to contain the expression when the package
6517 containing the definition is elaborated. If this entity is defined at top
6518 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6519 if this is necessary. */
6520 if (TREE_CONSTANT (gnu_expr))
6521 expr_variable_p = false;
6522 else
6524 /* Skip any conversions and simple constant arithmetics to see if the
6525 expression is based on a read-only variable. */
6526 tree inner = remove_conversions (gnu_expr, true);
6528 inner = skip_simple_constant_arithmetic (inner);
6530 if (handled_component_p (inner))
6531 inner = get_inner_constant_reference (inner);
6533 expr_variable_p
6534 = !(inner
6535 && TREE_CODE (inner) == VAR_DECL
6536 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6539 /* We only need to use the variable if we are in a global context since GCC
6540 can do the right thing in the local case. However, when not optimizing,
6541 use it for bounds of loop iteration scheme to avoid code duplication. */
6542 use_variable = expr_variable_p
6543 && (expr_global_p
6544 || (!optimize
6545 && definition
6546 && Is_Itype (gnat_entity)
6547 && Nkind (Associated_Node_For_Itype (gnat_entity))
6548 == N_Loop_Parameter_Specification));
6550 /* Now create it, possibly only for debugging purposes. */
6551 if (use_variable || need_debug)
6553 /* The following variable creation can happen when processing the body
6554 of subprograms that are defined out of the extended main unit and
6555 inlined. In this case, we are not at the global scope, and thus the
6556 new variable must not be tagged "external", as we used to do here as
6557 soon as DEFINITION was false. */
6558 tree gnu_decl
6559 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6560 TREE_TYPE (gnu_expr), gnu_expr, true,
6561 expr_public_p, !definition && expr_global_p,
6562 expr_global_p, false, true, need_debug,
6563 NULL, gnat_entity);
6565 /* Using this variable at debug time (if need_debug is true) requires a
6566 proper location. The back-end will compute a location for this
6567 variable only if the variable is used by the generated code.
6568 Returning the variable ensures the caller will use it in generated
6569 code. Note that there is no need for a location if the debug info
6570 contains an integer constant.
6571 TODO: when the encoding-based debug scheme is dropped, move this
6572 condition to the top-level IF block: we will not need to create a
6573 variable anymore in such cases, then. */
6574 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6575 return gnu_decl;
6578 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6581 /* Similar, but take an alignment factor and make it explicit in the tree. */
6583 static tree
6584 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6585 bool definition, bool need_debug, unsigned int align)
6587 tree unit_align = size_int (align / BITS_PER_UNIT);
6588 return
6589 size_binop (MULT_EXPR,
6590 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6591 gnu_expr,
6592 unit_align),
6593 gnat_entity, s, definition,
6594 need_debug),
6595 unit_align);
6598 /* Structure to hold internal data for elaborate_reference. */
6600 struct er_data
6602 Entity_Id entity;
6603 bool definition;
6604 unsigned int n;
6607 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6609 static tree
6610 elaborate_reference_1 (tree ref, void *data)
6612 struct er_data *er = (struct er_data *)data;
6613 char suffix[16];
6615 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6616 if (TREE_CONSTANT (ref))
6617 return ref;
6619 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6620 pointer. This may be more efficient, but will also allow us to more
6621 easily find the match for the PLACEHOLDER_EXPR. */
6622 if (TREE_CODE (ref) == COMPONENT_REF
6623 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6624 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6625 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6626 TREE_OPERAND (ref, 1), NULL_TREE);
6628 sprintf (suffix, "EXP%d", ++er->n);
6629 return
6630 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6633 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6634 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6635 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6637 static tree
6638 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6639 tree *init)
6641 struct er_data er = { gnat_entity, definition, 0 };
6642 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6645 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6646 the value passed against the list of choices. */
6648 static tree
6649 choices_to_gnu (tree operand, Node_Id choices)
6651 Node_Id choice;
6652 Node_Id gnat_temp;
6653 tree result = boolean_false_node;
6654 tree this_test, low = 0, high = 0, single = 0;
6656 for (choice = First (choices); Present (choice); choice = Next (choice))
6658 switch (Nkind (choice))
6660 case N_Range:
6661 low = gnat_to_gnu (Low_Bound (choice));
6662 high = gnat_to_gnu (High_Bound (choice));
6664 this_test
6665 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6666 build_binary_op (GE_EXPR, boolean_type_node,
6667 operand, low, true),
6668 build_binary_op (LE_EXPR, boolean_type_node,
6669 operand, high, true),
6670 true);
6672 break;
6674 case N_Subtype_Indication:
6675 gnat_temp = Range_Expression (Constraint (choice));
6676 low = gnat_to_gnu (Low_Bound (gnat_temp));
6677 high = gnat_to_gnu (High_Bound (gnat_temp));
6679 this_test
6680 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6681 build_binary_op (GE_EXPR, boolean_type_node,
6682 operand, low, true),
6683 build_binary_op (LE_EXPR, boolean_type_node,
6684 operand, high, true),
6685 true);
6686 break;
6688 case N_Identifier:
6689 case N_Expanded_Name:
6690 /* This represents either a subtype range, an enumeration
6691 literal, or a constant Ekind says which. If an enumeration
6692 literal or constant, fall through to the next case. */
6693 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6694 && Ekind (Entity (choice)) != E_Constant)
6696 tree type = gnat_to_gnu_type (Entity (choice));
6698 low = TYPE_MIN_VALUE (type);
6699 high = TYPE_MAX_VALUE (type);
6701 this_test
6702 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6703 build_binary_op (GE_EXPR, boolean_type_node,
6704 operand, low, true),
6705 build_binary_op (LE_EXPR, boolean_type_node,
6706 operand, high, true),
6707 true);
6708 break;
6711 /* ... fall through ... */
6713 case N_Character_Literal:
6714 case N_Integer_Literal:
6715 single = gnat_to_gnu (choice);
6716 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6717 single, true);
6718 break;
6720 case N_Others_Choice:
6721 this_test = boolean_true_node;
6722 break;
6724 default:
6725 gcc_unreachable ();
6728 if (result == boolean_false_node)
6729 result = this_test;
6730 else
6731 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6732 this_test, true);
6735 return result;
6738 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6739 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6741 static int
6742 adjust_packed (tree field_type, tree record_type, int packed)
6744 /* If the field contains an item of variable size, we cannot pack it
6745 because we cannot create temporaries of non-fixed size in case
6746 we need to take the address of the field. See addressable_p and
6747 the notes on the addressability issues for further details. */
6748 if (type_has_variable_size (field_type))
6749 return 0;
6751 /* In the other cases, we can honor the packing. */
6752 if (packed)
6753 return packed;
6755 /* If the alignment of the record is specified and the field type
6756 is over-aligned, request Storage_Unit alignment for the field. */
6757 if (TYPE_ALIGN (record_type)
6758 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6759 return -1;
6761 /* Likewise if the maximum alignment of the record is specified. */
6762 if (TYPE_MAX_ALIGN (record_type)
6763 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6764 return -1;
6766 return 0;
6769 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6770 placed in GNU_RECORD_TYPE.
6772 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6773 record has Component_Alignment of Storage_Unit.
6775 DEFINITION is true if this field is for a record being defined.
6777 DEBUG_INFO_P is true if we need to write debug information for types
6778 that we may create in the process. */
6780 static tree
6781 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6782 bool definition, bool debug_info_p)
6784 const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field));
6785 const Entity_Id gnat_field_type = Etype (gnat_field);
6786 const bool is_atomic
6787 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6788 const bool is_aliased = Is_Aliased (gnat_field);
6789 const bool is_independent
6790 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6791 const bool is_volatile
6792 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6793 const bool is_strict_alignment = Strict_Alignment (gnat_field_type);
6794 /* We used to consider that volatile fields also require strict alignment,
6795 but that was an interpolation and would cause us to reject a pragma
6796 volatile on a packed record type containing boolean components, while
6797 there is no basis to do so in the RM. In such cases, the writes will
6798 involve load-modify-store sequences, but that's OK for volatile. The
6799 only constraint is the implementation advice whereby only the bits of
6800 the components should be accessed if they both start and end on byte
6801 boundaries, but that should be guaranteed by the GCC memory model. */
6802 const bool needs_strict_alignment
6803 = (is_atomic || is_aliased || is_independent || is_strict_alignment);
6804 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6805 tree gnu_field_id = get_entity_name (gnat_field);
6806 tree gnu_field, gnu_size, gnu_pos;
6808 /* If this field requires strict alignment, we cannot pack it because
6809 it would very likely be under-aligned in the record. */
6810 if (needs_strict_alignment)
6811 packed = 0;
6812 else
6813 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6815 /* If a size is specified, use it. Otherwise, if the record type is packed,
6816 use the official RM size. See "Handling of Type'Size Values" in Einfo
6817 for further details. */
6818 if (Known_Esize (gnat_field))
6819 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6820 gnat_field, FIELD_DECL, false, true);
6821 else if (packed == 1)
6822 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6823 gnat_field, FIELD_DECL, false, true);
6824 else
6825 gnu_size = NULL_TREE;
6827 /* If we have a specified size that is smaller than that of the field's type,
6828 or a position is specified, and the field's type is a record that doesn't
6829 require strict alignment, see if we can get either an integral mode form
6830 of the type or a smaller form. If we can, show a size was specified for
6831 the field if there wasn't one already, so we know to make this a bitfield
6832 and avoid making things wider.
6834 Changing to an integral mode form is useful when the record is packed as
6835 we can then place the field at a non-byte-aligned position and so achieve
6836 tighter packing. This is in addition required if the field shares a byte
6837 with another field and the front-end lets the back-end handle the access
6838 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6840 Changing to a smaller form is required if the specified size is smaller
6841 than that of the field's type and the type contains sub-fields that are
6842 padded, in order to avoid generating accesses to these sub-fields that
6843 are wider than the field.
6845 We avoid the transformation if it is not required or potentially useful,
6846 as it might entail an increase of the field's alignment and have ripple
6847 effects on the outer record type. A typical case is a field known to be
6848 byte-aligned and not to share a byte with another field. */
6849 if (!needs_strict_alignment
6850 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6851 && !TYPE_FAT_POINTER_P (gnu_field_type)
6852 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6853 && (packed == 1
6854 || (gnu_size
6855 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6856 || (Present (Component_Clause (gnat_field))
6857 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6858 % BITS_PER_UNIT == 0
6859 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6861 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6862 if (gnu_packable_type != gnu_field_type)
6864 gnu_field_type = gnu_packable_type;
6865 if (!gnu_size)
6866 gnu_size = rm_size (gnu_field_type);
6870 if (Is_Atomic_Or_VFA (gnat_field))
6871 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6873 if (Present (Component_Clause (gnat_field)))
6875 Node_Id gnat_clause = Component_Clause (gnat_field);
6876 Entity_Id gnat_parent = Parent_Subtype (gnat_record_type);
6878 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6879 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6880 gnat_field, FIELD_DECL, false, true);
6882 /* Ensure the position does not overlap with the parent subtype, if there
6883 is one. This test is omitted if the parent of the tagged type has a
6884 full rep clause since, in this case, component clauses are allowed to
6885 overlay the space allocated for the parent type and the front-end has
6886 checked that there are no overlapping components. */
6887 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6889 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6891 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6892 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6893 post_error_ne_tree
6894 ("offset of& must be beyond parent{, minimum allowed is ^}",
6895 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6898 /* If this field needs strict alignment, make sure that the record is
6899 sufficiently aligned and that the position and size are consistent
6900 with the type. But don't do it if we are just annotating types and
6901 the field's type is tagged, since tagged types aren't fully laid out
6902 in this mode. Also, note that atomic implies volatile so the inner
6903 test sequences ordering is significant here. */
6904 if (needs_strict_alignment
6905 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6907 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6909 if (TYPE_ALIGN (gnu_record_type) < type_align)
6910 SET_TYPE_ALIGN (gnu_record_type, type_align);
6912 /* If the position is not a multiple of the alignment of the type,
6913 then error out and reset the position. */
6914 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6915 bitsize_int (type_align))))
6917 const char *s;
6919 if (is_atomic)
6920 s = "position of atomic field& must be multiple of ^ bits";
6921 else if (is_aliased)
6922 s = "position of aliased field& must be multiple of ^ bits";
6923 else if (is_independent)
6924 s = "position of independent field& must be multiple of ^ bits";
6925 else if (is_strict_alignment)
6926 s = "position of & with aliased or tagged part must be"
6927 " multiple of ^ bits";
6928 else
6929 gcc_unreachable ();
6931 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6932 type_align);
6933 gnu_pos = NULL_TREE;
6936 if (gnu_size)
6938 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6939 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6941 /* If the size is lower than that of the type, or greater for
6942 atomic and aliased, then error out and reset the size. */
6943 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6945 const char *s;
6947 if (is_atomic)
6948 s = "size of atomic field& must be ^ bits";
6949 else if (is_aliased)
6950 s = "size of aliased field& must be ^ bits";
6951 else if (is_independent)
6952 s = "size of independent field& must be at least ^ bits";
6953 else if (is_strict_alignment)
6954 s = "size of & with aliased or tagged part must be"
6955 " at least ^ bits";
6956 else
6957 gcc_unreachable ();
6959 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6960 gnu_type_size);
6961 gnu_size = NULL_TREE;
6964 /* Likewise if the size is not a multiple of a byte, */
6965 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6966 bitsize_unit_node)))
6968 const char *s;
6970 if (is_independent)
6971 s = "size of independent field& must be multiple of"
6972 " Storage_Unit";
6973 else if (is_strict_alignment)
6974 s = "size of & with aliased or tagged part must be"
6975 " multiple of Storage_Unit";
6976 else
6977 gcc_unreachable ();
6979 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6980 gnu_size = NULL_TREE;
6986 /* If the record has rep clauses and this is the tag field, make a rep
6987 clause for it as well. */
6988 else if (Has_Specified_Layout (gnat_record_type)
6989 && Chars (gnat_field) == Name_uTag)
6991 gnu_pos = bitsize_zero_node;
6992 gnu_size = TYPE_SIZE (gnu_field_type);
6995 else
6997 gnu_pos = NULL_TREE;
6999 /* If we are packing the record and the field is BLKmode, round the
7000 size up to a byte boundary. */
7001 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7002 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7005 /* We need to make the size the maximum for the type if it is
7006 self-referential and an unconstrained type. In that case, we can't
7007 pack the field since we can't make a copy to align it. */
7008 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7009 && !gnu_size
7010 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7011 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7013 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7014 packed = 0;
7017 /* If a size is specified, adjust the field's type to it. */
7018 if (gnu_size)
7020 tree orig_field_type;
7022 /* If the field's type is justified modular, we would need to remove
7023 the wrapper to (better) meet the layout requirements. However we
7024 can do so only if the field is not aliased to preserve the unique
7025 layout, if it has the same storage order as the enclosing record
7026 and if the prescribed size is not greater than that of the packed
7027 array to preserve the justification. */
7028 if (!needs_strict_alignment
7029 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7030 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7031 && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type)
7032 == Reverse_Storage_Order (gnat_record_type)
7033 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7034 <= 0)
7035 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7037 /* Similarly if the field's type is a misaligned integral type, but
7038 there is no restriction on the size as there is no justification. */
7039 if (!needs_strict_alignment
7040 && TYPE_IS_PADDING_P (gnu_field_type)
7041 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7042 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7044 gnu_field_type
7045 = make_type_from_size (gnu_field_type, gnu_size,
7046 Has_Biased_Representation (gnat_field));
7048 orig_field_type = gnu_field_type;
7049 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7050 false, false, definition, true);
7052 /* If a padding record was made, declare it now since it will never be
7053 declared otherwise. This is necessary to ensure that its subtrees
7054 are properly marked. */
7055 if (gnu_field_type != orig_field_type
7056 && !DECL_P (TYPE_NAME (gnu_field_type)))
7057 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7058 debug_info_p, gnat_field);
7061 /* Otherwise (or if there was an error), don't specify a position. */
7062 else
7063 gnu_pos = NULL_TREE;
7065 /* If the field's type is a padded type made for a scalar field of a record
7066 type with reverse storage order, we need to propagate the reverse storage
7067 order to the padding type since it is the innermost enclosing aggregate
7068 type around the scalar. */
7069 if (TYPE_IS_PADDING_P (gnu_field_type)
7070 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7071 && Is_Scalar_Type (gnat_field_type))
7072 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7074 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7075 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7077 /* Now create the decl for the field. */
7078 gnu_field
7079 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7080 gnu_size, gnu_pos, packed, is_aliased);
7081 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7082 DECL_ALIASED_P (gnu_field) = is_aliased;
7083 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7085 if (Ekind (gnat_field) == E_Discriminant)
7087 DECL_INVARIANT_P (gnu_field)
7088 = No (Discriminant_Default_Value (gnat_field));
7089 DECL_DISCRIMINANT_NUMBER (gnu_field)
7090 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7093 return gnu_field;
7096 /* Return true if at least one member of COMPONENT_LIST needs strict
7097 alignment. */
7099 static bool
7100 components_need_strict_alignment (Node_Id component_list)
7102 Node_Id component_decl;
7104 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7105 Present (component_decl);
7106 component_decl = Next_Non_Pragma (component_decl))
7108 Entity_Id gnat_field = Defining_Entity (component_decl);
7110 if (Is_Aliased (gnat_field))
7111 return true;
7113 if (Strict_Alignment (Etype (gnat_field)))
7114 return true;
7117 return false;
7120 /* Return true if TYPE is a type with variable size or a padding type with a
7121 field of variable size or a record that has a field with such a type. */
7123 static bool
7124 type_has_variable_size (tree type)
7126 tree field;
7128 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7129 return true;
7131 if (TYPE_IS_PADDING_P (type)
7132 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7133 return true;
7135 if (!RECORD_OR_UNION_TYPE_P (type))
7136 return false;
7138 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7139 if (type_has_variable_size (TREE_TYPE (field)))
7140 return true;
7142 return false;
7145 /* Return true if FIELD is an artificial field. */
7147 static bool
7148 field_is_artificial (tree field)
7150 /* These fields are generated by the front-end proper. */
7151 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7152 return true;
7154 /* These fields are generated by gigi. */
7155 if (DECL_INTERNAL_P (field))
7156 return true;
7158 return false;
7161 /* Return true if FIELD is a non-artificial field with self-referential
7162 size. */
7164 static bool
7165 field_has_self_size (tree field)
7167 if (field_is_artificial (field))
7168 return false;
7170 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7171 return false;
7173 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7176 /* Return true if FIELD is a non-artificial field with variable size. */
7178 static bool
7179 field_has_variable_size (tree field)
7181 if (field_is_artificial (field))
7182 return false;
7184 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7185 return false;
7187 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7190 /* qsort comparer for the bit positions of two record components. */
7192 static int
7193 compare_field_bitpos (const PTR rt1, const PTR rt2)
7195 const_tree const field1 = * (const_tree const *) rt1;
7196 const_tree const field2 = * (const_tree const *) rt2;
7197 const int ret
7198 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7200 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7203 /* Reverse function from gnat_to_gnu_field: return the GNAT field present in
7204 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and
7205 corresponding to the GNU tree GNU_FIELD. */
7207 static Entity_Id
7208 gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list,
7209 Entity_Id gnat_record_type)
7211 Entity_Id gnat_component_decl, gnat_field;
7213 if (Present (Component_Items (gnat_component_list)))
7214 for (gnat_component_decl
7215 = First_Non_Pragma (Component_Items (gnat_component_list));
7216 Present (gnat_component_decl);
7217 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7219 gnat_field = Defining_Entity (gnat_component_decl);
7220 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7221 return gnat_field;
7224 if (Has_Discriminants (gnat_record_type))
7225 for (gnat_field = First_Stored_Discriminant (gnat_record_type);
7226 Present (gnat_field);
7227 gnat_field = Next_Stored_Discriminant (gnat_field))
7228 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7229 return gnat_field;
7231 return Empty;
7234 /* Issue a warning for the problematic placement of GNU_FIELD present in
7235 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE.
7236 IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant.
7237 DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */
7239 static void
7240 warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list,
7241 Entity_Id gnat_record_type, bool in_variant,
7242 bool do_reorder)
7244 const char *msg1
7245 = in_variant
7246 ? "?variant layout may cause performance issues"
7247 : "?record layout may cause performance issues";
7248 const char *msg2
7249 = field_has_self_size (gnu_field)
7250 ? "?component & whose length depends on a discriminant"
7251 : field_has_variable_size (gnu_field)
7252 ? "?component & whose length is not fixed"
7253 : "?component & whose length is not multiple of a byte";
7254 const char *msg3
7255 = do_reorder
7256 ? "?comes too early and was moved down"
7257 : "?comes too early and ought to be moved down";
7258 Entity_Id gnat_field
7259 = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type);
7261 gcc_assert (Present (gnat_field));
7263 post_error (msg1, gnat_field);
7264 post_error_ne (msg2, gnat_field, gnat_field);
7265 post_error (msg3, gnat_field);
7268 /* Structure holding information for a given variant. */
7269 typedef struct vinfo
7271 /* The record type of the variant. */
7272 tree type;
7274 /* The name of the variant. */
7275 tree name;
7277 /* The qualifier of the variant. */
7278 tree qual;
7280 /* Whether the variant has a rep clause. */
7281 bool has_rep;
7283 /* Whether the variant is packed. */
7284 bool packed;
7286 } vinfo_t;
7288 /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to
7289 GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and
7290 finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects
7291 the layout (see below). When called from gnat_to_gnu_entity during the
7292 processing of a record definition, the GCC node for the parent, if any,
7293 will be the single field of GNU_RECORD_TYPE and the GCC nodes for the
7294 discriminants will be on GNU_FIELD_LIST. The other call to this function
7295 is a recursive call for the component list of a variant and, in this case,
7296 GNU_FIELD_LIST is empty.
7298 PACKED is 1 if this is for a packed record or -1 if this is for a record
7299 with Component_Alignment of Storage_Unit.
7301 DEFINITION is true if we are defining this record type.
7303 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7304 out the record. This means the alignment only serves to force fields to
7305 be bitfields, but not to require the record to be that aligned. This is
7306 used for variants.
7308 ALL_REP is true if a rep clause is present for all the fields.
7310 UNCHECKED_UNION is true if we are building this type for a record with a
7311 Pragma Unchecked_Union.
7313 ARTIFICIAL is true if this is a type that was generated by the compiler.
7315 DEBUG_INFO is true if we need to write debug information about the type.
7317 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7318 mean that its contents may be unused as well, only the container itself.
7320 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7321 the outer record type down to this variant level. It is nonzero only if
7322 all the fields down to this level have a rep clause and ALL_REP is false.
7324 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7325 with a rep clause is to be added; in this case, that is all that should
7326 be done with such fields and the return value will be false. */
7328 static bool
7329 components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type,
7330 tree gnu_field_list, tree gnu_record_type, int packed,
7331 bool definition, bool cancel_alignment, bool all_rep,
7332 bool unchecked_union, bool artificial, bool debug_info,
7333 bool maybe_unused, tree first_free_pos,
7334 tree *p_gnu_rep_list)
7336 const bool needs_xv_encodings
7337 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7338 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7339 bool variants_have_rep = all_rep;
7340 bool layout_with_rep = false;
7341 bool has_self_field = false;
7342 bool has_aliased_after_self_field = false;
7343 Entity_Id gnat_component_decl, gnat_variant_part;
7344 tree gnu_field, gnu_next, gnu_last;
7345 tree gnu_variant_part = NULL_TREE;
7346 tree gnu_rep_list = NULL_TREE;
7348 /* For each component referenced in a component declaration create a GCC
7349 field and add it to the list, skipping pragmas in the GNAT list. */
7350 gnu_last = tree_last (gnu_field_list);
7351 if (Present (Component_Items (gnat_component_list)))
7352 for (gnat_component_decl
7353 = First_Non_Pragma (Component_Items (gnat_component_list));
7354 Present (gnat_component_decl);
7355 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7357 Entity_Id gnat_field = Defining_Entity (gnat_component_decl);
7358 Name_Id gnat_name = Chars (gnat_field);
7360 /* If present, the _Parent field must have been created as the single
7361 field of the record type. Put it before any other fields. */
7362 if (gnat_name == Name_uParent)
7364 gnu_field = TYPE_FIELDS (gnu_record_type);
7365 gnu_field_list = chainon (gnu_field_list, gnu_field);
7367 else
7369 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7370 definition, debug_info);
7372 /* If this is the _Tag field, put it before any other fields. */
7373 if (gnat_name == Name_uTag)
7374 gnu_field_list = chainon (gnu_field_list, gnu_field);
7376 /* If this is the _Controller field, put it before the other
7377 fields except for the _Tag or _Parent field. */
7378 else if (gnat_name == Name_uController && gnu_last)
7380 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7381 DECL_CHAIN (gnu_last) = gnu_field;
7384 /* If this is a regular field, put it after the other fields. */
7385 else
7387 DECL_CHAIN (gnu_field) = gnu_field_list;
7388 gnu_field_list = gnu_field;
7389 if (!gnu_last)
7390 gnu_last = gnu_field;
7392 /* And record information for the final layout. */
7393 if (field_has_self_size (gnu_field))
7394 has_self_field = true;
7395 else if (has_self_field && DECL_ALIASED_P (gnu_field))
7396 has_aliased_after_self_field = true;
7400 save_gnu_tree (gnat_field, gnu_field, false);
7403 /* At the end of the component list there may be a variant part. */
7404 gnat_variant_part = Variant_Part (gnat_component_list);
7406 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7407 mutually exclusive and should go in the same memory. To do this we need
7408 to treat each variant as a record whose elements are created from the
7409 component list for the variant. So here we create the records from the
7410 lists for the variants and put them all into the QUAL_UNION_TYPE.
7411 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7412 use GNU_RECORD_TYPE if there are no fields so far. */
7413 if (Present (gnat_variant_part))
7415 Node_Id gnat_discr = Name (gnat_variant_part), variant;
7416 tree gnu_discr = gnat_to_gnu (gnat_discr);
7417 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7418 tree gnu_var_name
7419 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7420 "XVN");
7421 tree gnu_union_type, gnu_union_name;
7422 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7423 bool union_field_needs_strict_alignment = false;
7424 auto_vec <vinfo_t, 16> variant_types;
7425 vinfo_t *gnu_variant;
7426 unsigned int variants_align = 0;
7427 unsigned int i;
7429 gnu_union_name
7430 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7432 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7433 are all in the variant part, to match the layout of C unions. There
7434 is an associated check below. */
7435 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7436 gnu_union_type = gnu_record_type;
7437 else
7439 gnu_union_type
7440 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7442 TYPE_NAME (gnu_union_type) = gnu_union_name;
7443 SET_TYPE_ALIGN (gnu_union_type, 0);
7444 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7445 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7446 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7449 /* If all the fields down to this level have a rep clause, find out
7450 whether all the fields at this level also have one. If so, then
7451 compute the new first free position to be passed downward. */
7452 this_first_free_pos = first_free_pos;
7453 if (this_first_free_pos)
7455 for (gnu_field = gnu_field_list;
7456 gnu_field;
7457 gnu_field = DECL_CHAIN (gnu_field))
7458 if (DECL_FIELD_OFFSET (gnu_field))
7460 tree pos = bit_position (gnu_field);
7461 if (!tree_int_cst_lt (pos, this_first_free_pos))
7462 this_first_free_pos
7463 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7465 else
7467 this_first_free_pos = NULL_TREE;
7468 break;
7472 /* We build the variants in two passes. The bulk of the work is done in
7473 the first pass, that is to say translating the GNAT nodes, building
7474 the container types and computing the associated properties. However
7475 we cannot finish up the container types during this pass because we
7476 don't know where the variant part will be placed until the end. */
7477 for (variant = First_Non_Pragma (Variants (gnat_variant_part));
7478 Present (variant);
7479 variant = Next_Non_Pragma (variant))
7481 tree gnu_variant_type = make_node (RECORD_TYPE);
7482 tree gnu_inner_name, gnu_qual;
7483 bool has_rep;
7484 int field_packed;
7485 vinfo_t vinfo;
7487 Get_Variant_Encoding (variant);
7488 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7489 TYPE_NAME (gnu_variant_type)
7490 = concat_name (gnu_union_name,
7491 IDENTIFIER_POINTER (gnu_inner_name));
7493 /* Set the alignment of the inner type in case we need to make
7494 inner objects into bitfields, but then clear it out so the
7495 record actually gets only the alignment required. */
7496 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7497 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7498 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7499 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7501 /* Similarly, if the outer record has a size specified and all
7502 the fields have a rep clause, we can propagate the size. */
7503 if (all_rep_and_size)
7505 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7506 TYPE_SIZE_UNIT (gnu_variant_type)
7507 = TYPE_SIZE_UNIT (gnu_record_type);
7510 /* Add the fields into the record type for the variant. Note that
7511 we aren't sure to really use it at this point, see below. */
7512 has_rep
7513 = components_to_record (Component_List (variant), gnat_record_type,
7514 NULL_TREE, gnu_variant_type, packed,
7515 definition, !all_rep_and_size, all_rep,
7516 unchecked_union, true, needs_xv_encodings,
7517 true, this_first_free_pos,
7518 all_rep || this_first_free_pos
7519 ? NULL : &gnu_rep_list);
7521 /* Translate the qualifier and annotate the GNAT node. */
7522 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7523 Set_Present_Expr (variant, annotate_value (gnu_qual));
7525 /* Deal with packedness like in gnat_to_gnu_field. */
7526 if (components_need_strict_alignment (Component_List (variant)))
7528 field_packed = 0;
7529 union_field_needs_strict_alignment = true;
7531 else
7532 field_packed
7533 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7535 /* Push this variant onto the stack for the second pass. */
7536 vinfo.type = gnu_variant_type;
7537 vinfo.name = gnu_inner_name;
7538 vinfo.qual = gnu_qual;
7539 vinfo.has_rep = has_rep;
7540 vinfo.packed = field_packed;
7541 variant_types.safe_push (vinfo);
7543 /* Compute the global properties that will determine the placement of
7544 the variant part. */
7545 variants_have_rep |= has_rep;
7546 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7547 variants_align = TYPE_ALIGN (gnu_variant_type);
7550 /* Round up the first free position to the alignment of the variant part
7551 for the variants without rep clause. This will guarantee a consistent
7552 layout independently of the placement of the variant part. */
7553 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7554 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7556 /* In the second pass, the container types are adjusted if necessary and
7557 finished up, then the corresponding fields of the variant part are
7558 built with their qualifier, unless this is an unchecked union. */
7559 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7561 tree gnu_variant_type = gnu_variant->type;
7562 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7564 /* If this is an Unchecked_Union whose fields are all in the variant
7565 part and we have a single field with no representation clause or
7566 placed at offset zero, use the field directly to match the layout
7567 of C unions. */
7568 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7569 && gnu_field_list
7570 && !DECL_CHAIN (gnu_field_list)
7571 && (!DECL_FIELD_OFFSET (gnu_field_list)
7572 || integer_zerop (bit_position (gnu_field_list))))
7574 gnu_field = gnu_field_list;
7575 DECL_CONTEXT (gnu_field) = gnu_record_type;
7577 else
7579 /* Finalize the variant type now. We used to throw away empty
7580 record types but we no longer do that because we need them to
7581 generate complete debug info for the variant; otherwise, the
7582 union type definition will be lacking the fields associated
7583 with these empty variants. */
7584 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7586 /* The variant part will be at offset 0 so we need to ensure
7587 that the fields are laid out starting from the first free
7588 position at this level. */
7589 tree gnu_rep_type = make_node (RECORD_TYPE);
7590 tree gnu_rep_part;
7591 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7592 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7593 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7594 gnu_rep_part
7595 = create_rep_part (gnu_rep_type, gnu_variant_type,
7596 this_first_free_pos);
7597 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7598 gnu_field_list = gnu_rep_part;
7599 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7600 false);
7603 if (debug_info)
7604 rest_of_record_type_compilation (gnu_variant_type);
7605 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7606 true, needs_xv_encodings, gnat_component_list);
7608 gnu_field
7609 = create_field_decl (gnu_variant->name, gnu_variant_type,
7610 gnu_union_type,
7611 all_rep_and_size
7612 ? TYPE_SIZE (gnu_variant_type) : 0,
7613 variants_have_rep ? bitsize_zero_node : 0,
7614 gnu_variant->packed, 0);
7616 DECL_INTERNAL_P (gnu_field) = 1;
7618 if (!unchecked_union)
7619 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7622 DECL_CHAIN (gnu_field) = gnu_variant_list;
7623 gnu_variant_list = gnu_field;
7626 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7627 if (gnu_variant_list)
7629 int union_field_packed;
7631 if (all_rep_and_size)
7633 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7634 TYPE_SIZE_UNIT (gnu_union_type)
7635 = TYPE_SIZE_UNIT (gnu_record_type);
7638 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7639 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7641 /* If GNU_UNION_TYPE is our record type, it means we must have an
7642 Unchecked_Union with no fields. Verify that and, if so, just
7643 return. */
7644 if (gnu_union_type == gnu_record_type)
7646 gcc_assert (unchecked_union
7647 && !gnu_field_list
7648 && !gnu_rep_list);
7649 return variants_have_rep;
7652 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7653 needs_xv_encodings, gnat_component_list);
7655 /* Deal with packedness like in gnat_to_gnu_field. */
7656 if (union_field_needs_strict_alignment)
7657 union_field_packed = 0;
7658 else
7659 union_field_packed
7660 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7662 gnu_variant_part
7663 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7664 all_rep_and_size
7665 ? TYPE_SIZE (gnu_union_type) : 0,
7666 variants_have_rep ? bitsize_zero_node : 0,
7667 union_field_packed, 0);
7669 DECL_INTERNAL_P (gnu_variant_part) = 1;
7673 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do,
7674 pull them out and put them onto the appropriate list. We have to do it
7675 in a separate pass since we want to handle the discriminants but can't
7676 play with them until we've used them in debugging data above.
7678 Similarly, pull out the fields with zero size and no rep clause, as they
7679 would otherwise modify the layout and thus very likely run afoul of the
7680 Ada semantics, which are different from those of C here.
7682 Finally, if there is an aliased field placed in the list after fields
7683 with self-referential size, pull out the latter in the same way.
7685 Optionally, if the reordering mechanism is enabled, pull out the fields
7686 with self-referential size, variable size and fixed size not a multiple
7687 of a byte, so that they don't cause the regular fields to be either at
7688 self-referential/variable offset or misaligned. Note, in the latter
7689 case, that this can only happen in packed record types so the alignment
7690 is effectively capped to the byte for the whole record.
7692 Optionally, if the layout warning is enabled, keep track of the above 4
7693 different kinds of fields and issue a warning if some of them would be
7694 (or are being) reordered by the reordering mechanism.
7696 Finally, pull out the fields whose size is not a multiple of a byte, so
7697 that they don't cause the regular fields to be misaligned. As this can
7698 only happen in packed record types, the alignment is capped to the byte.
7700 ??? If we reorder them, debugging information will be wrong but there is
7701 nothing that can be done about this at the moment. */
7702 const bool do_reorder = OK_To_Reorder_Components (gnat_record_type);
7703 const bool w_reorder
7704 = Warn_On_Questionable_Layout
7705 && (Convention (gnat_record_type) == Convention_Ada);
7706 const bool in_variant = (p_gnu_rep_list != NULL);
7707 tree gnu_zero_list = NULL_TREE;
7708 tree gnu_self_list = NULL_TREE;
7709 tree gnu_var_list = NULL_TREE;
7710 tree gnu_bitp_list = NULL_TREE;
7711 tree gnu_tmp_bitp_list = NULL_TREE;
7712 unsigned int tmp_bitp_size = 0;
7713 unsigned int last_reorder_field_type = -1;
7714 unsigned int tmp_last_reorder_field_type = -1;
7716 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7717 do { \
7718 if (gnu_last) \
7719 DECL_CHAIN (gnu_last) = gnu_next; \
7720 else \
7721 gnu_field_list = gnu_next; \
7723 DECL_CHAIN (gnu_field) = (LIST); \
7724 (LIST) = gnu_field; \
7725 } while (0)
7727 gnu_last = NULL_TREE;
7728 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7730 gnu_next = DECL_CHAIN (gnu_field);
7732 if (DECL_FIELD_OFFSET (gnu_field))
7734 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7735 continue;
7738 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7740 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7741 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7742 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7743 if (DECL_ALIASED_P (gnu_field))
7744 SET_TYPE_ALIGN (gnu_record_type,
7745 MAX (TYPE_ALIGN (gnu_record_type),
7746 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7747 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7748 continue;
7751 if (has_aliased_after_self_field && field_has_self_size (gnu_field))
7753 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7754 continue;
7757 /* We don't need further processing in default mode. */
7758 if (!w_reorder && !do_reorder)
7760 gnu_last = gnu_field;
7761 continue;
7764 if (field_has_self_size (gnu_field))
7766 if (w_reorder)
7768 if (last_reorder_field_type < 4)
7769 warn_on_field_placement (gnu_field, gnat_component_list,
7770 gnat_record_type, in_variant,
7771 do_reorder);
7772 else
7773 last_reorder_field_type = 4;
7776 if (do_reorder)
7778 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7779 continue;
7783 else if (field_has_variable_size (gnu_field))
7785 if (w_reorder)
7787 if (last_reorder_field_type < 3)
7788 warn_on_field_placement (gnu_field, gnat_component_list,
7789 gnat_record_type, in_variant,
7790 do_reorder);
7791 else
7792 last_reorder_field_type = 3;
7795 if (do_reorder)
7797 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7798 continue;
7802 else
7804 /* If the field has no size, then it cannot be bit-packed. */
7805 const unsigned int bitp_size
7806 = DECL_SIZE (gnu_field)
7807 ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT
7808 : 0;
7810 /* If the field is bit-packed, we move it to a temporary list that
7811 contains the contiguously preceding bit-packed fields, because
7812 we want to be able to put them back if the misalignment happens
7813 to cancel itself after several bit-packed fields. */
7814 if (bitp_size != 0)
7816 tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT;
7818 if (last_reorder_field_type != 2)
7820 tmp_last_reorder_field_type = last_reorder_field_type;
7821 last_reorder_field_type = 2;
7824 if (do_reorder)
7826 MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list);
7827 continue;
7831 /* No more bit-packed fields, move the existing ones to the end or
7832 put them back at their original location. */
7833 else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list)
7835 last_reorder_field_type = 1;
7837 if (tmp_bitp_size != 0)
7839 if (w_reorder && tmp_last_reorder_field_type < 2)
7840 warn_on_field_placement (gnu_tmp_bitp_list
7841 ? gnu_tmp_bitp_list : gnu_last,
7842 gnat_component_list,
7843 gnat_record_type, in_variant,
7844 do_reorder);
7846 if (do_reorder)
7847 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7849 gnu_tmp_bitp_list = NULL_TREE;
7850 tmp_bitp_size = 0;
7852 else
7854 /* Rechain the temporary list in front of GNU_FIELD. */
7855 tree gnu_bitp_field = gnu_field;
7856 while (gnu_tmp_bitp_list)
7858 tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list);
7859 DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field;
7860 if (gnu_last)
7861 DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list;
7862 else
7863 gnu_field_list = gnu_tmp_bitp_list;
7864 gnu_bitp_field = gnu_tmp_bitp_list;
7865 gnu_tmp_bitp_list = gnu_bitp_next;
7870 else
7871 last_reorder_field_type = 1;
7874 gnu_last = gnu_field;
7877 #undef MOVE_FROM_FIELD_LIST_TO
7879 gnu_field_list = nreverse (gnu_field_list);
7881 /* If permitted, we reorder the fields as follows:
7883 1) all (groups of) fields whose length is fixed and multiple of a byte,
7884 2) the remaining fields whose length is fixed and not multiple of a byte,
7885 3) the remaining fields whose length doesn't depend on discriminants,
7886 4) all fields whose length depends on discriminants,
7887 5) the variant part,
7889 within the record and within each variant recursively. */
7891 if (w_reorder)
7893 /* If we have pending bit-packed fields, warn if they would be moved
7894 to after regular fields. */
7895 if (last_reorder_field_type == 2
7896 && tmp_bitp_size != 0
7897 && tmp_last_reorder_field_type < 2)
7898 warn_on_field_placement (gnu_tmp_bitp_list
7899 ? gnu_tmp_bitp_list : gnu_field_list,
7900 gnat_component_list, gnat_record_type,
7901 in_variant, do_reorder);
7904 if (do_reorder)
7906 /* If we have pending bit-packed fields on the temporary list, we put
7907 them either on the bit-packed list or back on the regular list. */
7908 if (gnu_tmp_bitp_list)
7910 if (tmp_bitp_size != 0)
7911 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7912 else
7913 gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list);
7916 gnu_field_list
7917 = chainon (gnu_field_list,
7918 chainon (gnu_bitp_list,
7919 chainon (gnu_var_list, gnu_self_list)));
7922 /* Otherwise, if there is an aliased field placed after a field whose length
7923 depends on discriminants, we put all the fields of the latter sort, last.
7924 We need to do this in case an object of this record type is mutable. */
7925 else if (has_aliased_after_self_field)
7926 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7928 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7929 in our REP list to the previous level because this level needs them in
7930 order to do a correct layout, i.e. avoid having overlapping fields. */
7931 if (p_gnu_rep_list && gnu_rep_list)
7932 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7934 /* Deal with the annoying case of an extension of a record with variable size
7935 and partial rep clause, for which the _Parent field is forced at offset 0
7936 and has variable size, which we do not support below. Note that we cannot
7937 do it if the field has fixed size because we rely on the presence of the
7938 REP part built below to trigger the reordering of the fields in a derived
7939 record type when all the fields have a fixed position. */
7940 else if (gnu_rep_list
7941 && !DECL_CHAIN (gnu_rep_list)
7942 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7943 && !variants_have_rep
7944 && first_free_pos
7945 && integer_zerop (first_free_pos)
7946 && integer_zerop (bit_position (gnu_rep_list)))
7948 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7949 gnu_field_list = gnu_rep_list;
7950 gnu_rep_list = NULL_TREE;
7953 /* Otherwise, sort the fields by bit position and put them into their own
7954 record, before the others, if we also have fields without rep clause. */
7955 else if (gnu_rep_list)
7957 tree gnu_rep_type, gnu_rep_part;
7958 int i, len = list_length (gnu_rep_list);
7959 tree *gnu_arr = XALLOCAVEC (tree, len);
7961 /* If all the fields have a rep clause, we can do a flat layout. */
7962 layout_with_rep = !gnu_field_list
7963 && (!gnu_variant_part || variants_have_rep);
7964 gnu_rep_type
7965 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7967 for (gnu_field = gnu_rep_list, i = 0;
7968 gnu_field;
7969 gnu_field = DECL_CHAIN (gnu_field), i++)
7970 gnu_arr[i] = gnu_field;
7972 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7974 /* Put the fields in the list in order of increasing position, which
7975 means we start from the end. */
7976 gnu_rep_list = NULL_TREE;
7977 for (i = len - 1; i >= 0; i--)
7979 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7980 gnu_rep_list = gnu_arr[i];
7981 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7984 if (layout_with_rep)
7985 gnu_field_list = gnu_rep_list;
7986 else
7988 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7989 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7990 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7992 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7993 without rep clause are laid out starting from this position.
7994 Therefore, we force it as a minimal size on the REP part. */
7995 gnu_rep_part
7996 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7998 /* Chain the REP part at the beginning of the field list. */
7999 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
8000 gnu_field_list = gnu_rep_part;
8004 /* Chain the variant part at the end of the field list. */
8005 if (gnu_variant_part)
8006 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
8008 if (cancel_alignment)
8009 SET_TYPE_ALIGN (gnu_record_type, 0);
8011 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
8013 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
8014 debug_info && !maybe_unused);
8016 /* Chain the fields with zero size at the beginning of the field list. */
8017 if (gnu_zero_list)
8018 TYPE_FIELDS (gnu_record_type)
8019 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
8021 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
8024 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
8025 placed into an Esize, Component_Bit_Offset, or Component_Size value
8026 in the GNAT tree. */
8028 static Uint
8029 annotate_value (tree gnu_size)
8031 TCode tcode;
8032 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
8033 struct tree_int_map in;
8034 int i;
8036 /* See if we've already saved the value for this node. */
8037 if (EXPR_P (gnu_size))
8039 struct tree_int_map *e;
8041 in.base.from = gnu_size;
8042 e = annotate_value_cache->find (&in);
8044 if (e)
8045 return (Node_Ref_Or_Val) e->to;
8047 else
8048 in.base.from = NULL_TREE;
8050 /* If we do not return inside this switch, TCODE will be set to the
8051 code to use for a Create_Node operand and LEN (set above) will be
8052 the number of recursive calls for us to make. */
8054 switch (TREE_CODE (gnu_size))
8056 case INTEGER_CST:
8057 /* For negative values, build NEGATE_EXPR of the opposite. Such values
8058 can appear for discriminants in expressions for variants. */
8059 if (tree_int_cst_sgn (gnu_size) < 0)
8061 tree t = wide_int_to_tree (sizetype, wi::neg (gnu_size));
8062 return annotate_value (build1 (NEGATE_EXPR, sizetype, t));
8065 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8067 case COMPONENT_REF:
8068 /* The only case we handle here is a simple discriminant reference. */
8069 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8071 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
8073 /* Climb up the chain of successive extensions, if any. */
8074 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
8075 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
8076 == parent_name_id)
8077 gnu_size = TREE_OPERAND (gnu_size, 0);
8079 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
8080 return
8081 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
8084 return No_Uint;
8086 CASE_CONVERT: case NON_LVALUE_EXPR:
8087 return annotate_value (TREE_OPERAND (gnu_size, 0));
8089 /* Now just list the operations we handle. */
8090 case COND_EXPR: tcode = Cond_Expr; break;
8091 case PLUS_EXPR: tcode = Plus_Expr; break;
8092 case MINUS_EXPR: tcode = Minus_Expr; break;
8093 case MULT_EXPR: tcode = Mult_Expr; break;
8094 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8095 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8096 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8097 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8098 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8099 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8100 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8101 case NEGATE_EXPR: tcode = Negate_Expr; break;
8102 case MIN_EXPR: tcode = Min_Expr; break;
8103 case MAX_EXPR: tcode = Max_Expr; break;
8104 case ABS_EXPR: tcode = Abs_Expr; break;
8105 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
8106 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
8107 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8108 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8109 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8110 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8111 case LT_EXPR: tcode = Lt_Expr; break;
8112 case LE_EXPR: tcode = Le_Expr; break;
8113 case GT_EXPR: tcode = Gt_Expr; break;
8114 case GE_EXPR: tcode = Ge_Expr; break;
8115 case EQ_EXPR: tcode = Eq_Expr; break;
8116 case NE_EXPR: tcode = Ne_Expr; break;
8118 case BIT_AND_EXPR:
8119 tcode = Bit_And_Expr;
8120 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8121 Such values appear in expressions with aligning patterns. Note that,
8122 since sizetype is unsigned, we have to jump through some hoops. */
8123 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8125 tree op1 = TREE_OPERAND (gnu_size, 1);
8126 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
8127 if (wi::neg_p (signed_op1))
8129 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
8130 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8133 break;
8135 case CALL_EXPR:
8136 /* In regular mode, inline back only if symbolic annotation is requested
8137 in order to avoid memory explosion on big discriminated record types.
8138 But not in ASIS mode, as symbolic annotation is required for DDA. */
8139 if (List_Representation_Info == 3 || type_annotate_only)
8141 tree t = maybe_inline_call_in_expr (gnu_size);
8142 if (t)
8143 return annotate_value (t);
8145 else
8146 return Uint_Minus_1;
8148 /* Fall through... */
8150 default:
8151 return No_Uint;
8154 /* Now get each of the operands that's relevant for this code. If any
8155 cannot be expressed as a repinfo node, say we can't. */
8156 for (i = 0; i < 3; i++)
8157 ops[i] = No_Uint;
8159 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8161 if (i == 1 && pre_op1 != No_Uint)
8162 ops[i] = pre_op1;
8163 else
8164 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8165 if (ops[i] == No_Uint)
8166 return No_Uint;
8169 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8171 /* Save the result in the cache. */
8172 if (in.base.from)
8174 struct tree_int_map **h;
8175 /* We can't assume the hash table data hasn't moved since the initial
8176 look up, so we have to search again. Allocating and inserting an
8177 entry at that point would be an alternative, but then we'd better
8178 discard the entry if we decided not to cache it. */
8179 h = annotate_value_cache->find_slot (&in, INSERT);
8180 gcc_assert (!*h);
8181 *h = ggc_alloc<tree_int_map> ();
8182 (*h)->base.from = gnu_size;
8183 (*h)->to = ret;
8186 return ret;
8189 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8190 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8191 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8192 BY_REF is true if the object is used by reference. */
8194 void
8195 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8197 if (by_ref)
8199 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8200 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8201 else
8202 gnu_type = TREE_TYPE (gnu_type);
8205 if (Unknown_Esize (gnat_entity))
8207 if (TREE_CODE (gnu_type) == RECORD_TYPE
8208 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8209 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8210 else if (!size)
8211 size = TYPE_SIZE (gnu_type);
8213 if (size)
8214 Set_Esize (gnat_entity, annotate_value (size));
8217 if (Unknown_Alignment (gnat_entity))
8218 Set_Alignment (gnat_entity,
8219 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8222 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8223 Return NULL_TREE if there is no such element in the list. */
8225 static tree
8226 purpose_member_field (const_tree elem, tree list)
8228 while (list)
8230 tree field = TREE_PURPOSE (list);
8231 if (SAME_FIELD_P (field, elem))
8232 return list;
8233 list = TREE_CHAIN (list);
8235 return NULL_TREE;
8238 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8239 set Component_Bit_Offset and Esize of the components to the position and
8240 size used by Gigi. */
8242 static void
8243 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8245 /* For an extension, the inherited components have not been translated because
8246 they are fetched from the _Parent component on the fly. */
8247 const bool is_extension
8248 = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity);
8250 /* We operate by first making a list of all fields and their position (we
8251 can get the size easily) and then update all the sizes in the tree. */
8252 tree gnu_list
8253 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8254 BIGGEST_ALIGNMENT, NULL_TREE);
8256 for (Entity_Id gnat_field = First_Entity (gnat_entity);
8257 Present (gnat_field);
8258 gnat_field = Next_Entity (gnat_field))
8259 if ((Ekind (gnat_field) == E_Component
8260 && (is_extension || present_gnu_tree (gnat_field)))
8261 || (Ekind (gnat_field) == E_Discriminant
8262 && !Is_Unchecked_Union (Scope (gnat_field))))
8264 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8265 gnu_list);
8266 if (t)
8268 tree parent_offset;
8270 /* If we are just annotating types and the type is tagged, the tag
8271 and the parent components are not generated by the front-end so
8272 we need to add the appropriate offset to each component without
8273 representation clause. */
8274 if (type_annotate_only
8275 && Is_Tagged_Type (gnat_entity)
8276 && No (Component_Clause (gnat_field)))
8278 /* For a component appearing in the current extension, the
8279 offset is the size of the parent. */
8280 if (Is_Derived_Type (gnat_entity)
8281 && Original_Record_Component (gnat_field) == gnat_field)
8282 parent_offset
8283 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8284 bitsizetype);
8285 else
8286 parent_offset = bitsize_int (POINTER_SIZE);
8288 if (TYPE_FIELDS (gnu_type))
8289 parent_offset
8290 = round_up (parent_offset,
8291 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8293 else
8294 parent_offset = bitsize_zero_node;
8296 Set_Component_Bit_Offset
8297 (gnat_field,
8298 annotate_value
8299 (size_binop (PLUS_EXPR,
8300 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
8301 TREE_VEC_ELT (TREE_VALUE (t), 2)),
8302 parent_offset)));
8304 Set_Esize (gnat_field,
8305 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8307 else if (is_extension)
8309 /* If there is no entry, this is an inherited component whose
8310 position is the same as in the parent type. */
8311 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
8313 /* If we are just annotating types, discriminants renaming those of
8314 the parent have no entry so deal with them specifically. */
8315 if (type_annotate_only
8316 && gnat_orig_field == gnat_field
8317 && Ekind (gnat_field) == E_Discriminant)
8318 gnat_orig_field = Corresponding_Discriminant (gnat_field);
8320 Set_Component_Bit_Offset (gnat_field,
8321 Component_Bit_Offset (gnat_orig_field));
8323 Set_Esize (gnat_field, Esize (gnat_orig_field));
8328 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8329 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8330 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8331 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8332 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8333 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8334 pre-existing list to be chained to the newly created entries. */
8336 static tree
8337 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8338 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8340 tree gnu_field;
8342 for (gnu_field = TYPE_FIELDS (gnu_type);
8343 gnu_field;
8344 gnu_field = DECL_CHAIN (gnu_field))
8346 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8347 DECL_FIELD_BIT_OFFSET (gnu_field));
8348 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8349 DECL_FIELD_OFFSET (gnu_field));
8350 unsigned int our_offset_align
8351 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8352 tree v = make_tree_vec (3);
8354 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8355 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8356 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8357 gnu_list = tree_cons (gnu_field, v, gnu_list);
8359 /* Recurse on internal fields, flattening the nested fields except for
8360 those in the variant part, if requested. */
8361 if (DECL_INTERNAL_P (gnu_field))
8363 tree gnu_field_type = TREE_TYPE (gnu_field);
8364 if (do_not_flatten_variant
8365 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8366 gnu_list
8367 = build_position_list (gnu_field_type, do_not_flatten_variant,
8368 size_zero_node, bitsize_zero_node,
8369 BIGGEST_ALIGNMENT, gnu_list);
8370 else
8371 gnu_list
8372 = build_position_list (gnu_field_type, do_not_flatten_variant,
8373 gnu_our_offset, gnu_our_bitpos,
8374 our_offset_align, gnu_list);
8378 return gnu_list;
8381 /* Return a list describing the substitutions needed to reflect the
8382 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8383 be in any order. The values in an element of the list are in the form
8384 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8385 a definition of GNAT_SUBTYPE. */
8387 static vec<subst_pair>
8388 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8390 vec<subst_pair> gnu_list = vNULL;
8391 Entity_Id gnat_discrim;
8392 Node_Id gnat_constr;
8394 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8395 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8396 Present (gnat_discrim);
8397 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8398 gnat_constr = Next_Elmt (gnat_constr))
8399 /* Ignore access discriminants. */
8400 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8402 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8403 tree replacement = convert (TREE_TYPE (gnu_field),
8404 elaborate_expression
8405 (Node (gnat_constr), gnat_subtype,
8406 get_entity_char (gnat_discrim),
8407 definition, true, false));
8408 subst_pair s = { gnu_field, replacement };
8409 gnu_list.safe_push (s);
8412 return gnu_list;
8415 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8416 variants of QUAL_UNION_TYPE that are still relevant after applying
8417 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8418 list to be prepended to the newly created entries. */
8420 static vec<variant_desc>
8421 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8422 vec<variant_desc> gnu_list)
8424 tree gnu_field;
8426 for (gnu_field = TYPE_FIELDS (qual_union_type);
8427 gnu_field;
8428 gnu_field = DECL_CHAIN (gnu_field))
8430 tree qual = DECL_QUALIFIER (gnu_field);
8431 unsigned int i;
8432 subst_pair *s;
8434 FOR_EACH_VEC_ELT (subst_list, i, s)
8435 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8437 /* If the new qualifier is not unconditionally false, its variant may
8438 still be accessed. */
8439 if (!integer_zerop (qual))
8441 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8442 variant_desc v = { variant_type, gnu_field, qual, NULL_TREE };
8444 gnu_list.safe_push (v);
8446 /* Recurse on the variant subpart of the variant, if any. */
8447 variant_subpart = get_variant_part (variant_type);
8448 if (variant_subpart)
8449 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8450 subst_list, gnu_list);
8452 /* If the new qualifier is unconditionally true, the subsequent
8453 variants cannot be accessed. */
8454 if (integer_onep (qual))
8455 break;
8459 return gnu_list;
8462 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8463 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8464 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8465 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8466 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8467 true if we are being called to process the Component_Size of GNAT_OBJECT;
8468 this is used only for error messages. ZERO_OK is true if a size of zero
8469 is permitted; if ZERO_OK is false, it means that a size of zero should be
8470 treated as an unspecified size. */
8472 static tree
8473 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8474 enum tree_code kind, bool component_p, bool zero_ok)
8476 Node_Id gnat_error_node;
8477 tree type_size, size;
8479 /* Return 0 if no size was specified. */
8480 if (uint_size == No_Uint)
8481 return NULL_TREE;
8483 /* Ignore a negative size since that corresponds to our back-annotation. */
8484 if (UI_Lt (uint_size, Uint_0))
8485 return NULL_TREE;
8487 /* Find the node to use for error messages. */
8488 if ((Ekind (gnat_object) == E_Component
8489 || Ekind (gnat_object) == E_Discriminant)
8490 && Present (Component_Clause (gnat_object)))
8491 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8492 else if (Present (Size_Clause (gnat_object)))
8493 gnat_error_node = Expression (Size_Clause (gnat_object));
8494 else
8495 gnat_error_node = gnat_object;
8497 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8498 but cannot be represented in bitsizetype. */
8499 size = UI_To_gnu (uint_size, bitsizetype);
8500 if (TREE_OVERFLOW (size))
8502 if (component_p)
8503 post_error_ne ("component size for& is too large", gnat_error_node,
8504 gnat_object);
8505 else
8506 post_error_ne ("size for& is too large", gnat_error_node,
8507 gnat_object);
8508 return NULL_TREE;
8511 /* Ignore a zero size if it is not permitted. */
8512 if (!zero_ok && integer_zerop (size))
8513 return NULL_TREE;
8515 /* The size of objects is always a multiple of a byte. */
8516 if (kind == VAR_DECL
8517 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8519 if (component_p)
8520 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8521 gnat_error_node, gnat_object);
8522 else
8523 post_error_ne ("size for& is not a multiple of Storage_Unit",
8524 gnat_error_node, gnat_object);
8525 return NULL_TREE;
8528 /* If this is an integral type or a packed array type, the front-end has
8529 already verified the size, so we need not do it here (which would mean
8530 checking against the bounds). However, if this is an aliased object,
8531 it may not be smaller than the type of the object. */
8532 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8533 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8534 return size;
8536 /* If the object is a record that contains a template, add the size of the
8537 template to the specified size. */
8538 if (TREE_CODE (gnu_type) == RECORD_TYPE
8539 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8540 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8542 if (kind == VAR_DECL
8543 /* If a type needs strict alignment, a component of this type in
8544 a packed record cannot be packed and thus uses the type size. */
8545 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8546 type_size = TYPE_SIZE (gnu_type);
8547 else
8548 type_size = rm_size (gnu_type);
8550 /* Modify the size of a discriminated type to be the maximum size. */
8551 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8552 type_size = max_size (type_size, true);
8554 /* If this is an access type or a fat pointer, the minimum size is that given
8555 by the smallest integral mode that's valid for pointers. */
8556 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8558 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8559 while (!targetm.valid_pointer_mode (p_mode))
8560 p_mode = GET_MODE_WIDER_MODE (p_mode);
8561 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8564 /* Issue an error either if the default size of the object isn't a constant
8565 or if the new size is smaller than it. */
8566 if (TREE_CODE (type_size) != INTEGER_CST
8567 || TREE_OVERFLOW (type_size)
8568 || tree_int_cst_lt (size, type_size))
8570 if (component_p)
8571 post_error_ne_tree
8572 ("component size for& too small{, minimum allowed is ^}",
8573 gnat_error_node, gnat_object, type_size);
8574 else
8575 post_error_ne_tree
8576 ("size for& too small{, minimum allowed is ^}",
8577 gnat_error_node, gnat_object, type_size);
8578 return NULL_TREE;
8581 return size;
8584 /* Similarly, but both validate and process a value of RM size. This routine
8585 is only called for types. */
8587 static void
8588 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8590 Node_Id gnat_attr_node;
8591 tree old_size, size;
8593 /* Do nothing if no size was specified. */
8594 if (uint_size == No_Uint)
8595 return;
8597 /* Ignore a negative size since that corresponds to our back-annotation. */
8598 if (UI_Lt (uint_size, Uint_0))
8599 return;
8601 /* Only issue an error if a Value_Size clause was explicitly given.
8602 Otherwise, we'd be duplicating an error on the Size clause. */
8603 gnat_attr_node
8604 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8606 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8607 but cannot be represented in bitsizetype. */
8608 size = UI_To_gnu (uint_size, bitsizetype);
8609 if (TREE_OVERFLOW (size))
8611 if (Present (gnat_attr_node))
8612 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8613 gnat_entity);
8614 return;
8617 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8618 exists, or this is an integer type, in which case the front-end will
8619 have always set it. */
8620 if (No (gnat_attr_node)
8621 && integer_zerop (size)
8622 && !Has_Size_Clause (gnat_entity)
8623 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8624 return;
8626 old_size = rm_size (gnu_type);
8628 /* If the old size is self-referential, get the maximum size. */
8629 if (CONTAINS_PLACEHOLDER_P (old_size))
8630 old_size = max_size (old_size, true);
8632 /* Issue an error either if the old size of the object isn't a constant or
8633 if the new size is smaller than it. The front-end has already verified
8634 this for scalar and packed array types. */
8635 if (TREE_CODE (old_size) != INTEGER_CST
8636 || TREE_OVERFLOW (old_size)
8637 || (AGGREGATE_TYPE_P (gnu_type)
8638 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8639 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8640 && !(TYPE_IS_PADDING_P (gnu_type)
8641 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8642 && TYPE_PACKED_ARRAY_TYPE_P
8643 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8644 && tree_int_cst_lt (size, old_size)))
8646 if (Present (gnat_attr_node))
8647 post_error_ne_tree
8648 ("Value_Size for& too small{, minimum allowed is ^}",
8649 gnat_attr_node, gnat_entity, old_size);
8650 return;
8653 /* Otherwise, set the RM size proper for integral types... */
8654 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8655 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8656 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8657 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8658 SET_TYPE_RM_SIZE (gnu_type, size);
8660 /* ...or the Ada size for record and union types. */
8661 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8662 && !TYPE_FAT_POINTER_P (gnu_type))
8663 SET_TYPE_ADA_SIZE (gnu_type, size);
8666 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8667 a type or object whose present alignment is ALIGN. If this alignment is
8668 valid, return it. Otherwise, give an error and return ALIGN. */
8670 static unsigned int
8671 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8673 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8674 unsigned int new_align;
8675 Node_Id gnat_error_node;
8677 /* Don't worry about checking alignment if alignment was not specified
8678 by the source program and we already posted an error for this entity. */
8679 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8680 return align;
8682 /* Post the error on the alignment clause if any. Note, for the implicit
8683 base type of an array type, the alignment clause is on the first
8684 subtype. */
8685 if (Present (Alignment_Clause (gnat_entity)))
8686 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8688 else if (Is_Itype (gnat_entity)
8689 && Is_Array_Type (gnat_entity)
8690 && Etype (gnat_entity) == gnat_entity
8691 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8692 gnat_error_node =
8693 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8695 else
8696 gnat_error_node = gnat_entity;
8698 /* Within GCC, an alignment is an integer, so we must make sure a value is
8699 specified that fits in that range. Also, there is an upper bound to
8700 alignments we can support/allow. */
8701 if (!UI_Is_In_Int_Range (alignment)
8702 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8703 post_error_ne_num ("largest supported alignment for& is ^",
8704 gnat_error_node, gnat_entity, max_allowed_alignment);
8705 else if (!(Present (Alignment_Clause (gnat_entity))
8706 && From_At_Mod (Alignment_Clause (gnat_entity)))
8707 && new_align * BITS_PER_UNIT < align)
8709 unsigned int double_align;
8710 bool is_capped_double, align_clause;
8712 /* If the default alignment of "double" or larger scalar types is
8713 specifically capped and the new alignment is above the cap, do
8714 not post an error and change the alignment only if there is an
8715 alignment clause; this makes it possible to have the associated
8716 GCC type overaligned by default for performance reasons. */
8717 if ((double_align = double_float_alignment) > 0)
8719 Entity_Id gnat_type
8720 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8721 is_capped_double
8722 = is_double_float_or_array (gnat_type, &align_clause);
8724 else if ((double_align = double_scalar_alignment) > 0)
8726 Entity_Id gnat_type
8727 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8728 is_capped_double
8729 = is_double_scalar_or_array (gnat_type, &align_clause);
8731 else
8732 is_capped_double = align_clause = false;
8734 if (is_capped_double && new_align >= double_align)
8736 if (align_clause)
8737 align = new_align * BITS_PER_UNIT;
8739 else
8741 if (is_capped_double)
8742 align = double_align * BITS_PER_UNIT;
8744 post_error_ne_num ("alignment for& must be at least ^",
8745 gnat_error_node, gnat_entity,
8746 align / BITS_PER_UNIT);
8749 else
8751 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8752 if (new_align > align)
8753 align = new_align;
8756 return align;
8759 /* Verify that TYPE is something we can implement atomically. If not, issue
8760 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8761 process a component type. */
8763 static void
8764 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8766 Node_Id gnat_error_point = gnat_entity;
8767 Node_Id gnat_node;
8768 machine_mode mode;
8769 enum mode_class mclass;
8770 unsigned int align;
8771 tree size;
8773 /* If this is an anonymous base type, nothing to check, the error will be
8774 reported on the source type if need be. */
8775 if (!Comes_From_Source (gnat_entity))
8776 return;
8778 mode = TYPE_MODE (type);
8779 mclass = GET_MODE_CLASS (mode);
8780 align = TYPE_ALIGN (type);
8781 size = TYPE_SIZE (type);
8783 /* Consider all aligned floating-point types atomic and any aligned types
8784 that are represented by integers no wider than a machine word. */
8785 if ((mclass == MODE_FLOAT
8786 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8787 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8788 && align >= GET_MODE_ALIGNMENT (mode))
8789 return;
8791 /* For the moment, also allow anything that has an alignment equal to its
8792 size and which is smaller than a word. */
8793 if (size
8794 && TREE_CODE (size) == INTEGER_CST
8795 && compare_tree_int (size, align) == 0
8796 && align <= BITS_PER_WORD)
8797 return;
8799 for (gnat_node = First_Rep_Item (gnat_entity);
8800 Present (gnat_node);
8801 gnat_node = Next_Rep_Item (gnat_node))
8802 if (Nkind (gnat_node) == N_Pragma)
8804 unsigned char pragma_id
8805 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8807 if ((pragma_id == Pragma_Atomic && !component_p)
8808 || (pragma_id == Pragma_Atomic_Components && component_p))
8810 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8811 break;
8815 if (component_p)
8816 post_error_ne ("atomic access to component of & cannot be guaranteed",
8817 gnat_error_point, gnat_entity);
8818 else if (Is_Volatile_Full_Access (gnat_entity))
8819 post_error_ne ("volatile full access to & cannot be guaranteed",
8820 gnat_error_point, gnat_entity);
8821 else
8822 post_error_ne ("atomic access to & cannot be guaranteed",
8823 gnat_error_point, gnat_entity);
8827 /* Helper for the intrin compatibility checks family. Evaluate whether
8828 two types are definitely incompatible. */
8830 static bool
8831 intrin_types_incompatible_p (tree t1, tree t2)
8833 enum tree_code code;
8835 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8836 return false;
8838 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8839 return true;
8841 if (TREE_CODE (t1) != TREE_CODE (t2))
8842 return true;
8844 code = TREE_CODE (t1);
8846 switch (code)
8848 case INTEGER_TYPE:
8849 case REAL_TYPE:
8850 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8852 case POINTER_TYPE:
8853 case REFERENCE_TYPE:
8854 /* Assume designated types are ok. We'd need to account for char * and
8855 void * variants to do better, which could rapidly get messy and isn't
8856 clearly worth the effort. */
8857 return false;
8859 default:
8860 break;
8863 return false;
8866 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8867 on the Ada/builtin argument lists for the INB binding. */
8869 static bool
8870 intrin_arglists_compatible_p (intrin_binding_t * inb)
8872 function_args_iterator ada_iter, btin_iter;
8874 function_args_iter_init (&ada_iter, inb->ada_fntype);
8875 function_args_iter_init (&btin_iter, inb->btin_fntype);
8877 /* Sequence position of the last argument we checked. */
8878 int argpos = 0;
8880 while (true)
8882 tree ada_type = function_args_iter_cond (&ada_iter);
8883 tree btin_type = function_args_iter_cond (&btin_iter);
8885 /* If we've exhausted both lists simultaneously, we're done. */
8886 if (!ada_type && !btin_type)
8887 break;
8889 /* If one list is shorter than the other, they fail to match. */
8890 if (!ada_type || !btin_type)
8891 return false;
8893 /* If we're done with the Ada args and not with the internal builtin
8894 args, or the other way around, complain. */
8895 if (ada_type == void_type_node
8896 && btin_type != void_type_node)
8898 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8899 return false;
8902 if (btin_type == void_type_node
8903 && ada_type != void_type_node)
8905 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8906 inb->gnat_entity, inb->gnat_entity, argpos);
8907 return false;
8910 /* Otherwise, check that types match for the current argument. */
8911 argpos ++;
8912 if (intrin_types_incompatible_p (ada_type, btin_type))
8914 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8915 inb->gnat_entity, inb->gnat_entity, argpos);
8916 return false;
8920 function_args_iter_next (&ada_iter);
8921 function_args_iter_next (&btin_iter);
8924 return true;
8927 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8928 on the Ada/builtin return values for the INB binding. */
8930 static bool
8931 intrin_return_compatible_p (intrin_binding_t * inb)
8933 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8934 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8936 /* Accept function imported as procedure, common and convenient. */
8937 if (VOID_TYPE_P (ada_return_type)
8938 && !VOID_TYPE_P (btin_return_type))
8939 return true;
8941 /* Check return types compatibility otherwise. Note that this
8942 handles void/void as well. */
8943 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8945 post_error ("?intrinsic binding type mismatch on return value!",
8946 inb->gnat_entity);
8947 return false;
8950 return true;
8953 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8954 compatible. Issue relevant warnings when they are not.
8956 This is intended as a light check to diagnose the most obvious cases, not
8957 as a full fledged type compatibility predicate. It is the programmer's
8958 responsibility to ensure correctness of the Ada declarations in Imports,
8959 especially when binding straight to a compiler internal. */
8961 static bool
8962 intrin_profiles_compatible_p (intrin_binding_t * inb)
8964 /* Check compatibility on return values and argument lists, each responsible
8965 for posting warnings as appropriate. Ensure use of the proper sloc for
8966 this purpose. */
8968 bool arglists_compatible_p, return_compatible_p;
8969 location_t saved_location = input_location;
8971 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8973 return_compatible_p = intrin_return_compatible_p (inb);
8974 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8976 input_location = saved_location;
8978 return return_compatible_p && arglists_compatible_p;
8981 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8982 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8983 specified size for this field. POS_LIST is a position list describing
8984 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8985 to this layout. */
8987 static tree
8988 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8989 tree size, tree pos_list,
8990 vec<subst_pair> subst_list)
8992 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8993 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8994 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8995 tree new_pos, new_field;
8996 unsigned int i;
8997 subst_pair *s;
8999 if (CONTAINS_PLACEHOLDER_P (pos))
9000 FOR_EACH_VEC_ELT (subst_list, i, s)
9001 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
9003 /* If the position is now a constant, we can set it as the position of the
9004 field when we make it. Otherwise, we need to deal with it specially. */
9005 if (TREE_CONSTANT (pos))
9006 new_pos = bit_from_pos (pos, bitpos);
9007 else
9008 new_pos = NULL_TREE;
9010 new_field
9011 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
9012 size, new_pos, DECL_PACKED (old_field),
9013 !DECL_NONADDRESSABLE_P (old_field));
9015 if (!new_pos)
9017 normalize_offset (&pos, &bitpos, offset_align);
9018 /* Finalize the position. */
9019 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
9020 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
9021 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
9022 DECL_SIZE (new_field) = size;
9023 DECL_SIZE_UNIT (new_field)
9024 = convert (sizetype,
9025 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
9026 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
9029 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
9030 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
9031 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
9032 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
9034 return new_field;
9037 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
9038 it is the minimal size the REP_PART must have. */
9040 static tree
9041 create_rep_part (tree rep_type, tree record_type, tree min_size)
9043 tree field;
9045 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
9046 min_size = NULL_TREE;
9048 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
9049 min_size, NULL_TREE, 0, 1);
9050 DECL_INTERNAL_P (field) = 1;
9052 return field;
9055 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9057 static tree
9058 get_rep_part (tree record_type)
9060 tree field = TYPE_FIELDS (record_type);
9062 /* The REP part is the first field, internal, another record, and its name
9063 starts with an 'R'. */
9064 if (field
9065 && DECL_INTERNAL_P (field)
9066 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9067 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9068 return field;
9070 return NULL_TREE;
9073 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9075 tree
9076 get_variant_part (tree record_type)
9078 tree field;
9080 /* The variant part is the only internal field that is a qualified union. */
9081 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9082 if (DECL_INTERNAL_P (field)
9083 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9084 return field;
9086 return NULL_TREE;
9089 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9090 the list of variants to be used and RECORD_TYPE is the type of the parent.
9091 POS_LIST is a position list describing the layout of fields present in
9092 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9093 layout. DEBUG_INFO_P is true if we need to write debug information. */
9095 static tree
9096 create_variant_part_from (tree old_variant_part,
9097 vec<variant_desc> variant_list,
9098 tree record_type, tree pos_list,
9099 vec<subst_pair> subst_list,
9100 bool debug_info_p)
9102 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9103 tree old_union_type = TREE_TYPE (old_variant_part);
9104 tree new_union_type, new_variant_part;
9105 tree union_field_list = NULL_TREE;
9106 variant_desc *v;
9107 unsigned int i;
9109 /* First create the type of the variant part from that of the old one. */
9110 new_union_type = make_node (QUAL_UNION_TYPE);
9111 TYPE_NAME (new_union_type)
9112 = concat_name (TYPE_NAME (record_type),
9113 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9115 /* If the position of the variant part is constant, subtract it from the
9116 size of the type of the parent to get the new size. This manual CSE
9117 reduces the code size when not optimizing. */
9118 if (TREE_CODE (offset) == INTEGER_CST
9119 && TYPE_SIZE (record_type)
9120 && TYPE_SIZE_UNIT (record_type))
9122 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9123 tree first_bit = bit_from_pos (offset, bitpos);
9124 TYPE_SIZE (new_union_type)
9125 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9126 TYPE_SIZE_UNIT (new_union_type)
9127 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9128 byte_from_pos (offset, bitpos));
9129 SET_TYPE_ADA_SIZE (new_union_type,
9130 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9131 first_bit));
9132 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9133 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9135 else
9136 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9138 /* Now finish up the new variants and populate the union type. */
9139 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9141 tree old_field = v->field, new_field;
9142 tree old_variant, old_variant_subpart, new_variant, field_list;
9144 /* Skip variants that don't belong to this nesting level. */
9145 if (DECL_CONTEXT (old_field) != old_union_type)
9146 continue;
9148 /* Retrieve the list of fields already added to the new variant. */
9149 new_variant = v->new_type;
9150 field_list = TYPE_FIELDS (new_variant);
9152 /* If the old variant had a variant subpart, we need to create a new
9153 variant subpart and add it to the field list. */
9154 old_variant = v->type;
9155 old_variant_subpart = get_variant_part (old_variant);
9156 if (old_variant_subpart)
9158 tree new_variant_subpart
9159 = create_variant_part_from (old_variant_subpart, variant_list,
9160 new_variant, pos_list, subst_list,
9161 debug_info_p);
9162 DECL_CHAIN (new_variant_subpart) = field_list;
9163 field_list = new_variant_subpart;
9166 /* Finish up the new variant and create the field. */
9167 finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p);
9168 compute_record_mode (new_variant);
9169 create_type_decl (TYPE_NAME (new_variant), new_variant, true,
9170 debug_info_p, Empty);
9172 new_field
9173 = create_field_decl_from (old_field, new_variant, new_union_type,
9174 TYPE_SIZE (new_variant),
9175 pos_list, subst_list);
9176 DECL_QUALIFIER (new_field) = v->qual;
9177 DECL_INTERNAL_P (new_field) = 1;
9178 DECL_CHAIN (new_field) = union_field_list;
9179 union_field_list = new_field;
9182 /* Finish up the union type and create the variant part. Note that we don't
9183 reverse the field list because VARIANT_LIST has been traversed in reverse
9184 order. */
9185 finish_record_type (new_union_type, union_field_list, 2, debug_info_p);
9186 compute_record_mode (new_union_type);
9187 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true,
9188 debug_info_p, Empty);
9190 new_variant_part
9191 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9192 TYPE_SIZE (new_union_type),
9193 pos_list, subst_list);
9194 DECL_INTERNAL_P (new_variant_part) = 1;
9196 /* With multiple discriminants it is possible for an inner variant to be
9197 statically selected while outer ones are not; in this case, the list
9198 of fields of the inner variant is not flattened and we end up with a
9199 qualified union with a single member. Drop the useless container. */
9200 if (!DECL_CHAIN (union_field_list))
9202 DECL_CONTEXT (union_field_list) = record_type;
9203 DECL_FIELD_OFFSET (union_field_list)
9204 = DECL_FIELD_OFFSET (new_variant_part);
9205 DECL_FIELD_BIT_OFFSET (union_field_list)
9206 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9207 SET_DECL_OFFSET_ALIGN (union_field_list,
9208 DECL_OFFSET_ALIGN (new_variant_part));
9209 new_variant_part = union_field_list;
9212 return new_variant_part;
9215 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9216 which are both RECORD_TYPE, after applying the substitutions described
9217 in SUBST_LIST. */
9219 static void
9220 copy_and_substitute_in_size (tree new_type, tree old_type,
9221 vec<subst_pair> subst_list)
9223 unsigned int i;
9224 subst_pair *s;
9226 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9227 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9228 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9229 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9230 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9232 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9233 FOR_EACH_VEC_ELT (subst_list, i, s)
9234 TYPE_SIZE (new_type)
9235 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9236 s->discriminant, s->replacement);
9238 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9239 FOR_EACH_VEC_ELT (subst_list, i, s)
9240 TYPE_SIZE_UNIT (new_type)
9241 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9242 s->discriminant, s->replacement);
9244 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9245 FOR_EACH_VEC_ELT (subst_list, i, s)
9246 SET_TYPE_ADA_SIZE
9247 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9248 s->discriminant, s->replacement));
9250 /* Finalize the size. */
9251 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9252 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9255 /* Return true if DISC is a stored discriminant of RECORD_TYPE. */
9257 static inline bool
9258 is_stored_discriminant (Entity_Id discr, Entity_Id record_type)
9260 if (Is_Tagged_Type (record_type))
9261 return No (Corresponding_Discriminant (discr));
9262 else if (Ekind (record_type) == E_Record_Type)
9263 return Original_Record_Component (discr) == discr;
9264 else
9265 return true;
9268 /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are
9269 both record types, after applying the substitutions described in SUBST_LIST.
9270 DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */
9272 static void
9273 copy_and_substitute_in_layout (Entity_Id gnat_new_type,
9274 Entity_Id gnat_old_type,
9275 tree gnu_new_type,
9276 tree gnu_old_type,
9277 vec<subst_pair> gnu_subst_list,
9278 bool debug_info_p)
9280 const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype);
9281 tree gnu_field_list = NULL_TREE;
9282 bool selected_variant, all_constant_pos = true;
9283 vec<variant_desc> gnu_variant_list;
9285 /* Look for REP and variant parts in the old type. */
9286 tree gnu_rep_part = get_rep_part (gnu_old_type);
9287 tree gnu_variant_part = get_variant_part (gnu_old_type);
9289 /* If there is a variant part, we must compute whether the constraints
9290 statically select a particular variant. If so, we simply drop the
9291 qualified union and flatten the list of fields. Otherwise we will
9292 build a new qualified union for the variants that are still relevant. */
9293 if (gnu_variant_part)
9295 variant_desc *v;
9296 unsigned int i;
9298 gnu_variant_list = build_variant_list (TREE_TYPE (gnu_variant_part),
9299 gnu_subst_list, vNULL);
9301 /* If all the qualifiers are unconditionally true, the innermost variant
9302 is statically selected. */
9303 selected_variant = true;
9304 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9305 if (!integer_onep (v->qual))
9307 selected_variant = false;
9308 break;
9311 /* Otherwise, create the new variants. */
9312 if (!selected_variant)
9313 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9315 tree old_variant = v->type;
9316 tree new_variant = make_node (RECORD_TYPE);
9317 tree suffix
9318 = concat_name (DECL_NAME (gnu_variant_part),
9319 IDENTIFIER_POINTER (DECL_NAME (v->field)));
9320 TYPE_NAME (new_variant)
9321 = concat_name (TYPE_NAME (gnu_new_type),
9322 IDENTIFIER_POINTER (suffix));
9323 TYPE_REVERSE_STORAGE_ORDER (new_variant)
9324 = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type);
9325 copy_and_substitute_in_size (new_variant, old_variant,
9326 gnu_subst_list);
9327 v->new_type = new_variant;
9330 else
9332 gnu_variant_list.create (0);
9333 selected_variant = false;
9336 /* Make a list of fields and their position in the old type. */
9337 tree gnu_pos_list
9338 = build_position_list (gnu_old_type,
9339 gnu_variant_list.exists () && !selected_variant,
9340 size_zero_node, bitsize_zero_node,
9341 BIGGEST_ALIGNMENT, NULL_TREE);
9343 /* Now go down every component in the new type and compute its size and
9344 position from those of the component in the old type and the stored
9345 constraints of the new type. */
9346 Entity_Id gnat_field, gnat_old_field;
9347 for (gnat_field = First_Entity (gnat_new_type);
9348 Present (gnat_field);
9349 gnat_field = Next_Entity (gnat_field))
9350 if ((Ekind (gnat_field) == E_Component
9351 || (Ekind (gnat_field) == E_Discriminant
9352 && is_stored_discriminant (gnat_field, gnat_new_type)))
9353 && (gnat_old_field = is_subtype
9354 ? Original_Record_Component (gnat_field)
9355 : Corresponding_Record_Component (gnat_field))
9356 && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type
9357 && present_gnu_tree (gnat_old_field))
9359 Name_Id gnat_name = Chars (gnat_field);
9360 tree gnu_old_field = get_gnu_tree (gnat_old_field);
9361 if (TREE_CODE (gnu_old_field) == COMPONENT_REF)
9362 gnu_old_field = TREE_OPERAND (gnu_old_field, 1);
9363 tree gnu_context = DECL_CONTEXT (gnu_old_field);
9364 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
9365 tree gnu_cont_type, gnu_last = NULL_TREE;
9367 /* If the type is the same, retrieve the GCC type from the
9368 old field to take into account possible adjustments. */
9369 if (Etype (gnat_field) == Etype (gnat_old_field))
9370 gnu_field_type = TREE_TYPE (gnu_old_field);
9371 else
9372 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
9374 /* If there was a component clause, the field types must be the same
9375 for the old and new types, so copy the data from the old field to
9376 avoid recomputation here. Also if the field is justified modular
9377 and the optimization in gnat_to_gnu_field was applied. */
9378 if (Present (Component_Clause (gnat_old_field))
9379 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
9380 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
9381 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
9382 == TREE_TYPE (gnu_old_field)))
9384 gnu_size = DECL_SIZE (gnu_old_field);
9385 gnu_field_type = TREE_TYPE (gnu_old_field);
9388 /* If the old field was packed and of constant size, we have to get the
9389 old size here as it might differ from what the Etype conveys and the
9390 latter might overlap with the following field. Try to arrange the
9391 type for possible better packing along the way. */
9392 else if (DECL_PACKED (gnu_old_field)
9393 && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST)
9395 gnu_size = DECL_SIZE (gnu_old_field);
9396 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
9397 && !TYPE_FAT_POINTER_P (gnu_field_type)
9398 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
9399 gnu_field_type = make_packable_type (gnu_field_type, true);
9402 else
9403 gnu_size = TYPE_SIZE (gnu_field_type);
9405 /* If the context of the old field is the old type or its REP part,
9406 put the field directly in the new type; otherwise look up the
9407 context in the variant list and put the field either in the new
9408 type if there is a selected variant or in one new variant. */
9409 if (gnu_context == gnu_old_type
9410 || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part)))
9411 gnu_cont_type = gnu_new_type;
9412 else
9414 variant_desc *v;
9415 unsigned int i;
9416 tree rep_part;
9418 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9419 if (gnu_context == v->type
9420 || ((rep_part = get_rep_part (v->type))
9421 && gnu_context == TREE_TYPE (rep_part)))
9422 break;
9424 if (v)
9425 gnu_cont_type = selected_variant ? gnu_new_type : v->new_type;
9426 else
9427 /* The front-end may pass us "ghost" components if it fails to
9428 recognize that a constrain statically selects a particular
9429 variant. Discard them. */
9430 continue;
9433 /* Now create the new field modeled on the old one. */
9434 gnu_field
9435 = create_field_decl_from (gnu_old_field, gnu_field_type,
9436 gnu_cont_type, gnu_size,
9437 gnu_pos_list, gnu_subst_list);
9438 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
9440 /* If the context is a variant, put it in the new variant directly. */
9441 if (gnu_cont_type != gnu_new_type)
9443 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
9444 TYPE_FIELDS (gnu_cont_type) = gnu_field;
9447 /* To match the layout crafted in components_to_record, if this is
9448 the _Tag or _Parent field, put it before any other fields. */
9449 else if (gnat_name == Name_uTag || gnat_name == Name_uParent)
9450 gnu_field_list = chainon (gnu_field_list, gnu_field);
9452 /* Similarly, if this is the _Controller field, put it before the
9453 other fields except for the _Tag or _Parent field. */
9454 else if (gnat_name == Name_uController && gnu_last)
9456 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
9457 DECL_CHAIN (gnu_last) = gnu_field;
9460 /* Otherwise, put it after the other fields. */
9461 else
9463 DECL_CHAIN (gnu_field) = gnu_field_list;
9464 gnu_field_list = gnu_field;
9465 if (!gnu_last)
9466 gnu_last = gnu_field;
9467 if (TREE_CODE (gnu_pos) != INTEGER_CST)
9468 all_constant_pos = false;
9471 /* For a stored discriminant in a derived type, replace the field. */
9472 if (!is_subtype && Ekind (gnat_field) == E_Discriminant)
9474 tree gnu_ref = get_gnu_tree (gnat_field);
9475 TREE_OPERAND (gnu_ref, 1) = gnu_field;
9477 else
9478 save_gnu_tree (gnat_field, gnu_field, false);
9481 /* If there is a variant list, a selected variant and the fields all have a
9482 constant position, put them in order of increasing position to match that
9483 of constant CONSTRUCTORs. Likewise if there is no variant list but a REP
9484 part, since the latter has been flattened in the process. */
9485 if ((gnu_variant_list.exists () ? selected_variant : gnu_rep_part != NULL)
9486 && all_constant_pos)
9488 const int len = list_length (gnu_field_list);
9489 tree *field_arr = XALLOCAVEC (tree, len), t = gnu_field_list;
9491 for (int i = 0; t; t = DECL_CHAIN (t), i++)
9492 field_arr[i] = t;
9494 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
9496 gnu_field_list = NULL_TREE;
9497 for (int i = 0; i < len; i++)
9499 DECL_CHAIN (field_arr[i]) = gnu_field_list;
9500 gnu_field_list = field_arr[i];
9504 /* If there is a variant list and no selected variant, we need to create the
9505 nest of variant parts from the old nest. */
9506 else if (gnu_variant_list.exists () && !selected_variant)
9508 tree new_variant_part
9509 = create_variant_part_from (gnu_variant_part, gnu_variant_list,
9510 gnu_new_type, gnu_pos_list,
9511 gnu_subst_list, debug_info_p);
9512 DECL_CHAIN (new_variant_part) = gnu_field_list;
9513 gnu_field_list = new_variant_part;
9516 gnu_variant_list.release ();
9517 gnu_subst_list.release ();
9519 gnu_field_list = nreverse (gnu_field_list);
9521 /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE.
9522 Otherwise sizes and alignment must be computed independently. */
9523 if (is_subtype)
9525 finish_record_type (gnu_new_type, gnu_field_list, 2, debug_info_p);
9526 compute_record_mode (gnu_new_type);
9528 else
9529 finish_record_type (gnu_new_type, gnu_field_list, 1, debug_info_p);
9531 /* Now go through the entities again looking for Itypes that we have not yet
9532 elaborated (e.g. Etypes of fields that have Original_Components). */
9533 for (Entity_Id gnat_field = First_Entity (gnat_new_type);
9534 Present (gnat_field);
9535 gnat_field = Next_Entity (gnat_field))
9536 if ((Ekind (gnat_field) == E_Component
9537 || Ekind (gnat_field) == E_Discriminant)
9538 && Is_Itype (Etype (gnat_field))
9539 && !present_gnu_tree (Etype (gnat_field)))
9540 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
9543 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9544 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9545 the original array type if it has been translated. This association is a
9546 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9547 that for standard DWARF, we also want to get the original type name. */
9549 static void
9550 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9552 Entity_Id gnat_original_array_type
9553 = Underlying_Type (Original_Array_Type (gnat_entity));
9554 tree gnu_original_array_type;
9556 if (!present_gnu_tree (gnat_original_array_type))
9557 return;
9559 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9561 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9562 return;
9564 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9566 tree original_name = TYPE_NAME (gnu_original_array_type);
9568 if (TREE_CODE (original_name) == TYPE_DECL)
9569 original_name = DECL_NAME (original_name);
9571 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9572 TYPE_NAME (gnu_type) = original_name;
9574 else
9575 add_parallel_type (gnu_type, gnu_original_array_type);
9578 /* Given a type T, a FIELD_DECL F, and a replacement value R, return an
9579 equivalent type with adjusted size expressions where all occurrences
9580 of references to F in a PLACEHOLDER_EXPR have been replaced by R.
9582 The function doesn't update the layout of the type, i.e. it assumes
9583 that the substitution is purely formal. That's why the replacement
9584 value R must itself contain a PLACEHOLDER_EXPR. */
9586 tree
9587 substitute_in_type (tree t, tree f, tree r)
9589 tree nt;
9591 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9593 switch (TREE_CODE (t))
9595 case INTEGER_TYPE:
9596 case ENUMERAL_TYPE:
9597 case BOOLEAN_TYPE:
9598 case REAL_TYPE:
9600 /* First the domain types of arrays. */
9601 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9602 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9604 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9605 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9607 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9608 return t;
9610 nt = copy_type (t);
9611 TYPE_GCC_MIN_VALUE (nt) = low;
9612 TYPE_GCC_MAX_VALUE (nt) = high;
9614 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9615 SET_TYPE_INDEX_TYPE
9616 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9618 return nt;
9621 /* Then the subtypes. */
9622 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9623 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9625 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9626 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9628 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9629 return t;
9631 nt = copy_type (t);
9632 SET_TYPE_RM_MIN_VALUE (nt, low);
9633 SET_TYPE_RM_MAX_VALUE (nt, high);
9635 return nt;
9638 return t;
9640 case COMPLEX_TYPE:
9641 nt = substitute_in_type (TREE_TYPE (t), f, r);
9642 if (nt == TREE_TYPE (t))
9643 return t;
9645 return build_complex_type (nt);
9647 case FUNCTION_TYPE:
9648 /* These should never show up here. */
9649 gcc_unreachable ();
9651 case ARRAY_TYPE:
9653 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9654 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9656 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9657 return t;
9659 nt = build_nonshared_array_type (component, domain);
9660 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9661 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9662 SET_TYPE_MODE (nt, TYPE_MODE (t));
9663 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9664 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9665 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9666 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9667 if (TYPE_REVERSE_STORAGE_ORDER (t))
9668 set_reverse_storage_order_on_array_type (nt);
9669 if (TYPE_NONALIASED_COMPONENT (t))
9670 set_nonaliased_component_on_array_type (nt);
9671 return nt;
9674 case RECORD_TYPE:
9675 case UNION_TYPE:
9676 case QUAL_UNION_TYPE:
9678 bool changed_field = false;
9679 tree field;
9681 /* Start out with no fields, make new fields, and chain them
9682 in. If we haven't actually changed the type of any field,
9683 discard everything we've done and return the old type. */
9684 nt = copy_type (t);
9685 TYPE_FIELDS (nt) = NULL_TREE;
9687 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9689 tree new_field = copy_node (field), new_n;
9691 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9692 if (new_n != TREE_TYPE (field))
9694 TREE_TYPE (new_field) = new_n;
9695 changed_field = true;
9698 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9699 if (new_n != DECL_FIELD_OFFSET (field))
9701 DECL_FIELD_OFFSET (new_field) = new_n;
9702 changed_field = true;
9705 /* Do the substitution inside the qualifier, if any. */
9706 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9708 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9709 if (new_n != DECL_QUALIFIER (field))
9711 DECL_QUALIFIER (new_field) = new_n;
9712 changed_field = true;
9716 DECL_CONTEXT (new_field) = nt;
9717 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9719 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9720 TYPE_FIELDS (nt) = new_field;
9723 if (!changed_field)
9724 return t;
9726 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9727 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9728 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9729 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9730 return nt;
9733 default:
9734 return t;
9738 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9739 needed to represent the object. */
9741 tree
9742 rm_size (tree gnu_type)
9744 /* For integral types, we store the RM size explicitly. */
9745 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9746 return TYPE_RM_SIZE (gnu_type);
9748 /* Return the RM size of the actual data plus the size of the template. */
9749 if (TREE_CODE (gnu_type) == RECORD_TYPE
9750 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9751 return
9752 size_binop (PLUS_EXPR,
9753 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9754 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9756 /* For record or union types, we store the size explicitly. */
9757 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9758 && !TYPE_FAT_POINTER_P (gnu_type)
9759 && TYPE_ADA_SIZE (gnu_type))
9760 return TYPE_ADA_SIZE (gnu_type);
9762 /* For other types, this is just the size. */
9763 return TYPE_SIZE (gnu_type);
9766 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9767 fully-qualified name, possibly with type information encoding.
9768 Otherwise, return the name. */
9770 static const char *
9771 get_entity_char (Entity_Id gnat_entity)
9773 Get_Encoded_Name (gnat_entity);
9774 return ggc_strdup (Name_Buffer);
9777 tree
9778 get_entity_name (Entity_Id gnat_entity)
9780 Get_Encoded_Name (gnat_entity);
9781 return get_identifier_with_length (Name_Buffer, Name_Len);
9784 /* Return an identifier representing the external name to be used for
9785 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9786 and the specified suffix. */
9788 tree
9789 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9791 const Entity_Kind kind = Ekind (gnat_entity);
9792 const bool has_suffix = (suffix != NULL);
9793 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9794 String_Pointer sp = {suffix, &temp};
9796 Get_External_Name (gnat_entity, has_suffix, sp);
9798 /* A variable using the Stdcall convention lives in a DLL. We adjust
9799 its name to use the jump table, the _imp__NAME contains the address
9800 for the NAME variable. */
9801 if ((kind == E_Variable || kind == E_Constant)
9802 && Has_Stdcall_Convention (gnat_entity))
9804 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9805 char *new_name = (char *) alloca (len + 1);
9806 strcpy (new_name, STDCALL_PREFIX);
9807 strcat (new_name, Name_Buffer);
9808 return get_identifier_with_length (new_name, len);
9811 return get_identifier_with_length (Name_Buffer, Name_Len);
9814 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9815 string, return a new IDENTIFIER_NODE that is the concatenation of
9816 the name followed by "___" and the specified suffix. */
9818 tree
9819 concat_name (tree gnu_name, const char *suffix)
9821 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9822 char *new_name = (char *) alloca (len + 1);
9823 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9824 strcat (new_name, "___");
9825 strcat (new_name, suffix);
9826 return get_identifier_with_length (new_name, len);
9829 /* Initialize data structures of the decl.c module. */
9831 void
9832 init_gnat_decl (void)
9834 /* Initialize the cache of annotated values. */
9835 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9837 /* Initialize the association of dummy types with subprograms. */
9838 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
9841 /* Destroy data structures of the decl.c module. */
9843 void
9844 destroy_gnat_decl (void)
9846 /* Destroy the cache of annotated values. */
9847 annotate_value_cache->empty ();
9848 annotate_value_cache = NULL;
9850 /* Destroy the association of dummy types with subprograms. */
9851 dummy_to_subprog_map->empty ();
9852 dummy_to_subprog_map = NULL;
9855 #include "gt-ada-decl.h"