* gcc-interface/decl.c (components_to_record): Add special case for
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobad8452810d49a04d8f1e5ef199b714829cd31fc5
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2016, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
38 #include "ada.h"
39 #include "types.h"
40 #include "atree.h"
41 #include "elists.h"
42 #include "namet.h"
43 #include "nlists.h"
44 #include "repinfo.h"
45 #include "snames.h"
46 #include "uintp.h"
47 #include "urealp.h"
48 #include "fe.h"
49 #include "sinfo.h"
50 #include "einfo.h"
51 #include "ada-tree.h"
52 #include "gigi.h"
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
59 #ifdef TARGET_64BIT
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
64 #else
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
67 #endif
68 #else
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
71 #endif
73 #define STDCALL_PREFIX "_imp__"
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
87 #else
88 #define FOREIGN_FORCE_REALIGN_STACK 0
89 #endif
91 struct incomplete
93 struct incomplete *next;
94 tree old_type;
95 Entity_Id full_type;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_Limited_With types until the
104 end of the spec. */
105 static struct incomplete *defer_limited_with;
107 typedef struct subst_pair_d {
108 tree discriminant;
109 tree replacement;
110 } subst_pair;
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
115 tree type;
117 /* The associated field. */
118 tree field;
120 /* The value of the qualifier. */
121 tree qual;
123 /* The type of the variant after transformation. */
124 tree new_type;
125 } variant_desc;
128 /* A hash table 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 static void prepend_one_attribute (struct attrib **,
154 enum attrib_type, tree, tree, Node_Id);
155 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
156 static void prepend_attributes (struct attrib **, Entity_Id);
157 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
158 bool);
159 static bool type_has_variable_size (tree);
160 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
161 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
162 unsigned int);
163 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
164 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
165 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
166 bool *);
167 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
168 static bool is_from_limited_with_of_main (Entity_Id);
169 static tree change_qualified_type (tree, int);
170 static bool same_discriminant_p (Entity_Id, Entity_Id);
171 static bool array_type_has_nonaliased_component (tree, Entity_Id);
172 static bool compile_time_known_address_p (Node_Id);
173 static bool cannot_be_superflat (Node_Id);
174 static bool constructor_address_p (tree);
175 static bool allocatable_size_p (tree, bool);
176 static bool initial_value_needs_conversion (tree, tree);
177 static int compare_field_bitpos (const PTR, const PTR);
178 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
179 bool, bool, bool, bool, bool, tree, tree *);
180 static Uint annotate_value (tree);
181 static void annotate_rep (Entity_Id, tree);
182 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
183 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
184 static vec<variant_desc> build_variant_list (tree,
185 vec<subst_pair> ,
186 vec<variant_desc> );
187 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
188 static void set_rm_size (Uint, tree, Entity_Id);
189 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
190 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
191 static tree create_field_decl_from (tree, tree, tree, tree, tree,
192 vec<subst_pair> );
193 static tree create_rep_part (tree, tree, tree);
194 static tree get_rep_part (tree);
195 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
196 tree, vec<subst_pair> );
197 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
198 static void associate_original_type_to_packed_array (tree, Entity_Id);
199 static const char *get_entity_char (Entity_Id);
201 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
202 to pass around calls performing profile compatibility checks. */
204 typedef struct {
205 Entity_Id gnat_entity; /* The Ada subprogram entity. */
206 tree ada_fntype; /* The corresponding GCC type node. */
207 tree btin_fntype; /* The GCC builtin function type node. */
208 } intrin_binding_t;
210 static bool intrin_profiles_compatible_p (intrin_binding_t *);
212 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
213 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
214 and associate the ..._DECL node with the input GNAT defining identifier.
216 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
217 initial value (in GCC tree form). This is optional for a variable. For
218 a renamed entity, GNU_EXPR gives the object being renamed.
220 DEFINITION is nonzero if this call is intended for a definition. This is
221 used for separate compilation where it is necessary to know whether an
222 external declaration or a definition must be created if the GCC equivalent
223 was not created previously. The value of 1 is normally used for a nonzero
224 DEFINITION, but a value of 2 is used in special circumstances, defined in
225 the code. */
227 tree
228 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
230 /* Contains the kind of the input GNAT node. */
231 const Entity_Kind kind = Ekind (gnat_entity);
232 /* True if this is a type. */
233 const bool is_type = IN (kind, Type_Kind);
234 /* True if this is an artificial entity. */
235 const bool artificial_p = !Comes_From_Source (gnat_entity);
236 /* True if debug info is requested for this entity. */
237 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
238 /* True if this entity is to be considered as imported. */
239 const bool imported_p
240 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
241 /* For a type, contains the equivalent GNAT node to be used in gigi. */
242 Entity_Id gnat_equiv_type = Empty;
243 /* Temporary used to walk the GNAT tree. */
244 Entity_Id gnat_temp;
245 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
246 This node will be associated with the GNAT node by calling at the end
247 of the `switch' statement. */
248 tree gnu_decl = NULL_TREE;
249 /* Contains the GCC type to be used for the GCC node. */
250 tree gnu_type = NULL_TREE;
251 /* Contains the GCC size tree to be used for the GCC node. */
252 tree gnu_size = NULL_TREE;
253 /* Contains the GCC name to be used for the GCC node. */
254 tree gnu_entity_name;
255 /* True if we have already saved gnu_decl as a GNAT association. */
256 bool saved = false;
257 /* True if we incremented defer_incomplete_level. */
258 bool this_deferred = false;
259 /* True if we incremented force_global. */
260 bool this_global = false;
261 /* True if we should check to see if elaborated during processing. */
262 bool maybe_present = false;
263 /* True if we made GNU_DECL and its type here. */
264 bool this_made_decl = false;
265 /* Size and alignment of the GCC node, if meaningful. */
266 unsigned int esize = 0, align = 0;
267 /* Contains the list of attributes directly attached to the entity. */
268 struct attrib *attr_list = NULL;
270 /* Since a use of an Itype is a definition, process it as such if it
271 is not in a with'ed unit. */
272 if (!definition
273 && is_type
274 && Is_Itype (gnat_entity)
275 && !present_gnu_tree (gnat_entity)
276 && In_Extended_Main_Code_Unit (gnat_entity))
278 /* Ensure that we are in a subprogram mentioned in the Scope chain of
279 this entity, our current scope is global, or we encountered a task
280 or entry (where we can't currently accurately check scoping). */
281 if (!current_function_decl
282 || DECL_ELABORATION_PROC_P (current_function_decl))
284 process_type (gnat_entity);
285 return get_gnu_tree (gnat_entity);
288 for (gnat_temp = Scope (gnat_entity);
289 Present (gnat_temp);
290 gnat_temp = Scope (gnat_temp))
292 if (Is_Type (gnat_temp))
293 gnat_temp = Underlying_Type (gnat_temp);
295 if (Ekind (gnat_temp) == E_Subprogram_Body)
296 gnat_temp
297 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
299 if (IN (Ekind (gnat_temp), Subprogram_Kind)
300 && Present (Protected_Body_Subprogram (gnat_temp)))
301 gnat_temp = Protected_Body_Subprogram (gnat_temp);
303 if (Ekind (gnat_temp) == E_Entry
304 || Ekind (gnat_temp) == E_Entry_Family
305 || Ekind (gnat_temp) == E_Task_Type
306 || (IN (Ekind (gnat_temp), Subprogram_Kind)
307 && present_gnu_tree (gnat_temp)
308 && (current_function_decl
309 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
311 process_type (gnat_entity);
312 return get_gnu_tree (gnat_entity);
316 /* This abort means the Itype has an incorrect scope, i.e. that its
317 scope does not correspond to the subprogram it is declared in. */
318 gcc_unreachable ();
321 /* If we've already processed this entity, return what we got last time.
322 If we are defining the node, we should not have already processed it.
323 In that case, we will abort below when we try to save a new GCC tree
324 for this object. We also need to handle the case of getting a dummy
325 type when a Full_View exists but be careful so as not to trigger its
326 premature elaboration. */
327 if ((!definition || (is_type && imported_p))
328 && present_gnu_tree (gnat_entity))
330 gnu_decl = get_gnu_tree (gnat_entity);
332 if (TREE_CODE (gnu_decl) == TYPE_DECL
333 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
334 && IN (kind, Incomplete_Or_Private_Kind)
335 && Present (Full_View (gnat_entity))
336 && (present_gnu_tree (Full_View (gnat_entity))
337 || No (Freeze_Node (Full_View (gnat_entity)))))
339 gnu_decl
340 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
341 save_gnu_tree (gnat_entity, NULL_TREE, false);
342 save_gnu_tree (gnat_entity, gnu_decl, false);
345 return gnu_decl;
348 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
349 must be specified unless it was specified by the programmer. Exceptions
350 are for access-to-protected-subprogram types and all access subtypes, as
351 another GNAT type is used to lay out the GCC type for them. */
352 gcc_assert (!Unknown_Esize (gnat_entity)
353 || Has_Size_Clause (gnat_entity)
354 || (!IN (kind, Numeric_Kind)
355 && !IN (kind, Enumeration_Kind)
356 && (!IN (kind, Access_Kind)
357 || kind == E_Access_Protected_Subprogram_Type
358 || kind == E_Anonymous_Access_Protected_Subprogram_Type
359 || kind == E_Access_Subtype
360 || type_annotate_only)));
362 /* The RM size must be specified for all discrete and fixed-point types. */
363 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
364 && Unknown_RM_Size (gnat_entity)));
366 /* If we get here, it means we have not yet done anything with this entity.
367 If we are not defining it, it must be a type or an entity that is defined
368 elsewhere or externally, otherwise we should have defined it already. */
369 gcc_assert (definition
370 || type_annotate_only
371 || is_type
372 || kind == E_Discriminant
373 || kind == E_Component
374 || kind == E_Label
375 || (kind == E_Constant && Present (Full_View (gnat_entity)))
376 || Is_Public (gnat_entity));
378 /* Get the name of the entity and set up the line number and filename of
379 the original definition for use in any decl we make. Make sure we do not
380 inherit another source location. */
381 gnu_entity_name = get_entity_name (gnat_entity);
382 if (Sloc (gnat_entity) != No_Location
383 && !renaming_from_generic_instantiation_p (gnat_entity))
384 Sloc_to_locus (Sloc (gnat_entity), &input_location);
386 /* For cases when we are not defining (i.e., we are referencing from
387 another compilation unit) public entities, show we are at global level
388 for the purpose of computing scopes. Don't do this for components or
389 discriminants since the relevant test is whether or not the record is
390 being defined. */
391 if (!definition
392 && kind != E_Component
393 && kind != E_Discriminant
394 && Is_Public (gnat_entity)
395 && !Is_Statically_Allocated (gnat_entity))
396 force_global++, this_global = true;
398 /* Handle any attributes directly attached to the entity. */
399 if (Has_Gigi_Rep_Item (gnat_entity))
400 prepend_attributes (&attr_list, gnat_entity);
402 /* Do some common processing for types. */
403 if (is_type)
405 /* Compute the equivalent type to be used in gigi. */
406 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
408 /* Machine_Attributes on types are expected to be propagated to
409 subtypes. The corresponding Gigi_Rep_Items are only attached
410 to the first subtype though, so we handle the propagation here. */
411 if (Base_Type (gnat_entity) != gnat_entity
412 && !Is_First_Subtype (gnat_entity)
413 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
414 prepend_attributes (&attr_list,
415 First_Subtype (Base_Type (gnat_entity)));
417 /* Compute a default value for the size of an elementary type. */
418 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
420 unsigned int max_esize;
422 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
423 esize = UI_To_Int (Esize (gnat_entity));
425 if (IN (kind, Float_Kind))
426 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
427 else if (IN (kind, Access_Kind))
428 max_esize = POINTER_SIZE * 2;
429 else
430 max_esize = LONG_LONG_TYPE_SIZE;
432 if (esize > max_esize)
433 esize = max_esize;
437 switch (kind)
439 case E_Component:
440 case E_Discriminant:
442 /* The GNAT record where the component was defined. */
443 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
445 /* If the entity is a discriminant of an extended tagged type used to
446 rename a discriminant of the parent type, return the latter. */
447 if (Is_Tagged_Type (gnat_record)
448 && Present (Corresponding_Discriminant (gnat_entity)))
450 gnu_decl
451 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
452 gnu_expr, definition);
453 saved = true;
454 break;
457 /* If the entity is an inherited component (in the case of extended
458 tagged record types), just return the original entity, which must
459 be a FIELD_DECL. Likewise for discriminants. If the entity is a
460 non-girder discriminant (in the case of derived untagged record
461 types), return the stored discriminant it renames. */
462 else if (Present (Original_Record_Component (gnat_entity))
463 && Original_Record_Component (gnat_entity) != gnat_entity)
465 gnu_decl
466 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
467 gnu_expr, definition);
468 saved = true;
469 break;
472 /* Otherwise, if we are not defining this and we have no GCC type
473 for the containing record, make one for it. Then we should
474 have made our own equivalent. */
475 else if (!definition && !present_gnu_tree (gnat_record))
477 /* ??? If this is in a record whose scope is a protected
478 type and we have an Original_Record_Component, use it.
479 This is a workaround for major problems in protected type
480 handling. */
481 Entity_Id Scop = Scope (Scope (gnat_entity));
482 if (Is_Protected_Type (Underlying_Type (Scop))
483 && Present (Original_Record_Component (gnat_entity)))
485 gnu_decl
486 = gnat_to_gnu_entity (Original_Record_Component
487 (gnat_entity),
488 gnu_expr, 0);
489 saved = true;
490 break;
493 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
494 gnu_decl = get_gnu_tree (gnat_entity);
495 saved = true;
496 break;
499 else
500 /* Here we have no GCC type and this is a reference rather than a
501 definition. This should never happen. Most likely the cause is
502 reference before declaration in the GNAT tree for gnat_entity. */
503 gcc_unreachable ();
506 case E_Constant:
507 /* Ignore constant definitions already marked with the error node. See
508 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
509 if (definition
510 && present_gnu_tree (gnat_entity)
511 && get_gnu_tree (gnat_entity) == error_mark_node)
513 maybe_present = true;
514 break;
517 /* Ignore deferred constant definitions without address clause since
518 they are processed fully in the front-end. If No_Initialization
519 is set, this is not a deferred constant but a constant whose value
520 is built manually. And constants that are renamings are handled
521 like variables. */
522 if (definition
523 && !gnu_expr
524 && No (Address_Clause (gnat_entity))
525 && !No_Initialization (Declaration_Node (gnat_entity))
526 && No (Renamed_Object (gnat_entity)))
528 gnu_decl = error_mark_node;
529 saved = true;
530 break;
533 /* If this is a use of a deferred constant without address clause,
534 get its full definition. */
535 if (!definition
536 && No (Address_Clause (gnat_entity))
537 && Present (Full_View (gnat_entity)))
539 gnu_decl
540 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
541 saved = true;
542 break;
545 /* If we have a constant that we are not defining, get the expression it
546 was defined to represent. This is necessary to avoid generating dumb
547 elaboration code in simple cases, but we may throw it away later if it
548 is not a constant. But do not retrieve it if it is an allocator since
549 the designated type might still be dummy at this point. */
550 if (!definition
551 && !No_Initialization (Declaration_Node (gnat_entity))
552 && Present (Expression (Declaration_Node (gnat_entity)))
553 && Nkind (Expression (Declaration_Node (gnat_entity)))
554 != N_Allocator)
555 /* The expression may contain N_Expression_With_Actions nodes and
556 thus object declarations from other units. Discard them. */
557 gnu_expr
558 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
560 /* ... fall through ... */
562 case E_Exception:
563 case E_Loop_Parameter:
564 case E_Out_Parameter:
565 case E_Variable:
567 /* Always create a variable for volatile objects and variables seen
568 constant but with a Linker_Section pragma. */
569 bool const_flag
570 = ((kind == E_Constant || kind == E_Variable)
571 && Is_True_Constant (gnat_entity)
572 && !(kind == E_Variable
573 && Present (Linker_Section_Pragma (gnat_entity)))
574 && !Treat_As_Volatile (gnat_entity)
575 && (((Nkind (Declaration_Node (gnat_entity))
576 == N_Object_Declaration)
577 && Present (Expression (Declaration_Node (gnat_entity))))
578 || Present (Renamed_Object (gnat_entity))
579 || imported_p));
580 bool inner_const_flag = const_flag;
581 bool static_flag = Is_Statically_Allocated (gnat_entity);
582 /* We implement RM 13.3(19) for exported and imported (non-constant)
583 objects by making them volatile. */
584 bool volatile_flag
585 = (Treat_As_Volatile (gnat_entity)
586 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
587 bool mutable_p = false;
588 bool used_by_ref = false;
589 tree gnu_ext_name = NULL_TREE;
590 tree renamed_obj = NULL_TREE;
591 tree gnu_object_size;
593 /* We need to translate the renamed object even though we are only
594 referencing the renaming. But it may contain a call for which
595 we'll generate a temporary to hold the return value and which
596 is part of the definition of the renaming, so discard it. */
597 if (Present (Renamed_Object (gnat_entity)) && !definition)
599 if (kind == E_Exception)
600 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
601 NULL_TREE, 0);
602 else
603 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
606 /* Get the type after elaborating the renamed object. */
607 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
609 /* If this is a standard exception definition, then use the standard
610 exception type. This is necessary to make sure that imported and
611 exported views of exceptions are properly merged in LTO mode. */
612 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
613 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
614 gnu_type = except_type_node;
616 /* For a debug renaming declaration, build a debug-only entity. */
617 if (Present (Debug_Renaming_Link (gnat_entity)))
619 /* Force a non-null value to make sure the symbol is retained. */
620 tree value = build1 (INDIRECT_REF, gnu_type,
621 build1 (NOP_EXPR,
622 build_pointer_type (gnu_type),
623 integer_minus_one_node));
624 gnu_decl = build_decl (input_location,
625 VAR_DECL, gnu_entity_name, gnu_type);
626 SET_DECL_VALUE_EXPR (gnu_decl, value);
627 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
628 gnat_pushdecl (gnu_decl, gnat_entity);
629 break;
632 /* If this is a loop variable, its type should be the base type.
633 This is because the code for processing a loop determines whether
634 a normal loop end test can be done by comparing the bounds of the
635 loop against those of the base type, which is presumed to be the
636 size used for computation. But this is not correct when the size
637 of the subtype is smaller than the type. */
638 if (kind == E_Loop_Parameter)
639 gnu_type = get_base_type (gnu_type);
641 /* Reject non-renamed objects whose type is an unconstrained array or
642 any object whose type is a dummy type or void. */
643 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
644 && No (Renamed_Object (gnat_entity)))
645 || TYPE_IS_DUMMY_P (gnu_type)
646 || TREE_CODE (gnu_type) == VOID_TYPE)
648 gcc_assert (type_annotate_only);
649 if (this_global)
650 force_global--;
651 return error_mark_node;
654 /* If an alignment is specified, use it if valid. Note that exceptions
655 are objects but don't have an alignment. We must do this before we
656 validate the size, since the alignment can affect the size. */
657 if (kind != E_Exception && Known_Alignment (gnat_entity))
659 gcc_assert (Present (Alignment (gnat_entity)));
661 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
662 TYPE_ALIGN (gnu_type));
664 /* No point in changing the type if there is an address clause
665 as the final type of the object will be a reference type. */
666 if (Present (Address_Clause (gnat_entity)))
667 align = 0;
668 else
670 tree orig_type = gnu_type;
672 gnu_type
673 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
674 false, false, definition, true);
676 /* If a padding record was made, declare it now since it will
677 never be declared otherwise. This is necessary to ensure
678 that its subtrees are properly marked. */
679 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
680 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
681 debug_info_p, gnat_entity);
685 /* If we are defining the object, see if it has a Size and validate it
686 if so. If we are not defining the object and a Size clause applies,
687 simply retrieve the value. We don't want to ignore the clause and
688 it is expected to have been validated already. Then get the new
689 type, if any. */
690 if (definition)
691 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
692 gnat_entity, VAR_DECL, false,
693 Has_Size_Clause (gnat_entity));
694 else if (Has_Size_Clause (gnat_entity))
695 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
697 if (gnu_size)
699 gnu_type
700 = make_type_from_size (gnu_type, gnu_size,
701 Has_Biased_Representation (gnat_entity));
703 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
704 gnu_size = NULL_TREE;
707 /* If this object has self-referential size, it must be a record with
708 a default discriminant. We are supposed to allocate an object of
709 the maximum size in this case, unless it is a constant with an
710 initializing expression, in which case we can get the size from
711 that. Note that the resulting size may still be a variable, so
712 this may end up with an indirect allocation. */
713 if (No (Renamed_Object (gnat_entity))
714 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
716 if (gnu_expr && kind == E_Constant)
718 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
719 if (CONTAINS_PLACEHOLDER_P (size))
721 /* If the initializing expression is itself a constant,
722 despite having a nominal type with self-referential
723 size, we can get the size directly from it. */
724 if (TREE_CODE (gnu_expr) == COMPONENT_REF
725 && TYPE_IS_PADDING_P
726 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
727 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
728 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
729 || DECL_READONLY_ONCE_ELAB
730 (TREE_OPERAND (gnu_expr, 0))))
731 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
732 else
733 gnu_size
734 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
736 else
737 gnu_size = size;
739 /* We may have no GNU_EXPR because No_Initialization is
740 set even though there's an Expression. */
741 else if (kind == E_Constant
742 && (Nkind (Declaration_Node (gnat_entity))
743 == N_Object_Declaration)
744 && Present (Expression (Declaration_Node (gnat_entity))))
745 gnu_size
746 = TYPE_SIZE (gnat_to_gnu_type
747 (Etype
748 (Expression (Declaration_Node (gnat_entity)))));
749 else
751 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
752 mutable_p = true;
755 /* If we are at global level and the size isn't constant, call
756 elaborate_expression_1 to make a variable for it rather than
757 calculating it each time. */
758 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
759 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
760 "SIZE", definition, false);
763 /* If the size is zero byte, make it one byte since some linkers have
764 troubles with zero-sized objects. If the object will have a
765 template, that will make it nonzero so don't bother. Also avoid
766 doing that for an object renaming or an object with an address
767 clause, as we would lose useful information on the view size
768 (e.g. for null array slices) and we are not allocating the object
769 here anyway. */
770 if (((gnu_size
771 && integer_zerop (gnu_size)
772 && !TREE_OVERFLOW (gnu_size))
773 || (TYPE_SIZE (gnu_type)
774 && integer_zerop (TYPE_SIZE (gnu_type))
775 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
776 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
777 && No (Renamed_Object (gnat_entity))
778 && No (Address_Clause (gnat_entity)))
779 gnu_size = bitsize_unit_node;
781 /* If this is an object with no specified size and alignment, and
782 if either it is atomic or we are not optimizing alignment for
783 space and it is composite and not an exception, an Out parameter
784 or a reference to another object, and the size of its type is a
785 constant, set the alignment to the smallest one which is not
786 smaller than the size, with an appropriate cap. */
787 if (!gnu_size && align == 0
788 && (Is_Atomic_Or_VFA (gnat_entity)
789 || (!Optimize_Alignment_Space (gnat_entity)
790 && kind != E_Exception
791 && kind != E_Out_Parameter
792 && Is_Composite_Type (Etype (gnat_entity))
793 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
794 && !Is_Exported (gnat_entity)
795 && !imported_p
796 && No (Renamed_Object (gnat_entity))
797 && No (Address_Clause (gnat_entity))))
798 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
800 unsigned int size_cap, align_cap;
802 /* No point in promoting the alignment if this doesn't prevent
803 BLKmode access to the object, in particular block copy, as
804 this will for example disable the NRV optimization for it.
805 No point in jumping through all the hoops needed in order
806 to support BIGGEST_ALIGNMENT if we don't really have to.
807 So we cap to the smallest alignment that corresponds to
808 a known efficient memory access pattern of the target. */
809 if (Is_Atomic_Or_VFA (gnat_entity))
811 size_cap = UINT_MAX;
812 align_cap = BIGGEST_ALIGNMENT;
814 else
816 size_cap = MAX_FIXED_MODE_SIZE;
817 align_cap = get_mode_alignment (ptr_mode);
820 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
821 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
822 align = 0;
823 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
824 align = align_cap;
825 else
826 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
828 /* But make sure not to under-align the object. */
829 if (align <= TYPE_ALIGN (gnu_type))
830 align = 0;
832 /* And honor the minimum valid atomic alignment, if any. */
833 #ifdef MINIMUM_ATOMIC_ALIGNMENT
834 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
835 align = MINIMUM_ATOMIC_ALIGNMENT;
836 #endif
839 /* If the object is set to have atomic components, find the component
840 type and validate it.
842 ??? Note that we ignore Has_Volatile_Components on objects; it's
843 not at all clear what to do in that case. */
844 if (Has_Atomic_Components (gnat_entity))
846 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
847 ? TREE_TYPE (gnu_type) : gnu_type);
849 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
850 && TYPE_MULTI_ARRAY_P (gnu_inner))
851 gnu_inner = TREE_TYPE (gnu_inner);
853 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
856 /* If this is an aliased object with an unconstrained array nominal
857 subtype, make a type that includes the template. We will either
858 allocate or create a variable of that type, see below. */
859 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
860 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
861 && !type_annotate_only)
863 tree gnu_array
864 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
865 gnu_type
866 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
867 gnu_type,
868 concat_name (gnu_entity_name,
869 "UNC"),
870 debug_info_p);
873 /* ??? If this is an object of CW type initialized to a value, try to
874 ensure that the object is sufficient aligned for this value, but
875 without pessimizing the allocation. This is a kludge necessary
876 because we don't support dynamic alignment. */
877 if (align == 0
878 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
879 && No (Renamed_Object (gnat_entity))
880 && No (Address_Clause (gnat_entity)))
881 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
883 #ifdef MINIMUM_ATOMIC_ALIGNMENT
884 /* If the size is a constant and no alignment is specified, force
885 the alignment to be the minimum valid atomic alignment. The
886 restriction on constant size avoids problems with variable-size
887 temporaries; if the size is variable, there's no issue with
888 atomic access. Also don't do this for a constant, since it isn't
889 necessary and can interfere with constant replacement. Finally,
890 do not do it for Out parameters since that creates an
891 size inconsistency with In parameters. */
892 if (align == 0
893 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
894 && !FLOAT_TYPE_P (gnu_type)
895 && !const_flag && No (Renamed_Object (gnat_entity))
896 && !imported_p && No (Address_Clause (gnat_entity))
897 && kind != E_Out_Parameter
898 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
899 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
900 align = MINIMUM_ATOMIC_ALIGNMENT;
901 #endif
903 /* Make a new type with the desired size and alignment, if needed.
904 But do not take into account alignment promotions to compute the
905 size of the object. */
906 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
907 if (gnu_size || align > 0)
909 tree orig_type = gnu_type;
911 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
912 false, false, definition, true);
914 /* If a padding record was made, declare it now since it will
915 never be declared otherwise. This is necessary to ensure
916 that its subtrees are properly marked. */
917 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
918 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
919 debug_info_p, gnat_entity);
922 /* Now check if the type of the object allows atomic access. */
923 if (Is_Atomic_Or_VFA (gnat_entity))
924 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
926 /* If this is a renaming, avoid as much as possible to create a new
927 object. However, in some cases, creating it is required because
928 renaming can be applied to objects that are not names in Ada.
929 This processing needs to be applied to the raw expression so as
930 to make it more likely to rename the underlying object. */
931 if (Present (Renamed_Object (gnat_entity)))
933 /* If the renamed object had padding, strip off the reference to
934 the inner object and reset our type. */
935 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
936 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
937 /* Strip useless conversions around the object. */
938 || gnat_useless_type_conversion (gnu_expr))
940 gnu_expr = TREE_OPERAND (gnu_expr, 0);
941 gnu_type = TREE_TYPE (gnu_expr);
944 /* Or else, if the renamed object has an unconstrained type with
945 default discriminant, use the padded type. */
946 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
947 gnu_type = TREE_TYPE (gnu_expr);
949 /* Case 1: if this is a constant renaming stemming from a function
950 call, treat it as a normal object whose initial value is what
951 is being renamed. RM 3.3 says that the result of evaluating a
952 function call is a constant object. Therefore, it can be the
953 inner object of a constant renaming and the renaming must be
954 fully instantiated, i.e. it cannot be a reference to (part of)
955 an existing object. And treat other rvalues (addresses, null
956 expressions, constructors and literals) the same way. */
957 tree inner = gnu_expr;
958 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
959 inner = TREE_OPERAND (inner, 0);
960 /* Expand_Dispatching_Call can prepend a comparison of the tags
961 before the call to "=". */
962 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
963 || TREE_CODE (inner) == COMPOUND_EXPR)
964 inner = TREE_OPERAND (inner, 1);
965 if ((TREE_CODE (inner) == CALL_EXPR
966 && !call_is_atomic_load (inner))
967 || TREE_CODE (inner) == ADDR_EXPR
968 || TREE_CODE (inner) == NULL_EXPR
969 || TREE_CODE (inner) == CONSTRUCTOR
970 || CONSTANT_CLASS_P (inner)
971 /* We need to detect the case where a temporary is created to
972 hold the return value, since we cannot safely rename it at
973 top level as it lives only in the elaboration routine. */
974 || (TREE_CODE (inner) == VAR_DECL
975 && DECL_RETURN_VALUE_P (inner))
976 /* We also need to detect the case where the front-end creates
977 a dangling 'reference to a function call at top level and
978 substitutes it in the renaming, for example:
980 q__b : boolean renames r__f.e (1);
982 can be rewritten into:
984 q__R1s : constant q__A2s := r__f'reference;
985 [...]
986 q__b : boolean renames q__R1s.all.e (1);
988 We cannot safely rename the rewritten expression since the
989 underlying object lives only in the elaboration routine. */
990 || (TREE_CODE (inner) == INDIRECT_REF
991 && (inner
992 = remove_conversions (TREE_OPERAND (inner, 0), true))
993 && TREE_CODE (inner) == VAR_DECL
994 && DECL_RETURN_VALUE_P (inner)))
997 /* Case 2: if the renaming entity need not be materialized, use
998 the elaborated renamed expression for the renaming. But this
999 means that the caller is responsible for evaluating the address
1000 of the renaming in the correct place for the definition case to
1001 instantiate the SAVE_EXPRs. */
1002 else if (!Materialize_Entity (gnat_entity))
1004 tree init = NULL_TREE;
1006 gnu_decl
1007 = elaborate_reference (gnu_expr, gnat_entity, definition,
1008 &init);
1010 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1011 correct place for this case. */
1012 gcc_assert (!init);
1014 /* No DECL_EXPR will be created so the expression needs to be
1015 marked manually because it will likely be shared. */
1016 if (global_bindings_p ())
1017 MARK_VISITED (gnu_decl);
1019 /* This assertion will fail if the renamed object isn't aligned
1020 enough as to make it possible to honor the alignment set on
1021 the renaming. */
1022 if (align)
1024 unsigned int ralign = DECL_P (gnu_decl)
1025 ? DECL_ALIGN (gnu_decl)
1026 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1027 gcc_assert (ralign >= align);
1030 save_gnu_tree (gnat_entity, gnu_decl, true);
1031 saved = true;
1032 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1033 break;
1036 /* Case 3: otherwise, make a constant pointer to the object we
1037 are renaming and attach the object to the pointer after it is
1038 elaborated. The object will be referenced directly instead
1039 of indirectly via the pointer to avoid aliasing problems with
1040 non-addressable entities. The pointer is called a "renaming"
1041 pointer in this case. Note that we also need to preserve the
1042 volatility of the renamed object through the indirection. */
1043 else
1045 tree init = NULL_TREE;
1047 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1048 gnu_type
1049 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1050 gnu_type = build_reference_type (gnu_type);
1051 used_by_ref = true;
1052 const_flag = true;
1053 volatile_flag = false;
1054 inner_const_flag = TREE_READONLY (gnu_expr);
1055 gnu_size = NULL_TREE;
1057 renamed_obj
1058 = elaborate_reference (gnu_expr, gnat_entity, definition,
1059 &init);
1061 /* The expression needs to be marked manually because it will
1062 likely be shared, even for a definition since the ADDR_EXPR
1063 built below can cause the first few nodes to be folded. */
1064 if (global_bindings_p ())
1065 MARK_VISITED (renamed_obj);
1067 if (type_annotate_only
1068 && TREE_CODE (renamed_obj) == ERROR_MARK)
1069 gnu_expr = NULL_TREE;
1070 else
1072 gnu_expr
1073 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1074 if (init)
1075 gnu_expr
1076 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1077 gnu_expr);
1082 /* If we are defining an aliased object whose nominal subtype is
1083 unconstrained, the object is a record that contains both the
1084 template and the object. If there is an initializer, it will
1085 have already been converted to the right type, but we need to
1086 create the template if there is no initializer. */
1087 if (definition
1088 && !gnu_expr
1089 && TREE_CODE (gnu_type) == RECORD_TYPE
1090 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1091 /* Beware that padding might have been introduced above. */
1092 || (TYPE_PADDING_P (gnu_type)
1093 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1094 == RECORD_TYPE
1095 && TYPE_CONTAINS_TEMPLATE_P
1096 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1098 tree template_field
1099 = TYPE_PADDING_P (gnu_type)
1100 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1101 : TYPE_FIELDS (gnu_type);
1102 vec<constructor_elt, va_gc> *v;
1103 vec_alloc (v, 1);
1104 tree t = build_template (TREE_TYPE (template_field),
1105 TREE_TYPE (DECL_CHAIN (template_field)),
1106 NULL_TREE);
1107 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1108 gnu_expr = gnat_build_constructor (gnu_type, v);
1111 /* Convert the expression to the type of the object if need be. */
1112 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1113 gnu_expr = convert (gnu_type, gnu_expr);
1115 /* If this is a pointer that doesn't have an initializing expression,
1116 initialize it to NULL, unless the object is declared imported as
1117 per RM B.1(24). */
1118 if (definition
1119 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1120 && !gnu_expr
1121 && !Is_Imported (gnat_entity))
1122 gnu_expr = integer_zero_node;
1124 /* If we are defining the object and it has an Address clause, we must
1125 either get the address expression from the saved GCC tree for the
1126 object if it has a Freeze node, or elaborate the address expression
1127 here since the front-end has guaranteed that the elaboration has no
1128 effects in this case. */
1129 if (definition && Present (Address_Clause (gnat_entity)))
1131 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1132 Node_Id gnat_expr = Expression (gnat_clause);
1133 tree gnu_address
1134 = present_gnu_tree (gnat_entity)
1135 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1137 save_gnu_tree (gnat_entity, NULL_TREE, false);
1139 /* Convert the type of the object to a reference type that can
1140 alias everything as per RM 13.3(19). */
1141 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1142 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1143 gnu_type
1144 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1145 gnu_address = convert (gnu_type, gnu_address);
1146 used_by_ref = true;
1147 const_flag
1148 = (!Is_Public (gnat_entity)
1149 || compile_time_known_address_p (gnat_expr));
1150 volatile_flag = false;
1151 gnu_size = NULL_TREE;
1153 /* If this is an aliased object with an unconstrained array nominal
1154 subtype, then it can overlay only another aliased object with an
1155 unconstrained array nominal subtype and compatible template. */
1156 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1157 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1158 && !type_annotate_only)
1160 tree rec_type = TREE_TYPE (gnu_type);
1161 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1163 /* This is the pattern built for a regular object. */
1164 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1165 && TREE_OPERAND (gnu_address, 1) == off)
1166 gnu_address = TREE_OPERAND (gnu_address, 0);
1167 /* This is the pattern built for an overaligned object. */
1168 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1169 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1170 == PLUS_EXPR
1171 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1172 == off)
1173 gnu_address
1174 = build2 (POINTER_PLUS_EXPR, gnu_type,
1175 TREE_OPERAND (gnu_address, 0),
1176 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1177 else
1179 post_error_ne ("aliased object& with unconstrained array "
1180 "nominal subtype", gnat_clause,
1181 gnat_entity);
1182 post_error ("\\can overlay only aliased object with "
1183 "compatible subtype", gnat_clause);
1187 /* If we don't have an initializing expression for the underlying
1188 variable, the initializing expression for the pointer is the
1189 specified address. Otherwise, we have to make a COMPOUND_EXPR
1190 to assign both the address and the initial value. */
1191 if (!gnu_expr)
1192 gnu_expr = gnu_address;
1193 else
1194 gnu_expr
1195 = build2 (COMPOUND_EXPR, gnu_type,
1196 build_binary_op (INIT_EXPR, NULL_TREE,
1197 build_unary_op (INDIRECT_REF,
1198 NULL_TREE,
1199 gnu_address),
1200 gnu_expr),
1201 gnu_address);
1204 /* If it has an address clause and we are not defining it, mark it
1205 as an indirect object. Likewise for Stdcall objects that are
1206 imported. */
1207 if ((!definition && Present (Address_Clause (gnat_entity)))
1208 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1210 /* Convert the type of the object to a reference type that can
1211 alias everything as per RM 13.3(19). */
1212 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1213 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1214 gnu_type
1215 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1216 used_by_ref = true;
1217 const_flag = false;
1218 volatile_flag = false;
1219 gnu_size = NULL_TREE;
1221 /* No point in taking the address of an initializing expression
1222 that isn't going to be used. */
1223 gnu_expr = NULL_TREE;
1225 /* If it has an address clause whose value is known at compile
1226 time, make the object a CONST_DECL. This will avoid a
1227 useless dereference. */
1228 if (Present (Address_Clause (gnat_entity)))
1230 Node_Id gnat_address
1231 = Expression (Address_Clause (gnat_entity));
1233 if (compile_time_known_address_p (gnat_address))
1235 gnu_expr = gnat_to_gnu (gnat_address);
1236 const_flag = true;
1241 /* If we are at top level and this object is of variable size,
1242 make the actual type a hidden pointer to the real type and
1243 make the initializer be a memory allocation and initialization.
1244 Likewise for objects we aren't defining (presumed to be
1245 external references from other packages), but there we do
1246 not set up an initialization.
1248 If the object's size overflows, make an allocator too, so that
1249 Storage_Error gets raised. Note that we will never free
1250 such memory, so we presume it never will get allocated. */
1251 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1252 global_bindings_p ()
1253 || !definition
1254 || static_flag)
1255 || (gnu_size
1256 && !allocatable_size_p (convert (sizetype,
1257 size_binop
1258 (CEIL_DIV_EXPR, gnu_size,
1259 bitsize_unit_node)),
1260 global_bindings_p ()
1261 || !definition
1262 || static_flag)))
1264 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1265 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1266 gnu_type = build_reference_type (gnu_type);
1267 used_by_ref = true;
1268 const_flag = true;
1269 volatile_flag = false;
1270 gnu_size = NULL_TREE;
1272 /* In case this was a aliased object whose nominal subtype is
1273 unconstrained, the pointer above will be a thin pointer and
1274 build_allocator will automatically make the template.
1276 If we have a template initializer only (that we made above),
1277 pretend there is none and rely on what build_allocator creates
1278 again anyway. Otherwise (if we have a full initializer), get
1279 the data part and feed that to build_allocator.
1281 If we are elaborating a mutable object, tell build_allocator to
1282 ignore a possibly simpler size from the initializer, if any, as
1283 we must allocate the maximum possible size in this case. */
1284 if (definition && !imported_p)
1286 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1288 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1289 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1291 gnu_alloc_type
1292 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1294 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1295 && vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)) == 1)
1296 gnu_expr = NULL_TREE;
1297 else
1298 gnu_expr
1299 = build_component_ref
1300 (gnu_expr,
1301 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1302 false);
1305 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1306 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1307 post_error ("?`Storage_Error` will be raised at run time!",
1308 gnat_entity);
1310 gnu_expr
1311 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1312 Empty, Empty, gnat_entity, mutable_p);
1314 else
1315 gnu_expr = NULL_TREE;
1318 /* If this object would go into the stack and has an alignment larger
1319 than the largest stack alignment the back-end can honor, resort to
1320 a variable of "aligning type". */
1321 if (definition
1322 && !global_bindings_p ()
1323 && !static_flag
1324 && !imported_p
1325 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1327 /* Create the new variable. No need for extra room before the
1328 aligned field as this is in automatic storage. */
1329 tree gnu_new_type
1330 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1331 TYPE_SIZE_UNIT (gnu_type),
1332 BIGGEST_ALIGNMENT, 0, gnat_entity);
1333 tree gnu_new_var
1334 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1335 NULL_TREE, gnu_new_type, NULL_TREE,
1336 false, false, false, false, false,
1337 true, debug_info_p, NULL, gnat_entity);
1339 /* Initialize the aligned field if we have an initializer. */
1340 if (gnu_expr)
1341 add_stmt_with_node
1342 (build_binary_op (INIT_EXPR, NULL_TREE,
1343 build_component_ref
1344 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1345 false),
1346 gnu_expr),
1347 gnat_entity);
1349 /* And setup this entity as a reference to the aligned field. */
1350 gnu_type = build_reference_type (gnu_type);
1351 gnu_expr
1352 = build_unary_op
1353 (ADDR_EXPR, NULL_TREE,
1354 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1355 false));
1356 TREE_CONSTANT (gnu_expr) = 1;
1358 used_by_ref = true;
1359 const_flag = true;
1360 volatile_flag = false;
1361 gnu_size = NULL_TREE;
1364 /* If this is an aliased object with an unconstrained array nominal
1365 subtype, we make its type a thin reference, i.e. the reference
1366 counterpart of a thin pointer, so it points to the array part.
1367 This is aimed to make it easier for the debugger to decode the
1368 object. Note that we have to do it this late because of the
1369 couple of allocation adjustments that might be made above. */
1370 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1371 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1372 && !type_annotate_only)
1374 /* In case the object with the template has already been allocated
1375 just above, we have nothing to do here. */
1376 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1378 /* This variable is a GNAT encoding used by Workbench: let it
1379 go through the debugging information but mark it as
1380 artificial: users are not interested in it. */
1381 tree gnu_unc_var
1382 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1383 NULL_TREE, gnu_type, gnu_expr,
1384 const_flag, Is_Public (gnat_entity),
1385 imported_p || !definition, static_flag,
1386 volatile_flag, true, debug_info_p,
1387 NULL, gnat_entity);
1388 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1389 TREE_CONSTANT (gnu_expr) = 1;
1391 used_by_ref = true;
1392 const_flag = true;
1393 volatile_flag = false;
1394 inner_const_flag = TREE_READONLY (gnu_unc_var);
1395 gnu_size = NULL_TREE;
1398 tree gnu_array
1399 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1400 gnu_type
1401 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1404 if (const_flag)
1405 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1407 /* Convert the expression to the type of the object if need be. */
1408 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1409 gnu_expr = convert (gnu_type, gnu_expr);
1411 /* If this name is external or a name was specified, use it, but don't
1412 use the Interface_Name with an address clause (see cd30005). */
1413 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1414 || (Present (Interface_Name (gnat_entity))
1415 && No (Address_Clause (gnat_entity))))
1416 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1418 /* If this is an aggregate constant initialized to a constant, force it
1419 to be statically allocated. This saves an initialization copy. */
1420 if (!static_flag
1421 && const_flag
1422 && gnu_expr && TREE_CONSTANT (gnu_expr)
1423 && AGGREGATE_TYPE_P (gnu_type)
1424 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1425 && !(TYPE_IS_PADDING_P (gnu_type)
1426 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1427 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1428 static_flag = true;
1430 /* Deal with a pragma Linker_Section on a constant or variable. */
1431 if ((kind == E_Constant || kind == E_Variable)
1432 && Present (Linker_Section_Pragma (gnat_entity)))
1433 prepend_one_attribute_pragma (&attr_list,
1434 Linker_Section_Pragma (gnat_entity));
1436 /* Now create the variable or the constant and set various flags. */
1437 gnu_decl
1438 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1439 gnu_expr, const_flag, Is_Public (gnat_entity),
1440 imported_p || !definition, static_flag,
1441 volatile_flag, artificial_p, debug_info_p,
1442 attr_list, gnat_entity, !renamed_obj);
1443 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1444 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1445 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1447 /* If we are defining an Out parameter and optimization isn't enabled,
1448 create a fake PARM_DECL for debugging purposes and make it point to
1449 the VAR_DECL. Suppress debug info for the latter but make sure it
1450 will live in memory so that it can be accessed from within the
1451 debugger through the PARM_DECL. */
1452 if (kind == E_Out_Parameter
1453 && definition
1454 && debug_info_p
1455 && !optimize
1456 && !flag_generate_lto)
1458 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1459 gnat_pushdecl (param, gnat_entity);
1460 SET_DECL_VALUE_EXPR (param, gnu_decl);
1461 DECL_HAS_VALUE_EXPR_P (param) = 1;
1462 DECL_IGNORED_P (gnu_decl) = 1;
1463 TREE_ADDRESSABLE (gnu_decl) = 1;
1466 /* If this is a loop parameter, set the corresponding flag. */
1467 else if (kind == E_Loop_Parameter)
1468 DECL_LOOP_PARM_P (gnu_decl) = 1;
1470 /* If this is a renaming pointer, attach the renamed object to it. */
1471 if (renamed_obj)
1472 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1474 /* If this is a constant and we are defining it or it generates a real
1475 symbol at the object level and we are referencing it, we may want
1476 or need to have a true variable to represent it:
1477 - if optimization isn't enabled, for debugging purposes,
1478 - if the constant is public and not overlaid on something else,
1479 - if its address is taken,
1480 - if either itself or its type is aliased. */
1481 if (TREE_CODE (gnu_decl) == CONST_DECL
1482 && (definition || Sloc (gnat_entity) > Standard_Location)
1483 && ((!optimize && debug_info_p)
1484 || (Is_Public (gnat_entity)
1485 && No (Address_Clause (gnat_entity)))
1486 || Address_Taken (gnat_entity)
1487 || Is_Aliased (gnat_entity)
1488 || Is_Aliased (Etype (gnat_entity))))
1490 tree gnu_corr_var
1491 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1492 gnu_expr, true, Is_Public (gnat_entity),
1493 !definition, static_flag, volatile_flag,
1494 artificial_p, debug_info_p, attr_list,
1495 gnat_entity, false);
1497 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1500 /* If this is a constant, even if we don't need a true variable, we
1501 may need to avoid returning the initializer in every case. That
1502 can happen for the address of a (constant) constructor because,
1503 upon dereferencing it, the constructor will be reinjected in the
1504 tree, which may not be valid in every case; see lvalue_required_p
1505 for more details. */
1506 if (TREE_CODE (gnu_decl) == CONST_DECL)
1507 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1509 /* If this object is declared in a block that contains a block with an
1510 exception handler, and we aren't using the GCC exception mechanism,
1511 we must force this variable in memory in order to avoid an invalid
1512 optimization. */
1513 if (Front_End_Exceptions ()
1514 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1515 TREE_ADDRESSABLE (gnu_decl) = 1;
1517 /* If this is a local variable with non-BLKmode and aggregate type,
1518 and optimization isn't enabled, then force it in memory so that
1519 a register won't be allocated to it with possible subparts left
1520 uninitialized and reaching the register allocator. */
1521 else if (TREE_CODE (gnu_decl) == VAR_DECL
1522 && !DECL_EXTERNAL (gnu_decl)
1523 && !TREE_STATIC (gnu_decl)
1524 && DECL_MODE (gnu_decl) != BLKmode
1525 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1526 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1527 && !optimize)
1528 TREE_ADDRESSABLE (gnu_decl) = 1;
1530 /* If we are defining an object with variable size or an object with
1531 fixed size that will be dynamically allocated, and we are using the
1532 front-end setjmp/longjmp exception mechanism, update the setjmp
1533 buffer. */
1534 if (definition
1535 && Exception_Mechanism == Front_End_SJLJ
1536 && get_block_jmpbuf_decl ()
1537 && DECL_SIZE_UNIT (gnu_decl)
1538 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1539 || (flag_stack_check == GENERIC_STACK_CHECK
1540 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1541 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1542 add_stmt_with_node (build_call_n_expr
1543 (update_setjmp_buf_decl, 1,
1544 build_unary_op (ADDR_EXPR, NULL_TREE,
1545 get_block_jmpbuf_decl ())),
1546 gnat_entity);
1548 /* Back-annotate Esize and Alignment of the object if not already
1549 known. Note that we pick the values of the type, not those of
1550 the object, to shield ourselves from low-level platform-dependent
1551 adjustments like alignment promotion. This is both consistent with
1552 all the treatment above, where alignment and size are set on the
1553 type of the object and not on the object directly, and makes it
1554 possible to support all confirming representation clauses. */
1555 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1556 used_by_ref);
1558 break;
1560 case E_Void:
1561 /* Return a TYPE_DECL for "void" that we previously made. */
1562 gnu_decl = TYPE_NAME (void_type_node);
1563 break;
1565 case E_Enumeration_Type:
1566 /* A special case: for the types Character and Wide_Character in
1567 Standard, we do not list all the literals. So if the literals
1568 are not specified, make this an integer type. */
1569 if (No (First_Literal (gnat_entity)))
1571 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1572 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1573 else
1574 gnu_type = make_unsigned_type (esize);
1575 TYPE_NAME (gnu_type) = gnu_entity_name;
1577 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1578 This is needed by the DWARF-2 back-end to distinguish between
1579 unsigned integer types and character types. */
1580 TYPE_STRING_FLAG (gnu_type) = 1;
1582 /* This flag is needed by the call just below. */
1583 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1585 finish_character_type (gnu_type);
1587 else
1589 /* We have a list of enumeral constants in First_Literal. We make a
1590 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1591 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1592 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1593 value of the literal. But when we have a regular boolean type, we
1594 simplify this a little by using a BOOLEAN_TYPE. */
1595 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1596 && !Has_Non_Standard_Rep (gnat_entity);
1597 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1598 tree gnu_list = NULL_TREE;
1599 Entity_Id gnat_literal;
1601 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1602 TYPE_PRECISION (gnu_type) = esize;
1603 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1604 set_min_and_max_values_for_integral_type (gnu_type, esize,
1605 TYPE_SIGN (gnu_type));
1606 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1607 layout_type (gnu_type);
1609 for (gnat_literal = First_Literal (gnat_entity);
1610 Present (gnat_literal);
1611 gnat_literal = Next_Literal (gnat_literal))
1613 tree gnu_value
1614 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1615 /* Do not generate debug info for individual enumerators. */
1616 tree gnu_literal
1617 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1618 gnu_type, gnu_value, true, false, false,
1619 false, false, artificial_p, false,
1620 NULL, gnat_literal);
1621 save_gnu_tree (gnat_literal, gnu_literal, false);
1622 gnu_list
1623 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1626 if (!is_boolean)
1627 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1629 /* Note that the bounds are updated at the end of this function
1630 to avoid an infinite recursion since they refer to the type. */
1631 goto discrete_type;
1633 break;
1635 case E_Signed_Integer_Type:
1636 /* For integer types, just make a signed type the appropriate number
1637 of bits. */
1638 gnu_type = make_signed_type (esize);
1639 goto discrete_type;
1641 case E_Ordinary_Fixed_Point_Type:
1642 case E_Decimal_Fixed_Point_Type:
1644 /* Small_Value is the scale factor. */
1645 const Ureal gnat_small_value = Small_Value (gnat_entity);
1646 tree scale_factor = NULL_TREE;
1648 gnu_type = make_signed_type (esize);
1650 /* Try to decode the scale factor and to save it for the fixed-point
1651 types debug hook. */
1653 /* There are various ways to describe the scale factor, however there
1654 are cases where back-end internals cannot hold it. In such cases,
1655 we output invalid scale factor for such cases (i.e. the 0/0
1656 rational constant) but we expect GNAT to output GNAT encodings,
1657 then. Thus, keep this in sync with
1658 Exp_Dbug.Is_Handled_Scale_Factor. */
1660 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1661 binary or decimal scale: it is easier to read for humans. */
1662 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1663 && (Rbase (gnat_small_value) == 2
1664 || Rbase (gnat_small_value) == 10))
1666 /* Given RM restrictions on 'Small values, we assume here that
1667 the denominator fits in an int. */
1668 const tree base = build_int_cst (integer_type_node,
1669 Rbase (gnat_small_value));
1670 const tree exponent
1671 = build_int_cst (integer_type_node,
1672 UI_To_Int (Denominator (gnat_small_value)));
1673 scale_factor
1674 = build2 (RDIV_EXPR, integer_type_node,
1675 integer_one_node,
1676 build2 (POWER_EXPR, integer_type_node,
1677 base, exponent));
1680 /* Default to arbitrary scale factors descriptions. */
1681 else
1683 const Uint num = Norm_Num (gnat_small_value);
1684 const Uint den = Norm_Den (gnat_small_value);
1686 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1688 const tree gnu_num
1689 = build_int_cst (integer_type_node,
1690 UI_To_Int (Norm_Num (gnat_small_value)));
1691 const tree gnu_den
1692 = build_int_cst (integer_type_node,
1693 UI_To_Int (Norm_Den (gnat_small_value)));
1694 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1695 gnu_num, gnu_den);
1697 else
1698 /* If compiler internals cannot represent arbitrary scale
1699 factors, output an invalid scale factor so that debugger
1700 don't try to handle them but so that we still have a type
1701 in the output. Note that GNAT */
1702 scale_factor = integer_zero_node;
1705 TYPE_FIXED_POINT_P (gnu_type) = 1;
1706 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1708 goto discrete_type;
1710 case E_Modular_Integer_Type:
1712 /* For modular types, make the unsigned type of the proper number
1713 of bits and then set up the modulus, if required. */
1714 tree gnu_modulus, gnu_high = NULL_TREE;
1716 /* Packed Array Impl. Types are supposed to be subtypes only. */
1717 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1719 gnu_type = make_unsigned_type (esize);
1721 /* Get the modulus in this type. If it overflows, assume it is because
1722 it is equal to 2**Esize. Note that there is no overflow checking
1723 done on unsigned type, so we detect the overflow by looking for
1724 a modulus of zero, which is otherwise invalid. */
1725 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1727 if (!integer_zerop (gnu_modulus))
1729 TYPE_MODULAR_P (gnu_type) = 1;
1730 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1731 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1732 build_int_cst (gnu_type, 1));
1735 /* If the upper bound is not maximal, make an extra subtype. */
1736 if (gnu_high
1737 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1739 tree gnu_subtype = make_unsigned_type (esize);
1740 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1741 TREE_TYPE (gnu_subtype) = gnu_type;
1742 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1743 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1744 gnu_type = gnu_subtype;
1747 goto discrete_type;
1749 case E_Signed_Integer_Subtype:
1750 case E_Enumeration_Subtype:
1751 case E_Modular_Integer_Subtype:
1752 case E_Ordinary_Fixed_Point_Subtype:
1753 case E_Decimal_Fixed_Point_Subtype:
1755 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1756 not want to call create_range_type since we would like each subtype
1757 node to be distinct. ??? Historically this was in preparation for
1758 when memory aliasing is implemented, but that's obsolete now given
1759 the call to relate_alias_sets below.
1761 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1762 this fact is used by the arithmetic conversion functions.
1764 We elaborate the Ancestor_Subtype if it is not in the current unit
1765 and one of our bounds is non-static. We do this to ensure consistent
1766 naming in the case where several subtypes share the same bounds, by
1767 elaborating the first such subtype first, thus using its name. */
1769 if (!definition
1770 && Present (Ancestor_Subtype (gnat_entity))
1771 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1772 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1773 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1774 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1776 /* Set the precision to the Esize except for bit-packed arrays. */
1777 if (Is_Packed_Array_Impl_Type (gnat_entity)
1778 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1779 esize = UI_To_Int (RM_Size (gnat_entity));
1781 /* First subtypes of Character are treated as Character; otherwise
1782 this should be an unsigned type if the base type is unsigned or
1783 if the lower bound is constant and non-negative or if the type
1784 is biased. */
1785 if (kind == E_Enumeration_Subtype
1786 && No (First_Literal (Etype (gnat_entity)))
1787 && Esize (gnat_entity) == RM_Size (gnat_entity)
1788 && esize == CHAR_TYPE_SIZE
1789 && flag_signed_char)
1790 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1791 else if (Is_Unsigned_Type (Etype (gnat_entity))
1792 || Is_Unsigned_Type (gnat_entity)
1793 || Has_Biased_Representation (gnat_entity))
1794 gnu_type = make_unsigned_type (esize);
1795 else
1796 gnu_type = make_signed_type (esize);
1797 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1799 SET_TYPE_RM_MIN_VALUE
1800 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1801 gnat_entity, "L", definition, true,
1802 debug_info_p));
1804 SET_TYPE_RM_MAX_VALUE
1805 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1806 gnat_entity, "U", definition, true,
1807 debug_info_p));
1809 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1810 = Has_Biased_Representation (gnat_entity);
1812 /* Set TYPE_STRING_FLAG for Character and Wide_Character subtypes. */
1813 TYPE_STRING_FLAG (gnu_type) = TYPE_STRING_FLAG (TREE_TYPE (gnu_type));
1815 /* Inherit our alias set from what we're a subtype of. Subtypes
1816 are not different types and a pointer can designate any instance
1817 within a subtype hierarchy. */
1818 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1820 /* One of the above calls might have caused us to be elaborated,
1821 so don't blow up if so. */
1822 if (present_gnu_tree (gnat_entity))
1824 maybe_present = true;
1825 break;
1828 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1829 TYPE_STUB_DECL (gnu_type)
1830 = create_type_stub_decl (gnu_entity_name, gnu_type);
1832 /* For a packed array, make the original array type a parallel/debug
1833 type. */
1834 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1835 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1837 discrete_type:
1839 /* We have to handle clauses that under-align the type specially. */
1840 if ((Present (Alignment_Clause (gnat_entity))
1841 || (Is_Packed_Array_Impl_Type (gnat_entity)
1842 && Present
1843 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1844 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1846 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1847 if (align >= TYPE_ALIGN (gnu_type))
1848 align = 0;
1851 /* If the type we are dealing with represents a bit-packed array,
1852 we need to have the bits left justified on big-endian targets
1853 and right justified on little-endian targets. We also need to
1854 ensure that when the value is read (e.g. for comparison of two
1855 such values), we only get the good bits, since the unused bits
1856 are uninitialized. Both goals are accomplished by wrapping up
1857 the modular type in an enclosing record type. */
1858 if (Is_Packed_Array_Impl_Type (gnat_entity)
1859 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1861 tree gnu_field_type, gnu_field;
1863 /* Set the RM size before wrapping up the original type. */
1864 SET_TYPE_RM_SIZE (gnu_type,
1865 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1866 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1868 /* Strip the ___XP suffix for standard DWARF. */
1869 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1870 gnu_entity_name = TYPE_NAME (gnu_type);
1872 /* Create a stripped-down declaration, mainly for debugging. */
1873 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1874 gnat_entity);
1876 /* Now save it and build the enclosing record type. */
1877 gnu_field_type = gnu_type;
1879 gnu_type = make_node (RECORD_TYPE);
1880 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1881 TYPE_PACKED (gnu_type) = 1;
1882 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1883 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1884 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1886 /* Propagate the alignment of the modular type to the record type,
1887 unless there is an alignment clause that under-aligns the type.
1888 This means that bit-packed arrays are given "ceil" alignment for
1889 their size by default, which may seem counter-intuitive but makes
1890 it possible to overlay them on modular types easily. */
1891 TYPE_ALIGN (gnu_type)
1892 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1894 /* Propagate the reverse storage order flag to the record type so
1895 that the required byte swapping is performed when retrieving the
1896 enclosed modular value. */
1897 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1898 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1900 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1902 /* Don't declare the field as addressable since we won't be taking
1903 its address and this would prevent create_field_decl from making
1904 a bitfield. */
1905 gnu_field
1906 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1907 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1909 /* Do not emit debug info until after the parallel type is added. */
1910 finish_record_type (gnu_type, gnu_field, 2, false);
1911 compute_record_mode (gnu_type);
1912 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1914 if (debug_info_p)
1916 /* Make the original array type a parallel/debug type. */
1917 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1919 /* Since GNU_TYPE is a padding type around the packed array
1920 implementation type, the padded type is its debug type. */
1921 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1922 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1924 rest_of_record_type_compilation (gnu_type);
1928 /* If the type we are dealing with has got a smaller alignment than the
1929 natural one, we need to wrap it up in a record type and misalign the
1930 latter; we reuse the padding machinery for this purpose. Note that,
1931 even if the record type is marked as packed because of misalignment,
1932 we don't pack the field so as to give it the size of the type. */
1933 else if (align > 0)
1935 tree gnu_field_type, gnu_field;
1937 /* Set the RM size before wrapping up the type. */
1938 SET_TYPE_RM_SIZE (gnu_type,
1939 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1941 /* Create a stripped-down declaration, mainly for debugging. */
1942 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1943 gnat_entity);
1945 /* Now save it and build the enclosing record type. */
1946 gnu_field_type = gnu_type;
1948 gnu_type = make_node (RECORD_TYPE);
1949 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1950 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1951 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1952 TYPE_PACKED (gnu_type) = 1;
1953 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1954 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1955 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1956 TYPE_ALIGN (gnu_type) = align;
1957 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1959 /* Don't declare the field as addressable since we won't be taking
1960 its address and this would prevent create_field_decl from making
1961 a bitfield. */
1962 gnu_field
1963 = create_field_decl (get_identifier ("F"), gnu_field_type,
1964 gnu_type, TYPE_SIZE (gnu_field_type),
1965 bitsize_zero_node, 0, 0);
1967 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1968 compute_record_mode (gnu_type);
1969 TYPE_PADDING_P (gnu_type) = 1;
1972 break;
1974 case E_Floating_Point_Type:
1975 /* The type of the Low and High bounds can be our type if this is
1976 a type from Standard, so set them at the end of the function. */
1977 gnu_type = make_node (REAL_TYPE);
1978 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1979 layout_type (gnu_type);
1980 break;
1982 case E_Floating_Point_Subtype:
1983 /* See the E_Signed_Integer_Subtype case for the rationale. */
1984 if (!definition
1985 && Present (Ancestor_Subtype (gnat_entity))
1986 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1987 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1988 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1989 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1991 gnu_type = make_node (REAL_TYPE);
1992 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1993 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1994 TYPE_GCC_MIN_VALUE (gnu_type)
1995 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1996 TYPE_GCC_MAX_VALUE (gnu_type)
1997 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1998 layout_type (gnu_type);
2000 SET_TYPE_RM_MIN_VALUE
2001 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2002 gnat_entity, "L", definition, true,
2003 debug_info_p));
2005 SET_TYPE_RM_MAX_VALUE
2006 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2007 gnat_entity, "U", definition, true,
2008 debug_info_p));
2010 /* Inherit our alias set from what we're a subtype of, as for
2011 integer subtypes. */
2012 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2014 /* One of the above calls might have caused us to be elaborated,
2015 so don't blow up if so. */
2016 maybe_present = true;
2017 break;
2019 /* Array Types and Subtypes
2021 Unconstrained array types are represented by E_Array_Type and
2022 constrained array types are represented by E_Array_Subtype. There
2023 are no actual objects of an unconstrained array type; all we have
2024 are pointers to that type.
2026 The following fields are defined on array types and subtypes:
2028 Component_Type Component type of the array.
2029 Number_Dimensions Number of dimensions (an int).
2030 First_Index Type of first index. */
2032 case E_Array_Type:
2034 const bool convention_fortran_p
2035 = (Convention (gnat_entity) == Convention_Fortran);
2036 const int ndim = Number_Dimensions (gnat_entity);
2037 tree gnu_template_type;
2038 tree gnu_ptr_template;
2039 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2040 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2041 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2042 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2043 Entity_Id gnat_index, gnat_name;
2044 int index;
2045 tree comp_type;
2047 /* Create the type for the component now, as it simplifies breaking
2048 type reference loops. */
2049 comp_type
2050 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2051 if (present_gnu_tree (gnat_entity))
2053 /* As a side effect, the type may have been translated. */
2054 maybe_present = true;
2055 break;
2058 /* We complete an existing dummy fat pointer type in place. This both
2059 avoids further complex adjustments in update_pointer_to and yields
2060 better debugging information in DWARF by leveraging the support for
2061 incomplete declarations of "tagged" types in the DWARF back-end. */
2062 gnu_type = get_dummy_type (gnat_entity);
2063 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2065 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2066 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2067 /* Save the contents of the dummy type for update_pointer_to. */
2068 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2069 gnu_ptr_template =
2070 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2071 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2073 else
2075 gnu_fat_type = make_node (RECORD_TYPE);
2076 gnu_template_type = make_node (RECORD_TYPE);
2077 gnu_ptr_template = build_pointer_type (gnu_template_type);
2080 /* Make a node for the array. If we are not defining the array
2081 suppress expanding incomplete types. */
2082 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2084 if (!definition)
2086 defer_incomplete_level++;
2087 this_deferred = true;
2090 /* Build the fat pointer type. Use a "void *" object instead of
2091 a pointer to the array type since we don't have the array type
2092 yet (it will reference the fat pointer via the bounds). */
2094 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2095 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2096 DECL_CHAIN (tem)
2097 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2098 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2100 if (COMPLETE_TYPE_P (gnu_fat_type))
2102 /* We are going to lay it out again so reset the alias set. */
2103 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2104 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2105 finish_fat_pointer_type (gnu_fat_type, tem);
2106 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2107 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2109 TYPE_FIELDS (t) = tem;
2110 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2113 else
2115 finish_fat_pointer_type (gnu_fat_type, tem);
2116 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2119 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2120 is the fat pointer. This will be used to access the individual
2121 fields once we build them. */
2122 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2123 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2124 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2125 gnu_template_reference
2126 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2127 TREE_READONLY (gnu_template_reference) = 1;
2128 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2130 /* Now create the GCC type for each index and add the fields for that
2131 index to the template. */
2132 for (index = (convention_fortran_p ? ndim - 1 : 0),
2133 gnat_index = First_Index (gnat_entity);
2134 0 <= index && index < ndim;
2135 index += (convention_fortran_p ? - 1 : 1),
2136 gnat_index = Next_Index (gnat_index))
2138 char field_name[16];
2139 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2140 tree gnu_index_base_type
2141 = maybe_character_type (get_base_type (gnu_index_type));
2142 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2143 tree gnu_min, gnu_max, gnu_high;
2145 /* Make the FIELD_DECLs for the low and high bounds of this
2146 type and then make extractions of these fields from the
2147 template. */
2148 sprintf (field_name, "LB%d", index);
2149 gnu_lb_field = create_field_decl (get_identifier (field_name),
2150 gnu_index_base_type,
2151 gnu_template_type, NULL_TREE,
2152 NULL_TREE, 0, 0);
2153 Sloc_to_locus (Sloc (gnat_entity),
2154 &DECL_SOURCE_LOCATION (gnu_lb_field));
2156 field_name[0] = 'U';
2157 gnu_hb_field = create_field_decl (get_identifier (field_name),
2158 gnu_index_base_type,
2159 gnu_template_type, NULL_TREE,
2160 NULL_TREE, 0, 0);
2161 Sloc_to_locus (Sloc (gnat_entity),
2162 &DECL_SOURCE_LOCATION (gnu_hb_field));
2164 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2166 /* We can't use build_component_ref here since the template type
2167 isn't complete yet. */
2168 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2169 gnu_template_reference, gnu_lb_field,
2170 NULL_TREE);
2171 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2172 gnu_template_reference, gnu_hb_field,
2173 NULL_TREE);
2174 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2176 gnu_min = convert (sizetype, gnu_orig_min);
2177 gnu_max = convert (sizetype, gnu_orig_max);
2179 /* Compute the size of this dimension. See the E_Array_Subtype
2180 case below for the rationale. */
2181 gnu_high
2182 = build3 (COND_EXPR, sizetype,
2183 build2 (GE_EXPR, boolean_type_node,
2184 gnu_orig_max, gnu_orig_min),
2185 gnu_max,
2186 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2188 /* Make a range type with the new range in the Ada base type.
2189 Then make an index type with the size range in sizetype. */
2190 gnu_index_types[index]
2191 = create_index_type (gnu_min, gnu_high,
2192 create_range_type (gnu_index_base_type,
2193 gnu_orig_min,
2194 gnu_orig_max),
2195 gnat_entity);
2197 /* Update the maximum size of the array in elements. */
2198 if (gnu_max_size)
2200 tree gnu_min
2201 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2202 tree gnu_max
2203 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2204 tree gnu_this_max
2205 = size_binop (PLUS_EXPR, size_one_node,
2206 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2208 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2209 && TREE_OVERFLOW (gnu_this_max))
2210 gnu_max_size = NULL_TREE;
2211 else
2212 gnu_max_size
2213 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2216 TYPE_NAME (gnu_index_types[index])
2217 = create_concat_name (gnat_entity, field_name);
2220 /* Install all the fields into the template. */
2221 TYPE_NAME (gnu_template_type)
2222 = create_concat_name (gnat_entity, "XUB");
2223 gnu_template_fields = NULL_TREE;
2224 for (index = 0; index < ndim; index++)
2225 gnu_template_fields
2226 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2227 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2228 debug_info_p);
2229 TYPE_READONLY (gnu_template_type) = 1;
2231 /* If Component_Size is not already specified, annotate it with the
2232 size of the component. */
2233 if (Unknown_Component_Size (gnat_entity))
2234 Set_Component_Size (gnat_entity,
2235 annotate_value (TYPE_SIZE (comp_type)));
2237 /* Compute the maximum size of the array in units and bits. */
2238 if (gnu_max_size)
2240 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2241 TYPE_SIZE_UNIT (comp_type));
2242 gnu_max_size = size_binop (MULT_EXPR,
2243 convert (bitsizetype, gnu_max_size),
2244 TYPE_SIZE (comp_type));
2246 else
2247 gnu_max_size_unit = NULL_TREE;
2249 /* Now build the array type. */
2250 tem = comp_type;
2251 for (index = ndim - 1; index >= 0; index--)
2253 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2254 if (index == ndim - 1)
2255 TYPE_REVERSE_STORAGE_ORDER (tem)
2256 = Reverse_Storage_Order (gnat_entity);
2257 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2258 if (array_type_has_nonaliased_component (tem, gnat_entity))
2259 TYPE_NONALIASED_COMPONENT (tem) = 1;
2262 /* If an alignment is specified, use it if valid. But ignore it
2263 for the original type of packed array types. If the alignment
2264 was requested with an explicit alignment clause, state so. */
2265 if (No (Packed_Array_Impl_Type (gnat_entity))
2266 && Known_Alignment (gnat_entity))
2268 TYPE_ALIGN (tem)
2269 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2270 TYPE_ALIGN (tem));
2271 if (Present (Alignment_Clause (gnat_entity)))
2272 TYPE_USER_ALIGN (tem) = 1;
2275 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2277 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2278 implementation types as such so that the debug information back-end
2279 can output the appropriate description for them. */
2280 TYPE_PACKED (tem)
2281 = (Is_Packed (gnat_entity)
2282 || Is_Packed_Array_Impl_Type (gnat_entity));
2284 if (Treat_As_Volatile (gnat_entity))
2285 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2287 /* Adjust the type of the pointer-to-array field of the fat pointer
2288 and record the aliasing relationships if necessary. */
2289 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2290 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2291 record_component_aliases (gnu_fat_type);
2293 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2294 corresponding fat pointer. */
2295 TREE_TYPE (gnu_type) = gnu_fat_type;
2296 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2297 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2298 SET_TYPE_MODE (gnu_type, BLKmode);
2299 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2301 /* If the maximum size doesn't overflow, use it. */
2302 if (gnu_max_size
2303 && TREE_CODE (gnu_max_size) == INTEGER_CST
2304 && !TREE_OVERFLOW (gnu_max_size)
2305 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2306 && !TREE_OVERFLOW (gnu_max_size_unit))
2308 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2309 TYPE_SIZE (tem));
2310 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2311 TYPE_SIZE_UNIT (tem));
2314 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2315 artificial_p, debug_info_p, gnat_entity);
2317 /* If told to generate GNAT encodings for them (GDB rely on them at the
2318 moment): give the fat pointer type a name. If this is a packed
2319 array, tell the debugger how to interpret the underlying bits. */
2320 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2321 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2322 else
2323 gnat_name = gnat_entity;
2324 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2325 gnu_entity_name = create_concat_name (gnat_name, "XUP");
2326 create_type_decl (gnu_entity_name, gnu_fat_type, artificial_p,
2327 debug_info_p, gnat_entity);
2329 /* Create the type to be designated by thin pointers: a record type for
2330 the array and its template. We used to shift the fields to have the
2331 template at a negative offset, but this was somewhat of a kludge; we
2332 now shift thin pointer values explicitly but only those which have a
2333 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2334 Note that GDB can handle standard DWARF information for them, so we
2335 don't have to name them as a GNAT encoding, except if specifically
2336 asked to. */
2337 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2338 gnu_entity_name = create_concat_name (gnat_name, "XUT");
2339 else
2340 gnu_entity_name = get_entity_name (gnat_name);
2341 tem = build_unc_object_type (gnu_template_type, tem, gnu_entity_name,
2342 debug_info_p);
2344 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2345 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2347 break;
2349 case E_Array_Subtype:
2351 /* This is the actual data type for array variables. Multidimensional
2352 arrays are implemented as arrays of arrays. Note that arrays which
2353 have sparse enumeration subtypes as index components create sparse
2354 arrays, which is obviously space inefficient but so much easier to
2355 code for now.
2357 Also note that the subtype never refers to the unconstrained array
2358 type, which is somewhat at variance with Ada semantics.
2360 First check to see if this is simply a renaming of the array type.
2361 If so, the result is the array type. */
2363 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2364 if (!Is_Constrained (gnat_entity))
2366 else
2368 Entity_Id gnat_index, gnat_base_index;
2369 const bool convention_fortran_p
2370 = (Convention (gnat_entity) == Convention_Fortran);
2371 const int ndim = Number_Dimensions (gnat_entity);
2372 tree gnu_base_type = gnu_type;
2373 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2374 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2375 bool need_index_type_struct = false;
2376 int index;
2378 /* First create the GCC type for each index and find out whether
2379 special types are needed for debugging information. */
2380 for (index = (convention_fortran_p ? ndim - 1 : 0),
2381 gnat_index = First_Index (gnat_entity),
2382 gnat_base_index
2383 = First_Index (Implementation_Base_Type (gnat_entity));
2384 0 <= index && index < ndim;
2385 index += (convention_fortran_p ? - 1 : 1),
2386 gnat_index = Next_Index (gnat_index),
2387 gnat_base_index = Next_Index (gnat_base_index))
2389 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2390 tree gnu_index_base_type
2391 = maybe_character_type (get_base_type (gnu_index_type));
2392 tree gnu_orig_min
2393 = convert (gnu_index_base_type,
2394 TYPE_MIN_VALUE (gnu_index_type));
2395 tree gnu_orig_max
2396 = convert (gnu_index_base_type,
2397 TYPE_MAX_VALUE (gnu_index_type));
2398 tree gnu_min = convert (sizetype, gnu_orig_min);
2399 tree gnu_max = convert (sizetype, gnu_orig_max);
2400 tree gnu_base_index_type
2401 = get_unpadded_type (Etype (gnat_base_index));
2402 tree gnu_base_index_base_type
2403 = maybe_character_type (get_base_type (gnu_base_index_type));
2404 tree gnu_base_orig_min
2405 = convert (gnu_base_index_base_type,
2406 TYPE_MIN_VALUE (gnu_base_index_type));
2407 tree gnu_base_orig_max
2408 = convert (gnu_base_index_base_type,
2409 TYPE_MAX_VALUE (gnu_base_index_type));
2410 tree gnu_high;
2412 /* See if the base array type is already flat. If it is, we
2413 are probably compiling an ACATS test but it will cause the
2414 code below to malfunction if we don't handle it specially. */
2415 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2416 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2417 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2419 gnu_min = size_one_node;
2420 gnu_max = size_zero_node;
2421 gnu_high = gnu_max;
2424 /* Similarly, if one of the values overflows in sizetype and the
2425 range is null, use 1..0 for the sizetype bounds. */
2426 else if (TREE_CODE (gnu_min) == INTEGER_CST
2427 && TREE_CODE (gnu_max) == INTEGER_CST
2428 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2429 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2431 gnu_min = size_one_node;
2432 gnu_max = size_zero_node;
2433 gnu_high = gnu_max;
2436 /* If the minimum and maximum values both overflow in sizetype,
2437 but the difference in the original type does not overflow in
2438 sizetype, ignore the overflow indication. */
2439 else if (TREE_CODE (gnu_min) == INTEGER_CST
2440 && TREE_CODE (gnu_max) == INTEGER_CST
2441 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2442 && !TREE_OVERFLOW
2443 (convert (sizetype,
2444 fold_build2 (MINUS_EXPR, gnu_index_type,
2445 gnu_orig_max,
2446 gnu_orig_min))))
2448 TREE_OVERFLOW (gnu_min) = 0;
2449 TREE_OVERFLOW (gnu_max) = 0;
2450 gnu_high = gnu_max;
2453 /* Compute the size of this dimension in the general case. We
2454 need to provide GCC with an upper bound to use but have to
2455 deal with the "superflat" case. There are three ways to do
2456 this. If we can prove that the array can never be superflat,
2457 we can just use the high bound of the index type. */
2458 else if ((Nkind (gnat_index) == N_Range
2459 && cannot_be_superflat (gnat_index))
2460 /* Bit-Packed Array Impl. Types are never superflat. */
2461 || (Is_Packed_Array_Impl_Type (gnat_entity)
2462 && Is_Bit_Packed_Array
2463 (Original_Array_Type (gnat_entity))))
2464 gnu_high = gnu_max;
2466 /* Otherwise, if the high bound is constant but the low bound is
2467 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2468 lower bound. Note that the comparison must be done in the
2469 original type to avoid any overflow during the conversion. */
2470 else if (TREE_CODE (gnu_max) == INTEGER_CST
2471 && TREE_CODE (gnu_min) != INTEGER_CST)
2473 gnu_high = gnu_max;
2474 gnu_min
2475 = build_cond_expr (sizetype,
2476 build_binary_op (GE_EXPR,
2477 boolean_type_node,
2478 gnu_orig_max,
2479 gnu_orig_min),
2480 gnu_min,
2481 int_const_binop (PLUS_EXPR, gnu_max,
2482 size_one_node));
2485 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2486 in all the other cases. Note that, here as well as above,
2487 the condition used in the comparison must be equivalent to
2488 the condition (length != 0). This is relied upon in order
2489 to optimize array comparisons in compare_arrays. Moreover
2490 we use int_const_binop for the shift by 1 if the bound is
2491 constant to avoid any unwanted overflow. */
2492 else
2493 gnu_high
2494 = build_cond_expr (sizetype,
2495 build_binary_op (GE_EXPR,
2496 boolean_type_node,
2497 gnu_orig_max,
2498 gnu_orig_min),
2499 gnu_max,
2500 TREE_CODE (gnu_min) == INTEGER_CST
2501 ? int_const_binop (MINUS_EXPR, gnu_min,
2502 size_one_node)
2503 : size_binop (MINUS_EXPR, gnu_min,
2504 size_one_node));
2506 /* Reuse the index type for the range type. Then make an index
2507 type with the size range in sizetype. */
2508 gnu_index_types[index]
2509 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2510 gnat_entity);
2512 /* Update the maximum size of the array in elements. Here we
2513 see if any constraint on the index type of the base type
2514 can be used in the case of self-referential bound on the
2515 index type of the subtype. We look for a non-"infinite"
2516 and non-self-referential bound from any type involved and
2517 handle each bound separately. */
2518 if (gnu_max_size)
2520 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2521 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2522 tree gnu_base_base_min
2523 = convert (sizetype,
2524 TYPE_MIN_VALUE (gnu_base_index_base_type));
2525 tree gnu_base_base_max
2526 = convert (sizetype,
2527 TYPE_MAX_VALUE (gnu_base_index_base_type));
2529 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2530 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2531 && !TREE_OVERFLOW (gnu_base_min)))
2532 gnu_base_min = gnu_min;
2534 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2535 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2536 && !TREE_OVERFLOW (gnu_base_max)))
2537 gnu_base_max = gnu_max;
2539 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2540 && TREE_OVERFLOW (gnu_base_min))
2541 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2542 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2543 && TREE_OVERFLOW (gnu_base_max))
2544 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2545 gnu_max_size = NULL_TREE;
2546 else
2548 tree gnu_this_max;
2550 /* Use int_const_binop if the bounds are constant to
2551 avoid any unwanted overflow. */
2552 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2553 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2554 gnu_this_max
2555 = int_const_binop (PLUS_EXPR, size_one_node,
2556 int_const_binop (MINUS_EXPR,
2557 gnu_base_max,
2558 gnu_base_min));
2559 else
2560 gnu_this_max
2561 = size_binop (PLUS_EXPR, size_one_node,
2562 size_binop (MINUS_EXPR,
2563 gnu_base_max,
2564 gnu_base_min));
2566 gnu_max_size
2567 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2571 /* We need special types for debugging information to point to
2572 the index types if they have variable bounds, are not integer
2573 types, are biased or are wider than sizetype. These are GNAT
2574 encodings, so we have to include them only when all encodings
2575 are requested. */
2576 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2577 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2578 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2579 || (TREE_TYPE (gnu_index_type)
2580 && TREE_CODE (TREE_TYPE (gnu_index_type))
2581 != INTEGER_TYPE)
2582 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2583 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2584 need_index_type_struct = true;
2587 /* Then flatten: create the array of arrays. For an array type
2588 used to implement a packed array, get the component type from
2589 the original array type since the representation clauses that
2590 can affect it are on the latter. */
2591 if (Is_Packed_Array_Impl_Type (gnat_entity)
2592 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2594 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2595 for (index = ndim - 1; index >= 0; index--)
2596 gnu_type = TREE_TYPE (gnu_type);
2598 /* One of the above calls might have caused us to be elaborated,
2599 so don't blow up if so. */
2600 if (present_gnu_tree (gnat_entity))
2602 maybe_present = true;
2603 break;
2606 else
2608 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2609 debug_info_p);
2611 /* One of the above calls might have caused us to be elaborated,
2612 so don't blow up if so. */
2613 if (present_gnu_tree (gnat_entity))
2615 maybe_present = true;
2616 break;
2620 /* Compute the maximum size of the array in units and bits. */
2621 if (gnu_max_size)
2623 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2624 TYPE_SIZE_UNIT (gnu_type));
2625 gnu_max_size = size_binop (MULT_EXPR,
2626 convert (bitsizetype, gnu_max_size),
2627 TYPE_SIZE (gnu_type));
2629 else
2630 gnu_max_size_unit = NULL_TREE;
2632 /* Now build the array type. */
2633 for (index = ndim - 1; index >= 0; index --)
2635 gnu_type = build_nonshared_array_type (gnu_type,
2636 gnu_index_types[index]);
2637 if (index == ndim - 1)
2638 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2639 = Reverse_Storage_Order (gnat_entity);
2640 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2641 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2642 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2645 /* Strip the ___XP suffix for standard DWARF. */
2646 if (Is_Packed_Array_Impl_Type (gnat_entity)
2647 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2649 Entity_Id gnat_original_array_type
2650 = Underlying_Type (Original_Array_Type (gnat_entity));
2652 gnu_entity_name
2653 = get_entity_name (gnat_original_array_type);
2656 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2657 TYPE_STUB_DECL (gnu_type)
2658 = create_type_stub_decl (gnu_entity_name, gnu_type);
2660 /* If we are at file level and this is a multi-dimensional array,
2661 we need to make a variable corresponding to the stride of the
2662 inner dimensions. */
2663 if (global_bindings_p () && ndim > 1)
2665 tree gnu_arr_type;
2667 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2668 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2669 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2671 tree eltype = TREE_TYPE (gnu_arr_type);
2672 char stride_name[32];
2674 sprintf (stride_name, "ST%d", index);
2675 TYPE_SIZE (gnu_arr_type)
2676 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2677 gnat_entity, stride_name,
2678 definition, false);
2680 /* ??? For now, store the size as a multiple of the
2681 alignment of the element type in bytes so that we
2682 can see the alignment from the tree. */
2683 sprintf (stride_name, "ST%d_A_UNIT", index);
2684 TYPE_SIZE_UNIT (gnu_arr_type)
2685 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2686 gnat_entity, stride_name,
2687 definition, false,
2688 TYPE_ALIGN (eltype));
2690 /* ??? create_type_decl is not invoked on the inner types so
2691 the MULT_EXPR node built above will never be marked. */
2692 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2696 /* If we need to write out a record type giving the names of the
2697 bounds for debugging purposes, do it now and make the record
2698 type a parallel type. This is not needed for a packed array
2699 since the bounds are conveyed by the original array type. */
2700 if (need_index_type_struct
2701 && debug_info_p
2702 && !Is_Packed_Array_Impl_Type (gnat_entity))
2704 tree gnu_bound_rec = make_node (RECORD_TYPE);
2705 tree gnu_field_list = NULL_TREE;
2706 tree gnu_field;
2708 TYPE_NAME (gnu_bound_rec)
2709 = create_concat_name (gnat_entity, "XA");
2711 for (index = ndim - 1; index >= 0; index--)
2713 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2714 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2716 /* Make sure to reference the types themselves, and not just
2717 their names, as the debugger may fall back on them. */
2718 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2719 gnu_bound_rec, NULL_TREE,
2720 NULL_TREE, 0, 0);
2721 DECL_CHAIN (gnu_field) = gnu_field_list;
2722 gnu_field_list = gnu_field;
2725 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2726 add_parallel_type (gnu_type, gnu_bound_rec);
2729 /* If this is a packed array type, make the original array type a
2730 parallel/debug type. Otherwise, if such GNAT encodings are
2731 required, do it for the base array type if it isn't artificial to
2732 make sure it is kept in the debug info. */
2733 if (debug_info_p)
2735 if (Is_Packed_Array_Impl_Type (gnat_entity))
2736 associate_original_type_to_packed_array (gnu_type,
2737 gnat_entity);
2738 else
2740 tree gnu_base_decl
2741 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2742 if (!DECL_ARTIFICIAL (gnu_base_decl)
2743 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2744 add_parallel_type (gnu_type,
2745 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2749 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2750 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2751 = (Is_Packed_Array_Impl_Type (gnat_entity)
2752 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2754 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2755 implementation types as such so that the debug information back-end
2756 can output the appropriate description for them. */
2757 TYPE_PACKED (gnu_type)
2758 = (Is_Packed (gnat_entity)
2759 || Is_Packed_Array_Impl_Type (gnat_entity));
2761 /* If the size is self-referential and the maximum size doesn't
2762 overflow, use it. */
2763 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2764 && gnu_max_size
2765 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2766 && TREE_OVERFLOW (gnu_max_size))
2767 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2768 && TREE_OVERFLOW (gnu_max_size_unit)))
2770 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2771 TYPE_SIZE (gnu_type));
2772 TYPE_SIZE_UNIT (gnu_type)
2773 = size_binop (MIN_EXPR, gnu_max_size_unit,
2774 TYPE_SIZE_UNIT (gnu_type));
2777 /* Set our alias set to that of our base type. This gives all
2778 array subtypes the same alias set. */
2779 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2781 /* If this is a packed type, make this type the same as the packed
2782 array type, but do some adjusting in the type first. */
2783 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2785 Entity_Id gnat_index;
2786 tree gnu_inner;
2788 /* First finish the type we had been making so that we output
2789 debugging information for it. */
2790 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2791 if (Treat_As_Volatile (gnat_entity))
2793 const int quals
2794 = TYPE_QUAL_VOLATILE
2795 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2796 gnu_type = change_qualified_type (gnu_type, quals);
2798 /* Make it artificial only if the base type was artificial too.
2799 That's sort of "morally" true and will make it possible for
2800 the debugger to look it up by name in DWARF, which is needed
2801 in order to decode the packed array type. */
2802 gnu_decl
2803 = create_type_decl (gnu_entity_name, gnu_type,
2804 !Comes_From_Source (Etype (gnat_entity))
2805 && artificial_p, debug_info_p,
2806 gnat_entity);
2808 /* Save it as our equivalent in case the call below elaborates
2809 this type again. */
2810 save_gnu_tree (gnat_entity, gnu_decl, false);
2812 gnu_decl
2813 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2814 NULL_TREE, 0);
2815 this_made_decl = true;
2816 gnu_type = TREE_TYPE (gnu_decl);
2818 save_gnu_tree (gnat_entity, NULL_TREE, false);
2820 gnu_inner = gnu_type;
2821 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2822 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2823 || TYPE_PADDING_P (gnu_inner)))
2824 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2826 /* We need to attach the index type to the type we just made so
2827 that the actual bounds can later be put into a template. */
2828 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2829 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2830 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2831 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2833 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2835 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2836 TYPE_MODULUS for modular types so we make an extra
2837 subtype if necessary. */
2838 if (TYPE_MODULAR_P (gnu_inner))
2840 tree gnu_subtype
2841 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2842 TREE_TYPE (gnu_subtype) = gnu_inner;
2843 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2844 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2845 TYPE_MIN_VALUE (gnu_inner));
2846 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2847 TYPE_MAX_VALUE (gnu_inner));
2848 gnu_inner = gnu_subtype;
2851 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2853 /* Check for other cases of overloading. */
2854 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2857 for (gnat_index = First_Index (gnat_entity);
2858 Present (gnat_index);
2859 gnat_index = Next_Index (gnat_index))
2860 SET_TYPE_ACTUAL_BOUNDS
2861 (gnu_inner,
2862 tree_cons (NULL_TREE,
2863 get_unpadded_type (Etype (gnat_index)),
2864 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2866 if (Convention (gnat_entity) != Convention_Fortran)
2867 SET_TYPE_ACTUAL_BOUNDS
2868 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2870 if (TREE_CODE (gnu_type) == RECORD_TYPE
2871 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2872 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2876 else
2877 /* Abort if packed array with no Packed_Array_Impl_Type. */
2878 gcc_assert (!Is_Packed (gnat_entity));
2880 break;
2882 case E_String_Literal_Subtype:
2883 /* Create the type for a string literal. */
2885 Entity_Id gnat_full_type
2886 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2887 && Present (Full_View (Etype (gnat_entity)))
2888 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2889 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2890 tree gnu_string_array_type
2891 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2892 tree gnu_string_index_type
2893 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2894 (TYPE_DOMAIN (gnu_string_array_type))));
2895 tree gnu_lower_bound
2896 = convert (gnu_string_index_type,
2897 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2898 tree gnu_length
2899 = UI_To_gnu (String_Literal_Length (gnat_entity),
2900 gnu_string_index_type);
2901 tree gnu_upper_bound
2902 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2903 gnu_lower_bound,
2904 int_const_binop (MINUS_EXPR, gnu_length,
2905 convert (gnu_string_index_type,
2906 integer_one_node)));
2907 tree gnu_index_type
2908 = create_index_type (convert (sizetype, gnu_lower_bound),
2909 convert (sizetype, gnu_upper_bound),
2910 create_range_type (gnu_string_index_type,
2911 gnu_lower_bound,
2912 gnu_upper_bound),
2913 gnat_entity);
2915 gnu_type
2916 = build_nonshared_array_type (gnat_to_gnu_type
2917 (Component_Type (gnat_entity)),
2918 gnu_index_type);
2919 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2920 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2921 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2923 break;
2925 /* Record Types and Subtypes
2927 The following fields are defined on record types:
2929 Has_Discriminants True if the record has discriminants
2930 First_Discriminant Points to head of list of discriminants
2931 First_Entity Points to head of list of fields
2932 Is_Tagged_Type True if the record is tagged
2934 Implementation of Ada records and discriminated records:
2936 A record type definition is transformed into the equivalent of a C
2937 struct definition. The fields that are the discriminants which are
2938 found in the Full_Type_Declaration node and the elements of the
2939 Component_List found in the Record_Type_Definition node. The
2940 Component_List can be a recursive structure since each Variant of
2941 the Variant_Part of the Component_List has a Component_List.
2943 Processing of a record type definition comprises starting the list of
2944 field declarations here from the discriminants and the calling the
2945 function components_to_record to add the rest of the fields from the
2946 component list and return the gnu type node. The function
2947 components_to_record will call itself recursively as it traverses
2948 the tree. */
2950 case E_Record_Type:
2951 if (Has_Complex_Representation (gnat_entity))
2953 gnu_type
2954 = build_complex_type
2955 (get_unpadded_type
2956 (Etype (Defining_Entity
2957 (First (Component_Items
2958 (Component_List
2959 (Type_Definition
2960 (Declaration_Node (gnat_entity)))))))));
2962 break;
2966 Node_Id full_definition = Declaration_Node (gnat_entity);
2967 Node_Id record_definition = Type_Definition (full_definition);
2968 Node_Id gnat_constr;
2969 Entity_Id gnat_field;
2970 tree gnu_field, gnu_field_list = NULL_TREE;
2971 tree gnu_get_parent;
2972 /* Set PACKED in keeping with gnat_to_gnu_field. */
2973 const int packed
2974 = Is_Packed (gnat_entity)
2976 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2977 ? -1
2978 : 0;
2979 const bool has_align = Known_Alignment (gnat_entity);
2980 const bool has_discr = Has_Discriminants (gnat_entity);
2981 const bool has_rep = Has_Specified_Layout (gnat_entity);
2982 const bool is_extension
2983 = (Is_Tagged_Type (gnat_entity)
2984 && Nkind (record_definition) == N_Derived_Type_Definition);
2985 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2986 bool all_rep = has_rep;
2988 /* See if all fields have a rep clause. Stop when we find one
2989 that doesn't. */
2990 if (all_rep)
2991 for (gnat_field = First_Entity (gnat_entity);
2992 Present (gnat_field);
2993 gnat_field = Next_Entity (gnat_field))
2994 if ((Ekind (gnat_field) == E_Component
2995 || Ekind (gnat_field) == E_Discriminant)
2996 && No (Component_Clause (gnat_field)))
2998 all_rep = false;
2999 break;
3002 /* If this is a record extension, go a level further to find the
3003 record definition. Also, verify we have a Parent_Subtype. */
3004 if (is_extension)
3006 if (!type_annotate_only
3007 || Present (Record_Extension_Part (record_definition)))
3008 record_definition = Record_Extension_Part (record_definition);
3010 gcc_assert (type_annotate_only
3011 || Present (Parent_Subtype (gnat_entity)));
3014 /* Make a node for the record. If we are not defining the record,
3015 suppress expanding incomplete types. */
3016 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3017 TYPE_NAME (gnu_type) = gnu_entity_name;
3018 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3019 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3020 = Reverse_Storage_Order (gnat_entity);
3021 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3023 if (!definition)
3025 defer_incomplete_level++;
3026 this_deferred = true;
3029 /* If both a size and rep clause were specified, put the size on
3030 the record type now so that it can get the proper layout. */
3031 if (has_rep && Known_RM_Size (gnat_entity))
3032 TYPE_SIZE (gnu_type)
3033 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3035 /* Always set the alignment on the record type here so that it can
3036 get the proper layout. */
3037 if (has_align)
3038 TYPE_ALIGN (gnu_type)
3039 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
3040 else
3042 TYPE_ALIGN (gnu_type) = 0;
3044 /* If a type needs strict alignment, the minimum size will be the
3045 type size instead of the RM size (see validate_size). Cap the
3046 alignment lest it causes this type size to become too large. */
3047 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3049 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3050 unsigned int max_align = max_size & -max_size;
3051 if (max_align < BIGGEST_ALIGNMENT)
3052 TYPE_MAX_ALIGN (gnu_type) = max_align;
3056 /* If we have a Parent_Subtype, make a field for the parent. If
3057 this record has rep clauses, force the position to zero. */
3058 if (Present (Parent_Subtype (gnat_entity)))
3060 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3061 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3062 tree gnu_parent;
3064 /* A major complexity here is that the parent subtype will
3065 reference our discriminants in its Stored_Constraint list.
3066 But those must reference the parent component of this record
3067 which is precisely of the parent subtype we have not built yet!
3068 To break the circle we first build a dummy COMPONENT_REF which
3069 represents the "get to the parent" operation and initialize
3070 each of those discriminants to a COMPONENT_REF of the above
3071 dummy parent referencing the corresponding discriminant of the
3072 base type of the parent subtype. */
3073 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3074 build0 (PLACEHOLDER_EXPR, gnu_type),
3075 build_decl (input_location,
3076 FIELD_DECL, NULL_TREE,
3077 gnu_dummy_parent_type),
3078 NULL_TREE);
3080 if (has_discr)
3081 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3082 Present (gnat_field);
3083 gnat_field = Next_Stored_Discriminant (gnat_field))
3084 if (Present (Corresponding_Discriminant (gnat_field)))
3086 tree gnu_field
3087 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3088 (gnat_field));
3089 save_gnu_tree
3090 (gnat_field,
3091 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3092 gnu_get_parent, gnu_field, NULL_TREE),
3093 true);
3096 /* Then we build the parent subtype. If it has discriminants but
3097 the type itself has unknown discriminants, this means that it
3098 doesn't contain information about how the discriminants are
3099 derived from those of the ancestor type, so it cannot be used
3100 directly. Instead it is built by cloning the parent subtype
3101 of the underlying record view of the type, for which the above
3102 derivation of discriminants has been made explicit. */
3103 if (Has_Discriminants (gnat_parent)
3104 && Has_Unknown_Discriminants (gnat_entity))
3106 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3108 /* If we are defining the type, the underlying record
3109 view must already have been elaborated at this point.
3110 Otherwise do it now as its parent subtype cannot be
3111 technically elaborated on its own. */
3112 if (definition)
3113 gcc_assert (present_gnu_tree (gnat_uview));
3114 else
3115 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3117 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3119 /* Substitute the "get to the parent" of the type for that
3120 of its underlying record view in the cloned type. */
3121 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3122 Present (gnat_field);
3123 gnat_field = Next_Stored_Discriminant (gnat_field))
3124 if (Present (Corresponding_Discriminant (gnat_field)))
3126 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3127 tree gnu_ref
3128 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3129 gnu_get_parent, gnu_field, NULL_TREE);
3130 gnu_parent
3131 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3134 else
3135 gnu_parent = gnat_to_gnu_type (gnat_parent);
3137 /* The parent field needs strict alignment so, if it is to
3138 be created with a component clause below, then we need
3139 to apply the same adjustment as in gnat_to_gnu_field. */
3140 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3141 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_parent);
3143 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3144 initially built. The discriminants must reference the fields
3145 of the parent subtype and not those of its base type for the
3146 placeholder machinery to properly work. */
3147 if (has_discr)
3149 /* The actual parent subtype is the full view. */
3150 if (IN (Ekind (gnat_parent), Private_Kind))
3152 if (Present (Full_View (gnat_parent)))
3153 gnat_parent = Full_View (gnat_parent);
3154 else
3155 gnat_parent = Underlying_Full_View (gnat_parent);
3158 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3159 Present (gnat_field);
3160 gnat_field = Next_Stored_Discriminant (gnat_field))
3161 if (Present (Corresponding_Discriminant (gnat_field)))
3163 Entity_Id field;
3164 for (field = First_Stored_Discriminant (gnat_parent);
3165 Present (field);
3166 field = Next_Stored_Discriminant (field))
3167 if (same_discriminant_p (gnat_field, field))
3168 break;
3169 gcc_assert (Present (field));
3170 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3171 = gnat_to_gnu_field_decl (field);
3175 /* The "get to the parent" COMPONENT_REF must be given its
3176 proper type... */
3177 TREE_TYPE (gnu_get_parent) = gnu_parent;
3179 /* ...and reference the _Parent field of this record. */
3180 gnu_field
3181 = create_field_decl (parent_name_id,
3182 gnu_parent, gnu_type,
3183 has_rep
3184 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3185 has_rep
3186 ? bitsize_zero_node : NULL_TREE,
3187 0, 1);
3188 DECL_INTERNAL_P (gnu_field) = 1;
3189 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3190 TYPE_FIELDS (gnu_type) = gnu_field;
3193 /* Make the fields for the discriminants and put them into the record
3194 unless it's an Unchecked_Union. */
3195 if (has_discr)
3196 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3197 Present (gnat_field);
3198 gnat_field = Next_Stored_Discriminant (gnat_field))
3200 /* If this is a record extension and this discriminant is the
3201 renaming of another discriminant, we've handled it above. */
3202 if (Present (Parent_Subtype (gnat_entity))
3203 && Present (Corresponding_Discriminant (gnat_field)))
3204 continue;
3206 /* However, if we are just annotating types, the Parent_Subtype
3207 doesn't exist so we need skip the discriminant altogether. */
3208 if (type_annotate_only
3209 && Is_Tagged_Type (gnat_entity)
3210 && Is_Derived_Type (gnat_entity)
3211 && Present (Corresponding_Discriminant (gnat_field)))
3212 continue;
3214 gnu_field
3215 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3216 debug_info_p);
3218 /* Make an expression using a PLACEHOLDER_EXPR from the
3219 FIELD_DECL node just created and link that with the
3220 corresponding GNAT defining identifier. */
3221 save_gnu_tree (gnat_field,
3222 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3223 build0 (PLACEHOLDER_EXPR, gnu_type),
3224 gnu_field, NULL_TREE),
3225 true);
3227 if (!is_unchecked_union)
3229 DECL_CHAIN (gnu_field) = gnu_field_list;
3230 gnu_field_list = gnu_field;
3234 /* If we have a derived untagged type that renames discriminants in
3235 the root type, the (stored) discriminants are a just copy of the
3236 discriminants of the root type. This means that any constraints
3237 added by the renaming in the derivation are disregarded as far
3238 as the layout of the derived type is concerned. To rescue them,
3239 we change the type of the (stored) discriminants to a subtype
3240 with the bounds of the type of the visible discriminants. */
3241 if (has_discr
3242 && !is_extension
3243 && Stored_Constraint (gnat_entity) != No_Elist)
3244 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3245 gnat_constr != No_Elmt;
3246 gnat_constr = Next_Elmt (gnat_constr))
3247 if (Nkind (Node (gnat_constr)) == N_Identifier
3248 /* Ignore access discriminants. */
3249 && !Is_Access_Type (Etype (Node (gnat_constr)))
3250 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3252 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3253 tree gnu_discr_type, gnu_ref;
3255 /* If the scope of the discriminant is not the record type,
3256 this means that we're processing the implicit full view
3257 of a type derived from a private discriminated type: in
3258 this case, the Stored_Constraint list is simply copied
3259 from the partial view, see Build_Derived_Private_Type.
3260 So we need to retrieve the corresponding discriminant
3261 of the implicit full view, otherwise we will abort. */
3262 if (Scope (gnat_discr) != gnat_entity)
3264 Entity_Id field;
3265 for (field = First_Entity (gnat_entity);
3266 Present (field);
3267 field = Next_Entity (field))
3268 if (Ekind (field) == E_Discriminant
3269 && same_discriminant_p (gnat_discr, field))
3270 break;
3271 gcc_assert (Present (field));
3272 gnat_discr = field;
3275 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3276 gnu_ref
3277 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3278 NULL_TREE, 0);
3280 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3281 just above for one of the stored discriminants. */
3282 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3284 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3286 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3287 tree gnu_subtype
3288 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3289 ? make_unsigned_type (prec) : make_signed_type (prec);
3290 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3291 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3292 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3293 TYPE_MIN_VALUE (gnu_discr_type));
3294 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3295 TYPE_MAX_VALUE (gnu_discr_type));
3296 TREE_TYPE (gnu_ref)
3297 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3301 /* Add the fields into the record type and finish it up. */
3302 components_to_record (gnu_type, Component_List (record_definition),
3303 gnu_field_list, packed, definition, false,
3304 all_rep, is_unchecked_union,
3305 artificial_p, debug_info_p,
3306 false, OK_To_Reorder_Components (gnat_entity),
3307 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3309 /* Fill in locations of fields. */
3310 annotate_rep (gnat_entity, gnu_type);
3312 /* If there are any entities in the chain corresponding to components
3313 that we did not elaborate, ensure we elaborate their types if they
3314 are Itypes. */
3315 for (gnat_temp = First_Entity (gnat_entity);
3316 Present (gnat_temp);
3317 gnat_temp = Next_Entity (gnat_temp))
3318 if ((Ekind (gnat_temp) == E_Component
3319 || Ekind (gnat_temp) == E_Discriminant)
3320 && Is_Itype (Etype (gnat_temp))
3321 && !present_gnu_tree (gnat_temp))
3322 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3324 /* If this is a record type associated with an exception definition,
3325 equate its fields to those of the standard exception type. This
3326 will make it possible to convert between them. */
3327 if (gnu_entity_name == exception_data_name_id)
3329 tree gnu_std_field;
3330 for (gnu_field = TYPE_FIELDS (gnu_type),
3331 gnu_std_field = TYPE_FIELDS (except_type_node);
3332 gnu_field;
3333 gnu_field = DECL_CHAIN (gnu_field),
3334 gnu_std_field = DECL_CHAIN (gnu_std_field))
3335 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3336 gcc_assert (!gnu_std_field);
3339 break;
3341 case E_Class_Wide_Subtype:
3342 /* If an equivalent type is present, that is what we should use.
3343 Otherwise, fall through to handle this like a record subtype
3344 since it may have constraints. */
3345 if (gnat_equiv_type != gnat_entity)
3347 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3348 maybe_present = true;
3349 break;
3352 /* ... fall through ... */
3354 case E_Record_Subtype:
3355 /* If Cloned_Subtype is Present it means this record subtype has
3356 identical layout to that type or subtype and we should use
3357 that GCC type for this one. The front end guarantees that
3358 the component list is shared. */
3359 if (Present (Cloned_Subtype (gnat_entity)))
3361 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3362 NULL_TREE, 0);
3363 maybe_present = true;
3364 break;
3367 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3368 changing the type, make a new type with each field having the type of
3369 the field in the new subtype but the position computed by transforming
3370 every discriminant reference according to the constraints. We don't
3371 see any difference between private and non-private type here since
3372 derivations from types should have been deferred until the completion
3373 of the private type. */
3374 else
3376 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3377 tree gnu_base_type;
3379 if (!definition)
3381 defer_incomplete_level++;
3382 this_deferred = true;
3385 gnu_base_type
3386 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3388 if (present_gnu_tree (gnat_entity))
3390 maybe_present = true;
3391 break;
3394 /* If this is a record subtype associated with a dispatch table,
3395 strip the suffix. This is necessary to make sure 2 different
3396 subtypes associated with the imported and exported views of a
3397 dispatch table are properly merged in LTO mode. */
3398 if (Is_Dispatch_Table_Entity (gnat_entity))
3400 char *p;
3401 Get_Encoded_Name (gnat_entity);
3402 p = strchr (Name_Buffer, '_');
3403 gcc_assert (p);
3404 strcpy (p+2, "dtS");
3405 gnu_entity_name = get_identifier (Name_Buffer);
3408 /* When the subtype has discriminants and these discriminants affect
3409 the initial shape it has inherited, factor them in. But for an
3410 Unchecked_Union (it must be an Itype), just return the type.
3411 We can't just test Is_Constrained because private subtypes without
3412 discriminants of types with discriminants with default expressions
3413 are Is_Constrained but aren't constrained! */
3414 if (IN (Ekind (gnat_base_type), Record_Kind)
3415 && !Is_Unchecked_Union (gnat_base_type)
3416 && !Is_For_Access_Subtype (gnat_entity)
3417 && Has_Discriminants (gnat_entity)
3418 && Is_Constrained (gnat_entity)
3419 && Stored_Constraint (gnat_entity) != No_Elist)
3421 vec<subst_pair> gnu_subst_list
3422 = build_subst_list (gnat_entity, gnat_base_type, definition);
3423 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3424 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3425 bool selected_variant = false, all_constant_pos = true;
3426 Entity_Id gnat_field;
3427 vec<variant_desc> gnu_variant_list;
3429 gnu_type = make_node (RECORD_TYPE);
3430 TYPE_NAME (gnu_type) = gnu_entity_name;
3431 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3432 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3433 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3434 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3435 = Reverse_Storage_Order (gnat_entity);
3436 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3438 /* Set the size, alignment and alias set of the new type to
3439 match that of the old one, doing required substitutions. */
3440 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3441 gnu_subst_list);
3443 if (TYPE_IS_PADDING_P (gnu_base_type))
3444 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3445 else
3446 gnu_unpad_base_type = gnu_base_type;
3448 /* Look for REP and variant parts in the base type. */
3449 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3450 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3452 /* If there is a variant part, we must compute whether the
3453 constraints statically select a particular variant. If
3454 so, we simply drop the qualified union and flatten the
3455 list of fields. Otherwise we'll build a new qualified
3456 union for the variants that are still relevant. */
3457 if (gnu_variant_part)
3459 variant_desc *v;
3460 unsigned int i;
3462 gnu_variant_list
3463 = build_variant_list (TREE_TYPE (gnu_variant_part),
3464 gnu_subst_list,
3465 vNULL);
3467 /* If all the qualifiers are unconditionally true, the
3468 innermost variant is statically selected. */
3469 selected_variant = true;
3470 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3471 if (!integer_onep (v->qual))
3473 selected_variant = false;
3474 break;
3477 /* Otherwise, create the new variants. */
3478 if (!selected_variant)
3479 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3481 tree old_variant = v->type;
3482 tree new_variant = make_node (RECORD_TYPE);
3483 tree suffix
3484 = concat_name (DECL_NAME (gnu_variant_part),
3485 IDENTIFIER_POINTER
3486 (DECL_NAME (v->field)));
3487 TYPE_NAME (new_variant)
3488 = concat_name (TYPE_NAME (gnu_type),
3489 IDENTIFIER_POINTER (suffix));
3490 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3491 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3492 copy_and_substitute_in_size (new_variant, old_variant,
3493 gnu_subst_list);
3494 v->new_type = new_variant;
3497 else
3499 gnu_variant_list.create (0);
3500 selected_variant = false;
3503 /* Make a list of fields and their position in the base type. */
3504 gnu_pos_list
3505 = build_position_list (gnu_unpad_base_type,
3506 gnu_variant_list.exists ()
3507 && !selected_variant,
3508 size_zero_node, bitsize_zero_node,
3509 BIGGEST_ALIGNMENT, NULL_TREE);
3511 /* Now go down every component in the subtype and compute its
3512 size and position from those of the component in the base
3513 type and from the constraints of the subtype. */
3514 for (gnat_field = First_Entity (gnat_entity);
3515 Present (gnat_field);
3516 gnat_field = Next_Entity (gnat_field))
3517 if ((Ekind (gnat_field) == E_Component
3518 || Ekind (gnat_field) == E_Discriminant)
3519 && !(Present (Corresponding_Discriminant (gnat_field))
3520 && Is_Tagged_Type (gnat_base_type))
3521 && Underlying_Type
3522 (Scope (Original_Record_Component (gnat_field)))
3523 == gnat_base_type)
3525 Name_Id gnat_name = Chars (gnat_field);
3526 Entity_Id gnat_old_field
3527 = Original_Record_Component (gnat_field);
3528 tree gnu_old_field
3529 = gnat_to_gnu_field_decl (gnat_old_field);
3530 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3531 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3532 tree gnu_cont_type, gnu_last = NULL_TREE;
3534 /* If the type is the same, retrieve the GCC type from the
3535 old field to take into account possible adjustments. */
3536 if (Etype (gnat_field) == Etype (gnat_old_field))
3537 gnu_field_type = TREE_TYPE (gnu_old_field);
3538 else
3539 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3541 /* If there was a component clause, the field types must be
3542 the same for the type and subtype, so copy the data from
3543 the old field to avoid recomputation here. Also if the
3544 field is justified modular and the optimization in
3545 gnat_to_gnu_field was applied. */
3546 if (Present (Component_Clause (gnat_old_field))
3547 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3548 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3549 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3550 == TREE_TYPE (gnu_old_field)))
3552 gnu_size = DECL_SIZE (gnu_old_field);
3553 gnu_field_type = TREE_TYPE (gnu_old_field);
3556 /* If the old field was packed and of constant size, we
3557 have to get the old size here, as it might differ from
3558 what the Etype conveys and the latter might overlap
3559 onto the following field. Try to arrange the type for
3560 possible better packing along the way. */
3561 else if (DECL_PACKED (gnu_old_field)
3562 && TREE_CODE (DECL_SIZE (gnu_old_field))
3563 == INTEGER_CST)
3565 gnu_size = DECL_SIZE (gnu_old_field);
3566 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3567 && !TYPE_FAT_POINTER_P (gnu_field_type)
3568 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3569 gnu_field_type
3570 = make_packable_type (gnu_field_type, true);
3573 else
3574 gnu_size = TYPE_SIZE (gnu_field_type);
3576 /* If the context of the old field is the base type or its
3577 REP part (if any), put the field directly in the new
3578 type; otherwise look up the context in the variant list
3579 and put the field either in the new type if there is a
3580 selected variant or in one of the new variants. */
3581 if (gnu_context == gnu_unpad_base_type
3582 || (gnu_rep_part
3583 && gnu_context == TREE_TYPE (gnu_rep_part)))
3584 gnu_cont_type = gnu_type;
3585 else
3587 variant_desc *v;
3588 unsigned int i;
3589 tree rep_part;
3591 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3592 if (gnu_context == v->type
3593 || ((rep_part = get_rep_part (v->type))
3594 && gnu_context == TREE_TYPE (rep_part)))
3595 break;
3596 if (v)
3598 if (selected_variant)
3599 gnu_cont_type = gnu_type;
3600 else
3601 gnu_cont_type = v->new_type;
3603 else
3604 /* The front-end may pass us "ghost" components if
3605 it fails to recognize that a constrained subtype
3606 is statically constrained. Discard them. */
3607 continue;
3610 /* Now create the new field modeled on the old one. */
3611 gnu_field
3612 = create_field_decl_from (gnu_old_field, gnu_field_type,
3613 gnu_cont_type, gnu_size,
3614 gnu_pos_list, gnu_subst_list);
3615 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3617 /* Put it in one of the new variants directly. */
3618 if (gnu_cont_type != gnu_type)
3620 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3621 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3624 /* To match the layout crafted in components_to_record,
3625 if this is the _Tag or _Parent field, put it before
3626 any other fields. */
3627 else if (gnat_name == Name_uTag
3628 || gnat_name == Name_uParent)
3629 gnu_field_list = chainon (gnu_field_list, gnu_field);
3631 /* Similarly, if this is the _Controller field, put
3632 it before the other fields except for the _Tag or
3633 _Parent field. */
3634 else if (gnat_name == Name_uController && gnu_last)
3636 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3637 DECL_CHAIN (gnu_last) = gnu_field;
3640 /* Otherwise, if this is a regular field, put it after
3641 the other fields. */
3642 else
3644 DECL_CHAIN (gnu_field) = gnu_field_list;
3645 gnu_field_list = gnu_field;
3646 if (!gnu_last)
3647 gnu_last = gnu_field;
3648 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3649 all_constant_pos = false;
3652 save_gnu_tree (gnat_field, gnu_field, false);
3655 /* If there is a variant list, a selected variant and the fields
3656 all have a constant position, put them in order of increasing
3657 position to match that of constant CONSTRUCTORs. Likewise if
3658 there is no variant list but a REP part, since the latter has
3659 been flattened in the process. */
3660 if (((gnu_variant_list.exists () && selected_variant)
3661 || (!gnu_variant_list.exists () && gnu_rep_part))
3662 && all_constant_pos)
3664 const int len = list_length (gnu_field_list);
3665 tree *field_arr = XALLOCAVEC (tree, len), t;
3666 int i;
3668 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3669 field_arr[i] = t;
3671 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3673 gnu_field_list = NULL_TREE;
3674 for (i = 0; i < len; i++)
3676 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3677 gnu_field_list = field_arr[i];
3681 /* If there is a variant list and no selected variant, we need
3682 to create the nest of variant parts from the old nest. */
3683 else if (gnu_variant_list.exists () && !selected_variant)
3685 tree new_variant_part
3686 = create_variant_part_from (gnu_variant_part,
3687 gnu_variant_list, gnu_type,
3688 gnu_pos_list, gnu_subst_list);
3689 DECL_CHAIN (new_variant_part) = gnu_field_list;
3690 gnu_field_list = new_variant_part;
3693 /* Now go through the entities again looking for Itypes that
3694 we have not elaborated but should (e.g., Etypes of fields
3695 that have Original_Components). */
3696 for (gnat_field = First_Entity (gnat_entity);
3697 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3698 if ((Ekind (gnat_field) == E_Discriminant
3699 || Ekind (gnat_field) == E_Component)
3700 && !present_gnu_tree (Etype (gnat_field)))
3701 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3703 /* Do not emit debug info for the type yet since we're going to
3704 modify it below. */
3705 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3706 false);
3707 compute_record_mode (gnu_type);
3709 /* Fill in locations of fields. */
3710 annotate_rep (gnat_entity, gnu_type);
3712 /* If debugging information is being written for the type and if
3713 we are asked to output such encodings, write a record that
3714 shows what we are a subtype of and also make a variable that
3715 indicates our size, if still variable. */
3716 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3718 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3719 tree gnu_unpad_base_name
3720 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3721 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3723 TYPE_NAME (gnu_subtype_marker)
3724 = create_concat_name (gnat_entity, "XVS");
3725 finish_record_type (gnu_subtype_marker,
3726 create_field_decl (gnu_unpad_base_name,
3727 build_reference_type
3728 (gnu_unpad_base_type),
3729 gnu_subtype_marker,
3730 NULL_TREE, NULL_TREE,
3731 0, 0),
3732 0, true);
3734 add_parallel_type (gnu_type, gnu_subtype_marker);
3736 if (definition
3737 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3738 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3739 TYPE_SIZE_UNIT (gnu_subtype_marker)
3740 = create_var_decl (create_concat_name (gnat_entity,
3741 "XVZ"),
3742 NULL_TREE, sizetype, gnu_size_unit,
3743 false, false, false, false, false,
3744 true, debug_info_p,
3745 NULL, gnat_entity);
3748 gnu_variant_list.release ();
3749 gnu_subst_list.release ();
3751 /* Now we can finalize it. */
3752 rest_of_record_type_compilation (gnu_type);
3755 /* Otherwise, go down all the components in the new type and make
3756 them equivalent to those in the base type. */
3757 else
3759 gnu_type = gnu_base_type;
3761 for (gnat_temp = First_Entity (gnat_entity);
3762 Present (gnat_temp);
3763 gnat_temp = Next_Entity (gnat_temp))
3764 if ((Ekind (gnat_temp) == E_Discriminant
3765 && !Is_Unchecked_Union (gnat_base_type))
3766 || Ekind (gnat_temp) == E_Component)
3767 save_gnu_tree (gnat_temp,
3768 gnat_to_gnu_field_decl
3769 (Original_Record_Component (gnat_temp)),
3770 false);
3773 break;
3775 case E_Access_Subprogram_Type:
3776 /* Use the special descriptor type for dispatch tables if needed,
3777 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3778 Note that we are only required to do so for static tables in
3779 order to be compatible with the C++ ABI, but Ada 2005 allows
3780 to extend library level tagged types at the local level so
3781 we do it in the non-static case as well. */
3782 if (TARGET_VTABLE_USES_DESCRIPTORS
3783 && Is_Dispatch_Table_Entity (gnat_entity))
3785 gnu_type = fdesc_type_node;
3786 gnu_size = TYPE_SIZE (gnu_type);
3787 break;
3790 /* ... fall through ... */
3792 case E_Anonymous_Access_Subprogram_Type:
3793 /* If we are not defining this entity, and we have incomplete
3794 entities being processed above us, make a dummy type and
3795 fill it in later. */
3796 if (!definition && defer_incomplete_level != 0)
3798 struct incomplete *p = XNEW (struct incomplete);
3800 gnu_type
3801 = build_pointer_type
3802 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3803 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3804 artificial_p, debug_info_p,
3805 gnat_entity);
3806 this_made_decl = true;
3807 gnu_type = TREE_TYPE (gnu_decl);
3808 save_gnu_tree (gnat_entity, gnu_decl, false);
3809 saved = true;
3811 p->old_type = TREE_TYPE (gnu_type);
3812 p->full_type = Directly_Designated_Type (gnat_entity);
3813 p->next = defer_incomplete_list;
3814 defer_incomplete_list = p;
3815 break;
3818 /* ... fall through ... */
3820 case E_Allocator_Type:
3821 case E_Access_Type:
3822 case E_Access_Attribute_Type:
3823 case E_Anonymous_Access_Type:
3824 case E_General_Access_Type:
3826 /* The designated type and its equivalent type for gigi. */
3827 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3828 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3829 /* Whether it comes from a limited with. */
3830 bool is_from_limited_with
3831 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3832 && From_Limited_With (gnat_desig_equiv));
3833 /* The "full view" of the designated type. If this is an incomplete
3834 entity from a limited with, treat its non-limited view as the full
3835 view. Otherwise, if this is an incomplete or private type, use the
3836 full view. In the former case, we might point to a private type,
3837 in which case, we need its full view. Also, we want to look at the
3838 actual type used for the representation, so this takes a total of
3839 three steps. */
3840 Entity_Id gnat_desig_full_direct_first
3841 = (is_from_limited_with
3842 ? Non_Limited_View (gnat_desig_equiv)
3843 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3844 ? Full_View (gnat_desig_equiv) : Empty));
3845 Entity_Id gnat_desig_full_direct
3846 = ((is_from_limited_with
3847 && Present (gnat_desig_full_direct_first)
3848 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3849 ? Full_View (gnat_desig_full_direct_first)
3850 : gnat_desig_full_direct_first);
3851 Entity_Id gnat_desig_full
3852 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3853 /* The type actually used to represent the designated type, either
3854 gnat_desig_full or gnat_desig_equiv. */
3855 Entity_Id gnat_desig_rep;
3856 /* We want to know if we'll be seeing the freeze node for any
3857 incomplete type we may be pointing to. */
3858 bool in_main_unit
3859 = (Present (gnat_desig_full)
3860 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3861 : In_Extended_Main_Code_Unit (gnat_desig_type));
3862 /* True if we make a dummy type here. */
3863 bool made_dummy = false;
3864 /* The mode to be used for the pointer type. */
3865 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3866 /* The GCC type used for the designated type. */
3867 tree gnu_desig_type = NULL_TREE;
3869 if (!targetm.valid_pointer_mode (p_mode))
3870 p_mode = ptr_mode;
3872 /* If either the designated type or its full view is an unconstrained
3873 array subtype, replace it with the type it's a subtype of. This
3874 avoids problems with multiple copies of unconstrained array types.
3875 Likewise, if the designated type is a subtype of an incomplete
3876 record type, use the parent type to avoid order of elaboration
3877 issues. This can lose some code efficiency, but there is no
3878 alternative. */
3879 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3880 && !Is_Constrained (gnat_desig_equiv))
3881 gnat_desig_equiv = Etype (gnat_desig_equiv);
3882 if (Present (gnat_desig_full)
3883 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3884 && !Is_Constrained (gnat_desig_full))
3885 || (Ekind (gnat_desig_full) == E_Record_Subtype
3886 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3887 gnat_desig_full = Etype (gnat_desig_full);
3889 /* Set the type that's the representation of the designated type. */
3890 gnat_desig_rep
3891 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3893 /* If we already know what the full type is, use it. */
3894 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3895 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3897 /* Get the type of the thing we are to point to and build a pointer to
3898 it. If it is a reference to an incomplete or private type with a
3899 full view that is a record or an array, make a dummy type node and
3900 get the actual type later when we have verified it is safe. */
3901 else if ((!in_main_unit
3902 && !present_gnu_tree (gnat_desig_equiv)
3903 && Present (gnat_desig_full)
3904 && (Is_Record_Type (gnat_desig_full)
3905 || Is_Array_Type (gnat_desig_full)))
3906 /* Likewise if we are pointing to a record or array and we are
3907 to defer elaborating incomplete types. We do this as this
3908 access type may be the full view of a private type. */
3909 || ((!in_main_unit || imported_p)
3910 && defer_incomplete_level != 0
3911 && !present_gnu_tree (gnat_desig_equiv)
3912 && (Is_Record_Type (gnat_desig_rep)
3913 || Is_Array_Type (gnat_desig_rep)))
3914 /* If this is a reference from a limited_with type back to our
3915 main unit and there's a freeze node for it, either we have
3916 already processed the declaration and made the dummy type,
3917 in which case we just reuse the latter, or we have not yet,
3918 in which case we make the dummy type and it will be reused
3919 when the declaration is finally processed. In both cases,
3920 the pointer eventually created below will be automatically
3921 adjusted when the freeze node is processed. */
3922 || (in_main_unit
3923 && is_from_limited_with
3924 && Present (Freeze_Node (gnat_desig_rep))))
3926 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3927 made_dummy = true;
3930 /* Otherwise handle the case of a pointer to itself. */
3931 else if (gnat_desig_equiv == gnat_entity)
3933 gnu_type
3934 = build_pointer_type_for_mode (void_type_node, p_mode,
3935 No_Strict_Aliasing (gnat_entity));
3936 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3939 /* If expansion is disabled, the equivalent type of a concurrent type
3940 is absent, so build a dummy pointer type. */
3941 else if (type_annotate_only && No (gnat_desig_equiv))
3942 gnu_type = ptr_type_node;
3944 /* Finally, handle the default case where we can just elaborate our
3945 designated type. */
3946 else
3947 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3949 /* It is possible that a call to gnat_to_gnu_type above resolved our
3950 type. If so, just return it. */
3951 if (present_gnu_tree (gnat_entity))
3953 maybe_present = true;
3954 break;
3957 /* For an unconstrained array, make dummy fat & thin pointer types. */
3958 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3960 /* If the processing above got something that has a pointer, then
3961 we are done. This could have happened either because the type
3962 was elaborated or because somebody else executed the code. */
3963 if (!TYPE_POINTER_TO (gnu_desig_type))
3964 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3965 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3968 /* If we haven't done it yet, build the pointer type the usual way. */
3969 else if (!gnu_type)
3971 /* Modify the designated type if we are pointing only to constant
3972 objects, but don't do it for unconstrained arrays. */
3973 if (Is_Access_Constant (gnat_entity)
3974 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3976 gnu_desig_type
3977 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3979 /* Some extra processing is required if we are building a
3980 pointer to an incomplete type (in the GCC sense). We might
3981 have such a type if we just made a dummy, or directly out
3982 of the call to gnat_to_gnu_type above if we are processing
3983 an access type for a record component designating the
3984 record type itself. */
3985 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3987 /* We must ensure that the pointer to variant we make will
3988 be processed by update_pointer_to when the initial type
3989 is completed. Pretend we made a dummy and let further
3990 processing act as usual. */
3991 made_dummy = true;
3993 /* We must ensure that update_pointer_to will not retrieve
3994 the dummy variant when building a properly qualified
3995 version of the complete type. We take advantage of the
3996 fact that get_qualified_type is requiring TYPE_NAMEs to
3997 match to influence build_qualified_type and then also
3998 update_pointer_to here. */
3999 TYPE_NAME (gnu_desig_type)
4000 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
4004 gnu_type
4005 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
4006 No_Strict_Aliasing (gnat_entity));
4009 /* If we are not defining this object and we have made a dummy pointer,
4010 save our current definition, evaluate the actual type, and replace
4011 the tentative type we made with the actual one. If we are to defer
4012 actually looking up the actual type, make an entry in the deferred
4013 list. If this is from a limited with, we may have to defer to the
4014 end of the current unit. */
4015 if ((!in_main_unit || is_from_limited_with) && made_dummy)
4017 tree gnu_old_desig_type;
4019 if (TYPE_IS_FAT_POINTER_P (gnu_type))
4021 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
4022 if (esize == POINTER_SIZE)
4023 gnu_type = build_pointer_type
4024 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
4026 else
4027 gnu_old_desig_type = TREE_TYPE (gnu_type);
4029 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4030 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4031 artificial_p, debug_info_p,
4032 gnat_entity);
4033 this_made_decl = true;
4034 gnu_type = TREE_TYPE (gnu_decl);
4035 save_gnu_tree (gnat_entity, gnu_decl, false);
4036 saved = true;
4038 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4039 update gnu_old_desig_type directly, in which case it will not be
4040 a dummy type any more when we get into update_pointer_to.
4042 This can happen e.g. when the designated type is a record type,
4043 because their elaboration starts with an initial node from
4044 make_dummy_type, which may be the same node as the one we got.
4046 Besides, variants of this non-dummy type might have been created
4047 along the way. update_pointer_to is expected to properly take
4048 care of those situations. */
4049 if (defer_incomplete_level == 0 && !is_from_limited_with)
4051 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4052 gnat_to_gnu_type (gnat_desig_equiv));
4054 else
4056 struct incomplete *p = XNEW (struct incomplete);
4057 struct incomplete **head
4058 = (is_from_limited_with
4059 ? &defer_limited_with : &defer_incomplete_list);
4060 p->old_type = gnu_old_desig_type;
4061 p->full_type = gnat_desig_equiv;
4062 p->next = *head;
4063 *head = p;
4067 break;
4069 case E_Access_Protected_Subprogram_Type:
4070 case E_Anonymous_Access_Protected_Subprogram_Type:
4071 if (type_annotate_only && No (gnat_equiv_type))
4072 gnu_type = ptr_type_node;
4073 else
4075 /* The run-time representation is the equivalent type. */
4076 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4077 maybe_present = true;
4080 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4081 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4082 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4083 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4084 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4085 NULL_TREE, 0);
4087 break;
4089 case E_Access_Subtype:
4091 /* We treat this as identical to its base type; any constraint is
4092 meaningful only to the front-end.
4094 The designated type must be elaborated as well, if it does
4095 not have its own freeze node. Designated (sub)types created
4096 for constrained components of records with discriminants are
4097 not frozen by the front-end and thus not elaborated by gigi,
4098 because their use may appear before the base type is frozen,
4099 and because it is not clear that they are needed anywhere in
4100 gigi. With the current model, there is no correct place where
4101 they could be elaborated. */
4103 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4104 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4105 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4106 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4107 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4109 /* If we are not defining this entity, and we have incomplete
4110 entities being processed above us, make a dummy type and
4111 elaborate it later. */
4112 if (!definition && defer_incomplete_level != 0)
4114 struct incomplete *p = XNEW (struct incomplete);
4116 p->old_type
4117 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4118 p->full_type = Directly_Designated_Type (gnat_entity);
4119 p->next = defer_incomplete_list;
4120 defer_incomplete_list = p;
4122 else if (!IN (Ekind (Base_Type
4123 (Directly_Designated_Type (gnat_entity))),
4124 Incomplete_Or_Private_Kind))
4125 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4126 NULL_TREE, 0);
4129 maybe_present = true;
4130 break;
4132 /* Subprogram Entities
4134 The following access functions are defined for subprograms:
4136 Etype Return type or Standard_Void_Type.
4137 First_Formal The first formal parameter.
4138 Is_Imported Indicates that the subprogram has appeared in
4139 an INTERFACE or IMPORT pragma. For now we
4140 assume that the external language is C.
4141 Is_Exported Likewise but for an EXPORT pragma.
4142 Is_Inlined True if the subprogram is to be inlined.
4144 Each parameter is first checked by calling must_pass_by_ref on its
4145 type to determine if it is passed by reference. For parameters which
4146 are copied in, if they are Ada In Out or Out parameters, their return
4147 value becomes part of a record which becomes the return type of the
4148 function (C function - note that this applies only to Ada procedures
4149 so there is no Ada return type). Additional code to store back the
4150 parameters will be generated on the caller side. This transformation
4151 is done here, not in the front-end.
4153 The intended result of the transformation can be seen from the
4154 equivalent source rewritings that follow:
4156 struct temp {int a,b};
4157 procedure P (A,B: In Out ...) is temp P (int A,B)
4158 begin {
4159 .. ..
4160 end P; return {A,B};
4163 temp t;
4164 P(X,Y); t = P(X,Y);
4165 X = t.a , Y = t.b;
4167 For subprogram types we need to perform mainly the same conversions to
4168 GCC form that are needed for procedures and function declarations. The
4169 only difference is that at the end, we make a type declaration instead
4170 of a function declaration. */
4172 case E_Subprogram_Type:
4173 case E_Function:
4174 case E_Procedure:
4176 /* The type returned by a function or else Standard_Void_Type for a
4177 procedure. */
4178 Entity_Id gnat_return_type = Etype (gnat_entity);
4179 tree gnu_return_type;
4180 /* The first GCC parameter declaration (a PARM_DECL node). The
4181 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4182 actually is the head of this parameter list. */
4183 tree gnu_param_list = NULL_TREE;
4184 /* Non-null for subprograms containing parameters passed by copy-in
4185 copy-out (Ada In Out or Out parameters not passed by reference),
4186 in which case it is the list of nodes used to specify the values
4187 of the In Out/Out parameters that are returned as a record upon
4188 procedure return. The TREE_PURPOSE of an element of this list is
4189 a field of the record and the TREE_VALUE is the PARM_DECL
4190 corresponding to that field. This list will be saved in the
4191 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4192 tree gnu_cico_list = NULL_TREE;
4193 /* List of fields in return type of procedure with copy-in copy-out
4194 parameters. */
4195 tree gnu_field_list = NULL_TREE;
4196 /* If an import pragma asks to map this subprogram to a GCC builtin,
4197 this is the builtin DECL node. */
4198 tree gnu_builtin_decl = NULL_TREE;
4199 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4200 Entity_Id gnat_param;
4201 enum inline_status_t inline_status
4202 = Has_Pragma_No_Inline (gnat_entity)
4203 ? is_suppressed
4204 : Has_Pragma_Inline_Always (gnat_entity)
4205 ? is_required
4206 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4207 bool public_flag = Is_Public (gnat_entity) || imported_p;
4208 /* Subprograms marked both Intrinsic and Always_Inline need not
4209 have a body of their own. */
4210 bool extern_flag
4211 = ((Is_Public (gnat_entity) && !definition)
4212 || imported_p
4213 || (Convention (gnat_entity) == Convention_Intrinsic
4214 && Has_Pragma_Inline_Always (gnat_entity)));
4215 /* The semantics of "pure" in Ada essentially matches that of "const"
4216 in the back-end. In particular, both properties are orthogonal to
4217 the "nothrow" property if the EH circuitry is explicit in the
4218 internal representation of the back-end. If we are to completely
4219 hide the EH circuitry from it, we need to declare that calls to pure
4220 Ada subprograms that can throw have side effects since they can
4221 trigger an "abnormal" transfer of control flow; thus they can be
4222 neither "const" nor "pure" in the back-end sense. */
4223 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_entity));
4224 bool volatile_flag = No_Return (gnat_entity);
4225 bool return_by_direct_ref_p = false;
4226 bool return_by_invisi_ref_p = false;
4227 bool return_unconstrained_p = false;
4228 int parmnum;
4230 /* A parameter may refer to this type, so defer completion of any
4231 incomplete types. */
4232 if (kind == E_Subprogram_Type && !definition)
4234 defer_incomplete_level++;
4235 this_deferred = true;
4238 /* If the subprogram has an alias, it is probably inherited, so
4239 we can use the original one. If the original "subprogram"
4240 is actually an enumeration literal, it may be the first use
4241 of its type, so we must elaborate that type now. */
4242 if (Present (Alias (gnat_entity)))
4244 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
4246 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4247 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4249 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4251 /* Elaborate any Itypes in the parameters of this entity. */
4252 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4253 Present (gnat_temp);
4254 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4255 if (Is_Itype (Etype (gnat_temp)))
4256 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4258 /* Materialize renamed subprograms in the debugging information
4259 when the renamed object is compile time known. We can consider
4260 such renamings as imported declarations.
4262 Because the parameters in generics instantiation are generally
4263 materialized as renamings, we ofter end up having both the
4264 renamed subprogram and the renaming in the same context and with
4265 the same name: in this case, renaming is both useless debug-wise
4266 and potentially harmful as name resolution in the debugger could
4267 return twice the same entity! So avoid this case. */
4268 if (debug_info_p && !artificial_p
4269 && !(get_debug_scope (gnat_entity, NULL)
4270 == get_debug_scope (gnat_renamed, NULL)
4271 && Name_Equals (Chars (gnat_entity),
4272 Chars (gnat_renamed)))
4273 && Present (gnat_renamed)
4274 && (Ekind (gnat_renamed) == E_Function
4275 || Ekind (gnat_renamed) == E_Procedure)
4276 && gnu_decl
4277 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
4279 tree decl = build_decl (input_location, IMPORTED_DECL,
4280 gnu_entity_name, void_type_node);
4281 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
4282 gnat_pushdecl (decl, gnat_entity);
4285 break;
4288 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4289 corresponding DECL node. Proper generation of calls later on need
4290 proper parameter associations so we don't "break;" here. */
4291 if (Convention (gnat_entity) == Convention_Intrinsic
4292 && Present (Interface_Name (gnat_entity)))
4294 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4296 /* Inability to find the builtin decl most often indicates a
4297 genuine mistake, but imports of unregistered intrinsics are
4298 sometimes issued on purpose to allow hooking in alternate
4299 bodies. We post a warning conditioned on Wshadow in this case,
4300 to let developers be notified on demand without risking false
4301 positives with common default sets of options. */
4303 if (!gnu_builtin_decl && warn_shadow)
4304 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4307 /* ??? What if we don't find the builtin node above ? warn ? err ?
4308 In the current state we neither warn nor err, and calls will just
4309 be handled as for regular subprograms. */
4311 /* Look into the return type and get its associated GCC tree. If it
4312 is not void, compute various flags for the subprogram type. */
4313 if (Ekind (gnat_return_type) == E_Void)
4314 gnu_return_type = void_type_node;
4315 else
4317 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4318 context may now appear in parameter and result profiles. If
4319 we are only annotating types, break circularities here. */
4320 if (type_annotate_only
4321 && is_from_limited_with_of_main (gnat_return_type))
4322 gnu_return_type = void_type_node;
4323 else
4324 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4326 /* If this function returns by reference, make the actual return
4327 type the pointer type and make a note of that. */
4328 if (Returns_By_Ref (gnat_entity))
4330 gnu_return_type = build_reference_type (gnu_return_type);
4331 return_by_direct_ref_p = true;
4334 /* If the return type is an unconstrained array type, the return
4335 value will be allocated on the secondary stack so the actual
4336 return type is the fat pointer type. */
4337 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4339 gnu_return_type = TREE_TYPE (gnu_return_type);
4340 return_unconstrained_p = true;
4343 /* Likewise, if the return type requires a transient scope, the
4344 return value will also be allocated on the secondary stack so
4345 the actual return type is the pointer type. */
4346 else if (Requires_Transient_Scope (gnat_return_type))
4348 gnu_return_type = build_reference_type (gnu_return_type);
4349 return_unconstrained_p = true;
4352 /* If the Mechanism is By_Reference, ensure this function uses the
4353 target's by-invisible-reference mechanism, which may not be the
4354 same as above (e.g. it might be passing an extra parameter). */
4355 else if (kind == E_Function
4356 && Mechanism (gnat_entity) == By_Reference)
4357 return_by_invisi_ref_p = true;
4359 /* Likewise, if the return type is itself By_Reference. */
4360 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4361 return_by_invisi_ref_p = true;
4363 /* If the type is a padded type and the underlying type would not
4364 be passed by reference or the function has a foreign convention,
4365 return the underlying type. */
4366 else if (TYPE_IS_PADDING_P (gnu_return_type)
4367 && (!default_pass_by_ref
4368 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4369 || Has_Foreign_Convention (gnat_entity)))
4370 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4372 /* If the return type is unconstrained, that means it must have a
4373 maximum size. Use the padded type as the effective return type.
4374 And ensure the function uses the target's by-invisible-reference
4375 mechanism to avoid copying too much data when it returns. */
4376 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4378 tree orig_type = gnu_return_type;
4379 tree max_return_size
4380 = max_size (TYPE_SIZE (gnu_return_type), true);
4382 /* If the size overflows to 0, set it to an arbitrary positive
4383 value so that assignments in the type are preserved. Their
4384 actual size is independent of this positive value. */
4385 if (TREE_CODE (max_return_size) == INTEGER_CST
4386 && TREE_OVERFLOW (max_return_size)
4387 && integer_zerop (max_return_size))
4389 max_return_size = copy_node (bitsize_unit_node);
4390 TREE_OVERFLOW (max_return_size) = 1;
4393 gnu_return_type
4394 = maybe_pad_type (gnu_return_type, max_return_size, 0,
4395 gnat_entity, false, false, definition,
4396 true);
4398 /* Declare it now since it will never be declared otherwise.
4399 This is necessary to ensure that its subtrees are properly
4400 marked. */
4401 if (gnu_return_type != orig_type
4402 && !DECL_P (TYPE_NAME (gnu_return_type)))
4403 create_type_decl (TYPE_NAME (gnu_return_type),
4404 gnu_return_type, true, debug_info_p,
4405 gnat_entity);
4407 return_by_invisi_ref_p = true;
4410 /* If the return type has a size that overflows, we cannot have
4411 a function that returns that type. This usage doesn't make
4412 sense anyway, so give an error here. */
4413 if (!return_by_invisi_ref_p
4414 && TYPE_SIZE_UNIT (gnu_return_type)
4415 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4416 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4418 post_error ("cannot return type whose size overflows",
4419 gnat_entity);
4420 gnu_return_type = copy_node (gnu_return_type);
4421 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4422 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4423 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4424 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4428 /* Loop over the parameters and get their associated GCC tree. While
4429 doing this, build a copy-in copy-out structure if we need one. */
4430 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4431 Present (gnat_param);
4432 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4434 Entity_Id gnat_param_type = Etype (gnat_param);
4435 tree gnu_param_name = get_entity_name (gnat_param);
4436 tree gnu_param_type, gnu_param, gnu_field;
4437 Mechanism_Type mech = Mechanism (gnat_param);
4438 bool copy_in_copy_out = false, fake_param_type;
4440 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4441 context may now appear in parameter and result profiles. If
4442 we are only annotating types, break circularities here. */
4443 if (type_annotate_only
4444 && is_from_limited_with_of_main (gnat_param_type))
4446 gnu_param_type = void_type_node;
4447 fake_param_type = true;
4449 else
4451 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4452 fake_param_type = false;
4455 /* Builtins are expanded inline and there is no real call sequence
4456 involved. So the type expected by the underlying expander is
4457 always the type of each argument "as is". */
4458 if (gnu_builtin_decl)
4459 mech = By_Copy;
4460 /* Handle the first parameter of a valued procedure specially. */
4461 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4462 mech = By_Copy_Return;
4463 /* Otherwise, see if a Mechanism was supplied that forced this
4464 parameter to be passed one way or another. */
4465 else if (mech == Default
4466 || mech == By_Copy
4467 || mech == By_Reference)
4469 else if (mech > 0)
4471 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4472 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4473 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4474 mech))
4475 mech = By_Reference;
4476 else
4477 mech = By_Copy;
4479 else
4481 post_error ("unsupported mechanism for&", gnat_param);
4482 mech = Default;
4485 /* Do not call gnat_to_gnu_param for a fake parameter type since
4486 it will try to use the real type again. */
4487 if (fake_param_type)
4489 if (Ekind (gnat_param) == E_Out_Parameter)
4490 gnu_param = NULL_TREE;
4491 else
4493 gnu_param
4494 = create_param_decl (gnu_param_name, gnu_param_type,
4495 false);
4496 Set_Mechanism (gnat_param,
4497 mech == Default ? By_Copy : mech);
4498 if (Ekind (gnat_param) == E_In_Out_Parameter)
4499 copy_in_copy_out = true;
4502 else
4503 gnu_param
4504 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4505 Has_Foreign_Convention (gnat_entity),
4506 &copy_in_copy_out);
4508 /* We are returned either a PARM_DECL or a type if no parameter
4509 needs to be passed; in either case, adjust the type. */
4510 if (DECL_P (gnu_param))
4511 gnu_param_type = TREE_TYPE (gnu_param);
4512 else
4514 gnu_param_type = gnu_param;
4515 gnu_param = NULL_TREE;
4518 /* The failure of this assertion will very likely come from an
4519 order of elaboration issue for the type of the parameter. */
4520 gcc_assert (kind == E_Subprogram_Type
4521 || !TYPE_IS_DUMMY_P (gnu_param_type)
4522 || type_annotate_only);
4524 if (gnu_param)
4526 gnu_param_list = chainon (gnu_param, gnu_param_list);
4527 Sloc_to_locus (Sloc (gnat_param),
4528 &DECL_SOURCE_LOCATION (gnu_param));
4529 save_gnu_tree (gnat_param, gnu_param, false);
4531 /* If a parameter is a pointer, this function may modify
4532 memory through it and thus shouldn't be considered
4533 a const function. Also, the memory may be modified
4534 between two calls, so they can't be CSE'ed. The latter
4535 case also handles by-ref parameters. */
4536 if (POINTER_TYPE_P (gnu_param_type)
4537 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4538 const_flag = false;
4541 if (copy_in_copy_out)
4543 if (!gnu_cico_list)
4545 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4547 /* If this is a function, we also need a field for the
4548 return value to be placed. */
4549 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4551 gnu_field
4552 = create_field_decl (get_identifier ("RETVAL"),
4553 gnu_return_type,
4554 gnu_new_ret_type, NULL_TREE,
4555 NULL_TREE, 0, 0);
4556 Sloc_to_locus (Sloc (gnat_entity),
4557 &DECL_SOURCE_LOCATION (gnu_field));
4558 gnu_field_list = gnu_field;
4559 gnu_cico_list
4560 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4563 gnu_return_type = gnu_new_ret_type;
4564 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4565 /* Set a default alignment to speed up accesses. But we
4566 shouldn't increase the size of the structure too much,
4567 lest it doesn't fit in return registers anymore. */
4568 TYPE_ALIGN (gnu_return_type)
4569 = get_mode_alignment (ptr_mode);
4572 gnu_field
4573 = create_field_decl (gnu_param_name, gnu_param_type,
4574 gnu_return_type, NULL_TREE, NULL_TREE,
4575 0, 0);
4576 Sloc_to_locus (Sloc (gnat_param),
4577 &DECL_SOURCE_LOCATION (gnu_field));
4578 DECL_CHAIN (gnu_field) = gnu_field_list;
4579 gnu_field_list = gnu_field;
4580 gnu_cico_list
4581 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4585 if (gnu_cico_list)
4587 /* If we have a CICO list but it has only one entry, we convert
4588 this function into a function that returns this object. */
4589 if (list_length (gnu_cico_list) == 1)
4590 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4592 /* Do not finalize the return type if the subprogram is stubbed
4593 since structures are incomplete for the back-end. */
4594 else if (Convention (gnat_entity) != Convention_Stubbed)
4596 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4597 0, false);
4599 /* Try to promote the mode of the return type if it is passed
4600 in registers, again to speed up accesses. */
4601 if (TYPE_MODE (gnu_return_type) == BLKmode
4602 && !targetm.calls.return_in_memory (gnu_return_type,
4603 NULL_TREE))
4605 unsigned int size
4606 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4607 unsigned int i = BITS_PER_UNIT;
4608 machine_mode mode;
4610 while (i < size)
4611 i <<= 1;
4612 mode = mode_for_size (i, MODE_INT, 0);
4613 if (mode != BLKmode)
4615 SET_TYPE_MODE (gnu_return_type, mode);
4616 TYPE_ALIGN (gnu_return_type)
4617 = GET_MODE_ALIGNMENT (mode);
4618 TYPE_SIZE (gnu_return_type)
4619 = bitsize_int (GET_MODE_BITSIZE (mode));
4620 TYPE_SIZE_UNIT (gnu_return_type)
4621 = size_int (GET_MODE_SIZE (mode));
4625 if (debug_info_p)
4626 rest_of_record_type_compilation (gnu_return_type);
4630 /* Deal with platform-specific calling conventions. */
4631 if (Has_Stdcall_Convention (gnat_entity))
4632 prepend_one_attribute
4633 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4634 get_identifier ("stdcall"), NULL_TREE,
4635 gnat_entity);
4636 else if (Has_Thiscall_Convention (gnat_entity))
4637 prepend_one_attribute
4638 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4639 get_identifier ("thiscall"), NULL_TREE,
4640 gnat_entity);
4642 /* If we should request stack realignment for a foreign convention
4643 subprogram, do so. Note that this applies to task entry points
4644 in particular. */
4645 if (FOREIGN_FORCE_REALIGN_STACK
4646 && Has_Foreign_Convention (gnat_entity))
4647 prepend_one_attribute
4648 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4649 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4650 gnat_entity);
4652 /* Deal with a pragma Linker_Section on a subprogram. */
4653 if ((kind == E_Function || kind == E_Procedure)
4654 && Present (Linker_Section_Pragma (gnat_entity)))
4655 prepend_one_attribute_pragma (&attr_list,
4656 Linker_Section_Pragma (gnat_entity));
4658 /* The lists have been built in reverse. */
4659 gnu_param_list = nreverse (gnu_param_list);
4660 gnu_cico_list = nreverse (gnu_cico_list);
4662 if (kind == E_Function)
4663 Set_Mechanism (gnat_entity, return_unconstrained_p
4664 || return_by_direct_ref_p
4665 || return_by_invisi_ref_p
4666 ? By_Reference : By_Copy);
4667 gnu_type
4668 = create_subprog_type (gnu_return_type, gnu_param_list,
4669 gnu_cico_list, return_unconstrained_p,
4670 return_by_direct_ref_p,
4671 return_by_invisi_ref_p);
4673 /* A procedure (something that doesn't return anything) shouldn't be
4674 considered const since there would be no reason for calling such a
4675 subprogram. Note that procedures with Out (or In Out) parameters
4676 have already been converted into a function with a return type.
4677 Similarly, if the function returns an unconstrained type, then the
4678 function will allocate the return value on the secondary stack and
4679 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4680 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4681 const_flag = false;
4683 /* If we have a builtin decl for that function, use it. Check if the
4684 profiles are compatible and warn if they are not. The checker is
4685 expected to post extra diagnostics in this case. */
4686 if (gnu_builtin_decl)
4688 intrin_binding_t inb;
4690 inb.gnat_entity = gnat_entity;
4691 inb.ada_fntype = gnu_type;
4692 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4694 if (!intrin_profiles_compatible_p (&inb))
4695 post_error
4696 ("?profile of& doesn''t match the builtin it binds!",
4697 gnat_entity);
4699 gnu_decl = gnu_builtin_decl;
4700 gnu_type = TREE_TYPE (gnu_builtin_decl);
4701 break;
4704 /* If there was no specified Interface_Name and the external and
4705 internal names of the subprogram are the same, only use the
4706 internal name to allow disambiguation of nested subprograms. */
4707 if (No (Interface_Name (gnat_entity))
4708 && gnu_ext_name == gnu_entity_name)
4709 gnu_ext_name = NULL_TREE;
4711 /* If we are defining the subprogram and it has an Address clause
4712 we must get the address expression from the saved GCC tree for the
4713 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4714 the address expression here since the front-end has guaranteed
4715 in that case that the elaboration has no effects. If there is
4716 an Address clause and we are not defining the object, just
4717 make it a constant. */
4718 if (Present (Address_Clause (gnat_entity)))
4720 tree gnu_address = NULL_TREE;
4722 if (definition)
4723 gnu_address
4724 = (present_gnu_tree (gnat_entity)
4725 ? get_gnu_tree (gnat_entity)
4726 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4728 save_gnu_tree (gnat_entity, NULL_TREE, false);
4730 /* Convert the type of the object to a reference type that can
4731 alias everything as per RM 13.3(19). */
4732 gnu_type
4733 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4734 if (gnu_address)
4735 gnu_address = convert (gnu_type, gnu_address);
4737 gnu_decl
4738 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4739 gnu_address, false, Is_Public (gnat_entity),
4740 extern_flag, false, false, artificial_p,
4741 debug_info_p, NULL, gnat_entity);
4742 DECL_BY_REF_P (gnu_decl) = 1;
4745 else if (kind == E_Subprogram_Type)
4747 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4749 if (const_flag || volatile_flag)
4751 const int quals
4752 = (const_flag ? TYPE_QUAL_CONST : 0)
4753 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4754 gnu_type = change_qualified_type (gnu_type, quals);
4757 gnu_decl
4758 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4759 debug_info_p, gnat_entity);
4761 else
4763 gnu_decl
4764 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4765 gnu_param_list, inline_status, const_flag,
4766 public_flag, extern_flag, volatile_flag,
4767 artificial_p, debug_info_p,
4768 attr_list, gnat_entity);
4769 /* This is unrelated to the stub built right above. */
4770 DECL_STUBBED_P (gnu_decl)
4771 = Convention (gnat_entity) == Convention_Stubbed;
4774 break;
4776 case E_Incomplete_Type:
4777 case E_Incomplete_Subtype:
4778 case E_Private_Type:
4779 case E_Private_Subtype:
4780 case E_Limited_Private_Type:
4781 case E_Limited_Private_Subtype:
4782 case E_Record_Type_With_Private:
4783 case E_Record_Subtype_With_Private:
4785 bool is_from_limited_with
4786 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4787 /* Get the "full view" of this entity. If this is an incomplete
4788 entity from a limited with, treat its non-limited view as the
4789 full view. Otherwise, use either the full view or the underlying
4790 full view, whichever is present. This is used in all the tests
4791 below. */
4792 Entity_Id full_view
4793 = is_from_limited_with
4794 ? Non_Limited_View (gnat_entity)
4795 : Present (Full_View (gnat_entity))
4796 ? Full_View (gnat_entity)
4797 : IN (kind, Private_Kind)
4798 ? Underlying_Full_View (gnat_entity)
4799 : Empty;
4801 /* If this is an incomplete type with no full view, it must be a Taft
4802 Amendment type, in which case we return a dummy type. Otherwise,
4803 just get the type from its Etype. */
4804 if (No (full_view))
4806 if (kind == E_Incomplete_Type)
4808 gnu_type = make_dummy_type (gnat_entity);
4809 gnu_decl = TYPE_STUB_DECL (gnu_type);
4811 else
4813 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4814 NULL_TREE, 0);
4815 maybe_present = true;
4817 break;
4820 /* If we already made a type for the full view, reuse it. */
4821 else if (present_gnu_tree (full_view))
4823 gnu_decl = get_gnu_tree (full_view);
4824 break;
4827 /* Otherwise, if we are not defining the type now, get the type
4828 from the full view. But always get the type from the full view
4829 for define on use types, since otherwise we won't see them.
4830 Likewise if this is a non-limited view not declared in the main
4831 unit, which can happen for incomplete formal types instantiated
4832 on a type coming from a limited_with clause. */
4833 else if (!definition
4834 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4835 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view)))
4836 || (is_from_limited_with
4837 && !In_Extended_Main_Code_Unit (full_view)))
4839 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4840 maybe_present = true;
4841 break;
4844 /* For incomplete types, make a dummy type entry which will be
4845 replaced later. Save it as the full declaration's type so
4846 we can do any needed updates when we see it. */
4847 gnu_type = make_dummy_type (gnat_entity);
4848 gnu_decl = TYPE_STUB_DECL (gnu_type);
4849 if (Has_Completion_In_Body (gnat_entity))
4850 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4851 save_gnu_tree (full_view, gnu_decl, 0);
4852 break;
4855 case E_Class_Wide_Type:
4856 /* Class-wide types are always transformed into their root type. */
4857 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4858 maybe_present = true;
4859 break;
4861 case E_Protected_Type:
4862 case E_Protected_Subtype:
4863 case E_Task_Type:
4864 case E_Task_Subtype:
4865 /* If we are just annotating types and have no equivalent record type,
4866 just return void_type, except for root types that have discriminants
4867 because the discriminants will very likely be used in the declarative
4868 part of the associated body so they need to be translated. */
4869 if (type_annotate_only && No (gnat_equiv_type))
4871 if (Has_Discriminants (gnat_entity)
4872 && Root_Type (gnat_entity) == gnat_entity)
4874 tree gnu_field_list = NULL_TREE;
4875 Entity_Id gnat_field;
4877 /* This is a minimal version of the E_Record_Type handling. */
4878 gnu_type = make_node (RECORD_TYPE);
4879 TYPE_NAME (gnu_type) = gnu_entity_name;
4881 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4882 Present (gnat_field);
4883 gnat_field = Next_Stored_Discriminant (gnat_field))
4885 tree gnu_field
4886 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4887 definition, debug_info_p);
4889 save_gnu_tree (gnat_field,
4890 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4891 build0 (PLACEHOLDER_EXPR, gnu_type),
4892 gnu_field, NULL_TREE),
4893 true);
4895 DECL_CHAIN (gnu_field) = gnu_field_list;
4896 gnu_field_list = gnu_field;
4899 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4900 false);
4902 else
4903 gnu_type = void_type_node;
4906 /* Concurrent types are always transformed into their record type. */
4907 else
4908 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4909 maybe_present = true;
4910 break;
4912 case E_Label:
4913 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4914 break;
4916 case E_Block:
4917 case E_Loop:
4918 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4919 we've already saved it, so we don't try to. */
4920 gnu_decl = error_mark_node;
4921 saved = true;
4922 break;
4924 case E_Abstract_State:
4925 /* This is a SPARK annotation that only reaches here when compiling in
4926 ASIS mode. */
4927 gcc_assert (type_annotate_only);
4928 gnu_decl = error_mark_node;
4929 saved = true;
4930 break;
4932 default:
4933 gcc_unreachable ();
4936 /* If we had a case where we evaluated another type and it might have
4937 defined this one, handle it here. */
4938 if (maybe_present && present_gnu_tree (gnat_entity))
4940 gnu_decl = get_gnu_tree (gnat_entity);
4941 saved = true;
4944 /* If we are processing a type and there is either no decl for it or
4945 we just made one, do some common processing for the type, such as
4946 handling alignment and possible padding. */
4947 if (is_type && (!gnu_decl || this_made_decl))
4949 /* Process the attributes, if not already done. Note that the type is
4950 already defined so we cannot pass true for IN_PLACE here. */
4951 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4953 /* Tell the middle-end that objects of tagged types are guaranteed to
4954 be properly aligned. This is necessary because conversions to the
4955 class-wide type are translated into conversions to the root type,
4956 which can be less aligned than some of its derived types. */
4957 if (Is_Tagged_Type (gnat_entity)
4958 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4959 TYPE_ALIGN_OK (gnu_type) = 1;
4961 /* Record whether the type is passed by reference. */
4962 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4963 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4965 /* ??? Don't set the size for a String_Literal since it is either
4966 confirming or we don't handle it properly (if the low bound is
4967 non-constant). */
4968 if (!gnu_size && kind != E_String_Literal_Subtype)
4970 Uint gnat_size = Known_Esize (gnat_entity)
4971 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4972 gnu_size
4973 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4974 false, Has_Size_Clause (gnat_entity));
4977 /* If a size was specified, see if we can make a new type of that size
4978 by rearranging the type, for example from a fat to a thin pointer. */
4979 if (gnu_size)
4981 gnu_type
4982 = make_type_from_size (gnu_type, gnu_size,
4983 Has_Biased_Representation (gnat_entity));
4985 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4986 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4987 gnu_size = NULL_TREE;
4990 /* If the alignment has not already been processed and this is not
4991 an unconstrained array type, see if an alignment is specified.
4992 If not, we pick a default alignment for atomic objects. */
4993 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4995 else if (Known_Alignment (gnat_entity))
4997 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4998 TYPE_ALIGN (gnu_type));
5000 /* Warn on suspiciously large alignments. This should catch
5001 errors about the (alignment,byte)/(size,bit) discrepancy. */
5002 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
5004 tree size;
5006 /* If a size was specified, take it into account. Otherwise
5007 use the RM size for records or unions as the type size has
5008 already been adjusted to the alignment. */
5009 if (gnu_size)
5010 size = gnu_size;
5011 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
5012 && !TYPE_FAT_POINTER_P (gnu_type))
5013 size = rm_size (gnu_type);
5014 else
5015 size = TYPE_SIZE (gnu_type);
5017 /* Consider an alignment as suspicious if the alignment/size
5018 ratio is greater or equal to the byte/bit ratio. */
5019 if (tree_fits_uhwi_p (size)
5020 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
5021 post_error_ne ("?suspiciously large alignment specified for&",
5022 Expression (Alignment_Clause (gnat_entity)),
5023 gnat_entity);
5026 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
5027 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
5028 && integer_pow2p (TYPE_SIZE (gnu_type)))
5029 align = MIN (BIGGEST_ALIGNMENT,
5030 tree_to_uhwi (TYPE_SIZE (gnu_type)));
5031 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
5032 && tree_fits_uhwi_p (gnu_size)
5033 && integer_pow2p (gnu_size))
5034 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
5036 /* See if we need to pad the type. If we did, and made a record,
5037 the name of the new type may be changed. So get it back for
5038 us when we make the new TYPE_DECL below. */
5039 if (gnu_size || align > 0)
5040 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
5041 false, !gnu_decl, definition, false);
5043 if (TYPE_IS_PADDING_P (gnu_type))
5044 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
5046 /* Now set the RM size of the type. We cannot do it before padding
5047 because we need to accept arbitrary RM sizes on integral types. */
5048 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
5050 /* If we are at global level, GCC will have applied variable_size to
5051 the type, but that won't have done anything. So, if it's not
5052 a constant or self-referential, call elaborate_expression_1 to
5053 make a variable for the size rather than calculating it each time.
5054 Handle both the RM size and the actual size. */
5055 if (global_bindings_p ()
5056 && TYPE_SIZE (gnu_type)
5057 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
5058 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5060 tree size = TYPE_SIZE (gnu_type);
5062 TYPE_SIZE (gnu_type)
5063 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
5064 false);
5066 /* ??? For now, store the size as a multiple of the alignment in
5067 bytes so that we can see the alignment from the tree. */
5068 TYPE_SIZE_UNIT (gnu_type)
5069 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
5070 "SIZE_A_UNIT", definition, false,
5071 TYPE_ALIGN (gnu_type));
5073 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
5074 may not be marked by the call to create_type_decl below. */
5075 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
5077 if (TREE_CODE (gnu_type) == RECORD_TYPE)
5079 tree variant_part = get_variant_part (gnu_type);
5080 tree ada_size = TYPE_ADA_SIZE (gnu_type);
5082 if (variant_part)
5084 tree union_type = TREE_TYPE (variant_part);
5085 tree offset = DECL_FIELD_OFFSET (variant_part);
5087 /* If the position of the variant part is constant, subtract
5088 it from the size of the type of the parent to get the new
5089 size. This manual CSE reduces the data size. */
5090 if (TREE_CODE (offset) == INTEGER_CST)
5092 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
5093 TYPE_SIZE (union_type)
5094 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
5095 bit_from_pos (offset, bitpos));
5096 TYPE_SIZE_UNIT (union_type)
5097 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
5098 byte_from_pos (offset, bitpos));
5100 else
5102 TYPE_SIZE (union_type)
5103 = elaborate_expression_1 (TYPE_SIZE (union_type),
5104 gnat_entity, "VSIZE",
5105 definition, false);
5107 /* ??? For now, store the size as a multiple of the
5108 alignment in bytes so that we can see the alignment
5109 from the tree. */
5110 TYPE_SIZE_UNIT (union_type)
5111 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5112 gnat_entity, "VSIZE_A_UNIT",
5113 definition, false,
5114 TYPE_ALIGN (union_type));
5116 /* ??? For now, store the offset as a multiple of the
5117 alignment in bytes so that we can see the alignment
5118 from the tree. */
5119 DECL_FIELD_OFFSET (variant_part)
5120 = elaborate_expression_2 (offset, gnat_entity,
5121 "VOFFSET", definition, false,
5122 DECL_OFFSET_ALIGN
5123 (variant_part));
5126 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5127 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5130 if (operand_equal_p (ada_size, size, 0))
5131 ada_size = TYPE_SIZE (gnu_type);
5132 else
5133 ada_size
5134 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
5135 definition, false);
5136 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5140 /* If this is a record type or subtype, call elaborate_expression_2 on
5141 any field position. Do this for both global and local types.
5142 Skip any fields that we haven't made trees for to avoid problems with
5143 class wide types. */
5144 if (IN (kind, Record_Kind))
5145 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5146 gnat_temp = Next_Entity (gnat_temp))
5147 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5149 tree gnu_field = get_gnu_tree (gnat_temp);
5151 /* ??? For now, store the offset as a multiple of the alignment
5152 in bytes so that we can see the alignment from the tree. */
5153 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5155 DECL_FIELD_OFFSET (gnu_field)
5156 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5157 gnat_temp, "OFFSET", definition,
5158 false,
5159 DECL_OFFSET_ALIGN (gnu_field));
5161 /* ??? The context of gnu_field is not necessarily gnu_type
5162 so the MULT_EXPR node built above may not be marked by
5163 the call to create_type_decl below. */
5164 if (global_bindings_p ())
5165 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5169 if (Is_Atomic_Or_VFA (gnat_entity))
5170 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5172 /* If this is not an unconstrained array type, set some flags. */
5173 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5175 if (Present (Alignment_Clause (gnat_entity)))
5176 TYPE_USER_ALIGN (gnu_type) = 1;
5178 if (Universal_Aliasing (gnat_entity))
5179 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
5181 /* If it is passed by reference, force BLKmode to ensure that
5182 objects of this type will always be put in memory. */
5183 if (TYPE_MODE (gnu_type) != BLKmode
5184 && AGGREGATE_TYPE_P (gnu_type)
5185 && TYPE_BY_REFERENCE_P (gnu_type))
5186 SET_TYPE_MODE (gnu_type, BLKmode);
5188 if (Treat_As_Volatile (gnat_entity))
5190 const int quals
5191 = TYPE_QUAL_VOLATILE
5192 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
5193 gnu_type = change_qualified_type (gnu_type, quals);
5197 if (!gnu_decl)
5198 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5199 artificial_p, debug_info_p,
5200 gnat_entity);
5201 else
5203 TREE_TYPE (gnu_decl) = gnu_type;
5204 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5208 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5210 gnu_type = TREE_TYPE (gnu_decl);
5212 /* If this is a derived type, relate its alias set to that of its parent
5213 to avoid troubles when a call to an inherited primitive is inlined in
5214 a context where a derived object is accessed. The inlined code works
5215 on the parent view so the resulting code may access the same object
5216 using both the parent and the derived alias sets, which thus have to
5217 conflict. As the same issue arises with component references, the
5218 parent alias set also has to conflict with composite types enclosing
5219 derived components. For instance, if we have:
5221 type D is new T;
5222 type R is record
5223 Component : D;
5224 end record;
5226 we want T to conflict with both D and R, in addition to R being a
5227 superset of D by record/component construction.
5229 One way to achieve this is to perform an alias set copy from the
5230 parent to the derived type. This is not quite appropriate, though,
5231 as we don't want separate derived types to conflict with each other:
5233 type I1 is new Integer;
5234 type I2 is new Integer;
5236 We want I1 and I2 to both conflict with Integer but we do not want
5237 I1 to conflict with I2, and an alias set copy on derivation would
5238 have that effect.
5240 The option chosen is to make the alias set of the derived type a
5241 superset of that of its parent type. It trivially fulfills the
5242 simple requirement for the Integer derivation example above, and
5243 the component case as well by superset transitivity:
5245 superset superset
5246 R ----------> D ----------> T
5248 However, for composite types, conversions between derived types are
5249 translated into VIEW_CONVERT_EXPRs so a sequence like:
5251 type Comp1 is new Comp;
5252 type Comp2 is new Comp;
5253 procedure Proc (C : Comp1);
5255 C : Comp2;
5256 Proc (Comp1 (C));
5258 is translated into:
5260 C : Comp2;
5261 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5263 and gimplified into:
5265 C : Comp2;
5266 Comp1 *C.0;
5267 C.0 = (Comp1 *) &C;
5268 Proc (C.0);
5270 i.e. generates code involving type punning. Therefore, Comp1 needs
5271 to conflict with Comp2 and an alias set copy is required.
5273 The language rules ensure the parent type is already frozen here. */
5274 if (kind != E_Subprogram_Type
5275 && Is_Derived_Type (gnat_entity)
5276 && !type_annotate_only)
5278 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5279 /* For constrained packed array subtypes, the implementation type is
5280 used instead of the nominal type. */
5281 if (kind == E_Array_Subtype
5282 && Is_Constrained (gnat_entity)
5283 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5284 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5285 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5286 Is_Composite_Type (gnat_entity)
5287 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5290 /* Back-annotate the Alignment of the type if not already in the
5291 tree. Likewise for sizes. */
5292 if (Unknown_Alignment (gnat_entity))
5294 unsigned int double_align, align;
5295 bool is_capped_double, align_clause;
5297 /* If the default alignment of "double" or larger scalar types is
5298 specifically capped and this is not an array with an alignment
5299 clause on the component type, return the cap. */
5300 if ((double_align = double_float_alignment) > 0)
5301 is_capped_double
5302 = is_double_float_or_array (gnat_entity, &align_clause);
5303 else if ((double_align = double_scalar_alignment) > 0)
5304 is_capped_double
5305 = is_double_scalar_or_array (gnat_entity, &align_clause);
5306 else
5307 is_capped_double = align_clause = false;
5309 if (is_capped_double && !align_clause)
5310 align = double_align;
5311 else
5312 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5314 Set_Alignment (gnat_entity, UI_From_Int (align));
5317 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5319 tree gnu_size = TYPE_SIZE (gnu_type);
5321 /* If the size is self-referential, annotate the maximum value. */
5322 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5323 gnu_size = max_size (gnu_size, true);
5325 /* If we are just annotating types and the type is tagged, the tag
5326 and the parent components are not generated by the front-end so
5327 alignment and sizes must be adjusted if there is no rep clause. */
5328 if (type_annotate_only
5329 && Is_Tagged_Type (gnat_entity)
5330 && Unknown_RM_Size (gnat_entity)
5331 && !VOID_TYPE_P (gnu_type)
5332 && (!TYPE_FIELDS (gnu_type)
5333 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5335 tree offset;
5337 if (Is_Derived_Type (gnat_entity))
5339 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5340 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5341 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5343 else
5345 unsigned int align
5346 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
5347 offset = bitsize_int (POINTER_SIZE);
5348 Set_Alignment (gnat_entity, UI_From_Int (align));
5351 if (TYPE_FIELDS (gnu_type))
5352 offset
5353 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5355 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5356 gnu_size = round_up (gnu_size, POINTER_SIZE);
5357 Uint uint_size = annotate_value (gnu_size);
5358 Set_RM_Size (gnat_entity, uint_size);
5359 Set_Esize (gnat_entity, uint_size);
5362 /* If there is a rep clause, only adjust alignment and Esize. */
5363 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5365 unsigned int align
5366 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
5367 Set_Alignment (gnat_entity, UI_From_Int (align));
5368 gnu_size = round_up (gnu_size, POINTER_SIZE);
5369 Set_Esize (gnat_entity, annotate_value (gnu_size));
5372 /* Otherwise no adjustment is needed. */
5373 else
5374 Set_Esize (gnat_entity, annotate_value (gnu_size));
5377 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5378 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5381 /* If we haven't already, associate the ..._DECL node that we just made with
5382 the input GNAT entity node. */
5383 if (!saved)
5384 save_gnu_tree (gnat_entity, gnu_decl, false);
5386 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5387 eliminate as many deferred computations as possible. */
5388 process_deferred_decl_context (false);
5390 /* If this is an enumeration or floating-point type, we were not able to set
5391 the bounds since they refer to the type. These are always static. */
5392 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5393 || (kind == E_Floating_Point_Type))
5395 tree gnu_scalar_type = gnu_type;
5396 tree gnu_low_bound, gnu_high_bound;
5398 /* If this is a padded type, we need to use the underlying type. */
5399 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5400 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5402 /* If this is a floating point type and we haven't set a floating
5403 point type yet, use this in the evaluation of the bounds. */
5404 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5405 longest_float_type_node = gnu_scalar_type;
5407 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5408 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5410 if (kind == E_Enumeration_Type)
5412 /* Enumeration types have specific RM bounds. */
5413 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5414 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5416 else
5418 /* Floating-point types don't have specific RM bounds. */
5419 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5420 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5424 /* If we deferred processing of incomplete types, re-enable it. If there
5425 were no other disables and we have deferred types to process, do so. */
5426 if (this_deferred
5427 && --defer_incomplete_level == 0
5428 && defer_incomplete_list)
5430 struct incomplete *p, *next;
5432 /* We are back to level 0 for the deferring of incomplete types.
5433 But processing these incomplete types below may itself require
5434 deferring, so preserve what we have and restart from scratch. */
5435 p = defer_incomplete_list;
5436 defer_incomplete_list = NULL;
5438 for (; p; p = next)
5440 next = p->next;
5442 if (p->old_type)
5443 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5444 gnat_to_gnu_type (p->full_type));
5445 free (p);
5449 /* If we are not defining this type, see if it's on one of the lists of
5450 incomplete types. If so, handle the list entry now. */
5451 if (is_type && !definition)
5453 struct incomplete *p;
5455 for (p = defer_incomplete_list; p; p = p->next)
5456 if (p->old_type && p->full_type == gnat_entity)
5458 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5459 TREE_TYPE (gnu_decl));
5460 p->old_type = NULL_TREE;
5463 for (p = defer_limited_with; p; p = p->next)
5464 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5466 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5467 TREE_TYPE (gnu_decl));
5468 p->old_type = NULL_TREE;
5472 if (this_global)
5473 force_global--;
5475 /* If this is a packed array type whose original array type is itself
5476 an Itype without freeze node, make sure the latter is processed. */
5477 if (Is_Packed_Array_Impl_Type (gnat_entity)
5478 && Is_Itype (Original_Array_Type (gnat_entity))
5479 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5480 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5481 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5483 return gnu_decl;
5486 /* Similar, but if the returned value is a COMPONENT_REF, return the
5487 FIELD_DECL. */
5489 tree
5490 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5492 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5494 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5495 gnu_field = TREE_OPERAND (gnu_field, 1);
5497 return gnu_field;
5500 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5501 the GCC type corresponding to that entity. */
5503 tree
5504 gnat_to_gnu_type (Entity_Id gnat_entity)
5506 tree gnu_decl;
5508 /* The back end never attempts to annotate generic types. */
5509 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5510 return void_type_node;
5512 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5513 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5515 return TREE_TYPE (gnu_decl);
5518 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5519 the unpadded version of the GCC type corresponding to that entity. */
5521 tree
5522 get_unpadded_type (Entity_Id gnat_entity)
5524 tree type = gnat_to_gnu_type (gnat_entity);
5526 if (TYPE_IS_PADDING_P (type))
5527 type = TREE_TYPE (TYPE_FIELDS (type));
5529 return type;
5532 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5533 type has been changed to that of the parameterless procedure, except if an
5534 alias is already present, in which case it is returned instead. */
5536 tree
5537 get_minimal_subprog_decl (Entity_Id gnat_entity)
5539 tree gnu_entity_name, gnu_ext_name;
5540 struct attrib *attr_list = NULL;
5542 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5543 of the handling applied here. */
5545 while (Present (Alias (gnat_entity)))
5547 gnat_entity = Alias (gnat_entity);
5548 if (present_gnu_tree (gnat_entity))
5549 return get_gnu_tree (gnat_entity);
5552 gnu_entity_name = get_entity_name (gnat_entity);
5553 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5555 if (Has_Stdcall_Convention (gnat_entity))
5556 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5557 get_identifier ("stdcall"), NULL_TREE,
5558 gnat_entity);
5559 else if (Has_Thiscall_Convention (gnat_entity))
5560 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5561 get_identifier ("thiscall"), NULL_TREE,
5562 gnat_entity);
5564 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5565 gnu_ext_name = NULL_TREE;
5567 return
5568 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5569 is_disabled, false, true, true, false, true, false,
5570 attr_list, gnat_entity);
5573 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5574 a C++ imported method or equivalent.
5576 We use the predicate on 32-bit x86/Windows to find out whether we need to
5577 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5578 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5580 bool
5581 is_cplusplus_method (Entity_Id gnat_entity)
5583 /* Check that the subprogram has C++ convention. */
5584 if (Convention (gnat_entity) != Convention_CPP)
5585 return false;
5587 /* A constructor is a method on the C++ side. We deal with it now because
5588 it is declared without the 'this' parameter in the sources and, although
5589 the front-end will create a version with the 'this' parameter for code
5590 generation purposes, we want to return true for both versions. */
5591 if (Is_Constructor (gnat_entity))
5592 return true;
5594 /* And that the type of the first parameter (indirectly) has it too. */
5595 Entity_Id gnat_first = First_Formal (gnat_entity);
5596 if (No (gnat_first))
5597 return false;
5599 Entity_Id gnat_type = Etype (gnat_first);
5600 if (Is_Access_Type (gnat_type))
5601 gnat_type = Directly_Designated_Type (gnat_type);
5602 if (Convention (gnat_type) != Convention_CPP)
5603 return false;
5605 /* This is the main case: C++ method imported as a primitive operation.
5606 Note that a C++ class with no virtual functions can be imported as a
5607 limited record type so the operation is not necessarily dispatching. */
5608 if (Is_Primitive (gnat_entity))
5609 return true;
5611 /* A thunk needs to be handled like its associated primitive operation. */
5612 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5613 return true;
5615 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5616 if (Is_Dispatch_Table_Entity (gnat_entity))
5617 return true;
5619 return false;
5622 /* Finalize the processing of From_Limited_With incomplete types. */
5624 void
5625 finalize_from_limited_with (void)
5627 struct incomplete *p, *next;
5629 p = defer_limited_with;
5630 defer_limited_with = NULL;
5632 for (; p; p = next)
5634 next = p->next;
5636 if (p->old_type)
5637 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5638 gnat_to_gnu_type (p->full_type));
5639 free (p);
5643 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5644 kind of type (such E_Task_Type) that has a different type which Gigi
5645 uses for its representation. If the type does not have a special type
5646 for its representation, return GNAT_ENTITY. If a type is supposed to
5647 exist, but does not, abort unless annotating types, in which case
5648 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5650 Entity_Id
5651 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5653 Entity_Id gnat_equiv = gnat_entity;
5655 if (No (gnat_entity))
5656 return gnat_entity;
5658 switch (Ekind (gnat_entity))
5660 case E_Class_Wide_Subtype:
5661 if (Present (Equivalent_Type (gnat_entity)))
5662 gnat_equiv = Equivalent_Type (gnat_entity);
5663 break;
5665 case E_Access_Protected_Subprogram_Type:
5666 case E_Anonymous_Access_Protected_Subprogram_Type:
5667 gnat_equiv = Equivalent_Type (gnat_entity);
5668 break;
5670 case E_Class_Wide_Type:
5671 gnat_equiv = Root_Type (gnat_entity);
5672 break;
5674 case E_Task_Type:
5675 case E_Task_Subtype:
5676 case E_Protected_Type:
5677 case E_Protected_Subtype:
5678 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5679 break;
5681 default:
5682 break;
5685 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5687 return gnat_equiv;
5690 /* Return a GCC tree for a type corresponding to the component type of the
5691 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5692 is for an array being defined. DEBUG_INFO_P is true if we need to write
5693 debug information for other types that we may create in the process. */
5695 static tree
5696 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5697 bool debug_info_p)
5699 const Entity_Id gnat_type = Component_Type (gnat_array);
5700 tree gnu_type = gnat_to_gnu_type (gnat_type);
5701 tree gnu_comp_size;
5703 /* Try to get a smaller form of the component if needed. */
5704 if ((Is_Packed (gnat_array)
5705 || Has_Component_Size_Clause (gnat_array))
5706 && !Is_Bit_Packed_Array (gnat_array)
5707 && !Has_Aliased_Components (gnat_array)
5708 && !Strict_Alignment (gnat_type)
5709 && RECORD_OR_UNION_TYPE_P (gnu_type)
5710 && !TYPE_FAT_POINTER_P (gnu_type)
5711 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5712 gnu_type = make_packable_type (gnu_type, false);
5714 if (Has_Atomic_Components (gnat_array))
5715 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5717 /* Get and validate any specified Component_Size. */
5718 gnu_comp_size
5719 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5720 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5721 true, Has_Component_Size_Clause (gnat_array));
5723 /* If the array has aliased components and the component size can be zero,
5724 force at least unit size to ensure that the components have distinct
5725 addresses. */
5726 if (!gnu_comp_size
5727 && Has_Aliased_Components (gnat_array)
5728 && (integer_zerop (TYPE_SIZE (gnu_type))
5729 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5730 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5731 gnu_comp_size
5732 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5734 /* If the component type is a RECORD_TYPE that has a self-referential size,
5735 then use the maximum size for the component size. */
5736 if (!gnu_comp_size
5737 && TREE_CODE (gnu_type) == RECORD_TYPE
5738 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5739 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5741 /* Honor the component size. This is not needed for bit-packed arrays. */
5742 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5744 tree orig_type = gnu_type;
5745 unsigned int max_align;
5747 /* If an alignment is specified, use it as a cap on the component type
5748 so that it can be honored for the whole type. But ignore it for the
5749 original type of packed array types. */
5750 if (No (Packed_Array_Impl_Type (gnat_array))
5751 && Known_Alignment (gnat_array))
5752 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5753 else
5754 max_align = 0;
5756 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5757 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5758 gnu_type = orig_type;
5759 else
5760 orig_type = gnu_type;
5762 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5763 true, false, definition, true);
5765 /* If a padding record was made, declare it now since it will never be
5766 declared otherwise. This is necessary to ensure that its subtrees
5767 are properly marked. */
5768 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5769 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5770 gnat_array);
5773 /* If the component type is a padded type made for a non-bit-packed array
5774 of scalars with reverse storage order, we need to propagate the reverse
5775 storage order to the padding type since it is the innermost enclosing
5776 aggregate type around the scalar. */
5777 if (TYPE_IS_PADDING_P (gnu_type)
5778 && Reverse_Storage_Order (gnat_array)
5779 && !Is_Bit_Packed_Array (gnat_array)
5780 && Is_Scalar_Type (gnat_type))
5781 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5783 if (Has_Volatile_Components (gnat_array))
5785 const int quals
5786 = TYPE_QUAL_VOLATILE
5787 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5788 gnu_type = change_qualified_type (gnu_type, quals);
5791 return gnu_type;
5794 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5795 using MECH as its passing mechanism, to be placed in the parameter
5796 list built for GNAT_SUBPROG. Assume a foreign convention for the
5797 latter if FOREIGN is true. Also set CICO to true if the parameter
5798 must use the copy-in copy-out implementation mechanism.
5800 The returned tree is a PARM_DECL, except for those cases where no
5801 parameter needs to be actually passed to the subprogram; the type
5802 of this "shadow" parameter is then returned instead. */
5804 static tree
5805 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5806 Entity_Id gnat_subprog, bool foreign, bool *cico)
5808 tree gnu_param_name = get_entity_name (gnat_param);
5809 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5810 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5811 /* The parameter can be indirectly modified if its address is taken. */
5812 bool ro_param = in_param && !Address_Taken (gnat_param);
5813 bool by_return = false, by_component_ptr = false;
5814 bool by_ref = false;
5815 bool restricted_aliasing_p = false;
5816 tree gnu_param;
5818 /* Copy-return is used only for the first parameter of a valued procedure.
5819 It's a copy mechanism for which a parameter is never allocated. */
5820 if (mech == By_Copy_Return)
5822 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5823 mech = By_Copy;
5824 by_return = true;
5827 /* If this is either a foreign function or if the underlying type won't
5828 be passed by reference and is as aligned as the original type, strip
5829 off possible padding type. */
5830 if (TYPE_IS_PADDING_P (gnu_param_type))
5832 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5834 if (foreign
5835 || (!must_pass_by_ref (unpadded_type)
5836 && mech != By_Reference
5837 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5838 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5839 gnu_param_type = unpadded_type;
5842 /* If this is a read-only parameter, make a variant of the type that is
5843 read-only. ??? However, if this is an unconstrained array, that type
5844 can be very complex, so skip it for now. Likewise for any other
5845 self-referential type. */
5846 if (ro_param
5847 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5848 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5849 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5851 /* For foreign conventions, pass arrays as pointers to the element type.
5852 First check for unconstrained array and get the underlying array. */
5853 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5854 gnu_param_type
5855 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5857 /* For GCC builtins, pass Address integer types as (void *) */
5858 if (Convention (gnat_subprog) == Convention_Intrinsic
5859 && Present (Interface_Name (gnat_subprog))
5860 && Is_Descendent_Of_Address (Etype (gnat_param)))
5861 gnu_param_type = ptr_type_node;
5863 /* Arrays are passed as pointers to element type for foreign conventions. */
5864 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5866 /* Strip off any multi-dimensional entries, then strip
5867 off the last array to get the component type. */
5868 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5869 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5870 gnu_param_type = TREE_TYPE (gnu_param_type);
5872 by_component_ptr = true;
5873 gnu_param_type = TREE_TYPE (gnu_param_type);
5875 if (ro_param)
5876 gnu_param_type
5877 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5879 gnu_param_type = build_pointer_type (gnu_param_type);
5882 /* Fat pointers are passed as thin pointers for foreign conventions. */
5883 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5884 gnu_param_type
5885 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5887 /* If we must pass or were requested to pass by reference, do so.
5888 If we were requested to pass by copy, do so.
5889 Otherwise, for foreign conventions, pass In Out or Out parameters
5890 or aggregates by reference. For COBOL and Fortran, pass all
5891 integer and FP types that way too. For Convention Ada, use
5892 the standard Ada default. */
5893 else if (must_pass_by_ref (gnu_param_type)
5894 || mech == By_Reference
5895 || (mech != By_Copy
5896 && ((foreign
5897 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5898 || (foreign
5899 && (Convention (gnat_subprog) == Convention_Fortran
5900 || Convention (gnat_subprog) == Convention_COBOL)
5901 && (INTEGRAL_TYPE_P (gnu_param_type)
5902 || FLOAT_TYPE_P (gnu_param_type)))
5903 || (!foreign
5904 && default_pass_by_ref (gnu_param_type)))))
5906 gnu_param_type = build_reference_type (gnu_param_type);
5907 /* We take advantage of 6.2(12) by considering that references built for
5908 parameters whose type isn't by-ref and for which the mechanism hasn't
5909 been forced to by-ref allow only a restricted form of aliasing. */
5910 restricted_aliasing_p
5911 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5912 by_ref = true;
5915 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5916 else if (!in_param)
5917 *cico = true;
5919 if (mech == By_Copy && (by_ref || by_component_ptr))
5920 post_error ("?cannot pass & by copy", gnat_param);
5922 /* If this is an Out parameter that isn't passed by reference and isn't
5923 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5924 it will be a VAR_DECL created when we process the procedure, so just
5925 return its type. For the special parameter of a valued procedure,
5926 never pass it in.
5928 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5929 Out parameters with discriminants or implicit initial values to be
5930 handled like In Out parameters. These type are normally built as
5931 aggregates, hence passed by reference, except for some packed arrays
5932 which end up encoded in special integer types. Note that scalars can
5933 be given implicit initial values using the Default_Value aspect.
5935 The exception we need to make is then for packed arrays of records
5936 with discriminants or implicit initial values. We have no light/easy
5937 way to check for the latter case, so we merely check for packed arrays
5938 of records. This may lead to useless copy-in operations, but in very
5939 rare cases only, as these would be exceptions in a set of already
5940 exceptional situations. */
5941 if (Ekind (gnat_param) == E_Out_Parameter
5942 && !by_ref
5943 && (by_return
5944 || (!POINTER_TYPE_P (gnu_param_type)
5945 && !AGGREGATE_TYPE_P (gnu_param_type)
5946 && !Has_Default_Aspect (Etype (gnat_param))))
5947 && !(Is_Array_Type (Etype (gnat_param))
5948 && Is_Packed (Etype (gnat_param))
5949 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5950 return gnu_param_type;
5952 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5953 ro_param || by_ref || by_component_ptr);
5954 DECL_BY_REF_P (gnu_param) = by_ref;
5955 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5956 DECL_POINTS_TO_READONLY_P (gnu_param)
5957 = (ro_param && (by_ref || by_component_ptr));
5958 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5959 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5961 /* If no Mechanism was specified, indicate what we're using, then
5962 back-annotate it. */
5963 if (mech == Default)
5964 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5966 Set_Mechanism (gnat_param, mech);
5967 return gnu_param;
5970 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5971 with of the main unit and whose full view has not been elaborated yet. */
5973 static bool
5974 is_from_limited_with_of_main (Entity_Id gnat_entity)
5976 /* Class-wide types are always transformed into their root type. */
5977 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5978 gnat_entity = Root_Type (gnat_entity);
5980 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5981 && From_Limited_With (gnat_entity))
5983 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5985 if (present_gnu_tree (gnat_full_view))
5986 return false;
5988 return In_Extended_Main_Code_Unit (gnat_full_view);
5991 return false;
5994 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5995 qualifiers on TYPE. */
5997 static tree
5998 change_qualified_type (tree type, int type_quals)
6000 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6003 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6005 static bool
6006 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6008 while (Present (Corresponding_Discriminant (discr1)))
6009 discr1 = Corresponding_Discriminant (discr1);
6011 while (Present (Corresponding_Discriminant (discr2)))
6012 discr2 = Corresponding_Discriminant (discr2);
6014 return
6015 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6018 /* Return true if the array type GNU_TYPE, which represents a dimension of
6019 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6021 static bool
6022 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6024 /* If the array type is not the innermost dimension of the GNAT type,
6025 then it has a non-aliased component. */
6026 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6027 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6028 return true;
6030 /* If the array type has an aliased component in the front-end sense,
6031 then it also has an aliased component in the back-end sense. */
6032 if (Has_Aliased_Components (gnat_type))
6033 return false;
6035 /* If this is a derived type, then it has a non-aliased component if
6036 and only if its parent type also has one. */
6037 if (Is_Derived_Type (gnat_type))
6039 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6040 int index;
6041 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6042 gnu_parent_type
6043 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6044 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6045 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6046 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6049 /* Otherwise, rely exclusively on properties of the element type. */
6050 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6053 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6055 static bool
6056 compile_time_known_address_p (Node_Id gnat_address)
6058 /* Catch System'To_Address. */
6059 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6060 gnat_address = Expression (gnat_address);
6062 return Compile_Time_Known_Value (gnat_address);
6065 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6066 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6068 static bool
6069 cannot_be_superflat (Node_Id gnat_range)
6071 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6072 Node_Id scalar_range;
6073 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6075 /* If the low bound is not constant, try to find an upper bound. */
6076 while (Nkind (gnat_lb) != N_Integer_Literal
6077 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6078 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6079 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6080 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6081 || Nkind (scalar_range) == N_Range))
6082 gnat_lb = High_Bound (scalar_range);
6084 /* If the high bound is not constant, try to find a lower bound. */
6085 while (Nkind (gnat_hb) != N_Integer_Literal
6086 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6087 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6088 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6089 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6090 || Nkind (scalar_range) == N_Range))
6091 gnat_hb = Low_Bound (scalar_range);
6093 /* If we have failed to find constant bounds, punt. */
6094 if (Nkind (gnat_lb) != N_Integer_Literal
6095 || Nkind (gnat_hb) != N_Integer_Literal)
6096 return false;
6098 /* We need at least a signed 64-bit type to catch most cases. */
6099 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6100 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6101 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6102 return false;
6104 /* If the low bound is the smallest integer, nothing can be smaller. */
6105 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6106 if (TREE_OVERFLOW (gnu_lb_minus_one))
6107 return true;
6109 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6112 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6114 static bool
6115 constructor_address_p (tree gnu_expr)
6117 while (TREE_CODE (gnu_expr) == NOP_EXPR
6118 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6119 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6120 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6122 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6123 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6126 /* Return true if the size in units represented by GNU_SIZE can be handled by
6127 an allocation. If STATIC_P is true, consider only what can be done with a
6128 static allocation. */
6130 static bool
6131 allocatable_size_p (tree gnu_size, bool static_p)
6133 /* We can allocate a fixed size if it is a valid for the middle-end. */
6134 if (TREE_CODE (gnu_size) == INTEGER_CST)
6135 return valid_constant_size_p (gnu_size);
6137 /* We can allocate a variable size if this isn't a static allocation. */
6138 else
6139 return !static_p;
6142 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6143 initial value of an object of GNU_TYPE. */
6145 static bool
6146 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6148 /* Do not convert if the object's type is unconstrained because this would
6149 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6150 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6151 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6152 return false;
6154 /* Do not convert if the object's type is a padding record whose field is of
6155 self-referential size because we want to copy only the actual data. */
6156 if (type_is_padding_self_referential (gnu_type))
6157 return false;
6159 /* Do not convert a call to a function that returns with variable size since
6160 we want to use the return slot optimization in this case. */
6161 if (TREE_CODE (gnu_expr) == CALL_EXPR
6162 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6163 return false;
6165 /* Do not convert to a record type with a variant part from a record type
6166 without one, to keep the object simpler. */
6167 if (TREE_CODE (gnu_type) == RECORD_TYPE
6168 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6169 && get_variant_part (gnu_type)
6170 && !get_variant_part (TREE_TYPE (gnu_expr)))
6171 return false;
6173 /* In all the other cases, convert the expression to the object's type. */
6174 return true;
6177 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6178 be elaborated at the point of its definition, but do nothing else. */
6180 void
6181 elaborate_entity (Entity_Id gnat_entity)
6183 switch (Ekind (gnat_entity))
6185 case E_Signed_Integer_Subtype:
6186 case E_Modular_Integer_Subtype:
6187 case E_Enumeration_Subtype:
6188 case E_Ordinary_Fixed_Point_Subtype:
6189 case E_Decimal_Fixed_Point_Subtype:
6190 case E_Floating_Point_Subtype:
6192 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6193 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6195 /* ??? Tests to avoid Constraint_Error in static expressions
6196 are needed until after the front stops generating bogus
6197 conversions on bounds of real types. */
6198 if (!Raises_Constraint_Error (gnat_lb))
6199 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6200 Needs_Debug_Info (gnat_entity));
6201 if (!Raises_Constraint_Error (gnat_hb))
6202 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6203 Needs_Debug_Info (gnat_entity));
6204 break;
6207 case E_Record_Subtype:
6208 case E_Private_Subtype:
6209 case E_Limited_Private_Subtype:
6210 case E_Record_Subtype_With_Private:
6211 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6213 Node_Id gnat_discriminant_expr;
6214 Entity_Id gnat_field;
6216 for (gnat_field
6217 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6218 gnat_discriminant_expr
6219 = First_Elmt (Discriminant_Constraint (gnat_entity));
6220 Present (gnat_field);
6221 gnat_field = Next_Discriminant (gnat_field),
6222 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6223 /* Ignore access discriminants. */
6224 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6225 elaborate_expression (Node (gnat_discriminant_expr),
6226 gnat_entity, get_entity_char (gnat_field),
6227 true, false, false);
6229 break;
6234 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6235 NAME, ARGS and ERROR_POINT. */
6237 static void
6238 prepend_one_attribute (struct attrib **attr_list,
6239 enum attrib_type attrib_type,
6240 tree attr_name,
6241 tree attr_args,
6242 Node_Id attr_error_point)
6244 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6246 attr->type = attrib_type;
6247 attr->name = attr_name;
6248 attr->args = attr_args;
6249 attr->error_point = attr_error_point;
6251 attr->next = *attr_list;
6252 *attr_list = attr;
6255 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6257 static void
6258 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6260 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6261 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6262 enum attrib_type etype;
6264 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6265 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6267 case Pragma_Machine_Attribute:
6268 etype = ATTR_MACHINE_ATTRIBUTE;
6269 break;
6271 case Pragma_Linker_Alias:
6272 etype = ATTR_LINK_ALIAS;
6273 break;
6275 case Pragma_Linker_Section:
6276 etype = ATTR_LINK_SECTION;
6277 break;
6279 case Pragma_Linker_Constructor:
6280 etype = ATTR_LINK_CONSTRUCTOR;
6281 break;
6283 case Pragma_Linker_Destructor:
6284 etype = ATTR_LINK_DESTRUCTOR;
6285 break;
6287 case Pragma_Weak_External:
6288 etype = ATTR_WEAK_EXTERNAL;
6289 break;
6291 case Pragma_Thread_Local_Storage:
6292 etype = ATTR_THREAD_LOCAL_STORAGE;
6293 break;
6295 default:
6296 return;
6299 /* See what arguments we have and turn them into GCC trees for attribute
6300 handlers. These expect identifier for strings. We handle at most two
6301 arguments and static expressions only. */
6302 if (Present (gnat_arg) && Present (First (gnat_arg)))
6304 Node_Id gnat_arg0 = Next (First (gnat_arg));
6305 Node_Id gnat_arg1 = Empty;
6307 if (Present (gnat_arg0)
6308 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6310 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6312 if (TREE_CODE (gnu_arg0) == STRING_CST)
6314 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6315 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6316 return;
6319 gnat_arg1 = Next (gnat_arg0);
6322 if (Present (gnat_arg1)
6323 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6325 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6327 if (TREE_CODE (gnu_arg1) == STRING_CST)
6328 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6332 /* Prepend to the list. Make a list of the argument we might have, as GCC
6333 expects it. */
6334 prepend_one_attribute (attr_list, etype, gnu_arg0,
6335 gnu_arg1
6336 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6337 Present (Next (First (gnat_arg)))
6338 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6341 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6343 static void
6344 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6346 Node_Id gnat_temp;
6348 /* Attributes are stored as Representation Item pragmas. */
6349 for (gnat_temp = First_Rep_Item (gnat_entity);
6350 Present (gnat_temp);
6351 gnat_temp = Next_Rep_Item (gnat_temp))
6352 if (Nkind (gnat_temp) == N_Pragma)
6353 prepend_one_attribute_pragma (attr_list, gnat_temp);
6356 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6357 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6358 return the GCC tree to use for that expression. S is the suffix to use
6359 if a variable needs to be created and DEFINITION is true if this is done
6360 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6361 otherwise, we are just elaborating the expression for side-effects. If
6362 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6363 isn't needed for code generation. */
6365 static tree
6366 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6367 bool definition, bool need_value, bool need_debug)
6369 tree gnu_expr;
6371 /* If we already elaborated this expression (e.g. it was involved
6372 in the definition of a private type), use the old value. */
6373 if (present_gnu_tree (gnat_expr))
6374 return get_gnu_tree (gnat_expr);
6376 /* If we don't need a value and this is static or a discriminant,
6377 we don't need to do anything. */
6378 if (!need_value
6379 && (Is_OK_Static_Expression (gnat_expr)
6380 || (Nkind (gnat_expr) == N_Identifier
6381 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6382 return NULL_TREE;
6384 /* If it's a static expression, we don't need a variable for debugging. */
6385 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6386 need_debug = false;
6388 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6389 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6390 definition, need_debug);
6392 /* Save the expression in case we try to elaborate this entity again. Since
6393 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6394 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6395 save_gnu_tree (gnat_expr, gnu_expr, true);
6397 return need_value ? gnu_expr : error_mark_node;
6400 /* Similar, but take a GNU expression and always return a result. */
6402 static tree
6403 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6404 bool definition, bool need_debug)
6406 const bool expr_public_p = Is_Public (gnat_entity);
6407 const bool expr_global_p = expr_public_p || global_bindings_p ();
6408 bool expr_variable_p, use_variable;
6410 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6411 that an expression cannot contain both a discriminant and a variable. */
6412 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6413 return gnu_expr;
6415 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6416 a variable that is initialized to contain the expression when the package
6417 containing the definition is elaborated. If this entity is defined at top
6418 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6419 if this is necessary. */
6420 if (TREE_CONSTANT (gnu_expr))
6421 expr_variable_p = false;
6422 else
6424 /* Skip any conversions and simple constant arithmetics to see if the
6425 expression is based on a read-only variable. */
6426 tree inner = remove_conversions (gnu_expr, true);
6428 inner = skip_simple_constant_arithmetic (inner);
6430 if (handled_component_p (inner))
6431 inner = get_inner_constant_reference (inner);
6433 expr_variable_p
6434 = !(inner
6435 && TREE_CODE (inner) == VAR_DECL
6436 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6439 /* We only need to use the variable if we are in a global context since GCC
6440 can do the right thing in the local case. However, when not optimizing,
6441 use it for bounds of loop iteration scheme to avoid code duplication. */
6442 use_variable = expr_variable_p
6443 && (expr_global_p
6444 || (!optimize
6445 && definition
6446 && Is_Itype (gnat_entity)
6447 && Nkind (Associated_Node_For_Itype (gnat_entity))
6448 == N_Loop_Parameter_Specification));
6450 /* Now create it, possibly only for debugging purposes. */
6451 if (use_variable || need_debug)
6453 /* The following variable creation can happen when processing the body
6454 of subprograms that are defined out of the extended main unit and
6455 inlined. In this case, we are not at the global scope, and thus the
6456 new variable must not be tagged "external", as we used to do here as
6457 soon as DEFINITION was false. */
6458 tree gnu_decl
6459 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6460 TREE_TYPE (gnu_expr), gnu_expr, true,
6461 expr_public_p, !definition && expr_global_p,
6462 expr_global_p, false, true, need_debug,
6463 NULL, gnat_entity);
6465 /* Using this variable at debug time (if need_debug is true) requires a
6466 proper location. The back-end will compute a location for this
6467 variable only if the variable is used by the generated code.
6468 Returning the variable ensures the caller will use it in generated
6469 code. Note that there is no need for a location if the debug info
6470 contains an integer constant.
6471 TODO: when the encoding-based debug scheme is dropped, move this
6472 condition to the top-level IF block: we will not need to create a
6473 variable anymore in such cases, then. */
6474 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6475 return gnu_decl;
6478 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6481 /* Similar, but take an alignment factor and make it explicit in the tree. */
6483 static tree
6484 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6485 bool definition, bool need_debug, unsigned int align)
6487 tree unit_align = size_int (align / BITS_PER_UNIT);
6488 return
6489 size_binop (MULT_EXPR,
6490 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6491 gnu_expr,
6492 unit_align),
6493 gnat_entity, s, definition,
6494 need_debug),
6495 unit_align);
6498 /* Structure to hold internal data for elaborate_reference. */
6500 struct er_data
6502 Entity_Id entity;
6503 bool definition;
6504 unsigned int n;
6507 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6509 static tree
6510 elaborate_reference_1 (tree ref, void *data)
6512 struct er_data *er = (struct er_data *)data;
6513 char suffix[16];
6515 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6516 if (TREE_CONSTANT (ref))
6517 return ref;
6519 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6520 pointer. This may be more efficient, but will also allow us to more
6521 easily find the match for the PLACEHOLDER_EXPR. */
6522 if (TREE_CODE (ref) == COMPONENT_REF
6523 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6524 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6525 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6526 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
6528 sprintf (suffix, "EXP%d", ++er->n);
6529 return
6530 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6533 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6534 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6535 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6537 static tree
6538 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6539 tree *init)
6541 struct er_data er = { gnat_entity, definition, 0 };
6542 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6545 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6546 the value passed against the list of choices. */
6548 tree
6549 choices_to_gnu (tree operand, Node_Id choices)
6551 Node_Id choice;
6552 Node_Id gnat_temp;
6553 tree result = boolean_false_node;
6554 tree this_test, low = 0, high = 0, single = 0;
6556 for (choice = First (choices); Present (choice); choice = Next (choice))
6558 switch (Nkind (choice))
6560 case N_Range:
6561 low = gnat_to_gnu (Low_Bound (choice));
6562 high = gnat_to_gnu (High_Bound (choice));
6564 this_test
6565 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6566 build_binary_op (GE_EXPR, boolean_type_node,
6567 operand, low),
6568 build_binary_op (LE_EXPR, boolean_type_node,
6569 operand, high));
6571 break;
6573 case N_Subtype_Indication:
6574 gnat_temp = Range_Expression (Constraint (choice));
6575 low = gnat_to_gnu (Low_Bound (gnat_temp));
6576 high = gnat_to_gnu (High_Bound (gnat_temp));
6578 this_test
6579 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6580 build_binary_op (GE_EXPR, boolean_type_node,
6581 operand, low),
6582 build_binary_op (LE_EXPR, boolean_type_node,
6583 operand, high));
6584 break;
6586 case N_Identifier:
6587 case N_Expanded_Name:
6588 /* This represents either a subtype range, an enumeration
6589 literal, or a constant Ekind says which. If an enumeration
6590 literal or constant, fall through to the next case. */
6591 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6592 && Ekind (Entity (choice)) != E_Constant)
6594 tree type = gnat_to_gnu_type (Entity (choice));
6596 low = TYPE_MIN_VALUE (type);
6597 high = TYPE_MAX_VALUE (type);
6599 this_test
6600 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6601 build_binary_op (GE_EXPR, boolean_type_node,
6602 operand, low),
6603 build_binary_op (LE_EXPR, boolean_type_node,
6604 operand, high));
6605 break;
6608 /* ... fall through ... */
6610 case N_Character_Literal:
6611 case N_Integer_Literal:
6612 single = gnat_to_gnu (choice);
6613 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6614 single);
6615 break;
6617 case N_Others_Choice:
6618 this_test = boolean_true_node;
6619 break;
6621 default:
6622 gcc_unreachable ();
6625 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6626 this_test);
6629 return result;
6632 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6633 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6635 static int
6636 adjust_packed (tree field_type, tree record_type, int packed)
6638 /* If the field contains an item of variable size, we cannot pack it
6639 because we cannot create temporaries of non-fixed size in case
6640 we need to take the address of the field. See addressable_p and
6641 the notes on the addressability issues for further details. */
6642 if (type_has_variable_size (field_type))
6643 return 0;
6645 /* In the other cases, we can honor the packing. */
6646 if (packed)
6647 return packed;
6649 /* If the alignment of the record is specified and the field type
6650 is over-aligned, request Storage_Unit alignment for the field. */
6651 if (TYPE_ALIGN (record_type)
6652 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6653 return -1;
6655 /* Likewise if the maximum alignment of the record is specified. */
6656 if (TYPE_MAX_ALIGN (record_type)
6657 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6658 return -1;
6660 return 0;
6663 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6664 placed in GNU_RECORD_TYPE.
6666 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6667 record has Component_Alignment of Storage_Unit.
6669 DEFINITION is true if this field is for a record being defined.
6671 DEBUG_INFO_P is true if we need to write debug information for types
6672 that we may create in the process. */
6674 static tree
6675 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6676 bool definition, bool debug_info_p)
6678 const Entity_Id gnat_field_type = Etype (gnat_field);
6679 const bool is_aliased
6680 = Is_Aliased (gnat_field);
6681 const bool is_atomic
6682 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6683 const bool is_independent
6684 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6685 const bool is_volatile
6686 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6687 const bool needs_strict_alignment
6688 = (is_aliased
6689 || is_independent
6690 || is_volatile
6691 || Strict_Alignment (gnat_field_type));
6692 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6693 tree gnu_field_id = get_entity_name (gnat_field);
6694 tree gnu_field, gnu_size, gnu_pos;
6696 /* If this field requires strict alignment, we cannot pack it because
6697 it would very likely be under-aligned in the record. */
6698 if (needs_strict_alignment)
6699 packed = 0;
6700 else
6701 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6703 /* If a size is specified, use it. Otherwise, if the record type is packed,
6704 use the official RM size. See "Handling of Type'Size Values" in Einfo
6705 for further details. */
6706 if (Known_Esize (gnat_field))
6707 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6708 gnat_field, FIELD_DECL, false, true);
6709 else if (packed == 1)
6710 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6711 gnat_field, FIELD_DECL, false, true);
6712 else
6713 gnu_size = NULL_TREE;
6715 /* If we have a specified size that is smaller than that of the field's type,
6716 or a position is specified, and the field's type is a record that doesn't
6717 require strict alignment, see if we can get either an integral mode form
6718 of the type or a smaller form. If we can, show a size was specified for
6719 the field if there wasn't one already, so we know to make this a bitfield
6720 and avoid making things wider.
6722 Changing to an integral mode form is useful when the record is packed as
6723 we can then place the field at a non-byte-aligned position and so achieve
6724 tighter packing. This is in addition required if the field shares a byte
6725 with another field and the front-end lets the back-end handle the access
6726 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6728 Changing to a smaller form is required if the specified size is smaller
6729 than that of the field's type and the type contains sub-fields that are
6730 padded, in order to avoid generating accesses to these sub-fields that
6731 are wider than the field.
6733 We avoid the transformation if it is not required or potentially useful,
6734 as it might entail an increase of the field's alignment and have ripple
6735 effects on the outer record type. A typical case is a field known to be
6736 byte-aligned and not to share a byte with another field. */
6737 if (!needs_strict_alignment
6738 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6739 && !TYPE_FAT_POINTER_P (gnu_field_type)
6740 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6741 && (packed == 1
6742 || (gnu_size
6743 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6744 || (Present (Component_Clause (gnat_field))
6745 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6746 % BITS_PER_UNIT == 0
6747 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6749 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6750 if (gnu_packable_type != gnu_field_type)
6752 gnu_field_type = gnu_packable_type;
6753 if (!gnu_size)
6754 gnu_size = rm_size (gnu_field_type);
6758 if (Is_Atomic_Or_VFA (gnat_field))
6759 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6761 if (Present (Component_Clause (gnat_field)))
6763 Node_Id gnat_clause = Component_Clause (gnat_field);
6764 Entity_Id gnat_parent
6765 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6767 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6768 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6769 gnat_field, FIELD_DECL, false, true);
6771 /* Ensure the position does not overlap with the parent subtype, if there
6772 is one. This test is omitted if the parent of the tagged type has a
6773 full rep clause since, in this case, component clauses are allowed to
6774 overlay the space allocated for the parent type and the front-end has
6775 checked that there are no overlapping components. */
6776 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6778 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6780 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6781 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6782 post_error_ne_tree
6783 ("offset of& must be beyond parent{, minimum allowed is ^}",
6784 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6787 /* If this field needs strict alignment, make sure that the record is
6788 sufficiently aligned and that the position and size are consistent
6789 with the type. But don't do it if we are just annotating types and
6790 the field's type is tagged, since tagged types aren't fully laid out
6791 in this mode. Also, note that atomic implies volatile so the inner
6792 test sequences ordering is significant here. */
6793 if (needs_strict_alignment
6794 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6796 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6798 if (TYPE_ALIGN (gnu_record_type) < type_align)
6799 TYPE_ALIGN (gnu_record_type) = type_align;
6801 /* If the position is not a multiple of the alignment of the type,
6802 then error out and reset the position. */
6803 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6804 bitsize_int (type_align))))
6806 const char *s;
6808 if (is_atomic)
6809 s = "position of atomic field& must be multiple of ^ bits";
6810 else if (is_aliased)
6811 s = "position of aliased field& must be multiple of ^ bits";
6812 else if (is_independent)
6813 s = "position of independent field& must be multiple of ^ bits";
6814 else if (is_volatile)
6815 s = "position of volatile field& must be multiple of ^ bits";
6816 else if (Strict_Alignment (gnat_field_type))
6817 s = "position of & with aliased or tagged part must be"
6818 " multiple of ^ bits";
6819 else
6820 gcc_unreachable ();
6822 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6823 type_align);
6824 gnu_pos = NULL_TREE;
6827 if (gnu_size)
6829 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6830 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6832 /* If the size is lower than that of the type, or greater for
6833 atomic and aliased, then error out and reset the size. */
6834 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6836 const char *s;
6838 if (is_atomic)
6839 s = "size of atomic field& must be ^ bits";
6840 else if (is_aliased)
6841 s = "size of aliased field& must be ^ bits";
6842 else if (is_independent)
6843 s = "size of independent field& must be at least ^ bits";
6844 else if (is_volatile)
6845 s = "size of volatile field& must be at least ^ bits";
6846 else if (Strict_Alignment (gnat_field_type))
6847 s = "size of & with aliased or tagged part must be"
6848 " at least ^ bits";
6849 else
6850 gcc_unreachable ();
6852 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6853 gnu_type_size);
6854 gnu_size = NULL_TREE;
6857 /* Likewise if the size is not a multiple of a byte, */
6858 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6859 bitsize_unit_node)))
6861 const char *s;
6863 if (is_independent)
6864 s = "size of independent field& must be multiple of"
6865 " Storage_Unit";
6866 else if (is_volatile)
6867 s = "size of volatile field& must be multiple of"
6868 " Storage_Unit";
6869 else if (Strict_Alignment (gnat_field_type))
6870 s = "size of & with aliased or tagged part must be"
6871 " multiple of Storage_Unit";
6872 else
6873 gcc_unreachable ();
6875 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6876 gnu_size = NULL_TREE;
6882 /* If the record has rep clauses and this is the tag field, make a rep
6883 clause for it as well. */
6884 else if (Has_Specified_Layout (Scope (gnat_field))
6885 && Chars (gnat_field) == Name_uTag)
6887 gnu_pos = bitsize_zero_node;
6888 gnu_size = TYPE_SIZE (gnu_field_type);
6891 else
6893 gnu_pos = NULL_TREE;
6895 /* If we are packing the record and the field is BLKmode, round the
6896 size up to a byte boundary. */
6897 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6898 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6901 /* We need to make the size the maximum for the type if it is
6902 self-referential and an unconstrained type. In that case, we can't
6903 pack the field since we can't make a copy to align it. */
6904 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6905 && !gnu_size
6906 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6907 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6909 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6910 packed = 0;
6913 /* If a size is specified, adjust the field's type to it. */
6914 if (gnu_size)
6916 tree orig_field_type;
6918 /* If the field's type is justified modular, we would need to remove
6919 the wrapper to (better) meet the layout requirements. However we
6920 can do so only if the field is not aliased to preserve the unique
6921 layout and if the prescribed size is not greater than that of the
6922 packed array to preserve the justification. */
6923 if (!needs_strict_alignment
6924 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6925 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6926 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6927 <= 0)
6928 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6930 /* Similarly if the field's type is a misaligned integral type, but
6931 there is no restriction on the size as there is no justification. */
6932 if (!needs_strict_alignment
6933 && TYPE_IS_PADDING_P (gnu_field_type)
6934 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6935 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6937 gnu_field_type
6938 = make_type_from_size (gnu_field_type, gnu_size,
6939 Has_Biased_Representation (gnat_field));
6941 orig_field_type = gnu_field_type;
6942 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6943 false, false, definition, true);
6945 /* If a padding record was made, declare it now since it will never be
6946 declared otherwise. This is necessary to ensure that its subtrees
6947 are properly marked. */
6948 if (gnu_field_type != orig_field_type
6949 && !DECL_P (TYPE_NAME (gnu_field_type)))
6950 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6951 debug_info_p, gnat_field);
6954 /* Otherwise (or if there was an error), don't specify a position. */
6955 else
6956 gnu_pos = NULL_TREE;
6958 /* If the field's type is a padded type made for a scalar field of a record
6959 type with reverse storage order, we need to propagate the reverse storage
6960 order to the padding type since it is the innermost enclosing aggregate
6961 type around the scalar. */
6962 if (TYPE_IS_PADDING_P (gnu_field_type)
6963 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
6964 && Is_Scalar_Type (gnat_field_type))
6965 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
6967 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6968 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6970 /* Now create the decl for the field. */
6971 gnu_field
6972 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6973 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6974 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6975 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6976 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
6978 if (Ekind (gnat_field) == E_Discriminant)
6980 DECL_INVARIANT_P (gnu_field)
6981 = No (Discriminant_Default_Value (gnat_field));
6982 DECL_DISCRIMINANT_NUMBER (gnu_field)
6983 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6986 return gnu_field;
6989 /* Return true if at least one member of COMPONENT_LIST needs strict
6990 alignment. */
6992 static bool
6993 components_need_strict_alignment (Node_Id component_list)
6995 Node_Id component_decl;
6997 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6998 Present (component_decl);
6999 component_decl = Next_Non_Pragma (component_decl))
7001 Entity_Id gnat_field = Defining_Entity (component_decl);
7003 if (Is_Aliased (gnat_field))
7004 return true;
7006 if (Strict_Alignment (Etype (gnat_field)))
7007 return true;
7010 return false;
7013 /* Return true if TYPE is a type with variable size or a padding type with a
7014 field of variable size or a record that has a field with such a type. */
7016 static bool
7017 type_has_variable_size (tree type)
7019 tree field;
7021 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7022 return true;
7024 if (TYPE_IS_PADDING_P (type)
7025 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7026 return true;
7028 if (!RECORD_OR_UNION_TYPE_P (type))
7029 return false;
7031 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7032 if (type_has_variable_size (TREE_TYPE (field)))
7033 return true;
7035 return false;
7038 /* Return true if FIELD is an artificial field. */
7040 static bool
7041 field_is_artificial (tree field)
7043 /* These fields are generated by the front-end proper. */
7044 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7045 return true;
7047 /* These fields are generated by gigi. */
7048 if (DECL_INTERNAL_P (field))
7049 return true;
7051 return false;
7054 /* Return true if FIELD is a non-artificial aliased field. */
7056 static bool
7057 field_is_aliased (tree field)
7059 if (field_is_artificial (field))
7060 return false;
7062 return DECL_ALIASED_P (field);
7065 /* Return true if FIELD is a non-artificial field with self-referential
7066 size. */
7068 static bool
7069 field_has_self_size (tree field)
7071 if (field_is_artificial (field))
7072 return false;
7074 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7075 return false;
7077 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7080 /* Return true if FIELD is a non-artificial field with variable size. */
7082 static bool
7083 field_has_variable_size (tree field)
7085 if (field_is_artificial (field))
7086 return false;
7088 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7089 return false;
7091 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7094 /* qsort comparer for the bit positions of two record components. */
7096 static int
7097 compare_field_bitpos (const PTR rt1, const PTR rt2)
7099 const_tree const field1 = * (const_tree const *) rt1;
7100 const_tree const field2 = * (const_tree const *) rt2;
7101 const int ret
7102 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7104 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7107 /* Structure holding information for a given variant. */
7108 typedef struct vinfo
7110 /* The record type of the variant. */
7111 tree type;
7113 /* The name of the variant. */
7114 tree name;
7116 /* The qualifier of the variant. */
7117 tree qual;
7119 /* Whether the variant has a rep clause. */
7120 bool has_rep;
7122 /* Whether the variant is packed. */
7123 bool packed;
7125 } vinfo_t;
7127 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
7128 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
7129 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
7130 When called from gnat_to_gnu_entity during the processing of a record type
7131 definition, the GCC node for the parent, if any, will be the single field
7132 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7133 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7134 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7136 PACKED is 1 if this is for a packed record or -1 if this is for a record
7137 with Component_Alignment of Storage_Unit.
7139 DEFINITION is true if we are defining this record type.
7141 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7142 out the record. This means the alignment only serves to force fields to
7143 be bitfields, but not to require the record to be that aligned. This is
7144 used for variants.
7146 ALL_REP is true if a rep clause is present for all the fields.
7148 UNCHECKED_UNION is true if we are building this type for a record with a
7149 Pragma Unchecked_Union.
7151 ARTIFICIAL is true if this is a type that was generated by the compiler.
7153 DEBUG_INFO is true if we need to write debug information about the type.
7155 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7156 mean that its contents may be unused as well, only the container itself.
7158 REORDER is true if we are permitted to reorder components of this type.
7160 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7161 the outer record type down to this variant level. It is nonzero only if
7162 all the fields down to this level have a rep clause and ALL_REP is false.
7164 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7165 with a rep clause is to be added; in this case, that is all that should
7166 be done with such fields and the return value will be false. */
7168 static bool
7169 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7170 tree gnu_field_list, int packed, bool definition,
7171 bool cancel_alignment, bool all_rep,
7172 bool unchecked_union, bool artificial,
7173 bool debug_info, bool maybe_unused, bool reorder,
7174 tree first_free_pos, tree *p_gnu_rep_list)
7176 const bool needs_xv_encodings
7177 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7178 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7179 bool variants_have_rep = all_rep;
7180 bool layout_with_rep = false;
7181 bool has_self_field = false;
7182 bool has_aliased_after_self_field = false;
7183 Node_Id component_decl, variant_part;
7184 tree gnu_field, gnu_next, gnu_last;
7185 tree gnu_variant_part = NULL_TREE;
7186 tree gnu_rep_list = NULL_TREE;
7187 tree gnu_var_list = NULL_TREE;
7188 tree gnu_self_list = NULL_TREE;
7189 tree gnu_zero_list = NULL_TREE;
7191 /* For each component referenced in a component declaration create a GCC
7192 field and add it to the list, skipping pragmas in the GNAT list. */
7193 gnu_last = tree_last (gnu_field_list);
7194 if (Present (Component_Items (gnat_component_list)))
7195 for (component_decl
7196 = First_Non_Pragma (Component_Items (gnat_component_list));
7197 Present (component_decl);
7198 component_decl = Next_Non_Pragma (component_decl))
7200 Entity_Id gnat_field = Defining_Entity (component_decl);
7201 Name_Id gnat_name = Chars (gnat_field);
7203 /* If present, the _Parent field must have been created as the single
7204 field of the record type. Put it before any other fields. */
7205 if (gnat_name == Name_uParent)
7207 gnu_field = TYPE_FIELDS (gnu_record_type);
7208 gnu_field_list = chainon (gnu_field_list, gnu_field);
7210 else
7212 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7213 definition, debug_info);
7215 /* If this is the _Tag field, put it before any other fields. */
7216 if (gnat_name == Name_uTag)
7217 gnu_field_list = chainon (gnu_field_list, gnu_field);
7219 /* If this is the _Controller field, put it before the other
7220 fields except for the _Tag or _Parent field. */
7221 else if (gnat_name == Name_uController && gnu_last)
7223 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7224 DECL_CHAIN (gnu_last) = gnu_field;
7227 /* If this is a regular field, put it after the other fields. */
7228 else
7230 DECL_CHAIN (gnu_field) = gnu_field_list;
7231 gnu_field_list = gnu_field;
7232 if (!gnu_last)
7233 gnu_last = gnu_field;
7235 /* And record information for the final layout. */
7236 if (field_has_self_size (gnu_field))
7237 has_self_field = true;
7238 else if (has_self_field && field_is_aliased (gnu_field))
7239 has_aliased_after_self_field = true;
7243 save_gnu_tree (gnat_field, gnu_field, false);
7246 /* At the end of the component list there may be a variant part. */
7247 variant_part = Variant_Part (gnat_component_list);
7249 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7250 mutually exclusive and should go in the same memory. To do this we need
7251 to treat each variant as a record whose elements are created from the
7252 component list for the variant. So here we create the records from the
7253 lists for the variants and put them all into the QUAL_UNION_TYPE.
7254 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7255 use GNU_RECORD_TYPE if there are no fields so far. */
7256 if (Present (variant_part))
7258 Node_Id gnat_discr = Name (variant_part), variant;
7259 tree gnu_discr = gnat_to_gnu (gnat_discr);
7260 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7261 tree gnu_var_name
7262 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7263 "XVN");
7264 tree gnu_union_type, gnu_union_name;
7265 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7266 bool union_field_needs_strict_alignment = false;
7267 auto_vec <vinfo_t, 16> variant_types;
7268 vinfo_t *gnu_variant;
7269 unsigned int variants_align = 0;
7270 unsigned int i;
7272 gnu_union_name
7273 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7275 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7276 are all in the variant part, to match the layout of C unions. There
7277 is an associated check below. */
7278 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7279 gnu_union_type = gnu_record_type;
7280 else
7282 gnu_union_type
7283 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7285 TYPE_NAME (gnu_union_type) = gnu_union_name;
7286 TYPE_ALIGN (gnu_union_type) = 0;
7287 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7288 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7289 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7292 /* If all the fields down to this level have a rep clause, find out
7293 whether all the fields at this level also have one. If so, then
7294 compute the new first free position to be passed downward. */
7295 this_first_free_pos = first_free_pos;
7296 if (this_first_free_pos)
7298 for (gnu_field = gnu_field_list;
7299 gnu_field;
7300 gnu_field = DECL_CHAIN (gnu_field))
7301 if (DECL_FIELD_OFFSET (gnu_field))
7303 tree pos = bit_position (gnu_field);
7304 if (!tree_int_cst_lt (pos, this_first_free_pos))
7305 this_first_free_pos
7306 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7308 else
7310 this_first_free_pos = NULL_TREE;
7311 break;
7315 /* We build the variants in two passes. The bulk of the work is done in
7316 the first pass, that is to say translating the GNAT nodes, building
7317 the container types and computing the associated properties. However
7318 we cannot finish up the container types during this pass because we
7319 don't know where the variant part will be placed until the end. */
7320 for (variant = First_Non_Pragma (Variants (variant_part));
7321 Present (variant);
7322 variant = Next_Non_Pragma (variant))
7324 tree gnu_variant_type = make_node (RECORD_TYPE);
7325 tree gnu_inner_name, gnu_qual;
7326 bool has_rep;
7327 int field_packed;
7328 vinfo_t vinfo;
7330 Get_Variant_Encoding (variant);
7331 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7332 TYPE_NAME (gnu_variant_type)
7333 = concat_name (gnu_union_name,
7334 IDENTIFIER_POINTER (gnu_inner_name));
7336 /* Set the alignment of the inner type in case we need to make
7337 inner objects into bitfields, but then clear it out so the
7338 record actually gets only the alignment required. */
7339 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7340 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7341 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7342 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7344 /* Similarly, if the outer record has a size specified and all
7345 the fields have a rep clause, we can propagate the size. */
7346 if (all_rep_and_size)
7348 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7349 TYPE_SIZE_UNIT (gnu_variant_type)
7350 = TYPE_SIZE_UNIT (gnu_record_type);
7353 /* Add the fields into the record type for the variant. Note that
7354 we aren't sure to really use it at this point, see below. */
7355 has_rep
7356 = components_to_record (gnu_variant_type, Component_List (variant),
7357 NULL_TREE, packed, definition,
7358 !all_rep_and_size, all_rep,
7359 unchecked_union,
7360 true, needs_xv_encodings, true, reorder,
7361 this_first_free_pos,
7362 all_rep || this_first_free_pos
7363 ? NULL : &gnu_rep_list);
7365 /* Translate the qualifier and annotate the GNAT node. */
7366 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7367 Set_Present_Expr (variant, annotate_value (gnu_qual));
7369 /* Deal with packedness like in gnat_to_gnu_field. */
7370 if (components_need_strict_alignment (Component_List (variant)))
7372 field_packed = 0;
7373 union_field_needs_strict_alignment = true;
7375 else
7376 field_packed
7377 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7379 /* Push this variant onto the stack for the second pass. */
7380 vinfo.type = gnu_variant_type;
7381 vinfo.name = gnu_inner_name;
7382 vinfo.qual = gnu_qual;
7383 vinfo.has_rep = has_rep;
7384 vinfo.packed = field_packed;
7385 variant_types.safe_push (vinfo);
7387 /* Compute the global properties that will determine the placement of
7388 the variant part. */
7389 variants_have_rep |= has_rep;
7390 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7391 variants_align = TYPE_ALIGN (gnu_variant_type);
7394 /* Round up the first free position to the alignment of the variant part
7395 for the variants without rep clause. This will guarantee a consistent
7396 layout independently of the placement of the variant part. */
7397 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7398 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7400 /* In the second pass, the container types are adjusted if necessary and
7401 finished up, then the corresponding fields of the variant part are
7402 built with their qualifier, unless this is an unchecked union. */
7403 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7405 tree gnu_variant_type = gnu_variant->type;
7406 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7408 /* If this is an Unchecked_Union whose fields are all in the variant
7409 part and we have a single field with no representation clause or
7410 placed at offset zero, use the field directly to match the layout
7411 of C unions. */
7412 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7413 && gnu_field_list
7414 && !DECL_CHAIN (gnu_field_list)
7415 && (!DECL_FIELD_OFFSET (gnu_field_list)
7416 || integer_zerop (bit_position (gnu_field_list))))
7418 gnu_field = gnu_field_list;
7419 DECL_CONTEXT (gnu_field) = gnu_record_type;
7421 else
7423 /* Finalize the variant type now. We used to throw away empty
7424 record types but we no longer do that because we need them to
7425 generate complete debug info for the variant; otherwise, the
7426 union type definition will be lacking the fields associated
7427 with these empty variants. */
7428 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7430 /* The variant part will be at offset 0 so we need to ensure
7431 that the fields are laid out starting from the first free
7432 position at this level. */
7433 tree gnu_rep_type = make_node (RECORD_TYPE);
7434 tree gnu_rep_part;
7435 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7436 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7437 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7438 gnu_rep_part
7439 = create_rep_part (gnu_rep_type, gnu_variant_type,
7440 this_first_free_pos);
7441 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7442 gnu_field_list = gnu_rep_part;
7443 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7444 false);
7447 if (debug_info)
7448 rest_of_record_type_compilation (gnu_variant_type);
7449 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7450 true, needs_xv_encodings, gnat_component_list);
7452 gnu_field
7453 = create_field_decl (gnu_variant->name, gnu_variant_type,
7454 gnu_union_type,
7455 all_rep_and_size
7456 ? TYPE_SIZE (gnu_variant_type) : 0,
7457 variants_have_rep ? bitsize_zero_node : 0,
7458 gnu_variant->packed, 0);
7460 DECL_INTERNAL_P (gnu_field) = 1;
7462 if (!unchecked_union)
7463 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7466 DECL_CHAIN (gnu_field) = gnu_variant_list;
7467 gnu_variant_list = gnu_field;
7470 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7471 if (gnu_variant_list)
7473 int union_field_packed;
7475 if (all_rep_and_size)
7477 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7478 TYPE_SIZE_UNIT (gnu_union_type)
7479 = TYPE_SIZE_UNIT (gnu_record_type);
7482 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7483 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7485 /* If GNU_UNION_TYPE is our record type, it means we must have an
7486 Unchecked_Union with no fields. Verify that and, if so, just
7487 return. */
7488 if (gnu_union_type == gnu_record_type)
7490 gcc_assert (unchecked_union
7491 && !gnu_field_list
7492 && !gnu_rep_list);
7493 return variants_have_rep;
7496 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7497 needs_xv_encodings, gnat_component_list);
7499 /* Deal with packedness like in gnat_to_gnu_field. */
7500 if (union_field_needs_strict_alignment)
7501 union_field_packed = 0;
7502 else
7503 union_field_packed
7504 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7506 gnu_variant_part
7507 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7508 all_rep_and_size
7509 ? TYPE_SIZE (gnu_union_type) : 0,
7510 variants_have_rep ? bitsize_zero_node : 0,
7511 union_field_packed, 0);
7513 DECL_INTERNAL_P (gnu_variant_part) = 1;
7517 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7518 permitted to reorder components, self-referential sizes or variable sizes.
7519 If they do, pull them out and put them onto the appropriate list. We have
7520 to do this in a separate pass since we want to handle the discriminants
7521 but can't play with them until we've used them in debugging data above.
7523 Similarly, pull out the fields with zero size and no rep clause, as they
7524 would otherwise modify the layout and thus very likely run afoul of the
7525 Ada semantics, which are different from those of C here.
7527 ??? If we reorder them, debugging information will be wrong but there is
7528 nothing that can be done about this at the moment. */
7529 gnu_last = NULL_TREE;
7531 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7532 do { \
7533 if (gnu_last) \
7534 DECL_CHAIN (gnu_last) = gnu_next; \
7535 else \
7536 gnu_field_list = gnu_next; \
7538 DECL_CHAIN (gnu_field) = (LIST); \
7539 (LIST) = gnu_field; \
7540 } while (0)
7542 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7544 gnu_next = DECL_CHAIN (gnu_field);
7546 if (DECL_FIELD_OFFSET (gnu_field))
7548 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7549 continue;
7552 if ((reorder || has_aliased_after_self_field)
7553 && field_has_self_size (gnu_field))
7555 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7556 continue;
7559 if (reorder && field_has_variable_size (gnu_field))
7561 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7562 continue;
7565 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7567 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7568 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7569 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7570 if (field_is_aliased (gnu_field))
7571 TYPE_ALIGN (gnu_record_type)
7572 = MAX (TYPE_ALIGN (gnu_record_type),
7573 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7574 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7575 continue;
7578 gnu_last = gnu_field;
7581 #undef MOVE_FROM_FIELD_LIST_TO
7583 gnu_field_list = nreverse (gnu_field_list);
7585 /* If permitted, we reorder the fields as follows:
7587 1) all fixed length fields,
7588 2) all fields whose length doesn't depend on discriminants,
7589 3) all fields whose length depends on discriminants,
7590 4) the variant part,
7592 within the record and within each variant recursively. */
7593 if (reorder)
7594 gnu_field_list
7595 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7597 /* Otherwise, if there is an aliased field placed after a field whose length
7598 depends on discriminants, we put all the fields of the latter sort, last.
7599 We need to do this in case an object of this record type is mutable. */
7600 else if (has_aliased_after_self_field)
7601 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7603 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7604 in our REP list to the previous level because this level needs them in
7605 order to do a correct layout, i.e. avoid having overlapping fields. */
7606 if (p_gnu_rep_list && gnu_rep_list)
7607 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7609 /* If only one field has a rep clause and it starts at 0, put back the field
7610 at the head of the regular field list. This will avoid creating a useless
7611 REP part below and deal with the annoying case of an extension of a record
7612 with variable size and rep clause, for which the _Parent field is forced
7613 at offset 0 and has variable size, which we do not support below. */
7614 else if (gnu_rep_list
7615 && !DECL_CHAIN (gnu_rep_list)
7616 && !variants_have_rep
7617 && first_free_pos
7618 && integer_zerop (first_free_pos)
7619 && integer_zerop (bit_position (gnu_rep_list)))
7621 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7622 gnu_field_list = gnu_rep_list;
7623 gnu_rep_list = NULL_TREE;
7626 /* Otherwise, sort the fields by bit position and put them into their own
7627 record, before the others, if we also have fields without rep clause. */
7628 else if (gnu_rep_list)
7630 tree gnu_rep_type, gnu_rep_part;
7631 int i, len = list_length (gnu_rep_list);
7632 tree *gnu_arr = XALLOCAVEC (tree, len);
7634 /* If all the fields have a rep clause, we can do a flat layout. */
7635 layout_with_rep = !gnu_field_list
7636 && (!gnu_variant_part || variants_have_rep);
7637 gnu_rep_type
7638 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7640 for (gnu_field = gnu_rep_list, i = 0;
7641 gnu_field;
7642 gnu_field = DECL_CHAIN (gnu_field), i++)
7643 gnu_arr[i] = gnu_field;
7645 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7647 /* Put the fields in the list in order of increasing position, which
7648 means we start from the end. */
7649 gnu_rep_list = NULL_TREE;
7650 for (i = len - 1; i >= 0; i--)
7652 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7653 gnu_rep_list = gnu_arr[i];
7654 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7657 if (layout_with_rep)
7658 gnu_field_list = gnu_rep_list;
7659 else
7661 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7662 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7663 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7665 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7666 without rep clause are laid out starting from this position.
7667 Therefore, we force it as a minimal size on the REP part. */
7668 gnu_rep_part
7669 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7671 /* Chain the REP part at the beginning of the field list. */
7672 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7673 gnu_field_list = gnu_rep_part;
7677 /* Chain the variant part at the end of the field list. */
7678 if (gnu_variant_part)
7679 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7681 if (cancel_alignment)
7682 TYPE_ALIGN (gnu_record_type) = 0;
7684 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7686 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7687 debug_info && !maybe_unused);
7689 /* Chain the fields with zero size at the beginning of the field list. */
7690 if (gnu_zero_list)
7691 TYPE_FIELDS (gnu_record_type)
7692 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7694 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7697 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7698 placed into an Esize, Component_Bit_Offset, or Component_Size value
7699 in the GNAT tree. */
7701 static Uint
7702 annotate_value (tree gnu_size)
7704 TCode tcode;
7705 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7706 struct tree_int_map in;
7707 int i;
7709 /* See if we've already saved the value for this node. */
7710 if (EXPR_P (gnu_size))
7712 struct tree_int_map *e;
7714 in.base.from = gnu_size;
7715 e = annotate_value_cache->find (&in);
7717 if (e)
7718 return (Node_Ref_Or_Val) e->to;
7720 else
7721 in.base.from = NULL_TREE;
7723 /* If we do not return inside this switch, TCODE will be set to the
7724 code to use for a Create_Node operand and LEN (set above) will be
7725 the number of recursive calls for us to make. */
7727 switch (TREE_CODE (gnu_size))
7729 case INTEGER_CST:
7730 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7732 case COMPONENT_REF:
7733 /* The only case we handle here is a simple discriminant reference. */
7734 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7736 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7738 /* Climb up the chain of successive extensions, if any. */
7739 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7740 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7741 == parent_name_id)
7742 gnu_size = TREE_OPERAND (gnu_size, 0);
7744 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7745 return
7746 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7749 return No_Uint;
7751 CASE_CONVERT: case NON_LVALUE_EXPR:
7752 return annotate_value (TREE_OPERAND (gnu_size, 0));
7754 /* Now just list the operations we handle. */
7755 case COND_EXPR: tcode = Cond_Expr; break;
7756 case PLUS_EXPR: tcode = Plus_Expr; break;
7757 case MINUS_EXPR: tcode = Minus_Expr; break;
7758 case MULT_EXPR: tcode = Mult_Expr; break;
7759 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7760 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7761 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7762 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7763 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7764 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7765 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7766 case NEGATE_EXPR: tcode = Negate_Expr; break;
7767 case MIN_EXPR: tcode = Min_Expr; break;
7768 case MAX_EXPR: tcode = Max_Expr; break;
7769 case ABS_EXPR: tcode = Abs_Expr; break;
7770 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7771 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7772 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7773 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7774 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7775 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7776 case LT_EXPR: tcode = Lt_Expr; break;
7777 case LE_EXPR: tcode = Le_Expr; break;
7778 case GT_EXPR: tcode = Gt_Expr; break;
7779 case GE_EXPR: tcode = Ge_Expr; break;
7780 case EQ_EXPR: tcode = Eq_Expr; break;
7781 case NE_EXPR: tcode = Ne_Expr; break;
7783 case BIT_AND_EXPR:
7784 tcode = Bit_And_Expr;
7785 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7786 Such values appear in expressions with aligning patterns. Note that,
7787 since sizetype is unsigned, we have to jump through some hoops. */
7788 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7790 tree op1 = TREE_OPERAND (gnu_size, 1);
7791 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7792 if (wi::neg_p (signed_op1))
7794 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7795 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7798 break;
7800 case CALL_EXPR:
7801 /* In regular mode, inline back only if symbolic annotation is requested
7802 in order to avoid memory explosion on big discriminated record types.
7803 But not in ASIS mode, as symbolic annotation is required for DDA. */
7804 if (List_Representation_Info == 3 || type_annotate_only)
7806 tree t = maybe_inline_call_in_expr (gnu_size);
7807 if (t)
7808 return annotate_value (t);
7810 else
7811 return Uint_Minus_1;
7813 /* Fall through... */
7815 default:
7816 return No_Uint;
7819 /* Now get each of the operands that's relevant for this code. If any
7820 cannot be expressed as a repinfo node, say we can't. */
7821 for (i = 0; i < 3; i++)
7822 ops[i] = No_Uint;
7824 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7826 if (i == 1 && pre_op1 != No_Uint)
7827 ops[i] = pre_op1;
7828 else
7829 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7830 if (ops[i] == No_Uint)
7831 return No_Uint;
7834 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7836 /* Save the result in the cache. */
7837 if (in.base.from)
7839 struct tree_int_map **h;
7840 /* We can't assume the hash table data hasn't moved since the initial
7841 look up, so we have to search again. Allocating and inserting an
7842 entry at that point would be an alternative, but then we'd better
7843 discard the entry if we decided not to cache it. */
7844 h = annotate_value_cache->find_slot (&in, INSERT);
7845 gcc_assert (!*h);
7846 *h = ggc_alloc<tree_int_map> ();
7847 (*h)->base.from = gnu_size;
7848 (*h)->to = ret;
7851 return ret;
7854 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7855 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7856 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7857 BY_REF is true if the object is used by reference. */
7859 void
7860 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7862 if (by_ref)
7864 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7865 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7866 else
7867 gnu_type = TREE_TYPE (gnu_type);
7870 if (Unknown_Esize (gnat_entity))
7872 if (TREE_CODE (gnu_type) == RECORD_TYPE
7873 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7874 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7875 else if (!size)
7876 size = TYPE_SIZE (gnu_type);
7878 if (size)
7879 Set_Esize (gnat_entity, annotate_value (size));
7882 if (Unknown_Alignment (gnat_entity))
7883 Set_Alignment (gnat_entity,
7884 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7887 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7888 Return NULL_TREE if there is no such element in the list. */
7890 static tree
7891 purpose_member_field (const_tree elem, tree list)
7893 while (list)
7895 tree field = TREE_PURPOSE (list);
7896 if (SAME_FIELD_P (field, elem))
7897 return list;
7898 list = TREE_CHAIN (list);
7900 return NULL_TREE;
7903 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7904 set Component_Bit_Offset and Esize of the components to the position and
7905 size used by Gigi. */
7907 static void
7908 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7910 Entity_Id gnat_field;
7911 tree gnu_list;
7913 /* We operate by first making a list of all fields and their position (we
7914 can get the size easily) and then update all the sizes in the tree. */
7915 gnu_list
7916 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7917 BIGGEST_ALIGNMENT, NULL_TREE);
7919 for (gnat_field = First_Entity (gnat_entity);
7920 Present (gnat_field);
7921 gnat_field = Next_Entity (gnat_field))
7922 if (Ekind (gnat_field) == E_Component
7923 || (Ekind (gnat_field) == E_Discriminant
7924 && !Is_Unchecked_Union (Scope (gnat_field))))
7926 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7927 gnu_list);
7928 if (t)
7930 tree parent_offset;
7932 /* If we are just annotating types and the type is tagged, the tag
7933 and the parent components are not generated by the front-end so
7934 we need to add the appropriate offset to each component without
7935 representation clause. */
7936 if (type_annotate_only
7937 && Is_Tagged_Type (gnat_entity)
7938 && No (Component_Clause (gnat_field)))
7940 /* For a component appearing in the current extension, the
7941 offset is the size of the parent. */
7942 if (Is_Derived_Type (gnat_entity)
7943 && Original_Record_Component (gnat_field) == gnat_field)
7944 parent_offset
7945 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7946 bitsizetype);
7947 else
7948 parent_offset = bitsize_int (POINTER_SIZE);
7950 if (TYPE_FIELDS (gnu_type))
7951 parent_offset
7952 = round_up (parent_offset,
7953 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7955 else
7956 parent_offset = bitsize_zero_node;
7958 Set_Component_Bit_Offset
7959 (gnat_field,
7960 annotate_value
7961 (size_binop (PLUS_EXPR,
7962 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7963 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7964 parent_offset)));
7966 Set_Esize (gnat_field,
7967 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7969 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7971 /* If there is no entry, this is an inherited component whose
7972 position is the same as in the parent type. */
7973 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
7975 /* If we are just annotating types, discriminants renaming those of
7976 the parent have no entry so deal with them specifically. */
7977 if (type_annotate_only
7978 && gnat_orig_field == gnat_field
7979 && Ekind (gnat_field) == E_Discriminant)
7980 gnat_orig_field = Corresponding_Discriminant (gnat_field);
7982 Set_Component_Bit_Offset (gnat_field,
7983 Component_Bit_Offset (gnat_orig_field));
7985 Set_Esize (gnat_field, Esize (gnat_orig_field));
7990 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7991 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7992 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7993 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7994 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7995 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7996 pre-existing list to be chained to the newly created entries. */
7998 static tree
7999 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8000 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8002 tree gnu_field;
8004 for (gnu_field = TYPE_FIELDS (gnu_type);
8005 gnu_field;
8006 gnu_field = DECL_CHAIN (gnu_field))
8008 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8009 DECL_FIELD_BIT_OFFSET (gnu_field));
8010 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8011 DECL_FIELD_OFFSET (gnu_field));
8012 unsigned int our_offset_align
8013 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8014 tree v = make_tree_vec (3);
8016 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8017 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8018 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8019 gnu_list = tree_cons (gnu_field, v, gnu_list);
8021 /* Recurse on internal fields, flattening the nested fields except for
8022 those in the variant part, if requested. */
8023 if (DECL_INTERNAL_P (gnu_field))
8025 tree gnu_field_type = TREE_TYPE (gnu_field);
8026 if (do_not_flatten_variant
8027 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8028 gnu_list
8029 = build_position_list (gnu_field_type, do_not_flatten_variant,
8030 size_zero_node, bitsize_zero_node,
8031 BIGGEST_ALIGNMENT, gnu_list);
8032 else
8033 gnu_list
8034 = build_position_list (gnu_field_type, do_not_flatten_variant,
8035 gnu_our_offset, gnu_our_bitpos,
8036 our_offset_align, gnu_list);
8040 return gnu_list;
8043 /* Return a list describing the substitutions needed to reflect the
8044 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8045 be in any order. The values in an element of the list are in the form
8046 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8047 a definition of GNAT_SUBTYPE. */
8049 static vec<subst_pair>
8050 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8052 vec<subst_pair> gnu_list = vNULL;
8053 Entity_Id gnat_discrim;
8054 Node_Id gnat_constr;
8056 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8057 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8058 Present (gnat_discrim);
8059 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8060 gnat_constr = Next_Elmt (gnat_constr))
8061 /* Ignore access discriminants. */
8062 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8064 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8065 tree replacement = convert (TREE_TYPE (gnu_field),
8066 elaborate_expression
8067 (Node (gnat_constr), gnat_subtype,
8068 get_entity_char (gnat_discrim),
8069 definition, true, false));
8070 subst_pair s = {gnu_field, replacement};
8071 gnu_list.safe_push (s);
8074 return gnu_list;
8077 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8078 variants of QUAL_UNION_TYPE that are still relevant after applying
8079 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8080 list to be prepended to the newly created entries. */
8082 static vec<variant_desc>
8083 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8084 vec<variant_desc> gnu_list)
8086 tree gnu_field;
8088 for (gnu_field = TYPE_FIELDS (qual_union_type);
8089 gnu_field;
8090 gnu_field = DECL_CHAIN (gnu_field))
8092 tree qual = DECL_QUALIFIER (gnu_field);
8093 unsigned int i;
8094 subst_pair *s;
8096 FOR_EACH_VEC_ELT (subst_list, i, s)
8097 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8099 /* If the new qualifier is not unconditionally false, its variant may
8100 still be accessed. */
8101 if (!integer_zerop (qual))
8103 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8104 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
8106 gnu_list.safe_push (v);
8108 /* Recurse on the variant subpart of the variant, if any. */
8109 variant_subpart = get_variant_part (variant_type);
8110 if (variant_subpart)
8111 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8112 subst_list, gnu_list);
8114 /* If the new qualifier is unconditionally true, the subsequent
8115 variants cannot be accessed. */
8116 if (integer_onep (qual))
8117 break;
8121 return gnu_list;
8124 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8125 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8126 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8127 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8128 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8129 true if we are being called to process the Component_Size of GNAT_OBJECT;
8130 this is used only for error messages. ZERO_OK is true if a size of zero
8131 is permitted; if ZERO_OK is false, it means that a size of zero should be
8132 treated as an unspecified size. */
8134 static tree
8135 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8136 enum tree_code kind, bool component_p, bool zero_ok)
8138 Node_Id gnat_error_node;
8139 tree type_size, size;
8141 /* Return 0 if no size was specified. */
8142 if (uint_size == No_Uint)
8143 return NULL_TREE;
8145 /* Ignore a negative size since that corresponds to our back-annotation. */
8146 if (UI_Lt (uint_size, Uint_0))
8147 return NULL_TREE;
8149 /* Find the node to use for error messages. */
8150 if ((Ekind (gnat_object) == E_Component
8151 || Ekind (gnat_object) == E_Discriminant)
8152 && Present (Component_Clause (gnat_object)))
8153 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8154 else if (Present (Size_Clause (gnat_object)))
8155 gnat_error_node = Expression (Size_Clause (gnat_object));
8156 else
8157 gnat_error_node = gnat_object;
8159 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8160 but cannot be represented in bitsizetype. */
8161 size = UI_To_gnu (uint_size, bitsizetype);
8162 if (TREE_OVERFLOW (size))
8164 if (component_p)
8165 post_error_ne ("component size for& is too large", gnat_error_node,
8166 gnat_object);
8167 else
8168 post_error_ne ("size for& is too large", gnat_error_node,
8169 gnat_object);
8170 return NULL_TREE;
8173 /* Ignore a zero size if it is not permitted. */
8174 if (!zero_ok && integer_zerop (size))
8175 return NULL_TREE;
8177 /* The size of objects is always a multiple of a byte. */
8178 if (kind == VAR_DECL
8179 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8181 if (component_p)
8182 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8183 gnat_error_node, gnat_object);
8184 else
8185 post_error_ne ("size for& is not a multiple of Storage_Unit",
8186 gnat_error_node, gnat_object);
8187 return NULL_TREE;
8190 /* If this is an integral type or a packed array type, the front-end has
8191 already verified the size, so we need not do it here (which would mean
8192 checking against the bounds). However, if this is an aliased object,
8193 it may not be smaller than the type of the object. */
8194 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8195 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8196 return size;
8198 /* If the object is a record that contains a template, add the size of the
8199 template to the specified size. */
8200 if (TREE_CODE (gnu_type) == RECORD_TYPE
8201 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8202 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8204 if (kind == VAR_DECL
8205 /* If a type needs strict alignment, a component of this type in
8206 a packed record cannot be packed and thus uses the type size. */
8207 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8208 type_size = TYPE_SIZE (gnu_type);
8209 else
8210 type_size = rm_size (gnu_type);
8212 /* Modify the size of a discriminated type to be the maximum size. */
8213 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8214 type_size = max_size (type_size, true);
8216 /* If this is an access type or a fat pointer, the minimum size is that given
8217 by the smallest integral mode that's valid for pointers. */
8218 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8220 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8221 while (!targetm.valid_pointer_mode (p_mode))
8222 p_mode = GET_MODE_WIDER_MODE (p_mode);
8223 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8226 /* Issue an error either if the default size of the object isn't a constant
8227 or if the new size is smaller than it. */
8228 if (TREE_CODE (type_size) != INTEGER_CST
8229 || TREE_OVERFLOW (type_size)
8230 || tree_int_cst_lt (size, type_size))
8232 if (component_p)
8233 post_error_ne_tree
8234 ("component size for& too small{, minimum allowed is ^}",
8235 gnat_error_node, gnat_object, type_size);
8236 else
8237 post_error_ne_tree
8238 ("size for& too small{, minimum allowed is ^}",
8239 gnat_error_node, gnat_object, type_size);
8240 return NULL_TREE;
8243 return size;
8246 /* Similarly, but both validate and process a value of RM size. This routine
8247 is only called for types. */
8249 static void
8250 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8252 Node_Id gnat_attr_node;
8253 tree old_size, size;
8255 /* Do nothing if no size was specified. */
8256 if (uint_size == No_Uint)
8257 return;
8259 /* Ignore a negative size since that corresponds to our back-annotation. */
8260 if (UI_Lt (uint_size, Uint_0))
8261 return;
8263 /* Only issue an error if a Value_Size clause was explicitly given.
8264 Otherwise, we'd be duplicating an error on the Size clause. */
8265 gnat_attr_node
8266 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8268 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8269 but cannot be represented in bitsizetype. */
8270 size = UI_To_gnu (uint_size, bitsizetype);
8271 if (TREE_OVERFLOW (size))
8273 if (Present (gnat_attr_node))
8274 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8275 gnat_entity);
8276 return;
8279 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8280 exists, or this is an integer type, in which case the front-end will
8281 have always set it. */
8282 if (No (gnat_attr_node)
8283 && integer_zerop (size)
8284 && !Has_Size_Clause (gnat_entity)
8285 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8286 return;
8288 old_size = rm_size (gnu_type);
8290 /* If the old size is self-referential, get the maximum size. */
8291 if (CONTAINS_PLACEHOLDER_P (old_size))
8292 old_size = max_size (old_size, true);
8294 /* Issue an error either if the old size of the object isn't a constant or
8295 if the new size is smaller than it. The front-end has already verified
8296 this for scalar and packed array types. */
8297 if (TREE_CODE (old_size) != INTEGER_CST
8298 || TREE_OVERFLOW (old_size)
8299 || (AGGREGATE_TYPE_P (gnu_type)
8300 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8301 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8302 && !(TYPE_IS_PADDING_P (gnu_type)
8303 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8304 && TYPE_PACKED_ARRAY_TYPE_P
8305 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8306 && tree_int_cst_lt (size, old_size)))
8308 if (Present (gnat_attr_node))
8309 post_error_ne_tree
8310 ("Value_Size for& too small{, minimum allowed is ^}",
8311 gnat_attr_node, gnat_entity, old_size);
8312 return;
8315 /* Otherwise, set the RM size proper for integral types... */
8316 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8317 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8318 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8319 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8320 SET_TYPE_RM_SIZE (gnu_type, size);
8322 /* ...or the Ada size for record and union types. */
8323 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8324 && !TYPE_FAT_POINTER_P (gnu_type))
8325 SET_TYPE_ADA_SIZE (gnu_type, size);
8328 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8329 a type or object whose present alignment is ALIGN. If this alignment is
8330 valid, return it. Otherwise, give an error and return ALIGN. */
8332 static unsigned int
8333 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8335 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8336 unsigned int new_align;
8337 Node_Id gnat_error_node;
8339 /* Don't worry about checking alignment if alignment was not specified
8340 by the source program and we already posted an error for this entity. */
8341 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8342 return align;
8344 /* Post the error on the alignment clause if any. Note, for the implicit
8345 base type of an array type, the alignment clause is on the first
8346 subtype. */
8347 if (Present (Alignment_Clause (gnat_entity)))
8348 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8350 else if (Is_Itype (gnat_entity)
8351 && Is_Array_Type (gnat_entity)
8352 && Etype (gnat_entity) == gnat_entity
8353 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8354 gnat_error_node =
8355 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8357 else
8358 gnat_error_node = gnat_entity;
8360 /* Within GCC, an alignment is an integer, so we must make sure a value is
8361 specified that fits in that range. Also, there is an upper bound to
8362 alignments we can support/allow. */
8363 if (!UI_Is_In_Int_Range (alignment)
8364 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8365 post_error_ne_num ("largest supported alignment for& is ^",
8366 gnat_error_node, gnat_entity, max_allowed_alignment);
8367 else if (!(Present (Alignment_Clause (gnat_entity))
8368 && From_At_Mod (Alignment_Clause (gnat_entity)))
8369 && new_align * BITS_PER_UNIT < align)
8371 unsigned int double_align;
8372 bool is_capped_double, align_clause;
8374 /* If the default alignment of "double" or larger scalar types is
8375 specifically capped and the new alignment is above the cap, do
8376 not post an error and change the alignment only if there is an
8377 alignment clause; this makes it possible to have the associated
8378 GCC type overaligned by default for performance reasons. */
8379 if ((double_align = double_float_alignment) > 0)
8381 Entity_Id gnat_type
8382 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8383 is_capped_double
8384 = is_double_float_or_array (gnat_type, &align_clause);
8386 else if ((double_align = double_scalar_alignment) > 0)
8388 Entity_Id gnat_type
8389 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8390 is_capped_double
8391 = is_double_scalar_or_array (gnat_type, &align_clause);
8393 else
8394 is_capped_double = align_clause = false;
8396 if (is_capped_double && new_align >= double_align)
8398 if (align_clause)
8399 align = new_align * BITS_PER_UNIT;
8401 else
8403 if (is_capped_double)
8404 align = double_align * BITS_PER_UNIT;
8406 post_error_ne_num ("alignment for& must be at least ^",
8407 gnat_error_node, gnat_entity,
8408 align / BITS_PER_UNIT);
8411 else
8413 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8414 if (new_align > align)
8415 align = new_align;
8418 return align;
8421 /* Verify that TYPE is something we can implement atomically. If not, issue
8422 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8423 process a component type. */
8425 static void
8426 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8428 Node_Id gnat_error_point = gnat_entity;
8429 Node_Id gnat_node;
8430 machine_mode mode;
8431 enum mode_class mclass;
8432 unsigned int align;
8433 tree size;
8435 /* If this is an anonymous base type, nothing to check, the error will be
8436 reported on the source type if need be. */
8437 if (!Comes_From_Source (gnat_entity))
8438 return;
8440 mode = TYPE_MODE (type);
8441 mclass = GET_MODE_CLASS (mode);
8442 align = TYPE_ALIGN (type);
8443 size = TYPE_SIZE (type);
8445 /* Consider all aligned floating-point types atomic and any aligned types
8446 that are represented by integers no wider than a machine word. */
8447 if ((mclass == MODE_FLOAT
8448 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8449 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8450 && align >= GET_MODE_ALIGNMENT (mode))
8451 return;
8453 /* For the moment, also allow anything that has an alignment equal to its
8454 size and which is smaller than a word. */
8455 if (size
8456 && TREE_CODE (size) == INTEGER_CST
8457 && compare_tree_int (size, align) == 0
8458 && align <= BITS_PER_WORD)
8459 return;
8461 for (gnat_node = First_Rep_Item (gnat_entity);
8462 Present (gnat_node);
8463 gnat_node = Next_Rep_Item (gnat_node))
8464 if (Nkind (gnat_node) == N_Pragma)
8466 unsigned char pragma_id
8467 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8469 if ((pragma_id == Pragma_Atomic && !component_p)
8470 || (pragma_id == Pragma_Atomic_Components && component_p))
8472 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8473 break;
8477 if (component_p)
8478 post_error_ne ("atomic access to component of & cannot be guaranteed",
8479 gnat_error_point, gnat_entity);
8480 else if (Is_Volatile_Full_Access (gnat_entity))
8481 post_error_ne ("volatile full access to & cannot be guaranteed",
8482 gnat_error_point, gnat_entity);
8483 else
8484 post_error_ne ("atomic access to & cannot be guaranteed",
8485 gnat_error_point, gnat_entity);
8489 /* Helper for the intrin compatibility checks family. Evaluate whether
8490 two types are definitely incompatible. */
8492 static bool
8493 intrin_types_incompatible_p (tree t1, tree t2)
8495 enum tree_code code;
8497 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8498 return false;
8500 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8501 return true;
8503 if (TREE_CODE (t1) != TREE_CODE (t2))
8504 return true;
8506 code = TREE_CODE (t1);
8508 switch (code)
8510 case INTEGER_TYPE:
8511 case REAL_TYPE:
8512 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8514 case POINTER_TYPE:
8515 case REFERENCE_TYPE:
8516 /* Assume designated types are ok. We'd need to account for char * and
8517 void * variants to do better, which could rapidly get messy and isn't
8518 clearly worth the effort. */
8519 return false;
8521 default:
8522 break;
8525 return false;
8528 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8529 on the Ada/builtin argument lists for the INB binding. */
8531 static bool
8532 intrin_arglists_compatible_p (intrin_binding_t * inb)
8534 function_args_iterator ada_iter, btin_iter;
8536 function_args_iter_init (&ada_iter, inb->ada_fntype);
8537 function_args_iter_init (&btin_iter, inb->btin_fntype);
8539 /* Sequence position of the last argument we checked. */
8540 int argpos = 0;
8542 while (true)
8544 tree ada_type = function_args_iter_cond (&ada_iter);
8545 tree btin_type = function_args_iter_cond (&btin_iter);
8547 /* If we've exhausted both lists simultaneously, we're done. */
8548 if (!ada_type && !btin_type)
8549 break;
8551 /* If one list is shorter than the other, they fail to match. */
8552 if (!ada_type || !btin_type)
8553 return false;
8555 /* If we're done with the Ada args and not with the internal builtin
8556 args, or the other way around, complain. */
8557 if (ada_type == void_type_node
8558 && btin_type != void_type_node)
8560 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8561 return false;
8564 if (btin_type == void_type_node
8565 && ada_type != void_type_node)
8567 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8568 inb->gnat_entity, inb->gnat_entity, argpos);
8569 return false;
8572 /* Otherwise, check that types match for the current argument. */
8573 argpos ++;
8574 if (intrin_types_incompatible_p (ada_type, btin_type))
8576 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8577 inb->gnat_entity, inb->gnat_entity, argpos);
8578 return false;
8582 function_args_iter_next (&ada_iter);
8583 function_args_iter_next (&btin_iter);
8586 return true;
8589 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8590 on the Ada/builtin return values for the INB binding. */
8592 static bool
8593 intrin_return_compatible_p (intrin_binding_t * inb)
8595 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8596 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8598 /* Accept function imported as procedure, common and convenient. */
8599 if (VOID_TYPE_P (ada_return_type)
8600 && !VOID_TYPE_P (btin_return_type))
8601 return true;
8603 /* If return type is Address (integer type), map it to void *. */
8604 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8605 ada_return_type = ptr_type_node;
8607 /* Check return types compatibility otherwise. Note that this
8608 handles void/void as well. */
8609 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8611 post_error ("?intrinsic binding type mismatch on return value!",
8612 inb->gnat_entity);
8613 return false;
8616 return true;
8619 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8620 compatible. Issue relevant warnings when they are not.
8622 This is intended as a light check to diagnose the most obvious cases, not
8623 as a full fledged type compatibility predicate. It is the programmer's
8624 responsibility to ensure correctness of the Ada declarations in Imports,
8625 especially when binding straight to a compiler internal. */
8627 static bool
8628 intrin_profiles_compatible_p (intrin_binding_t * inb)
8630 /* Check compatibility on return values and argument lists, each responsible
8631 for posting warnings as appropriate. Ensure use of the proper sloc for
8632 this purpose. */
8634 bool arglists_compatible_p, return_compatible_p;
8635 location_t saved_location = input_location;
8637 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8639 return_compatible_p = intrin_return_compatible_p (inb);
8640 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8642 input_location = saved_location;
8644 return return_compatible_p && arglists_compatible_p;
8647 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8648 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8649 specified size for this field. POS_LIST is a position list describing
8650 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8651 to this layout. */
8653 static tree
8654 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8655 tree size, tree pos_list,
8656 vec<subst_pair> subst_list)
8658 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8659 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8660 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8661 tree new_pos, new_field;
8662 unsigned int i;
8663 subst_pair *s;
8665 if (CONTAINS_PLACEHOLDER_P (pos))
8666 FOR_EACH_VEC_ELT (subst_list, i, s)
8667 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8669 /* If the position is now a constant, we can set it as the position of the
8670 field when we make it. Otherwise, we need to deal with it specially. */
8671 if (TREE_CONSTANT (pos))
8672 new_pos = bit_from_pos (pos, bitpos);
8673 else
8674 new_pos = NULL_TREE;
8676 new_field
8677 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8678 size, new_pos, DECL_PACKED (old_field),
8679 !DECL_NONADDRESSABLE_P (old_field));
8681 if (!new_pos)
8683 normalize_offset (&pos, &bitpos, offset_align);
8684 /* Finalize the position. */
8685 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8686 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8687 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8688 DECL_SIZE (new_field) = size;
8689 DECL_SIZE_UNIT (new_field)
8690 = convert (sizetype,
8691 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8692 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8695 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8696 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8697 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8698 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8700 return new_field;
8703 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8704 it is the minimal size the REP_PART must have. */
8706 static tree
8707 create_rep_part (tree rep_type, tree record_type, tree min_size)
8709 tree field;
8711 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8712 min_size = NULL_TREE;
8714 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8715 min_size, NULL_TREE, 0, 1);
8716 DECL_INTERNAL_P (field) = 1;
8718 return field;
8721 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8723 static tree
8724 get_rep_part (tree record_type)
8726 tree field = TYPE_FIELDS (record_type);
8728 /* The REP part is the first field, internal, another record, and its name
8729 starts with an 'R'. */
8730 if (field
8731 && DECL_INTERNAL_P (field)
8732 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8733 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8734 return field;
8736 return NULL_TREE;
8739 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8741 tree
8742 get_variant_part (tree record_type)
8744 tree field;
8746 /* The variant part is the only internal field that is a qualified union. */
8747 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8748 if (DECL_INTERNAL_P (field)
8749 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8750 return field;
8752 return NULL_TREE;
8755 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8756 the list of variants to be used and RECORD_TYPE is the type of the parent.
8757 POS_LIST is a position list describing the layout of fields present in
8758 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8759 layout. */
8761 static tree
8762 create_variant_part_from (tree old_variant_part,
8763 vec<variant_desc> variant_list,
8764 tree record_type, tree pos_list,
8765 vec<subst_pair> subst_list)
8767 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8768 tree old_union_type = TREE_TYPE (old_variant_part);
8769 tree new_union_type, new_variant_part;
8770 tree union_field_list = NULL_TREE;
8771 variant_desc *v;
8772 unsigned int i;
8774 /* First create the type of the variant part from that of the old one. */
8775 new_union_type = make_node (QUAL_UNION_TYPE);
8776 TYPE_NAME (new_union_type)
8777 = concat_name (TYPE_NAME (record_type),
8778 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8780 /* If the position of the variant part is constant, subtract it from the
8781 size of the type of the parent to get the new size. This manual CSE
8782 reduces the code size when not optimizing. */
8783 if (TREE_CODE (offset) == INTEGER_CST)
8785 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8786 tree first_bit = bit_from_pos (offset, bitpos);
8787 TYPE_SIZE (new_union_type)
8788 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8789 TYPE_SIZE_UNIT (new_union_type)
8790 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8791 byte_from_pos (offset, bitpos));
8792 SET_TYPE_ADA_SIZE (new_union_type,
8793 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8794 first_bit));
8795 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8796 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8798 else
8799 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8801 /* Now finish up the new variants and populate the union type. */
8802 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8804 tree old_field = v->field, new_field;
8805 tree old_variant, old_variant_subpart, new_variant, field_list;
8807 /* Skip variants that don't belong to this nesting level. */
8808 if (DECL_CONTEXT (old_field) != old_union_type)
8809 continue;
8811 /* Retrieve the list of fields already added to the new variant. */
8812 new_variant = v->new_type;
8813 field_list = TYPE_FIELDS (new_variant);
8815 /* If the old variant had a variant subpart, we need to create a new
8816 variant subpart and add it to the field list. */
8817 old_variant = v->type;
8818 old_variant_subpart = get_variant_part (old_variant);
8819 if (old_variant_subpart)
8821 tree new_variant_subpart
8822 = create_variant_part_from (old_variant_subpart, variant_list,
8823 new_variant, pos_list, subst_list);
8824 DECL_CHAIN (new_variant_subpart) = field_list;
8825 field_list = new_variant_subpart;
8828 /* Finish up the new variant and create the field. No need for debug
8829 info thanks to the XVS type. */
8830 finish_record_type (new_variant, nreverse (field_list), 2, false);
8831 compute_record_mode (new_variant);
8832 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8833 Empty);
8835 new_field
8836 = create_field_decl_from (old_field, new_variant, new_union_type,
8837 TYPE_SIZE (new_variant),
8838 pos_list, subst_list);
8839 DECL_QUALIFIER (new_field) = v->qual;
8840 DECL_INTERNAL_P (new_field) = 1;
8841 DECL_CHAIN (new_field) = union_field_list;
8842 union_field_list = new_field;
8845 /* Finish up the union type and create the variant part. No need for debug
8846 info thanks to the XVS type. Note that we don't reverse the field list
8847 because VARIANT_LIST has been traversed in reverse order. */
8848 finish_record_type (new_union_type, union_field_list, 2, false);
8849 compute_record_mode (new_union_type);
8850 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8851 Empty);
8853 new_variant_part
8854 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8855 TYPE_SIZE (new_union_type),
8856 pos_list, subst_list);
8857 DECL_INTERNAL_P (new_variant_part) = 1;
8859 /* With multiple discriminants it is possible for an inner variant to be
8860 statically selected while outer ones are not; in this case, the list
8861 of fields of the inner variant is not flattened and we end up with a
8862 qualified union with a single member. Drop the useless container. */
8863 if (!DECL_CHAIN (union_field_list))
8865 DECL_CONTEXT (union_field_list) = record_type;
8866 DECL_FIELD_OFFSET (union_field_list)
8867 = DECL_FIELD_OFFSET (new_variant_part);
8868 DECL_FIELD_BIT_OFFSET (union_field_list)
8869 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8870 SET_DECL_OFFSET_ALIGN (union_field_list,
8871 DECL_OFFSET_ALIGN (new_variant_part));
8872 new_variant_part = union_field_list;
8875 return new_variant_part;
8878 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8879 which are both RECORD_TYPE, after applying the substitutions described
8880 in SUBST_LIST. */
8882 static void
8883 copy_and_substitute_in_size (tree new_type, tree old_type,
8884 vec<subst_pair> subst_list)
8886 unsigned int i;
8887 subst_pair *s;
8889 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8890 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8891 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8892 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8893 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8895 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8896 FOR_EACH_VEC_ELT (subst_list, i, s)
8897 TYPE_SIZE (new_type)
8898 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8899 s->discriminant, s->replacement);
8901 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8902 FOR_EACH_VEC_ELT (subst_list, i, s)
8903 TYPE_SIZE_UNIT (new_type)
8904 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8905 s->discriminant, s->replacement);
8907 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8908 FOR_EACH_VEC_ELT (subst_list, i, s)
8909 SET_TYPE_ADA_SIZE
8910 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8911 s->discriminant, s->replacement));
8913 /* Finalize the size. */
8914 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8915 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8918 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
8919 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
8920 the original array type if it has been translated. This association is a
8921 parallel type for GNAT encodings or a debug type for standard DWARF. Note
8922 that for standard DWARF, we also want to get the original type name. */
8924 static void
8925 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
8927 Entity_Id gnat_original_array_type
8928 = Underlying_Type (Original_Array_Type (gnat_entity));
8929 tree gnu_original_array_type;
8931 if (!present_gnu_tree (gnat_original_array_type))
8932 return;
8934 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8936 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8937 return;
8939 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
8941 tree original_name = TYPE_NAME (gnu_original_array_type);
8943 if (TREE_CODE (original_name) == TYPE_DECL)
8944 original_name = DECL_NAME (original_name);
8946 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
8947 TYPE_NAME (gnu_type) = original_name;
8949 else
8950 add_parallel_type (gnu_type, gnu_original_array_type);
8953 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8954 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8955 updated by replacing F with R.
8957 The function doesn't update the layout of the type, i.e. it assumes
8958 that the substitution is purely formal. That's why the replacement
8959 value R must itself contain a PLACEHOLDER_EXPR. */
8961 tree
8962 substitute_in_type (tree t, tree f, tree r)
8964 tree nt;
8966 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8968 switch (TREE_CODE (t))
8970 case INTEGER_TYPE:
8971 case ENUMERAL_TYPE:
8972 case BOOLEAN_TYPE:
8973 case REAL_TYPE:
8975 /* First the domain types of arrays. */
8976 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8977 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8979 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8980 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8982 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8983 return t;
8985 nt = copy_type (t);
8986 TYPE_GCC_MIN_VALUE (nt) = low;
8987 TYPE_GCC_MAX_VALUE (nt) = high;
8989 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8990 SET_TYPE_INDEX_TYPE
8991 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8993 return nt;
8996 /* Then the subtypes. */
8997 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8998 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9000 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9001 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9003 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9004 return t;
9006 nt = copy_type (t);
9007 SET_TYPE_RM_MIN_VALUE (nt, low);
9008 SET_TYPE_RM_MAX_VALUE (nt, high);
9010 return nt;
9013 return t;
9015 case COMPLEX_TYPE:
9016 nt = substitute_in_type (TREE_TYPE (t), f, r);
9017 if (nt == TREE_TYPE (t))
9018 return t;
9020 return build_complex_type (nt);
9022 case FUNCTION_TYPE:
9023 /* These should never show up here. */
9024 gcc_unreachable ();
9026 case ARRAY_TYPE:
9028 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9029 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9031 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9032 return t;
9034 nt = build_nonshared_array_type (component, domain);
9035 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
9036 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9037 SET_TYPE_MODE (nt, TYPE_MODE (t));
9038 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9039 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9040 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
9041 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9042 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9043 return nt;
9046 case RECORD_TYPE:
9047 case UNION_TYPE:
9048 case QUAL_UNION_TYPE:
9050 bool changed_field = false;
9051 tree field;
9053 /* Start out with no fields, make new fields, and chain them
9054 in. If we haven't actually changed the type of any field,
9055 discard everything we've done and return the old type. */
9056 nt = copy_type (t);
9057 TYPE_FIELDS (nt) = NULL_TREE;
9059 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9061 tree new_field = copy_node (field), new_n;
9063 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9064 if (new_n != TREE_TYPE (field))
9066 TREE_TYPE (new_field) = new_n;
9067 changed_field = true;
9070 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9071 if (new_n != DECL_FIELD_OFFSET (field))
9073 DECL_FIELD_OFFSET (new_field) = new_n;
9074 changed_field = true;
9077 /* Do the substitution inside the qualifier, if any. */
9078 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9080 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9081 if (new_n != DECL_QUALIFIER (field))
9083 DECL_QUALIFIER (new_field) = new_n;
9084 changed_field = true;
9088 DECL_CONTEXT (new_field) = nt;
9089 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9091 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9092 TYPE_FIELDS (nt) = new_field;
9095 if (!changed_field)
9096 return t;
9098 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9099 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9100 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9101 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9102 return nt;
9105 default:
9106 return t;
9110 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9111 needed to represent the object. */
9113 tree
9114 rm_size (tree gnu_type)
9116 /* For integral types, we store the RM size explicitly. */
9117 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9118 return TYPE_RM_SIZE (gnu_type);
9120 /* Return the RM size of the actual data plus the size of the template. */
9121 if (TREE_CODE (gnu_type) == RECORD_TYPE
9122 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9123 return
9124 size_binop (PLUS_EXPR,
9125 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9126 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9128 /* For record or union types, we store the size explicitly. */
9129 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9130 && !TYPE_FAT_POINTER_P (gnu_type)
9131 && TYPE_ADA_SIZE (gnu_type))
9132 return TYPE_ADA_SIZE (gnu_type);
9134 /* For other types, this is just the size. */
9135 return TYPE_SIZE (gnu_type);
9138 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9139 fully-qualified name, possibly with type information encoding.
9140 Otherwise, return the name. */
9142 static const char *
9143 get_entity_char (Entity_Id gnat_entity)
9145 Get_Encoded_Name (gnat_entity);
9146 return ggc_strdup (Name_Buffer);
9149 tree
9150 get_entity_name (Entity_Id gnat_entity)
9152 Get_Encoded_Name (gnat_entity);
9153 return get_identifier_with_length (Name_Buffer, Name_Len);
9156 /* Return an identifier representing the external name to be used for
9157 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9158 and the specified suffix. */
9160 tree
9161 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9163 const Entity_Kind kind = Ekind (gnat_entity);
9164 const bool has_suffix = (suffix != NULL);
9165 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9166 String_Pointer sp = {suffix, &temp};
9168 Get_External_Name (gnat_entity, has_suffix, sp);
9170 /* A variable using the Stdcall convention lives in a DLL. We adjust
9171 its name to use the jump table, the _imp__NAME contains the address
9172 for the NAME variable. */
9173 if ((kind == E_Variable || kind == E_Constant)
9174 && Has_Stdcall_Convention (gnat_entity))
9176 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9177 char *new_name = (char *) alloca (len + 1);
9178 strcpy (new_name, STDCALL_PREFIX);
9179 strcat (new_name, Name_Buffer);
9180 return get_identifier_with_length (new_name, len);
9183 return get_identifier_with_length (Name_Buffer, Name_Len);
9186 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9187 string, return a new IDENTIFIER_NODE that is the concatenation of
9188 the name followed by "___" and the specified suffix. */
9190 tree
9191 concat_name (tree gnu_name, const char *suffix)
9193 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9194 char *new_name = (char *) alloca (len + 1);
9195 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9196 strcat (new_name, "___");
9197 strcat (new_name, suffix);
9198 return get_identifier_with_length (new_name, len);
9201 /* Initialize data structures of the decl.c module. */
9203 void
9204 init_gnat_decl (void)
9206 /* Initialize the cache of annotated values. */
9207 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9210 /* Destroy data structures of the decl.c module. */
9212 void
9213 destroy_gnat_decl (void)
9215 /* Destroy the cache of annotated values. */
9216 annotate_value_cache->empty ();
9217 annotate_value_cache = NULL;
9220 #include "gt-ada-decl.h"