* gcc-interface/decl.c (gnat_to_gnu_entity): Only set the TYPE_ALIGN_OK
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobdf88ce7849e658305f9579e38bcc277746ffbace
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2017, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
37 #include "demangle.h"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "uintp.h"
48 #include "urealp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
55 /* "stdcall" and "thiscall" conventions should be processed in a specific way
56 on 32-bit x86/Windows only. The macros below are helpers to avoid having
57 to check for a Windows specific attribute throughout this unit. */
59 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #ifdef TARGET_64BIT
61 #define Has_Stdcall_Convention(E) \
62 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
63 #define Has_Thiscall_Convention(E) \
64 (!TARGET_64BIT && is_cplusplus_method (E))
65 #else
66 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
67 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
68 #endif
69 #else
70 #define Has_Stdcall_Convention(E) 0
71 #define Has_Thiscall_Convention(E) 0
72 #endif
74 #define STDCALL_PREFIX "_imp__"
76 /* Stack realignment is necessary for functions with foreign conventions when
77 the ABI doesn't mandate as much as what the compiler assumes - that is, up
78 to PREFERRED_STACK_BOUNDARY.
80 Such realignment can be requested with a dedicated function type attribute
81 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
82 characterize the situations where the attribute should be set. We rely on
83 compiler configuration settings for 'main' to decide. */
85 #ifdef MAIN_STACK_BOUNDARY
86 #define FOREIGN_FORCE_REALIGN_STACK \
87 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
88 #else
89 #define FOREIGN_FORCE_REALIGN_STACK 0
90 #endif
92 struct incomplete
94 struct incomplete *next;
95 tree old_type;
96 Entity_Id full_type;
99 /* These variables are used to defer recursively expanding incomplete types
100 while we are processing a record, an array or a subprogram type. */
101 static int defer_incomplete_level = 0;
102 static struct incomplete *defer_incomplete_list;
104 /* This variable is used to delay expanding types coming from a limited with
105 clause and completed Taft Amendment types until the end of the spec. */
106 static struct incomplete *defer_limited_with_list;
108 typedef struct subst_pair_d {
109 tree discriminant;
110 tree replacement;
111 } subst_pair;
114 typedef struct variant_desc_d {
115 /* The type of the variant. */
116 tree type;
118 /* The associated field. */
119 tree field;
121 /* The value of the qualifier. */
122 tree qual;
124 /* The type of the variant after transformation. */
125 tree new_type;
126 } variant_desc;
129 /* A map used to cache the result of annotate_value. */
130 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
132 static inline hashval_t
133 hash (tree_int_map *m)
135 return htab_hash_pointer (m->base.from);
138 static inline bool
139 equal (tree_int_map *a, tree_int_map *b)
141 return a->base.from == b->base.from;
144 static int
145 keep_cache_entry (tree_int_map *&m)
147 return ggc_marked_p (m->base.from);
151 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
153 /* A map used to associate a dummy type with a list of subprogram entities. */
154 struct GTY((for_user)) tree_entity_vec_map
156 struct tree_map_base base;
157 vec<Entity_Id, va_gc_atomic> *to;
160 void
161 gt_pch_nx (Entity_Id &)
165 void
166 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
168 op (x, cookie);
171 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
173 static inline hashval_t
174 hash (tree_entity_vec_map *m)
176 return htab_hash_pointer (m->base.from);
179 static inline bool
180 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
182 return a->base.from == b->base.from;
185 static int
186 keep_cache_entry (tree_entity_vec_map *&m)
188 return ggc_marked_p (m->base.from);
192 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
194 static void prepend_one_attribute (struct attrib **,
195 enum attrib_type, tree, tree, Node_Id);
196 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
197 static void prepend_attributes (struct attrib **, Entity_Id);
198 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
199 bool);
200 static bool type_has_variable_size (tree);
201 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
202 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
203 unsigned int);
204 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
205 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
206 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
207 static int adjust_packed (tree, tree, int);
208 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
209 static tree gnu_ext_name_for_subprog (Entity_Id, tree);
210 static tree change_qualified_type (tree, int);
211 static void set_nonaliased_component_on_array_type (tree);
212 static void set_reverse_storage_order_on_array_type (tree);
213 static bool same_discriminant_p (Entity_Id, Entity_Id);
214 static bool array_type_has_nonaliased_component (tree, Entity_Id);
215 static bool compile_time_known_address_p (Node_Id);
216 static bool cannot_be_superflat (Node_Id);
217 static bool constructor_address_p (tree);
218 static bool allocatable_size_p (tree, bool);
219 static bool initial_value_needs_conversion (tree, tree);
220 static int compare_field_bitpos (const PTR, const PTR);
221 static bool components_to_record (Node_Id, Entity_Id, tree, tree, int, bool,
222 bool, bool, bool, bool, bool, bool, tree,
223 tree *);
224 static Uint annotate_value (tree);
225 static void annotate_rep (Entity_Id, tree);
226 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
227 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
228 static vec<variant_desc> build_variant_list (tree, vec<subst_pair>,
229 vec<variant_desc>);
230 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
231 static void set_rm_size (Uint, tree, Entity_Id);
232 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
233 static 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 is in
314 the main unit, except for E_Access_Subtype because it's actually a use
315 of its base type, and for E_Record_Subtype with cloned subtype because
316 it's actually a use of the cloned subtype, see below. */
317 if (!definition
318 && is_type
319 && Is_Itype (gnat_entity)
320 && !(kind == E_Access_Subtype
321 || (kind == E_Record_Subtype
322 && Present (Cloned_Subtype (gnat_entity))))
323 && !present_gnu_tree (gnat_entity)
324 && In_Extended_Main_Code_Unit (gnat_entity))
326 /* Ensure that we are in a subprogram mentioned in the Scope chain of
327 this entity, our current scope is global, or we encountered a task
328 or entry (where we can't currently accurately check scoping). */
329 if (!current_function_decl
330 || DECL_ELABORATION_PROC_P (current_function_decl))
332 process_type (gnat_entity);
333 return get_gnu_tree (gnat_entity);
336 for (gnat_temp = Scope (gnat_entity);
337 Present (gnat_temp);
338 gnat_temp = Scope (gnat_temp))
340 if (Is_Type (gnat_temp))
341 gnat_temp = Underlying_Type (gnat_temp);
343 if (Ekind (gnat_temp) == E_Subprogram_Body)
344 gnat_temp
345 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
347 if (Is_Subprogram (gnat_temp)
348 && Present (Protected_Body_Subprogram (gnat_temp)))
349 gnat_temp = Protected_Body_Subprogram (gnat_temp);
351 if (Ekind (gnat_temp) == E_Entry
352 || Ekind (gnat_temp) == E_Entry_Family
353 || Ekind (gnat_temp) == E_Task_Type
354 || (Is_Subprogram (gnat_temp)
355 && present_gnu_tree (gnat_temp)
356 && (current_function_decl
357 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
359 process_type (gnat_entity);
360 return get_gnu_tree (gnat_entity);
364 /* This abort means the Itype has an incorrect scope, i.e. that its
365 scope does not correspond to the subprogram it is declared in. */
366 gcc_unreachable ();
369 /* If we've already processed this entity, return what we got last time.
370 If we are defining the node, we should not have already processed it.
371 In that case, we will abort below when we try to save a new GCC tree
372 for this object. We also need to handle the case of getting a dummy
373 type when a Full_View exists but be careful so as not to trigger its
374 premature elaboration. */
375 if ((!definition || (is_type && imported_p))
376 && present_gnu_tree (gnat_entity))
378 gnu_decl = get_gnu_tree (gnat_entity);
380 if (TREE_CODE (gnu_decl) == TYPE_DECL
381 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
382 && IN (kind, Incomplete_Or_Private_Kind)
383 && Present (Full_View (gnat_entity))
384 && (present_gnu_tree (Full_View (gnat_entity))
385 || No (Freeze_Node (Full_View (gnat_entity)))))
387 gnu_decl
388 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, false);
389 save_gnu_tree (gnat_entity, NULL_TREE, false);
390 save_gnu_tree (gnat_entity, gnu_decl, false);
393 return gnu_decl;
396 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
397 must be specified unless it was specified by the programmer. Exceptions
398 are for access-to-protected-subprogram types and all access subtypes, as
399 another GNAT type is used to lay out the GCC type for them. */
400 gcc_assert (!is_type
401 || Known_Esize (gnat_entity)
402 || Has_Size_Clause (gnat_entity)
403 || (!IN (kind, Numeric_Kind)
404 && !IN (kind, Enumeration_Kind)
405 && (!IN (kind, Access_Kind)
406 || kind == E_Access_Protected_Subprogram_Type
407 || kind == E_Anonymous_Access_Protected_Subprogram_Type
408 || kind == E_Access_Subtype
409 || type_annotate_only)));
411 /* The RM size must be specified for all discrete and fixed-point types. */
412 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
413 && Unknown_RM_Size (gnat_entity)));
415 /* If we get here, it means we have not yet done anything with this entity.
416 If we are not defining it, it must be a type or an entity that is defined
417 elsewhere or externally, otherwise we should have defined it already. */
418 gcc_assert (definition
419 || type_annotate_only
420 || is_type
421 || kind == E_Discriminant
422 || kind == E_Component
423 || kind == E_Label
424 || (kind == E_Constant && Present (Full_View (gnat_entity)))
425 || Is_Public (gnat_entity));
427 /* Get the name of the entity and set up the line number and filename of
428 the original definition for use in any decl we make. Make sure we do not
429 inherit another source location. */
430 gnu_entity_name = get_entity_name (gnat_entity);
431 if (Sloc (gnat_entity) != No_Location
432 && !renaming_from_instantiation_p (gnat_entity))
433 Sloc_to_locus (Sloc (gnat_entity), &input_location);
435 /* For cases when we are not defining (i.e., we are referencing from
436 another compilation unit) public entities, show we are at global level
437 for the purpose of computing scopes. Don't do this for components or
438 discriminants since the relevant test is whether or not the record is
439 being defined. */
440 if (!definition
441 && kind != E_Component
442 && kind != E_Discriminant
443 && Is_Public (gnat_entity)
444 && !Is_Statically_Allocated (gnat_entity))
445 force_global++, this_global = true;
447 /* Handle any attributes directly attached to the entity. */
448 if (Has_Gigi_Rep_Item (gnat_entity))
449 prepend_attributes (&attr_list, gnat_entity);
451 /* Do some common processing for types. */
452 if (is_type)
454 /* Compute the equivalent type to be used in gigi. */
455 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
457 /* Machine_Attributes on types are expected to be propagated to
458 subtypes. The corresponding Gigi_Rep_Items are only attached
459 to the first subtype though, so we handle the propagation here. */
460 if (Base_Type (gnat_entity) != gnat_entity
461 && !Is_First_Subtype (gnat_entity)
462 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
463 prepend_attributes (&attr_list,
464 First_Subtype (Base_Type (gnat_entity)));
466 /* Compute a default value for the size of an elementary type. */
467 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
469 unsigned int max_esize;
471 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
472 esize = UI_To_Int (Esize (gnat_entity));
474 if (IN (kind, Float_Kind))
475 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
476 else if (IN (kind, Access_Kind))
477 max_esize = POINTER_SIZE * 2;
478 else
479 max_esize = LONG_LONG_TYPE_SIZE;
481 if (esize > max_esize)
482 esize = max_esize;
486 switch (kind)
488 case E_Component:
489 case E_Discriminant:
491 /* The GNAT record where the component was defined. */
492 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
494 /* If the entity is a discriminant of an extended tagged type used to
495 rename a discriminant of the parent type, return the latter. */
496 if (kind == E_Discriminant
497 && Present (Corresponding_Discriminant (gnat_entity))
498 && Is_Tagged_Type (gnat_record))
500 gnu_decl
501 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
502 gnu_expr, definition);
503 saved = true;
504 break;
507 /* If the entity is an inherited component (in the case of extended
508 tagged record types), just return the original entity, which must
509 be a FIELD_DECL. Likewise for discriminants. If the entity is a
510 non-girder discriminant (in the case of derived untagged record
511 types), return the stored discriminant it renames. */
512 if (Present (Original_Record_Component (gnat_entity))
513 && Original_Record_Component (gnat_entity) != gnat_entity)
515 gnu_decl
516 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
517 gnu_expr, definition);
518 /* GNU_DECL contains a PLACEHOLDER_EXPR for discriminants. */
519 if (kind == E_Discriminant)
520 saved = true;
521 break;
524 /* Otherwise, if we are not defining this and we have no GCC type
525 for the containing record, make one for it. Then we should
526 have made our own equivalent. */
527 if (!definition && !present_gnu_tree (gnat_record))
529 /* ??? If this is in a record whose scope is a protected
530 type and we have an Original_Record_Component, use it.
531 This is a workaround for major problems in protected type
532 handling. */
533 Entity_Id Scop = Scope (Scope (gnat_entity));
534 if (Is_Protected_Type (Underlying_Type (Scop))
535 && Present (Original_Record_Component (gnat_entity)))
537 gnu_decl
538 = gnat_to_gnu_entity (Original_Record_Component
539 (gnat_entity),
540 gnu_expr, false);
542 else
544 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
545 gnu_decl = get_gnu_tree (gnat_entity);
548 saved = true;
549 break;
552 /* Here we have no GCC type and this is a reference rather than a
553 definition. This should never happen. Most likely the cause is
554 reference before declaration in the GNAT tree for gnat_entity. */
555 gcc_unreachable ();
558 case E_Constant:
559 /* Ignore constant definitions already marked with the error node. See
560 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
561 if (definition
562 && present_gnu_tree (gnat_entity)
563 && get_gnu_tree (gnat_entity) == error_mark_node)
565 maybe_present = true;
566 break;
569 /* Ignore deferred constant definitions without address clause since
570 they are processed fully in the front-end. If No_Initialization
571 is set, this is not a deferred constant but a constant whose value
572 is built manually. And constants that are renamings are handled
573 like variables. */
574 if (definition
575 && !gnu_expr
576 && No (Address_Clause (gnat_entity))
577 && !No_Initialization (Declaration_Node (gnat_entity))
578 && No (Renamed_Object (gnat_entity)))
580 gnu_decl = error_mark_node;
581 saved = true;
582 break;
585 /* If this is a use of a deferred constant without address clause,
586 get its full definition. */
587 if (!definition
588 && No (Address_Clause (gnat_entity))
589 && Present (Full_View (gnat_entity)))
591 gnu_decl
592 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
593 saved = true;
594 break;
597 /* If we have a constant that we are not defining, get the expression it
598 was defined to represent. This is necessary to avoid generating dumb
599 elaboration code in simple cases, but we may throw it away later if it
600 is not a constant. But do not retrieve it if it is an allocator since
601 the designated type might still be dummy at this point. */
602 if (!definition
603 && !No_Initialization (Declaration_Node (gnat_entity))
604 && Present (Expression (Declaration_Node (gnat_entity)))
605 && Nkind (Expression (Declaration_Node (gnat_entity)))
606 != N_Allocator)
607 /* The expression may contain N_Expression_With_Actions nodes and
608 thus object declarations from other units. Discard them. */
609 gnu_expr
610 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
612 /* ... fall through ... */
614 case E_Exception:
615 case E_Loop_Parameter:
616 case E_Out_Parameter:
617 case E_Variable:
619 const Entity_Id gnat_type = Etype (gnat_entity);
620 /* Always create a variable for volatile objects and variables seen
621 constant but with a Linker_Section pragma. */
622 bool const_flag
623 = ((kind == E_Constant || kind == E_Variable)
624 && Is_True_Constant (gnat_entity)
625 && !(kind == E_Variable
626 && Present (Linker_Section_Pragma (gnat_entity)))
627 && !Treat_As_Volatile (gnat_entity)
628 && (((Nkind (Declaration_Node (gnat_entity))
629 == N_Object_Declaration)
630 && Present (Expression (Declaration_Node (gnat_entity))))
631 || Present (Renamed_Object (gnat_entity))
632 || imported_p));
633 bool inner_const_flag = const_flag;
634 bool static_flag = Is_Statically_Allocated (gnat_entity);
635 /* We implement RM 13.3(19) for exported and imported (non-constant)
636 objects by making them volatile. */
637 bool volatile_flag
638 = (Treat_As_Volatile (gnat_entity)
639 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
640 bool mutable_p = false;
641 bool used_by_ref = false;
642 tree gnu_ext_name = NULL_TREE;
643 tree renamed_obj = NULL_TREE;
644 tree gnu_object_size;
646 /* We need to translate the renamed object even though we are only
647 referencing the renaming. But it may contain a call for which
648 we'll generate a temporary to hold the return value and which
649 is part of the definition of the renaming, so discard it. */
650 if (Present (Renamed_Object (gnat_entity)) && !definition)
652 if (kind == E_Exception)
653 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
654 NULL_TREE, false);
655 else
656 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
659 /* Get the type after elaborating the renamed object. */
660 if (Has_Foreign_Convention (gnat_entity)
661 && Is_Descendant_Of_Address (gnat_type))
662 gnu_type = ptr_type_node;
663 else
665 gnu_type = gnat_to_gnu_type (gnat_type);
667 /* If this is a standard exception definition, use the standard
668 exception type. This is necessary to make sure that imported
669 and exported views of exceptions are merged in LTO mode. */
670 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
671 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
672 gnu_type = except_type_node;
675 /* For a debug renaming declaration, build a debug-only entity. */
676 if (Present (Debug_Renaming_Link (gnat_entity)))
678 /* Force a non-null value to make sure the symbol is retained. */
679 tree value = build1 (INDIRECT_REF, gnu_type,
680 build1 (NOP_EXPR,
681 build_pointer_type (gnu_type),
682 integer_minus_one_node));
683 gnu_decl = build_decl (input_location,
684 VAR_DECL, gnu_entity_name, gnu_type);
685 SET_DECL_VALUE_EXPR (gnu_decl, value);
686 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
687 TREE_STATIC (gnu_decl) = global_bindings_p ();
688 gnat_pushdecl (gnu_decl, gnat_entity);
689 break;
692 /* If this is a loop variable, its type should be the base type.
693 This is because the code for processing a loop determines whether
694 a normal loop end test can be done by comparing the bounds of the
695 loop against those of the base type, which is presumed to be the
696 size used for computation. But this is not correct when the size
697 of the subtype is smaller than the type. */
698 if (kind == E_Loop_Parameter)
699 gnu_type = get_base_type (gnu_type);
701 /* Reject non-renamed objects whose type is an unconstrained array or
702 any object whose type is a dummy type or void. */
703 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
704 && No (Renamed_Object (gnat_entity)))
705 || TYPE_IS_DUMMY_P (gnu_type)
706 || TREE_CODE (gnu_type) == VOID_TYPE)
708 gcc_assert (type_annotate_only);
709 if (this_global)
710 force_global--;
711 return error_mark_node;
714 /* If an alignment is specified, use it if valid. Note that exceptions
715 are objects but don't have an alignment. We must do this before we
716 validate the size, since the alignment can affect the size. */
717 if (kind != E_Exception && Known_Alignment (gnat_entity))
719 gcc_assert (Present (Alignment (gnat_entity)));
721 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
722 TYPE_ALIGN (gnu_type));
724 /* No point in changing the type if there is an address clause
725 as the final type of the object will be a reference type. */
726 if (Present (Address_Clause (gnat_entity)))
727 align = 0;
728 else
730 tree orig_type = gnu_type;
732 gnu_type
733 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
734 false, false, definition, true);
736 /* If a padding record was made, declare it now since it will
737 never be declared otherwise. This is necessary to ensure
738 that its subtrees are properly marked. */
739 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
740 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
741 debug_info_p, gnat_entity);
745 /* If we are defining the object, see if it has a Size and validate it
746 if so. If we are not defining the object and a Size clause applies,
747 simply retrieve the value. We don't want to ignore the clause and
748 it is expected to have been validated already. Then get the new
749 type, if any. */
750 if (definition)
751 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
752 gnat_entity, VAR_DECL, false,
753 Has_Size_Clause (gnat_entity));
754 else if (Has_Size_Clause (gnat_entity))
755 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
757 if (gnu_size)
759 gnu_type
760 = make_type_from_size (gnu_type, gnu_size,
761 Has_Biased_Representation (gnat_entity));
763 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
764 gnu_size = NULL_TREE;
767 /* If this object has self-referential size, it must be a record with
768 a default discriminant. We are supposed to allocate an object of
769 the maximum size in this case, unless it is a constant with an
770 initializing expression, in which case we can get the size from
771 that. Note that the resulting size may still be a variable, so
772 this may end up with an indirect allocation. */
773 if (No (Renamed_Object (gnat_entity))
774 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
776 if (gnu_expr && kind == E_Constant)
778 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
779 if (CONTAINS_PLACEHOLDER_P (size))
781 /* If the initializing expression is itself a constant,
782 despite having a nominal type with self-referential
783 size, we can get the size directly from it. */
784 if (TREE_CODE (gnu_expr) == COMPONENT_REF
785 && TYPE_IS_PADDING_P
786 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
787 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
788 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
789 || DECL_READONLY_ONCE_ELAB
790 (TREE_OPERAND (gnu_expr, 0))))
791 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
792 else
793 gnu_size
794 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
796 else
797 gnu_size = size;
799 /* We may have no GNU_EXPR because No_Initialization is
800 set even though there's an Expression. */
801 else if (kind == E_Constant
802 && (Nkind (Declaration_Node (gnat_entity))
803 == N_Object_Declaration)
804 && Present (Expression (Declaration_Node (gnat_entity))))
805 gnu_size
806 = TYPE_SIZE (gnat_to_gnu_type
807 (Etype
808 (Expression (Declaration_Node (gnat_entity)))));
809 else
811 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
812 mutable_p = true;
815 /* If the size isn't constant and we are at global level, call
816 elaborate_expression_1 to make a variable for it rather than
817 calculating it each time. */
818 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
819 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
820 "SIZE", definition, false);
823 /* If the size is zero byte, make it one byte since some linkers have
824 troubles with zero-sized objects. If the object will have a
825 template, that will make it nonzero so don't bother. Also avoid
826 doing that for an object renaming or an object with an address
827 clause, as we would lose useful information on the view size
828 (e.g. for null array slices) and we are not allocating the object
829 here anyway. */
830 if (((gnu_size
831 && integer_zerop (gnu_size)
832 && !TREE_OVERFLOW (gnu_size))
833 || (TYPE_SIZE (gnu_type)
834 && integer_zerop (TYPE_SIZE (gnu_type))
835 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
836 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
837 && No (Renamed_Object (gnat_entity))
838 && No (Address_Clause (gnat_entity)))
839 gnu_size = bitsize_unit_node;
841 /* If this is an object with no specified size and alignment, and
842 if either it is atomic or we are not optimizing alignment for
843 space and it is composite and not an exception, an Out parameter
844 or a reference to another object, and the size of its type is a
845 constant, set the alignment to the smallest one which is not
846 smaller than the size, with an appropriate cap. */
847 if (!gnu_size && align == 0
848 && (Is_Atomic_Or_VFA (gnat_entity)
849 || (!Optimize_Alignment_Space (gnat_entity)
850 && kind != E_Exception
851 && kind != E_Out_Parameter
852 && Is_Composite_Type (gnat_type)
853 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
854 && !Is_Exported (gnat_entity)
855 && !imported_p
856 && No (Renamed_Object (gnat_entity))
857 && No (Address_Clause (gnat_entity))))
858 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
860 unsigned int size_cap, align_cap;
862 /* No point in promoting the alignment if this doesn't prevent
863 BLKmode access to the object, in particular block copy, as
864 this will for example disable the NRV optimization for it.
865 No point in jumping through all the hoops needed in order
866 to support BIGGEST_ALIGNMENT if we don't really have to.
867 So we cap to the smallest alignment that corresponds to
868 a known efficient memory access pattern of the target. */
869 if (Is_Atomic_Or_VFA (gnat_entity))
871 size_cap = UINT_MAX;
872 align_cap = BIGGEST_ALIGNMENT;
874 else
876 size_cap = MAX_FIXED_MODE_SIZE;
877 align_cap = get_mode_alignment (ptr_mode);
880 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
881 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
882 align = 0;
883 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
884 align = align_cap;
885 else
886 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
888 /* But make sure not to under-align the object. */
889 if (align <= TYPE_ALIGN (gnu_type))
890 align = 0;
892 /* And honor the minimum valid atomic alignment, if any. */
893 #ifdef MINIMUM_ATOMIC_ALIGNMENT
894 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
895 align = MINIMUM_ATOMIC_ALIGNMENT;
896 #endif
899 /* If the object is set to have atomic components, find the component
900 type and validate it.
902 ??? Note that we ignore Has_Volatile_Components on objects; it's
903 not at all clear what to do in that case. */
904 if (Has_Atomic_Components (gnat_entity))
906 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
907 ? TREE_TYPE (gnu_type) : gnu_type);
909 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
910 && TYPE_MULTI_ARRAY_P (gnu_inner))
911 gnu_inner = TREE_TYPE (gnu_inner);
913 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
916 /* If this is an aliased object with an unconstrained array nominal
917 subtype, make a type that includes the template. We will either
918 allocate or create a variable of that type, see below. */
919 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
920 && Is_Array_Type (Underlying_Type (gnat_type))
921 && !type_annotate_only)
923 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
924 gnu_type
925 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
926 gnu_type,
927 concat_name (gnu_entity_name,
928 "UNC"),
929 debug_info_p);
932 /* ??? If this is an object of CW type initialized to a value, try to
933 ensure that the object is sufficient aligned for this value, but
934 without pessimizing the allocation. This is a kludge necessary
935 because we don't support dynamic alignment. */
936 if (align == 0
937 && Ekind (gnat_type) == E_Class_Wide_Subtype
938 && No (Renamed_Object (gnat_entity))
939 && No (Address_Clause (gnat_entity)))
940 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
942 #ifdef MINIMUM_ATOMIC_ALIGNMENT
943 /* If the size is a constant and no alignment is specified, force
944 the alignment to be the minimum valid atomic alignment. The
945 restriction on constant size avoids problems with variable-size
946 temporaries; if the size is variable, there's no issue with
947 atomic access. Also don't do this for a constant, since it isn't
948 necessary and can interfere with constant replacement. Finally,
949 do not do it for Out parameters since that creates an
950 size inconsistency with In parameters. */
951 if (align == 0
952 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
953 && !FLOAT_TYPE_P (gnu_type)
954 && !const_flag && No (Renamed_Object (gnat_entity))
955 && !imported_p && No (Address_Clause (gnat_entity))
956 && kind != E_Out_Parameter
957 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
958 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
959 align = MINIMUM_ATOMIC_ALIGNMENT;
960 #endif
962 /* Make a new type with the desired size and alignment, if needed.
963 But do not take into account alignment promotions to compute the
964 size of the object. */
965 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
966 if (gnu_size || align > 0)
968 tree orig_type = gnu_type;
970 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
971 false, false, definition, true);
973 /* If a padding record was made, declare it now since it will
974 never be declared otherwise. This is necessary to ensure
975 that its subtrees are properly marked. */
976 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
977 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
978 debug_info_p, gnat_entity);
981 /* Now check if the type of the object allows atomic access. */
982 if (Is_Atomic_Or_VFA (gnat_entity))
983 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
985 /* If this is a renaming, avoid as much as possible to create a new
986 object. However, in some cases, creating it is required because
987 renaming can be applied to objects that are not names in Ada.
988 This processing needs to be applied to the raw expression so as
989 to make it more likely to rename the underlying object. */
990 if (Present (Renamed_Object (gnat_entity)))
992 /* If the renamed object had padding, strip off the reference to
993 the inner object and reset our type. */
994 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
995 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
996 /* Strip useless conversions around the object. */
997 || gnat_useless_type_conversion (gnu_expr))
999 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1000 gnu_type = TREE_TYPE (gnu_expr);
1003 /* Or else, if the renamed object has an unconstrained type with
1004 default discriminant, use the padded type. */
1005 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
1006 gnu_type = TREE_TYPE (gnu_expr);
1008 /* Case 1: if this is a constant renaming stemming from a function
1009 call, treat it as a normal object whose initial value is what
1010 is being renamed. RM 3.3 says that the result of evaluating a
1011 function call is a constant object. Therefore, it can be the
1012 inner object of a constant renaming and the renaming must be
1013 fully instantiated, i.e. it cannot be a reference to (part of)
1014 an existing object. And treat other rvalues (addresses, null
1015 expressions, constructors and literals) the same way. */
1016 tree inner = gnu_expr;
1017 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
1018 inner = TREE_OPERAND (inner, 0);
1019 /* Expand_Dispatching_Call can prepend a comparison of the tags
1020 before the call to "=". */
1021 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
1022 || TREE_CODE (inner) == COMPOUND_EXPR)
1023 inner = TREE_OPERAND (inner, 1);
1024 if ((TREE_CODE (inner) == CALL_EXPR
1025 && !call_is_atomic_load (inner))
1026 || TREE_CODE (inner) == ADDR_EXPR
1027 || TREE_CODE (inner) == NULL_EXPR
1028 || TREE_CODE (inner) == PLUS_EXPR
1029 || TREE_CODE (inner) == CONSTRUCTOR
1030 || CONSTANT_CLASS_P (inner)
1031 /* We need to detect the case where a temporary is created to
1032 hold the return value, since we cannot safely rename it at
1033 top level as it lives only in the elaboration routine. */
1034 || (TREE_CODE (inner) == VAR_DECL
1035 && DECL_RETURN_VALUE_P (inner))
1036 /* We also need to detect the case where the front-end creates
1037 a dangling 'reference to a function call at top level and
1038 substitutes it in the renaming, for example:
1040 q__b : boolean renames r__f.e (1);
1042 can be rewritten into:
1044 q__R1s : constant q__A2s := r__f'reference;
1045 [...]
1046 q__b : boolean renames q__R1s.all.e (1);
1048 We cannot safely rename the rewritten expression since the
1049 underlying object lives only in the elaboration routine. */
1050 || (TREE_CODE (inner) == INDIRECT_REF
1051 && (inner
1052 = remove_conversions (TREE_OPERAND (inner, 0), true))
1053 && TREE_CODE (inner) == VAR_DECL
1054 && DECL_RETURN_VALUE_P (inner)))
1057 /* Case 2: if the renaming entity need not be materialized, use
1058 the elaborated renamed expression for the renaming. But this
1059 means that the caller is responsible for evaluating the address
1060 of the renaming in the correct place for the definition case to
1061 instantiate the SAVE_EXPRs. */
1062 else if (!Materialize_Entity (gnat_entity))
1064 tree init = NULL_TREE;
1066 gnu_decl
1067 = elaborate_reference (gnu_expr, gnat_entity, definition,
1068 &init);
1070 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1071 correct place for this case. */
1072 gcc_assert (!init);
1074 /* No DECL_EXPR will be created so the expression needs to be
1075 marked manually because it will likely be shared. */
1076 if (global_bindings_p ())
1077 MARK_VISITED (gnu_decl);
1079 /* This assertion will fail if the renamed object isn't aligned
1080 enough as to make it possible to honor the alignment set on
1081 the renaming. */
1082 if (align)
1084 unsigned int ralign = DECL_P (gnu_decl)
1085 ? DECL_ALIGN (gnu_decl)
1086 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1087 gcc_assert (ralign >= align);
1090 /* The expression might not be a DECL so save it manually. */
1091 save_gnu_tree (gnat_entity, gnu_decl, true);
1092 saved = true;
1093 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1094 break;
1097 /* Case 3: otherwise, make a constant pointer to the object we
1098 are renaming and attach the object to the pointer after it is
1099 elaborated. The object will be referenced directly instead
1100 of indirectly via the pointer to avoid aliasing problems with
1101 non-addressable entities. The pointer is called a "renaming"
1102 pointer in this case. Note that we also need to preserve the
1103 volatility of the renamed object through the indirection. */
1104 else
1106 tree init = NULL_TREE;
1108 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1109 gnu_type
1110 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1111 gnu_type = build_reference_type (gnu_type);
1112 used_by_ref = true;
1113 const_flag = true;
1114 volatile_flag = false;
1115 inner_const_flag = TREE_READONLY (gnu_expr);
1116 gnu_size = NULL_TREE;
1118 renamed_obj
1119 = elaborate_reference (gnu_expr, gnat_entity, definition,
1120 &init);
1122 /* The expression needs to be marked manually because it will
1123 likely be shared, even for a definition since the ADDR_EXPR
1124 built below can cause the first few nodes to be folded. */
1125 if (global_bindings_p ())
1126 MARK_VISITED (renamed_obj);
1128 if (type_annotate_only
1129 && TREE_CODE (renamed_obj) == ERROR_MARK)
1130 gnu_expr = NULL_TREE;
1131 else
1133 gnu_expr
1134 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1135 if (init)
1136 gnu_expr
1137 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1138 gnu_expr);
1143 /* If we are defining an aliased object whose nominal subtype is
1144 unconstrained, the object is a record that contains both the
1145 template and the object. If there is an initializer, it will
1146 have already been converted to the right type, but we need to
1147 create the template if there is no initializer. */
1148 if (definition
1149 && !gnu_expr
1150 && TREE_CODE (gnu_type) == RECORD_TYPE
1151 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1152 /* Beware that padding might have been introduced above. */
1153 || (TYPE_PADDING_P (gnu_type)
1154 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1155 == RECORD_TYPE
1156 && TYPE_CONTAINS_TEMPLATE_P
1157 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1159 tree template_field
1160 = TYPE_PADDING_P (gnu_type)
1161 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1162 : TYPE_FIELDS (gnu_type);
1163 vec<constructor_elt, va_gc> *v;
1164 vec_alloc (v, 1);
1165 tree t = build_template (TREE_TYPE (template_field),
1166 TREE_TYPE (DECL_CHAIN (template_field)),
1167 NULL_TREE);
1168 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1169 gnu_expr = gnat_build_constructor (gnu_type, v);
1172 /* Convert the expression to the type of the object if need be. */
1173 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1174 gnu_expr = convert (gnu_type, gnu_expr);
1176 /* If this is a pointer that doesn't have an initializing expression,
1177 initialize it to NULL, unless the object is declared imported as
1178 per RM B.1(24). */
1179 if (definition
1180 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1181 && !gnu_expr
1182 && !Is_Imported (gnat_entity))
1183 gnu_expr = integer_zero_node;
1185 /* If we are defining the object and it has an Address clause, we must
1186 either get the address expression from the saved GCC tree for the
1187 object if it has a Freeze node, or elaborate the address expression
1188 here since the front-end has guaranteed that the elaboration has no
1189 effects in this case. */
1190 if (definition && Present (Address_Clause (gnat_entity)))
1192 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1193 Node_Id gnat_address = Expression (gnat_clause);
1194 tree gnu_address
1195 = present_gnu_tree (gnat_entity)
1196 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1198 save_gnu_tree (gnat_entity, NULL_TREE, false);
1200 /* Convert the type of the object to a reference type that can
1201 alias everything as per RM 13.3(19). */
1202 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1203 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1204 gnu_type
1205 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1206 gnu_address = convert (gnu_type, gnu_address);
1207 used_by_ref = true;
1208 const_flag
1209 = (!Is_Public (gnat_entity)
1210 || compile_time_known_address_p (gnat_address));
1211 volatile_flag = false;
1212 gnu_size = NULL_TREE;
1214 /* If this is an aliased object with an unconstrained array nominal
1215 subtype, then it can overlay only another aliased object with an
1216 unconstrained array nominal subtype and compatible template. */
1217 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1218 && Is_Array_Type (Underlying_Type (gnat_type))
1219 && !type_annotate_only)
1221 tree rec_type = TREE_TYPE (gnu_type);
1222 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1224 /* This is the pattern built for a regular object. */
1225 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1226 && TREE_OPERAND (gnu_address, 1) == off)
1227 gnu_address = TREE_OPERAND (gnu_address, 0);
1228 /* This is the pattern built for an overaligned object. */
1229 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1230 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1231 == PLUS_EXPR
1232 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1233 == off)
1234 gnu_address
1235 = build2 (POINTER_PLUS_EXPR, gnu_type,
1236 TREE_OPERAND (gnu_address, 0),
1237 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1238 else
1240 post_error_ne ("aliased object& with unconstrained array "
1241 "nominal subtype", gnat_clause,
1242 gnat_entity);
1243 post_error ("\\can overlay only aliased object with "
1244 "compatible subtype", gnat_clause);
1248 /* If we don't have an initializing expression for the underlying
1249 variable, the initializing expression for the pointer is the
1250 specified address. Otherwise, we have to make a COMPOUND_EXPR
1251 to assign both the address and the initial value. */
1252 if (!gnu_expr)
1253 gnu_expr = gnu_address;
1254 else
1255 gnu_expr
1256 = build2 (COMPOUND_EXPR, gnu_type,
1257 build_binary_op (INIT_EXPR, NULL_TREE,
1258 build_unary_op (INDIRECT_REF,
1259 NULL_TREE,
1260 gnu_address),
1261 gnu_expr),
1262 gnu_address);
1265 /* If it has an address clause and we are not defining it, mark it
1266 as an indirect object. Likewise for Stdcall objects that are
1267 imported. */
1268 if ((!definition && Present (Address_Clause (gnat_entity)))
1269 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1271 /* Convert the type of the object to a reference type that can
1272 alias everything as per RM 13.3(19). */
1273 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1274 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1275 gnu_type
1276 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1277 used_by_ref = true;
1278 const_flag = false;
1279 volatile_flag = false;
1280 gnu_size = NULL_TREE;
1282 /* No point in taking the address of an initializing expression
1283 that isn't going to be used. */
1284 gnu_expr = NULL_TREE;
1286 /* If it has an address clause whose value is known at compile
1287 time, make the object a CONST_DECL. This will avoid a
1288 useless dereference. */
1289 if (Present (Address_Clause (gnat_entity)))
1291 Node_Id gnat_address
1292 = Expression (Address_Clause (gnat_entity));
1294 if (compile_time_known_address_p (gnat_address))
1296 gnu_expr = gnat_to_gnu (gnat_address);
1297 const_flag = true;
1302 /* If we are at top level and this object is of variable size,
1303 make the actual type a hidden pointer to the real type and
1304 make the initializer be a memory allocation and initialization.
1305 Likewise for objects we aren't defining (presumed to be
1306 external references from other packages), but there we do
1307 not set up an initialization.
1309 If the object's size overflows, make an allocator too, so that
1310 Storage_Error gets raised. Note that we will never free
1311 such memory, so we presume it never will get allocated. */
1312 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1313 global_bindings_p ()
1314 || !definition
1315 || static_flag)
1316 || (gnu_size
1317 && !allocatable_size_p (convert (sizetype,
1318 size_binop
1319 (CEIL_DIV_EXPR, gnu_size,
1320 bitsize_unit_node)),
1321 global_bindings_p ()
1322 || !definition
1323 || static_flag)))
1325 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1326 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1327 gnu_type = build_reference_type (gnu_type);
1328 used_by_ref = true;
1329 const_flag = true;
1330 volatile_flag = false;
1331 gnu_size = NULL_TREE;
1333 /* In case this was a aliased object whose nominal subtype is
1334 unconstrained, the pointer above will be a thin pointer and
1335 build_allocator will automatically make the template.
1337 If we have a template initializer only (that we made above),
1338 pretend there is none and rely on what build_allocator creates
1339 again anyway. Otherwise (if we have a full initializer), get
1340 the data part and feed that to build_allocator.
1342 If we are elaborating a mutable object, tell build_allocator to
1343 ignore a possibly simpler size from the initializer, if any, as
1344 we must allocate the maximum possible size in this case. */
1345 if (definition && !imported_p)
1347 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1349 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1350 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1352 gnu_alloc_type
1353 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1355 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1356 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1357 gnu_expr = NULL_TREE;
1358 else
1359 gnu_expr
1360 = build_component_ref
1361 (gnu_expr,
1362 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1363 false);
1366 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1367 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1368 post_error ("?`Storage_Error` will be raised at run time!",
1369 gnat_entity);
1371 gnu_expr
1372 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1373 Empty, Empty, gnat_entity, mutable_p);
1375 else
1376 gnu_expr = NULL_TREE;
1379 /* If this object would go into the stack and has an alignment larger
1380 than the largest stack alignment the back-end can honor, resort to
1381 a variable of "aligning type". */
1382 if (definition
1383 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1384 && !imported_p
1385 && !static_flag
1386 && !global_bindings_p ())
1388 /* Create the new variable. No need for extra room before the
1389 aligned field as this is in automatic storage. */
1390 tree gnu_new_type
1391 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1392 TYPE_SIZE_UNIT (gnu_type),
1393 BIGGEST_ALIGNMENT, 0, gnat_entity);
1394 tree gnu_new_var
1395 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1396 NULL_TREE, gnu_new_type, NULL_TREE,
1397 false, false, false, false, false,
1398 true, debug_info_p && definition, NULL,
1399 gnat_entity);
1401 /* Initialize the aligned field if we have an initializer. */
1402 if (gnu_expr)
1403 add_stmt_with_node
1404 (build_binary_op (INIT_EXPR, NULL_TREE,
1405 build_component_ref
1406 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1407 false),
1408 gnu_expr),
1409 gnat_entity);
1411 /* And setup this entity as a reference to the aligned field. */
1412 gnu_type = build_reference_type (gnu_type);
1413 gnu_expr
1414 = build_unary_op
1415 (ADDR_EXPR, NULL_TREE,
1416 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1417 false));
1418 TREE_CONSTANT (gnu_expr) = 1;
1420 used_by_ref = true;
1421 const_flag = true;
1422 volatile_flag = false;
1423 gnu_size = NULL_TREE;
1426 /* If this is an aliased object with an unconstrained array nominal
1427 subtype, we make its type a thin reference, i.e. the reference
1428 counterpart of a thin pointer, so it points to the array part.
1429 This is aimed to make it easier for the debugger to decode the
1430 object. Note that we have to do it this late because of the
1431 couple of allocation adjustments that might be made above. */
1432 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1433 && Is_Array_Type (Underlying_Type (gnat_type))
1434 && !type_annotate_only)
1436 /* In case the object with the template has already been allocated
1437 just above, we have nothing to do here. */
1438 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1440 /* This variable is a GNAT encoding used by Workbench: let it
1441 go through the debugging information but mark it as
1442 artificial: users are not interested in it. */
1443 tree gnu_unc_var
1444 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1445 NULL_TREE, gnu_type, gnu_expr,
1446 const_flag, Is_Public (gnat_entity),
1447 imported_p || !definition, static_flag,
1448 volatile_flag, true,
1449 debug_info_p && definition,
1450 NULL, gnat_entity);
1451 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1452 TREE_CONSTANT (gnu_expr) = 1;
1454 used_by_ref = true;
1455 const_flag = true;
1456 volatile_flag = false;
1457 inner_const_flag = TREE_READONLY (gnu_unc_var);
1458 gnu_size = NULL_TREE;
1461 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1462 gnu_type
1463 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1466 /* Convert the expression to the type of the object if need be. */
1467 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1468 gnu_expr = convert (gnu_type, gnu_expr);
1470 /* If this name is external or a name was specified, use it, but don't
1471 use the Interface_Name with an address clause (see cd30005). */
1472 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1473 || (Present (Interface_Name (gnat_entity))
1474 && No (Address_Clause (gnat_entity))))
1475 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1477 /* If this is an aggregate constant initialized to a constant, force it
1478 to be statically allocated. This saves an initialization copy. */
1479 if (!static_flag
1480 && const_flag
1481 && gnu_expr && TREE_CONSTANT (gnu_expr)
1482 && AGGREGATE_TYPE_P (gnu_type)
1483 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1484 && !(TYPE_IS_PADDING_P (gnu_type)
1485 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1486 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1487 static_flag = true;
1489 /* Deal with a pragma Linker_Section on a constant or variable. */
1490 if ((kind == E_Constant || kind == E_Variable)
1491 && Present (Linker_Section_Pragma (gnat_entity)))
1492 prepend_one_attribute_pragma (&attr_list,
1493 Linker_Section_Pragma (gnat_entity));
1495 /* Now create the variable or the constant and set various flags. */
1496 gnu_decl
1497 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1498 gnu_expr, const_flag, Is_Public (gnat_entity),
1499 imported_p || !definition, static_flag,
1500 volatile_flag, artificial_p,
1501 debug_info_p && definition, attr_list,
1502 gnat_entity, !renamed_obj);
1503 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1504 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1505 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1507 /* If we are defining an Out parameter and optimization isn't enabled,
1508 create a fake PARM_DECL for debugging purposes and make it point to
1509 the VAR_DECL. Suppress debug info for the latter but make sure it
1510 will live in memory so that it can be accessed from within the
1511 debugger through the PARM_DECL. */
1512 if (kind == E_Out_Parameter
1513 && definition
1514 && debug_info_p
1515 && !optimize
1516 && !flag_generate_lto)
1518 tree param = create_param_decl (gnu_entity_name, gnu_type);
1519 gnat_pushdecl (param, gnat_entity);
1520 SET_DECL_VALUE_EXPR (param, gnu_decl);
1521 DECL_HAS_VALUE_EXPR_P (param) = 1;
1522 DECL_IGNORED_P (gnu_decl) = 1;
1523 TREE_ADDRESSABLE (gnu_decl) = 1;
1526 /* If this is a loop parameter, set the corresponding flag. */
1527 else if (kind == E_Loop_Parameter)
1528 DECL_LOOP_PARM_P (gnu_decl) = 1;
1530 /* If this is a renaming pointer, attach the renamed object to it. */
1531 if (renamed_obj)
1532 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1534 /* If this is a constant and we are defining it or it generates a real
1535 symbol at the object level and we are referencing it, we may want
1536 or need to have a true variable to represent it:
1537 - if optimization isn't enabled, for debugging purposes,
1538 - if the constant is public and not overlaid on something else,
1539 - if its address is taken,
1540 - if either itself or its type is aliased. */
1541 if (TREE_CODE (gnu_decl) == CONST_DECL
1542 && (definition || Sloc (gnat_entity) > Standard_Location)
1543 && ((!optimize && debug_info_p)
1544 || (Is_Public (gnat_entity)
1545 && No (Address_Clause (gnat_entity)))
1546 || Address_Taken (gnat_entity)
1547 || Is_Aliased (gnat_entity)
1548 || Is_Aliased (gnat_type)))
1550 tree gnu_corr_var
1551 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1552 gnu_expr, true, Is_Public (gnat_entity),
1553 !definition, static_flag, volatile_flag,
1554 artificial_p, debug_info_p && definition,
1555 attr_list, gnat_entity, false);
1557 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1560 /* If this is a constant, even if we don't need a true variable, we
1561 may need to avoid returning the initializer in every case. That
1562 can happen for the address of a (constant) constructor because,
1563 upon dereferencing it, the constructor will be reinjected in the
1564 tree, which may not be valid in every case; see lvalue_required_p
1565 for more details. */
1566 if (TREE_CODE (gnu_decl) == CONST_DECL)
1567 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1569 /* If this object is declared in a block that contains a block with an
1570 exception handler, and we aren't using the GCC exception mechanism,
1571 we must force this variable in memory in order to avoid an invalid
1572 optimization. */
1573 if (Front_End_Exceptions ()
1574 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1575 TREE_ADDRESSABLE (gnu_decl) = 1;
1577 /* If this is a local variable with non-BLKmode and aggregate type,
1578 and optimization isn't enabled, then force it in memory so that
1579 a register won't be allocated to it with possible subparts left
1580 uninitialized and reaching the register allocator. */
1581 else if (TREE_CODE (gnu_decl) == VAR_DECL
1582 && !DECL_EXTERNAL (gnu_decl)
1583 && !TREE_STATIC (gnu_decl)
1584 && DECL_MODE (gnu_decl) != BLKmode
1585 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1586 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1587 && !optimize)
1588 TREE_ADDRESSABLE (gnu_decl) = 1;
1590 /* If we are defining an object with variable size or an object with
1591 fixed size that will be dynamically allocated, and we are using the
1592 front-end setjmp/longjmp exception mechanism, update the setjmp
1593 buffer. */
1594 if (definition
1595 && Exception_Mechanism == Front_End_SJLJ
1596 && get_block_jmpbuf_decl ()
1597 && DECL_SIZE_UNIT (gnu_decl)
1598 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1599 || (flag_stack_check == GENERIC_STACK_CHECK
1600 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1601 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1602 add_stmt_with_node (build_call_n_expr
1603 (update_setjmp_buf_decl, 1,
1604 build_unary_op (ADDR_EXPR, NULL_TREE,
1605 get_block_jmpbuf_decl ())),
1606 gnat_entity);
1608 /* Back-annotate Esize and Alignment of the object if not already
1609 known. Note that we pick the values of the type, not those of
1610 the object, to shield ourselves from low-level platform-dependent
1611 adjustments like alignment promotion. This is both consistent with
1612 all the treatment above, where alignment and size are set on the
1613 type of the object and not on the object directly, and makes it
1614 possible to support all confirming representation clauses. */
1615 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1616 used_by_ref);
1618 break;
1620 case E_Void:
1621 /* Return a TYPE_DECL for "void" that we previously made. */
1622 gnu_decl = TYPE_NAME (void_type_node);
1623 break;
1625 case E_Enumeration_Type:
1626 /* A special case: for the types Character and Wide_Character in
1627 Standard, we do not list all the literals. So if the literals
1628 are not specified, make this an integer type. */
1629 if (No (First_Literal (gnat_entity)))
1631 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1632 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1633 else
1634 gnu_type = make_unsigned_type (esize);
1635 TYPE_NAME (gnu_type) = gnu_entity_name;
1637 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1638 This is needed by the DWARF-2 back-end to distinguish between
1639 unsigned integer types and character types. */
1640 TYPE_STRING_FLAG (gnu_type) = 1;
1642 /* This flag is needed by the call just below. */
1643 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1645 finish_character_type (gnu_type);
1647 else
1649 /* We have a list of enumeral constants in First_Literal. We make a
1650 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1651 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1652 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1653 value of the literal. But when we have a regular boolean type, we
1654 simplify this a little by using a BOOLEAN_TYPE. */
1655 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1656 && !Has_Non_Standard_Rep (gnat_entity);
1657 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1658 tree gnu_list = NULL_TREE;
1659 Entity_Id gnat_literal;
1661 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1662 TYPE_PRECISION (gnu_type) = esize;
1663 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1664 set_min_and_max_values_for_integral_type (gnu_type, esize,
1665 TYPE_SIGN (gnu_type));
1666 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1667 layout_type (gnu_type);
1669 for (gnat_literal = First_Literal (gnat_entity);
1670 Present (gnat_literal);
1671 gnat_literal = Next_Literal (gnat_literal))
1673 tree gnu_value
1674 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1675 /* Do not generate debug info for individual enumerators. */
1676 tree gnu_literal
1677 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1678 gnu_type, gnu_value, true, false, false,
1679 false, false, artificial_p, false,
1680 NULL, gnat_literal);
1681 save_gnu_tree (gnat_literal, gnu_literal, false);
1682 gnu_list
1683 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1686 if (!is_boolean)
1687 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1689 /* Note that the bounds are updated at the end of this function
1690 to avoid an infinite recursion since they refer to the type. */
1691 goto discrete_type;
1693 break;
1695 case E_Signed_Integer_Type:
1696 /* For integer types, just make a signed type the appropriate number
1697 of bits. */
1698 gnu_type = make_signed_type (esize);
1699 goto discrete_type;
1701 case E_Ordinary_Fixed_Point_Type:
1702 case E_Decimal_Fixed_Point_Type:
1704 /* Small_Value is the scale factor. */
1705 const Ureal gnat_small_value = Small_Value (gnat_entity);
1706 tree scale_factor = NULL_TREE;
1708 gnu_type = make_signed_type (esize);
1710 /* Try to decode the scale factor and to save it for the fixed-point
1711 types debug hook. */
1713 /* There are various ways to describe the scale factor, however there
1714 are cases where back-end internals cannot hold it. In such cases,
1715 we output invalid scale factor for such cases (i.e. the 0/0
1716 rational constant) but we expect GNAT to output GNAT encodings,
1717 then. Thus, keep this in sync with
1718 Exp_Dbug.Is_Handled_Scale_Factor. */
1720 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1721 binary or decimal scale: it is easier to read for humans. */
1722 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1723 && (Rbase (gnat_small_value) == 2
1724 || Rbase (gnat_small_value) == 10))
1726 /* Given RM restrictions on 'Small values, we assume here that
1727 the denominator fits in an int. */
1728 const tree base = build_int_cst (integer_type_node,
1729 Rbase (gnat_small_value));
1730 const tree exponent
1731 = build_int_cst (integer_type_node,
1732 UI_To_Int (Denominator (gnat_small_value)));
1733 scale_factor
1734 = build2 (RDIV_EXPR, integer_type_node,
1735 integer_one_node,
1736 build2 (POWER_EXPR, integer_type_node,
1737 base, exponent));
1740 /* Default to arbitrary scale factors descriptions. */
1741 else
1743 const Uint num = Norm_Num (gnat_small_value);
1744 const Uint den = Norm_Den (gnat_small_value);
1746 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1748 const tree gnu_num
1749 = build_int_cst (integer_type_node,
1750 UI_To_Int (Norm_Num (gnat_small_value)));
1751 const tree gnu_den
1752 = build_int_cst (integer_type_node,
1753 UI_To_Int (Norm_Den (gnat_small_value)));
1754 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1755 gnu_num, gnu_den);
1757 else
1758 /* If compiler internals cannot represent arbitrary scale
1759 factors, output an invalid scale factor so that debugger
1760 don't try to handle them but so that we still have a type
1761 in the output. Note that GNAT */
1762 scale_factor = integer_zero_node;
1765 TYPE_FIXED_POINT_P (gnu_type) = 1;
1766 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1768 goto discrete_type;
1770 case E_Modular_Integer_Type:
1772 /* For modular types, make the unsigned type of the proper number
1773 of bits and then set up the modulus, if required. */
1774 tree gnu_modulus, gnu_high = NULL_TREE;
1776 /* Packed Array Impl. Types are supposed to be subtypes only. */
1777 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1779 gnu_type = make_unsigned_type (esize);
1781 /* Get the modulus in this type. If it overflows, assume it is because
1782 it is equal to 2**Esize. Note that there is no overflow checking
1783 done on unsigned type, so we detect the overflow by looking for
1784 a modulus of zero, which is otherwise invalid. */
1785 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1787 if (!integer_zerop (gnu_modulus))
1789 TYPE_MODULAR_P (gnu_type) = 1;
1790 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1791 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1792 build_int_cst (gnu_type, 1));
1795 /* If the upper bound is not maximal, make an extra subtype. */
1796 if (gnu_high
1797 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1799 tree gnu_subtype = make_unsigned_type (esize);
1800 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1801 TREE_TYPE (gnu_subtype) = gnu_type;
1802 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1803 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1804 gnu_type = gnu_subtype;
1807 goto discrete_type;
1809 case E_Signed_Integer_Subtype:
1810 case E_Enumeration_Subtype:
1811 case E_Modular_Integer_Subtype:
1812 case E_Ordinary_Fixed_Point_Subtype:
1813 case E_Decimal_Fixed_Point_Subtype:
1815 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1816 not want to call create_range_type since we would like each subtype
1817 node to be distinct. ??? Historically this was in preparation for
1818 when memory aliasing is implemented, but that's obsolete now given
1819 the call to relate_alias_sets below.
1821 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1822 this fact is used by the arithmetic conversion functions.
1824 We elaborate the Ancestor_Subtype if it is not in the current unit
1825 and one of our bounds is non-static. We do this to ensure consistent
1826 naming in the case where several subtypes share the same bounds, by
1827 elaborating the first such subtype first, thus using its name. */
1829 if (!definition
1830 && Present (Ancestor_Subtype (gnat_entity))
1831 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1832 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1833 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1834 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1836 /* Set the precision to the Esize except for bit-packed arrays. */
1837 if (Is_Packed_Array_Impl_Type (gnat_entity)
1838 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1839 esize = UI_To_Int (RM_Size (gnat_entity));
1841 /* First subtypes of Character are treated as Character; otherwise
1842 this should be an unsigned type if the base type is unsigned or
1843 if the lower bound is constant and non-negative or if the type
1844 is biased. However, even if the lower bound is constant and
1845 non-negative, we use a signed type for a subtype with the same
1846 size as its signed base type, because this eliminates useless
1847 conversions to it and gives more leeway to the optimizer; but
1848 this means that we will need to explicitly test for this case
1849 when we change the representation based on the RM size. */
1850 if (kind == E_Enumeration_Subtype
1851 && No (First_Literal (Etype (gnat_entity)))
1852 && Esize (gnat_entity) == RM_Size (gnat_entity)
1853 && esize == CHAR_TYPE_SIZE
1854 && flag_signed_char)
1855 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1856 else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity)))
1857 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1858 && Is_Unsigned_Type (gnat_entity))
1859 || Has_Biased_Representation (gnat_entity))
1860 gnu_type = make_unsigned_type (esize);
1861 else
1862 gnu_type = make_signed_type (esize);
1863 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1865 SET_TYPE_RM_MIN_VALUE
1866 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1867 gnat_entity, "L", definition, true,
1868 debug_info_p));
1870 SET_TYPE_RM_MAX_VALUE
1871 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1872 gnat_entity, "U", definition, true,
1873 debug_info_p));
1875 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1876 = Has_Biased_Representation (gnat_entity);
1878 /* Do the same processing for Character subtypes as for types. */
1879 if (TYPE_STRING_FLAG (TREE_TYPE (gnu_type)))
1881 TYPE_NAME (gnu_type) = gnu_entity_name;
1882 TYPE_STRING_FLAG (gnu_type) = 1;
1883 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1884 finish_character_type (gnu_type);
1887 /* Inherit our alias set from what we're a subtype of. Subtypes
1888 are not different types and a pointer can designate any instance
1889 within a subtype hierarchy. */
1890 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1892 /* One of the above calls might have caused us to be elaborated,
1893 so don't blow up if so. */
1894 if (present_gnu_tree (gnat_entity))
1896 maybe_present = true;
1897 break;
1900 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1901 TYPE_STUB_DECL (gnu_type)
1902 = create_type_stub_decl (gnu_entity_name, gnu_type);
1904 /* For a packed array, make the original array type a parallel/debug
1905 type. */
1906 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1907 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1909 discrete_type:
1911 /* We have to handle clauses that under-align the type specially. */
1912 if ((Present (Alignment_Clause (gnat_entity))
1913 || (Is_Packed_Array_Impl_Type (gnat_entity)
1914 && Present
1915 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1916 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1918 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1919 if (align >= TYPE_ALIGN (gnu_type))
1920 align = 0;
1923 /* If the type we are dealing with represents a bit-packed array,
1924 we need to have the bits left justified on big-endian targets
1925 and right justified on little-endian targets. We also need to
1926 ensure that when the value is read (e.g. for comparison of two
1927 such values), we only get the good bits, since the unused bits
1928 are uninitialized. Both goals are accomplished by wrapping up
1929 the modular type in an enclosing record type. */
1930 if (Is_Packed_Array_Impl_Type (gnat_entity)
1931 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1933 tree gnu_field_type, gnu_field;
1935 /* Set the RM size before wrapping up the original type. */
1936 SET_TYPE_RM_SIZE (gnu_type,
1937 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1938 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1940 /* Strip the ___XP suffix for standard DWARF. */
1941 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1942 gnu_entity_name = TYPE_NAME (gnu_type);
1944 /* Create a stripped-down declaration, mainly for debugging. */
1945 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1946 gnat_entity);
1948 /* Now save it and build the enclosing record type. */
1949 gnu_field_type = gnu_type;
1951 gnu_type = make_node (RECORD_TYPE);
1952 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1953 TYPE_PACKED (gnu_type) = 1;
1954 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1955 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1956 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1958 /* Propagate the alignment of the modular type to the record type,
1959 unless there is an alignment clause that under-aligns the type.
1960 This means that bit-packed arrays are given "ceil" alignment for
1961 their size by default, which may seem counter-intuitive but makes
1962 it possible to overlay them on modular types easily. */
1963 SET_TYPE_ALIGN (gnu_type,
1964 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1966 /* Propagate the reverse storage order flag to the record type so
1967 that the required byte swapping is performed when retrieving the
1968 enclosed modular value. */
1969 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1970 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1972 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1974 /* Don't declare the field as addressable since we won't be taking
1975 its address and this would prevent create_field_decl from making
1976 a bitfield. */
1977 gnu_field
1978 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1979 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1981 /* We will output additional debug info manually below. */
1982 finish_record_type (gnu_type, gnu_field, 2, false);
1983 compute_record_mode (gnu_type);
1984 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1986 if (debug_info_p)
1988 /* Make the original array type a parallel/debug type. */
1989 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1991 /* Since GNU_TYPE is a padding type around the packed array
1992 implementation type, the padded type is its debug type. */
1993 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1994 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1998 /* If the type we are dealing with has got a smaller alignment than the
1999 natural one, we need to wrap it up in a record type and misalign the
2000 latter; we reuse the padding machinery for this purpose. */
2001 else if (align > 0)
2003 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2005 /* Set the RM size before wrapping the type. */
2006 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
2008 gnu_type
2009 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
2010 gnat_entity, false, true, definition, false);
2012 TYPE_PACKED (gnu_type) = 1;
2013 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
2016 break;
2018 case E_Floating_Point_Type:
2019 /* The type of the Low and High bounds can be our type if this is
2020 a type from Standard, so set them at the end of the function. */
2021 gnu_type = make_node (REAL_TYPE);
2022 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2023 layout_type (gnu_type);
2024 break;
2026 case E_Floating_Point_Subtype:
2027 /* See the E_Signed_Integer_Subtype case for the rationale. */
2028 if (!definition
2029 && Present (Ancestor_Subtype (gnat_entity))
2030 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
2031 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
2032 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
2033 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
2035 gnu_type = make_node (REAL_TYPE);
2036 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2037 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2038 TYPE_GCC_MIN_VALUE (gnu_type)
2039 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2040 TYPE_GCC_MAX_VALUE (gnu_type)
2041 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2042 layout_type (gnu_type);
2044 SET_TYPE_RM_MIN_VALUE
2045 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2046 gnat_entity, "L", definition, true,
2047 debug_info_p));
2049 SET_TYPE_RM_MAX_VALUE
2050 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2051 gnat_entity, "U", definition, true,
2052 debug_info_p));
2054 /* Inherit our alias set from what we're a subtype of, as for
2055 integer subtypes. */
2056 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2058 /* One of the above calls might have caused us to be elaborated,
2059 so don't blow up if so. */
2060 maybe_present = true;
2061 break;
2063 /* Array Types and Subtypes
2065 Unconstrained array types are represented by E_Array_Type and
2066 constrained array types are represented by E_Array_Subtype. There
2067 are no actual objects of an unconstrained array type; all we have
2068 are pointers to that type.
2070 The following fields are defined on array types and subtypes:
2072 Component_Type Component type of the array.
2073 Number_Dimensions Number of dimensions (an int).
2074 First_Index Type of first index. */
2076 case E_Array_Type:
2078 const bool convention_fortran_p
2079 = (Convention (gnat_entity) == Convention_Fortran);
2080 const int ndim = Number_Dimensions (gnat_entity);
2081 tree gnu_template_type;
2082 tree gnu_ptr_template;
2083 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2084 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2085 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2086 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2087 Entity_Id gnat_index, gnat_name;
2088 int index;
2089 tree comp_type;
2091 /* Create the type for the component now, as it simplifies breaking
2092 type reference loops. */
2093 comp_type
2094 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2095 if (present_gnu_tree (gnat_entity))
2097 /* As a side effect, the type may have been translated. */
2098 maybe_present = true;
2099 break;
2102 /* We complete an existing dummy fat pointer type in place. This both
2103 avoids further complex adjustments in update_pointer_to and yields
2104 better debugging information in DWARF by leveraging the support for
2105 incomplete declarations of "tagged" types in the DWARF back-end. */
2106 gnu_type = get_dummy_type (gnat_entity);
2107 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2109 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2110 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2111 /* Save the contents of the dummy type for update_pointer_to. */
2112 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2113 gnu_ptr_template =
2114 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2115 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2117 else
2119 gnu_fat_type = make_node (RECORD_TYPE);
2120 gnu_template_type = make_node (RECORD_TYPE);
2121 gnu_ptr_template = build_pointer_type (gnu_template_type);
2124 /* Make a node for the array. If we are not defining the array
2125 suppress expanding incomplete types. */
2126 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2128 if (!definition)
2130 defer_incomplete_level++;
2131 this_deferred = true;
2134 /* Build the fat pointer type. Use a "void *" object instead of
2135 a pointer to the array type since we don't have the array type
2136 yet (it will reference the fat pointer via the bounds). */
2138 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2139 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2140 DECL_CHAIN (tem)
2141 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2142 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2144 if (COMPLETE_TYPE_P (gnu_fat_type))
2146 /* We are going to lay it out again so reset the alias set. */
2147 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2148 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2149 finish_fat_pointer_type (gnu_fat_type, tem);
2150 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2151 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2153 TYPE_FIELDS (t) = tem;
2154 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2157 else
2159 finish_fat_pointer_type (gnu_fat_type, tem);
2160 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2163 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2164 is the fat pointer. This will be used to access the individual
2165 fields once we build them. */
2166 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2167 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2168 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2169 gnu_template_reference
2170 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2171 TREE_READONLY (gnu_template_reference) = 1;
2172 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2174 /* Now create the GCC type for each index and add the fields for that
2175 index to the template. */
2176 for (index = (convention_fortran_p ? ndim - 1 : 0),
2177 gnat_index = First_Index (gnat_entity);
2178 0 <= index && index < ndim;
2179 index += (convention_fortran_p ? - 1 : 1),
2180 gnat_index = Next_Index (gnat_index))
2182 char field_name[16];
2183 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2184 tree gnu_index_base_type
2185 = maybe_character_type (get_base_type (gnu_index_type));
2186 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2187 tree gnu_min, gnu_max, gnu_high;
2189 /* Make the FIELD_DECLs for the low and high bounds of this
2190 type and then make extractions of these fields from the
2191 template. */
2192 sprintf (field_name, "LB%d", index);
2193 gnu_lb_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_lb_field));
2200 field_name[0] = 'U';
2201 gnu_hb_field = create_field_decl (get_identifier (field_name),
2202 gnu_index_base_type,
2203 gnu_template_type, NULL_TREE,
2204 NULL_TREE, 0, 0);
2205 Sloc_to_locus (Sloc (gnat_entity),
2206 &DECL_SOURCE_LOCATION (gnu_hb_field));
2208 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2210 /* We can't use build_component_ref here since the template type
2211 isn't complete yet. */
2212 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2213 gnu_template_reference, gnu_lb_field,
2214 NULL_TREE);
2215 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2216 gnu_template_reference, gnu_hb_field,
2217 NULL_TREE);
2218 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2220 gnu_min = convert (sizetype, gnu_orig_min);
2221 gnu_max = convert (sizetype, gnu_orig_max);
2223 /* Compute the size of this dimension. See the E_Array_Subtype
2224 case below for the rationale. */
2225 gnu_high
2226 = build3 (COND_EXPR, sizetype,
2227 build2 (GE_EXPR, boolean_type_node,
2228 gnu_orig_max, gnu_orig_min),
2229 gnu_max,
2230 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2232 /* Make a range type with the new range in the Ada base type.
2233 Then make an index type with the size range in sizetype. */
2234 gnu_index_types[index]
2235 = create_index_type (gnu_min, gnu_high,
2236 create_range_type (gnu_index_base_type,
2237 gnu_orig_min,
2238 gnu_orig_max),
2239 gnat_entity);
2241 /* Update the maximum size of the array in elements. */
2242 if (gnu_max_size)
2244 tree gnu_min
2245 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2246 tree gnu_max
2247 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2248 tree gnu_this_max
2249 = size_binop (PLUS_EXPR, size_one_node,
2250 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2252 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2253 && TREE_OVERFLOW (gnu_this_max))
2254 gnu_max_size = NULL_TREE;
2255 else
2256 gnu_max_size
2257 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2260 TYPE_NAME (gnu_index_types[index])
2261 = create_concat_name (gnat_entity, field_name);
2264 /* Install all the fields into the template. */
2265 TYPE_NAME (gnu_template_type)
2266 = create_concat_name (gnat_entity, "XUB");
2267 gnu_template_fields = NULL_TREE;
2268 for (index = 0; index < ndim; index++)
2269 gnu_template_fields
2270 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2271 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2272 debug_info_p);
2273 TYPE_READONLY (gnu_template_type) = 1;
2275 /* If Component_Size is not already specified, annotate it with the
2276 size of the component. */
2277 if (Unknown_Component_Size (gnat_entity))
2278 Set_Component_Size (gnat_entity,
2279 annotate_value (TYPE_SIZE (comp_type)));
2281 /* Compute the maximum size of the array in units and bits. */
2282 if (gnu_max_size)
2284 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2285 TYPE_SIZE_UNIT (comp_type));
2286 gnu_max_size = size_binop (MULT_EXPR,
2287 convert (bitsizetype, gnu_max_size),
2288 TYPE_SIZE (comp_type));
2290 else
2291 gnu_max_size_unit = NULL_TREE;
2293 /* Now build the array type. */
2294 tem = comp_type;
2295 for (index = ndim - 1; index >= 0; index--)
2297 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2298 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2299 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2300 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2301 set_reverse_storage_order_on_array_type (tem);
2302 if (array_type_has_nonaliased_component (tem, gnat_entity))
2303 set_nonaliased_component_on_array_type (tem);
2306 /* If an alignment is specified, use it if valid. But ignore it
2307 for the original type of packed array types. If the alignment
2308 was requested with an explicit alignment clause, state so. */
2309 if (No (Packed_Array_Impl_Type (gnat_entity))
2310 && Known_Alignment (gnat_entity))
2312 SET_TYPE_ALIGN (tem,
2313 validate_alignment (Alignment (gnat_entity),
2314 gnat_entity,
2315 TYPE_ALIGN (tem)));
2316 if (Present (Alignment_Clause (gnat_entity)))
2317 TYPE_USER_ALIGN (tem) = 1;
2320 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2321 implementation types as such so that the debug information back-end
2322 can output the appropriate description for them. */
2323 TYPE_PACKED (tem)
2324 = (Is_Packed (gnat_entity)
2325 || Is_Packed_Array_Impl_Type (gnat_entity));
2327 if (Treat_As_Volatile (gnat_entity))
2328 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2330 /* Adjust the type of the pointer-to-array field of the fat pointer
2331 and record the aliasing relationships if necessary. */
2332 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2333 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2334 record_component_aliases (gnu_fat_type);
2336 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2337 corresponding fat pointer. */
2338 TREE_TYPE (gnu_type) = gnu_fat_type;
2339 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2340 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2341 SET_TYPE_MODE (gnu_type, BLKmode);
2342 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2344 /* If the maximum size doesn't overflow, use it. */
2345 if (gnu_max_size
2346 && TREE_CODE (gnu_max_size) == INTEGER_CST
2347 && !TREE_OVERFLOW (gnu_max_size)
2348 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2349 && !TREE_OVERFLOW (gnu_max_size_unit))
2351 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2352 TYPE_SIZE (tem));
2353 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2354 TYPE_SIZE_UNIT (tem));
2357 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2358 artificial_p, debug_info_p, gnat_entity);
2360 /* If told to generate GNAT encodings for them (GDB rely on them at the
2361 moment): give the fat pointer type a name. If this is a packed
2362 array, tell the debugger how to interpret the underlying bits. */
2363 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2364 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2365 else
2366 gnat_name = gnat_entity;
2367 tree xup_name
2368 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2369 ? get_entity_name (gnat_name)
2370 : create_concat_name (gnat_name, "XUP");
2371 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2372 gnat_entity);
2374 /* Create the type to be designated by thin pointers: a record type for
2375 the array and its template. We used to shift the fields to have the
2376 template at a negative offset, but this was somewhat of a kludge; we
2377 now shift thin pointer values explicitly but only those which have a
2378 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2379 Note that GDB can handle standard DWARF information for them, so we
2380 don't have to name them as a GNAT encoding, except if specifically
2381 asked to. */
2382 tree xut_name
2383 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2384 ? get_entity_name (gnat_name)
2385 : create_concat_name (gnat_name, "XUT");
2386 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2387 debug_info_p);
2389 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2390 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2392 break;
2394 case E_Array_Subtype:
2396 /* This is the actual data type for array variables. Multidimensional
2397 arrays are implemented as arrays of arrays. Note that arrays which
2398 have sparse enumeration subtypes as index components create sparse
2399 arrays, which is obviously space inefficient but so much easier to
2400 code for now.
2402 Also note that the subtype never refers to the unconstrained array
2403 type, which is somewhat at variance with Ada semantics.
2405 First check to see if this is simply a renaming of the array type.
2406 If so, the result is the array type. */
2408 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2409 if (!Is_Constrained (gnat_entity))
2411 else
2413 Entity_Id gnat_index, gnat_base_index;
2414 const bool convention_fortran_p
2415 = (Convention (gnat_entity) == Convention_Fortran);
2416 const int ndim = Number_Dimensions (gnat_entity);
2417 tree gnu_base_type = gnu_type;
2418 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2419 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2420 bool need_index_type_struct = false;
2421 int index;
2423 /* First create the GCC type for each index and find out whether
2424 special types are needed for debugging information. */
2425 for (index = (convention_fortran_p ? ndim - 1 : 0),
2426 gnat_index = First_Index (gnat_entity),
2427 gnat_base_index
2428 = First_Index (Implementation_Base_Type (gnat_entity));
2429 0 <= index && index < ndim;
2430 index += (convention_fortran_p ? - 1 : 1),
2431 gnat_index = Next_Index (gnat_index),
2432 gnat_base_index = Next_Index (gnat_base_index))
2434 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2435 tree gnu_index_base_type
2436 = maybe_character_type (get_base_type (gnu_index_type));
2437 tree gnu_orig_min
2438 = convert (gnu_index_base_type,
2439 TYPE_MIN_VALUE (gnu_index_type));
2440 tree gnu_orig_max
2441 = convert (gnu_index_base_type,
2442 TYPE_MAX_VALUE (gnu_index_type));
2443 tree gnu_min = convert (sizetype, gnu_orig_min);
2444 tree gnu_max = convert (sizetype, gnu_orig_max);
2445 tree gnu_base_index_type
2446 = get_unpadded_type (Etype (gnat_base_index));
2447 tree gnu_base_index_base_type
2448 = maybe_character_type (get_base_type (gnu_base_index_type));
2449 tree gnu_base_orig_min
2450 = convert (gnu_base_index_base_type,
2451 TYPE_MIN_VALUE (gnu_base_index_type));
2452 tree gnu_base_orig_max
2453 = convert (gnu_base_index_base_type,
2454 TYPE_MAX_VALUE (gnu_base_index_type));
2455 tree gnu_high;
2457 /* See if the base array type is already flat. If it is, we
2458 are probably compiling an ACATS test but it will cause the
2459 code below to malfunction if we don't handle it specially. */
2460 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2461 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2462 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2464 gnu_min = size_one_node;
2465 gnu_max = size_zero_node;
2466 gnu_high = gnu_max;
2469 /* Similarly, if one of the values overflows in sizetype and the
2470 range is null, use 1..0 for the sizetype bounds. */
2471 else if (TREE_CODE (gnu_min) == INTEGER_CST
2472 && TREE_CODE (gnu_max) == INTEGER_CST
2473 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2474 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2476 gnu_min = size_one_node;
2477 gnu_max = size_zero_node;
2478 gnu_high = gnu_max;
2481 /* If the minimum and maximum values both overflow in sizetype,
2482 but the difference in the original type does not overflow in
2483 sizetype, ignore the overflow indication. */
2484 else if (TREE_CODE (gnu_min) == INTEGER_CST
2485 && TREE_CODE (gnu_max) == INTEGER_CST
2486 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2487 && !TREE_OVERFLOW
2488 (convert (sizetype,
2489 fold_build2 (MINUS_EXPR, gnu_index_type,
2490 gnu_orig_max,
2491 gnu_orig_min))))
2493 TREE_OVERFLOW (gnu_min) = 0;
2494 TREE_OVERFLOW (gnu_max) = 0;
2495 gnu_high = gnu_max;
2498 /* Compute the size of this dimension in the general case. We
2499 need to provide GCC with an upper bound to use but have to
2500 deal with the "superflat" case. There are three ways to do
2501 this. If we can prove that the array can never be superflat,
2502 we can just use the high bound of the index type. */
2503 else if ((Nkind (gnat_index) == N_Range
2504 && cannot_be_superflat (gnat_index))
2505 /* Bit-Packed Array Impl. Types are never superflat. */
2506 || (Is_Packed_Array_Impl_Type (gnat_entity)
2507 && Is_Bit_Packed_Array
2508 (Original_Array_Type (gnat_entity))))
2509 gnu_high = gnu_max;
2511 /* Otherwise, if the high bound is constant but the low bound is
2512 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2513 lower bound. Note that the comparison must be done in the
2514 original type to avoid any overflow during the conversion. */
2515 else if (TREE_CODE (gnu_max) == INTEGER_CST
2516 && TREE_CODE (gnu_min) != INTEGER_CST)
2518 gnu_high = gnu_max;
2519 gnu_min
2520 = build_cond_expr (sizetype,
2521 build_binary_op (GE_EXPR,
2522 boolean_type_node,
2523 gnu_orig_max,
2524 gnu_orig_min),
2525 gnu_min,
2526 int_const_binop (PLUS_EXPR, gnu_max,
2527 size_one_node));
2530 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2531 in all the other cases. Note that, here as well as above,
2532 the condition used in the comparison must be equivalent to
2533 the condition (length != 0). This is relied upon in order
2534 to optimize array comparisons in compare_arrays. Moreover
2535 we use int_const_binop for the shift by 1 if the bound is
2536 constant to avoid any unwanted overflow. */
2537 else
2538 gnu_high
2539 = build_cond_expr (sizetype,
2540 build_binary_op (GE_EXPR,
2541 boolean_type_node,
2542 gnu_orig_max,
2543 gnu_orig_min),
2544 gnu_max,
2545 TREE_CODE (gnu_min) == INTEGER_CST
2546 ? int_const_binop (MINUS_EXPR, gnu_min,
2547 size_one_node)
2548 : size_binop (MINUS_EXPR, gnu_min,
2549 size_one_node));
2551 /* Reuse the index type for the range type. Then make an index
2552 type with the size range in sizetype. */
2553 gnu_index_types[index]
2554 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2555 gnat_entity);
2557 /* Update the maximum size of the array in elements. Here we
2558 see if any constraint on the index type of the base type
2559 can be used in the case of self-referential bound on the
2560 index type of the subtype. We look for a non-"infinite"
2561 and non-self-referential bound from any type involved and
2562 handle each bound separately. */
2563 if (gnu_max_size)
2565 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2566 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2567 tree gnu_base_base_min
2568 = convert (sizetype,
2569 TYPE_MIN_VALUE (gnu_base_index_base_type));
2570 tree gnu_base_base_max
2571 = convert (sizetype,
2572 TYPE_MAX_VALUE (gnu_base_index_base_type));
2574 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2575 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2576 && !TREE_OVERFLOW (gnu_base_min)))
2577 gnu_base_min = gnu_min;
2579 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2580 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2581 && !TREE_OVERFLOW (gnu_base_max)))
2582 gnu_base_max = gnu_max;
2584 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2585 && TREE_OVERFLOW (gnu_base_min))
2586 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2587 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2588 && TREE_OVERFLOW (gnu_base_max))
2589 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2590 gnu_max_size = NULL_TREE;
2591 else
2593 tree gnu_this_max;
2595 /* Use int_const_binop if the bounds are constant to
2596 avoid any unwanted overflow. */
2597 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2598 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2599 gnu_this_max
2600 = int_const_binop (PLUS_EXPR, size_one_node,
2601 int_const_binop (MINUS_EXPR,
2602 gnu_base_max,
2603 gnu_base_min));
2604 else
2605 gnu_this_max
2606 = size_binop (PLUS_EXPR, size_one_node,
2607 size_binop (MINUS_EXPR,
2608 gnu_base_max,
2609 gnu_base_min));
2611 gnu_max_size
2612 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2616 /* We need special types for debugging information to point to
2617 the index types if they have variable bounds, are not integer
2618 types, are biased or are wider than sizetype. These are GNAT
2619 encodings, so we have to include them only when all encodings
2620 are requested. */
2621 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2622 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2623 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2624 || (TREE_TYPE (gnu_index_type)
2625 && TREE_CODE (TREE_TYPE (gnu_index_type))
2626 != INTEGER_TYPE)
2627 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2628 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2629 need_index_type_struct = true;
2632 /* Then flatten: create the array of arrays. For an array type
2633 used to implement a packed array, get the component type from
2634 the original array type since the representation clauses that
2635 can affect it are on the latter. */
2636 if (Is_Packed_Array_Impl_Type (gnat_entity)
2637 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2639 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2640 for (index = ndim - 1; index >= 0; index--)
2641 gnu_type = TREE_TYPE (gnu_type);
2643 /* One of the above calls might have caused us to be elaborated,
2644 so don't blow up if so. */
2645 if (present_gnu_tree (gnat_entity))
2647 maybe_present = true;
2648 break;
2651 else
2653 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2654 debug_info_p);
2656 /* One of the above calls might have caused us to be elaborated,
2657 so don't blow up if so. */
2658 if (present_gnu_tree (gnat_entity))
2660 maybe_present = true;
2661 break;
2665 /* Compute the maximum size of the array in units and bits. */
2666 if (gnu_max_size)
2668 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2669 TYPE_SIZE_UNIT (gnu_type));
2670 gnu_max_size = size_binop (MULT_EXPR,
2671 convert (bitsizetype, gnu_max_size),
2672 TYPE_SIZE (gnu_type));
2674 else
2675 gnu_max_size_unit = NULL_TREE;
2677 /* Now build the array type. */
2678 for (index = ndim - 1; index >= 0; index --)
2680 gnu_type = build_nonshared_array_type (gnu_type,
2681 gnu_index_types[index]);
2682 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2683 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2684 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2685 set_reverse_storage_order_on_array_type (gnu_type);
2686 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2687 set_nonaliased_component_on_array_type (gnu_type);
2690 /* Strip the ___XP suffix for standard DWARF. */
2691 if (Is_Packed_Array_Impl_Type (gnat_entity)
2692 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2694 Entity_Id gnat_original_array_type
2695 = Underlying_Type (Original_Array_Type (gnat_entity));
2697 gnu_entity_name
2698 = get_entity_name (gnat_original_array_type);
2701 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2702 TYPE_STUB_DECL (gnu_type)
2703 = create_type_stub_decl (gnu_entity_name, gnu_type);
2705 /* If this is a multi-dimensional array and we are at global level,
2706 we need to make a variable corresponding to the stride of the
2707 inner dimensions. */
2708 if (ndim > 1 && global_bindings_p ())
2710 tree gnu_arr_type;
2712 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2713 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2714 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2716 tree eltype = TREE_TYPE (gnu_arr_type);
2717 char stride_name[32];
2719 sprintf (stride_name, "ST%d", index);
2720 TYPE_SIZE (gnu_arr_type)
2721 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2722 gnat_entity, stride_name,
2723 definition, false);
2725 /* ??? For now, store the size as a multiple of the
2726 alignment of the element type in bytes so that we
2727 can see the alignment from the tree. */
2728 sprintf (stride_name, "ST%d_A_UNIT", index);
2729 TYPE_SIZE_UNIT (gnu_arr_type)
2730 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2731 gnat_entity, stride_name,
2732 definition, false,
2733 TYPE_ALIGN (eltype));
2735 /* ??? create_type_decl is not invoked on the inner types so
2736 the MULT_EXPR node built above will never be marked. */
2737 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2741 /* If we need to write out a record type giving the names of the
2742 bounds for debugging purposes, do it now and make the record
2743 type a parallel type. This is not needed for a packed array
2744 since the bounds are conveyed by the original array type. */
2745 if (need_index_type_struct
2746 && debug_info_p
2747 && !Is_Packed_Array_Impl_Type (gnat_entity))
2749 tree gnu_bound_rec = make_node (RECORD_TYPE);
2750 tree gnu_field_list = NULL_TREE;
2751 tree gnu_field;
2753 TYPE_NAME (gnu_bound_rec)
2754 = create_concat_name (gnat_entity, "XA");
2756 for (index = ndim - 1; index >= 0; index--)
2758 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2759 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2761 /* Make sure to reference the types themselves, and not just
2762 their names, as the debugger may fall back on them. */
2763 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2764 gnu_bound_rec, NULL_TREE,
2765 NULL_TREE, 0, 0);
2766 DECL_CHAIN (gnu_field) = gnu_field_list;
2767 gnu_field_list = gnu_field;
2770 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2771 add_parallel_type (gnu_type, gnu_bound_rec);
2774 /* If this is a packed array type, make the original array type a
2775 parallel/debug type. Otherwise, if such GNAT encodings are
2776 required, do it for the base array type if it isn't artificial to
2777 make sure it is kept in the debug info. */
2778 if (debug_info_p)
2780 if (Is_Packed_Array_Impl_Type (gnat_entity))
2781 associate_original_type_to_packed_array (gnu_type,
2782 gnat_entity);
2783 else
2785 tree gnu_base_decl
2786 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2787 false);
2788 if (!DECL_ARTIFICIAL (gnu_base_decl)
2789 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2790 add_parallel_type (gnu_type,
2791 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2795 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2796 = (Is_Packed_Array_Impl_Type (gnat_entity)
2797 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2799 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2800 implementation types as such so that the debug information back-end
2801 can output the appropriate description for them. */
2802 TYPE_PACKED (gnu_type)
2803 = (Is_Packed (gnat_entity)
2804 || Is_Packed_Array_Impl_Type (gnat_entity));
2806 /* If the size is self-referential and the maximum size doesn't
2807 overflow, use it. */
2808 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2809 && gnu_max_size
2810 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2811 && TREE_OVERFLOW (gnu_max_size))
2812 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2813 && TREE_OVERFLOW (gnu_max_size_unit)))
2815 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2816 TYPE_SIZE (gnu_type));
2817 TYPE_SIZE_UNIT (gnu_type)
2818 = size_binop (MIN_EXPR, gnu_max_size_unit,
2819 TYPE_SIZE_UNIT (gnu_type));
2822 /* Set our alias set to that of our base type. This gives all
2823 array subtypes the same alias set. */
2824 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2826 /* If this is a packed type, make this type the same as the packed
2827 array type, but do some adjusting in the type first. */
2828 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2830 Entity_Id gnat_index;
2831 tree gnu_inner;
2833 /* First finish the type we had been making so that we output
2834 debugging information for it. */
2835 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2836 if (Treat_As_Volatile (gnat_entity))
2838 const int quals
2839 = TYPE_QUAL_VOLATILE
2840 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2841 gnu_type = change_qualified_type (gnu_type, quals);
2843 /* Make it artificial only if the base type was artificial too.
2844 That's sort of "morally" true and will make it possible for
2845 the debugger to look it up by name in DWARF, which is needed
2846 in order to decode the packed array type. */
2847 gnu_decl
2848 = create_type_decl (gnu_entity_name, gnu_type,
2849 !Comes_From_Source (Etype (gnat_entity))
2850 && artificial_p, debug_info_p,
2851 gnat_entity);
2853 /* Save it as our equivalent in case the call below elaborates
2854 this type again. */
2855 save_gnu_tree (gnat_entity, gnu_decl, false);
2857 gnu_decl
2858 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2859 NULL_TREE, false);
2860 this_made_decl = true;
2861 gnu_type = TREE_TYPE (gnu_decl);
2862 save_gnu_tree (gnat_entity, NULL_TREE, false);
2863 save_gnu_tree (gnat_entity, gnu_decl, false);
2864 saved = true;
2866 gnu_inner = gnu_type;
2867 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2868 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2869 || TYPE_PADDING_P (gnu_inner)))
2870 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2872 /* We need to attach the index type to the type we just made so
2873 that the actual bounds can later be put into a template. */
2874 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2875 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2876 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2877 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2879 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2881 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2882 TYPE_MODULUS for modular types so we make an extra
2883 subtype if necessary. */
2884 if (TYPE_MODULAR_P (gnu_inner))
2886 tree gnu_subtype
2887 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2888 TREE_TYPE (gnu_subtype) = gnu_inner;
2889 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2890 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2891 TYPE_MIN_VALUE (gnu_inner));
2892 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2893 TYPE_MAX_VALUE (gnu_inner));
2894 gnu_inner = gnu_subtype;
2897 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2899 /* Check for other cases of overloading. */
2900 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2903 for (gnat_index = First_Index (gnat_entity);
2904 Present (gnat_index);
2905 gnat_index = Next_Index (gnat_index))
2906 SET_TYPE_ACTUAL_BOUNDS
2907 (gnu_inner,
2908 tree_cons (NULL_TREE,
2909 get_unpadded_type (Etype (gnat_index)),
2910 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2912 if (Convention (gnat_entity) != Convention_Fortran)
2913 SET_TYPE_ACTUAL_BOUNDS
2914 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2916 if (TREE_CODE (gnu_type) == RECORD_TYPE
2917 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2918 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2922 break;
2924 case E_String_Literal_Subtype:
2925 /* Create the type for a string literal. */
2927 Entity_Id gnat_full_type
2928 = (Is_Private_Type (Etype (gnat_entity))
2929 && Present (Full_View (Etype (gnat_entity)))
2930 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2931 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2932 tree gnu_string_array_type
2933 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2934 tree gnu_string_index_type
2935 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2936 (TYPE_DOMAIN (gnu_string_array_type))));
2937 tree gnu_lower_bound
2938 = convert (gnu_string_index_type,
2939 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2940 tree gnu_length
2941 = UI_To_gnu (String_Literal_Length (gnat_entity),
2942 gnu_string_index_type);
2943 tree gnu_upper_bound
2944 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2945 gnu_lower_bound,
2946 int_const_binop (MINUS_EXPR, gnu_length,
2947 convert (gnu_string_index_type,
2948 integer_one_node)));
2949 tree gnu_index_type
2950 = create_index_type (convert (sizetype, gnu_lower_bound),
2951 convert (sizetype, gnu_upper_bound),
2952 create_range_type (gnu_string_index_type,
2953 gnu_lower_bound,
2954 gnu_upper_bound),
2955 gnat_entity);
2957 gnu_type
2958 = build_nonshared_array_type (gnat_to_gnu_type
2959 (Component_Type (gnat_entity)),
2960 gnu_index_type);
2961 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2962 set_nonaliased_component_on_array_type (gnu_type);
2963 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2965 break;
2967 /* Record Types and Subtypes
2969 The following fields are defined on record types:
2971 Has_Discriminants True if the record has discriminants
2972 First_Discriminant Points to head of list of discriminants
2973 First_Entity Points to head of list of fields
2974 Is_Tagged_Type True if the record is tagged
2976 Implementation of Ada records and discriminated records:
2978 A record type definition is transformed into the equivalent of a C
2979 struct definition. The fields that are the discriminants which are
2980 found in the Full_Type_Declaration node and the elements of the
2981 Component_List found in the Record_Type_Definition node. The
2982 Component_List can be a recursive structure since each Variant of
2983 the Variant_Part of the Component_List has a Component_List.
2985 Processing of a record type definition comprises starting the list of
2986 field declarations here from the discriminants and the calling the
2987 function components_to_record to add the rest of the fields from the
2988 component list and return the gnu type node. The function
2989 components_to_record will call itself recursively as it traverses
2990 the tree. */
2992 case E_Record_Type:
2993 if (Has_Complex_Representation (gnat_entity))
2995 gnu_type
2996 = build_complex_type
2997 (get_unpadded_type
2998 (Etype (Defining_Entity
2999 (First (Component_Items
3000 (Component_List
3001 (Type_Definition
3002 (Declaration_Node (gnat_entity)))))))));
3004 break;
3008 Node_Id full_definition = Declaration_Node (gnat_entity);
3009 Node_Id record_definition = Type_Definition (full_definition);
3010 Node_Id gnat_constr;
3011 Entity_Id gnat_field, gnat_parent_type;
3012 tree gnu_field, gnu_field_list = NULL_TREE;
3013 tree gnu_get_parent;
3014 /* Set PACKED in keeping with gnat_to_gnu_field. */
3015 const int packed
3016 = Is_Packed (gnat_entity)
3018 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
3019 ? -1
3020 : 0;
3021 const bool has_align = Known_Alignment (gnat_entity);
3022 const bool has_discr = Has_Discriminants (gnat_entity);
3023 const bool has_rep = Has_Specified_Layout (gnat_entity);
3024 const bool is_extension
3025 = (Is_Tagged_Type (gnat_entity)
3026 && Nkind (record_definition) == N_Derived_Type_Definition);
3027 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
3028 bool all_rep = has_rep;
3030 /* See if all fields have a rep clause. Stop when we find one
3031 that doesn't. */
3032 if (all_rep)
3033 for (gnat_field = First_Entity (gnat_entity);
3034 Present (gnat_field);
3035 gnat_field = Next_Entity (gnat_field))
3036 if ((Ekind (gnat_field) == E_Component
3037 || Ekind (gnat_field) == E_Discriminant)
3038 && No (Component_Clause (gnat_field)))
3040 all_rep = false;
3041 break;
3044 /* If this is a record extension, go a level further to find the
3045 record definition. Also, verify we have a Parent_Subtype. */
3046 if (is_extension)
3048 if (!type_annotate_only
3049 || Present (Record_Extension_Part (record_definition)))
3050 record_definition = Record_Extension_Part (record_definition);
3052 gcc_assert (type_annotate_only
3053 || Present (Parent_Subtype (gnat_entity)));
3056 /* Make a node for the record. If we are not defining the record,
3057 suppress expanding incomplete types. */
3058 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3059 TYPE_NAME (gnu_type) = gnu_entity_name;
3060 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3061 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3062 = Reverse_Storage_Order (gnat_entity);
3063 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3065 if (!definition)
3067 defer_incomplete_level++;
3068 this_deferred = true;
3071 /* If both a size and rep clause were specified, put the size on
3072 the record type now so that it can get the proper layout. */
3073 if (has_rep && Known_RM_Size (gnat_entity))
3074 TYPE_SIZE (gnu_type)
3075 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3077 /* Always set the alignment on the record type here so that it can
3078 get the proper layout. */
3079 if (has_align)
3080 SET_TYPE_ALIGN (gnu_type,
3081 validate_alignment (Alignment (gnat_entity),
3082 gnat_entity, 0));
3083 else
3085 SET_TYPE_ALIGN (gnu_type, 0);
3087 /* If a type needs strict alignment, the minimum size will be the
3088 type size instead of the RM size (see validate_size). Cap the
3089 alignment lest it causes this type size to become too large. */
3090 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3092 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3093 unsigned int max_align = max_size & -max_size;
3094 if (max_align < BIGGEST_ALIGNMENT)
3095 TYPE_MAX_ALIGN (gnu_type) = max_align;
3099 /* If we have a Parent_Subtype, make a field for the parent. If
3100 this record has rep clauses, force the position to zero. */
3101 if (Present (Parent_Subtype (gnat_entity)))
3103 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3104 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3105 tree gnu_parent;
3106 int parent_packed = 0;
3108 /* A major complexity here is that the parent subtype will
3109 reference our discriminants in its Stored_Constraint list.
3110 But those must reference the parent component of this record
3111 which is precisely of the parent subtype we have not built yet!
3112 To break the circle we first build a dummy COMPONENT_REF which
3113 represents the "get to the parent" operation and initialize
3114 each of those discriminants to a COMPONENT_REF of the above
3115 dummy parent referencing the corresponding discriminant of the
3116 base type of the parent subtype. */
3117 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3118 build0 (PLACEHOLDER_EXPR, gnu_type),
3119 build_decl (input_location,
3120 FIELD_DECL, NULL_TREE,
3121 gnu_dummy_parent_type),
3122 NULL_TREE);
3124 if (has_discr)
3125 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3126 Present (gnat_field);
3127 gnat_field = Next_Stored_Discriminant (gnat_field))
3128 if (Present (Corresponding_Discriminant (gnat_field)))
3130 tree gnu_field
3131 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3132 (gnat_field));
3133 save_gnu_tree
3134 (gnat_field,
3135 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3136 gnu_get_parent, gnu_field, NULL_TREE),
3137 true);
3140 /* Then we build the parent subtype. If it has discriminants but
3141 the type itself has unknown discriminants, this means that it
3142 doesn't contain information about how the discriminants are
3143 derived from those of the ancestor type, so it cannot be used
3144 directly. Instead it is built by cloning the parent subtype
3145 of the underlying record view of the type, for which the above
3146 derivation of discriminants has been made explicit. */
3147 if (Has_Discriminants (gnat_parent)
3148 && Has_Unknown_Discriminants (gnat_entity))
3150 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3152 /* If we are defining the type, the underlying record
3153 view must already have been elaborated at this point.
3154 Otherwise do it now as its parent subtype cannot be
3155 technically elaborated on its own. */
3156 if (definition)
3157 gcc_assert (present_gnu_tree (gnat_uview));
3158 else
3159 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3161 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3163 /* Substitute the "get to the parent" of the type for that
3164 of its underlying record view in the cloned type. */
3165 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3166 Present (gnat_field);
3167 gnat_field = Next_Stored_Discriminant (gnat_field))
3168 if (Present (Corresponding_Discriminant (gnat_field)))
3170 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3171 tree gnu_ref
3172 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3173 gnu_get_parent, gnu_field, NULL_TREE);
3174 gnu_parent
3175 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3178 else
3179 gnu_parent = gnat_to_gnu_type (gnat_parent);
3181 /* The parent field needs strict alignment so, if it is to
3182 be created with a component clause below, then we need
3183 to apply the same adjustment as in gnat_to_gnu_field. */
3184 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3186 /* ??? For historical reasons, we do it on strict-alignment
3187 platforms only, where it is really required. This means
3188 that a confirming representation clause will change the
3189 behavior of the compiler on the other platforms. */
3190 if (STRICT_ALIGNMENT)
3191 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3192 else
3193 parent_packed
3194 = adjust_packed (gnu_parent, gnu_type, parent_packed);
3197 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3198 initially built. The discriminants must reference the fields
3199 of the parent subtype and not those of its base type for the
3200 placeholder machinery to properly work. */
3201 if (has_discr)
3203 /* The actual parent subtype is the full view. */
3204 if (Is_Private_Type (gnat_parent))
3206 if (Present (Full_View (gnat_parent)))
3207 gnat_parent = Full_View (gnat_parent);
3208 else
3209 gnat_parent = Underlying_Full_View (gnat_parent);
3212 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3213 Present (gnat_field);
3214 gnat_field = Next_Stored_Discriminant (gnat_field))
3215 if (Present (Corresponding_Discriminant (gnat_field)))
3217 Entity_Id field;
3218 for (field = First_Stored_Discriminant (gnat_parent);
3219 Present (field);
3220 field = Next_Stored_Discriminant (field))
3221 if (same_discriminant_p (gnat_field, field))
3222 break;
3223 gcc_assert (Present (field));
3224 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3225 = gnat_to_gnu_field_decl (field);
3229 /* The "get to the parent" COMPONENT_REF must be given its
3230 proper type... */
3231 TREE_TYPE (gnu_get_parent) = gnu_parent;
3233 /* ...and reference the _Parent field of this record. */
3234 gnu_field
3235 = create_field_decl (parent_name_id,
3236 gnu_parent, gnu_type,
3237 has_rep
3238 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3239 has_rep
3240 ? bitsize_zero_node : NULL_TREE,
3241 parent_packed, 1);
3242 DECL_INTERNAL_P (gnu_field) = 1;
3243 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3244 TYPE_FIELDS (gnu_type) = gnu_field;
3247 /* Make the fields for the discriminants and put them into the record
3248 unless it's an Unchecked_Union. */
3249 if (has_discr)
3250 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3251 Present (gnat_field);
3252 gnat_field = Next_Stored_Discriminant (gnat_field))
3254 /* If this is a record extension and this discriminant is the
3255 renaming of another discriminant, we've handled it above. */
3256 if (is_extension
3257 && Present (Corresponding_Discriminant (gnat_field)))
3258 continue;
3260 gnu_field
3261 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3262 debug_info_p);
3264 /* Make an expression using a PLACEHOLDER_EXPR from the
3265 FIELD_DECL node just created and link that with the
3266 corresponding GNAT defining identifier. */
3267 save_gnu_tree (gnat_field,
3268 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3269 build0 (PLACEHOLDER_EXPR, gnu_type),
3270 gnu_field, NULL_TREE),
3271 true);
3273 if (!is_unchecked_union)
3275 DECL_CHAIN (gnu_field) = gnu_field_list;
3276 gnu_field_list = gnu_field;
3280 /* If we have a derived untagged type that renames discriminants in
3281 the parent type, the (stored) discriminants are just a copy of the
3282 discriminants of the parent type. This means that any constraints
3283 added by the renaming in the derivation are disregarded as far as
3284 the layout of the derived type is concerned. To rescue them, we
3285 change the type of the (stored) discriminants to a subtype with
3286 the bounds of the type of the visible discriminants. */
3287 if (has_discr
3288 && !is_extension
3289 && Stored_Constraint (gnat_entity) != No_Elist)
3290 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3291 gnat_constr != No_Elmt;
3292 gnat_constr = Next_Elmt (gnat_constr))
3293 if (Nkind (Node (gnat_constr)) == N_Identifier
3294 /* Ignore access discriminants. */
3295 && !Is_Access_Type (Etype (Node (gnat_constr)))
3296 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3298 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3299 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3300 tree gnu_ref
3301 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3302 NULL_TREE, false);
3304 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3305 just above for one of the stored discriminants. */
3306 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3308 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3310 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3311 tree gnu_subtype
3312 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3313 ? make_unsigned_type (prec) : make_signed_type (prec);
3314 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3315 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3316 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3317 TYPE_MIN_VALUE (gnu_discr_type));
3318 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3319 TYPE_MAX_VALUE (gnu_discr_type));
3320 TREE_TYPE (gnu_ref)
3321 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3325 /* If this is a derived type with discriminants and these discriminants
3326 affect the initial shape it has inherited, factor them in. But for
3327 an Unchecked_Union (it must be an Itype), just process the type. */
3328 if (has_discr
3329 && !is_extension
3330 && !Has_Record_Rep_Clause (gnat_entity)
3331 && Stored_Constraint (gnat_entity) != No_Elist
3332 && (gnat_parent_type = Underlying_Type (Etype (gnat_entity)))
3333 && Is_Record_Type (gnat_parent_type)
3334 && !Is_Unchecked_Union (gnat_parent_type)
3335 && No_Reordering (gnat_entity) == No_Reordering (gnat_parent_type))
3337 tree gnu_parent_type
3338 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type));
3340 if (TYPE_IS_PADDING_P (gnu_parent_type))
3341 gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type));
3343 vec<subst_pair> gnu_subst_list
3344 = build_subst_list (gnat_entity, gnat_parent_type, definition);
3346 /* Set the layout of the type to match that of the parent type,
3347 doing required substitutions. */
3348 copy_and_substitute_in_layout (gnat_entity, gnat_parent_type,
3349 gnu_type, gnu_parent_type,
3350 gnu_subst_list, debug_info_p);
3352 else
3354 /* Add the fields into the record type and finish it up. */
3355 components_to_record (Component_List (record_definition),
3356 gnat_entity, gnu_field_list, gnu_type,
3357 packed, definition, false, all_rep,
3358 is_unchecked_union, artificial_p,
3359 debug_info_p, false,
3360 all_rep ? NULL_TREE : bitsize_zero_node,
3361 NULL);
3363 /* If there are entities in the chain corresponding to components
3364 that we did not elaborate, ensure we elaborate their types if
3365 they are Itypes. */
3366 for (gnat_temp = First_Entity (gnat_entity);
3367 Present (gnat_temp);
3368 gnat_temp = Next_Entity (gnat_temp))
3369 if ((Ekind (gnat_temp) == E_Component
3370 || Ekind (gnat_temp) == E_Discriminant)
3371 && Is_Itype (Etype (gnat_temp))
3372 && !present_gnu_tree (gnat_temp))
3373 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3376 /* Fill in locations of fields. */
3377 annotate_rep (gnat_entity, gnu_type);
3379 /* If this is a record type associated with an exception definition,
3380 equate its fields to those of the standard exception type. This
3381 will make it possible to convert between them. */
3382 if (gnu_entity_name == exception_data_name_id)
3384 tree gnu_std_field;
3385 for (gnu_field = TYPE_FIELDS (gnu_type),
3386 gnu_std_field = TYPE_FIELDS (except_type_node);
3387 gnu_field;
3388 gnu_field = DECL_CHAIN (gnu_field),
3389 gnu_std_field = DECL_CHAIN (gnu_std_field))
3390 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3391 gcc_assert (!gnu_std_field);
3394 break;
3396 case E_Class_Wide_Subtype:
3397 /* If an equivalent type is present, that is what we should use.
3398 Otherwise, fall through to handle this like a record subtype
3399 since it may have constraints. */
3400 if (gnat_equiv_type != gnat_entity)
3402 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3403 maybe_present = true;
3404 break;
3407 /* ... fall through ... */
3409 case E_Record_Subtype:
3410 /* If Cloned_Subtype is Present it means this record subtype has
3411 identical layout to that type or subtype and we should use
3412 that GCC type for this one. The front end guarantees that
3413 the component list is shared. */
3414 if (Present (Cloned_Subtype (gnat_entity)))
3416 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3417 NULL_TREE, false);
3418 saved = true;
3419 break;
3422 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3423 changing the type, make a new type with each field having the type of
3424 the field in the new subtype but the position computed by transforming
3425 every discriminant reference according to the constraints. We don't
3426 see any difference between private and non-private type here since
3427 derivations from types should have been deferred until the completion
3428 of the private type. */
3429 else
3431 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3433 if (!definition)
3435 defer_incomplete_level++;
3436 this_deferred = true;
3439 tree gnu_base_type
3440 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3442 if (present_gnu_tree (gnat_entity))
3444 maybe_present = true;
3445 break;
3448 /* If this is a record subtype associated with a dispatch table,
3449 strip the suffix. This is necessary to make sure 2 different
3450 subtypes associated with the imported and exported views of a
3451 dispatch table are properly merged in LTO mode. */
3452 if (Is_Dispatch_Table_Entity (gnat_entity))
3454 char *p;
3455 Get_Encoded_Name (gnat_entity);
3456 p = strchr (Name_Buffer, '_');
3457 gcc_assert (p);
3458 strcpy (p+2, "dtS");
3459 gnu_entity_name = get_identifier (Name_Buffer);
3462 /* When the subtype has discriminants and these discriminants affect
3463 the initial shape it has inherited, factor them in. But for an
3464 Unchecked_Union (it must be an Itype), just return the type. */
3465 if (Has_Discriminants (gnat_entity)
3466 && Stored_Constraint (gnat_entity) != No_Elist
3467 && !Is_For_Access_Subtype (gnat_entity)
3468 && Is_Record_Type (gnat_base_type)
3469 && !Is_Unchecked_Union (gnat_base_type))
3471 vec<subst_pair> gnu_subst_list
3472 = build_subst_list (gnat_entity, gnat_base_type, definition);
3473 tree gnu_unpad_base_type;
3475 gnu_type = make_node (RECORD_TYPE);
3476 TYPE_NAME (gnu_type) = gnu_entity_name;
3477 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3478 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3479 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3480 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3481 = Reverse_Storage_Order (gnat_entity);
3482 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3484 /* Set the size, alignment and alias set of the type to match
3485 those of the base type, doing required substitutions. */
3486 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3487 gnu_subst_list);
3489 if (TYPE_IS_PADDING_P (gnu_base_type))
3490 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3491 else
3492 gnu_unpad_base_type = gnu_base_type;
3494 /* Set the layout of the type to match that of the base type,
3495 doing required substitutions. We will output debug info
3496 manually below so pass false as last argument. */
3497 copy_and_substitute_in_layout (gnat_entity, gnat_base_type,
3498 gnu_type, gnu_unpad_base_type,
3499 gnu_subst_list, false);
3501 /* Fill in locations of fields. */
3502 annotate_rep (gnat_entity, gnu_type);
3504 /* If debugging information is being written for the type and if
3505 we are asked to output such encodings, write a record that
3506 shows what we are a subtype of and also make a variable that
3507 indicates our size, if still variable. */
3508 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3510 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3511 tree gnu_unpad_base_name
3512 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3513 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3515 TYPE_NAME (gnu_subtype_marker)
3516 = create_concat_name (gnat_entity, "XVS");
3517 finish_record_type (gnu_subtype_marker,
3518 create_field_decl (gnu_unpad_base_name,
3519 build_reference_type
3520 (gnu_unpad_base_type),
3521 gnu_subtype_marker,
3522 NULL_TREE, NULL_TREE,
3523 0, 0),
3524 0, true);
3526 add_parallel_type (gnu_type, gnu_subtype_marker);
3528 if (definition
3529 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3530 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3531 TYPE_SIZE_UNIT (gnu_subtype_marker)
3532 = create_var_decl (create_concat_name (gnat_entity,
3533 "XVZ"),
3534 NULL_TREE, sizetype, gnu_size_unit,
3535 false, false, false, false, false,
3536 true, debug_info_p,
3537 NULL, gnat_entity);
3541 /* Otherwise, go down all the components in the new type and make
3542 them equivalent to those in the base type. */
3543 else
3545 gnu_type = gnu_base_type;
3547 for (gnat_temp = First_Entity (gnat_entity);
3548 Present (gnat_temp);
3549 gnat_temp = Next_Entity (gnat_temp))
3550 if ((Ekind (gnat_temp) == E_Discriminant
3551 && !Is_Unchecked_Union (gnat_base_type))
3552 || Ekind (gnat_temp) == E_Component)
3553 save_gnu_tree (gnat_temp,
3554 gnat_to_gnu_field_decl
3555 (Original_Record_Component (gnat_temp)),
3556 false);
3559 break;
3561 case E_Access_Subprogram_Type:
3562 case E_Anonymous_Access_Subprogram_Type:
3563 /* Use the special descriptor type for dispatch tables if needed,
3564 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3565 Note that we are only required to do so for static tables in
3566 order to be compatible with the C++ ABI, but Ada 2005 allows
3567 to extend library level tagged types at the local level so
3568 we do it in the non-static case as well. */
3569 if (TARGET_VTABLE_USES_DESCRIPTORS
3570 && Is_Dispatch_Table_Entity (gnat_entity))
3572 gnu_type = fdesc_type_node;
3573 gnu_size = TYPE_SIZE (gnu_type);
3574 break;
3577 /* ... fall through ... */
3579 case E_Allocator_Type:
3580 case E_Access_Type:
3581 case E_Access_Attribute_Type:
3582 case E_Anonymous_Access_Type:
3583 case E_General_Access_Type:
3585 /* The designated type and its equivalent type for gigi. */
3586 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3587 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3588 /* Whether it comes from a limited with. */
3589 const bool is_from_limited_with
3590 = (Is_Incomplete_Type (gnat_desig_equiv)
3591 && From_Limited_With (gnat_desig_equiv));
3592 /* Whether it is a completed Taft Amendment type. Such a type is to
3593 be treated as coming from a limited with clause if it is not in
3594 the main unit, i.e. we break potential circularities here in case
3595 the body of an external unit is loaded for inter-unit inlining. */
3596 const bool is_completed_taft_type
3597 = (Is_Incomplete_Type (gnat_desig_equiv)
3598 && Has_Completion_In_Body (gnat_desig_equiv)
3599 && Present (Full_View (gnat_desig_equiv)));
3600 /* The "full view" of the designated type. If this is an incomplete
3601 entity from a limited with, treat its non-limited view as the full
3602 view. Otherwise, if this is an incomplete or private type, use the
3603 full view. In the former case, we might point to a private type,
3604 in which case, we need its full view. Also, we want to look at the
3605 actual type used for the representation, so this takes a total of
3606 three steps. */
3607 Entity_Id gnat_desig_full_direct_first
3608 = (is_from_limited_with
3609 ? Non_Limited_View (gnat_desig_equiv)
3610 : (Is_Incomplete_Or_Private_Type (gnat_desig_equiv)
3611 ? Full_View (gnat_desig_equiv) : Empty));
3612 Entity_Id gnat_desig_full_direct
3613 = ((is_from_limited_with
3614 && Present (gnat_desig_full_direct_first)
3615 && Is_Private_Type (gnat_desig_full_direct_first))
3616 ? Full_View (gnat_desig_full_direct_first)
3617 : gnat_desig_full_direct_first);
3618 Entity_Id gnat_desig_full
3619 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3620 /* The type actually used to represent the designated type, either
3621 gnat_desig_full or gnat_desig_equiv. */
3622 Entity_Id gnat_desig_rep;
3623 /* We want to know if we'll be seeing the freeze node for any
3624 incomplete type we may be pointing to. */
3625 const bool in_main_unit
3626 = (Present (gnat_desig_full)
3627 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3628 : In_Extended_Main_Code_Unit (gnat_desig_type));
3629 /* True if we make a dummy type here. */
3630 bool made_dummy = false;
3631 /* The mode to be used for the pointer type. */
3632 scalar_int_mode p_mode;
3633 /* The GCC type used for the designated type. */
3634 tree gnu_desig_type = NULL_TREE;
3636 if (!int_mode_for_size (esize, 0).exists (&p_mode)
3637 || !targetm.valid_pointer_mode (p_mode))
3638 p_mode = ptr_mode;
3640 /* If either the designated type or its full view is an unconstrained
3641 array subtype, replace it with the type it's a subtype of. This
3642 avoids problems with multiple copies of unconstrained array types.
3643 Likewise, if the designated type is a subtype of an incomplete
3644 record type, use the parent type to avoid order of elaboration
3645 issues. This can lose some code efficiency, but there is no
3646 alternative. */
3647 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3648 && !Is_Constrained (gnat_desig_equiv))
3649 gnat_desig_equiv = Etype (gnat_desig_equiv);
3650 if (Present (gnat_desig_full)
3651 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3652 && !Is_Constrained (gnat_desig_full))
3653 || (Ekind (gnat_desig_full) == E_Record_Subtype
3654 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3655 gnat_desig_full = Etype (gnat_desig_full);
3657 /* Set the type that's the representation of the designated type. */
3658 gnat_desig_rep
3659 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3661 /* If we already know what the full type is, use it. */
3662 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3663 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3665 /* Get the type of the thing we are to point to and build a pointer to
3666 it. If it is a reference to an incomplete or private type with a
3667 full view that is a record, an array or an access, make a dummy type
3668 and get the actual type later when we have verified it is safe. */
3669 else if ((!in_main_unit
3670 && !present_gnu_tree (gnat_desig_equiv)
3671 && Present (gnat_desig_full)
3672 && (Is_Record_Type (gnat_desig_full)
3673 || Is_Array_Type (gnat_desig_full)
3674 || Is_Access_Type (gnat_desig_full)))
3675 /* Likewise if this is a reference to a record, an array or a
3676 subprogram type and we are to defer elaborating incomplete
3677 types. We do this because this access type may be the full
3678 view of a private type. */
3679 || ((!in_main_unit || imported_p)
3680 && defer_incomplete_level != 0
3681 && !present_gnu_tree (gnat_desig_equiv)
3682 && (Is_Record_Type (gnat_desig_rep)
3683 || Is_Array_Type (gnat_desig_rep)
3684 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3685 /* If this is a reference from a limited_with type back to our
3686 main unit and there's a freeze node for it, either we have
3687 already processed the declaration and made the dummy type,
3688 in which case we just reuse the latter, or we have not yet,
3689 in which case we make the dummy type and it will be reused
3690 when the declaration is finally processed. In both cases,
3691 the pointer eventually created below will be automatically
3692 adjusted when the freeze node is processed. */
3693 || (in_main_unit
3694 && is_from_limited_with
3695 && Present (Freeze_Node (gnat_desig_rep))))
3697 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3698 made_dummy = true;
3701 /* Otherwise handle the case of a pointer to itself. */
3702 else if (gnat_desig_equiv == gnat_entity)
3704 gnu_type
3705 = build_pointer_type_for_mode (void_type_node, p_mode,
3706 No_Strict_Aliasing (gnat_entity));
3707 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3710 /* If expansion is disabled, the equivalent type of a concurrent type
3711 is absent, so we use the void pointer type. */
3712 else if (type_annotate_only && No (gnat_desig_equiv))
3713 gnu_type = ptr_type_node;
3715 /* If the ultimately designated type is an incomplete type with no full
3716 view, we use the void pointer type in LTO mode to avoid emitting a
3717 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3718 the name of the dummy type in used by GDB for a global lookup. */
3719 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3720 && No (Full_View (gnat_desig_rep))
3721 && flag_generate_lto)
3722 gnu_type = ptr_type_node;
3724 /* Finally, handle the default case where we can just elaborate our
3725 designated type. */
3726 else
3727 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3729 /* It is possible that a call to gnat_to_gnu_type above resolved our
3730 type. If so, just return it. */
3731 if (present_gnu_tree (gnat_entity))
3733 maybe_present = true;
3734 break;
3737 /* Access-to-unconstrained-array types need a special treatment. */
3738 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3740 /* If the processing above got something that has a pointer, then
3741 we are done. This could have happened either because the type
3742 was elaborated or because somebody else executed the code. */
3743 if (!TYPE_POINTER_TO (gnu_desig_type))
3744 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3746 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3749 /* If we haven't done it yet, build the pointer type the usual way. */
3750 else if (!gnu_type)
3752 /* Modify the designated type if we are pointing only to constant
3753 objects, but don't do it for a dummy type. */
3754 if (Is_Access_Constant (gnat_entity)
3755 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3756 gnu_desig_type
3757 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3759 gnu_type
3760 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3761 No_Strict_Aliasing (gnat_entity));
3764 /* If the designated type is not declared in the main unit and we made
3765 a dummy node for it, save our definition, elaborate the actual type
3766 and replace the dummy type we made with the actual one. But if we
3767 are to defer actually looking up the actual type, make an entry in
3768 the deferred list instead. If this is from a limited with, we may
3769 have to defer until the end of the current unit. */
3770 if (!in_main_unit && made_dummy)
3772 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3773 gnu_type
3774 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3776 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3777 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3778 artificial_p, debug_info_p,
3779 gnat_entity);
3780 this_made_decl = true;
3781 gnu_type = TREE_TYPE (gnu_decl);
3782 save_gnu_tree (gnat_entity, gnu_decl, false);
3783 saved = true;
3785 if (defer_incomplete_level == 0
3786 && !is_from_limited_with
3787 && !is_completed_taft_type)
3789 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3790 gnat_to_gnu_type (gnat_desig_equiv));
3792 else
3794 struct incomplete *p = XNEW (struct incomplete);
3795 struct incomplete **head
3796 = (is_from_limited_with || is_completed_taft_type
3797 ? &defer_limited_with_list : &defer_incomplete_list);
3799 p->old_type = gnu_desig_type;
3800 p->full_type = gnat_desig_equiv;
3801 p->next = *head;
3802 *head = p;
3806 break;
3808 case E_Access_Protected_Subprogram_Type:
3809 case E_Anonymous_Access_Protected_Subprogram_Type:
3810 /* If we are just annotating types and have no equivalent record type,
3811 just use the void pointer type. */
3812 if (type_annotate_only && gnat_equiv_type == gnat_entity)
3813 gnu_type = ptr_type_node;
3815 /* The run-time representation is the equivalent type. */
3816 else
3818 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3819 maybe_present = true;
3822 /* The designated subtype must be elaborated as well, if it does
3823 not have its own freeze node. */
3824 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3825 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3826 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3827 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3828 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3829 NULL_TREE, false);
3831 break;
3833 case E_Access_Subtype:
3834 /* We treat this as identical to its base type; any constraint is
3835 meaningful only to the front-end. */
3836 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
3837 saved = true;
3839 /* The designated subtype must be elaborated as well, if it does
3840 not have its own freeze node. But designated subtypes created
3841 for constrained components of records with discriminants are
3842 not frozen by the front-end and not elaborated here, because
3843 their use may appear before the base type is frozen and it is
3844 not clear that they are needed in gigi. With the current model,
3845 there is no correct place where they could be elaborated. */
3846 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3847 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3848 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3849 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3851 /* If we are to defer elaborating incomplete types, make a dummy
3852 type node and elaborate it later. */
3853 if (defer_incomplete_level != 0)
3855 struct incomplete *p = XNEW (struct incomplete);
3857 p->old_type
3858 = make_dummy_type (Directly_Designated_Type (gnat_entity));
3859 p->full_type = Directly_Designated_Type (gnat_entity);
3860 p->next = defer_incomplete_list;
3861 defer_incomplete_list = p;
3863 else if (!Is_Incomplete_Or_Private_Type
3864 (Base_Type (Directly_Designated_Type (gnat_entity))))
3865 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3866 NULL_TREE, false);
3868 break;
3870 /* Subprogram Entities
3872 The following access functions are defined for subprograms:
3874 Etype Return type or Standard_Void_Type.
3875 First_Formal The first formal parameter.
3876 Is_Imported Indicates that the subprogram has appeared in
3877 an INTERFACE or IMPORT pragma. For now we
3878 assume that the external language is C.
3879 Is_Exported Likewise but for an EXPORT pragma.
3880 Is_Inlined True if the subprogram is to be inlined.
3882 Each parameter is first checked by calling must_pass_by_ref on its
3883 type to determine if it is passed by reference. For parameters which
3884 are copied in, if they are Ada In Out or Out parameters, their return
3885 value becomes part of a record which becomes the return type of the
3886 function (C function - note that this applies only to Ada procedures
3887 so there is no Ada return type). Additional code to store back the
3888 parameters will be generated on the caller side. This transformation
3889 is done here, not in the front-end.
3891 The intended result of the transformation can be seen from the
3892 equivalent source rewritings that follow:
3894 struct temp {int a,b};
3895 procedure P (A,B: In Out ...) is temp P (int A,B)
3896 begin {
3897 .. ..
3898 end P; return {A,B};
3901 temp t;
3902 P(X,Y); t = P(X,Y);
3903 X = t.a , Y = t.b;
3905 For subprogram types we need to perform mainly the same conversions to
3906 GCC form that are needed for procedures and function declarations. The
3907 only difference is that at the end, we make a type declaration instead
3908 of a function declaration. */
3910 case E_Subprogram_Type:
3911 case E_Function:
3912 case E_Procedure:
3914 tree gnu_ext_name
3915 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
3916 enum inline_status_t inline_status
3917 = Has_Pragma_No_Inline (gnat_entity)
3918 ? is_suppressed
3919 : Has_Pragma_Inline_Always (gnat_entity)
3920 ? is_required
3921 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
3922 bool public_flag = Is_Public (gnat_entity) || imported_p;
3923 /* Subprograms marked both Intrinsic and Always_Inline need not
3924 have a body of their own. */
3925 bool extern_flag
3926 = ((Is_Public (gnat_entity) && !definition)
3927 || imported_p
3928 || (Convention (gnat_entity) == Convention_Intrinsic
3929 && Has_Pragma_Inline_Always (gnat_entity)));
3930 tree gnu_param_list;
3932 /* A parameter may refer to this type, so defer completion of any
3933 incomplete types. */
3934 if (kind == E_Subprogram_Type && !definition)
3936 defer_incomplete_level++;
3937 this_deferred = true;
3940 /* If the subprogram has an alias, it is probably inherited, so
3941 we can use the original one. If the original "subprogram"
3942 is actually an enumeration literal, it may be the first use
3943 of its type, so we must elaborate that type now. */
3944 if (Present (Alias (gnat_entity)))
3946 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
3948 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3949 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
3950 false);
3952 gnu_decl
3953 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
3955 /* Elaborate any Itypes in the parameters of this entity. */
3956 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3957 Present (gnat_temp);
3958 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3959 if (Is_Itype (Etype (gnat_temp)))
3960 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3962 /* Materialize renamed subprograms in the debugging information
3963 when the renamed object is compile time known. We can consider
3964 such renamings as imported declarations.
3966 Because the parameters in generics instantiation are generally
3967 materialized as renamings, we ofter end up having both the
3968 renamed subprogram and the renaming in the same context and with
3969 the same name: in this case, renaming is both useless debug-wise
3970 and potentially harmful as name resolution in the debugger could
3971 return twice the same entity! So avoid this case. */
3972 if (debug_info_p && !artificial_p
3973 && !(get_debug_scope (gnat_entity, NULL)
3974 == get_debug_scope (gnat_renamed, NULL)
3975 && Name_Equals (Chars (gnat_entity),
3976 Chars (gnat_renamed)))
3977 && Present (gnat_renamed)
3978 && (Ekind (gnat_renamed) == E_Function
3979 || Ekind (gnat_renamed) == E_Procedure)
3980 && gnu_decl
3981 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
3983 tree decl = build_decl (input_location, IMPORTED_DECL,
3984 gnu_entity_name, void_type_node);
3985 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
3986 gnat_pushdecl (decl, gnat_entity);
3989 break;
3992 /* Get the GCC tree for the (underlying) subprogram type. If the
3993 entity is an actual subprogram, also get the parameter list. */
3994 gnu_type
3995 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
3996 &gnu_param_list);
3997 if (DECL_P (gnu_type))
3999 gnu_decl = gnu_type;
4000 gnu_type = TREE_TYPE (gnu_decl);
4001 break;
4004 /* Deal with platform-specific calling conventions. */
4005 if (Has_Stdcall_Convention (gnat_entity))
4006 prepend_one_attribute
4007 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4008 get_identifier ("stdcall"), NULL_TREE,
4009 gnat_entity);
4010 else if (Has_Thiscall_Convention (gnat_entity))
4011 prepend_one_attribute
4012 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4013 get_identifier ("thiscall"), NULL_TREE,
4014 gnat_entity);
4016 /* If we should request stack realignment for a foreign convention
4017 subprogram, do so. Note that this applies to task entry points
4018 in particular. */
4019 if (FOREIGN_FORCE_REALIGN_STACK
4020 && Has_Foreign_Convention (gnat_entity))
4021 prepend_one_attribute
4022 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4023 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4024 gnat_entity);
4026 /* Deal with a pragma Linker_Section on a subprogram. */
4027 if ((kind == E_Function || kind == E_Procedure)
4028 && Present (Linker_Section_Pragma (gnat_entity)))
4029 prepend_one_attribute_pragma (&attr_list,
4030 Linker_Section_Pragma (gnat_entity));
4032 /* If we are defining the subprogram and it has an Address clause
4033 we must get the address expression from the saved GCC tree for the
4034 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4035 the address expression here since the front-end has guaranteed
4036 in that case that the elaboration has no effects. If there is
4037 an Address clause and we are not defining the object, just
4038 make it a constant. */
4039 if (Present (Address_Clause (gnat_entity)))
4041 tree gnu_address = NULL_TREE;
4043 if (definition)
4044 gnu_address
4045 = (present_gnu_tree (gnat_entity)
4046 ? get_gnu_tree (gnat_entity)
4047 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4049 save_gnu_tree (gnat_entity, NULL_TREE, false);
4051 /* Convert the type of the object to a reference type that can
4052 alias everything as per RM 13.3(19). */
4053 gnu_type
4054 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4055 if (gnu_address)
4056 gnu_address = convert (gnu_type, gnu_address);
4058 gnu_decl
4059 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4060 gnu_address, false, Is_Public (gnat_entity),
4061 extern_flag, false, false, artificial_p,
4062 debug_info_p, NULL, gnat_entity);
4063 DECL_BY_REF_P (gnu_decl) = 1;
4066 /* If this is a mere subprogram type, just create the declaration. */
4067 else if (kind == E_Subprogram_Type)
4069 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4071 gnu_decl
4072 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4073 debug_info_p, gnat_entity);
4076 /* Otherwise create the subprogram declaration with the external name,
4077 the type and the parameter list. However, if this a reference to
4078 the allocation routines, reuse the canonical declaration nodes as
4079 they come with special properties. */
4080 else
4082 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4083 gnu_decl = malloc_decl;
4084 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4085 gnu_decl = realloc_decl;
4086 else
4088 gnu_decl
4089 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4090 gnu_type, gnu_param_list,
4091 inline_status, public_flag,
4092 extern_flag, artificial_p,
4093 debug_info_p,
4094 definition && imported_p, attr_list,
4095 gnat_entity);
4097 DECL_STUBBED_P (gnu_decl)
4098 = (Convention (gnat_entity) == Convention_Stubbed);
4102 break;
4104 case E_Incomplete_Type:
4105 case E_Incomplete_Subtype:
4106 case E_Private_Type:
4107 case E_Private_Subtype:
4108 case E_Limited_Private_Type:
4109 case E_Limited_Private_Subtype:
4110 case E_Record_Type_With_Private:
4111 case E_Record_Subtype_With_Private:
4113 const bool is_from_limited_with
4114 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4115 /* Get the "full view" of this entity. If this is an incomplete
4116 entity from a limited with, treat its non-limited view as the
4117 full view. Otherwise, use either the full view or the underlying
4118 full view, whichever is present. This is used in all the tests
4119 below. */
4120 const Entity_Id full_view
4121 = is_from_limited_with
4122 ? Non_Limited_View (gnat_entity)
4123 : Present (Full_View (gnat_entity))
4124 ? Full_View (gnat_entity)
4125 : IN (kind, Private_Kind)
4126 ? Underlying_Full_View (gnat_entity)
4127 : Empty;
4129 /* If this is an incomplete type with no full view, it must be a Taft
4130 Amendment type or an incomplete type coming from a limited context,
4131 in which cases we return a dummy type. Otherwise, we just get the
4132 type from its Etype. */
4133 if (No (full_view))
4135 if (kind == E_Incomplete_Type)
4137 gnu_type = make_dummy_type (gnat_entity);
4138 gnu_decl = TYPE_STUB_DECL (gnu_type);
4140 else
4142 gnu_decl
4143 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4144 maybe_present = true;
4148 /* Or else, if we already made a type for the full view, reuse it. */
4149 else if (present_gnu_tree (full_view))
4150 gnu_decl = get_gnu_tree (full_view);
4152 /* Or else, if we are not defining the type or there is no freeze
4153 node on it, get the type for the full view. Likewise if this is
4154 a limited_with'ed type not declared in the main unit, which can
4155 happen for incomplete formal types instantiated on a type coming
4156 from a limited_with clause. */
4157 else if (!definition
4158 || No (Freeze_Node (full_view))
4159 || (is_from_limited_with
4160 && !In_Extended_Main_Code_Unit (full_view)))
4162 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4163 maybe_present = true;
4166 /* Otherwise, make a dummy type entry which will be replaced later.
4167 Save it as the full declaration's type so we can do any needed
4168 updates when we see it. */
4169 else
4171 gnu_type = make_dummy_type (gnat_entity);
4172 gnu_decl = TYPE_STUB_DECL (gnu_type);
4173 if (Has_Completion_In_Body (gnat_entity))
4174 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4175 save_gnu_tree (full_view, gnu_decl, false);
4178 break;
4180 case E_Class_Wide_Type:
4181 /* Class-wide types are always transformed into their root type. */
4182 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4183 maybe_present = true;
4184 break;
4186 case E_Protected_Type:
4187 case E_Protected_Subtype:
4188 case E_Task_Type:
4189 case E_Task_Subtype:
4190 /* If we are just annotating types and have no equivalent record type,
4191 just return void_type, except for root types that have discriminants
4192 because the discriminants will very likely be used in the declarative
4193 part of the associated body so they need to be translated. */
4194 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4196 if (Has_Discriminants (gnat_entity)
4197 && Root_Type (gnat_entity) == gnat_entity)
4199 tree gnu_field_list = NULL_TREE;
4200 Entity_Id gnat_field;
4202 /* This is a minimal version of the E_Record_Type handling. */
4203 gnu_type = make_node (RECORD_TYPE);
4204 TYPE_NAME (gnu_type) = gnu_entity_name;
4206 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4207 Present (gnat_field);
4208 gnat_field = Next_Stored_Discriminant (gnat_field))
4210 tree gnu_field
4211 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4212 definition, debug_info_p);
4214 save_gnu_tree (gnat_field,
4215 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4216 build0 (PLACEHOLDER_EXPR, gnu_type),
4217 gnu_field, NULL_TREE),
4218 true);
4220 DECL_CHAIN (gnu_field) = gnu_field_list;
4221 gnu_field_list = gnu_field;
4224 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4225 false);
4227 else
4228 gnu_type = void_type_node;
4231 /* Concurrent types are always transformed into their record type. */
4232 else
4233 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4234 maybe_present = true;
4235 break;
4237 case E_Label:
4238 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4239 break;
4241 case E_Block:
4242 case E_Loop:
4243 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4244 we've already saved it, so we don't try to. */
4245 gnu_decl = error_mark_node;
4246 saved = true;
4247 break;
4249 case E_Abstract_State:
4250 /* This is a SPARK annotation that only reaches here when compiling in
4251 ASIS mode. */
4252 gcc_assert (type_annotate_only);
4253 gnu_decl = error_mark_node;
4254 saved = true;
4255 break;
4257 default:
4258 gcc_unreachable ();
4261 /* If we had a case where we evaluated another type and it might have
4262 defined this one, handle it here. */
4263 if (maybe_present && present_gnu_tree (gnat_entity))
4265 gnu_decl = get_gnu_tree (gnat_entity);
4266 saved = true;
4269 /* If we are processing a type and there is either no decl for it or
4270 we just made one, do some common processing for the type, such as
4271 handling alignment and possible padding. */
4272 if (is_type && (!gnu_decl || this_made_decl))
4274 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4276 /* Process the attributes, if not already done. Note that the type is
4277 already defined so we cannot pass true for IN_PLACE here. */
4278 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4280 /* ??? Don't set the size for a String_Literal since it is either
4281 confirming or we don't handle it properly (if the low bound is
4282 non-constant). */
4283 if (!gnu_size && kind != E_String_Literal_Subtype)
4285 Uint gnat_size = Known_Esize (gnat_entity)
4286 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4287 gnu_size
4288 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4289 false, Has_Size_Clause (gnat_entity));
4292 /* If a size was specified, see if we can make a new type of that size
4293 by rearranging the type, for example from a fat to a thin pointer. */
4294 if (gnu_size)
4296 gnu_type
4297 = make_type_from_size (gnu_type, gnu_size,
4298 Has_Biased_Representation (gnat_entity));
4300 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4301 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4302 gnu_size = NULL_TREE;
4305 /* If the alignment has not already been processed and this is not
4306 an unconstrained array type, see if an alignment is specified.
4307 If not, we pick a default alignment for atomic objects. */
4308 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4310 else if (Known_Alignment (gnat_entity))
4312 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4313 TYPE_ALIGN (gnu_type));
4315 /* Warn on suspiciously large alignments. This should catch
4316 errors about the (alignment,byte)/(size,bit) discrepancy. */
4317 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4319 tree size;
4321 /* If a size was specified, take it into account. Otherwise
4322 use the RM size for records or unions as the type size has
4323 already been adjusted to the alignment. */
4324 if (gnu_size)
4325 size = gnu_size;
4326 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4327 && !TYPE_FAT_POINTER_P (gnu_type))
4328 size = rm_size (gnu_type);
4329 else
4330 size = TYPE_SIZE (gnu_type);
4332 /* Consider an alignment as suspicious if the alignment/size
4333 ratio is greater or equal to the byte/bit ratio. */
4334 if (tree_fits_uhwi_p (size)
4335 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4336 post_error_ne ("?suspiciously large alignment specified for&",
4337 Expression (Alignment_Clause (gnat_entity)),
4338 gnat_entity);
4341 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4342 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4343 && integer_pow2p (TYPE_SIZE (gnu_type)))
4344 align = MIN (BIGGEST_ALIGNMENT,
4345 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4346 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4347 && tree_fits_uhwi_p (gnu_size)
4348 && integer_pow2p (gnu_size))
4349 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4351 /* See if we need to pad the type. If we did, and made a record,
4352 the name of the new type may be changed. So get it back for
4353 us when we make the new TYPE_DECL below. */
4354 if (gnu_size || align > 0)
4355 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4356 false, !gnu_decl, definition, false);
4358 if (TYPE_IS_PADDING_P (gnu_type))
4359 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4361 /* Now set the RM size of the type. We cannot do it before padding
4362 because we need to accept arbitrary RM sizes on integral types. */
4363 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4365 /* If we are at global level, GCC will have applied variable_size to
4366 the type, but that won't have done anything. So, if it's not
4367 a constant or self-referential, call elaborate_expression_1 to
4368 make a variable for the size rather than calculating it each time.
4369 Handle both the RM size and the actual size. */
4370 if (TYPE_SIZE (gnu_type)
4371 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4372 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4373 && global_bindings_p ())
4375 tree size = TYPE_SIZE (gnu_type);
4377 TYPE_SIZE (gnu_type)
4378 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4379 false);
4381 /* ??? For now, store the size as a multiple of the alignment in
4382 bytes so that we can see the alignment from the tree. */
4383 TYPE_SIZE_UNIT (gnu_type)
4384 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4385 "SIZE_A_UNIT", definition, false,
4386 TYPE_ALIGN (gnu_type));
4388 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4389 may not be marked by the call to create_type_decl below. */
4390 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4392 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4394 tree variant_part = get_variant_part (gnu_type);
4395 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4397 if (variant_part)
4399 tree union_type = TREE_TYPE (variant_part);
4400 tree offset = DECL_FIELD_OFFSET (variant_part);
4402 /* If the position of the variant part is constant, subtract
4403 it from the size of the type of the parent to get the new
4404 size. This manual CSE reduces the data size. */
4405 if (TREE_CODE (offset) == INTEGER_CST)
4407 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4408 TYPE_SIZE (union_type)
4409 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4410 bit_from_pos (offset, bitpos));
4411 TYPE_SIZE_UNIT (union_type)
4412 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4413 byte_from_pos (offset, bitpos));
4415 else
4417 TYPE_SIZE (union_type)
4418 = elaborate_expression_1 (TYPE_SIZE (union_type),
4419 gnat_entity, "VSIZE",
4420 definition, false);
4422 /* ??? For now, store the size as a multiple of the
4423 alignment in bytes so that we can see the alignment
4424 from the tree. */
4425 TYPE_SIZE_UNIT (union_type)
4426 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4427 gnat_entity, "VSIZE_A_UNIT",
4428 definition, false,
4429 TYPE_ALIGN (union_type));
4431 /* ??? For now, store the offset as a multiple of the
4432 alignment in bytes so that we can see the alignment
4433 from the tree. */
4434 DECL_FIELD_OFFSET (variant_part)
4435 = elaborate_expression_2 (offset, gnat_entity,
4436 "VOFFSET", definition, false,
4437 DECL_OFFSET_ALIGN
4438 (variant_part));
4441 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4442 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4445 if (operand_equal_p (ada_size, size, 0))
4446 ada_size = TYPE_SIZE (gnu_type);
4447 else
4448 ada_size
4449 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4450 definition, false);
4451 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4455 /* Similarly, if this is a record type or subtype at global level, call
4456 elaborate_expression_2 on any field position. Skip any fields that
4457 we haven't made trees for to avoid problems with class-wide types. */
4458 if (IN (kind, Record_Kind) && global_bindings_p ())
4459 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4460 gnat_temp = Next_Entity (gnat_temp))
4461 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4463 tree gnu_field = get_gnu_tree (gnat_temp);
4465 /* ??? For now, store the offset as a multiple of the alignment
4466 in bytes so that we can see the alignment from the tree. */
4467 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4468 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4470 DECL_FIELD_OFFSET (gnu_field)
4471 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4472 gnat_temp, "OFFSET", definition,
4473 false,
4474 DECL_OFFSET_ALIGN (gnu_field));
4476 /* ??? The context of gnu_field is not necessarily gnu_type
4477 so the MULT_EXPR node built above may not be marked by
4478 the call to create_type_decl below. */
4479 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4483 if (Is_Atomic_Or_VFA (gnat_entity))
4484 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4486 /* If this is not an unconstrained array type, set some flags. */
4487 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4489 /* Tell the middle-end that objects of tagged types are guaranteed to
4490 be properly aligned. This is necessary because conversions to the
4491 class-wide type are translated into conversions to the root type,
4492 which can be less aligned than some of its derived types. */
4493 if (Is_Tagged_Type (gnat_entity)
4494 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4495 TYPE_ALIGN_OK (gnu_type) = 1;
4497 /* Record whether the type is passed by reference. */
4498 if (Is_By_Reference_Type (gnat_entity) && !VOID_TYPE_P (gnu_type))
4499 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4501 /* Record whether an alignment clause was specified. */
4502 if (Present (Alignment_Clause (gnat_entity)))
4503 TYPE_USER_ALIGN (gnu_type) = 1;
4505 /* Record whether a pragma Universal_Aliasing was specified. */
4506 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4507 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4509 /* If it is passed by reference, force BLKmode to ensure that
4510 objects of this type will always be put in memory. */
4511 if (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type))
4512 SET_TYPE_MODE (gnu_type, BLKmode);
4515 /* If this is a derived type, relate its alias set to that of its parent
4516 to avoid troubles when a call to an inherited primitive is inlined in
4517 a context where a derived object is accessed. The inlined code works
4518 on the parent view so the resulting code may access the same object
4519 using both the parent and the derived alias sets, which thus have to
4520 conflict. As the same issue arises with component references, the
4521 parent alias set also has to conflict with composite types enclosing
4522 derived components. For instance, if we have:
4524 type D is new T;
4525 type R is record
4526 Component : D;
4527 end record;
4529 we want T to conflict with both D and R, in addition to R being a
4530 superset of D by record/component construction.
4532 One way to achieve this is to perform an alias set copy from the
4533 parent to the derived type. This is not quite appropriate, though,
4534 as we don't want separate derived types to conflict with each other:
4536 type I1 is new Integer;
4537 type I2 is new Integer;
4539 We want I1 and I2 to both conflict with Integer but we do not want
4540 I1 to conflict with I2, and an alias set copy on derivation would
4541 have that effect.
4543 The option chosen is to make the alias set of the derived type a
4544 superset of that of its parent type. It trivially fulfills the
4545 simple requirement for the Integer derivation example above, and
4546 the component case as well by superset transitivity:
4548 superset superset
4549 R ----------> D ----------> T
4551 However, for composite types, conversions between derived types are
4552 translated into VIEW_CONVERT_EXPRs so a sequence like:
4554 type Comp1 is new Comp;
4555 type Comp2 is new Comp;
4556 procedure Proc (C : Comp1);
4558 C : Comp2;
4559 Proc (Comp1 (C));
4561 is translated into:
4563 C : Comp2;
4564 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4566 and gimplified into:
4568 C : Comp2;
4569 Comp1 *C.0;
4570 C.0 = (Comp1 *) &C;
4571 Proc (C.0);
4573 i.e. generates code involving type punning. Therefore, Comp1 needs
4574 to conflict with Comp2 and an alias set copy is required.
4576 The language rules ensure the parent type is already frozen here. */
4577 if (kind != E_Subprogram_Type
4578 && Is_Derived_Type (gnat_entity)
4579 && !type_annotate_only)
4581 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4582 /* For constrained packed array subtypes, the implementation type is
4583 used instead of the nominal type. */
4584 if (kind == E_Array_Subtype
4585 && Is_Constrained (gnat_entity)
4586 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4587 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4588 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4589 Is_Composite_Type (gnat_entity)
4590 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4593 if (Treat_As_Volatile (gnat_entity))
4595 const int quals
4596 = TYPE_QUAL_VOLATILE
4597 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4598 gnu_type = change_qualified_type (gnu_type, quals);
4601 if (!gnu_decl)
4602 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4603 artificial_p, debug_info_p,
4604 gnat_entity);
4605 else
4607 TREE_TYPE (gnu_decl) = gnu_type;
4608 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4612 /* If we got a type that is not dummy, back-annotate the alignment of the
4613 type if not already in the tree. Likewise for the size, if any. */
4614 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4616 gnu_type = TREE_TYPE (gnu_decl);
4618 if (Unknown_Alignment (gnat_entity))
4620 unsigned int double_align, align;
4621 bool is_capped_double, align_clause;
4623 /* If the default alignment of "double" or larger scalar types is
4624 specifically capped and this is not an array with an alignment
4625 clause on the component type, return the cap. */
4626 if ((double_align = double_float_alignment) > 0)
4627 is_capped_double
4628 = is_double_float_or_array (gnat_entity, &align_clause);
4629 else if ((double_align = double_scalar_alignment) > 0)
4630 is_capped_double
4631 = is_double_scalar_or_array (gnat_entity, &align_clause);
4632 else
4633 is_capped_double = align_clause = false;
4635 if (is_capped_double && !align_clause)
4636 align = double_align;
4637 else
4638 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4640 Set_Alignment (gnat_entity, UI_From_Int (align));
4643 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4645 tree gnu_size = TYPE_SIZE (gnu_type);
4647 /* If the size is self-referential, annotate the maximum value. */
4648 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4649 gnu_size = max_size (gnu_size, true);
4651 /* If we are just annotating types and the type is tagged, the tag
4652 and the parent components are not generated by the front-end so
4653 alignment and sizes must be adjusted if there is no rep clause. */
4654 if (type_annotate_only
4655 && Is_Tagged_Type (gnat_entity)
4656 && Unknown_RM_Size (gnat_entity)
4657 && !VOID_TYPE_P (gnu_type)
4658 && (!TYPE_FIELDS (gnu_type)
4659 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4661 tree offset;
4663 if (Is_Derived_Type (gnat_entity))
4665 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4666 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4667 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4669 else
4671 unsigned int align
4672 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4673 offset = bitsize_int (POINTER_SIZE);
4674 Set_Alignment (gnat_entity, UI_From_Int (align));
4677 if (TYPE_FIELDS (gnu_type))
4678 offset
4679 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4681 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4682 gnu_size = round_up (gnu_size, POINTER_SIZE);
4683 Uint uint_size = annotate_value (gnu_size);
4684 Set_RM_Size (gnat_entity, uint_size);
4685 Set_Esize (gnat_entity, uint_size);
4688 /* If there is a rep clause, only adjust alignment and Esize. */
4689 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4691 unsigned int align
4692 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4693 Set_Alignment (gnat_entity, UI_From_Int (align));
4694 gnu_size = round_up (gnu_size, POINTER_SIZE);
4695 Set_Esize (gnat_entity, annotate_value (gnu_size));
4698 /* Otherwise no adjustment is needed. */
4699 else
4700 Set_Esize (gnat_entity, annotate_value (gnu_size));
4703 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4704 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4707 /* If we haven't already, associate the ..._DECL node that we just made with
4708 the input GNAT entity node. */
4709 if (!saved)
4710 save_gnu_tree (gnat_entity, gnu_decl, false);
4712 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4713 eliminate as many deferred computations as possible. */
4714 process_deferred_decl_context (false);
4716 /* If this is an enumeration or floating-point type, we were not able to set
4717 the bounds since they refer to the type. These are always static. */
4718 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4719 || (kind == E_Floating_Point_Type))
4721 tree gnu_scalar_type = gnu_type;
4722 tree gnu_low_bound, gnu_high_bound;
4724 /* If this is a padded type, we need to use the underlying type. */
4725 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4726 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4728 /* If this is a floating point type and we haven't set a floating
4729 point type yet, use this in the evaluation of the bounds. */
4730 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4731 longest_float_type_node = gnu_scalar_type;
4733 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4734 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4736 if (kind == E_Enumeration_Type)
4738 /* Enumeration types have specific RM bounds. */
4739 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4740 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4742 else
4744 /* Floating-point types don't have specific RM bounds. */
4745 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4746 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4750 /* If we deferred processing of incomplete types, re-enable it. If there
4751 were no other disables and we have deferred types to process, do so. */
4752 if (this_deferred
4753 && --defer_incomplete_level == 0
4754 && defer_incomplete_list)
4756 struct incomplete *p, *next;
4758 /* We are back to level 0 for the deferring of incomplete types.
4759 But processing these incomplete types below may itself require
4760 deferring, so preserve what we have and restart from scratch. */
4761 p = defer_incomplete_list;
4762 defer_incomplete_list = NULL;
4764 for (; p; p = next)
4766 next = p->next;
4768 if (p->old_type)
4769 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4770 gnat_to_gnu_type (p->full_type));
4771 free (p);
4775 /* If we are not defining this type, see if it's on one of the lists of
4776 incomplete types. If so, handle the list entry now. */
4777 if (is_type && !definition)
4779 struct incomplete *p;
4781 for (p = defer_incomplete_list; p; p = p->next)
4782 if (p->old_type && p->full_type == gnat_entity)
4784 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4785 TREE_TYPE (gnu_decl));
4786 p->old_type = NULL_TREE;
4789 for (p = defer_limited_with_list; p; p = p->next)
4790 if (p->old_type
4791 && (Non_Limited_View (p->full_type) == gnat_entity
4792 || Full_View (p->full_type) == gnat_entity))
4794 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4795 TREE_TYPE (gnu_decl));
4796 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4797 update_profiles_with (p->old_type);
4798 p->old_type = NULL_TREE;
4802 if (this_global)
4803 force_global--;
4805 /* If this is a packed array type whose original array type is itself
4806 an Itype without freeze node, make sure the latter is processed. */
4807 if (Is_Packed_Array_Impl_Type (gnat_entity)
4808 && Is_Itype (Original_Array_Type (gnat_entity))
4809 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4810 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4811 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
4813 return gnu_decl;
4816 /* Similar, but if the returned value is a COMPONENT_REF, return the
4817 FIELD_DECL. */
4819 tree
4820 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4822 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4824 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4825 gnu_field = TREE_OPERAND (gnu_field, 1);
4827 return gnu_field;
4830 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4831 the GCC type corresponding to that entity. */
4833 tree
4834 gnat_to_gnu_type (Entity_Id gnat_entity)
4836 tree gnu_decl;
4838 /* The back end never attempts to annotate generic types. */
4839 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4840 return void_type_node;
4842 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4843 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4845 return TREE_TYPE (gnu_decl);
4848 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4849 the unpadded version of the GCC type corresponding to that entity. */
4851 tree
4852 get_unpadded_type (Entity_Id gnat_entity)
4854 tree type = gnat_to_gnu_type (gnat_entity);
4856 if (TYPE_IS_PADDING_P (type))
4857 type = TREE_TYPE (TYPE_FIELDS (type));
4859 return type;
4862 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
4863 a C++ imported method or equivalent.
4865 We use the predicate on 32-bit x86/Windows to find out whether we need to
4866 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
4867 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
4869 bool
4870 is_cplusplus_method (Entity_Id gnat_entity)
4872 /* A constructor is a method on the C++ side. We deal with it now because
4873 it is declared without the 'this' parameter in the sources and, although
4874 the front-end will create a version with the 'this' parameter for code
4875 generation purposes, we want to return true for both versions. */
4876 if (Is_Constructor (gnat_entity))
4877 return true;
4879 /* Check that the subprogram has C++ convention. */
4880 if (Convention (gnat_entity) != Convention_CPP)
4881 return false;
4883 /* And that the type of the first parameter (indirectly) has it too. */
4884 Entity_Id gnat_first = First_Formal (gnat_entity);
4885 if (No (gnat_first))
4886 return false;
4888 Entity_Id gnat_type = Etype (gnat_first);
4889 if (Is_Access_Type (gnat_type))
4890 gnat_type = Directly_Designated_Type (gnat_type);
4891 if (Convention (gnat_type) != Convention_CPP)
4892 return false;
4894 /* This is the main case: a C++ virtual method imported as a primitive
4895 operation of a tagged type. */
4896 if (Is_Dispatching_Operation (gnat_entity))
4897 return true;
4899 /* This is set on the E_Subprogram_Type built for a dispatching call. */
4900 if (Is_Dispatch_Table_Entity (gnat_entity))
4901 return true;
4903 /* A thunk needs to be handled like its associated primitive operation. */
4904 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
4905 return true;
4907 /* Now on to the annoying case: a C++ non-virtual method, imported either
4908 as a non-primitive operation of a tagged type or as a primitive operation
4909 of an untagged type. We cannot reliably differentiate these cases from
4910 their static member or regular function equivalents in Ada, so we ask
4911 the C++ side through the mangled name of the function, as the implicit
4912 'this' parameter is not encoded in the mangled name of a method. */
4913 if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity)))
4915 String_Pointer sp = { NULL, NULL };
4916 Get_External_Name (gnat_entity, false, sp);
4918 void *mem;
4919 struct demangle_component *cmp
4920 = cplus_demangle_v3_components (Name_Buffer,
4921 DMGL_GNU_V3
4922 | DMGL_TYPES
4923 | DMGL_PARAMS
4924 | DMGL_RET_DROP,
4925 &mem);
4926 if (!cmp)
4927 return false;
4929 /* We need to release MEM once we have a successful demangling. */
4930 bool ret = false;
4932 if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME
4933 && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE
4934 && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL
4935 && cmp->type == DEMANGLE_COMPONENT_ARGLIST)
4937 /* Make sure there is at least one parameter in C++ too. */
4938 if (cmp->u.s_binary.left)
4940 unsigned int n_ada_args = 0;
4941 do {
4942 n_ada_args++;
4943 gnat_first = Next_Formal (gnat_first);
4944 } while (Present (gnat_first));
4946 unsigned int n_cpp_args = 0;
4947 do {
4948 n_cpp_args++;
4949 cmp = cmp->u.s_binary.right;
4950 } while (cmp);
4952 if (n_cpp_args < n_ada_args)
4953 ret = true;
4955 else
4956 ret = true;
4959 free (mem);
4961 return ret;
4964 return false;
4967 /* Finalize the processing of From_Limited_With incomplete types. */
4969 void
4970 finalize_from_limited_with (void)
4972 struct incomplete *p, *next;
4974 p = defer_limited_with_list;
4975 defer_limited_with_list = NULL;
4977 for (; p; p = next)
4979 next = p->next;
4981 if (p->old_type)
4983 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4984 gnat_to_gnu_type (p->full_type));
4985 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4986 update_profiles_with (p->old_type);
4989 free (p);
4993 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind
4994 of type (such E_Task_Type) that has a different type which Gigi uses
4995 for its representation. If the type does not have a special type for
4996 its representation, return GNAT_ENTITY. */
4998 Entity_Id
4999 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5001 Entity_Id gnat_equiv = gnat_entity;
5003 if (No (gnat_entity))
5004 return gnat_entity;
5006 switch (Ekind (gnat_entity))
5008 case E_Class_Wide_Subtype:
5009 if (Present (Equivalent_Type (gnat_entity)))
5010 gnat_equiv = Equivalent_Type (gnat_entity);
5011 break;
5013 case E_Access_Protected_Subprogram_Type:
5014 case E_Anonymous_Access_Protected_Subprogram_Type:
5015 if (Present (Equivalent_Type (gnat_entity)))
5016 gnat_equiv = Equivalent_Type (gnat_entity);
5017 break;
5019 case E_Class_Wide_Type:
5020 gnat_equiv = Root_Type (gnat_entity);
5021 break;
5023 case E_Protected_Type:
5024 case E_Protected_Subtype:
5025 case E_Task_Type:
5026 case E_Task_Subtype:
5027 if (Present (Corresponding_Record_Type (gnat_entity)))
5028 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5029 break;
5031 default:
5032 break;
5035 return gnat_equiv;
5038 /* Return a GCC tree for a type corresponding to the component type of the
5039 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5040 is for an array being defined. DEBUG_INFO_P is true if we need to write
5041 debug information for other types that we may create in the process. */
5043 static tree
5044 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5045 bool debug_info_p)
5047 const Entity_Id gnat_type = Component_Type (gnat_array);
5048 tree gnu_type = gnat_to_gnu_type (gnat_type);
5049 tree gnu_comp_size;
5050 unsigned int max_align;
5052 /* If an alignment is specified, use it as a cap on the component type
5053 so that it can be honored for the whole type. But ignore it for the
5054 original type of packed array types. */
5055 if (No (Packed_Array_Impl_Type (gnat_array))
5056 && Known_Alignment (gnat_array))
5057 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5058 else
5059 max_align = 0;
5061 /* Try to get a smaller form of the component if needed. */
5062 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5063 && !Is_Bit_Packed_Array (gnat_array)
5064 && !Has_Aliased_Components (gnat_array)
5065 && !Strict_Alignment (gnat_type)
5066 && RECORD_OR_UNION_TYPE_P (gnu_type)
5067 && !TYPE_FAT_POINTER_P (gnu_type)
5068 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5069 gnu_type = make_packable_type (gnu_type, false, max_align);
5071 if (Has_Atomic_Components (gnat_array))
5072 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5074 /* Get and validate any specified Component_Size. */
5075 gnu_comp_size
5076 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5077 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5078 true, Has_Component_Size_Clause (gnat_array));
5080 /* If the array has aliased components and the component size can be zero,
5081 force at least unit size to ensure that the components have distinct
5082 addresses. */
5083 if (!gnu_comp_size
5084 && Has_Aliased_Components (gnat_array)
5085 && (integer_zerop (TYPE_SIZE (gnu_type))
5086 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5087 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5088 gnu_comp_size
5089 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5091 /* If the component type is a RECORD_TYPE that has a self-referential size,
5092 then use the maximum size for the component size. */
5093 if (!gnu_comp_size
5094 && TREE_CODE (gnu_type) == RECORD_TYPE
5095 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5096 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5098 /* Honor the component size. This is not needed for bit-packed arrays. */
5099 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5101 tree orig_type = gnu_type;
5103 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5104 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5105 gnu_type = orig_type;
5106 else
5107 orig_type = gnu_type;
5109 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5110 true, false, definition, true);
5112 /* If a padding record was made, declare it now since it will never be
5113 declared otherwise. This is necessary to ensure that its subtrees
5114 are properly marked. */
5115 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5116 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5117 gnat_array);
5120 /* If the component type is a padded type made for a non-bit-packed array
5121 of scalars with reverse storage order, we need to propagate the reverse
5122 storage order to the padding type since it is the innermost enclosing
5123 aggregate type around the scalar. */
5124 if (TYPE_IS_PADDING_P (gnu_type)
5125 && Reverse_Storage_Order (gnat_array)
5126 && !Is_Bit_Packed_Array (gnat_array)
5127 && Is_Scalar_Type (gnat_type))
5128 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5130 if (Has_Volatile_Components (gnat_array))
5132 const int quals
5133 = TYPE_QUAL_VOLATILE
5134 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5135 gnu_type = change_qualified_type (gnu_type, quals);
5138 return gnu_type;
5141 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5142 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5143 the type of the parameter. FIRST is true if this is the first parameter in
5144 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5145 the copy-in copy-out implementation mechanism.
5147 The returned tree is a PARM_DECL, except for the cases where no parameter
5148 needs to be actually passed to the subprogram; the type of this "shadow"
5149 parameter is then returned instead. */
5151 static tree
5152 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5153 Entity_Id gnat_subprog, bool *cico)
5155 Entity_Id gnat_param_type = Etype (gnat_param);
5156 Mechanism_Type mech = Mechanism (gnat_param);
5157 tree gnu_param_name = get_entity_name (gnat_param);
5158 bool foreign = Has_Foreign_Convention (gnat_subprog);
5159 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5160 /* The parameter can be indirectly modified if its address is taken. */
5161 bool ro_param = in_param && !Address_Taken (gnat_param);
5162 bool by_return = false, by_component_ptr = false;
5163 bool by_ref = false;
5164 bool restricted_aliasing_p = false;
5165 location_t saved_location = input_location;
5166 tree gnu_param;
5168 /* Make sure to use the proper SLOC for vector ABI warnings. */
5169 if (VECTOR_TYPE_P (gnu_param_type))
5170 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5172 /* Builtins are expanded inline and there is no real call sequence involved.
5173 So the type expected by the underlying expander is always the type of the
5174 argument "as is". */
5175 if (Convention (gnat_subprog) == Convention_Intrinsic
5176 && Present (Interface_Name (gnat_subprog)))
5177 mech = By_Copy;
5179 /* Handle the first parameter of a valued procedure specially: it's a copy
5180 mechanism for which the parameter is never allocated. */
5181 else if (first && Is_Valued_Procedure (gnat_subprog))
5183 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5184 mech = By_Copy;
5185 by_return = true;
5188 /* Or else, see if a Mechanism was supplied that forced this parameter
5189 to be passed one way or another. */
5190 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5193 /* Positive mechanism means by copy for sufficiently small parameters. */
5194 else if (mech > 0)
5196 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5197 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5198 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5199 mech = By_Reference;
5200 else
5201 mech = By_Copy;
5204 /* Otherwise, it's an unsupported mechanism so error out. */
5205 else
5207 post_error ("unsupported mechanism for&", gnat_param);
5208 mech = Default;
5211 /* If this is either a foreign function or if the underlying type won't
5212 be passed by reference and is as aligned as the original type, strip
5213 off possible padding type. */
5214 if (TYPE_IS_PADDING_P (gnu_param_type))
5216 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5218 if (foreign
5219 || (!must_pass_by_ref (unpadded_type)
5220 && mech != By_Reference
5221 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5222 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5223 gnu_param_type = unpadded_type;
5226 /* If this is a read-only parameter, make a variant of the type that is
5227 read-only. ??? However, if this is a self-referential type, the type
5228 can be very complex, so skip it for now. */
5229 if (ro_param && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5230 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5232 /* For foreign conventions, pass arrays as pointers to the element type.
5233 First check for unconstrained array and get the underlying array. */
5234 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5235 gnu_param_type
5236 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5238 /* Arrays are passed as pointers to element type for foreign conventions. */
5239 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5241 /* Strip off any multi-dimensional entries, then strip
5242 off the last array to get the component type. */
5243 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5244 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5245 gnu_param_type = TREE_TYPE (gnu_param_type);
5247 by_component_ptr = true;
5248 gnu_param_type = TREE_TYPE (gnu_param_type);
5250 if (ro_param)
5251 gnu_param_type
5252 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5254 gnu_param_type = build_pointer_type (gnu_param_type);
5257 /* Fat pointers are passed as thin pointers for foreign conventions. */
5258 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5259 gnu_param_type
5260 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5262 /* If we were requested or muss pass by reference, do so.
5263 If we were requested to pass by copy, do so.
5264 Otherwise, for foreign conventions, pass In Out or Out parameters
5265 or aggregates by reference. For COBOL and Fortran, pass all
5266 integer and FP types that way too. For Convention Ada, use
5267 the standard Ada default. */
5268 else if (mech == By_Reference
5269 || must_pass_by_ref (gnu_param_type)
5270 || (mech != By_Copy
5271 && ((foreign
5272 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5273 || (foreign
5274 && (Convention (gnat_subprog) == Convention_Fortran
5275 || Convention (gnat_subprog) == Convention_COBOL)
5276 && (INTEGRAL_TYPE_P (gnu_param_type)
5277 || FLOAT_TYPE_P (gnu_param_type)))
5278 || (!foreign
5279 && default_pass_by_ref (gnu_param_type)))))
5281 /* We take advantage of 6.2(12) by considering that references built for
5282 parameters whose type isn't by-ref and for which the mechanism hasn't
5283 been forced to by-ref allow only a restricted form of aliasing. */
5284 restricted_aliasing_p
5285 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5286 gnu_param_type = build_reference_type (gnu_param_type);
5287 by_ref = true;
5290 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5291 else if (!in_param)
5292 *cico = true;
5294 input_location = saved_location;
5296 if (mech == By_Copy && (by_ref || by_component_ptr))
5297 post_error ("?cannot pass & by copy", gnat_param);
5299 /* If this is an Out parameter that isn't passed by reference and isn't
5300 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5301 it will be a VAR_DECL created when we process the procedure, so just
5302 return its type. For the special parameter of a valued procedure,
5303 never pass it in.
5305 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5306 Out parameters with discriminants or implicit initial values to be
5307 handled like In Out parameters. These type are normally built as
5308 aggregates, hence passed by reference, except for some packed arrays
5309 which end up encoded in special integer types. Note that scalars can
5310 be given implicit initial values using the Default_Value aspect.
5312 The exception we need to make is then for packed arrays of records
5313 with discriminants or implicit initial values. We have no light/easy
5314 way to check for the latter case, so we merely check for packed arrays
5315 of records. This may lead to useless copy-in operations, but in very
5316 rare cases only, as these would be exceptions in a set of already
5317 exceptional situations. */
5318 if (Ekind (gnat_param) == E_Out_Parameter
5319 && !by_ref
5320 && (by_return
5321 || (!POINTER_TYPE_P (gnu_param_type)
5322 && !AGGREGATE_TYPE_P (gnu_param_type)
5323 && !Has_Default_Aspect (gnat_param_type)))
5324 && !(Is_Array_Type (gnat_param_type)
5325 && Is_Packed (gnat_param_type)
5326 && Is_Composite_Type (Component_Type (gnat_param_type))))
5327 return gnu_param_type;
5329 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5330 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5331 DECL_BY_REF_P (gnu_param) = by_ref;
5332 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5333 DECL_POINTS_TO_READONLY_P (gnu_param)
5334 = (ro_param && (by_ref || by_component_ptr));
5335 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5336 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5337 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5339 /* If no Mechanism was specified, indicate what we're using, then
5340 back-annotate it. */
5341 if (mech == Default)
5342 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5344 Set_Mechanism (gnat_param, mech);
5345 return gnu_param;
5348 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5349 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5351 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5352 the corresponding profile, which means that, by the time the freeze node
5353 of the subprogram is encountered, types involved in its profile may still
5354 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5355 the freeze node of types involved in its profile, either types of formal
5356 parameters or the return type. */
5358 static void
5359 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5361 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5363 struct tree_entity_vec_map in;
5364 in.base.from = gnu_type;
5365 struct tree_entity_vec_map **slot
5366 = dummy_to_subprog_map->find_slot (&in, INSERT);
5367 if (!*slot)
5369 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5370 e->base.from = gnu_type;
5371 e->to = NULL;
5372 *slot = e;
5375 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5376 because the vector might have been just emptied by update_profiles_with.
5377 This can happen when there are 2 freeze nodes associated with different
5378 views of the same type; the type will be really complete only after the
5379 second freeze node is encountered. */
5380 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5382 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5384 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5385 since this would mean updating twice its profile. */
5386 if (v)
5388 const unsigned len = v->length ();
5389 unsigned int l = 0, u = len;
5391 /* Entity_Id is a simple integer so we can implement a stable order on
5392 the vector with an ordered insertion scheme and binary search. */
5393 while (l < u)
5395 unsigned int m = (l + u) / 2;
5396 int diff = (int) (*v)[m] - (int) gnat_subprog;
5397 if (diff > 0)
5398 u = m;
5399 else if (diff < 0)
5400 l = m + 1;
5401 else
5402 return;
5405 /* l == u and therefore is the insertion point. */
5406 vec_safe_insert (v, l, gnat_subprog);
5408 else
5409 vec_safe_push (v, gnat_subprog);
5411 (*slot)->to = v;
5414 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5416 static void
5417 update_profile (Entity_Id gnat_subprog)
5419 tree gnu_param_list;
5420 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5421 Needs_Debug_Info (gnat_subprog),
5422 &gnu_param_list);
5423 if (DECL_P (gnu_type))
5425 /* Builtins cannot have their address taken so we can reset them. */
5426 gcc_assert (DECL_BUILT_IN (gnu_type));
5427 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5428 save_gnu_tree (gnat_subprog, gnu_type, false);
5429 return;
5432 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5434 TREE_TYPE (gnu_subprog) = gnu_type;
5436 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5437 and needs to be adjusted too. */
5438 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5440 tree gnu_entity_name = get_entity_name (gnat_subprog);
5441 tree gnu_ext_name
5442 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5444 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5445 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5449 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5450 a dummy type which appears in profiles. */
5452 void
5453 update_profiles_with (tree gnu_type)
5455 struct tree_entity_vec_map in;
5456 in.base.from = gnu_type;
5457 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5458 gcc_assert (e);
5459 vec<Entity_Id, va_gc_atomic> *v = e->to;
5460 e->to = NULL;
5462 /* The flag needs to be reset before calling update_profile, in case
5463 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5464 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5466 unsigned int i;
5467 Entity_Id *iter;
5468 FOR_EACH_VEC_ELT (*v, i, iter)
5469 update_profile (*iter);
5471 vec_free (v);
5474 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5476 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5477 context may now appear as parameter and result types. As a consequence,
5478 we may need to defer their translation until after a freeze node is seen
5479 or to the end of the current unit. We also aim at handling temporarily
5480 incomplete types created by the usual delayed elaboration scheme. */
5482 static tree
5483 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5485 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5486 so the rationale is exposed in that place. These processings probably
5487 ought to be merged at some point. */
5488 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5489 const bool is_from_limited_with
5490 = (Is_Incomplete_Type (gnat_equiv)
5491 && From_Limited_With (gnat_equiv));
5492 Entity_Id gnat_full_direct_first
5493 = (is_from_limited_with
5494 ? Non_Limited_View (gnat_equiv)
5495 : (Is_Incomplete_Or_Private_Type (gnat_equiv)
5496 ? Full_View (gnat_equiv) : Empty));
5497 Entity_Id gnat_full_direct
5498 = ((is_from_limited_with
5499 && Present (gnat_full_direct_first)
5500 && Is_Private_Type (gnat_full_direct_first))
5501 ? Full_View (gnat_full_direct_first)
5502 : gnat_full_direct_first);
5503 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5504 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5505 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5506 tree gnu_type;
5508 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5509 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5511 else if (is_from_limited_with
5512 && ((!in_main_unit
5513 && !present_gnu_tree (gnat_equiv)
5514 && Present (gnat_full)
5515 && (Is_Record_Type (gnat_full)
5516 || Is_Array_Type (gnat_full)
5517 || Is_Access_Type (gnat_full)))
5518 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5520 gnu_type = make_dummy_type (gnat_equiv);
5522 if (!in_main_unit)
5524 struct incomplete *p = XNEW (struct incomplete);
5526 p->old_type = gnu_type;
5527 p->full_type = gnat_equiv;
5528 p->next = defer_limited_with_list;
5529 defer_limited_with_list = p;
5533 else if (type_annotate_only && No (gnat_equiv))
5534 gnu_type = void_type_node;
5536 else
5537 gnu_type = gnat_to_gnu_type (gnat_equiv);
5539 /* Access-to-unconstrained-array types need a special treatment. */
5540 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5542 if (!TYPE_POINTER_TO (gnu_type))
5543 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5546 return gnu_type;
5549 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5550 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5551 is true if we need to write debug information for other types that we may
5552 create in the process. Also set PARAM_LIST to the list of parameters.
5553 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5554 directly instead of its type. */
5556 static tree
5557 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5558 bool debug_info_p, tree *param_list)
5560 const Entity_Kind kind = Ekind (gnat_subprog);
5561 Entity_Id gnat_return_type = Etype (gnat_subprog);
5562 Entity_Id gnat_param;
5563 tree gnu_type = present_gnu_tree (gnat_subprog)
5564 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5565 tree gnu_return_type;
5566 tree gnu_param_type_list = NULL_TREE;
5567 tree gnu_param_list = NULL_TREE;
5568 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5569 (In Out or Out parameters not passed by reference), in which case it is
5570 the list of nodes used to specify the values of the In Out/Out parameters
5571 that are returned as a record upon procedure return. The TREE_PURPOSE of
5572 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5573 is the PARM_DECL corresponding to that field. This list will be saved in
5574 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5575 tree gnu_cico_list = NULL_TREE;
5576 tree gnu_cico_return_type = NULL_TREE;
5577 /* Fields in return type of procedure with copy-in copy-out parameters. */
5578 tree gnu_field_list = NULL_TREE;
5579 /* The semantics of "pure" in Ada essentially matches that of "const"
5580 in the back-end. In particular, both properties are orthogonal to
5581 the "nothrow" property if the EH circuitry is explicit in the
5582 internal representation of the back-end. If we are to completely
5583 hide the EH circuitry from it, we need to declare that calls to pure
5584 Ada subprograms that can throw have side effects since they can
5585 trigger an "abnormal" transfer of control flow; thus they can be
5586 neither "const" nor "pure" in the back-end sense. */
5587 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5588 bool return_by_direct_ref_p = false;
5589 bool return_by_invisi_ref_p = false;
5590 bool return_unconstrained_p = false;
5591 bool incomplete_profile_p = false;
5592 unsigned int num;
5594 /* Look into the return type and get its associated GCC tree if it is not
5595 void, and then compute various flags for the subprogram type. But make
5596 sure not to do this processing multiple times. */
5597 if (Ekind (gnat_return_type) == E_Void)
5598 gnu_return_type = void_type_node;
5600 else if (gnu_type
5601 && TREE_CODE (gnu_type) == FUNCTION_TYPE
5602 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5604 gnu_return_type = TREE_TYPE (gnu_type);
5605 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5606 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5607 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5610 else
5612 /* For foreign convention subprograms, return System.Address as void *
5613 or equivalent. Note that this comprises GCC builtins. */
5614 if (Has_Foreign_Convention (gnat_subprog)
5615 && Is_Descendant_Of_Address (gnat_return_type))
5616 gnu_return_type = ptr_type_node;
5617 else
5618 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5620 /* If this function returns by reference, make the actual return type
5621 the reference type and make a note of that. */
5622 if (Returns_By_Ref (gnat_subprog))
5624 gnu_return_type = build_reference_type (gnu_return_type);
5625 return_by_direct_ref_p = true;
5628 /* If the return type is an unconstrained array type, the return value
5629 will be allocated on the secondary stack so the actual return type
5630 is the fat pointer type. */
5631 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5633 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5634 return_unconstrained_p = true;
5637 /* This is the same unconstrained array case, but for a dummy type. */
5638 else if (TYPE_REFERENCE_TO (gnu_return_type)
5639 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5641 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5642 return_unconstrained_p = true;
5645 /* Likewise, if the return type requires a transient scope, the return
5646 value will also be allocated on the secondary stack so the actual
5647 return type is the reference type. */
5648 else if (Requires_Transient_Scope (gnat_return_type))
5650 gnu_return_type = build_reference_type (gnu_return_type);
5651 return_unconstrained_p = true;
5654 /* If the Mechanism is By_Reference, ensure this function uses the
5655 target's by-invisible-reference mechanism, which may not be the
5656 same as above (e.g. it might be passing an extra parameter). */
5657 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5658 return_by_invisi_ref_p = true;
5660 /* Likewise, if the return type is itself By_Reference. */
5661 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5662 return_by_invisi_ref_p = true;
5664 /* If the type is a padded type and the underlying type would not be
5665 passed by reference or the function has a foreign convention, return
5666 the underlying type. */
5667 else if (TYPE_IS_PADDING_P (gnu_return_type)
5668 && (!default_pass_by_ref
5669 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5670 || Has_Foreign_Convention (gnat_subprog)))
5671 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5673 /* If the return type is unconstrained, it must have a maximum size.
5674 Use the padded type as the effective return type. And ensure the
5675 function uses the target's by-invisible-reference mechanism to
5676 avoid copying too much data when it returns. */
5677 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5679 tree orig_type = gnu_return_type;
5680 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5682 /* If the size overflows to 0, set it to an arbitrary positive
5683 value so that assignments in the type are preserved. Their
5684 actual size is independent of this positive value. */
5685 if (TREE_CODE (max_return_size) == INTEGER_CST
5686 && TREE_OVERFLOW (max_return_size)
5687 && integer_zerop (max_return_size))
5689 max_return_size = copy_node (bitsize_unit_node);
5690 TREE_OVERFLOW (max_return_size) = 1;
5693 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5694 0, gnat_subprog, false, false,
5695 definition, true);
5697 /* Declare it now since it will never be declared otherwise. This
5698 is necessary to ensure that its subtrees are properly marked. */
5699 if (gnu_return_type != orig_type
5700 && !DECL_P (TYPE_NAME (gnu_return_type)))
5701 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5702 true, debug_info_p, gnat_subprog);
5704 return_by_invisi_ref_p = true;
5707 /* If the return type has a size that overflows, we usually cannot have
5708 a function that returns that type. This usage doesn't really make
5709 sense anyway, so issue an error here. */
5710 if (!return_by_invisi_ref_p
5711 && TYPE_SIZE_UNIT (gnu_return_type)
5712 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5713 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5715 post_error ("cannot return type whose size overflows", gnat_subprog);
5716 gnu_return_type = copy_type (gnu_return_type);
5717 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5718 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5721 /* If the return type is incomplete, there are 2 cases: if the function
5722 returns by reference, then the return type is only linked indirectly
5723 in the profile, so the profile can be seen as complete since it need
5724 not be further modified, only the reference types need be adjusted;
5725 otherwise the profile is incomplete and need be adjusted too. */
5726 if (TYPE_IS_DUMMY_P (gnu_return_type))
5728 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5729 incomplete_profile_p = true;
5732 if (kind == E_Function)
5733 Set_Mechanism (gnat_subprog, return_unconstrained_p
5734 || return_by_direct_ref_p
5735 || return_by_invisi_ref_p
5736 ? By_Reference : By_Copy);
5739 /* A procedure (something that doesn't return anything) shouldn't be
5740 considered const since there would be no reason for calling such a
5741 subprogram. Note that procedures with Out (or In Out) parameters
5742 have already been converted into a function with a return type.
5743 Similarly, if the function returns an unconstrained type, then the
5744 function will allocate the return value on the secondary stack and
5745 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5746 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5747 const_flag = false;
5749 /* Loop over the parameters and get their associated GCC tree. While doing
5750 this, build a copy-in copy-out structure if we need one. */
5751 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5752 Present (gnat_param);
5753 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5755 const bool mech_is_by_ref
5756 = Mechanism (gnat_param) == By_Reference
5757 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5758 tree gnu_param_name = get_entity_name (gnat_param);
5759 tree gnu_param, gnu_param_type;
5760 bool cico = false;
5762 /* Fetch an existing parameter with complete type and reuse it. But we
5763 didn't save the CICO property so we can only do it for In parameters
5764 or parameters passed by reference. */
5765 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5766 && present_gnu_tree (gnat_param)
5767 && (gnu_param = get_gnu_tree (gnat_param))
5768 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5770 DECL_CHAIN (gnu_param) = NULL_TREE;
5771 gnu_param_type = TREE_TYPE (gnu_param);
5774 /* Otherwise translate the parameter type and act accordingly. */
5775 else
5777 Entity_Id gnat_param_type = Etype (gnat_param);
5779 /* For foreign convention subprograms, pass System.Address as void *
5780 or equivalent. Note that this comprises GCC builtins. */
5781 if (Has_Foreign_Convention (gnat_subprog)
5782 && Is_Descendant_Of_Address (gnat_param_type))
5783 gnu_param_type = ptr_type_node;
5784 else
5785 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5787 /* If the parameter type is incomplete, there are 2 cases: if it is
5788 passed by reference, then the type is only linked indirectly in
5789 the profile, so the profile can be seen as complete since it need
5790 not be further modified, only the reference type need be adjusted;
5791 otherwise the profile is incomplete and need be adjusted too. */
5792 if (TYPE_IS_DUMMY_P (gnu_param_type))
5794 Node_Id gnat_decl;
5796 if (mech_is_by_ref
5797 || (TYPE_REFERENCE_TO (gnu_param_type)
5798 && TYPE_IS_FAT_POINTER_P
5799 (TYPE_REFERENCE_TO (gnu_param_type)))
5800 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5802 gnu_param_type = build_reference_type (gnu_param_type);
5803 gnu_param
5804 = create_param_decl (gnu_param_name, gnu_param_type);
5805 TREE_READONLY (gnu_param) = 1;
5806 DECL_BY_REF_P (gnu_param) = 1;
5807 DECL_POINTS_TO_READONLY_P (gnu_param)
5808 = (Ekind (gnat_param) == E_In_Parameter
5809 && !Address_Taken (gnat_param));
5810 Set_Mechanism (gnat_param, By_Reference);
5811 Sloc_to_locus (Sloc (gnat_param),
5812 &DECL_SOURCE_LOCATION (gnu_param));
5815 /* ??? This is a kludge to support null procedures in spec taking
5816 a parameter with an untagged incomplete type coming from a
5817 limited context. The front-end creates a body without knowing
5818 anything about the non-limited view, which is illegal Ada and
5819 cannot be supported. Create a parameter with a fake type. */
5820 else if (kind == E_Procedure
5821 && (gnat_decl = Parent (gnat_subprog))
5822 && Nkind (gnat_decl) == N_Procedure_Specification
5823 && Null_Present (gnat_decl)
5824 && Is_Incomplete_Type (gnat_param_type))
5825 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5827 else
5829 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
5830 Call_to_gnu will stop if it encounters the PARM_DECL. */
5831 gnu_param
5832 = build_decl (input_location, PARM_DECL, gnu_param_name,
5833 gnu_param_type);
5834 associate_subprog_with_dummy_type (gnat_subprog,
5835 gnu_param_type);
5836 incomplete_profile_p = true;
5840 /* Otherwise build the parameter declaration normally. */
5841 else
5843 gnu_param
5844 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
5845 gnat_subprog, &cico);
5847 /* We are returned either a PARM_DECL or a type if no parameter
5848 needs to be passed; in either case, adjust the type. */
5849 if (DECL_P (gnu_param))
5850 gnu_param_type = TREE_TYPE (gnu_param);
5851 else
5853 gnu_param_type = gnu_param;
5854 gnu_param = NULL_TREE;
5859 /* If we have a GCC tree for the parameter, register it. */
5860 save_gnu_tree (gnat_param, NULL_TREE, false);
5861 if (gnu_param)
5863 gnu_param_type_list
5864 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
5865 gnu_param_list = chainon (gnu_param, gnu_param_list);
5866 save_gnu_tree (gnat_param, gnu_param, false);
5868 /* If a parameter is a pointer, a function may modify memory through
5869 it and thus shouldn't be considered a const function. Also, the
5870 memory may be modified between two calls, so they can't be CSE'ed.
5871 The latter case also handles by-ref parameters. */
5872 if (POINTER_TYPE_P (gnu_param_type)
5873 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
5874 const_flag = false;
5877 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5878 for it in the return type and register the association. */
5879 if (cico && !incomplete_profile_p)
5881 if (!gnu_cico_list)
5883 gnu_cico_return_type = make_node (RECORD_TYPE);
5885 /* If this is a function, we also need a field for the
5886 return value to be placed. */
5887 if (!VOID_TYPE_P (gnu_return_type))
5889 tree gnu_field
5890 = create_field_decl (get_identifier ("RETVAL"),
5891 gnu_return_type,
5892 gnu_cico_return_type, NULL_TREE,
5893 NULL_TREE, 0, 0);
5894 Sloc_to_locus (Sloc (gnat_subprog),
5895 &DECL_SOURCE_LOCATION (gnu_field));
5896 gnu_field_list = gnu_field;
5897 gnu_cico_list
5898 = tree_cons (gnu_field, void_type_node, NULL_TREE);
5901 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
5902 /* Set a default alignment to speed up accesses. But we should
5903 not increase the size of the structure too much, lest it does
5904 not fit in return registers anymore. */
5905 SET_TYPE_ALIGN (gnu_cico_return_type,
5906 get_mode_alignment (ptr_mode));
5909 tree gnu_field
5910 = create_field_decl (gnu_param_name, gnu_param_type,
5911 gnu_cico_return_type, NULL_TREE, NULL_TREE,
5912 0, 0);
5913 Sloc_to_locus (Sloc (gnat_param),
5914 &DECL_SOURCE_LOCATION (gnu_field));
5915 DECL_CHAIN (gnu_field) = gnu_field_list;
5916 gnu_field_list = gnu_field;
5917 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
5921 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5922 and finish up the return type. */
5923 if (gnu_cico_list && !incomplete_profile_p)
5925 /* If we have a CICO list but it has only one entry, we convert
5926 this function into a function that returns this object. */
5927 if (list_length (gnu_cico_list) == 1)
5928 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
5930 /* Do not finalize the return type if the subprogram is stubbed
5931 since structures are incomplete for the back-end. */
5932 else if (Convention (gnat_subprog) != Convention_Stubbed)
5934 finish_record_type (gnu_cico_return_type, nreverse (gnu_field_list),
5935 0, false);
5937 /* Try to promote the mode of the return type if it is passed
5938 in registers, again to speed up accesses. */
5939 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
5940 && !targetm.calls.return_in_memory (gnu_cico_return_type,
5941 NULL_TREE))
5943 unsigned int size
5944 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
5945 unsigned int i = BITS_PER_UNIT;
5946 scalar_int_mode mode;
5948 while (i < size)
5949 i <<= 1;
5950 if (int_mode_for_size (i, 0).exists (&mode))
5952 SET_TYPE_MODE (gnu_cico_return_type, mode);
5953 SET_TYPE_ALIGN (gnu_cico_return_type,
5954 GET_MODE_ALIGNMENT (mode));
5955 TYPE_SIZE (gnu_cico_return_type)
5956 = bitsize_int (GET_MODE_BITSIZE (mode));
5957 TYPE_SIZE_UNIT (gnu_cico_return_type)
5958 = size_int (GET_MODE_SIZE (mode));
5962 if (debug_info_p)
5963 rest_of_record_type_compilation (gnu_cico_return_type);
5966 gnu_return_type = gnu_cico_return_type;
5969 /* The lists have been built in reverse. */
5970 gnu_param_type_list = nreverse (gnu_param_type_list);
5971 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
5972 *param_list = nreverse (gnu_param_list);
5973 gnu_cico_list = nreverse (gnu_cico_list);
5975 /* If the profile is incomplete, we only set the (temporary) return and
5976 parameter types; otherwise, we build the full type. In either case,
5977 we reuse an already existing GCC tree that we built previously here. */
5978 if (incomplete_profile_p)
5980 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5982 else
5983 gnu_type = make_node (FUNCTION_TYPE);
5984 TREE_TYPE (gnu_type) = gnu_return_type;
5985 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5986 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5987 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5988 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5990 else
5992 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5994 TREE_TYPE (gnu_type) = gnu_return_type;
5995 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5996 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5997 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5998 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5999 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6000 TYPE_CANONICAL (gnu_type) = gnu_type;
6001 layout_type (gnu_type);
6003 else
6005 gnu_type
6006 = build_function_type (gnu_return_type, gnu_param_type_list);
6008 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
6009 has a different TYPE_CI_CO_LIST or flags. */
6010 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
6011 return_unconstrained_p,
6012 return_by_direct_ref_p,
6013 return_by_invisi_ref_p))
6015 gnu_type = copy_type (gnu_type);
6016 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6017 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6018 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6019 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6023 if (const_flag)
6024 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6026 if (No_Return (gnat_subprog))
6027 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6029 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6030 corresponding DECL node and check the parameter association. */
6031 if (Convention (gnat_subprog) == Convention_Intrinsic
6032 && Present (Interface_Name (gnat_subprog)))
6034 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6035 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6037 /* If we have a builtin DECL for that function, use it. Check if
6038 the profiles are compatible and warn if they are not. Note that
6039 the checker is expected to post diagnostics in this case. */
6040 if (gnu_builtin_decl)
6042 intrin_binding_t inb
6043 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6045 if (!intrin_profiles_compatible_p (&inb))
6046 post_error
6047 ("?profile of& doesn''t match the builtin it binds!",
6048 gnat_subprog);
6050 return gnu_builtin_decl;
6053 /* Inability to find the builtin DECL most often indicates a genuine
6054 mistake, but imports of unregistered intrinsics are sometimes used
6055 on purpose to allow hooking in alternate bodies; we post a warning
6056 conditioned on Wshadow in this case, to let developers be notified
6057 on demand without risking false positives with common default sets
6058 of options. */
6059 if (warn_shadow)
6060 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6064 return gnu_type;
6067 /* Return the external name for GNAT_SUBPROG given its entity name. */
6069 static tree
6070 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6072 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6074 /* If there was no specified Interface_Name and the external and
6075 internal names of the subprogram are the same, only use the
6076 internal name to allow disambiguation of nested subprograms. */
6077 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6078 gnu_ext_name = NULL_TREE;
6080 return gnu_ext_name;
6083 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6084 qualifiers on TYPE. */
6086 static tree
6087 change_qualified_type (tree type, int type_quals)
6089 /* Qualifiers must be put on the associated array type. */
6090 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
6091 return type;
6093 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6096 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6097 build_nonshared_array_type. */
6099 static void
6100 set_nonaliased_component_on_array_type (tree type)
6102 TYPE_NONALIASED_COMPONENT (type) = 1;
6103 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6106 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6107 build_nonshared_array_type. */
6109 static void
6110 set_reverse_storage_order_on_array_type (tree type)
6112 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6113 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6116 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6118 static bool
6119 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6121 while (Present (Corresponding_Discriminant (discr1)))
6122 discr1 = Corresponding_Discriminant (discr1);
6124 while (Present (Corresponding_Discriminant (discr2)))
6125 discr2 = Corresponding_Discriminant (discr2);
6127 return
6128 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6131 /* Return true if the array type GNU_TYPE, which represents a dimension of
6132 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6134 static bool
6135 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6137 /* If the array type is not the innermost dimension of the GNAT type,
6138 then it has a non-aliased component. */
6139 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6140 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6141 return true;
6143 /* If the array type has an aliased component in the front-end sense,
6144 then it also has an aliased component in the back-end sense. */
6145 if (Has_Aliased_Components (gnat_type))
6146 return false;
6148 /* If this is a derived type, then it has a non-aliased component if
6149 and only if its parent type also has one. */
6150 if (Is_Derived_Type (gnat_type))
6152 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6153 int index;
6154 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6155 gnu_parent_type
6156 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6157 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6158 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6159 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6162 /* Otherwise, rely exclusively on properties of the element type. */
6163 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6166 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6168 static bool
6169 compile_time_known_address_p (Node_Id gnat_address)
6171 /* Handle reference to a constant. */
6172 if (Is_Entity_Name (gnat_address)
6173 && Ekind (Entity (gnat_address)) == E_Constant)
6175 gnat_address = Constant_Value (Entity (gnat_address));
6176 if (No (gnat_address))
6177 return false;
6180 /* Catch System'To_Address. */
6181 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6182 gnat_address = Expression (gnat_address);
6184 return Compile_Time_Known_Value (gnat_address);
6187 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6188 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6190 static bool
6191 cannot_be_superflat (Node_Id gnat_range)
6193 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6194 Node_Id scalar_range;
6195 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6197 /* If the low bound is not constant, try to find an upper bound. */
6198 while (Nkind (gnat_lb) != N_Integer_Literal
6199 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6200 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6201 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6202 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6203 || Nkind (scalar_range) == N_Range))
6204 gnat_lb = High_Bound (scalar_range);
6206 /* If the high bound is not constant, try to find a lower bound. */
6207 while (Nkind (gnat_hb) != N_Integer_Literal
6208 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6209 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6210 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6211 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6212 || Nkind (scalar_range) == N_Range))
6213 gnat_hb = Low_Bound (scalar_range);
6215 /* If we have failed to find constant bounds, punt. */
6216 if (Nkind (gnat_lb) != N_Integer_Literal
6217 || Nkind (gnat_hb) != N_Integer_Literal)
6218 return false;
6220 /* We need at least a signed 64-bit type to catch most cases. */
6221 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6222 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6223 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6224 return false;
6226 /* If the low bound is the smallest integer, nothing can be smaller. */
6227 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6228 if (TREE_OVERFLOW (gnu_lb_minus_one))
6229 return true;
6231 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6234 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6236 static bool
6237 constructor_address_p (tree gnu_expr)
6239 while (TREE_CODE (gnu_expr) == NOP_EXPR
6240 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6241 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6242 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6244 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6245 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6248 /* Return true if the size in units represented by GNU_SIZE can be handled by
6249 an allocation. If STATIC_P is true, consider only what can be done with a
6250 static allocation. */
6252 static bool
6253 allocatable_size_p (tree gnu_size, bool static_p)
6255 /* We can allocate a fixed size if it is a valid for the middle-end. */
6256 if (TREE_CODE (gnu_size) == INTEGER_CST)
6257 return valid_constant_size_p (gnu_size);
6259 /* We can allocate a variable size if this isn't a static allocation. */
6260 else
6261 return !static_p;
6264 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6265 initial value of an object of GNU_TYPE. */
6267 static bool
6268 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6270 /* Do not convert if the object's type is unconstrained because this would
6271 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6272 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6273 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6274 return false;
6276 /* Do not convert if the object's type is a padding record whose field is of
6277 self-referential size because we want to copy only the actual data. */
6278 if (type_is_padding_self_referential (gnu_type))
6279 return false;
6281 /* Do not convert a call to a function that returns with variable size since
6282 we want to use the return slot optimization in this case. */
6283 if (TREE_CODE (gnu_expr) == CALL_EXPR
6284 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6285 return false;
6287 /* Do not convert to a record type with a variant part from a record type
6288 without one, to keep the object simpler. */
6289 if (TREE_CODE (gnu_type) == RECORD_TYPE
6290 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6291 && get_variant_part (gnu_type)
6292 && !get_variant_part (TREE_TYPE (gnu_expr)))
6293 return false;
6295 /* In all the other cases, convert the expression to the object's type. */
6296 return true;
6299 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6300 be elaborated at the point of its definition, but do nothing else. */
6302 void
6303 elaborate_entity (Entity_Id gnat_entity)
6305 switch (Ekind (gnat_entity))
6307 case E_Signed_Integer_Subtype:
6308 case E_Modular_Integer_Subtype:
6309 case E_Enumeration_Subtype:
6310 case E_Ordinary_Fixed_Point_Subtype:
6311 case E_Decimal_Fixed_Point_Subtype:
6312 case E_Floating_Point_Subtype:
6314 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6315 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6317 /* ??? Tests to avoid Constraint_Error in static expressions
6318 are needed until after the front stops generating bogus
6319 conversions on bounds of real types. */
6320 if (!Raises_Constraint_Error (gnat_lb))
6321 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6322 Needs_Debug_Info (gnat_entity));
6323 if (!Raises_Constraint_Error (gnat_hb))
6324 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6325 Needs_Debug_Info (gnat_entity));
6326 break;
6329 case E_Record_Subtype:
6330 case E_Private_Subtype:
6331 case E_Limited_Private_Subtype:
6332 case E_Record_Subtype_With_Private:
6333 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6335 Node_Id gnat_discriminant_expr;
6336 Entity_Id gnat_field;
6338 for (gnat_field
6339 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6340 gnat_discriminant_expr
6341 = First_Elmt (Discriminant_Constraint (gnat_entity));
6342 Present (gnat_field);
6343 gnat_field = Next_Discriminant (gnat_field),
6344 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6345 /* Ignore access discriminants. */
6346 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6347 elaborate_expression (Node (gnat_discriminant_expr),
6348 gnat_entity, get_entity_char (gnat_field),
6349 true, false, false);
6351 break;
6356 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6357 NAME, ARGS and ERROR_POINT. */
6359 static void
6360 prepend_one_attribute (struct attrib **attr_list,
6361 enum attrib_type attrib_type,
6362 tree attr_name,
6363 tree attr_args,
6364 Node_Id attr_error_point)
6366 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6368 attr->type = attrib_type;
6369 attr->name = attr_name;
6370 attr->args = attr_args;
6371 attr->error_point = attr_error_point;
6373 attr->next = *attr_list;
6374 *attr_list = attr;
6377 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6379 static void
6380 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6382 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6383 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6384 enum attrib_type etype;
6386 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6387 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6389 case Pragma_Machine_Attribute:
6390 etype = ATTR_MACHINE_ATTRIBUTE;
6391 break;
6393 case Pragma_Linker_Alias:
6394 etype = ATTR_LINK_ALIAS;
6395 break;
6397 case Pragma_Linker_Section:
6398 etype = ATTR_LINK_SECTION;
6399 break;
6401 case Pragma_Linker_Constructor:
6402 etype = ATTR_LINK_CONSTRUCTOR;
6403 break;
6405 case Pragma_Linker_Destructor:
6406 etype = ATTR_LINK_DESTRUCTOR;
6407 break;
6409 case Pragma_Weak_External:
6410 etype = ATTR_WEAK_EXTERNAL;
6411 break;
6413 case Pragma_Thread_Local_Storage:
6414 etype = ATTR_THREAD_LOCAL_STORAGE;
6415 break;
6417 default:
6418 return;
6421 /* See what arguments we have and turn them into GCC trees for attribute
6422 handlers. These expect identifier for strings. We handle at most two
6423 arguments and static expressions only. */
6424 if (Present (gnat_arg) && Present (First (gnat_arg)))
6426 Node_Id gnat_arg0 = Next (First (gnat_arg));
6427 Node_Id gnat_arg1 = Empty;
6429 if (Present (gnat_arg0)
6430 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6432 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6434 if (TREE_CODE (gnu_arg0) == STRING_CST)
6436 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6437 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6438 return;
6441 gnat_arg1 = Next (gnat_arg0);
6444 if (Present (gnat_arg1)
6445 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6447 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6449 if (TREE_CODE (gnu_arg1) == STRING_CST)
6450 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6454 /* Prepend to the list. Make a list of the argument we might have, as GCC
6455 expects it. */
6456 prepend_one_attribute (attr_list, etype, gnu_arg0,
6457 gnu_arg1
6458 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6459 Present (Next (First (gnat_arg)))
6460 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6463 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6465 static void
6466 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6468 Node_Id gnat_temp;
6470 /* Attributes are stored as Representation Item pragmas. */
6471 for (gnat_temp = First_Rep_Item (gnat_entity);
6472 Present (gnat_temp);
6473 gnat_temp = Next_Rep_Item (gnat_temp))
6474 if (Nkind (gnat_temp) == N_Pragma)
6475 prepend_one_attribute_pragma (attr_list, gnat_temp);
6478 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6479 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6480 return the GCC tree to use for that expression. S is the suffix to use
6481 if a variable needs to be created and DEFINITION is true if this is done
6482 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6483 otherwise, we are just elaborating the expression for side-effects. If
6484 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6485 isn't needed for code generation. */
6487 static tree
6488 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6489 bool definition, bool need_value, bool need_debug)
6491 tree gnu_expr;
6493 /* If we already elaborated this expression (e.g. it was involved
6494 in the definition of a private type), use the old value. */
6495 if (present_gnu_tree (gnat_expr))
6496 return get_gnu_tree (gnat_expr);
6498 /* If we don't need a value and this is static or a discriminant,
6499 we don't need to do anything. */
6500 if (!need_value
6501 && (Is_OK_Static_Expression (gnat_expr)
6502 || (Nkind (gnat_expr) == N_Identifier
6503 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6504 return NULL_TREE;
6506 /* If it's a static expression, we don't need a variable for debugging. */
6507 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6508 need_debug = false;
6510 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6511 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6512 definition, need_debug);
6514 /* Save the expression in case we try to elaborate this entity again. Since
6515 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6516 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6517 save_gnu_tree (gnat_expr, gnu_expr, true);
6519 return need_value ? gnu_expr : error_mark_node;
6522 /* Similar, but take a GNU expression and always return a result. */
6524 static tree
6525 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6526 bool definition, bool need_debug)
6528 const bool expr_public_p = Is_Public (gnat_entity);
6529 const bool expr_global_p = expr_public_p || global_bindings_p ();
6530 bool expr_variable_p, use_variable;
6532 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6533 that an expression cannot contain both a discriminant and a variable. */
6534 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6535 return gnu_expr;
6537 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6538 a variable that is initialized to contain the expression when the package
6539 containing the definition is elaborated. If this entity is defined at top
6540 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6541 if this is necessary. */
6542 if (TREE_CONSTANT (gnu_expr))
6543 expr_variable_p = false;
6544 else
6546 /* Skip any conversions and simple constant arithmetics to see if the
6547 expression is based on a read-only variable. */
6548 tree inner = remove_conversions (gnu_expr, true);
6550 inner = skip_simple_constant_arithmetic (inner);
6552 if (handled_component_p (inner))
6553 inner = get_inner_constant_reference (inner);
6555 expr_variable_p
6556 = !(inner
6557 && TREE_CODE (inner) == VAR_DECL
6558 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6561 /* We only need to use the variable if we are in a global context since GCC
6562 can do the right thing in the local case. However, when not optimizing,
6563 use it for bounds of loop iteration scheme to avoid code duplication. */
6564 use_variable = expr_variable_p
6565 && (expr_global_p
6566 || (!optimize
6567 && definition
6568 && Is_Itype (gnat_entity)
6569 && Nkind (Associated_Node_For_Itype (gnat_entity))
6570 == N_Loop_Parameter_Specification));
6572 /* Now create it, possibly only for debugging purposes. */
6573 if (use_variable || need_debug)
6575 /* The following variable creation can happen when processing the body
6576 of subprograms that are defined out of the extended main unit and
6577 inlined. In this case, we are not at the global scope, and thus the
6578 new variable must not be tagged "external", as we used to do here as
6579 soon as DEFINITION was false. */
6580 tree gnu_decl
6581 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6582 TREE_TYPE (gnu_expr), gnu_expr, true,
6583 expr_public_p, !definition && expr_global_p,
6584 expr_global_p, false, true, need_debug,
6585 NULL, gnat_entity);
6587 /* Using this variable at debug time (if need_debug is true) requires a
6588 proper location. The back-end will compute a location for this
6589 variable only if the variable is used by the generated code.
6590 Returning the variable ensures the caller will use it in generated
6591 code. Note that there is no need for a location if the debug info
6592 contains an integer constant.
6593 TODO: when the encoding-based debug scheme is dropped, move this
6594 condition to the top-level IF block: we will not need to create a
6595 variable anymore in such cases, then. */
6596 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6597 return gnu_decl;
6600 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6603 /* Similar, but take an alignment factor and make it explicit in the tree. */
6605 static tree
6606 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6607 bool definition, bool need_debug, unsigned int align)
6609 tree unit_align = size_int (align / BITS_PER_UNIT);
6610 return
6611 size_binop (MULT_EXPR,
6612 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6613 gnu_expr,
6614 unit_align),
6615 gnat_entity, s, definition,
6616 need_debug),
6617 unit_align);
6620 /* Structure to hold internal data for elaborate_reference. */
6622 struct er_data
6624 Entity_Id entity;
6625 bool definition;
6626 unsigned int n;
6629 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6631 static tree
6632 elaborate_reference_1 (tree ref, void *data)
6634 struct er_data *er = (struct er_data *)data;
6635 char suffix[16];
6637 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6638 if (TREE_CONSTANT (ref))
6639 return ref;
6641 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6642 pointer. This may be more efficient, but will also allow us to more
6643 easily find the match for the PLACEHOLDER_EXPR. */
6644 if (TREE_CODE (ref) == COMPONENT_REF
6645 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6646 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6647 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6648 TREE_OPERAND (ref, 1), NULL_TREE);
6650 sprintf (suffix, "EXP%d", ++er->n);
6651 return
6652 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6655 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6656 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6657 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6659 static tree
6660 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6661 tree *init)
6663 struct er_data er = { gnat_entity, definition, 0 };
6664 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6667 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6668 the value passed against the list of choices. */
6670 static tree
6671 choices_to_gnu (tree operand, Node_Id choices)
6673 Node_Id choice;
6674 Node_Id gnat_temp;
6675 tree result = boolean_false_node;
6676 tree this_test, low = 0, high = 0, single = 0;
6678 for (choice = First (choices); Present (choice); choice = Next (choice))
6680 switch (Nkind (choice))
6682 case N_Range:
6683 low = gnat_to_gnu (Low_Bound (choice));
6684 high = gnat_to_gnu (High_Bound (choice));
6686 this_test
6687 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6688 build_binary_op (GE_EXPR, boolean_type_node,
6689 operand, low, true),
6690 build_binary_op (LE_EXPR, boolean_type_node,
6691 operand, high, true),
6692 true);
6694 break;
6696 case N_Subtype_Indication:
6697 gnat_temp = Range_Expression (Constraint (choice));
6698 low = gnat_to_gnu (Low_Bound (gnat_temp));
6699 high = gnat_to_gnu (High_Bound (gnat_temp));
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;
6710 case N_Identifier:
6711 case N_Expanded_Name:
6712 /* This represents either a subtype range, an enumeration
6713 literal, or a constant Ekind says which. If an enumeration
6714 literal or constant, fall through to the next case. */
6715 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6716 && Ekind (Entity (choice)) != E_Constant)
6718 tree type = gnat_to_gnu_type (Entity (choice));
6720 low = TYPE_MIN_VALUE (type);
6721 high = TYPE_MAX_VALUE (type);
6723 this_test
6724 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6725 build_binary_op (GE_EXPR, boolean_type_node,
6726 operand, low, true),
6727 build_binary_op (LE_EXPR, boolean_type_node,
6728 operand, high, true),
6729 true);
6730 break;
6733 /* ... fall through ... */
6735 case N_Character_Literal:
6736 case N_Integer_Literal:
6737 single = gnat_to_gnu (choice);
6738 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6739 single, true);
6740 break;
6742 case N_Others_Choice:
6743 this_test = boolean_true_node;
6744 break;
6746 default:
6747 gcc_unreachable ();
6750 if (result == boolean_false_node)
6751 result = this_test;
6752 else
6753 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6754 this_test, true);
6757 return result;
6760 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6761 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6763 static int
6764 adjust_packed (tree field_type, tree record_type, int packed)
6766 /* If the field contains an item of variable size, we cannot pack it
6767 because we cannot create temporaries of non-fixed size in case
6768 we need to take the address of the field. See addressable_p and
6769 the notes on the addressability issues for further details. */
6770 if (type_has_variable_size (field_type))
6771 return 0;
6773 /* In the other cases, we can honor the packing. */
6774 if (packed)
6775 return packed;
6777 /* If the alignment of the record is specified and the field type
6778 is over-aligned, request Storage_Unit alignment for the field. */
6779 if (TYPE_ALIGN (record_type)
6780 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6781 return -1;
6783 /* Likewise if the maximum alignment of the record is specified. */
6784 if (TYPE_MAX_ALIGN (record_type)
6785 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6786 return -1;
6788 return 0;
6791 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6792 placed in GNU_RECORD_TYPE.
6794 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6795 record has Component_Alignment of Storage_Unit.
6797 DEFINITION is true if this field is for a record being defined.
6799 DEBUG_INFO_P is true if we need to write debug information for types
6800 that we may create in the process. */
6802 static tree
6803 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6804 bool definition, bool debug_info_p)
6806 const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field));
6807 const Entity_Id gnat_field_type = Etype (gnat_field);
6808 const bool is_atomic
6809 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6810 const bool is_aliased = Is_Aliased (gnat_field);
6811 const bool is_independent
6812 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6813 const bool is_volatile
6814 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6815 const bool is_strict_alignment = Strict_Alignment (gnat_field_type);
6816 /* We used to consider that volatile fields also require strict alignment,
6817 but that was an interpolation and would cause us to reject a pragma
6818 volatile on a packed record type containing boolean components, while
6819 there is no basis to do so in the RM. In such cases, the writes will
6820 involve load-modify-store sequences, but that's OK for volatile. The
6821 only constraint is the implementation advice whereby only the bits of
6822 the components should be accessed if they both start and end on byte
6823 boundaries, but that should be guaranteed by the GCC memory model. */
6824 const bool needs_strict_alignment
6825 = (is_atomic || is_aliased || is_independent || is_strict_alignment);
6826 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6827 tree gnu_field_id = get_entity_name (gnat_field);
6828 tree gnu_field, gnu_size, gnu_pos;
6830 /* If this field requires strict alignment, we cannot pack it because
6831 it would very likely be under-aligned in the record. */
6832 if (needs_strict_alignment)
6833 packed = 0;
6834 else
6835 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6837 /* If a size is specified, use it. Otherwise, if the record type is packed,
6838 use the official RM size. See "Handling of Type'Size Values" in Einfo
6839 for further details. */
6840 if (Known_Esize (gnat_field))
6841 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6842 gnat_field, FIELD_DECL, false, true);
6843 else if (packed == 1)
6844 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6845 gnat_field, FIELD_DECL, false, true);
6846 else
6847 gnu_size = NULL_TREE;
6849 /* If we have a specified size that is smaller than that of the field's type,
6850 or a position is specified, and the field's type is a record that doesn't
6851 require strict alignment, see if we can get either an integral mode form
6852 of the type or a smaller form. If we can, show a size was specified for
6853 the field if there wasn't one already, so we know to make this a bitfield
6854 and avoid making things wider.
6856 Changing to an integral mode form is useful when the record is packed as
6857 we can then place the field at a non-byte-aligned position and so achieve
6858 tighter packing. This is in addition required if the field shares a byte
6859 with another field and the front-end lets the back-end handle the access
6860 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6862 Changing to a smaller form is required if the specified size is smaller
6863 than that of the field's type and the type contains sub-fields that are
6864 padded, in order to avoid generating accesses to these sub-fields that
6865 are wider than the field.
6867 We avoid the transformation if it is not required or potentially useful,
6868 as it might entail an increase of the field's alignment and have ripple
6869 effects on the outer record type. A typical case is a field known to be
6870 byte-aligned and not to share a byte with another field. */
6871 if (!needs_strict_alignment
6872 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6873 && !TYPE_FAT_POINTER_P (gnu_field_type)
6874 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6875 && (packed == 1
6876 || (gnu_size
6877 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6878 || (Present (Component_Clause (gnat_field))
6879 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6880 % BITS_PER_UNIT == 0
6881 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6883 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6884 if (gnu_packable_type != gnu_field_type)
6886 gnu_field_type = gnu_packable_type;
6887 if (!gnu_size)
6888 gnu_size = rm_size (gnu_field_type);
6892 if (Is_Atomic_Or_VFA (gnat_field))
6893 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6895 if (Present (Component_Clause (gnat_field)))
6897 Node_Id gnat_clause = Component_Clause (gnat_field);
6898 Entity_Id gnat_parent = Parent_Subtype (gnat_record_type);
6900 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6901 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6902 gnat_field, FIELD_DECL, false, true);
6904 /* Ensure the position does not overlap with the parent subtype, if there
6905 is one. This test is omitted if the parent of the tagged type has a
6906 full rep clause since, in this case, component clauses are allowed to
6907 overlay the space allocated for the parent type and the front-end has
6908 checked that there are no overlapping components. */
6909 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6911 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6913 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6914 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6915 post_error_ne_tree
6916 ("offset of& must be beyond parent{, minimum allowed is ^}",
6917 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6920 /* If this field needs strict alignment, make sure that the record is
6921 sufficiently aligned and that the position and size are consistent
6922 with the type. But don't do it if we are just annotating types and
6923 the field's type is tagged, since tagged types aren't fully laid out
6924 in this mode. Also, note that atomic implies volatile so the inner
6925 test sequences ordering is significant here. */
6926 if (needs_strict_alignment
6927 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6929 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6931 if (TYPE_ALIGN (gnu_record_type) < type_align)
6932 SET_TYPE_ALIGN (gnu_record_type, type_align);
6934 /* If the position is not a multiple of the alignment of the type,
6935 then error out and reset the position. */
6936 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6937 bitsize_int (type_align))))
6939 const char *s;
6941 if (is_atomic)
6942 s = "position of atomic field& must be multiple of ^ bits";
6943 else if (is_aliased)
6944 s = "position of aliased field& must be multiple of ^ bits";
6945 else if (is_independent)
6946 s = "position of independent field& must be multiple of ^ bits";
6947 else if (is_strict_alignment)
6948 s = "position of & with aliased or tagged part must be"
6949 " multiple of ^ bits";
6950 else
6951 gcc_unreachable ();
6953 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6954 type_align);
6955 gnu_pos = NULL_TREE;
6958 if (gnu_size)
6960 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6961 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6963 /* If the size is lower than that of the type, or greater for
6964 atomic and aliased, then error out and reset the size. */
6965 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6967 const char *s;
6969 if (is_atomic)
6970 s = "size of atomic field& must be ^ bits";
6971 else if (is_aliased)
6972 s = "size of aliased field& must be ^ bits";
6973 else if (is_independent)
6974 s = "size of independent field& must be at least ^ bits";
6975 else if (is_strict_alignment)
6976 s = "size of & with aliased or tagged part must be"
6977 " at least ^ bits";
6978 else
6979 gcc_unreachable ();
6981 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6982 gnu_type_size);
6983 gnu_size = NULL_TREE;
6986 /* Likewise if the size is not a multiple of a byte, */
6987 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6988 bitsize_unit_node)))
6990 const char *s;
6992 if (is_independent)
6993 s = "size of independent field& must be multiple of"
6994 " Storage_Unit";
6995 else if (is_strict_alignment)
6996 s = "size of & with aliased or tagged part must be"
6997 " multiple of Storage_Unit";
6998 else
6999 gcc_unreachable ();
7001 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
7002 gnu_size = NULL_TREE;
7008 /* If the record has rep clauses and this is the tag field, make a rep
7009 clause for it as well. */
7010 else if (Has_Specified_Layout (gnat_record_type)
7011 && Chars (gnat_field) == Name_uTag)
7013 gnu_pos = bitsize_zero_node;
7014 gnu_size = TYPE_SIZE (gnu_field_type);
7017 else
7019 gnu_pos = NULL_TREE;
7021 /* If we are packing the record and the field is BLKmode, round the
7022 size up to a byte boundary. */
7023 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7024 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7027 /* We need to make the size the maximum for the type if it is
7028 self-referential and an unconstrained type. In that case, we can't
7029 pack the field since we can't make a copy to align it. */
7030 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7031 && !gnu_size
7032 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7033 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7035 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7036 packed = 0;
7039 /* If a size is specified, adjust the field's type to it. */
7040 if (gnu_size)
7042 tree orig_field_type;
7044 /* If the field's type is justified modular, we would need to remove
7045 the wrapper to (better) meet the layout requirements. However we
7046 can do so only if the field is not aliased to preserve the unique
7047 layout, if it has the same storage order as the enclosing record
7048 and if the prescribed size is not greater than that of the packed
7049 array to preserve the justification. */
7050 if (!needs_strict_alignment
7051 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7052 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7053 && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type)
7054 == Reverse_Storage_Order (gnat_record_type)
7055 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7056 <= 0)
7057 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7059 /* Similarly if the field's type is a misaligned integral type, but
7060 there is no restriction on the size as there is no justification. */
7061 if (!needs_strict_alignment
7062 && TYPE_IS_PADDING_P (gnu_field_type)
7063 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7064 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7066 gnu_field_type
7067 = make_type_from_size (gnu_field_type, gnu_size,
7068 Has_Biased_Representation (gnat_field));
7070 orig_field_type = gnu_field_type;
7071 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7072 false, false, definition, true);
7074 /* If a padding record was made, declare it now since it will never be
7075 declared otherwise. This is necessary to ensure that its subtrees
7076 are properly marked. */
7077 if (gnu_field_type != orig_field_type
7078 && !DECL_P (TYPE_NAME (gnu_field_type)))
7079 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7080 debug_info_p, gnat_field);
7083 /* Otherwise (or if there was an error), don't specify a position. */
7084 else
7085 gnu_pos = NULL_TREE;
7087 /* If the field's type is a padded type made for a scalar field of a record
7088 type with reverse storage order, we need to propagate the reverse storage
7089 order to the padding type since it is the innermost enclosing aggregate
7090 type around the scalar. */
7091 if (TYPE_IS_PADDING_P (gnu_field_type)
7092 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7093 && Is_Scalar_Type (gnat_field_type))
7094 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7096 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7097 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7099 /* Now create the decl for the field. */
7100 gnu_field
7101 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7102 gnu_size, gnu_pos, packed, is_aliased);
7103 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7104 DECL_ALIASED_P (gnu_field) = is_aliased;
7105 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7107 if (Ekind (gnat_field) == E_Discriminant)
7109 DECL_INVARIANT_P (gnu_field)
7110 = No (Discriminant_Default_Value (gnat_field));
7111 DECL_DISCRIMINANT_NUMBER (gnu_field)
7112 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7115 return gnu_field;
7118 /* Return true if at least one member of COMPONENT_LIST needs strict
7119 alignment. */
7121 static bool
7122 components_need_strict_alignment (Node_Id component_list)
7124 Node_Id component_decl;
7126 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7127 Present (component_decl);
7128 component_decl = Next_Non_Pragma (component_decl))
7130 Entity_Id gnat_field = Defining_Entity (component_decl);
7132 if (Is_Aliased (gnat_field))
7133 return true;
7135 if (Strict_Alignment (Etype (gnat_field)))
7136 return true;
7139 return false;
7142 /* Return true if TYPE is a type with variable size or a padding type with a
7143 field of variable size or a record that has a field with such a type. */
7145 static bool
7146 type_has_variable_size (tree type)
7148 tree field;
7150 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7151 return true;
7153 if (TYPE_IS_PADDING_P (type)
7154 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7155 return true;
7157 if (!RECORD_OR_UNION_TYPE_P (type))
7158 return false;
7160 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7161 if (type_has_variable_size (TREE_TYPE (field)))
7162 return true;
7164 return false;
7167 /* Return true if FIELD is an artificial field. */
7169 static bool
7170 field_is_artificial (tree field)
7172 /* These fields are generated by the front-end proper. */
7173 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7174 return true;
7176 /* These fields are generated by gigi. */
7177 if (DECL_INTERNAL_P (field))
7178 return true;
7180 return false;
7183 /* Return true if FIELD is a non-artificial field with self-referential
7184 size. */
7186 static bool
7187 field_has_self_size (tree field)
7189 if (field_is_artificial (field))
7190 return false;
7192 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7193 return false;
7195 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7198 /* Return true if FIELD is a non-artificial field with variable size. */
7200 static bool
7201 field_has_variable_size (tree field)
7203 if (field_is_artificial (field))
7204 return false;
7206 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7207 return false;
7209 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7212 /* qsort comparer for the bit positions of two record components. */
7214 static int
7215 compare_field_bitpos (const PTR rt1, const PTR rt2)
7217 const_tree const field1 = * (const_tree const *) rt1;
7218 const_tree const field2 = * (const_tree const *) rt2;
7219 const int ret
7220 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7222 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7225 /* Reverse function from gnat_to_gnu_field: return the GNAT field present in
7226 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and
7227 corresponding to the GNU tree GNU_FIELD. */
7229 static Entity_Id
7230 gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list,
7231 Entity_Id gnat_record_type)
7233 Entity_Id gnat_component_decl, gnat_field;
7235 if (Present (Component_Items (gnat_component_list)))
7236 for (gnat_component_decl
7237 = First_Non_Pragma (Component_Items (gnat_component_list));
7238 Present (gnat_component_decl);
7239 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7241 gnat_field = Defining_Entity (gnat_component_decl);
7242 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7243 return gnat_field;
7246 if (Has_Discriminants (gnat_record_type))
7247 for (gnat_field = First_Stored_Discriminant (gnat_record_type);
7248 Present (gnat_field);
7249 gnat_field = Next_Stored_Discriminant (gnat_field))
7250 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7251 return gnat_field;
7253 return Empty;
7256 /* Issue a warning for the problematic placement of GNU_FIELD present in
7257 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE.
7258 IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant.
7259 DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */
7261 static void
7262 warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list,
7263 Entity_Id gnat_record_type, bool in_variant,
7264 bool do_reorder)
7266 if (!Comes_From_Source (gnat_record_type))
7267 return;
7269 const char *msg1
7270 = in_variant
7271 ? "?variant layout may cause performance issues"
7272 : "?record layout may cause performance issues";
7273 const char *msg2
7274 = field_has_self_size (gnu_field)
7275 ? "?component & whose length depends on a discriminant"
7276 : field_has_variable_size (gnu_field)
7277 ? "?component & whose length is not fixed"
7278 : "?component & whose length is not multiple of a byte";
7279 const char *msg3
7280 = do_reorder
7281 ? "?comes too early and was moved down"
7282 : "?comes too early and ought to be moved down";
7284 Entity_Id gnat_field
7285 = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type);
7287 gcc_assert (Present (gnat_field));
7289 post_error (msg1, gnat_field);
7290 post_error_ne (msg2, gnat_field, gnat_field);
7291 post_error (msg3, gnat_field);
7294 /* Structure holding information for a given variant. */
7295 typedef struct vinfo
7297 /* The record type of the variant. */
7298 tree type;
7300 /* The name of the variant. */
7301 tree name;
7303 /* The qualifier of the variant. */
7304 tree qual;
7306 /* Whether the variant has a rep clause. */
7307 bool has_rep;
7309 /* Whether the variant is packed. */
7310 bool packed;
7312 } vinfo_t;
7314 /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to
7315 GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and
7316 finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects
7317 the layout (see below). When called from gnat_to_gnu_entity during the
7318 processing of a record definition, the GCC node for the parent, if any,
7319 will be the single field of GNU_RECORD_TYPE and the GCC nodes for the
7320 discriminants will be on GNU_FIELD_LIST. The other call to this function
7321 is a recursive call for the component list of a variant and, in this case,
7322 GNU_FIELD_LIST is empty.
7324 PACKED is 1 if this is for a packed record or -1 if this is for a record
7325 with Component_Alignment of Storage_Unit.
7327 DEFINITION is true if we are defining this record type.
7329 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7330 out the record. This means the alignment only serves to force fields to
7331 be bitfields, but not to require the record to be that aligned. This is
7332 used for variants.
7334 ALL_REP is true if a rep clause is present for all the fields.
7336 UNCHECKED_UNION is true if we are building this type for a record with a
7337 Pragma Unchecked_Union.
7339 ARTIFICIAL is true if this is a type that was generated by the compiler.
7341 DEBUG_INFO is true if we need to write debug information about the type.
7343 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7344 mean that its contents may be unused as well, only the container itself.
7346 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7347 the outer record type down to this variant level. It is nonzero only if
7348 all the fields down to this level have a rep clause and ALL_REP is false.
7350 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7351 with a rep clause is to be added; in this case, that is all that should
7352 be done with such fields and the return value will be false. */
7354 static bool
7355 components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type,
7356 tree gnu_field_list, tree gnu_record_type, int packed,
7357 bool definition, bool cancel_alignment, bool all_rep,
7358 bool unchecked_union, bool artificial, bool debug_info,
7359 bool maybe_unused, tree first_free_pos,
7360 tree *p_gnu_rep_list)
7362 const bool needs_xv_encodings
7363 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7364 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7365 bool variants_have_rep = all_rep;
7366 bool layout_with_rep = false;
7367 bool has_self_field = false;
7368 bool has_aliased_after_self_field = false;
7369 Entity_Id gnat_component_decl, gnat_variant_part;
7370 tree gnu_field, gnu_next, gnu_last;
7371 tree gnu_variant_part = NULL_TREE;
7372 tree gnu_rep_list = NULL_TREE;
7374 /* For each component referenced in a component declaration create a GCC
7375 field and add it to the list, skipping pragmas in the GNAT list. */
7376 gnu_last = tree_last (gnu_field_list);
7377 if (Present (Component_Items (gnat_component_list)))
7378 for (gnat_component_decl
7379 = First_Non_Pragma (Component_Items (gnat_component_list));
7380 Present (gnat_component_decl);
7381 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7383 Entity_Id gnat_field = Defining_Entity (gnat_component_decl);
7384 Name_Id gnat_name = Chars (gnat_field);
7386 /* If present, the _Parent field must have been created as the single
7387 field of the record type. Put it before any other fields. */
7388 if (gnat_name == Name_uParent)
7390 gnu_field = TYPE_FIELDS (gnu_record_type);
7391 gnu_field_list = chainon (gnu_field_list, gnu_field);
7393 else
7395 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7396 definition, debug_info);
7398 /* If this is the _Tag field, put it before any other fields. */
7399 if (gnat_name == Name_uTag)
7400 gnu_field_list = chainon (gnu_field_list, gnu_field);
7402 /* If this is the _Controller field, put it before the other
7403 fields except for the _Tag or _Parent field. */
7404 else if (gnat_name == Name_uController && gnu_last)
7406 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7407 DECL_CHAIN (gnu_last) = gnu_field;
7410 /* If this is a regular field, put it after the other fields. */
7411 else
7413 DECL_CHAIN (gnu_field) = gnu_field_list;
7414 gnu_field_list = gnu_field;
7415 if (!gnu_last)
7416 gnu_last = gnu_field;
7418 /* And record information for the final layout. */
7419 if (field_has_self_size (gnu_field))
7420 has_self_field = true;
7421 else if (has_self_field && DECL_ALIASED_P (gnu_field))
7422 has_aliased_after_self_field = true;
7426 save_gnu_tree (gnat_field, gnu_field, false);
7429 /* At the end of the component list there may be a variant part. */
7430 gnat_variant_part = Variant_Part (gnat_component_list);
7432 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7433 mutually exclusive and should go in the same memory. To do this we need
7434 to treat each variant as a record whose elements are created from the
7435 component list for the variant. So here we create the records from the
7436 lists for the variants and put them all into the QUAL_UNION_TYPE.
7437 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7438 use GNU_RECORD_TYPE if there are no fields so far. */
7439 if (Present (gnat_variant_part))
7441 Node_Id gnat_discr = Name (gnat_variant_part), variant;
7442 tree gnu_discr = gnat_to_gnu (gnat_discr);
7443 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7444 tree gnu_var_name
7445 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7446 "XVN");
7447 tree gnu_union_type, gnu_union_name;
7448 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7449 bool union_field_needs_strict_alignment = false;
7450 auto_vec <vinfo_t, 16> variant_types;
7451 vinfo_t *gnu_variant;
7452 unsigned int variants_align = 0;
7453 unsigned int i;
7455 gnu_union_name
7456 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7458 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7459 are all in the variant part, to match the layout of C unions. There
7460 is an associated check below. */
7461 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7462 gnu_union_type = gnu_record_type;
7463 else
7465 gnu_union_type
7466 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7468 TYPE_NAME (gnu_union_type) = gnu_union_name;
7469 SET_TYPE_ALIGN (gnu_union_type, 0);
7470 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7471 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7472 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7475 /* If all the fields down to this level have a rep clause, find out
7476 whether all the fields at this level also have one. If so, then
7477 compute the new first free position to be passed downward. */
7478 this_first_free_pos = first_free_pos;
7479 if (this_first_free_pos)
7481 for (gnu_field = gnu_field_list;
7482 gnu_field;
7483 gnu_field = DECL_CHAIN (gnu_field))
7484 if (DECL_FIELD_OFFSET (gnu_field))
7486 tree pos = bit_position (gnu_field);
7487 if (!tree_int_cst_lt (pos, this_first_free_pos))
7488 this_first_free_pos
7489 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7491 else
7493 this_first_free_pos = NULL_TREE;
7494 break;
7498 /* We build the variants in two passes. The bulk of the work is done in
7499 the first pass, that is to say translating the GNAT nodes, building
7500 the container types and computing the associated properties. However
7501 we cannot finish up the container types during this pass because we
7502 don't know where the variant part will be placed until the end. */
7503 for (variant = First_Non_Pragma (Variants (gnat_variant_part));
7504 Present (variant);
7505 variant = Next_Non_Pragma (variant))
7507 tree gnu_variant_type = make_node (RECORD_TYPE);
7508 tree gnu_inner_name, gnu_qual;
7509 bool has_rep;
7510 int field_packed;
7511 vinfo_t vinfo;
7513 Get_Variant_Encoding (variant);
7514 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7515 TYPE_NAME (gnu_variant_type)
7516 = concat_name (gnu_union_name,
7517 IDENTIFIER_POINTER (gnu_inner_name));
7519 /* Set the alignment of the inner type in case we need to make
7520 inner objects into bitfields, but then clear it out so the
7521 record actually gets only the alignment required. */
7522 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7523 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7524 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7525 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7527 /* Similarly, if the outer record has a size specified and all
7528 the fields have a rep clause, we can propagate the size. */
7529 if (all_rep_and_size)
7531 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7532 TYPE_SIZE_UNIT (gnu_variant_type)
7533 = TYPE_SIZE_UNIT (gnu_record_type);
7536 /* Add the fields into the record type for the variant. Note that
7537 we aren't sure to really use it at this point, see below. */
7538 has_rep
7539 = components_to_record (Component_List (variant), gnat_record_type,
7540 NULL_TREE, gnu_variant_type, packed,
7541 definition, !all_rep_and_size, all_rep,
7542 unchecked_union, true, needs_xv_encodings,
7543 true, this_first_free_pos,
7544 all_rep || this_first_free_pos
7545 ? NULL : &gnu_rep_list);
7547 /* Translate the qualifier and annotate the GNAT node. */
7548 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7549 Set_Present_Expr (variant, annotate_value (gnu_qual));
7551 /* Deal with packedness like in gnat_to_gnu_field. */
7552 if (components_need_strict_alignment (Component_List (variant)))
7554 field_packed = 0;
7555 union_field_needs_strict_alignment = true;
7557 else
7558 field_packed
7559 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7561 /* Push this variant onto the stack for the second pass. */
7562 vinfo.type = gnu_variant_type;
7563 vinfo.name = gnu_inner_name;
7564 vinfo.qual = gnu_qual;
7565 vinfo.has_rep = has_rep;
7566 vinfo.packed = field_packed;
7567 variant_types.safe_push (vinfo);
7569 /* Compute the global properties that will determine the placement of
7570 the variant part. */
7571 variants_have_rep |= has_rep;
7572 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7573 variants_align = TYPE_ALIGN (gnu_variant_type);
7576 /* Round up the first free position to the alignment of the variant part
7577 for the variants without rep clause. This will guarantee a consistent
7578 layout independently of the placement of the variant part. */
7579 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7580 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7582 /* In the second pass, the container types are adjusted if necessary and
7583 finished up, then the corresponding fields of the variant part are
7584 built with their qualifier, unless this is an unchecked union. */
7585 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7587 tree gnu_variant_type = gnu_variant->type;
7588 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7590 /* If this is an Unchecked_Union whose fields are all in the variant
7591 part and we have a single field with no representation clause or
7592 placed at offset zero, use the field directly to match the layout
7593 of C unions. */
7594 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7595 && gnu_field_list
7596 && !DECL_CHAIN (gnu_field_list)
7597 && (!DECL_FIELD_OFFSET (gnu_field_list)
7598 || integer_zerop (bit_position (gnu_field_list))))
7600 gnu_field = gnu_field_list;
7601 DECL_CONTEXT (gnu_field) = gnu_record_type;
7603 else
7605 /* Finalize the variant type now. We used to throw away empty
7606 record types but we no longer do that because we need them to
7607 generate complete debug info for the variant; otherwise, the
7608 union type definition will be lacking the fields associated
7609 with these empty variants. */
7610 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7612 /* The variant part will be at offset 0 so we need to ensure
7613 that the fields are laid out starting from the first free
7614 position at this level. */
7615 tree gnu_rep_type = make_node (RECORD_TYPE);
7616 tree gnu_rep_part;
7617 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7618 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7619 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7620 gnu_rep_part
7621 = create_rep_part (gnu_rep_type, gnu_variant_type,
7622 this_first_free_pos);
7623 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7624 gnu_field_list = gnu_rep_part;
7625 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7626 false);
7629 if (debug_info)
7630 rest_of_record_type_compilation (gnu_variant_type);
7631 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7632 true, needs_xv_encodings, gnat_component_list);
7634 gnu_field
7635 = create_field_decl (gnu_variant->name, gnu_variant_type,
7636 gnu_union_type,
7637 all_rep_and_size
7638 ? TYPE_SIZE (gnu_variant_type) : 0,
7639 variants_have_rep ? bitsize_zero_node : 0,
7640 gnu_variant->packed, 0);
7642 DECL_INTERNAL_P (gnu_field) = 1;
7644 if (!unchecked_union)
7645 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7648 DECL_CHAIN (gnu_field) = gnu_variant_list;
7649 gnu_variant_list = gnu_field;
7652 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7653 if (gnu_variant_list)
7655 int union_field_packed;
7657 if (all_rep_and_size)
7659 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7660 TYPE_SIZE_UNIT (gnu_union_type)
7661 = TYPE_SIZE_UNIT (gnu_record_type);
7664 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7665 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7667 /* If GNU_UNION_TYPE is our record type, it means we must have an
7668 Unchecked_Union with no fields. Verify that and, if so, just
7669 return. */
7670 if (gnu_union_type == gnu_record_type)
7672 gcc_assert (unchecked_union
7673 && !gnu_field_list
7674 && !gnu_rep_list);
7675 return variants_have_rep;
7678 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7679 needs_xv_encodings, gnat_component_list);
7681 /* Deal with packedness like in gnat_to_gnu_field. */
7682 if (union_field_needs_strict_alignment)
7683 union_field_packed = 0;
7684 else
7685 union_field_packed
7686 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7688 gnu_variant_part
7689 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7690 all_rep_and_size
7691 ? TYPE_SIZE (gnu_union_type) : 0,
7692 variants_have_rep ? bitsize_zero_node : 0,
7693 union_field_packed, 0);
7695 DECL_INTERNAL_P (gnu_variant_part) = 1;
7699 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do,
7700 pull them out and put them onto the appropriate list.
7702 Similarly, pull out the fields with zero size and no rep clause, as they
7703 would otherwise modify the layout and thus very likely run afoul of the
7704 Ada semantics, which are different from those of C here.
7706 Finally, if there is an aliased field placed in the list after fields
7707 with self-referential size, pull out the latter in the same way.
7709 Optionally, if the reordering mechanism is enabled, pull out the fields
7710 with self-referential size, variable size and fixed size not a multiple
7711 of a byte, so that they don't cause the regular fields to be either at
7712 self-referential/variable offset or misaligned. Note, in the latter
7713 case, that this can only happen in packed record types so the alignment
7714 is effectively capped to the byte for the whole record.
7716 Optionally, if the layout warning is enabled, keep track of the above 4
7717 different kinds of fields and issue a warning if some of them would be
7718 (or are being) reordered by the reordering mechanism.
7720 ??? If we reorder fields, the debugging information will be affected and
7721 the debugger print fields in a different order from the source code. */
7722 const bool do_reorder
7723 = (Convention (gnat_record_type) == Convention_Ada
7724 && !No_Reordering (gnat_record_type)
7725 && !debug__debug_flag_dot_r);
7726 const bool w_reorder
7727 = (Convention (gnat_record_type) == Convention_Ada
7728 && Warn_On_Questionable_Layout
7729 && !(No_Reordering (gnat_record_type) && GNAT_Mode));
7730 const bool in_variant = (p_gnu_rep_list != NULL);
7731 tree gnu_zero_list = NULL_TREE;
7732 tree gnu_self_list = NULL_TREE;
7733 tree gnu_var_list = NULL_TREE;
7734 tree gnu_bitp_list = NULL_TREE;
7735 tree gnu_tmp_bitp_list = NULL_TREE;
7736 unsigned int tmp_bitp_size = 0;
7737 unsigned int last_reorder_field_type = -1;
7738 unsigned int tmp_last_reorder_field_type = -1;
7740 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7741 do { \
7742 if (gnu_last) \
7743 DECL_CHAIN (gnu_last) = gnu_next; \
7744 else \
7745 gnu_field_list = gnu_next; \
7747 DECL_CHAIN (gnu_field) = (LIST); \
7748 (LIST) = gnu_field; \
7749 } while (0)
7751 gnu_last = NULL_TREE;
7752 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7754 gnu_next = DECL_CHAIN (gnu_field);
7756 if (DECL_FIELD_OFFSET (gnu_field))
7758 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7759 continue;
7762 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7764 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7765 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7766 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7767 if (DECL_ALIASED_P (gnu_field))
7768 SET_TYPE_ALIGN (gnu_record_type,
7769 MAX (TYPE_ALIGN (gnu_record_type),
7770 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7771 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7772 continue;
7775 if (has_aliased_after_self_field && field_has_self_size (gnu_field))
7777 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7778 continue;
7781 /* We don't need further processing in default mode. */
7782 if (!w_reorder && !do_reorder)
7784 gnu_last = gnu_field;
7785 continue;
7788 if (field_has_self_size (gnu_field))
7790 if (w_reorder)
7792 if (last_reorder_field_type < 4)
7793 warn_on_field_placement (gnu_field, gnat_component_list,
7794 gnat_record_type, in_variant,
7795 do_reorder);
7796 else
7797 last_reorder_field_type = 4;
7800 if (do_reorder)
7802 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7803 continue;
7807 else if (field_has_variable_size (gnu_field))
7809 if (w_reorder)
7811 if (last_reorder_field_type < 3)
7812 warn_on_field_placement (gnu_field, gnat_component_list,
7813 gnat_record_type, in_variant,
7814 do_reorder);
7815 else
7816 last_reorder_field_type = 3;
7819 if (do_reorder)
7821 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7822 continue;
7826 else
7828 /* If the field has no size, then it cannot be bit-packed. */
7829 const unsigned int bitp_size
7830 = DECL_SIZE (gnu_field)
7831 ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT
7832 : 0;
7834 /* If the field is bit-packed, we move it to a temporary list that
7835 contains the contiguously preceding bit-packed fields, because
7836 we want to be able to put them back if the misalignment happens
7837 to cancel itself after several bit-packed fields. */
7838 if (bitp_size != 0)
7840 tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT;
7842 if (last_reorder_field_type != 2)
7844 tmp_last_reorder_field_type = last_reorder_field_type;
7845 last_reorder_field_type = 2;
7848 if (do_reorder)
7850 MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list);
7851 continue;
7855 /* No more bit-packed fields, move the existing ones to the end or
7856 put them back at their original location. */
7857 else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list)
7859 last_reorder_field_type = 1;
7861 if (tmp_bitp_size != 0)
7863 if (w_reorder && tmp_last_reorder_field_type < 2)
7864 warn_on_field_placement (gnu_tmp_bitp_list
7865 ? gnu_tmp_bitp_list : gnu_last,
7866 gnat_component_list,
7867 gnat_record_type, in_variant,
7868 do_reorder);
7870 if (do_reorder)
7871 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7873 gnu_tmp_bitp_list = NULL_TREE;
7874 tmp_bitp_size = 0;
7876 else
7878 /* Rechain the temporary list in front of GNU_FIELD. */
7879 tree gnu_bitp_field = gnu_field;
7880 while (gnu_tmp_bitp_list)
7882 tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list);
7883 DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field;
7884 if (gnu_last)
7885 DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list;
7886 else
7887 gnu_field_list = gnu_tmp_bitp_list;
7888 gnu_bitp_field = gnu_tmp_bitp_list;
7889 gnu_tmp_bitp_list = gnu_bitp_next;
7894 else
7895 last_reorder_field_type = 1;
7898 gnu_last = gnu_field;
7901 #undef MOVE_FROM_FIELD_LIST_TO
7903 gnu_field_list = nreverse (gnu_field_list);
7905 /* If permitted, we reorder the fields as follows:
7907 1) all (groups of) fields whose length is fixed and multiple of a byte,
7908 2) the remaining fields whose length is fixed and not multiple of a byte,
7909 3) the remaining fields whose length doesn't depend on discriminants,
7910 4) all fields whose length depends on discriminants,
7911 5) the variant part,
7913 within the record and within each variant recursively. */
7915 if (w_reorder)
7917 /* If we have pending bit-packed fields, warn if they would be moved
7918 to after regular fields. */
7919 if (last_reorder_field_type == 2
7920 && tmp_bitp_size != 0
7921 && tmp_last_reorder_field_type < 2)
7922 warn_on_field_placement (gnu_tmp_bitp_list
7923 ? gnu_tmp_bitp_list : gnu_field_list,
7924 gnat_component_list, gnat_record_type,
7925 in_variant, do_reorder);
7928 if (do_reorder)
7930 /* If we have pending bit-packed fields on the temporary list, we put
7931 them either on the bit-packed list or back on the regular list. */
7932 if (gnu_tmp_bitp_list)
7934 if (tmp_bitp_size != 0)
7935 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7936 else
7937 gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list);
7940 gnu_field_list
7941 = chainon (gnu_field_list,
7942 chainon (gnu_bitp_list,
7943 chainon (gnu_var_list, gnu_self_list)));
7946 /* Otherwise, if there is an aliased field placed after a field whose length
7947 depends on discriminants, we put all the fields of the latter sort, last.
7948 We need to do this in case an object of this record type is mutable. */
7949 else if (has_aliased_after_self_field)
7950 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7952 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7953 in our REP list to the previous level because this level needs them in
7954 order to do a correct layout, i.e. avoid having overlapping fields. */
7955 if (p_gnu_rep_list && gnu_rep_list)
7956 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7958 /* Deal with the annoying case of an extension of a record with variable size
7959 and partial rep clause, for which the _Parent field is forced at offset 0
7960 and has variable size, which we do not support below. Note that we cannot
7961 do it if the field has fixed size because we rely on the presence of the
7962 REP part built below to trigger the reordering of the fields in a derived
7963 record type when all the fields have a fixed position. */
7964 else if (gnu_rep_list
7965 && !DECL_CHAIN (gnu_rep_list)
7966 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7967 && !variants_have_rep
7968 && first_free_pos
7969 && integer_zerop (first_free_pos)
7970 && integer_zerop (bit_position (gnu_rep_list)))
7972 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7973 gnu_field_list = gnu_rep_list;
7974 gnu_rep_list = NULL_TREE;
7977 /* Otherwise, sort the fields by bit position and put them into their own
7978 record, before the others, if we also have fields without rep clause. */
7979 else if (gnu_rep_list)
7981 tree gnu_rep_type, gnu_rep_part;
7982 int i, len = list_length (gnu_rep_list);
7983 tree *gnu_arr = XALLOCAVEC (tree, len);
7985 /* If all the fields have a rep clause, we can do a flat layout. */
7986 layout_with_rep = !gnu_field_list
7987 && (!gnu_variant_part || variants_have_rep);
7988 gnu_rep_type
7989 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7991 for (gnu_field = gnu_rep_list, i = 0;
7992 gnu_field;
7993 gnu_field = DECL_CHAIN (gnu_field), i++)
7994 gnu_arr[i] = gnu_field;
7996 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7998 /* Put the fields in the list in order of increasing position, which
7999 means we start from the end. */
8000 gnu_rep_list = NULL_TREE;
8001 for (i = len - 1; i >= 0; i--)
8003 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
8004 gnu_rep_list = gnu_arr[i];
8005 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
8008 if (layout_with_rep)
8009 gnu_field_list = gnu_rep_list;
8010 else
8012 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
8013 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
8014 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
8016 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
8017 without rep clause are laid out starting from this position.
8018 Therefore, we force it as a minimal size on the REP part. */
8019 gnu_rep_part
8020 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
8022 /* Chain the REP part at the beginning of the field list. */
8023 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
8024 gnu_field_list = gnu_rep_part;
8028 /* Chain the variant part at the end of the field list. */
8029 if (gnu_variant_part)
8030 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
8032 if (cancel_alignment)
8033 SET_TYPE_ALIGN (gnu_record_type, 0);
8035 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
8037 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
8038 debug_info && !maybe_unused);
8040 /* Chain the fields with zero size at the beginning of the field list. */
8041 if (gnu_zero_list)
8042 TYPE_FIELDS (gnu_record_type)
8043 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
8045 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
8048 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
8049 placed into an Esize, Component_Bit_Offset, or Component_Size value
8050 in the GNAT tree. */
8052 static Uint
8053 annotate_value (tree gnu_size)
8055 static int var_count = 0;
8056 TCode tcode;
8057 Node_Ref_Or_Val ops[3] = { No_Uint, No_Uint, No_Uint };
8058 struct tree_int_map in;
8060 /* See if we've already saved the value for this node. */
8061 if (EXPR_P (gnu_size) || DECL_P (gnu_size))
8063 struct tree_int_map *e;
8065 in.base.from = gnu_size;
8066 e = annotate_value_cache->find (&in);
8068 if (e)
8069 return (Node_Ref_Or_Val) e->to;
8071 else
8072 in.base.from = NULL_TREE;
8074 /* If we do not return inside this switch, TCODE will be set to the
8075 code to be used in a call to Create_Node. */
8076 switch (TREE_CODE (gnu_size))
8078 case INTEGER_CST:
8079 /* For negative values, build NEGATE_EXPR of the opposite. Such values
8080 can appear for discriminants in expressions for variants. */
8081 if (tree_int_cst_sgn (gnu_size) < 0)
8083 tree t = wide_int_to_tree (sizetype, wi::neg (gnu_size));
8084 tcode = Negate_Expr;
8085 ops[0] = UI_From_gnu (t);
8087 else
8088 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8089 break;
8091 case COMPONENT_REF:
8092 /* The only case we handle here is a simple discriminant reference. */
8093 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8095 tree ref = gnu_size;
8096 gnu_size = TREE_OPERAND (ref, 1);
8098 /* Climb up the chain of successive extensions, if any. */
8099 while (TREE_CODE (TREE_OPERAND (ref, 0)) == COMPONENT_REF
8100 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (ref, 0), 1))
8101 == parent_name_id)
8102 ref = TREE_OPERAND (ref, 0);
8104 if (TREE_CODE (TREE_OPERAND (ref, 0)) == PLACEHOLDER_EXPR)
8106 /* Fall through to common processing as a FIELD_DECL. */
8107 tcode = Discrim_Val;
8108 ops[0] = UI_From_gnu (DECL_DISCRIMINANT_NUMBER (gnu_size));
8110 else
8111 return No_Uint;
8113 else
8114 return No_Uint;
8115 break;
8117 case VAR_DECL:
8118 tcode = Dynamic_Val;
8119 ops[0] = UI_From_Int (++var_count);
8120 break;
8122 CASE_CONVERT:
8123 case NON_LVALUE_EXPR:
8124 return annotate_value (TREE_OPERAND (gnu_size, 0));
8126 /* Now just list the operations we handle. */
8127 case COND_EXPR: tcode = Cond_Expr; break;
8128 case MINUS_EXPR: tcode = Minus_Expr; break;
8129 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8130 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8131 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8132 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8133 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8134 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8135 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8136 case NEGATE_EXPR: tcode = Negate_Expr; break;
8137 case MIN_EXPR: tcode = Min_Expr; break;
8138 case MAX_EXPR: tcode = Max_Expr; break;
8139 case ABS_EXPR: tcode = Abs_Expr; break;
8140 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
8141 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
8142 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8143 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8144 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8145 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8146 case LT_EXPR: tcode = Lt_Expr; break;
8147 case LE_EXPR: tcode = Le_Expr; break;
8148 case GT_EXPR: tcode = Gt_Expr; break;
8149 case GE_EXPR: tcode = Ge_Expr; break;
8150 case EQ_EXPR: tcode = Eq_Expr; break;
8151 case NE_EXPR: tcode = Ne_Expr; break;
8153 case MULT_EXPR:
8154 case PLUS_EXPR:
8155 tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr);
8156 /* Fold conversions from bytes to bits into inner operations. */
8157 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST
8158 && CONVERT_EXPR_P (TREE_OPERAND (gnu_size, 0)))
8160 tree inner_op = TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 0);
8161 if (TREE_CODE (inner_op) == TREE_CODE (gnu_size)
8162 && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST)
8164 tree inner_op_op1 = TREE_OPERAND (inner_op, 1);
8165 tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1);
8166 wide_int op1;
8167 if (TREE_CODE (gnu_size) == MULT_EXPR)
8168 op1 = wi::mul (inner_op_op1, gnu_size_op1);
8169 else
8170 op1 = wi::add (inner_op_op1, gnu_size_op1);
8171 ops[1] = UI_From_gnu (wide_int_to_tree (sizetype, op1));
8172 ops[0] = annotate_value (TREE_OPERAND (inner_op, 0));
8175 break;
8177 case BIT_AND_EXPR:
8178 tcode = Bit_And_Expr;
8179 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8180 Such values appear in expressions with aligning patterns. Note that,
8181 since sizetype is unsigned, we have to jump through some hoops. */
8182 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8184 tree op1 = TREE_OPERAND (gnu_size, 1);
8185 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
8186 if (wi::neg_p (signed_op1))
8188 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
8189 ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8192 break;
8194 case CALL_EXPR:
8195 /* In regular mode, inline back only if symbolic annotation is requested
8196 in order to avoid memory explosion on big discriminated record types.
8197 But not in ASIS mode, as symbolic annotation is required for DDA. */
8198 if (List_Representation_Info == 3 || type_annotate_only)
8200 tree t = maybe_inline_call_in_expr (gnu_size);
8201 return t ? annotate_value (t) : No_Uint;
8203 else
8204 return Uint_Minus_1;
8206 default:
8207 return No_Uint;
8210 /* Now get each of the operands that's relevant for this code. If any
8211 cannot be expressed as a repinfo node, say we can't. */
8212 for (int i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8213 if (ops[i] == No_Uint)
8215 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8216 if (ops[i] == No_Uint)
8217 return No_Uint;
8220 Node_Ref_Or_Val ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8222 /* Save the result in the cache. */
8223 if (in.base.from)
8225 struct tree_int_map **h;
8226 /* We can't assume the hash table data hasn't moved since the initial
8227 look up, so we have to search again. Allocating and inserting an
8228 entry at that point would be an alternative, but then we'd better
8229 discard the entry if we decided not to cache it. */
8230 h = annotate_value_cache->find_slot (&in, INSERT);
8231 gcc_assert (!*h);
8232 *h = ggc_alloc<tree_int_map> ();
8233 (*h)->base.from = in.base.from;
8234 (*h)->to = ret;
8237 return ret;
8240 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8241 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8242 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8243 BY_REF is true if the object is used by reference. */
8245 void
8246 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8248 if (by_ref)
8250 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8251 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8252 else
8253 gnu_type = TREE_TYPE (gnu_type);
8256 if (Unknown_Esize (gnat_entity))
8258 if (TREE_CODE (gnu_type) == RECORD_TYPE
8259 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8260 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8261 else if (!size)
8262 size = TYPE_SIZE (gnu_type);
8264 if (size)
8265 Set_Esize (gnat_entity, annotate_value (size));
8268 if (Unknown_Alignment (gnat_entity))
8269 Set_Alignment (gnat_entity,
8270 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8273 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8274 Return NULL_TREE if there is no such element in the list. */
8276 static tree
8277 purpose_member_field (const_tree elem, tree list)
8279 while (list)
8281 tree field = TREE_PURPOSE (list);
8282 if (SAME_FIELD_P (field, elem))
8283 return list;
8284 list = TREE_CHAIN (list);
8286 return NULL_TREE;
8289 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8290 set Component_Bit_Offset and Esize of the components to the position and
8291 size used by Gigi. */
8293 static void
8294 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8296 /* For an extension, the inherited components have not been translated because
8297 they are fetched from the _Parent component on the fly. */
8298 const bool is_extension
8299 = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity);
8301 /* We operate by first making a list of all fields and their position (we
8302 can get the size easily) and then update all the sizes in the tree. */
8303 tree gnu_list
8304 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8305 BIGGEST_ALIGNMENT, NULL_TREE);
8307 for (Entity_Id gnat_field = First_Entity (gnat_entity);
8308 Present (gnat_field);
8309 gnat_field = Next_Entity (gnat_field))
8310 if ((Ekind (gnat_field) == E_Component
8311 && (is_extension || present_gnu_tree (gnat_field)))
8312 || (Ekind (gnat_field) == E_Discriminant
8313 && !Is_Unchecked_Union (Scope (gnat_field))))
8315 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8316 gnu_list);
8317 if (t)
8319 tree parent_offset;
8321 /* If we are just annotating types and the type is tagged, the tag
8322 and the parent components are not generated by the front-end so
8323 we need to add the appropriate offset to each component without
8324 representation clause. */
8325 if (type_annotate_only
8326 && Is_Tagged_Type (gnat_entity)
8327 && No (Component_Clause (gnat_field)))
8329 /* For a component appearing in the current extension, the
8330 offset is the size of the parent. */
8331 if (Is_Derived_Type (gnat_entity)
8332 && Original_Record_Component (gnat_field) == gnat_field)
8333 parent_offset
8334 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8335 bitsizetype);
8336 else
8337 parent_offset = bitsize_int (POINTER_SIZE);
8339 if (TYPE_FIELDS (gnu_type))
8340 parent_offset
8341 = round_up (parent_offset,
8342 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8344 else
8345 parent_offset = bitsize_zero_node;
8347 Set_Component_Bit_Offset
8348 (gnat_field,
8349 annotate_value
8350 (size_binop (PLUS_EXPR,
8351 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
8352 TREE_VEC_ELT (TREE_VALUE (t), 2)),
8353 parent_offset)));
8355 Set_Esize (gnat_field,
8356 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8358 else if (is_extension)
8360 /* If there is no entry, this is an inherited component whose
8361 position is the same as in the parent type. */
8362 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
8364 /* If we are just annotating types, discriminants renaming those of
8365 the parent have no entry so deal with them specifically. */
8366 if (type_annotate_only
8367 && gnat_orig_field == gnat_field
8368 && Ekind (gnat_field) == E_Discriminant)
8369 gnat_orig_field = Corresponding_Discriminant (gnat_field);
8371 Set_Component_Bit_Offset (gnat_field,
8372 Component_Bit_Offset (gnat_orig_field));
8374 Set_Esize (gnat_field, Esize (gnat_orig_field));
8379 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8380 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8381 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8382 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8383 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8384 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8385 pre-existing list to be chained to the newly created entries. */
8387 static tree
8388 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8389 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8391 tree gnu_field;
8393 for (gnu_field = TYPE_FIELDS (gnu_type);
8394 gnu_field;
8395 gnu_field = DECL_CHAIN (gnu_field))
8397 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8398 DECL_FIELD_BIT_OFFSET (gnu_field));
8399 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8400 DECL_FIELD_OFFSET (gnu_field));
8401 unsigned int our_offset_align
8402 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8403 tree v = make_tree_vec (3);
8405 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8406 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8407 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8408 gnu_list = tree_cons (gnu_field, v, gnu_list);
8410 /* Recurse on internal fields, flattening the nested fields except for
8411 those in the variant part, if requested. */
8412 if (DECL_INTERNAL_P (gnu_field))
8414 tree gnu_field_type = TREE_TYPE (gnu_field);
8415 if (do_not_flatten_variant
8416 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8417 gnu_list
8418 = build_position_list (gnu_field_type, do_not_flatten_variant,
8419 size_zero_node, bitsize_zero_node,
8420 BIGGEST_ALIGNMENT, gnu_list);
8421 else
8422 gnu_list
8423 = build_position_list (gnu_field_type, do_not_flatten_variant,
8424 gnu_our_offset, gnu_our_bitpos,
8425 our_offset_align, gnu_list);
8429 return gnu_list;
8432 /* Return a list describing the substitutions needed to reflect the
8433 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8434 be in any order. The values in an element of the list are in the form
8435 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8436 a definition of GNAT_SUBTYPE. */
8438 static vec<subst_pair>
8439 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8441 vec<subst_pair> gnu_list = vNULL;
8442 Entity_Id gnat_discrim;
8443 Node_Id gnat_constr;
8445 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8446 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8447 Present (gnat_discrim);
8448 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8449 gnat_constr = Next_Elmt (gnat_constr))
8450 /* Ignore access discriminants. */
8451 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8453 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8454 tree replacement = convert (TREE_TYPE (gnu_field),
8455 elaborate_expression
8456 (Node (gnat_constr), gnat_subtype,
8457 get_entity_char (gnat_discrim),
8458 definition, true, false));
8459 subst_pair s = { gnu_field, replacement };
8460 gnu_list.safe_push (s);
8463 return gnu_list;
8466 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8467 variants of QUAL_UNION_TYPE that are still relevant after applying
8468 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8469 list to be prepended to the newly created entries. */
8471 static vec<variant_desc>
8472 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8473 vec<variant_desc> gnu_list)
8475 tree gnu_field;
8477 for (gnu_field = TYPE_FIELDS (qual_union_type);
8478 gnu_field;
8479 gnu_field = DECL_CHAIN (gnu_field))
8481 tree qual = DECL_QUALIFIER (gnu_field);
8482 unsigned int i;
8483 subst_pair *s;
8485 FOR_EACH_VEC_ELT (subst_list, i, s)
8486 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8488 /* If the new qualifier is not unconditionally false, its variant may
8489 still be accessed. */
8490 if (!integer_zerop (qual))
8492 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8493 variant_desc v = { variant_type, gnu_field, qual, NULL_TREE };
8495 gnu_list.safe_push (v);
8497 /* Recurse on the variant subpart of the variant, if any. */
8498 variant_subpart = get_variant_part (variant_type);
8499 if (variant_subpart)
8500 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8501 subst_list, gnu_list);
8503 /* If the new qualifier is unconditionally true, the subsequent
8504 variants cannot be accessed. */
8505 if (integer_onep (qual))
8506 break;
8510 return gnu_list;
8513 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8514 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8515 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8516 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8517 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8518 true if we are being called to process the Component_Size of GNAT_OBJECT;
8519 this is used only for error messages. ZERO_OK is true if a size of zero
8520 is permitted; if ZERO_OK is false, it means that a size of zero should be
8521 treated as an unspecified size. */
8523 static tree
8524 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8525 enum tree_code kind, bool component_p, bool zero_ok)
8527 Node_Id gnat_error_node;
8528 tree type_size, size;
8530 /* Return 0 if no size was specified. */
8531 if (uint_size == No_Uint)
8532 return NULL_TREE;
8534 /* Ignore a negative size since that corresponds to our back-annotation. */
8535 if (UI_Lt (uint_size, Uint_0))
8536 return NULL_TREE;
8538 /* Find the node to use for error messages. */
8539 if ((Ekind (gnat_object) == E_Component
8540 || Ekind (gnat_object) == E_Discriminant)
8541 && Present (Component_Clause (gnat_object)))
8542 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8543 else if (Present (Size_Clause (gnat_object)))
8544 gnat_error_node = Expression (Size_Clause (gnat_object));
8545 else
8546 gnat_error_node = gnat_object;
8548 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8549 but cannot be represented in bitsizetype. */
8550 size = UI_To_gnu (uint_size, bitsizetype);
8551 if (TREE_OVERFLOW (size))
8553 if (component_p)
8554 post_error_ne ("component size for& is too large", gnat_error_node,
8555 gnat_object);
8556 else
8557 post_error_ne ("size for& is too large", gnat_error_node,
8558 gnat_object);
8559 return NULL_TREE;
8562 /* Ignore a zero size if it is not permitted. */
8563 if (!zero_ok && integer_zerop (size))
8564 return NULL_TREE;
8566 /* The size of objects is always a multiple of a byte. */
8567 if (kind == VAR_DECL
8568 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8570 if (component_p)
8571 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8572 gnat_error_node, gnat_object);
8573 else
8574 post_error_ne ("size for& is not a multiple of Storage_Unit",
8575 gnat_error_node, gnat_object);
8576 return NULL_TREE;
8579 /* If this is an integral type or a packed array type, the front-end has
8580 already verified the size, so we need not do it here (which would mean
8581 checking against the bounds). However, if this is an aliased object,
8582 it may not be smaller than the type of the object. */
8583 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8584 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8585 return size;
8587 /* If the object is a record that contains a template, add the size of the
8588 template to the specified size. */
8589 if (TREE_CODE (gnu_type) == RECORD_TYPE
8590 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8591 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8593 if (kind == VAR_DECL
8594 /* If a type needs strict alignment, a component of this type in
8595 a packed record cannot be packed and thus uses the type size. */
8596 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8597 type_size = TYPE_SIZE (gnu_type);
8598 else
8599 type_size = rm_size (gnu_type);
8601 /* Modify the size of a discriminated type to be the maximum size. */
8602 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8603 type_size = max_size (type_size, true);
8605 /* If this is an access type or a fat pointer, the minimum size is that given
8606 by the smallest integral mode that's valid for pointers. */
8607 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8609 scalar_int_mode p_mode = NARROWEST_INT_MODE;
8610 while (!targetm.valid_pointer_mode (p_mode))
8611 p_mode = GET_MODE_WIDER_MODE (p_mode).require ();
8612 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8615 /* Issue an error either if the default size of the object isn't a constant
8616 or if the new size is smaller than it. */
8617 if (TREE_CODE (type_size) != INTEGER_CST
8618 || TREE_OVERFLOW (type_size)
8619 || tree_int_cst_lt (size, type_size))
8621 if (component_p)
8622 post_error_ne_tree
8623 ("component size for& too small{, minimum allowed is ^}",
8624 gnat_error_node, gnat_object, type_size);
8625 else
8626 post_error_ne_tree
8627 ("size for& too small{, minimum allowed is ^}",
8628 gnat_error_node, gnat_object, type_size);
8629 return NULL_TREE;
8632 return size;
8635 /* Similarly, but both validate and process a value of RM size. This routine
8636 is only called for types. */
8638 static void
8639 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8641 Node_Id gnat_attr_node;
8642 tree old_size, size;
8644 /* Do nothing if no size was specified. */
8645 if (uint_size == No_Uint)
8646 return;
8648 /* Ignore a negative size since that corresponds to our back-annotation. */
8649 if (UI_Lt (uint_size, Uint_0))
8650 return;
8652 /* Only issue an error if a Value_Size clause was explicitly given.
8653 Otherwise, we'd be duplicating an error on the Size clause. */
8654 gnat_attr_node
8655 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8657 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8658 but cannot be represented in bitsizetype. */
8659 size = UI_To_gnu (uint_size, bitsizetype);
8660 if (TREE_OVERFLOW (size))
8662 if (Present (gnat_attr_node))
8663 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8664 gnat_entity);
8665 return;
8668 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8669 exists, or this is an integer type, in which case the front-end will
8670 have always set it. */
8671 if (No (gnat_attr_node)
8672 && integer_zerop (size)
8673 && !Has_Size_Clause (gnat_entity)
8674 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8675 return;
8677 old_size = rm_size (gnu_type);
8679 /* If the old size is self-referential, get the maximum size. */
8680 if (CONTAINS_PLACEHOLDER_P (old_size))
8681 old_size = max_size (old_size, true);
8683 /* Issue an error either if the old size of the object isn't a constant or
8684 if the new size is smaller than it. The front-end has already verified
8685 this for scalar and packed array types. */
8686 if (TREE_CODE (old_size) != INTEGER_CST
8687 || TREE_OVERFLOW (old_size)
8688 || (AGGREGATE_TYPE_P (gnu_type)
8689 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8690 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8691 && !(TYPE_IS_PADDING_P (gnu_type)
8692 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8693 && TYPE_PACKED_ARRAY_TYPE_P
8694 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8695 && tree_int_cst_lt (size, old_size)))
8697 if (Present (gnat_attr_node))
8698 post_error_ne_tree
8699 ("Value_Size for& too small{, minimum allowed is ^}",
8700 gnat_attr_node, gnat_entity, old_size);
8701 return;
8704 /* Otherwise, set the RM size proper for integral types... */
8705 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8706 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8707 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8708 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8709 SET_TYPE_RM_SIZE (gnu_type, size);
8711 /* ...or the Ada size for record and union types. */
8712 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8713 && !TYPE_FAT_POINTER_P (gnu_type))
8714 SET_TYPE_ADA_SIZE (gnu_type, size);
8717 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8718 a type or object whose present alignment is ALIGN. If this alignment is
8719 valid, return it. Otherwise, give an error and return ALIGN. */
8721 static unsigned int
8722 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8724 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8725 unsigned int new_align;
8726 Node_Id gnat_error_node;
8728 /* Don't worry about checking alignment if alignment was not specified
8729 by the source program and we already posted an error for this entity. */
8730 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8731 return align;
8733 /* Post the error on the alignment clause if any. Note, for the implicit
8734 base type of an array type, the alignment clause is on the first
8735 subtype. */
8736 if (Present (Alignment_Clause (gnat_entity)))
8737 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8739 else if (Is_Itype (gnat_entity)
8740 && Is_Array_Type (gnat_entity)
8741 && Etype (gnat_entity) == gnat_entity
8742 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8743 gnat_error_node =
8744 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8746 else
8747 gnat_error_node = gnat_entity;
8749 /* Within GCC, an alignment is an integer, so we must make sure a value is
8750 specified that fits in that range. Also, there is an upper bound to
8751 alignments we can support/allow. */
8752 if (!UI_Is_In_Int_Range (alignment)
8753 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8754 post_error_ne_num ("largest supported alignment for& is ^",
8755 gnat_error_node, gnat_entity, max_allowed_alignment);
8756 else if (!(Present (Alignment_Clause (gnat_entity))
8757 && From_At_Mod (Alignment_Clause (gnat_entity)))
8758 && new_align * BITS_PER_UNIT < align)
8760 unsigned int double_align;
8761 bool is_capped_double, align_clause;
8763 /* If the default alignment of "double" or larger scalar types is
8764 specifically capped and the new alignment is above the cap, do
8765 not post an error and change the alignment only if there is an
8766 alignment clause; this makes it possible to have the associated
8767 GCC type overaligned by default for performance reasons. */
8768 if ((double_align = double_float_alignment) > 0)
8770 Entity_Id gnat_type
8771 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8772 is_capped_double
8773 = is_double_float_or_array (gnat_type, &align_clause);
8775 else if ((double_align = double_scalar_alignment) > 0)
8777 Entity_Id gnat_type
8778 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8779 is_capped_double
8780 = is_double_scalar_or_array (gnat_type, &align_clause);
8782 else
8783 is_capped_double = align_clause = false;
8785 if (is_capped_double && new_align >= double_align)
8787 if (align_clause)
8788 align = new_align * BITS_PER_UNIT;
8790 else
8792 if (is_capped_double)
8793 align = double_align * BITS_PER_UNIT;
8795 post_error_ne_num ("alignment for& must be at least ^",
8796 gnat_error_node, gnat_entity,
8797 align / BITS_PER_UNIT);
8800 else
8802 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8803 if (new_align > align)
8804 align = new_align;
8807 return align;
8810 /* Verify that TYPE is something we can implement atomically. If not, issue
8811 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8812 process a component type. */
8814 static void
8815 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8817 Node_Id gnat_error_point = gnat_entity;
8818 Node_Id gnat_node;
8819 machine_mode mode;
8820 enum mode_class mclass;
8821 unsigned int align;
8822 tree size;
8824 /* If this is an anonymous base type, nothing to check, the error will be
8825 reported on the source type if need be. */
8826 if (!Comes_From_Source (gnat_entity))
8827 return;
8829 mode = TYPE_MODE (type);
8830 mclass = GET_MODE_CLASS (mode);
8831 align = TYPE_ALIGN (type);
8832 size = TYPE_SIZE (type);
8834 /* Consider all aligned floating-point types atomic and any aligned types
8835 that are represented by integers no wider than a machine word. */
8836 scalar_int_mode int_mode;
8837 if ((mclass == MODE_FLOAT
8838 || (is_a <scalar_int_mode> (mode, &int_mode)
8839 && GET_MODE_BITSIZE (int_mode) <= BITS_PER_WORD))
8840 && align >= GET_MODE_ALIGNMENT (mode))
8841 return;
8843 /* For the moment, also allow anything that has an alignment equal to its
8844 size and which is smaller than a word. */
8845 if (size
8846 && TREE_CODE (size) == INTEGER_CST
8847 && compare_tree_int (size, align) == 0
8848 && align <= BITS_PER_WORD)
8849 return;
8851 for (gnat_node = First_Rep_Item (gnat_entity);
8852 Present (gnat_node);
8853 gnat_node = Next_Rep_Item (gnat_node))
8854 if (Nkind (gnat_node) == N_Pragma)
8856 unsigned char pragma_id
8857 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8859 if ((pragma_id == Pragma_Atomic && !component_p)
8860 || (pragma_id == Pragma_Atomic_Components && component_p))
8862 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8863 break;
8867 if (component_p)
8868 post_error_ne ("atomic access to component of & cannot be guaranteed",
8869 gnat_error_point, gnat_entity);
8870 else if (Is_Volatile_Full_Access (gnat_entity))
8871 post_error_ne ("volatile full access to & cannot be guaranteed",
8872 gnat_error_point, gnat_entity);
8873 else
8874 post_error_ne ("atomic access to & cannot be guaranteed",
8875 gnat_error_point, gnat_entity);
8879 /* Helper for the intrin compatibility checks family. Evaluate whether
8880 two types are definitely incompatible. */
8882 static bool
8883 intrin_types_incompatible_p (tree t1, tree t2)
8885 enum tree_code code;
8887 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8888 return false;
8890 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8891 return true;
8893 if (TREE_CODE (t1) != TREE_CODE (t2))
8894 return true;
8896 code = TREE_CODE (t1);
8898 switch (code)
8900 case INTEGER_TYPE:
8901 case REAL_TYPE:
8902 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8904 case POINTER_TYPE:
8905 case REFERENCE_TYPE:
8906 /* Assume designated types are ok. We'd need to account for char * and
8907 void * variants to do better, which could rapidly get messy and isn't
8908 clearly worth the effort. */
8909 return false;
8911 default:
8912 break;
8915 return false;
8918 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8919 on the Ada/builtin argument lists for the INB binding. */
8921 static bool
8922 intrin_arglists_compatible_p (intrin_binding_t * inb)
8924 function_args_iterator ada_iter, btin_iter;
8926 function_args_iter_init (&ada_iter, inb->ada_fntype);
8927 function_args_iter_init (&btin_iter, inb->btin_fntype);
8929 /* Sequence position of the last argument we checked. */
8930 int argpos = 0;
8932 while (true)
8934 tree ada_type = function_args_iter_cond (&ada_iter);
8935 tree btin_type = function_args_iter_cond (&btin_iter);
8937 /* If we've exhausted both lists simultaneously, we're done. */
8938 if (!ada_type && !btin_type)
8939 break;
8941 /* If one list is shorter than the other, they fail to match. */
8942 if (!ada_type || !btin_type)
8943 return false;
8945 /* If we're done with the Ada args and not with the internal builtin
8946 args, or the other way around, complain. */
8947 if (ada_type == void_type_node
8948 && btin_type != void_type_node)
8950 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8951 return false;
8954 if (btin_type == void_type_node
8955 && ada_type != void_type_node)
8957 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8958 inb->gnat_entity, inb->gnat_entity, argpos);
8959 return false;
8962 /* Otherwise, check that types match for the current argument. */
8963 argpos ++;
8964 if (intrin_types_incompatible_p (ada_type, btin_type))
8966 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8967 inb->gnat_entity, inb->gnat_entity, argpos);
8968 return false;
8972 function_args_iter_next (&ada_iter);
8973 function_args_iter_next (&btin_iter);
8976 return true;
8979 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8980 on the Ada/builtin return values for the INB binding. */
8982 static bool
8983 intrin_return_compatible_p (intrin_binding_t * inb)
8985 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8986 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8988 /* Accept function imported as procedure, common and convenient. */
8989 if (VOID_TYPE_P (ada_return_type)
8990 && !VOID_TYPE_P (btin_return_type))
8991 return true;
8993 /* Check return types compatibility otherwise. Note that this
8994 handles void/void as well. */
8995 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8997 post_error ("?intrinsic binding type mismatch on return value!",
8998 inb->gnat_entity);
8999 return false;
9002 return true;
9005 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
9006 compatible. Issue relevant warnings when they are not.
9008 This is intended as a light check to diagnose the most obvious cases, not
9009 as a full fledged type compatibility predicate. It is the programmer's
9010 responsibility to ensure correctness of the Ada declarations in Imports,
9011 especially when binding straight to a compiler internal. */
9013 static bool
9014 intrin_profiles_compatible_p (intrin_binding_t * inb)
9016 /* Check compatibility on return values and argument lists, each responsible
9017 for posting warnings as appropriate. Ensure use of the proper sloc for
9018 this purpose. */
9020 bool arglists_compatible_p, return_compatible_p;
9021 location_t saved_location = input_location;
9023 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
9025 return_compatible_p = intrin_return_compatible_p (inb);
9026 arglists_compatible_p = intrin_arglists_compatible_p (inb);
9028 input_location = saved_location;
9030 return return_compatible_p && arglists_compatible_p;
9033 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
9034 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
9035 specified size for this field. POS_LIST is a position list describing
9036 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
9037 to this layout. */
9039 static tree
9040 create_field_decl_from (tree old_field, tree field_type, tree record_type,
9041 tree size, tree pos_list,
9042 vec<subst_pair> subst_list)
9044 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
9045 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
9046 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
9047 tree new_pos, new_field;
9048 unsigned int i;
9049 subst_pair *s;
9051 if (CONTAINS_PLACEHOLDER_P (pos))
9052 FOR_EACH_VEC_ELT (subst_list, i, s)
9053 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
9055 /* If the position is now a constant, we can set it as the position of the
9056 field when we make it. Otherwise, we need to deal with it specially. */
9057 if (TREE_CONSTANT (pos))
9058 new_pos = bit_from_pos (pos, bitpos);
9059 else
9060 new_pos = NULL_TREE;
9062 new_field
9063 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
9064 size, new_pos, DECL_PACKED (old_field),
9065 !DECL_NONADDRESSABLE_P (old_field));
9067 if (!new_pos)
9069 normalize_offset (&pos, &bitpos, offset_align);
9070 /* Finalize the position. */
9071 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
9072 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
9073 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
9074 DECL_SIZE (new_field) = size;
9075 DECL_SIZE_UNIT (new_field)
9076 = convert (sizetype,
9077 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
9078 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
9081 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
9082 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
9083 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
9084 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
9086 return new_field;
9089 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
9090 it is the minimal size the REP_PART must have. */
9092 static tree
9093 create_rep_part (tree rep_type, tree record_type, tree min_size)
9095 tree field;
9097 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
9098 min_size = NULL_TREE;
9100 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
9101 min_size, NULL_TREE, 0, 1);
9102 DECL_INTERNAL_P (field) = 1;
9104 return field;
9107 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9109 static tree
9110 get_rep_part (tree record_type)
9112 tree field = TYPE_FIELDS (record_type);
9114 /* The REP part is the first field, internal, another record, and its name
9115 starts with an 'R'. */
9116 if (field
9117 && DECL_INTERNAL_P (field)
9118 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9119 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9120 return field;
9122 return NULL_TREE;
9125 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9127 tree
9128 get_variant_part (tree record_type)
9130 tree field;
9132 /* The variant part is the only internal field that is a qualified union. */
9133 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9134 if (DECL_INTERNAL_P (field)
9135 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9136 return field;
9138 return NULL_TREE;
9141 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9142 the list of variants to be used and RECORD_TYPE is the type of the parent.
9143 POS_LIST is a position list describing the layout of fields present in
9144 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9145 layout. DEBUG_INFO_P is true if we need to write debug information. */
9147 static tree
9148 create_variant_part_from (tree old_variant_part,
9149 vec<variant_desc> variant_list,
9150 tree record_type, tree pos_list,
9151 vec<subst_pair> subst_list,
9152 bool debug_info_p)
9154 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9155 tree old_union_type = TREE_TYPE (old_variant_part);
9156 tree new_union_type, new_variant_part;
9157 tree union_field_list = NULL_TREE;
9158 variant_desc *v;
9159 unsigned int i;
9161 /* First create the type of the variant part from that of the old one. */
9162 new_union_type = make_node (QUAL_UNION_TYPE);
9163 TYPE_NAME (new_union_type)
9164 = concat_name (TYPE_NAME (record_type),
9165 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9167 /* If the position of the variant part is constant, subtract it from the
9168 size of the type of the parent to get the new size. This manual CSE
9169 reduces the code size when not optimizing. */
9170 if (TREE_CODE (offset) == INTEGER_CST
9171 && TYPE_SIZE (record_type)
9172 && TYPE_SIZE_UNIT (record_type))
9174 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9175 tree first_bit = bit_from_pos (offset, bitpos);
9176 TYPE_SIZE (new_union_type)
9177 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9178 TYPE_SIZE_UNIT (new_union_type)
9179 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9180 byte_from_pos (offset, bitpos));
9181 SET_TYPE_ADA_SIZE (new_union_type,
9182 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9183 first_bit));
9184 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9185 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9187 else
9188 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9190 /* Now finish up the new variants and populate the union type. */
9191 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9193 tree old_field = v->field, new_field;
9194 tree old_variant, old_variant_subpart, new_variant, field_list;
9196 /* Skip variants that don't belong to this nesting level. */
9197 if (DECL_CONTEXT (old_field) != old_union_type)
9198 continue;
9200 /* Retrieve the list of fields already added to the new variant. */
9201 new_variant = v->new_type;
9202 field_list = TYPE_FIELDS (new_variant);
9204 /* If the old variant had a variant subpart, we need to create a new
9205 variant subpart and add it to the field list. */
9206 old_variant = v->type;
9207 old_variant_subpart = get_variant_part (old_variant);
9208 if (old_variant_subpart)
9210 tree new_variant_subpart
9211 = create_variant_part_from (old_variant_subpart, variant_list,
9212 new_variant, pos_list, subst_list,
9213 debug_info_p);
9214 DECL_CHAIN (new_variant_subpart) = field_list;
9215 field_list = new_variant_subpart;
9218 /* Finish up the new variant and create the field. */
9219 finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p);
9220 compute_record_mode (new_variant);
9221 create_type_decl (TYPE_NAME (new_variant), new_variant, true,
9222 debug_info_p, Empty);
9224 new_field
9225 = create_field_decl_from (old_field, new_variant, new_union_type,
9226 TYPE_SIZE (new_variant),
9227 pos_list, subst_list);
9228 DECL_QUALIFIER (new_field) = v->qual;
9229 DECL_INTERNAL_P (new_field) = 1;
9230 DECL_CHAIN (new_field) = union_field_list;
9231 union_field_list = new_field;
9234 /* Finish up the union type and create the variant part. Note that we don't
9235 reverse the field list because VARIANT_LIST has been traversed in reverse
9236 order. */
9237 finish_record_type (new_union_type, union_field_list, 2, debug_info_p);
9238 compute_record_mode (new_union_type);
9239 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true,
9240 debug_info_p, Empty);
9242 new_variant_part
9243 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9244 TYPE_SIZE (new_union_type),
9245 pos_list, subst_list);
9246 DECL_INTERNAL_P (new_variant_part) = 1;
9248 /* With multiple discriminants it is possible for an inner variant to be
9249 statically selected while outer ones are not; in this case, the list
9250 of fields of the inner variant is not flattened and we end up with a
9251 qualified union with a single member. Drop the useless container. */
9252 if (!DECL_CHAIN (union_field_list))
9254 DECL_CONTEXT (union_field_list) = record_type;
9255 DECL_FIELD_OFFSET (union_field_list)
9256 = DECL_FIELD_OFFSET (new_variant_part);
9257 DECL_FIELD_BIT_OFFSET (union_field_list)
9258 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9259 SET_DECL_OFFSET_ALIGN (union_field_list,
9260 DECL_OFFSET_ALIGN (new_variant_part));
9261 new_variant_part = union_field_list;
9264 return new_variant_part;
9267 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9268 which are both RECORD_TYPE, after applying the substitutions described
9269 in SUBST_LIST. */
9271 static void
9272 copy_and_substitute_in_size (tree new_type, tree old_type,
9273 vec<subst_pair> subst_list)
9275 unsigned int i;
9276 subst_pair *s;
9278 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9279 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9280 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9281 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9282 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9284 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9285 FOR_EACH_VEC_ELT (subst_list, i, s)
9286 TYPE_SIZE (new_type)
9287 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9288 s->discriminant, s->replacement);
9290 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9291 FOR_EACH_VEC_ELT (subst_list, i, s)
9292 TYPE_SIZE_UNIT (new_type)
9293 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9294 s->discriminant, s->replacement);
9296 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9297 FOR_EACH_VEC_ELT (subst_list, i, s)
9298 SET_TYPE_ADA_SIZE
9299 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9300 s->discriminant, s->replacement));
9302 /* Finalize the size. */
9303 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9304 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9307 /* Return true if DISC is a stored discriminant of RECORD_TYPE. */
9309 static inline bool
9310 is_stored_discriminant (Entity_Id discr, Entity_Id record_type)
9312 if (Is_Tagged_Type (record_type))
9313 return No (Corresponding_Discriminant (discr));
9314 else if (Ekind (record_type) == E_Record_Type)
9315 return Original_Record_Component (discr) == discr;
9316 else
9317 return true;
9320 /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are
9321 both record types, after applying the substitutions described in SUBST_LIST.
9322 DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */
9324 static void
9325 copy_and_substitute_in_layout (Entity_Id gnat_new_type,
9326 Entity_Id gnat_old_type,
9327 tree gnu_new_type,
9328 tree gnu_old_type,
9329 vec<subst_pair> gnu_subst_list,
9330 bool debug_info_p)
9332 const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype);
9333 tree gnu_field_list = NULL_TREE;
9334 bool selected_variant, all_constant_pos = true;
9335 vec<variant_desc> gnu_variant_list;
9337 /* Look for REP and variant parts in the old type. */
9338 tree gnu_rep_part = get_rep_part (gnu_old_type);
9339 tree gnu_variant_part = get_variant_part (gnu_old_type);
9341 /* If there is a variant part, we must compute whether the constraints
9342 statically select a particular variant. If so, we simply drop the
9343 qualified union and flatten the list of fields. Otherwise we will
9344 build a new qualified union for the variants that are still relevant. */
9345 if (gnu_variant_part)
9347 variant_desc *v;
9348 unsigned int i;
9350 gnu_variant_list = build_variant_list (TREE_TYPE (gnu_variant_part),
9351 gnu_subst_list, vNULL);
9353 /* If all the qualifiers are unconditionally true, the innermost variant
9354 is statically selected. */
9355 selected_variant = true;
9356 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9357 if (!integer_onep (v->qual))
9359 selected_variant = false;
9360 break;
9363 /* Otherwise, create the new variants. */
9364 if (!selected_variant)
9365 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9367 tree old_variant = v->type;
9368 tree new_variant = make_node (RECORD_TYPE);
9369 tree suffix
9370 = concat_name (DECL_NAME (gnu_variant_part),
9371 IDENTIFIER_POINTER (DECL_NAME (v->field)));
9372 TYPE_NAME (new_variant)
9373 = concat_name (TYPE_NAME (gnu_new_type),
9374 IDENTIFIER_POINTER (suffix));
9375 TYPE_REVERSE_STORAGE_ORDER (new_variant)
9376 = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type);
9377 copy_and_substitute_in_size (new_variant, old_variant,
9378 gnu_subst_list);
9379 v->new_type = new_variant;
9382 else
9384 gnu_variant_list.create (0);
9385 selected_variant = false;
9388 /* Make a list of fields and their position in the old type. */
9389 tree gnu_pos_list
9390 = build_position_list (gnu_old_type,
9391 gnu_variant_list.exists () && !selected_variant,
9392 size_zero_node, bitsize_zero_node,
9393 BIGGEST_ALIGNMENT, NULL_TREE);
9395 /* Now go down every component in the new type and compute its size and
9396 position from those of the component in the old type and the stored
9397 constraints of the new type. */
9398 Entity_Id gnat_field, gnat_old_field;
9399 for (gnat_field = First_Entity (gnat_new_type);
9400 Present (gnat_field);
9401 gnat_field = Next_Entity (gnat_field))
9402 if ((Ekind (gnat_field) == E_Component
9403 || (Ekind (gnat_field) == E_Discriminant
9404 && is_stored_discriminant (gnat_field, gnat_new_type)))
9405 && (gnat_old_field = is_subtype
9406 ? Original_Record_Component (gnat_field)
9407 : Corresponding_Record_Component (gnat_field))
9408 && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type
9409 && present_gnu_tree (gnat_old_field))
9411 Name_Id gnat_name = Chars (gnat_field);
9412 tree gnu_old_field = get_gnu_tree (gnat_old_field);
9413 if (TREE_CODE (gnu_old_field) == COMPONENT_REF)
9414 gnu_old_field = TREE_OPERAND (gnu_old_field, 1);
9415 tree gnu_context = DECL_CONTEXT (gnu_old_field);
9416 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
9417 tree gnu_cont_type, gnu_last = NULL_TREE;
9419 /* If the type is the same, retrieve the GCC type from the
9420 old field to take into account possible adjustments. */
9421 if (Etype (gnat_field) == Etype (gnat_old_field))
9422 gnu_field_type = TREE_TYPE (gnu_old_field);
9423 else
9424 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
9426 /* If there was a component clause, the field types must be the same
9427 for the old and new types, so copy the data from the old field to
9428 avoid recomputation here. Also if the field is justified modular
9429 and the optimization in gnat_to_gnu_field was applied. */
9430 if (Present (Component_Clause (gnat_old_field))
9431 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
9432 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
9433 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
9434 == TREE_TYPE (gnu_old_field)))
9436 gnu_size = DECL_SIZE (gnu_old_field);
9437 gnu_field_type = TREE_TYPE (gnu_old_field);
9440 /* If the old field was packed and of constant size, we have to get the
9441 old size here as it might differ from what the Etype conveys and the
9442 latter might overlap with the following field. Try to arrange the
9443 type for possible better packing along the way. */
9444 else if (DECL_PACKED (gnu_old_field)
9445 && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST)
9447 gnu_size = DECL_SIZE (gnu_old_field);
9448 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
9449 && !TYPE_FAT_POINTER_P (gnu_field_type)
9450 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
9451 gnu_field_type = make_packable_type (gnu_field_type, true);
9454 else
9455 gnu_size = TYPE_SIZE (gnu_field_type);
9457 /* If the context of the old field is the old type or its REP part,
9458 put the field directly in the new type; otherwise look up the
9459 context in the variant list and put the field either in the new
9460 type if there is a selected variant or in one new variant. */
9461 if (gnu_context == gnu_old_type
9462 || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part)))
9463 gnu_cont_type = gnu_new_type;
9464 else
9466 variant_desc *v;
9467 unsigned int i;
9468 tree rep_part;
9470 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9471 if (gnu_context == v->type
9472 || ((rep_part = get_rep_part (v->type))
9473 && gnu_context == TREE_TYPE (rep_part)))
9474 break;
9476 if (v)
9477 gnu_cont_type = selected_variant ? gnu_new_type : v->new_type;
9478 else
9479 /* The front-end may pass us "ghost" components if it fails to
9480 recognize that a constrain statically selects a particular
9481 variant. Discard them. */
9482 continue;
9485 /* Now create the new field modeled on the old one. */
9486 gnu_field
9487 = create_field_decl_from (gnu_old_field, gnu_field_type,
9488 gnu_cont_type, gnu_size,
9489 gnu_pos_list, gnu_subst_list);
9490 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
9492 /* If the context is a variant, put it in the new variant directly. */
9493 if (gnu_cont_type != gnu_new_type)
9495 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
9496 TYPE_FIELDS (gnu_cont_type) = gnu_field;
9499 /* To match the layout crafted in components_to_record, if this is
9500 the _Tag or _Parent field, put it before any other fields. */
9501 else if (gnat_name == Name_uTag || gnat_name == Name_uParent)
9502 gnu_field_list = chainon (gnu_field_list, gnu_field);
9504 /* Similarly, if this is the _Controller field, put it before the
9505 other fields except for the _Tag or _Parent field. */
9506 else if (gnat_name == Name_uController && gnu_last)
9508 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
9509 DECL_CHAIN (gnu_last) = gnu_field;
9512 /* Otherwise, put it after the other fields. */
9513 else
9515 DECL_CHAIN (gnu_field) = gnu_field_list;
9516 gnu_field_list = gnu_field;
9517 if (!gnu_last)
9518 gnu_last = gnu_field;
9519 if (TREE_CODE (gnu_pos) != INTEGER_CST)
9520 all_constant_pos = false;
9523 /* For a stored discriminant in a derived type, replace the field. */
9524 if (!is_subtype && Ekind (gnat_field) == E_Discriminant)
9526 tree gnu_ref = get_gnu_tree (gnat_field);
9527 TREE_OPERAND (gnu_ref, 1) = gnu_field;
9529 else
9530 save_gnu_tree (gnat_field, gnu_field, false);
9533 /* If there is no variant list or a selected variant and the fields all have
9534 constant position, put them in order of increasing position to match that
9535 of constant CONSTRUCTORs. */
9536 if ((!gnu_variant_list.exists () || selected_variant) && all_constant_pos)
9538 const int len = list_length (gnu_field_list);
9539 tree *field_arr = XALLOCAVEC (tree, len), t = gnu_field_list;
9541 for (int i = 0; t; t = DECL_CHAIN (t), i++)
9542 field_arr[i] = t;
9544 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
9546 gnu_field_list = NULL_TREE;
9547 for (int i = 0; i < len; i++)
9549 DECL_CHAIN (field_arr[i]) = gnu_field_list;
9550 gnu_field_list = field_arr[i];
9554 /* If there is a variant list and no selected variant, we need to create the
9555 nest of variant parts from the old nest. */
9556 else if (gnu_variant_list.exists () && !selected_variant)
9558 tree new_variant_part
9559 = create_variant_part_from (gnu_variant_part, gnu_variant_list,
9560 gnu_new_type, gnu_pos_list,
9561 gnu_subst_list, debug_info_p);
9562 DECL_CHAIN (new_variant_part) = gnu_field_list;
9563 gnu_field_list = new_variant_part;
9566 gnu_variant_list.release ();
9567 gnu_subst_list.release ();
9569 gnu_field_list = nreverse (gnu_field_list);
9571 /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE.
9572 Otherwise sizes and alignment must be computed independently. */
9573 if (is_subtype)
9575 finish_record_type (gnu_new_type, gnu_field_list, 2, debug_info_p);
9576 compute_record_mode (gnu_new_type);
9578 else
9579 finish_record_type (gnu_new_type, gnu_field_list, 1, debug_info_p);
9581 /* Now go through the entities again looking for Itypes that we have not yet
9582 elaborated (e.g. Etypes of fields that have Original_Components). */
9583 for (Entity_Id gnat_field = First_Entity (gnat_new_type);
9584 Present (gnat_field);
9585 gnat_field = Next_Entity (gnat_field))
9586 if ((Ekind (gnat_field) == E_Component
9587 || Ekind (gnat_field) == E_Discriminant)
9588 && Is_Itype (Etype (gnat_field))
9589 && !present_gnu_tree (Etype (gnat_field)))
9590 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
9593 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9594 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9595 the original array type if it has been translated. This association is a
9596 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9597 that for standard DWARF, we also want to get the original type name. */
9599 static void
9600 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9602 Entity_Id gnat_original_array_type
9603 = Underlying_Type (Original_Array_Type (gnat_entity));
9604 tree gnu_original_array_type;
9606 if (!present_gnu_tree (gnat_original_array_type))
9607 return;
9609 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9611 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9612 return;
9614 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9616 tree original_name = TYPE_NAME (gnu_original_array_type);
9618 if (TREE_CODE (original_name) == TYPE_DECL)
9619 original_name = DECL_NAME (original_name);
9621 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9622 TYPE_NAME (gnu_type) = original_name;
9624 else
9625 add_parallel_type (gnu_type, gnu_original_array_type);
9628 /* Given a type T, a FIELD_DECL F, and a replacement value R, return an
9629 equivalent type with adjusted size expressions where all occurrences
9630 of references to F in a PLACEHOLDER_EXPR have been replaced by R.
9632 The function doesn't update the layout of the type, i.e. it assumes
9633 that the substitution is purely formal. That's why the replacement
9634 value R must itself contain a PLACEHOLDER_EXPR. */
9636 tree
9637 substitute_in_type (tree t, tree f, tree r)
9639 tree nt;
9641 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9643 switch (TREE_CODE (t))
9645 case INTEGER_TYPE:
9646 case ENUMERAL_TYPE:
9647 case BOOLEAN_TYPE:
9648 case REAL_TYPE:
9650 /* First the domain types of arrays. */
9651 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9652 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9654 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9655 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9657 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9658 return t;
9660 nt = copy_type (t);
9661 TYPE_GCC_MIN_VALUE (nt) = low;
9662 TYPE_GCC_MAX_VALUE (nt) = high;
9664 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9665 SET_TYPE_INDEX_TYPE
9666 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9668 return nt;
9671 /* Then the subtypes. */
9672 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9673 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9675 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9676 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9678 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9679 return t;
9681 nt = copy_type (t);
9682 SET_TYPE_RM_MIN_VALUE (nt, low);
9683 SET_TYPE_RM_MAX_VALUE (nt, high);
9685 return nt;
9688 return t;
9690 case COMPLEX_TYPE:
9691 nt = substitute_in_type (TREE_TYPE (t), f, r);
9692 if (nt == TREE_TYPE (t))
9693 return t;
9695 return build_complex_type (nt);
9697 case FUNCTION_TYPE:
9698 /* These should never show up here. */
9699 gcc_unreachable ();
9701 case ARRAY_TYPE:
9703 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9704 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9706 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9707 return t;
9709 nt = build_nonshared_array_type (component, domain);
9710 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9711 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9712 SET_TYPE_MODE (nt, TYPE_MODE (t));
9713 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9714 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9715 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9716 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9717 if (TYPE_REVERSE_STORAGE_ORDER (t))
9718 set_reverse_storage_order_on_array_type (nt);
9719 if (TYPE_NONALIASED_COMPONENT (t))
9720 set_nonaliased_component_on_array_type (nt);
9721 return nt;
9724 case RECORD_TYPE:
9725 case UNION_TYPE:
9726 case QUAL_UNION_TYPE:
9728 bool changed_field = false;
9729 tree field;
9731 /* Start out with no fields, make new fields, and chain them
9732 in. If we haven't actually changed the type of any field,
9733 discard everything we've done and return the old type. */
9734 nt = copy_type (t);
9735 TYPE_FIELDS (nt) = NULL_TREE;
9737 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9739 tree new_field = copy_node (field), new_n;
9741 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9742 if (new_n != TREE_TYPE (field))
9744 TREE_TYPE (new_field) = new_n;
9745 changed_field = true;
9748 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9749 if (new_n != DECL_FIELD_OFFSET (field))
9751 DECL_FIELD_OFFSET (new_field) = new_n;
9752 changed_field = true;
9755 /* Do the substitution inside the qualifier, if any. */
9756 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9758 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9759 if (new_n != DECL_QUALIFIER (field))
9761 DECL_QUALIFIER (new_field) = new_n;
9762 changed_field = true;
9766 DECL_CONTEXT (new_field) = nt;
9767 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9769 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9770 TYPE_FIELDS (nt) = new_field;
9773 if (!changed_field)
9774 return t;
9776 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9777 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9778 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9779 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9780 return nt;
9783 default:
9784 return t;
9788 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9789 needed to represent the object. */
9791 tree
9792 rm_size (tree gnu_type)
9794 /* For integral types, we store the RM size explicitly. */
9795 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9796 return TYPE_RM_SIZE (gnu_type);
9798 /* Return the RM size of the actual data plus the size of the template. */
9799 if (TREE_CODE (gnu_type) == RECORD_TYPE
9800 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9801 return
9802 size_binop (PLUS_EXPR,
9803 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9804 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9806 /* For record or union types, we store the size explicitly. */
9807 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9808 && !TYPE_FAT_POINTER_P (gnu_type)
9809 && TYPE_ADA_SIZE (gnu_type))
9810 return TYPE_ADA_SIZE (gnu_type);
9812 /* For other types, this is just the size. */
9813 return TYPE_SIZE (gnu_type);
9816 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9817 fully-qualified name, possibly with type information encoding.
9818 Otherwise, return the name. */
9820 static const char *
9821 get_entity_char (Entity_Id gnat_entity)
9823 Get_Encoded_Name (gnat_entity);
9824 return ggc_strdup (Name_Buffer);
9827 tree
9828 get_entity_name (Entity_Id gnat_entity)
9830 Get_Encoded_Name (gnat_entity);
9831 return get_identifier_with_length (Name_Buffer, Name_Len);
9834 /* Return an identifier representing the external name to be used for
9835 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9836 and the specified suffix. */
9838 tree
9839 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9841 const Entity_Kind kind = Ekind (gnat_entity);
9842 const bool has_suffix = (suffix != NULL);
9843 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9844 String_Pointer sp = {suffix, &temp};
9846 Get_External_Name (gnat_entity, has_suffix, sp);
9848 /* A variable using the Stdcall convention lives in a DLL. We adjust
9849 its name to use the jump table, the _imp__NAME contains the address
9850 for the NAME variable. */
9851 if ((kind == E_Variable || kind == E_Constant)
9852 && Has_Stdcall_Convention (gnat_entity))
9854 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9855 char *new_name = (char *) alloca (len + 1);
9856 strcpy (new_name, STDCALL_PREFIX);
9857 strcat (new_name, Name_Buffer);
9858 return get_identifier_with_length (new_name, len);
9861 return get_identifier_with_length (Name_Buffer, Name_Len);
9864 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9865 string, return a new IDENTIFIER_NODE that is the concatenation of
9866 the name followed by "___" and the specified suffix. */
9868 tree
9869 concat_name (tree gnu_name, const char *suffix)
9871 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9872 char *new_name = (char *) alloca (len + 1);
9873 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9874 strcat (new_name, "___");
9875 strcat (new_name, suffix);
9876 return get_identifier_with_length (new_name, len);
9879 /* Initialize data structures of the decl.c module. */
9881 void
9882 init_gnat_decl (void)
9884 /* Initialize the cache of annotated values. */
9885 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9887 /* Initialize the association of dummy types with subprograms. */
9888 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
9891 /* Destroy data structures of the decl.c module. */
9893 void
9894 destroy_gnat_decl (void)
9896 /* Destroy the cache of annotated values. */
9897 annotate_value_cache->empty ();
9898 annotate_value_cache = NULL;
9900 /* Destroy the association of dummy types with subprograms. */
9901 dummy_to_subprog_map->empty ();
9902 dummy_to_subprog_map = NULL;
9905 #include "gt-ada-decl.h"