Re-commit wrongly reverted previous change.
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob05be419ce949b036348397e9425d827074a03171
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, 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 "tm.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "ggc.h"
36 #include "target.h"
37 #include "tree-inline.h"
38 #include "diagnostic-core.h"
40 #include "ada.h"
41 #include "types.h"
42 #include "atree.h"
43 #include "elists.h"
44 #include "namet.h"
45 #include "nlists.h"
46 #include "repinfo.h"
47 #include "snames.h"
48 #include "stringt.h"
49 #include "uintp.h"
50 #include "fe.h"
51 #include "sinfo.h"
52 #include "einfo.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 /* "stdcall" and "thiscall" conventions should be processed in a specific way
57 on 32-bit x86/Windows only. The macros below are helpers to avoid having
58 to check for a Windows specific attribute throughout this unit. */
60 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
61 #ifdef TARGET_64BIT
62 #define Has_Stdcall_Convention(E) \
63 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
64 #define Has_Thiscall_Convention(E) \
65 (!TARGET_64BIT && is_cplusplus_method (E))
66 #else
67 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
68 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #endif
70 #else
71 #define Has_Stdcall_Convention(E) 0
72 #define Has_Thiscall_Convention(E) 0
73 #endif
75 #define STDCALL_PREFIX "_imp__"
77 /* Stack realignment is necessary for functions with foreign conventions when
78 the ABI doesn't mandate as much as what the compiler assumes - that is, up
79 to PREFERRED_STACK_BOUNDARY.
81 Such realignment can be requested with a dedicated function type attribute
82 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
83 characterize the situations where the attribute should be set. We rely on
84 compiler configuration settings for 'main' to decide. */
86 #ifdef MAIN_STACK_BOUNDARY
87 #define FOREIGN_FORCE_REALIGN_STACK \
88 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
89 #else
90 #define FOREIGN_FORCE_REALIGN_STACK 0
91 #endif
93 struct incomplete
95 struct incomplete *next;
96 tree old_type;
97 Entity_Id full_type;
100 /* These variables are used to defer recursively expanding incomplete types
101 while we are processing an array, a record or a subprogram type. */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
105 /* This variable is used to delay expanding From_Limited_With types until the
106 end of the spec. */
107 static struct incomplete *defer_limited_with;
109 typedef struct subst_pair_d {
110 tree discriminant;
111 tree replacement;
112 } subst_pair;
115 typedef struct variant_desc_d {
116 /* The type of the variant. */
117 tree type;
119 /* The associated field. */
120 tree field;
122 /* The value of the qualifier. */
123 tree qual;
125 /* The type of the variant after transformation. */
126 tree new_type;
127 } variant_desc;
130 /* A hash table used to cache the result of annotate_value. */
131 static GTY ((if_marked ("tree_int_map_marked_p"),
132 param_is (struct tree_int_map))) htab_t annotate_value_cache;
134 static bool allocatable_size_p (tree, bool);
135 static void prepend_one_attribute (struct attrib **,
136 enum attr_type, tree, tree, Node_Id);
137 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
138 static void prepend_attributes (struct attrib **, Entity_Id);
139 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
140 static bool type_has_variable_size (tree);
141 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
142 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
143 unsigned int);
144 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
145 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
146 bool *);
147 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
148 static tree change_qualified_type (tree, int);
149 static bool same_discriminant_p (Entity_Id, Entity_Id);
150 static bool array_type_has_nonaliased_component (tree, Entity_Id);
151 static bool compile_time_known_address_p (Node_Id);
152 static bool cannot_be_superflat_p (Node_Id);
153 static bool constructor_address_p (tree);
154 static int compare_field_bitpos (const PTR, const PTR);
155 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
156 bool, bool, bool, bool, bool, tree, tree *);
157 static Uint annotate_value (tree);
158 static void annotate_rep (Entity_Id, tree);
159 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
160 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
161 static vec<variant_desc> build_variant_list (tree,
162 vec<subst_pair> ,
163 vec<variant_desc> );
164 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
165 static void set_rm_size (Uint, tree, Entity_Id);
166 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
167 static void check_ok_for_atomic (tree, Entity_Id, bool);
168 static tree create_field_decl_from (tree, tree, tree, tree, tree,
169 vec<subst_pair> );
170 static tree create_rep_part (tree, tree, tree);
171 static tree get_rep_part (tree);
172 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
173 tree, vec<subst_pair> );
174 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
175 static void add_parallel_type_for_packed_array (tree, Entity_Id);
177 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
178 to pass around calls performing profile compatibility checks. */
180 typedef struct {
181 Entity_Id gnat_entity; /* The Ada subprogram entity. */
182 tree ada_fntype; /* The corresponding GCC type node. */
183 tree btin_fntype; /* The GCC builtin function type node. */
184 } intrin_binding_t;
186 static bool intrin_profiles_compatible_p (intrin_binding_t *);
188 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
189 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
190 and associate the ..._DECL node with the input GNAT defining identifier.
192 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
193 initial value (in GCC tree form). This is optional for a variable. For
194 a renamed entity, GNU_EXPR gives the object being renamed.
196 DEFINITION is nonzero if this call is intended for a definition. This is
197 used for separate compilation where it is necessary to know whether an
198 external declaration or a definition must be created if the GCC equivalent
199 was not created previously. The value of 1 is normally used for a nonzero
200 DEFINITION, but a value of 2 is used in special circumstances, defined in
201 the code. */
203 tree
204 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
206 /* Contains the kind of the input GNAT node. */
207 const Entity_Kind kind = Ekind (gnat_entity);
208 /* True if this is a type. */
209 const bool is_type = IN (kind, Type_Kind);
210 /* True if debug info is requested for this entity. */
211 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
212 /* True if this entity is to be considered as imported. */
213 const bool imported_p
214 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
215 /* For a type, contains the equivalent GNAT node to be used in gigi. */
216 Entity_Id gnat_equiv_type = Empty;
217 /* Temporary used to walk the GNAT tree. */
218 Entity_Id gnat_temp;
219 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
220 This node will be associated with the GNAT node by calling at the end
221 of the `switch' statement. */
222 tree gnu_decl = NULL_TREE;
223 /* Contains the GCC type to be used for the GCC node. */
224 tree gnu_type = NULL_TREE;
225 /* Contains the GCC size tree to be used for the GCC node. */
226 tree gnu_size = NULL_TREE;
227 /* Contains the GCC name to be used for the GCC node. */
228 tree gnu_entity_name;
229 /* True if we have already saved gnu_decl as a GNAT association. */
230 bool saved = false;
231 /* True if we incremented defer_incomplete_level. */
232 bool this_deferred = false;
233 /* True if we incremented force_global. */
234 bool this_global = false;
235 /* True if we should check to see if elaborated during processing. */
236 bool maybe_present = false;
237 /* True if we made GNU_DECL and its type here. */
238 bool this_made_decl = false;
239 /* Size and alignment of the GCC node, if meaningful. */
240 unsigned int esize = 0, align = 0;
241 /* Contains the list of attributes directly attached to the entity. */
242 struct attrib *attr_list = NULL;
244 /* Since a use of an Itype is a definition, process it as such if it
245 is not in a with'ed unit. */
246 if (!definition
247 && is_type
248 && Is_Itype (gnat_entity)
249 && !present_gnu_tree (gnat_entity)
250 && In_Extended_Main_Code_Unit (gnat_entity))
252 /* Ensure that we are in a subprogram mentioned in the Scope chain of
253 this entity, our current scope is global, or we encountered a task
254 or entry (where we can't currently accurately check scoping). */
255 if (!current_function_decl
256 || DECL_ELABORATION_PROC_P (current_function_decl))
258 process_type (gnat_entity);
259 return get_gnu_tree (gnat_entity);
262 for (gnat_temp = Scope (gnat_entity);
263 Present (gnat_temp);
264 gnat_temp = Scope (gnat_temp))
266 if (Is_Type (gnat_temp))
267 gnat_temp = Underlying_Type (gnat_temp);
269 if (Ekind (gnat_temp) == E_Subprogram_Body)
270 gnat_temp
271 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
273 if (IN (Ekind (gnat_temp), Subprogram_Kind)
274 && Present (Protected_Body_Subprogram (gnat_temp)))
275 gnat_temp = Protected_Body_Subprogram (gnat_temp);
277 if (Ekind (gnat_temp) == E_Entry
278 || Ekind (gnat_temp) == E_Entry_Family
279 || Ekind (gnat_temp) == E_Task_Type
280 || (IN (Ekind (gnat_temp), Subprogram_Kind)
281 && present_gnu_tree (gnat_temp)
282 && (current_function_decl
283 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
285 process_type (gnat_entity);
286 return get_gnu_tree (gnat_entity);
290 /* This abort means the Itype has an incorrect scope, i.e. that its
291 scope does not correspond to the subprogram it is declared in. */
292 gcc_unreachable ();
295 /* If we've already processed this entity, return what we got last time.
296 If we are defining the node, we should not have already processed it.
297 In that case, we will abort below when we try to save a new GCC tree
298 for this object. We also need to handle the case of getting a dummy
299 type when a Full_View exists but be careful so as not to trigger its
300 premature elaboration. */
301 if ((!definition || (is_type && imported_p))
302 && present_gnu_tree (gnat_entity))
304 gnu_decl = get_gnu_tree (gnat_entity);
306 if (TREE_CODE (gnu_decl) == TYPE_DECL
307 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
308 && IN (kind, Incomplete_Or_Private_Kind)
309 && Present (Full_View (gnat_entity))
310 && (present_gnu_tree (Full_View (gnat_entity))
311 || No (Freeze_Node (Full_View (gnat_entity)))))
313 gnu_decl
314 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
315 save_gnu_tree (gnat_entity, NULL_TREE, false);
316 save_gnu_tree (gnat_entity, gnu_decl, false);
319 return gnu_decl;
322 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
323 must be specified unless it was specified by the programmer. Exceptions
324 are for access-to-protected-subprogram types and all access subtypes, as
325 another GNAT type is used to lay out the GCC type for them. */
326 gcc_assert (!Unknown_Esize (gnat_entity)
327 || Has_Size_Clause (gnat_entity)
328 || (!IN (kind, Numeric_Kind)
329 && !IN (kind, Enumeration_Kind)
330 && (!IN (kind, Access_Kind)
331 || kind == E_Access_Protected_Subprogram_Type
332 || kind == E_Anonymous_Access_Protected_Subprogram_Type
333 || kind == E_Access_Subtype
334 || type_annotate_only)));
336 /* The RM size must be specified for all discrete and fixed-point types. */
337 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
338 && Unknown_RM_Size (gnat_entity)));
340 /* If we get here, it means we have not yet done anything with this entity.
341 If we are not defining it, it must be a type or an entity that is defined
342 elsewhere or externally, otherwise we should have defined it already. */
343 gcc_assert (definition
344 || type_annotate_only
345 || is_type
346 || kind == E_Discriminant
347 || kind == E_Component
348 || kind == E_Label
349 || (kind == E_Constant && Present (Full_View (gnat_entity)))
350 || Is_Public (gnat_entity));
352 /* Get the name of the entity and set up the line number and filename of
353 the original definition for use in any decl we make. Make sure we do not
354 inherit another source location. */
355 gnu_entity_name = get_entity_name (gnat_entity);
356 if (Sloc (gnat_entity) != No_Location
357 && !renaming_from_generic_instantiation_p (gnat_entity))
358 Sloc_to_locus (Sloc (gnat_entity), &input_location);
360 /* For cases when we are not defining (i.e., we are referencing from
361 another compilation unit) public entities, show we are at global level
362 for the purpose of computing scopes. Don't do this for components or
363 discriminants since the relevant test is whether or not the record is
364 being defined. */
365 if (!definition
366 && kind != E_Component
367 && kind != E_Discriminant
368 && Is_Public (gnat_entity)
369 && !Is_Statically_Allocated (gnat_entity))
370 force_global++, this_global = true;
372 /* Handle any attributes directly attached to the entity. */
373 if (Has_Gigi_Rep_Item (gnat_entity))
374 prepend_attributes (&attr_list, gnat_entity);
376 /* Do some common processing for types. */
377 if (is_type)
379 /* Compute the equivalent type to be used in gigi. */
380 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
382 /* Machine_Attributes on types are expected to be propagated to
383 subtypes. The corresponding Gigi_Rep_Items are only attached
384 to the first subtype though, so we handle the propagation here. */
385 if (Base_Type (gnat_entity) != gnat_entity
386 && !Is_First_Subtype (gnat_entity)
387 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
388 prepend_attributes (&attr_list,
389 First_Subtype (Base_Type (gnat_entity)));
391 /* Compute a default value for the size of an elementary type. */
392 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
394 unsigned int max_esize;
396 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
397 esize = UI_To_Int (Esize (gnat_entity));
399 if (IN (kind, Float_Kind))
400 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
401 else if (IN (kind, Access_Kind))
402 max_esize = POINTER_SIZE * 2;
403 else
404 max_esize = LONG_LONG_TYPE_SIZE;
406 if (esize > max_esize)
407 esize = max_esize;
411 switch (kind)
413 case E_Constant:
414 /* If this is a use of a deferred constant without address clause,
415 get its full definition. */
416 if (!definition
417 && No (Address_Clause (gnat_entity))
418 && Present (Full_View (gnat_entity)))
420 gnu_decl
421 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
422 saved = true;
423 break;
426 /* If we have an external constant that we are not defining, get the
427 expression that is was defined to represent. We may throw it away
428 later if it is not a constant. But do not retrieve the expression
429 if it is an allocator because the designated type might be dummy
430 at this point. */
431 if (!definition
432 && !No_Initialization (Declaration_Node (gnat_entity))
433 && Present (Expression (Declaration_Node (gnat_entity)))
434 && Nkind (Expression (Declaration_Node (gnat_entity)))
435 != N_Allocator)
437 bool went_into_elab_proc = false;
438 int save_force_global = force_global;
440 /* The expression may contain N_Expression_With_Actions nodes and
441 thus object declarations from other units. In this case, even
442 though the expression will eventually be discarded since not a
443 constant, the declarations would be stuck either in the global
444 varpool or in the current scope. Therefore we force the local
445 context and create a fake scope that we'll zap at the end. */
446 if (!current_function_decl)
448 current_function_decl = get_elaboration_procedure ();
449 went_into_elab_proc = true;
451 force_global = 0;
452 gnat_pushlevel ();
454 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
456 gnat_zaplevel ();
457 force_global = save_force_global;
458 if (went_into_elab_proc)
459 current_function_decl = NULL_TREE;
462 /* Ignore deferred constant definitions without address clause since
463 they are processed fully in the front-end. If No_Initialization
464 is set, this is not a deferred constant but a constant whose value
465 is built manually. And constants that are renamings are handled
466 like variables. */
467 if (definition
468 && !gnu_expr
469 && No (Address_Clause (gnat_entity))
470 && !No_Initialization (Declaration_Node (gnat_entity))
471 && No (Renamed_Object (gnat_entity)))
473 gnu_decl = error_mark_node;
474 saved = true;
475 break;
478 /* Ignore constant definitions already marked with the error node. See
479 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
480 if (definition
481 && gnu_expr
482 && present_gnu_tree (gnat_entity)
483 && get_gnu_tree (gnat_entity) == error_mark_node)
485 maybe_present = true;
486 break;
489 goto object;
491 case E_Exception:
492 goto object;
494 case E_Component:
495 case E_Discriminant:
497 /* The GNAT record where the component was defined. */
498 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
500 /* If the entity is a discriminant of an extended tagged type used to
501 rename a discriminant of the parent type, return the latter. */
502 if (Is_Tagged_Type (gnat_record)
503 && Present (Corresponding_Discriminant (gnat_entity)))
505 gnu_decl
506 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
507 gnu_expr, definition);
508 saved = true;
509 break;
512 /* If the entity is an inherited component (in the case of extended
513 tagged record types), just return the original entity, which must
514 be a FIELD_DECL. Likewise for discriminants. If the entity is a
515 non-girder discriminant (in the case of derived untagged record
516 types), return the stored discriminant it renames. */
517 else if (Present (Original_Record_Component (gnat_entity))
518 && Original_Record_Component (gnat_entity) != gnat_entity)
520 gnu_decl
521 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
522 gnu_expr, definition);
523 saved = true;
524 break;
527 /* Otherwise, if we are not defining this and we have no GCC type
528 for the containing record, make one for it. Then we should
529 have made our own equivalent. */
530 else if (!definition && !present_gnu_tree (gnat_record))
532 /* ??? If this is in a record whose scope is a protected
533 type and we have an Original_Record_Component, use it.
534 This is a workaround for major problems in protected type
535 handling. */
536 Entity_Id Scop = Scope (Scope (gnat_entity));
537 if (Is_Protected_Type (Underlying_Type (Scop))
538 && Present (Original_Record_Component (gnat_entity)))
540 gnu_decl
541 = gnat_to_gnu_entity (Original_Record_Component
542 (gnat_entity),
543 gnu_expr, 0);
544 saved = true;
545 break;
548 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
549 gnu_decl = get_gnu_tree (gnat_entity);
550 saved = true;
551 break;
554 else
555 /* Here we have no GCC type and this is a reference rather than a
556 definition. This should never happen. Most likely the cause is
557 reference before declaration in the GNAT tree for gnat_entity. */
558 gcc_unreachable ();
561 case E_Loop_Parameter:
562 case E_Out_Parameter:
563 case E_Variable:
565 /* Simple variables, loop variables, Out parameters and exceptions. */
566 object:
568 /* Always create a variable for volatile objects and variables seen
569 constant but with a Linker_Section pragma. */
570 bool const_flag
571 = ((kind == E_Constant || kind == E_Variable)
572 && Is_True_Constant (gnat_entity)
573 && !(kind == E_Variable
574 && Present (Linker_Section_Pragma (gnat_entity)))
575 && !Treat_As_Volatile (gnat_entity)
576 && (((Nkind (Declaration_Node (gnat_entity))
577 == N_Object_Declaration)
578 && Present (Expression (Declaration_Node (gnat_entity))))
579 || Present (Renamed_Object (gnat_entity))
580 || imported_p));
581 bool inner_const_flag = const_flag;
582 bool static_p = Is_Statically_Allocated (gnat_entity);
583 bool mutable_p = false;
584 bool used_by_ref = false;
585 tree gnu_ext_name = NULL_TREE;
586 tree renamed_obj = NULL_TREE;
587 tree gnu_object_size;
589 if (Present (Renamed_Object (gnat_entity)) && !definition)
591 if (kind == E_Exception)
592 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
593 NULL_TREE, 0);
594 else
595 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
598 /* Get the type after elaborating the renamed object. */
599 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
601 /* If this is a standard exception definition, then use the standard
602 exception type. This is necessary to make sure that imported and
603 exported views of exceptions are properly merged in LTO mode. */
604 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
605 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
606 gnu_type = except_type_node;
608 /* For a debug renaming declaration, build a debug-only entity. */
609 if (Present (Debug_Renaming_Link (gnat_entity)))
611 /* Force a non-null value to make sure the symbol is retained. */
612 tree value = build1 (INDIRECT_REF, gnu_type,
613 build1 (NOP_EXPR,
614 build_pointer_type (gnu_type),
615 integer_minus_one_node));
616 gnu_decl = build_decl (input_location,
617 VAR_DECL, gnu_entity_name, gnu_type);
618 SET_DECL_VALUE_EXPR (gnu_decl, value);
619 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
620 gnat_pushdecl (gnu_decl, gnat_entity);
621 break;
624 /* If this is a loop variable, its type should be the base type.
625 This is because the code for processing a loop determines whether
626 a normal loop end test can be done by comparing the bounds of the
627 loop against those of the base type, which is presumed to be the
628 size used for computation. But this is not correct when the size
629 of the subtype is smaller than the type. */
630 if (kind == E_Loop_Parameter)
631 gnu_type = get_base_type (gnu_type);
633 /* Reject non-renamed objects whose type is an unconstrained array or
634 any object whose type is a dummy type or void. */
635 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
636 && No (Renamed_Object (gnat_entity)))
637 || TYPE_IS_DUMMY_P (gnu_type)
638 || TREE_CODE (gnu_type) == VOID_TYPE)
640 gcc_assert (type_annotate_only);
641 if (this_global)
642 force_global--;
643 return error_mark_node;
646 /* If an alignment is specified, use it if valid. Note that exceptions
647 are objects but don't have an alignment. We must do this before we
648 validate the size, since the alignment can affect the size. */
649 if (kind != E_Exception && Known_Alignment (gnat_entity))
651 gcc_assert (Present (Alignment (gnat_entity)));
653 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
654 TYPE_ALIGN (gnu_type));
656 /* No point in changing the type if there is an address clause
657 as the final type of the object will be a reference type. */
658 if (Present (Address_Clause (gnat_entity)))
659 align = 0;
660 else
662 tree orig_type = gnu_type;
664 gnu_type
665 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
666 false, false, definition, true);
668 /* If a padding record was made, declare it now since it will
669 never be declared otherwise. This is necessary to ensure
670 that its subtrees are properly marked. */
671 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
672 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
673 debug_info_p, gnat_entity);
677 /* If we are defining the object, see if it has a Size and validate it
678 if so. If we are not defining the object and a Size clause applies,
679 simply retrieve the value. We don't want to ignore the clause and
680 it is expected to have been validated already. Then get the new
681 type, if any. */
682 if (definition)
683 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
684 gnat_entity, VAR_DECL, false,
685 Has_Size_Clause (gnat_entity));
686 else if (Has_Size_Clause (gnat_entity))
687 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
689 if (gnu_size)
691 gnu_type
692 = make_type_from_size (gnu_type, gnu_size,
693 Has_Biased_Representation (gnat_entity));
695 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
696 gnu_size = NULL_TREE;
699 /* If this object has self-referential size, it must be a record with
700 a default discriminant. We are supposed to allocate an object of
701 the maximum size in this case, unless it is a constant with an
702 initializing expression, in which case we can get the size from
703 that. Note that the resulting size may still be a variable, so
704 this may end up with an indirect allocation. */
705 if (No (Renamed_Object (gnat_entity))
706 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
708 if (gnu_expr && kind == E_Constant)
710 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
711 if (CONTAINS_PLACEHOLDER_P (size))
713 /* If the initializing expression is itself a constant,
714 despite having a nominal type with self-referential
715 size, we can get the size directly from it. */
716 if (TREE_CODE (gnu_expr) == COMPONENT_REF
717 && TYPE_IS_PADDING_P
718 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
719 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
720 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
721 || DECL_READONLY_ONCE_ELAB
722 (TREE_OPERAND (gnu_expr, 0))))
723 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
724 else
725 gnu_size
726 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
728 else
729 gnu_size = size;
731 /* We may have no GNU_EXPR because No_Initialization is
732 set even though there's an Expression. */
733 else if (kind == E_Constant
734 && (Nkind (Declaration_Node (gnat_entity))
735 == N_Object_Declaration)
736 && Present (Expression (Declaration_Node (gnat_entity))))
737 gnu_size
738 = TYPE_SIZE (gnat_to_gnu_type
739 (Etype
740 (Expression (Declaration_Node (gnat_entity)))));
741 else
743 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
744 mutable_p = true;
747 /* If we are at global level and the size isn't constant, call
748 elaborate_expression_1 to make a variable for it rather than
749 calculating it each time. */
750 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
751 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
752 get_identifier ("SIZE"),
753 definition, false);
756 /* If the size is zero byte, make it one byte since some linkers have
757 troubles with zero-sized objects. If the object will have a
758 template, that will make it nonzero so don't bother. Also avoid
759 doing that for an object renaming or an object with an address
760 clause, as we would lose useful information on the view size
761 (e.g. for null array slices) and we are not allocating the object
762 here anyway. */
763 if (((gnu_size
764 && integer_zerop (gnu_size)
765 && !TREE_OVERFLOW (gnu_size))
766 || (TYPE_SIZE (gnu_type)
767 && integer_zerop (TYPE_SIZE (gnu_type))
768 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
769 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
770 && No (Renamed_Object (gnat_entity))
771 && No (Address_Clause (gnat_entity)))
772 gnu_size = bitsize_unit_node;
774 /* If this is an object with no specified size and alignment, and
775 if either it is atomic or we are not optimizing alignment for
776 space and it is composite and not an exception, an Out parameter
777 or a reference to another object, and the size of its type is a
778 constant, set the alignment to the smallest one which is not
779 smaller than the size, with an appropriate cap. */
780 if (!gnu_size && align == 0
781 && (Is_Atomic (gnat_entity)
782 || (!Optimize_Alignment_Space (gnat_entity)
783 && kind != E_Exception
784 && kind != E_Out_Parameter
785 && Is_Composite_Type (Etype (gnat_entity))
786 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
787 && !Is_Exported (gnat_entity)
788 && !imported_p
789 && No (Renamed_Object (gnat_entity))
790 && No (Address_Clause (gnat_entity))))
791 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
793 unsigned int size_cap, align_cap;
795 /* No point in promoting the alignment if this doesn't prevent
796 BLKmode access to the object, in particular block copy, as
797 this will for example disable the NRV optimization for it.
798 No point in jumping through all the hoops needed in order
799 to support BIGGEST_ALIGNMENT if we don't really have to.
800 So we cap to the smallest alignment that corresponds to
801 a known efficient memory access pattern of the target. */
802 if (Is_Atomic (gnat_entity))
804 size_cap = UINT_MAX;
805 align_cap = BIGGEST_ALIGNMENT;
807 else
809 size_cap = MAX_FIXED_MODE_SIZE;
810 align_cap = get_mode_alignment (ptr_mode);
813 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
814 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
815 align = 0;
816 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
817 align = align_cap;
818 else
819 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
821 /* But make sure not to under-align the object. */
822 if (align <= TYPE_ALIGN (gnu_type))
823 align = 0;
825 /* And honor the minimum valid atomic alignment, if any. */
826 #ifdef MINIMUM_ATOMIC_ALIGNMENT
827 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
828 align = MINIMUM_ATOMIC_ALIGNMENT;
829 #endif
832 /* If the object is set to have atomic components, find the component
833 type and validate it.
835 ??? Note that we ignore Has_Volatile_Components on objects; it's
836 not at all clear what to do in that case. */
837 if (Has_Atomic_Components (gnat_entity))
839 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
840 ? TREE_TYPE (gnu_type) : gnu_type);
842 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
843 && TYPE_MULTI_ARRAY_P (gnu_inner))
844 gnu_inner = TREE_TYPE (gnu_inner);
846 check_ok_for_atomic (gnu_inner, gnat_entity, true);
849 /* Now check if the type of the object allows atomic access. Note
850 that we must test the type, even if this object has size and
851 alignment to allow such access, because we will be going inside
852 the padded record to assign to the object. We could fix this by
853 always copying via an intermediate value, but it's not clear it's
854 worth the effort. */
855 if (Is_Atomic (gnat_entity))
856 check_ok_for_atomic (gnu_type, gnat_entity, false);
858 /* If this is an aliased object with an unconstrained nominal subtype,
859 make a type that includes the template. */
860 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
861 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
862 && !type_annotate_only)
864 tree gnu_array
865 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
866 gnu_type
867 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
868 gnu_type,
869 concat_name (gnu_entity_name,
870 "UNC"),
871 debug_info_p);
874 /* ??? If this is an object of CW type initialized to a value, try to
875 ensure that the object is sufficient aligned for this value, but
876 without pessimizing the allocation. This is a kludge necessary
877 because we don't support dynamic alignment. */
878 if (align == 0
879 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
880 && No (Renamed_Object (gnat_entity))
881 && No (Address_Clause (gnat_entity)))
882 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
884 #ifdef MINIMUM_ATOMIC_ALIGNMENT
885 /* If the size is a constant and no alignment is specified, force
886 the alignment to be the minimum valid atomic alignment. The
887 restriction on constant size avoids problems with variable-size
888 temporaries; if the size is variable, there's no issue with
889 atomic access. Also don't do this for a constant, since it isn't
890 necessary and can interfere with constant replacement. Finally,
891 do not do it for Out parameters since that creates an
892 size inconsistency with In parameters. */
893 if (align == 0
894 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
895 && !FLOAT_TYPE_P (gnu_type)
896 && !const_flag && No (Renamed_Object (gnat_entity))
897 && !imported_p && No (Address_Clause (gnat_entity))
898 && kind != E_Out_Parameter
899 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
900 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
901 align = MINIMUM_ATOMIC_ALIGNMENT;
902 #endif
904 /* Make a new type with the desired size and alignment, if needed.
905 But do not take into account alignment promotions to compute the
906 size of the object. */
907 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
908 if (gnu_size || align > 0)
910 tree orig_type = gnu_type;
912 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
913 false, false, definition, true);
915 /* If a padding record was made, declare it now since it will
916 never be declared otherwise. This is necessary to ensure
917 that its subtrees are properly marked. */
918 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
919 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
920 debug_info_p, gnat_entity);
923 /* If this is a renaming, avoid as much as possible to create a new
924 object. However, in several cases, creating it is required.
925 This processing needs to be applied to the raw expression so
926 as to make it more likely to rename the underlying object. */
927 if (Present (Renamed_Object (gnat_entity)))
929 bool create_normal_object = false;
931 /* If the renamed object had padding, strip off the reference
932 to the inner object and reset our type. */
933 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
934 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
935 /* Strip useless conversions around the object. */
936 || gnat_useless_type_conversion (gnu_expr))
938 gnu_expr = TREE_OPERAND (gnu_expr, 0);
939 gnu_type = TREE_TYPE (gnu_expr);
942 /* Or else, if the renamed object has an unconstrained type with
943 default discriminant, use the padded type. */
944 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
945 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
946 == gnu_type
947 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
948 gnu_type = TREE_TYPE (gnu_expr);
950 /* Case 1: If this is a constant renaming stemming from a function
951 call, treat it as a normal object whose initial value is what is
952 being renamed. RM 3.3 says that the result of evaluating a
953 function call is a constant object. Treat constant literals
954 the same way. As a consequence, it can be the inner object of
955 a constant renaming. In this case, the renaming must be fully
956 instantiated, i.e. it cannot be a mere reference to (part of) an
957 existing object. */
958 if (const_flag)
960 tree inner_object = gnu_expr;
961 while (handled_component_p (inner_object))
962 inner_object = TREE_OPERAND (inner_object, 0);
963 if (TREE_CODE (inner_object) == CALL_EXPR
964 || CONSTANT_CLASS_P (inner_object))
965 create_normal_object = true;
968 /* Otherwise, see if we can proceed with a stabilized version of
969 the renamed entity or if we need to make a new object. */
970 if (!create_normal_object)
972 tree maybe_stable_expr = NULL_TREE;
973 bool stable = false;
975 /* Case 2: If the renaming entity need not be materialized and
976 the renamed expression is something we can stabilize, use
977 that for the renaming. At the global level, we can only do
978 this if we know no SAVE_EXPRs need be made, because the
979 expression we return might be used in arbitrary conditional
980 branches so we must force the evaluation of the SAVE_EXPRs
981 immediately and this requires a proper function context.
982 Note that an external constant is at the global level. */
983 if (!Materialize_Entity (gnat_entity)
984 && (!((!definition && kind == E_Constant)
985 || global_bindings_p ())
986 || (staticp (gnu_expr)
987 && !TREE_SIDE_EFFECTS (gnu_expr))))
989 maybe_stable_expr
990 = gnat_stabilize_reference (gnu_expr, true, &stable);
992 if (stable)
994 /* ??? No DECL_EXPR is created so we need to mark
995 the expression manually lest it is shared. */
996 if ((!definition && kind == E_Constant)
997 || global_bindings_p ())
998 MARK_VISITED (maybe_stable_expr);
999 gnu_decl = maybe_stable_expr;
1000 save_gnu_tree (gnat_entity, gnu_decl, true);
1001 saved = true;
1002 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1003 false);
1004 /* This assertion will fail if the renamed object
1005 isn't aligned enough as to make it possible to
1006 honor the alignment set on the renaming. */
1007 if (align)
1009 unsigned int renamed_align
1010 = DECL_P (gnu_decl)
1011 ? DECL_ALIGN (gnu_decl)
1012 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1013 gcc_assert (renamed_align >= align);
1015 break;
1018 /* The stabilization failed. Keep maybe_stable_expr
1019 untouched here to let the pointer case below know
1020 about that failure. */
1023 /* Case 3: Make this into a constant pointer to the object we
1024 are to rename and attach the object to the pointer if it is
1025 something we can stabilize.
1027 From the proper scope, attached objects will be referenced
1028 directly instead of indirectly via the pointer to avoid
1029 subtle aliasing problems with non-addressable entities.
1030 They have to be stable because we must not evaluate the
1031 variables in the expression every time the renaming is used.
1032 The pointer is called a "renaming" pointer in this case.
1034 In the rare cases where we cannot stabilize the renamed
1035 object, we just make a "bare" pointer and the renamed
1036 object will always be accessed indirectly through it.
1038 Note that we need to preserve the volatility of the renamed
1039 object through the indirection. */
1040 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1041 gnu_type
1042 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1043 gnu_type = build_reference_type (gnu_type);
1044 inner_const_flag = TREE_READONLY (gnu_expr);
1045 const_flag = true;
1047 /* If the previous attempt at stabilizing failed, there is
1048 no point in trying again and we reuse the result without
1049 attaching it to the pointer. In this case it will only
1050 be used as the initializing expression of the pointer and
1051 thus needs no special treatment with regard to multiple
1052 evaluations.
1054 Otherwise, try to stabilize and attach the expression to
1055 the pointer if the stabilization succeeds.
1057 Note that this might introduce SAVE_EXPRs and we don't
1058 check whether we are at the global level or not. This
1059 is fine since we are building a pointer initializer and
1060 neither the pointer nor the initializing expression can
1061 be accessed before the pointer elaboration has taken
1062 place in a correct program.
1064 These SAVE_EXPRs will be evaluated at the right place
1065 by either the evaluation of the initializer for the
1066 non-global case or the elaboration code for the global
1067 case, and will be attached to the elaboration procedure
1068 in the latter case. */
1069 if (!maybe_stable_expr)
1071 maybe_stable_expr
1072 = gnat_stabilize_reference (gnu_expr, true, &stable);
1074 if (stable)
1075 renamed_obj = maybe_stable_expr;
1078 if (type_annotate_only
1079 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1080 gnu_expr = NULL_TREE;
1081 else
1082 gnu_expr
1083 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1085 gnu_size = NULL_TREE;
1086 used_by_ref = true;
1090 /* Make a volatile version of this object's type if we are to make
1091 the object volatile. We also interpret 13.3(19) conservatively
1092 and disallow any optimizations for such a non-constant object. */
1093 if ((Treat_As_Volatile (gnat_entity)
1094 || (!const_flag
1095 && gnu_type != except_type_node
1096 && (Is_Exported (gnat_entity)
1097 || imported_p
1098 || Present (Address_Clause (gnat_entity)))))
1099 && !TYPE_VOLATILE (gnu_type))
1100 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1102 /* If we are defining an aliased object whose nominal subtype is
1103 unconstrained, the object is a record that contains both the
1104 template and the object. If there is an initializer, it will
1105 have already been converted to the right type, but we need to
1106 create the template if there is no initializer. */
1107 if (definition
1108 && !gnu_expr
1109 && TREE_CODE (gnu_type) == RECORD_TYPE
1110 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1111 /* Beware that padding might have been introduced above. */
1112 || (TYPE_PADDING_P (gnu_type)
1113 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1114 == RECORD_TYPE
1115 && TYPE_CONTAINS_TEMPLATE_P
1116 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1118 tree template_field
1119 = TYPE_PADDING_P (gnu_type)
1120 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1121 : TYPE_FIELDS (gnu_type);
1122 vec<constructor_elt, va_gc> *v;
1123 vec_alloc (v, 1);
1124 tree t = build_template (TREE_TYPE (template_field),
1125 TREE_TYPE (DECL_CHAIN (template_field)),
1126 NULL_TREE);
1127 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1128 gnu_expr = gnat_build_constructor (gnu_type, v);
1131 /* Convert the expression to the type of the object except in the
1132 case where the object's type is unconstrained or the object's type
1133 is a padded record whose field is of self-referential size. In
1134 the former case, converting will generate unnecessary evaluations
1135 of the CONSTRUCTOR to compute the size and in the latter case, we
1136 want to only copy the actual data. Also don't convert to a record
1137 type with a variant part from a record type without one, to keep
1138 the object simpler. */
1139 if (gnu_expr
1140 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1141 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1142 && !(TYPE_IS_PADDING_P (gnu_type)
1143 && CONTAINS_PLACEHOLDER_P
1144 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1145 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1146 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1147 && get_variant_part (gnu_type) != NULL_TREE
1148 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1149 gnu_expr = convert (gnu_type, gnu_expr);
1151 /* If this is a pointer that doesn't have an initializing expression,
1152 initialize it to NULL, unless the object is imported. */
1153 if (definition
1154 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1155 && !gnu_expr
1156 && !Is_Imported (gnat_entity))
1157 gnu_expr = integer_zero_node;
1159 /* If we are defining the object and it has an Address clause, we must
1160 either get the address expression from the saved GCC tree for the
1161 object if it has a Freeze node, or elaborate the address expression
1162 here since the front-end has guaranteed that the elaboration has no
1163 effects in this case. */
1164 if (definition && Present (Address_Clause (gnat_entity)))
1166 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1167 tree gnu_address
1168 = present_gnu_tree (gnat_entity)
1169 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1171 save_gnu_tree (gnat_entity, NULL_TREE, false);
1173 /* Ignore the size. It's either meaningless or was handled
1174 above. */
1175 gnu_size = NULL_TREE;
1176 /* Convert the type of the object to a reference type that can
1177 alias everything as per 13.3(19). */
1178 gnu_type
1179 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1180 gnu_address = convert (gnu_type, gnu_address);
1181 used_by_ref = true;
1182 const_flag
1183 = !Is_Public (gnat_entity)
1184 || compile_time_known_address_p (gnat_expr);
1186 /* If this is a deferred constant, the initializer is attached to
1187 the full view. */
1188 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1189 gnu_expr
1190 = gnat_to_gnu
1191 (Expression (Declaration_Node (Full_View (gnat_entity))));
1193 /* If we don't have an initializing expression for the underlying
1194 variable, the initializing expression for the pointer is the
1195 specified address. Otherwise, we have to make a COMPOUND_EXPR
1196 to assign both the address and the initial value. */
1197 if (!gnu_expr)
1198 gnu_expr = gnu_address;
1199 else
1200 gnu_expr
1201 = build2 (COMPOUND_EXPR, gnu_type,
1202 build_binary_op
1203 (MODIFY_EXPR, NULL_TREE,
1204 build_unary_op (INDIRECT_REF, NULL_TREE,
1205 gnu_address),
1206 gnu_expr),
1207 gnu_address);
1210 /* If it has an address clause and we are not defining it, mark it
1211 as an indirect object. Likewise for Stdcall objects that are
1212 imported. */
1213 if ((!definition && Present (Address_Clause (gnat_entity)))
1214 || (Is_Imported (gnat_entity)
1215 && Has_Stdcall_Convention (gnat_entity)))
1217 /* Convert the type of the object to a reference type that can
1218 alias everything as per 13.3(19). */
1219 gnu_type
1220 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1221 gnu_size = NULL_TREE;
1223 /* No point in taking the address of an initializing expression
1224 that isn't going to be used. */
1225 gnu_expr = NULL_TREE;
1227 /* If it has an address clause whose value is known at compile
1228 time, make the object a CONST_DECL. This will avoid a
1229 useless dereference. */
1230 if (Present (Address_Clause (gnat_entity)))
1232 Node_Id gnat_address
1233 = Expression (Address_Clause (gnat_entity));
1235 if (compile_time_known_address_p (gnat_address))
1237 gnu_expr = gnat_to_gnu (gnat_address);
1238 const_flag = true;
1242 used_by_ref = true;
1245 /* If we are at top level and this object is of variable size,
1246 make the actual type a hidden pointer to the real type and
1247 make the initializer be a memory allocation and initialization.
1248 Likewise for objects we aren't defining (presumed to be
1249 external references from other packages), but there we do
1250 not set up an initialization.
1252 If the object's size overflows, make an allocator too, so that
1253 Storage_Error gets raised. Note that we will never free
1254 such memory, so we presume it never will get allocated. */
1255 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1256 global_bindings_p ()
1257 || !definition
1258 || static_p)
1259 || (gnu_size
1260 && !allocatable_size_p (convert (sizetype,
1261 size_binop
1262 (CEIL_DIV_EXPR, gnu_size,
1263 bitsize_unit_node)),
1264 global_bindings_p ()
1265 || !definition
1266 || static_p)))
1268 gnu_type = build_reference_type (gnu_type);
1269 gnu_size = NULL_TREE;
1270 used_by_ref = true;
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 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1296 gnu_expr = 0;
1297 else
1298 gnu_expr
1299 = build_component_ref
1300 (gnu_expr, NULL_TREE,
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);
1313 const_flag = true;
1315 else
1317 gnu_expr = NULL_TREE;
1318 const_flag = false;
1322 /* If this object would go into the stack and has an alignment larger
1323 than the largest stack alignment the back-end can honor, resort to
1324 a variable of "aligning type". */
1325 if (!global_bindings_p () && !static_p && definition
1326 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1328 /* Create the new variable. No need for extra room before the
1329 aligned field as this is in automatic storage. */
1330 tree gnu_new_type
1331 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1332 TYPE_SIZE_UNIT (gnu_type),
1333 BIGGEST_ALIGNMENT, 0, gnat_entity);
1334 tree gnu_new_var
1335 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1336 NULL_TREE, gnu_new_type, NULL_TREE, false,
1337 false, false, false, 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 (MODIFY_EXPR, NULL_TREE,
1343 build_component_ref
1344 (gnu_new_var, NULL_TREE,
1345 TYPE_FIELDS (gnu_new_type), 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, gnu_type,
1354 build_component_ref (gnu_new_var, NULL_TREE,
1355 TYPE_FIELDS (gnu_new_type), false));
1357 gnu_size = NULL_TREE;
1358 used_by_ref = true;
1359 const_flag = true;
1362 /* If this is an aliased object with an unconstrained nominal subtype,
1363 we make its type a thin reference, i.e. the reference counterpart
1364 of a thin pointer, so that it points to the array part. This is
1365 aimed at making it easier for the debugger to decode the object.
1366 Note that we have to do that this late because of the couple of
1367 allocation adjustments that might be made just above. */
1368 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1369 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1370 && !type_annotate_only)
1372 tree gnu_array
1373 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1375 /* In case the object with the template has already been allocated
1376 just above, we have nothing to do here. */
1377 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1379 tree gnu_unc_var
1380 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1381 NULL_TREE, gnu_type, gnu_expr,
1382 const_flag, Is_Public (gnat_entity),
1383 imported_p || !definition, static_p,
1384 NULL, gnat_entity);
1385 gnu_expr
1386 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1387 TREE_CONSTANT (gnu_expr) = 1;
1389 gnu_size = NULL_TREE;
1390 used_by_ref = true;
1391 const_flag = true;
1394 gnu_type
1395 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1398 if (const_flag)
1399 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1401 /* Convert the expression to the type of the object except in the
1402 case where the object's type is unconstrained or the object's type
1403 is a padded record whose field is of self-referential size. In
1404 the former case, converting will generate unnecessary evaluations
1405 of the CONSTRUCTOR to compute the size and in the latter case, we
1406 want to only copy the actual data. Also don't convert to a record
1407 type with a variant part from a record type without one, to keep
1408 the object simpler. */
1409 if (gnu_expr
1410 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1411 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1412 && !(TYPE_IS_PADDING_P (gnu_type)
1413 && CONTAINS_PLACEHOLDER_P
1414 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1415 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1416 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1417 && get_variant_part (gnu_type) != NULL_TREE
1418 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1419 gnu_expr = convert (gnu_type, gnu_expr);
1421 /* If this name is external or a name was specified, use it, but don't
1422 use the Interface_Name with an address clause (see cd30005). */
1423 if ((Present (Interface_Name (gnat_entity))
1424 && No (Address_Clause (gnat_entity)))
1425 || (Is_Public (gnat_entity)
1426 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1427 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1429 /* If this is an aggregate constant initialized to a constant, force it
1430 to be statically allocated. This saves an initialization copy. */
1431 if (!static_p
1432 && const_flag
1433 && gnu_expr && TREE_CONSTANT (gnu_expr)
1434 && AGGREGATE_TYPE_P (gnu_type)
1435 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1436 && !(TYPE_IS_PADDING_P (gnu_type)
1437 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1438 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1439 static_p = true;
1441 /* Deal with a pragma Linker_Section on a constant or variable. */
1442 if ((kind == E_Constant || kind == E_Variable)
1443 && Present (Linker_Section_Pragma (gnat_entity)))
1444 prepend_one_attribute_pragma (&attr_list,
1445 Linker_Section_Pragma (gnat_entity));
1447 /* Now create the variable or the constant and set various flags. */
1448 gnu_decl
1449 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1450 gnu_expr, const_flag, Is_Public (gnat_entity),
1451 imported_p || !definition, static_p,
1452 !renamed_obj, attr_list, gnat_entity);
1453 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1454 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1455 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1457 /* If we are defining an Out parameter and optimization isn't enabled,
1458 create a fake PARM_DECL for debugging purposes and make it point to
1459 the VAR_DECL. Suppress debug info for the latter but make sure it
1460 will live in memory so that it can be accessed from within the
1461 debugger through the PARM_DECL. */
1462 if (kind == E_Out_Parameter
1463 && definition
1464 && debug_info_p
1465 && !optimize
1466 && !flag_generate_lto)
1468 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1469 gnat_pushdecl (param, gnat_entity);
1470 SET_DECL_VALUE_EXPR (param, gnu_decl);
1471 DECL_HAS_VALUE_EXPR_P (param) = 1;
1472 DECL_IGNORED_P (gnu_decl) = 1;
1473 TREE_ADDRESSABLE (gnu_decl) = 1;
1476 /* If this is a loop parameter, set the corresponding flag. */
1477 else if (kind == E_Loop_Parameter)
1478 DECL_LOOP_PARM_P (gnu_decl) = 1;
1480 /* If this is a renaming pointer, attach the renamed object to it and
1481 register it if we are at the global level. Note that an external
1482 constant is at the global level. */
1483 if (renamed_obj)
1485 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1486 if ((!definition && kind == E_Constant) || global_bindings_p ())
1488 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1489 record_global_renaming_pointer (gnu_decl);
1493 /* If this is a constant and we are defining it or it generates a real
1494 symbol at the object level and we are referencing it, we may want
1495 or need to have a true variable to represent it:
1496 - if optimization isn't enabled, for debugging purposes,
1497 - if the constant is public and not overlaid on something else,
1498 - if its address is taken,
1499 - if either itself or its type is aliased. */
1500 if (TREE_CODE (gnu_decl) == CONST_DECL
1501 && (definition || Sloc (gnat_entity) > Standard_Location)
1502 && ((!optimize && debug_info_p)
1503 || (Is_Public (gnat_entity)
1504 && No (Address_Clause (gnat_entity)))
1505 || Address_Taken (gnat_entity)
1506 || Is_Aliased (gnat_entity)
1507 || Is_Aliased (Etype (gnat_entity))))
1509 tree gnu_corr_var
1510 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1511 gnu_expr, true, Is_Public (gnat_entity),
1512 !definition, static_p, attr_list,
1513 gnat_entity);
1515 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1517 /* As debugging information will be generated for the variable,
1518 do not generate debugging information for the constant. */
1519 if (debug_info_p)
1520 DECL_IGNORED_P (gnu_decl) = 1;
1521 else
1522 DECL_IGNORED_P (gnu_corr_var) = 1;
1525 /* If this is a constant, even if we don't need a true variable, we
1526 may need to avoid returning the initializer in every case. That
1527 can happen for the address of a (constant) constructor because,
1528 upon dereferencing it, the constructor will be reinjected in the
1529 tree, which may not be valid in every case; see lvalue_required_p
1530 for more details. */
1531 if (TREE_CODE (gnu_decl) == CONST_DECL)
1532 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1534 /* If this object is declared in a block that contains a block with an
1535 exception handler, and we aren't using the GCC exception mechanism,
1536 we must force this variable in memory in order to avoid an invalid
1537 optimization. */
1538 if (Exception_Mechanism != Back_End_Exceptions
1539 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1540 TREE_ADDRESSABLE (gnu_decl) = 1;
1542 /* If this is a local variable with non-BLKmode and aggregate type,
1543 and optimization isn't enabled, then force it in memory so that
1544 a register won't be allocated to it with possible subparts left
1545 uninitialized and reaching the register allocator. */
1546 else if (TREE_CODE (gnu_decl) == VAR_DECL
1547 && !DECL_EXTERNAL (gnu_decl)
1548 && !TREE_STATIC (gnu_decl)
1549 && DECL_MODE (gnu_decl) != BLKmode
1550 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1551 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1552 && !optimize)
1553 TREE_ADDRESSABLE (gnu_decl) = 1;
1555 /* If we are defining an object with variable size or an object with
1556 fixed size that will be dynamically allocated, and we are using the
1557 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1558 if (definition
1559 && Exception_Mechanism == Setjmp_Longjmp
1560 && get_block_jmpbuf_decl ()
1561 && DECL_SIZE_UNIT (gnu_decl)
1562 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1563 || (flag_stack_check == GENERIC_STACK_CHECK
1564 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1565 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1566 add_stmt_with_node (build_call_n_expr
1567 (update_setjmp_buf_decl, 1,
1568 build_unary_op (ADDR_EXPR, NULL_TREE,
1569 get_block_jmpbuf_decl ())),
1570 gnat_entity);
1572 /* Back-annotate Esize and Alignment of the object if not already
1573 known. Note that we pick the values of the type, not those of
1574 the object, to shield ourselves from low-level platform-dependent
1575 adjustments like alignment promotion. This is both consistent with
1576 all the treatment above, where alignment and size are set on the
1577 type of the object and not on the object directly, and makes it
1578 possible to support all confirming representation clauses. */
1579 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1580 used_by_ref);
1582 break;
1584 case E_Void:
1585 /* Return a TYPE_DECL for "void" that we previously made. */
1586 gnu_decl = TYPE_NAME (void_type_node);
1587 break;
1589 case E_Enumeration_Type:
1590 /* A special case: for the types Character and Wide_Character in
1591 Standard, we do not list all the literals. So if the literals
1592 are not specified, make this an unsigned integer type. */
1593 if (No (First_Literal (gnat_entity)))
1595 gnu_type = make_unsigned_type (esize);
1596 TYPE_NAME (gnu_type) = gnu_entity_name;
1598 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1599 This is needed by the DWARF-2 back-end to distinguish between
1600 unsigned integer types and character types. */
1601 TYPE_STRING_FLAG (gnu_type) = 1;
1603 else
1605 /* We have a list of enumeral constants in First_Literal. We make a
1606 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1607 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1608 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1609 value of the literal. But when we have a regular boolean type, we
1610 simplify this a little by using a BOOLEAN_TYPE. */
1611 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1612 && !Has_Non_Standard_Rep (gnat_entity);
1613 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1614 tree gnu_list = NULL_TREE;
1615 Entity_Id gnat_literal;
1617 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1618 TYPE_PRECISION (gnu_type) = esize;
1619 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1620 set_min_and_max_values_for_integral_type (gnu_type, esize,
1621 TYPE_SIGN (gnu_type));
1622 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1623 layout_type (gnu_type);
1625 for (gnat_literal = First_Literal (gnat_entity);
1626 Present (gnat_literal);
1627 gnat_literal = Next_Literal (gnat_literal))
1629 tree gnu_value
1630 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1631 tree gnu_literal
1632 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1633 gnu_type, gnu_value, true, false, false,
1634 false, NULL, gnat_literal);
1635 /* Do not generate debug info for individual enumerators. */
1636 DECL_IGNORED_P (gnu_literal) = 1;
1637 save_gnu_tree (gnat_literal, gnu_literal, false);
1638 gnu_list
1639 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1642 if (!is_boolean)
1643 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1645 /* Note that the bounds are updated at the end of this function
1646 to avoid an infinite recursion since they refer to the type. */
1647 goto discrete_type;
1649 break;
1651 case E_Signed_Integer_Type:
1652 case E_Ordinary_Fixed_Point_Type:
1653 case E_Decimal_Fixed_Point_Type:
1654 /* For integer types, just make a signed type the appropriate number
1655 of bits. */
1656 gnu_type = make_signed_type (esize);
1657 goto discrete_type;
1659 case E_Modular_Integer_Type:
1661 /* For modular types, make the unsigned type of the proper number
1662 of bits and then set up the modulus, if required. */
1663 tree gnu_modulus, gnu_high = NULL_TREE;
1665 /* Packed Array Impl. Types are supposed to be subtypes only. */
1666 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1668 gnu_type = make_unsigned_type (esize);
1670 /* Get the modulus in this type. If it overflows, assume it is because
1671 it is equal to 2**Esize. Note that there is no overflow checking
1672 done on unsigned type, so we detect the overflow by looking for
1673 a modulus of zero, which is otherwise invalid. */
1674 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1676 if (!integer_zerop (gnu_modulus))
1678 TYPE_MODULAR_P (gnu_type) = 1;
1679 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1680 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1681 convert (gnu_type, integer_one_node));
1684 /* If the upper bound is not maximal, make an extra subtype. */
1685 if (gnu_high
1686 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1688 tree gnu_subtype = make_unsigned_type (esize);
1689 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1690 TREE_TYPE (gnu_subtype) = gnu_type;
1691 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1692 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1693 gnu_type = gnu_subtype;
1696 goto discrete_type;
1698 case E_Signed_Integer_Subtype:
1699 case E_Enumeration_Subtype:
1700 case E_Modular_Integer_Subtype:
1701 case E_Ordinary_Fixed_Point_Subtype:
1702 case E_Decimal_Fixed_Point_Subtype:
1704 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1705 not want to call create_range_type since we would like each subtype
1706 node to be distinct. ??? Historically this was in preparation for
1707 when memory aliasing is implemented, but that's obsolete now given
1708 the call to relate_alias_sets below.
1710 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1711 this fact is used by the arithmetic conversion functions.
1713 We elaborate the Ancestor_Subtype if it is not in the current unit
1714 and one of our bounds is non-static. We do this to ensure consistent
1715 naming in the case where several subtypes share the same bounds, by
1716 elaborating the first such subtype first, thus using its name. */
1718 if (!definition
1719 && Present (Ancestor_Subtype (gnat_entity))
1720 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1721 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1722 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1723 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1725 /* Set the precision to the Esize except for bit-packed arrays. */
1726 if (Is_Packed_Array_Impl_Type (gnat_entity)
1727 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1728 esize = UI_To_Int (RM_Size (gnat_entity));
1730 /* This should be an unsigned type if the base type is unsigned or
1731 if the lower bound is constant and non-negative or if the type
1732 is biased. */
1733 if (Is_Unsigned_Type (Etype (gnat_entity))
1734 || Is_Unsigned_Type (gnat_entity)
1735 || Has_Biased_Representation (gnat_entity))
1736 gnu_type = make_unsigned_type (esize);
1737 else
1738 gnu_type = make_signed_type (esize);
1739 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1741 SET_TYPE_RM_MIN_VALUE
1742 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1743 gnat_entity, get_identifier ("L"),
1744 definition, true,
1745 Needs_Debug_Info (gnat_entity)));
1747 SET_TYPE_RM_MAX_VALUE
1748 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1749 gnat_entity, get_identifier ("U"),
1750 definition, true,
1751 Needs_Debug_Info (gnat_entity)));
1753 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1754 = Has_Biased_Representation (gnat_entity);
1756 /* Inherit our alias set from what we're a subtype of. Subtypes
1757 are not different types and a pointer can designate any instance
1758 within a subtype hierarchy. */
1759 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1761 /* One of the above calls might have caused us to be elaborated,
1762 so don't blow up if so. */
1763 if (present_gnu_tree (gnat_entity))
1765 maybe_present = true;
1766 break;
1769 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1770 TYPE_STUB_DECL (gnu_type)
1771 = create_type_stub_decl (gnu_entity_name, gnu_type);
1773 /* For a packed array, make the original array type a parallel type. */
1774 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1775 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1777 discrete_type:
1779 /* We have to handle clauses that under-align the type specially. */
1780 if ((Present (Alignment_Clause (gnat_entity))
1781 || (Is_Packed_Array_Impl_Type (gnat_entity)
1782 && Present
1783 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1784 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1786 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1787 if (align >= TYPE_ALIGN (gnu_type))
1788 align = 0;
1791 /* If the type we are dealing with represents a bit-packed array,
1792 we need to have the bits left justified on big-endian targets
1793 and right justified on little-endian targets. We also need to
1794 ensure that when the value is read (e.g. for comparison of two
1795 such values), we only get the good bits, since the unused bits
1796 are uninitialized. Both goals are accomplished by wrapping up
1797 the modular type in an enclosing record type. */
1798 if (Is_Packed_Array_Impl_Type (gnat_entity)
1799 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1801 tree gnu_field_type, gnu_field;
1803 /* Set the RM size before wrapping up the original type. */
1804 SET_TYPE_RM_SIZE (gnu_type,
1805 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1806 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1808 /* Create a stripped-down declaration, mainly for debugging. */
1809 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1810 gnat_entity);
1812 /* Now save it and build the enclosing record type. */
1813 gnu_field_type = gnu_type;
1815 gnu_type = make_node (RECORD_TYPE);
1816 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1817 TYPE_PACKED (gnu_type) = 1;
1818 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1819 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1820 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1822 /* Propagate the alignment of the modular type to the record type,
1823 unless there is an alignment clause that under-aligns the type.
1824 This means that bit-packed arrays are given "ceil" alignment for
1825 their size by default, which may seem counter-intuitive but makes
1826 it possible to overlay them on modular types easily. */
1827 TYPE_ALIGN (gnu_type)
1828 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1830 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1832 /* Don't declare the field as addressable since we won't be taking
1833 its address and this would prevent create_field_decl from making
1834 a bitfield. */
1835 gnu_field
1836 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1837 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1839 /* Do not emit debug info until after the parallel type is added. */
1840 finish_record_type (gnu_type, gnu_field, 2, false);
1841 compute_record_mode (gnu_type);
1842 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1844 if (debug_info_p)
1846 /* Make the original array type a parallel type. */
1847 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1849 rest_of_record_type_compilation (gnu_type);
1853 /* If the type we are dealing with has got a smaller alignment than the
1854 natural one, we need to wrap it up in a record type and misalign the
1855 latter; we reuse the padding machinery for this purpose. Note that,
1856 even if the record type is marked as packed because of misalignment,
1857 we don't pack the field so as to give it the size of the type. */
1858 else if (align > 0)
1860 tree gnu_field_type, gnu_field;
1862 /* Set the RM size before wrapping up the type. */
1863 SET_TYPE_RM_SIZE (gnu_type,
1864 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1866 /* Create a stripped-down declaration, mainly for debugging. */
1867 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1868 gnat_entity);
1870 /* Now save it and build the enclosing record type. */
1871 gnu_field_type = gnu_type;
1873 gnu_type = make_node (RECORD_TYPE);
1874 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1875 TYPE_PACKED (gnu_type) = 1;
1876 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1877 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1878 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1879 TYPE_ALIGN (gnu_type) = align;
1880 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1882 /* Don't declare the field as addressable since we won't be taking
1883 its address and this would prevent create_field_decl from making
1884 a bitfield. */
1885 gnu_field
1886 = create_field_decl (get_identifier ("F"), gnu_field_type,
1887 gnu_type, TYPE_SIZE (gnu_field_type),
1888 bitsize_zero_node, 0, 0);
1890 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1891 compute_record_mode (gnu_type);
1892 TYPE_PADDING_P (gnu_type) = 1;
1895 break;
1897 case E_Floating_Point_Type:
1898 /* The type of the Low and High bounds can be our type if this is
1899 a type from Standard, so set them at the end of the function. */
1900 gnu_type = make_node (REAL_TYPE);
1901 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1902 layout_type (gnu_type);
1903 break;
1905 case E_Floating_Point_Subtype:
1906 /* See the E_Signed_Integer_Subtype case for the rationale. */
1907 if (!definition
1908 && Present (Ancestor_Subtype (gnat_entity))
1909 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1910 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1911 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1912 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1914 gnu_type = make_node (REAL_TYPE);
1915 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1916 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1917 TYPE_GCC_MIN_VALUE (gnu_type)
1918 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1919 TYPE_GCC_MAX_VALUE (gnu_type)
1920 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1921 layout_type (gnu_type);
1923 SET_TYPE_RM_MIN_VALUE
1924 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1925 gnat_entity, get_identifier ("L"),
1926 definition, true,
1927 Needs_Debug_Info (gnat_entity)));
1929 SET_TYPE_RM_MAX_VALUE
1930 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1931 gnat_entity, get_identifier ("U"),
1932 definition, true,
1933 Needs_Debug_Info (gnat_entity)));
1935 /* Inherit our alias set from what we're a subtype of, as for
1936 integer subtypes. */
1937 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1939 /* One of the above calls might have caused us to be elaborated,
1940 so don't blow up if so. */
1941 maybe_present = true;
1942 break;
1944 /* Array Types and Subtypes
1946 Unconstrained array types are represented by E_Array_Type and
1947 constrained array types are represented by E_Array_Subtype. There
1948 are no actual objects of an unconstrained array type; all we have
1949 are pointers to that type.
1951 The following fields are defined on array types and subtypes:
1953 Component_Type Component type of the array.
1954 Number_Dimensions Number of dimensions (an int).
1955 First_Index Type of first index. */
1957 case E_Array_Type:
1959 const bool convention_fortran_p
1960 = (Convention (gnat_entity) == Convention_Fortran);
1961 const int ndim = Number_Dimensions (gnat_entity);
1962 tree gnu_template_type;
1963 tree gnu_ptr_template;
1964 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1965 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1966 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1967 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1968 Entity_Id gnat_index, gnat_name;
1969 int index;
1970 tree comp_type;
1972 /* Create the type for the component now, as it simplifies breaking
1973 type reference loops. */
1974 comp_type
1975 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
1976 if (present_gnu_tree (gnat_entity))
1978 /* As a side effect, the type may have been translated. */
1979 maybe_present = true;
1980 break;
1983 /* We complete an existing dummy fat pointer type in place. This both
1984 avoids further complex adjustments in update_pointer_to and yields
1985 better debugging information in DWARF by leveraging the support for
1986 incomplete declarations of "tagged" types in the DWARF back-end. */
1987 gnu_type = get_dummy_type (gnat_entity);
1988 if (gnu_type && TYPE_POINTER_TO (gnu_type))
1990 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
1991 TYPE_NAME (gnu_fat_type) = NULL_TREE;
1992 /* Save the contents of the dummy type for update_pointer_to. */
1993 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
1994 gnu_ptr_template =
1995 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1996 gnu_template_type = TREE_TYPE (gnu_ptr_template);
1998 else
2000 gnu_fat_type = make_node (RECORD_TYPE);
2001 gnu_template_type = make_node (RECORD_TYPE);
2002 gnu_ptr_template = build_pointer_type (gnu_template_type);
2005 /* Make a node for the array. If we are not defining the array
2006 suppress expanding incomplete types. */
2007 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2009 if (!definition)
2011 defer_incomplete_level++;
2012 this_deferred = true;
2015 /* Build the fat pointer type. Use a "void *" object instead of
2016 a pointer to the array type since we don't have the array type
2017 yet (it will reference the fat pointer via the bounds). */
2019 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2020 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2021 DECL_CHAIN (tem)
2022 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2023 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2025 if (COMPLETE_TYPE_P (gnu_fat_type))
2027 /* We are going to lay it out again so reset the alias set. */
2028 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2029 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2030 finish_fat_pointer_type (gnu_fat_type, tem);
2031 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2032 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2034 TYPE_FIELDS (t) = tem;
2035 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2038 else
2040 finish_fat_pointer_type (gnu_fat_type, tem);
2041 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2044 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2045 is the fat pointer. This will be used to access the individual
2046 fields once we build them. */
2047 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2048 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2049 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2050 gnu_template_reference
2051 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2052 TREE_READONLY (gnu_template_reference) = 1;
2053 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2055 /* Now create the GCC type for each index and add the fields for that
2056 index to the template. */
2057 for (index = (convention_fortran_p ? ndim - 1 : 0),
2058 gnat_index = First_Index (gnat_entity);
2059 0 <= index && index < ndim;
2060 index += (convention_fortran_p ? - 1 : 1),
2061 gnat_index = Next_Index (gnat_index))
2063 char field_name[16];
2064 tree gnu_index_base_type
2065 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2066 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2067 tree gnu_min, gnu_max, gnu_high;
2069 /* Make the FIELD_DECLs for the low and high bounds of this
2070 type and then make extractions of these fields from the
2071 template. */
2072 sprintf (field_name, "LB%d", index);
2073 gnu_lb_field = create_field_decl (get_identifier (field_name),
2074 gnu_index_base_type,
2075 gnu_template_type, NULL_TREE,
2076 NULL_TREE, 0, 0);
2077 Sloc_to_locus (Sloc (gnat_entity),
2078 &DECL_SOURCE_LOCATION (gnu_lb_field));
2080 field_name[0] = 'U';
2081 gnu_hb_field = create_field_decl (get_identifier (field_name),
2082 gnu_index_base_type,
2083 gnu_template_type, NULL_TREE,
2084 NULL_TREE, 0, 0);
2085 Sloc_to_locus (Sloc (gnat_entity),
2086 &DECL_SOURCE_LOCATION (gnu_hb_field));
2088 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2090 /* We can't use build_component_ref here since the template type
2091 isn't complete yet. */
2092 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2093 gnu_template_reference, gnu_lb_field,
2094 NULL_TREE);
2095 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2096 gnu_template_reference, gnu_hb_field,
2097 NULL_TREE);
2098 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2100 gnu_min = convert (sizetype, gnu_orig_min);
2101 gnu_max = convert (sizetype, gnu_orig_max);
2103 /* Compute the size of this dimension. See the E_Array_Subtype
2104 case below for the rationale. */
2105 gnu_high
2106 = build3 (COND_EXPR, sizetype,
2107 build2 (GE_EXPR, boolean_type_node,
2108 gnu_orig_max, gnu_orig_min),
2109 gnu_max,
2110 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2112 /* Make a range type with the new range in the Ada base type.
2113 Then make an index type with the size range in sizetype. */
2114 gnu_index_types[index]
2115 = create_index_type (gnu_min, gnu_high,
2116 create_range_type (gnu_index_base_type,
2117 gnu_orig_min,
2118 gnu_orig_max),
2119 gnat_entity);
2121 /* Update the maximum size of the array in elements. */
2122 if (gnu_max_size)
2124 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2125 tree gnu_min
2126 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2127 tree gnu_max
2128 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2129 tree gnu_this_max
2130 = size_binop (PLUS_EXPR, size_one_node,
2131 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2133 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2134 && TREE_OVERFLOW (gnu_this_max))
2135 gnu_max_size = NULL_TREE;
2136 else
2137 gnu_max_size
2138 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2141 TYPE_NAME (gnu_index_types[index])
2142 = create_concat_name (gnat_entity, field_name);
2145 /* Install all the fields into the template. */
2146 TYPE_NAME (gnu_template_type)
2147 = create_concat_name (gnat_entity, "XUB");
2148 gnu_template_fields = NULL_TREE;
2149 for (index = 0; index < ndim; index++)
2150 gnu_template_fields
2151 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2152 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2153 debug_info_p);
2154 TYPE_READONLY (gnu_template_type) = 1;
2156 /* If Component_Size is not already specified, annotate it with the
2157 size of the component. */
2158 if (Unknown_Component_Size (gnat_entity))
2159 Set_Component_Size (gnat_entity,
2160 annotate_value (TYPE_SIZE (comp_type)));
2162 /* Compute the maximum size of the array in units and bits. */
2163 if (gnu_max_size)
2165 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2166 TYPE_SIZE_UNIT (comp_type));
2167 gnu_max_size = size_binop (MULT_EXPR,
2168 convert (bitsizetype, gnu_max_size),
2169 TYPE_SIZE (comp_type));
2171 else
2172 gnu_max_size_unit = NULL_TREE;
2174 /* Now build the array type. */
2175 tem = comp_type;
2176 for (index = ndim - 1; index >= 0; index--)
2178 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2179 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2180 sorry ("non-default Scalar_Storage_Order");
2181 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2182 if (array_type_has_nonaliased_component (tem, gnat_entity))
2183 TYPE_NONALIASED_COMPONENT (tem) = 1;
2185 /* If it is passed by reference, force BLKmode to ensure that
2186 objects of this type will always be put in memory. */
2187 if (TYPE_MODE (tem) != BLKmode
2188 && Is_By_Reference_Type (gnat_entity))
2189 SET_TYPE_MODE (tem, BLKmode);
2192 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2194 /* If an alignment is specified, use it if valid. But ignore it
2195 for the original type of packed array types. If the alignment
2196 was requested with an explicit alignment clause, state so. */
2197 if (No (Packed_Array_Impl_Type (gnat_entity))
2198 && Known_Alignment (gnat_entity))
2200 TYPE_ALIGN (tem)
2201 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2202 TYPE_ALIGN (tem));
2203 if (Present (Alignment_Clause (gnat_entity)))
2204 TYPE_USER_ALIGN (tem) = 1;
2207 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2209 /* Adjust the type of the pointer-to-array field of the fat pointer
2210 and record the aliasing relationships if necessary. */
2211 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2212 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2213 record_component_aliases (gnu_fat_type);
2215 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2216 corresponding fat pointer. */
2217 TREE_TYPE (gnu_type) = gnu_fat_type;
2218 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2219 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2220 SET_TYPE_MODE (gnu_type, BLKmode);
2221 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2223 /* If the maximum size doesn't overflow, use it. */
2224 if (gnu_max_size
2225 && TREE_CODE (gnu_max_size) == INTEGER_CST
2226 && !TREE_OVERFLOW (gnu_max_size)
2227 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2228 && !TREE_OVERFLOW (gnu_max_size_unit))
2230 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2231 TYPE_SIZE (tem));
2232 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2233 TYPE_SIZE_UNIT (tem));
2236 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2237 !Comes_From_Source (gnat_entity), debug_info_p,
2238 gnat_entity);
2240 /* Give the fat pointer type a name. If this is a packed array, tell
2241 the debugger how to interpret the underlying bits. */
2242 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2243 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2244 else
2245 gnat_name = gnat_entity;
2246 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2247 !Comes_From_Source (gnat_entity), debug_info_p,
2248 gnat_entity);
2250 /* Create the type to be designated by thin pointers: a record type for
2251 the array and its template. We used to shift the fields to have the
2252 template at a negative offset, but this was somewhat of a kludge; we
2253 now shift thin pointer values explicitly but only those which have a
2254 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2255 tem = build_unc_object_type (gnu_template_type, tem,
2256 create_concat_name (gnat_name, "XUT"),
2257 debug_info_p);
2259 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2260 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2262 break;
2264 case E_Array_Subtype:
2266 /* This is the actual data type for array variables. Multidimensional
2267 arrays are implemented as arrays of arrays. Note that arrays which
2268 have sparse enumeration subtypes as index components create sparse
2269 arrays, which is obviously space inefficient but so much easier to
2270 code for now.
2272 Also note that the subtype never refers to the unconstrained array
2273 type, which is somewhat at variance with Ada semantics.
2275 First check to see if this is simply a renaming of the array type.
2276 If so, the result is the array type. */
2278 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2279 if (!Is_Constrained (gnat_entity))
2281 else
2283 Entity_Id gnat_index, gnat_base_index;
2284 const bool convention_fortran_p
2285 = (Convention (gnat_entity) == Convention_Fortran);
2286 const int ndim = Number_Dimensions (gnat_entity);
2287 tree gnu_base_type = gnu_type;
2288 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2289 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2290 bool need_index_type_struct = false;
2291 int index;
2293 /* First create the GCC type for each index and find out whether
2294 special types are needed for debugging information. */
2295 for (index = (convention_fortran_p ? ndim - 1 : 0),
2296 gnat_index = First_Index (gnat_entity),
2297 gnat_base_index
2298 = First_Index (Implementation_Base_Type (gnat_entity));
2299 0 <= index && index < ndim;
2300 index += (convention_fortran_p ? - 1 : 1),
2301 gnat_index = Next_Index (gnat_index),
2302 gnat_base_index = Next_Index (gnat_base_index))
2304 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2305 tree gnu_index_base_type = get_base_type (gnu_index_type);
2306 tree gnu_orig_min
2307 = convert (gnu_index_base_type,
2308 TYPE_MIN_VALUE (gnu_index_type));
2309 tree gnu_orig_max
2310 = convert (gnu_index_base_type,
2311 TYPE_MAX_VALUE (gnu_index_type));
2312 tree gnu_min = convert (sizetype, gnu_orig_min);
2313 tree gnu_max = convert (sizetype, gnu_orig_max);
2314 tree gnu_base_index_type
2315 = get_unpadded_type (Etype (gnat_base_index));
2316 tree gnu_base_index_base_type
2317 = get_base_type (gnu_base_index_type);
2318 tree gnu_base_orig_min
2319 = convert (gnu_base_index_base_type,
2320 TYPE_MIN_VALUE (gnu_base_index_type));
2321 tree gnu_base_orig_max
2322 = convert (gnu_base_index_base_type,
2323 TYPE_MAX_VALUE (gnu_base_index_type));
2324 tree gnu_high;
2326 /* See if the base array type is already flat. If it is, we
2327 are probably compiling an ACATS test but it will cause the
2328 code below to malfunction if we don't handle it specially. */
2329 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2330 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2331 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2333 gnu_min = size_one_node;
2334 gnu_max = size_zero_node;
2335 gnu_high = gnu_max;
2338 /* Similarly, if one of the values overflows in sizetype and the
2339 range is null, use 1..0 for the sizetype bounds. */
2340 else if (TREE_CODE (gnu_min) == INTEGER_CST
2341 && TREE_CODE (gnu_max) == INTEGER_CST
2342 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2343 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2345 gnu_min = size_one_node;
2346 gnu_max = size_zero_node;
2347 gnu_high = gnu_max;
2350 /* If the minimum and maximum values both overflow in sizetype,
2351 but the difference in the original type does not overflow in
2352 sizetype, ignore the overflow indication. */
2353 else if (TREE_CODE (gnu_min) == INTEGER_CST
2354 && TREE_CODE (gnu_max) == INTEGER_CST
2355 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2356 && !TREE_OVERFLOW
2357 (convert (sizetype,
2358 fold_build2 (MINUS_EXPR, gnu_index_type,
2359 gnu_orig_max,
2360 gnu_orig_min))))
2362 TREE_OVERFLOW (gnu_min) = 0;
2363 TREE_OVERFLOW (gnu_max) = 0;
2364 gnu_high = gnu_max;
2367 /* Compute the size of this dimension in the general case. We
2368 need to provide GCC with an upper bound to use but have to
2369 deal with the "superflat" case. There are three ways to do
2370 this. If we can prove that the array can never be superflat,
2371 we can just use the high bound of the index type. */
2372 else if ((Nkind (gnat_index) == N_Range
2373 && cannot_be_superflat_p (gnat_index))
2374 /* Bit-Packed Array Impl. Types are never superflat. */
2375 || (Is_Packed_Array_Impl_Type (gnat_entity)
2376 && Is_Bit_Packed_Array
2377 (Original_Array_Type (gnat_entity))))
2378 gnu_high = gnu_max;
2380 /* Otherwise, if the high bound is constant but the low bound is
2381 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2382 lower bound. Note that the comparison must be done in the
2383 original type to avoid any overflow during the conversion. */
2384 else if (TREE_CODE (gnu_max) == INTEGER_CST
2385 && TREE_CODE (gnu_min) != INTEGER_CST)
2387 gnu_high = gnu_max;
2388 gnu_min
2389 = build_cond_expr (sizetype,
2390 build_binary_op (GE_EXPR,
2391 boolean_type_node,
2392 gnu_orig_max,
2393 gnu_orig_min),
2394 gnu_min,
2395 int_const_binop (PLUS_EXPR, gnu_max,
2396 size_one_node));
2399 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2400 in all the other cases. Note that, here as well as above,
2401 the condition used in the comparison must be equivalent to
2402 the condition (length != 0). This is relied upon in order
2403 to optimize array comparisons in compare_arrays. Moreover
2404 we use int_const_binop for the shift by 1 if the bound is
2405 constant to avoid any unwanted overflow. */
2406 else
2407 gnu_high
2408 = build_cond_expr (sizetype,
2409 build_binary_op (GE_EXPR,
2410 boolean_type_node,
2411 gnu_orig_max,
2412 gnu_orig_min),
2413 gnu_max,
2414 TREE_CODE (gnu_min) == INTEGER_CST
2415 ? int_const_binop (MINUS_EXPR, gnu_min,
2416 size_one_node)
2417 : size_binop (MINUS_EXPR, gnu_min,
2418 size_one_node));
2420 /* Reuse the index type for the range type. Then make an index
2421 type with the size range in sizetype. */
2422 gnu_index_types[index]
2423 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2424 gnat_entity);
2426 /* Update the maximum size of the array in elements. Here we
2427 see if any constraint on the index type of the base type
2428 can be used in the case of self-referential bound on the
2429 index type of the subtype. We look for a non-"infinite"
2430 and non-self-referential bound from any type involved and
2431 handle each bound separately. */
2432 if (gnu_max_size)
2434 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2435 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2436 tree gnu_base_index_base_type
2437 = get_base_type (gnu_base_index_type);
2438 tree gnu_base_base_min
2439 = convert (sizetype,
2440 TYPE_MIN_VALUE (gnu_base_index_base_type));
2441 tree gnu_base_base_max
2442 = convert (sizetype,
2443 TYPE_MAX_VALUE (gnu_base_index_base_type));
2445 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2446 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2447 && !TREE_OVERFLOW (gnu_base_min)))
2448 gnu_base_min = gnu_min;
2450 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2451 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2452 && !TREE_OVERFLOW (gnu_base_max)))
2453 gnu_base_max = gnu_max;
2455 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2456 && TREE_OVERFLOW (gnu_base_min))
2457 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2458 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2459 && TREE_OVERFLOW (gnu_base_max))
2460 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2461 gnu_max_size = NULL_TREE;
2462 else
2464 tree gnu_this_max;
2466 /* Use int_const_binop if the bounds are constant to
2467 avoid any unwanted overflow. */
2468 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2469 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2470 gnu_this_max
2471 = int_const_binop (PLUS_EXPR, size_one_node,
2472 int_const_binop (MINUS_EXPR,
2473 gnu_base_max,
2474 gnu_base_min));
2475 else
2476 gnu_this_max
2477 = size_binop (PLUS_EXPR, size_one_node,
2478 size_binop (MINUS_EXPR,
2479 gnu_base_max,
2480 gnu_base_min));
2482 gnu_max_size
2483 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2487 /* We need special types for debugging information to point to
2488 the index types if they have variable bounds, are not integer
2489 types or are biased. */
2490 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2491 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2492 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2493 || (TREE_TYPE (gnu_index_type)
2494 && TREE_CODE (TREE_TYPE (gnu_index_type))
2495 != INTEGER_TYPE)
2496 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2497 need_index_type_struct = true;
2500 /* Then flatten: create the array of arrays. For an array type
2501 used to implement a packed array, get the component type from
2502 the original array type since the representation clauses that
2503 can affect it are on the latter. */
2504 if (Is_Packed_Array_Impl_Type (gnat_entity)
2505 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2507 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2508 for (index = ndim - 1; index >= 0; index--)
2509 gnu_type = TREE_TYPE (gnu_type);
2511 /* One of the above calls might have caused us to be elaborated,
2512 so don't blow up if so. */
2513 if (present_gnu_tree (gnat_entity))
2515 maybe_present = true;
2516 break;
2519 else
2521 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2522 debug_info_p);
2524 /* One of the above calls might have caused us to be elaborated,
2525 so don't blow up if so. */
2526 if (present_gnu_tree (gnat_entity))
2528 maybe_present = true;
2529 break;
2533 /* Compute the maximum size of the array in units and bits. */
2534 if (gnu_max_size)
2536 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2537 TYPE_SIZE_UNIT (gnu_type));
2538 gnu_max_size = size_binop (MULT_EXPR,
2539 convert (bitsizetype, gnu_max_size),
2540 TYPE_SIZE (gnu_type));
2542 else
2543 gnu_max_size_unit = NULL_TREE;
2545 /* Now build the array type. */
2546 for (index = ndim - 1; index >= 0; index --)
2548 gnu_type = build_nonshared_array_type (gnu_type,
2549 gnu_index_types[index]);
2550 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2551 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2552 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2554 /* See the E_Array_Type case for the rationale. */
2555 if (TYPE_MODE (gnu_type) != BLKmode
2556 && Is_By_Reference_Type (gnat_entity))
2557 SET_TYPE_MODE (gnu_type, BLKmode);
2560 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2562 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2563 TYPE_STUB_DECL (gnu_type)
2564 = create_type_stub_decl (gnu_entity_name, gnu_type);
2566 /* If we are at file level and this is a multi-dimensional array,
2567 we need to make a variable corresponding to the stride of the
2568 inner dimensions. */
2569 if (global_bindings_p () && ndim > 1)
2571 tree gnu_st_name = get_identifier ("ST");
2572 tree gnu_arr_type;
2574 for (gnu_arr_type = TREE_TYPE (gnu_type);
2575 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2576 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2577 gnu_st_name = concat_name (gnu_st_name, "ST"))
2579 tree eltype = TREE_TYPE (gnu_arr_type);
2581 TYPE_SIZE (gnu_arr_type)
2582 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2583 gnat_entity, gnu_st_name,
2584 definition, false);
2586 /* ??? For now, store the size as a multiple of the
2587 alignment of the element type in bytes so that we
2588 can see the alignment from the tree. */
2589 TYPE_SIZE_UNIT (gnu_arr_type)
2590 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2591 gnat_entity,
2592 concat_name (gnu_st_name, "A_U"),
2593 definition, false,
2594 TYPE_ALIGN (eltype));
2596 /* ??? create_type_decl is not invoked on the inner types so
2597 the MULT_EXPR node built above will never be marked. */
2598 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2602 /* If we need to write out a record type giving the names of the
2603 bounds for debugging purposes, do it now and make the record
2604 type a parallel type. This is not needed for a packed array
2605 since the bounds are conveyed by the original array type. */
2606 if (need_index_type_struct
2607 && debug_info_p
2608 && !Is_Packed_Array_Impl_Type (gnat_entity))
2610 tree gnu_bound_rec = make_node (RECORD_TYPE);
2611 tree gnu_field_list = NULL_TREE;
2612 tree gnu_field;
2614 TYPE_NAME (gnu_bound_rec)
2615 = create_concat_name (gnat_entity, "XA");
2617 for (index = ndim - 1; index >= 0; index--)
2619 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2620 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2622 /* Make sure to reference the types themselves, and not just
2623 their names, as the debugger may fall back on them. */
2624 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2625 gnu_bound_rec, NULL_TREE,
2626 NULL_TREE, 0, 0);
2627 DECL_CHAIN (gnu_field) = gnu_field_list;
2628 gnu_field_list = gnu_field;
2631 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2632 add_parallel_type (gnu_type, gnu_bound_rec);
2635 /* If this is a packed array type, make the original array type a
2636 parallel type. Otherwise, do it for the base array type if it
2637 isn't artificial to make sure it is kept in the debug info. */
2638 if (debug_info_p)
2640 if (Is_Packed_Array_Impl_Type (gnat_entity))
2641 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2642 else
2644 tree gnu_base_decl
2645 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2646 if (!DECL_ARTIFICIAL (gnu_base_decl))
2647 add_parallel_type (gnu_type,
2648 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2652 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2653 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2654 = (Is_Packed_Array_Impl_Type (gnat_entity)
2655 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2657 /* If the size is self-referential and the maximum size doesn't
2658 overflow, use it. */
2659 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2660 && gnu_max_size
2661 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2662 && TREE_OVERFLOW (gnu_max_size))
2663 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2664 && TREE_OVERFLOW (gnu_max_size_unit)))
2666 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2667 TYPE_SIZE (gnu_type));
2668 TYPE_SIZE_UNIT (gnu_type)
2669 = size_binop (MIN_EXPR, gnu_max_size_unit,
2670 TYPE_SIZE_UNIT (gnu_type));
2673 /* Set our alias set to that of our base type. This gives all
2674 array subtypes the same alias set. */
2675 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2677 /* If this is a packed type, make this type the same as the packed
2678 array type, but do some adjusting in the type first. */
2679 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2681 Entity_Id gnat_index;
2682 tree gnu_inner;
2684 /* First finish the type we had been making so that we output
2685 debugging information for it. */
2686 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2687 if (Treat_As_Volatile (gnat_entity))
2688 gnu_type
2689 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2690 /* Make it artificial only if the base type was artificial too.
2691 That's sort of "morally" true and will make it possible for
2692 the debugger to look it up by name in DWARF, which is needed
2693 in order to decode the packed array type. */
2694 gnu_decl
2695 = create_type_decl (gnu_entity_name, gnu_type,
2696 !Comes_From_Source (Etype (gnat_entity))
2697 && !Comes_From_Source (gnat_entity),
2698 debug_info_p, gnat_entity);
2700 /* Save it as our equivalent in case the call below elaborates
2701 this type again. */
2702 save_gnu_tree (gnat_entity, gnu_decl, false);
2704 gnu_decl
2705 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2706 NULL_TREE, 0);
2707 this_made_decl = true;
2708 gnu_type = TREE_TYPE (gnu_decl);
2709 save_gnu_tree (gnat_entity, NULL_TREE, false);
2711 gnu_inner = gnu_type;
2712 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2713 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2714 || TYPE_PADDING_P (gnu_inner)))
2715 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2717 /* We need to attach the index type to the type we just made so
2718 that the actual bounds can later be put into a template. */
2719 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2720 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2721 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2722 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2724 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2726 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2727 TYPE_MODULUS for modular types so we make an extra
2728 subtype if necessary. */
2729 if (TYPE_MODULAR_P (gnu_inner))
2731 tree gnu_subtype
2732 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2733 TREE_TYPE (gnu_subtype) = gnu_inner;
2734 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2735 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2736 TYPE_MIN_VALUE (gnu_inner));
2737 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2738 TYPE_MAX_VALUE (gnu_inner));
2739 gnu_inner = gnu_subtype;
2742 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2744 #ifdef ENABLE_CHECKING
2745 /* Check for other cases of overloading. */
2746 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2747 #endif
2750 for (gnat_index = First_Index (gnat_entity);
2751 Present (gnat_index);
2752 gnat_index = Next_Index (gnat_index))
2753 SET_TYPE_ACTUAL_BOUNDS
2754 (gnu_inner,
2755 tree_cons (NULL_TREE,
2756 get_unpadded_type (Etype (gnat_index)),
2757 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2759 if (Convention (gnat_entity) != Convention_Fortran)
2760 SET_TYPE_ACTUAL_BOUNDS
2761 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2763 if (TREE_CODE (gnu_type) == RECORD_TYPE
2764 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2765 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2769 else
2770 /* Abort if packed array with no Packed_Array_Impl_Type. */
2771 gcc_assert (!Is_Packed (gnat_entity));
2773 break;
2775 case E_String_Literal_Subtype:
2776 /* Create the type for a string literal. */
2778 Entity_Id gnat_full_type
2779 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2780 && Present (Full_View (Etype (gnat_entity)))
2781 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2782 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2783 tree gnu_string_array_type
2784 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2785 tree gnu_string_index_type
2786 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2787 (TYPE_DOMAIN (gnu_string_array_type))));
2788 tree gnu_lower_bound
2789 = convert (gnu_string_index_type,
2790 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2791 tree gnu_length
2792 = UI_To_gnu (String_Literal_Length (gnat_entity),
2793 gnu_string_index_type);
2794 tree gnu_upper_bound
2795 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2796 gnu_lower_bound,
2797 int_const_binop (MINUS_EXPR, gnu_length,
2798 convert (gnu_string_index_type,
2799 integer_one_node)));
2800 tree gnu_index_type
2801 = create_index_type (convert (sizetype, gnu_lower_bound),
2802 convert (sizetype, gnu_upper_bound),
2803 create_range_type (gnu_string_index_type,
2804 gnu_lower_bound,
2805 gnu_upper_bound),
2806 gnat_entity);
2808 gnu_type
2809 = build_nonshared_array_type (gnat_to_gnu_type
2810 (Component_Type (gnat_entity)),
2811 gnu_index_type);
2812 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2813 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2814 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2816 break;
2818 /* Record Types and Subtypes
2820 The following fields are defined on record types:
2822 Has_Discriminants True if the record has discriminants
2823 First_Discriminant Points to head of list of discriminants
2824 First_Entity Points to head of list of fields
2825 Is_Tagged_Type True if the record is tagged
2827 Implementation of Ada records and discriminated records:
2829 A record type definition is transformed into the equivalent of a C
2830 struct definition. The fields that are the discriminants which are
2831 found in the Full_Type_Declaration node and the elements of the
2832 Component_List found in the Record_Type_Definition node. The
2833 Component_List can be a recursive structure since each Variant of
2834 the Variant_Part of the Component_List has a Component_List.
2836 Processing of a record type definition comprises starting the list of
2837 field declarations here from the discriminants and the calling the
2838 function components_to_record to add the rest of the fields from the
2839 component list and return the gnu type node. The function
2840 components_to_record will call itself recursively as it traverses
2841 the tree. */
2843 case E_Record_Type:
2844 if (Has_Complex_Representation (gnat_entity))
2846 gnu_type
2847 = build_complex_type
2848 (get_unpadded_type
2849 (Etype (Defining_Entity
2850 (First (Component_Items
2851 (Component_List
2852 (Type_Definition
2853 (Declaration_Node (gnat_entity)))))))));
2855 break;
2859 Node_Id full_definition = Declaration_Node (gnat_entity);
2860 Node_Id record_definition = Type_Definition (full_definition);
2861 Node_Id gnat_constr;
2862 Entity_Id gnat_field;
2863 tree gnu_field, gnu_field_list = NULL_TREE;
2864 tree gnu_get_parent;
2865 /* Set PACKED in keeping with gnat_to_gnu_field. */
2866 const int packed
2867 = Is_Packed (gnat_entity)
2869 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2870 ? -1
2871 : (Known_Alignment (gnat_entity)
2872 || (Strict_Alignment (gnat_entity)
2873 && Known_RM_Size (gnat_entity)))
2874 ? -2
2875 : 0;
2876 const bool has_discr = Has_Discriminants (gnat_entity);
2877 const bool has_rep = Has_Specified_Layout (gnat_entity);
2878 const bool is_extension
2879 = (Is_Tagged_Type (gnat_entity)
2880 && Nkind (record_definition) == N_Derived_Type_Definition);
2881 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2882 bool all_rep = has_rep;
2884 /* See if all fields have a rep clause. Stop when we find one
2885 that doesn't. */
2886 if (all_rep)
2887 for (gnat_field = First_Entity (gnat_entity);
2888 Present (gnat_field);
2889 gnat_field = Next_Entity (gnat_field))
2890 if ((Ekind (gnat_field) == E_Component
2891 || Ekind (gnat_field) == E_Discriminant)
2892 && No (Component_Clause (gnat_field)))
2894 all_rep = false;
2895 break;
2898 /* If this is a record extension, go a level further to find the
2899 record definition. Also, verify we have a Parent_Subtype. */
2900 if (is_extension)
2902 if (!type_annotate_only
2903 || Present (Record_Extension_Part (record_definition)))
2904 record_definition = Record_Extension_Part (record_definition);
2906 gcc_assert (type_annotate_only
2907 || Present (Parent_Subtype (gnat_entity)));
2910 /* Make a node for the record. If we are not defining the record,
2911 suppress expanding incomplete types. */
2912 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2913 TYPE_NAME (gnu_type) = gnu_entity_name;
2914 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2915 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2916 sorry ("non-default Scalar_Storage_Order");
2917 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2919 if (!definition)
2921 defer_incomplete_level++;
2922 this_deferred = true;
2925 /* If both a size and rep clause was specified, put the size in
2926 the record type now so that it can get the proper mode. */
2927 if (has_rep && Known_RM_Size (gnat_entity))
2928 TYPE_SIZE (gnu_type)
2929 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2931 /* Always set the alignment here so that it can be used to
2932 set the mode, if it is making the alignment stricter. If
2933 it is invalid, it will be checked again below. If this is to
2934 be Atomic, choose a default alignment of a word unless we know
2935 the size and it's smaller. */
2936 if (Known_Alignment (gnat_entity))
2937 TYPE_ALIGN (gnu_type)
2938 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2939 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2941 unsigned int size = UI_To_Int (Esize (gnat_entity));
2942 TYPE_ALIGN (gnu_type)
2943 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2945 /* If a type needs strict alignment, the minimum size will be the
2946 type size instead of the RM size (see validate_size). Cap the
2947 alignment, lest it causes this type size to become too large. */
2948 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2950 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2951 unsigned int raw_align = raw_size & -raw_size;
2952 if (raw_align < BIGGEST_ALIGNMENT)
2953 TYPE_ALIGN (gnu_type) = raw_align;
2955 else
2956 TYPE_ALIGN (gnu_type) = 0;
2958 /* If we have a Parent_Subtype, make a field for the parent. If
2959 this record has rep clauses, force the position to zero. */
2960 if (Present (Parent_Subtype (gnat_entity)))
2962 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2963 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2964 tree gnu_parent;
2966 /* A major complexity here is that the parent subtype will
2967 reference our discriminants in its Stored_Constraint list.
2968 But those must reference the parent component of this record
2969 which is precisely of the parent subtype we have not built yet!
2970 To break the circle we first build a dummy COMPONENT_REF which
2971 represents the "get to the parent" operation and initialize
2972 each of those discriminants to a COMPONENT_REF of the above
2973 dummy parent referencing the corresponding discriminant of the
2974 base type of the parent subtype. */
2975 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
2976 build0 (PLACEHOLDER_EXPR, gnu_type),
2977 build_decl (input_location,
2978 FIELD_DECL, NULL_TREE,
2979 gnu_dummy_parent_type),
2980 NULL_TREE);
2982 if (has_discr)
2983 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2984 Present (gnat_field);
2985 gnat_field = Next_Stored_Discriminant (gnat_field))
2986 if (Present (Corresponding_Discriminant (gnat_field)))
2988 tree gnu_field
2989 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2990 (gnat_field));
2991 save_gnu_tree
2992 (gnat_field,
2993 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2994 gnu_get_parent, gnu_field, NULL_TREE),
2995 true);
2998 /* Then we build the parent subtype. If it has discriminants but
2999 the type itself has unknown discriminants, this means that it
3000 doesn't contain information about how the discriminants are
3001 derived from those of the ancestor type, so it cannot be used
3002 directly. Instead it is built by cloning the parent subtype
3003 of the underlying record view of the type, for which the above
3004 derivation of discriminants has been made explicit. */
3005 if (Has_Discriminants (gnat_parent)
3006 && Has_Unknown_Discriminants (gnat_entity))
3008 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3010 /* If we are defining the type, the underlying record
3011 view must already have been elaborated at this point.
3012 Otherwise do it now as its parent subtype cannot be
3013 technically elaborated on its own. */
3014 if (definition)
3015 gcc_assert (present_gnu_tree (gnat_uview));
3016 else
3017 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3019 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3021 /* Substitute the "get to the parent" of the type for that
3022 of its underlying record view in the cloned type. */
3023 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3024 Present (gnat_field);
3025 gnat_field = Next_Stored_Discriminant (gnat_field))
3026 if (Present (Corresponding_Discriminant (gnat_field)))
3028 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3029 tree gnu_ref
3030 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3031 gnu_get_parent, gnu_field, NULL_TREE);
3032 gnu_parent
3033 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3036 else
3037 gnu_parent = gnat_to_gnu_type (gnat_parent);
3039 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3040 initially built. The discriminants must reference the fields
3041 of the parent subtype and not those of its base type for the
3042 placeholder machinery to properly work. */
3043 if (has_discr)
3045 /* The actual parent subtype is the full view. */
3046 if (IN (Ekind (gnat_parent), Private_Kind))
3048 if (Present (Full_View (gnat_parent)))
3049 gnat_parent = Full_View (gnat_parent);
3050 else
3051 gnat_parent = Underlying_Full_View (gnat_parent);
3054 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3055 Present (gnat_field);
3056 gnat_field = Next_Stored_Discriminant (gnat_field))
3057 if (Present (Corresponding_Discriminant (gnat_field)))
3059 Entity_Id field = Empty;
3060 for (field = First_Stored_Discriminant (gnat_parent);
3061 Present (field);
3062 field = Next_Stored_Discriminant (field))
3063 if (same_discriminant_p (gnat_field, field))
3064 break;
3065 gcc_assert (Present (field));
3066 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3067 = gnat_to_gnu_field_decl (field);
3071 /* The "get to the parent" COMPONENT_REF must be given its
3072 proper type... */
3073 TREE_TYPE (gnu_get_parent) = gnu_parent;
3075 /* ...and reference the _Parent field of this record. */
3076 gnu_field
3077 = create_field_decl (parent_name_id,
3078 gnu_parent, gnu_type,
3079 has_rep
3080 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3081 has_rep
3082 ? bitsize_zero_node : NULL_TREE,
3083 0, 1);
3084 DECL_INTERNAL_P (gnu_field) = 1;
3085 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3086 TYPE_FIELDS (gnu_type) = gnu_field;
3089 /* Make the fields for the discriminants and put them into the record
3090 unless it's an Unchecked_Union. */
3091 if (has_discr)
3092 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3093 Present (gnat_field);
3094 gnat_field = Next_Stored_Discriminant (gnat_field))
3096 /* If this is a record extension and this discriminant is the
3097 renaming of another discriminant, we've handled it above. */
3098 if (Present (Parent_Subtype (gnat_entity))
3099 && Present (Corresponding_Discriminant (gnat_field)))
3100 continue;
3102 gnu_field
3103 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3104 debug_info_p);
3106 /* Make an expression using a PLACEHOLDER_EXPR from the
3107 FIELD_DECL node just created and link that with the
3108 corresponding GNAT defining identifier. */
3109 save_gnu_tree (gnat_field,
3110 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3111 build0 (PLACEHOLDER_EXPR, gnu_type),
3112 gnu_field, NULL_TREE),
3113 true);
3115 if (!is_unchecked_union)
3117 DECL_CHAIN (gnu_field) = gnu_field_list;
3118 gnu_field_list = gnu_field;
3122 /* If we have a derived untagged type that renames discriminants in
3123 the root type, the (stored) discriminants are a just copy of the
3124 discriminants of the root type. This means that any constraints
3125 added by the renaming in the derivation are disregarded as far
3126 as the layout of the derived type is concerned. To rescue them,
3127 we change the type of the (stored) discriminants to a subtype
3128 with the bounds of the type of the visible discriminants. */
3129 if (has_discr
3130 && !is_extension
3131 && Stored_Constraint (gnat_entity) != No_Elist)
3132 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3133 gnat_constr != No_Elmt;
3134 gnat_constr = Next_Elmt (gnat_constr))
3135 if (Nkind (Node (gnat_constr)) == N_Identifier
3136 /* Ignore access discriminants. */
3137 && !Is_Access_Type (Etype (Node (gnat_constr)))
3138 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3140 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3141 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3142 tree gnu_ref
3143 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3144 NULL_TREE, 0);
3146 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3147 just above for one of the stored discriminants. */
3148 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3150 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3152 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3153 tree gnu_subtype
3154 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3155 ? make_unsigned_type (prec) : make_signed_type (prec);
3156 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3157 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3158 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3159 TYPE_MIN_VALUE (gnu_discr_type));
3160 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3161 TYPE_MAX_VALUE (gnu_discr_type));
3162 TREE_TYPE (gnu_ref)
3163 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3167 /* Add the fields into the record type and finish it up. */
3168 components_to_record (gnu_type, Component_List (record_definition),
3169 gnu_field_list, packed, definition, false,
3170 all_rep, is_unchecked_union,
3171 !Comes_From_Source (gnat_entity), debug_info_p,
3172 false, OK_To_Reorder_Components (gnat_entity),
3173 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3175 /* If it is passed by reference, force BLKmode to ensure that objects
3176 of this type will always be put in memory. */
3177 if (TYPE_MODE (gnu_type) != BLKmode
3178 && Is_By_Reference_Type (gnat_entity))
3179 SET_TYPE_MODE (gnu_type, BLKmode);
3181 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3183 /* Fill in locations of fields. */
3184 annotate_rep (gnat_entity, gnu_type);
3186 /* If there are any entities in the chain corresponding to components
3187 that we did not elaborate, ensure we elaborate their types if they
3188 are Itypes. */
3189 for (gnat_temp = First_Entity (gnat_entity);
3190 Present (gnat_temp);
3191 gnat_temp = Next_Entity (gnat_temp))
3192 if ((Ekind (gnat_temp) == E_Component
3193 || Ekind (gnat_temp) == E_Discriminant)
3194 && Is_Itype (Etype (gnat_temp))
3195 && !present_gnu_tree (gnat_temp))
3196 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3198 /* If this is a record type associated with an exception definition,
3199 equate its fields to those of the standard exception type. This
3200 will make it possible to convert between them. */
3201 if (gnu_entity_name == exception_data_name_id)
3203 tree gnu_std_field;
3204 for (gnu_field = TYPE_FIELDS (gnu_type),
3205 gnu_std_field = TYPE_FIELDS (except_type_node);
3206 gnu_field;
3207 gnu_field = DECL_CHAIN (gnu_field),
3208 gnu_std_field = DECL_CHAIN (gnu_std_field))
3209 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3210 gcc_assert (!gnu_std_field);
3213 break;
3215 case E_Class_Wide_Subtype:
3216 /* If an equivalent type is present, that is what we should use.
3217 Otherwise, fall through to handle this like a record subtype
3218 since it may have constraints. */
3219 if (gnat_equiv_type != gnat_entity)
3221 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3222 maybe_present = true;
3223 break;
3226 /* ... fall through ... */
3228 case E_Record_Subtype:
3229 /* If Cloned_Subtype is Present it means this record subtype has
3230 identical layout to that type or subtype and we should use
3231 that GCC type for this one. The front end guarantees that
3232 the component list is shared. */
3233 if (Present (Cloned_Subtype (gnat_entity)))
3235 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3236 NULL_TREE, 0);
3237 maybe_present = true;
3238 break;
3241 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3242 changing the type, make a new type with each field having the type of
3243 the field in the new subtype but the position computed by transforming
3244 every discriminant reference according to the constraints. We don't
3245 see any difference between private and non-private type here since
3246 derivations from types should have been deferred until the completion
3247 of the private type. */
3248 else
3250 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3251 tree gnu_base_type;
3253 if (!definition)
3255 defer_incomplete_level++;
3256 this_deferred = true;
3259 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3261 if (present_gnu_tree (gnat_entity))
3263 maybe_present = true;
3264 break;
3267 /* If this is a record subtype associated with a dispatch table,
3268 strip the suffix. This is necessary to make sure 2 different
3269 subtypes associated with the imported and exported views of a
3270 dispatch table are properly merged in LTO mode. */
3271 if (Is_Dispatch_Table_Entity (gnat_entity))
3273 char *p;
3274 Get_Encoded_Name (gnat_entity);
3275 p = strchr (Name_Buffer, '_');
3276 gcc_assert (p);
3277 strcpy (p+2, "dtS");
3278 gnu_entity_name = get_identifier (Name_Buffer);
3281 /* When the subtype has discriminants and these discriminants affect
3282 the initial shape it has inherited, factor them in. But for an
3283 Unchecked_Union (it must be an Itype), just return the type.
3284 We can't just test Is_Constrained because private subtypes without
3285 discriminants of types with discriminants with default expressions
3286 are Is_Constrained but aren't constrained! */
3287 if (IN (Ekind (gnat_base_type), Record_Kind)
3288 && !Is_Unchecked_Union (gnat_base_type)
3289 && !Is_For_Access_Subtype (gnat_entity)
3290 && Has_Discriminants (gnat_entity)
3291 && Is_Constrained (gnat_entity)
3292 && Stored_Constraint (gnat_entity) != No_Elist)
3294 vec<subst_pair> gnu_subst_list
3295 = build_subst_list (gnat_entity, gnat_base_type, definition);
3296 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3297 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3298 bool selected_variant = false, all_constant_pos = true;
3299 Entity_Id gnat_field;
3300 vec<variant_desc> gnu_variant_list;
3302 gnu_type = make_node (RECORD_TYPE);
3303 TYPE_NAME (gnu_type) = gnu_entity_name;
3304 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3305 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3307 /* Set the size, alignment and alias set of the new type to
3308 match that of the old one, doing required substitutions. */
3309 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3310 gnu_subst_list);
3312 if (TYPE_IS_PADDING_P (gnu_base_type))
3313 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3314 else
3315 gnu_unpad_base_type = gnu_base_type;
3317 /* Look for REP and variant parts in the base type. */
3318 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3319 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3321 /* If there is a variant part, we must compute whether the
3322 constraints statically select a particular variant. If
3323 so, we simply drop the qualified union and flatten the
3324 list of fields. Otherwise we'll build a new qualified
3325 union for the variants that are still relevant. */
3326 if (gnu_variant_part)
3328 variant_desc *v;
3329 unsigned int i;
3331 gnu_variant_list
3332 = build_variant_list (TREE_TYPE (gnu_variant_part),
3333 gnu_subst_list,
3334 vNULL);
3336 /* If all the qualifiers are unconditionally true, the
3337 innermost variant is statically selected. */
3338 selected_variant = true;
3339 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3340 if (!integer_onep (v->qual))
3342 selected_variant = false;
3343 break;
3346 /* Otherwise, create the new variants. */
3347 if (!selected_variant)
3348 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3350 tree old_variant = v->type;
3351 tree new_variant = make_node (RECORD_TYPE);
3352 tree suffix
3353 = concat_name (DECL_NAME (gnu_variant_part),
3354 IDENTIFIER_POINTER
3355 (DECL_NAME (v->field)));
3356 TYPE_NAME (new_variant)
3357 = concat_name (TYPE_NAME (gnu_type),
3358 IDENTIFIER_POINTER (suffix));
3359 copy_and_substitute_in_size (new_variant, old_variant,
3360 gnu_subst_list);
3361 v->new_type = new_variant;
3364 else
3366 gnu_variant_list.create (0);
3367 selected_variant = false;
3370 /* Make a list of fields and their position in the base type. */
3371 gnu_pos_list
3372 = build_position_list (gnu_unpad_base_type,
3373 gnu_variant_list.exists ()
3374 && !selected_variant,
3375 size_zero_node, bitsize_zero_node,
3376 BIGGEST_ALIGNMENT, NULL_TREE);
3378 /* Now go down every component in the subtype and compute its
3379 size and position from those of the component in the base
3380 type and from the constraints of the subtype. */
3381 for (gnat_field = First_Entity (gnat_entity);
3382 Present (gnat_field);
3383 gnat_field = Next_Entity (gnat_field))
3384 if ((Ekind (gnat_field) == E_Component
3385 || Ekind (gnat_field) == E_Discriminant)
3386 && !(Present (Corresponding_Discriminant (gnat_field))
3387 && Is_Tagged_Type (gnat_base_type))
3388 && Underlying_Type
3389 (Scope (Original_Record_Component (gnat_field)))
3390 == gnat_base_type)
3392 Name_Id gnat_name = Chars (gnat_field);
3393 Entity_Id gnat_old_field
3394 = Original_Record_Component (gnat_field);
3395 tree gnu_old_field
3396 = gnat_to_gnu_field_decl (gnat_old_field);
3397 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3398 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3399 tree gnu_cont_type, gnu_last = NULL_TREE;
3401 /* If the type is the same, retrieve the GCC type from the
3402 old field to take into account possible adjustments. */
3403 if (Etype (gnat_field) == Etype (gnat_old_field))
3404 gnu_field_type = TREE_TYPE (gnu_old_field);
3405 else
3406 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3408 /* If there was a component clause, the field types must be
3409 the same for the type and subtype, so copy the data from
3410 the old field to avoid recomputation here. Also if the
3411 field is justified modular and the optimization in
3412 gnat_to_gnu_field was applied. */
3413 if (Present (Component_Clause (gnat_old_field))
3414 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3415 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3416 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3417 == TREE_TYPE (gnu_old_field)))
3419 gnu_size = DECL_SIZE (gnu_old_field);
3420 gnu_field_type = TREE_TYPE (gnu_old_field);
3423 /* If the old field was packed and of constant size, we
3424 have to get the old size here, as it might differ from
3425 what the Etype conveys and the latter might overlap
3426 onto the following field. Try to arrange the type for
3427 possible better packing along the way. */
3428 else if (DECL_PACKED (gnu_old_field)
3429 && TREE_CODE (DECL_SIZE (gnu_old_field))
3430 == INTEGER_CST)
3432 gnu_size = DECL_SIZE (gnu_old_field);
3433 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3434 && !TYPE_FAT_POINTER_P (gnu_field_type)
3435 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3436 gnu_field_type
3437 = make_packable_type (gnu_field_type, true);
3440 else
3441 gnu_size = TYPE_SIZE (gnu_field_type);
3443 /* If the context of the old field is the base type or its
3444 REP part (if any), put the field directly in the new
3445 type; otherwise look up the context in the variant list
3446 and put the field either in the new type if there is a
3447 selected variant or in one of the new variants. */
3448 if (gnu_context == gnu_unpad_base_type
3449 || (gnu_rep_part
3450 && gnu_context == TREE_TYPE (gnu_rep_part)))
3451 gnu_cont_type = gnu_type;
3452 else
3454 variant_desc *v;
3455 unsigned int i;
3456 tree rep_part;
3458 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3459 if (gnu_context == v->type
3460 || ((rep_part = get_rep_part (v->type))
3461 && gnu_context == TREE_TYPE (rep_part)))
3462 break;
3463 if (v)
3465 if (selected_variant)
3466 gnu_cont_type = gnu_type;
3467 else
3468 gnu_cont_type = v->new_type;
3470 else
3471 /* The front-end may pass us "ghost" components if
3472 it fails to recognize that a constrained subtype
3473 is statically constrained. Discard them. */
3474 continue;
3477 /* Now create the new field modeled on the old one. */
3478 gnu_field
3479 = create_field_decl_from (gnu_old_field, gnu_field_type,
3480 gnu_cont_type, gnu_size,
3481 gnu_pos_list, gnu_subst_list);
3482 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3484 /* Put it in one of the new variants directly. */
3485 if (gnu_cont_type != gnu_type)
3487 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3488 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3491 /* To match the layout crafted in components_to_record,
3492 if this is the _Tag or _Parent field, put it before
3493 any other fields. */
3494 else if (gnat_name == Name_uTag
3495 || gnat_name == Name_uParent)
3496 gnu_field_list = chainon (gnu_field_list, gnu_field);
3498 /* Similarly, if this is the _Controller field, put
3499 it before the other fields except for the _Tag or
3500 _Parent field. */
3501 else if (gnat_name == Name_uController && gnu_last)
3503 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3504 DECL_CHAIN (gnu_last) = gnu_field;
3507 /* Otherwise, if this is a regular field, put it after
3508 the other fields. */
3509 else
3511 DECL_CHAIN (gnu_field) = gnu_field_list;
3512 gnu_field_list = gnu_field;
3513 if (!gnu_last)
3514 gnu_last = gnu_field;
3515 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3516 all_constant_pos = false;
3519 save_gnu_tree (gnat_field, gnu_field, false);
3522 /* If there is a variant list, a selected variant and the fields
3523 all have a constant position, put them in order of increasing
3524 position to match that of constant CONSTRUCTORs. Likewise if
3525 there is no variant list but a REP part, since the latter has
3526 been flattened in the process. */
3527 if (((gnu_variant_list.exists () && selected_variant)
3528 || (!gnu_variant_list.exists () && gnu_rep_part))
3529 && all_constant_pos)
3531 const int len = list_length (gnu_field_list);
3532 tree *field_arr = XALLOCAVEC (tree, len), t;
3533 int i;
3535 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3536 field_arr[i] = t;
3538 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3540 gnu_field_list = NULL_TREE;
3541 for (i = 0; i < len; i++)
3543 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3544 gnu_field_list = field_arr[i];
3548 /* If there is a variant list and no selected variant, we need
3549 to create the nest of variant parts from the old nest. */
3550 else if (gnu_variant_list.exists () && !selected_variant)
3552 tree new_variant_part
3553 = create_variant_part_from (gnu_variant_part,
3554 gnu_variant_list, gnu_type,
3555 gnu_pos_list, gnu_subst_list);
3556 DECL_CHAIN (new_variant_part) = gnu_field_list;
3557 gnu_field_list = new_variant_part;
3560 /* Now go through the entities again looking for Itypes that
3561 we have not elaborated but should (e.g., Etypes of fields
3562 that have Original_Components). */
3563 for (gnat_field = First_Entity (gnat_entity);
3564 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3565 if ((Ekind (gnat_field) == E_Discriminant
3566 || Ekind (gnat_field) == E_Component)
3567 && !present_gnu_tree (Etype (gnat_field)))
3568 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3570 /* Do not emit debug info for the type yet since we're going to
3571 modify it below. */
3572 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3573 false);
3574 compute_record_mode (gnu_type);
3576 /* See the E_Record_Type case for the rationale. */
3577 if (TYPE_MODE (gnu_type) != BLKmode
3578 && Is_By_Reference_Type (gnat_entity))
3579 SET_TYPE_MODE (gnu_type, BLKmode);
3581 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3583 /* Fill in locations of fields. */
3584 annotate_rep (gnat_entity, gnu_type);
3586 /* If debugging information is being written for the type, write
3587 a record that shows what we are a subtype of and also make a
3588 variable that indicates our size, if still variable. */
3589 if (debug_info_p)
3591 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3592 tree gnu_unpad_base_name
3593 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3594 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3596 TYPE_NAME (gnu_subtype_marker)
3597 = create_concat_name (gnat_entity, "XVS");
3598 finish_record_type (gnu_subtype_marker,
3599 create_field_decl (gnu_unpad_base_name,
3600 build_reference_type
3601 (gnu_unpad_base_type),
3602 gnu_subtype_marker,
3603 NULL_TREE, NULL_TREE,
3604 0, 0),
3605 0, true);
3607 add_parallel_type (gnu_type, gnu_subtype_marker);
3609 if (definition
3610 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3611 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3612 TYPE_SIZE_UNIT (gnu_subtype_marker)
3613 = create_var_decl (create_concat_name (gnat_entity,
3614 "XVZ"),
3615 NULL_TREE, sizetype, gnu_size_unit,
3616 false, false, false, false, NULL,
3617 gnat_entity);
3620 gnu_variant_list.release ();
3621 gnu_subst_list.release ();
3623 /* Now we can finalize it. */
3624 rest_of_record_type_compilation (gnu_type);
3627 /* Otherwise, go down all the components in the new type and make
3628 them equivalent to those in the base type. */
3629 else
3631 gnu_type = gnu_base_type;
3633 for (gnat_temp = First_Entity (gnat_entity);
3634 Present (gnat_temp);
3635 gnat_temp = Next_Entity (gnat_temp))
3636 if ((Ekind (gnat_temp) == E_Discriminant
3637 && !Is_Unchecked_Union (gnat_base_type))
3638 || Ekind (gnat_temp) == E_Component)
3639 save_gnu_tree (gnat_temp,
3640 gnat_to_gnu_field_decl
3641 (Original_Record_Component (gnat_temp)),
3642 false);
3645 break;
3647 case E_Access_Subprogram_Type:
3648 /* Use the special descriptor type for dispatch tables if needed,
3649 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3650 Note that we are only required to do so for static tables in
3651 order to be compatible with the C++ ABI, but Ada 2005 allows
3652 to extend library level tagged types at the local level so
3653 we do it in the non-static case as well. */
3654 if (TARGET_VTABLE_USES_DESCRIPTORS
3655 && Is_Dispatch_Table_Entity (gnat_entity))
3657 gnu_type = fdesc_type_node;
3658 gnu_size = TYPE_SIZE (gnu_type);
3659 break;
3662 /* ... fall through ... */
3664 case E_Anonymous_Access_Subprogram_Type:
3665 /* If we are not defining this entity, and we have incomplete
3666 entities being processed above us, make a dummy type and
3667 fill it in later. */
3668 if (!definition && defer_incomplete_level != 0)
3670 struct incomplete *p = XNEW (struct incomplete);
3672 gnu_type
3673 = build_pointer_type
3674 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3675 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3676 !Comes_From_Source (gnat_entity),
3677 debug_info_p, gnat_entity);
3678 this_made_decl = true;
3679 gnu_type = TREE_TYPE (gnu_decl);
3680 save_gnu_tree (gnat_entity, gnu_decl, false);
3681 saved = true;
3683 p->old_type = TREE_TYPE (gnu_type);
3684 p->full_type = Directly_Designated_Type (gnat_entity);
3685 p->next = defer_incomplete_list;
3686 defer_incomplete_list = p;
3687 break;
3690 /* ... fall through ... */
3692 case E_Allocator_Type:
3693 case E_Access_Type:
3694 case E_Access_Attribute_Type:
3695 case E_Anonymous_Access_Type:
3696 case E_General_Access_Type:
3698 /* The designated type and its equivalent type for gigi. */
3699 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3700 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3701 /* Whether it comes from a limited with. */
3702 bool is_from_limited_with
3703 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3704 && From_Limited_With (gnat_desig_equiv));
3705 /* The "full view" of the designated type. If this is an incomplete
3706 entity from a limited with, treat its non-limited view as the full
3707 view. Otherwise, if this is an incomplete or private type, use the
3708 full view. In the former case, we might point to a private type,
3709 in which case, we need its full view. Also, we want to look at the
3710 actual type used for the representation, so this takes a total of
3711 three steps. */
3712 Entity_Id gnat_desig_full_direct_first
3713 = (is_from_limited_with
3714 ? Non_Limited_View (gnat_desig_equiv)
3715 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3716 ? Full_View (gnat_desig_equiv) : Empty));
3717 Entity_Id gnat_desig_full_direct
3718 = ((is_from_limited_with
3719 && Present (gnat_desig_full_direct_first)
3720 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3721 ? Full_View (gnat_desig_full_direct_first)
3722 : gnat_desig_full_direct_first);
3723 Entity_Id gnat_desig_full
3724 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3725 /* The type actually used to represent the designated type, either
3726 gnat_desig_full or gnat_desig_equiv. */
3727 Entity_Id gnat_desig_rep;
3728 /* True if this is a pointer to an unconstrained array. */
3729 bool is_unconstrained_array;
3730 /* We want to know if we'll be seeing the freeze node for any
3731 incomplete type we may be pointing to. */
3732 bool in_main_unit
3733 = (Present (gnat_desig_full)
3734 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3735 : In_Extended_Main_Code_Unit (gnat_desig_type));
3736 /* True if we make a dummy type here. */
3737 bool made_dummy = false;
3738 /* The mode to be used for the pointer type. */
3739 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3740 /* The GCC type used for the designated type. */
3741 tree gnu_desig_type = NULL_TREE;
3743 if (!targetm.valid_pointer_mode (p_mode))
3744 p_mode = ptr_mode;
3746 /* If either the designated type or its full view is an unconstrained
3747 array subtype, replace it with the type it's a subtype of. This
3748 avoids problems with multiple copies of unconstrained array types.
3749 Likewise, if the designated type is a subtype of an incomplete
3750 record type, use the parent type to avoid order of elaboration
3751 issues. This can lose some code efficiency, but there is no
3752 alternative. */
3753 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3754 && !Is_Constrained (gnat_desig_equiv))
3755 gnat_desig_equiv = Etype (gnat_desig_equiv);
3756 if (Present (gnat_desig_full)
3757 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3758 && !Is_Constrained (gnat_desig_full))
3759 || (Ekind (gnat_desig_full) == E_Record_Subtype
3760 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3761 gnat_desig_full = Etype (gnat_desig_full);
3763 /* Set the type that's actually the representation of the designated
3764 type and also flag whether we have a unconstrained array. */
3765 gnat_desig_rep
3766 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3767 is_unconstrained_array
3768 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3770 /* If we are pointing to an incomplete type whose completion is an
3771 unconstrained array, make dummy fat and thin pointer types to it.
3772 Likewise if the type itself is dummy or an unconstrained array. */
3773 if (is_unconstrained_array
3774 && (Present (gnat_desig_full)
3775 || (present_gnu_tree (gnat_desig_equiv)
3776 && TYPE_IS_DUMMY_P
3777 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3778 || (!in_main_unit
3779 && defer_incomplete_level != 0
3780 && !present_gnu_tree (gnat_desig_equiv))
3781 || (in_main_unit
3782 && is_from_limited_with
3783 && Present (Freeze_Node (gnat_desig_equiv)))))
3785 if (present_gnu_tree (gnat_desig_rep))
3786 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3787 else
3789 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3790 made_dummy = true;
3793 /* If the call above got something that has a pointer, the pointer
3794 is our type. This could have happened either because the type
3795 was elaborated or because somebody else executed the code. */
3796 if (!TYPE_POINTER_TO (gnu_desig_type))
3797 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3798 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3801 /* If we already know what the full type is, use it. */
3802 else if (Present (gnat_desig_full)
3803 && present_gnu_tree (gnat_desig_full))
3804 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3806 /* Get the type of the thing we are to point to and build a pointer to
3807 it. If it is a reference to an incomplete or private type with a
3808 full view that is a record, make a dummy type node and get the
3809 actual type later when we have verified it is safe. */
3810 else if ((!in_main_unit
3811 && !present_gnu_tree (gnat_desig_equiv)
3812 && Present (gnat_desig_full)
3813 && !present_gnu_tree (gnat_desig_full)
3814 && Is_Record_Type (gnat_desig_full))
3815 /* Likewise if we are pointing to a record or array and we are
3816 to defer elaborating incomplete types. We do this as this
3817 access type may be the full view of a private type. Note
3818 that the unconstrained array case is handled above. */
3819 || ((!in_main_unit || imported_p)
3820 && defer_incomplete_level != 0
3821 && !present_gnu_tree (gnat_desig_equiv)
3822 && (Is_Record_Type (gnat_desig_rep)
3823 || Is_Array_Type (gnat_desig_rep)))
3824 /* If this is a reference from a limited_with type back to our
3825 main unit and there's a freeze node for it, either we have
3826 already processed the declaration and made the dummy type,
3827 in which case we just reuse the latter, or we have not yet,
3828 in which case we make the dummy type and it will be reused
3829 when the declaration is finally processed. In both cases,
3830 the pointer eventually created below will be automatically
3831 adjusted when the freeze node is processed. Note that the
3832 unconstrained array case is handled above. */
3833 || (in_main_unit
3834 && is_from_limited_with
3835 && Present (Freeze_Node (gnat_desig_rep))))
3837 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3838 made_dummy = true;
3841 /* Otherwise handle the case of a pointer to itself. */
3842 else if (gnat_desig_equiv == gnat_entity)
3844 gnu_type
3845 = build_pointer_type_for_mode (void_type_node, p_mode,
3846 No_Strict_Aliasing (gnat_entity));
3847 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3850 /* If expansion is disabled, the equivalent type of a concurrent type
3851 is absent, so build a dummy pointer type. */
3852 else if (type_annotate_only && No (gnat_desig_equiv))
3853 gnu_type = ptr_void_type_node;
3855 /* Finally, handle the default case where we can just elaborate our
3856 designated type. */
3857 else
3858 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3860 /* It is possible that a call to gnat_to_gnu_type above resolved our
3861 type. If so, just return it. */
3862 if (present_gnu_tree (gnat_entity))
3864 maybe_present = true;
3865 break;
3868 /* If we haven't done it yet, build the pointer type the usual way. */
3869 if (!gnu_type)
3871 /* Modify the designated type if we are pointing only to constant
3872 objects, but don't do it for unconstrained arrays. */
3873 if (Is_Access_Constant (gnat_entity)
3874 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3876 gnu_desig_type
3877 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3879 /* Some extra processing is required if we are building a
3880 pointer to an incomplete type (in the GCC sense). We might
3881 have such a type if we just made a dummy, or directly out
3882 of the call to gnat_to_gnu_type above if we are processing
3883 an access type for a record component designating the
3884 record type itself. */
3885 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3887 /* We must ensure that the pointer to variant we make will
3888 be processed by update_pointer_to when the initial type
3889 is completed. Pretend we made a dummy and let further
3890 processing act as usual. */
3891 made_dummy = true;
3893 /* We must ensure that update_pointer_to will not retrieve
3894 the dummy variant when building a properly qualified
3895 version of the complete type. We take advantage of the
3896 fact that get_qualified_type is requiring TYPE_NAMEs to
3897 match to influence build_qualified_type and then also
3898 update_pointer_to here. */
3899 TYPE_NAME (gnu_desig_type)
3900 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3904 gnu_type
3905 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3906 No_Strict_Aliasing (gnat_entity));
3909 /* If we are not defining this object and we have made a dummy pointer,
3910 save our current definition, evaluate the actual type, and replace
3911 the tentative type we made with the actual one. If we are to defer
3912 actually looking up the actual type, make an entry in the deferred
3913 list. If this is from a limited with, we may have to defer to the
3914 end of the current unit. */
3915 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3917 tree gnu_old_desig_type;
3919 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3921 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3922 if (esize == POINTER_SIZE)
3923 gnu_type = build_pointer_type
3924 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3926 else
3927 gnu_old_desig_type = TREE_TYPE (gnu_type);
3929 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3930 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3931 !Comes_From_Source (gnat_entity),
3932 debug_info_p, gnat_entity);
3933 this_made_decl = true;
3934 gnu_type = TREE_TYPE (gnu_decl);
3935 save_gnu_tree (gnat_entity, gnu_decl, false);
3936 saved = true;
3938 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3939 update gnu_old_desig_type directly, in which case it will not be
3940 a dummy type any more when we get into update_pointer_to.
3942 This can happen e.g. when the designated type is a record type,
3943 because their elaboration starts with an initial node from
3944 make_dummy_type, which may be the same node as the one we got.
3946 Besides, variants of this non-dummy type might have been created
3947 along the way. update_pointer_to is expected to properly take
3948 care of those situations. */
3949 if (defer_incomplete_level == 0 && !is_from_limited_with)
3951 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3952 gnat_to_gnu_type (gnat_desig_equiv));
3954 else
3956 struct incomplete *p = XNEW (struct incomplete);
3957 struct incomplete **head
3958 = (is_from_limited_with
3959 ? &defer_limited_with : &defer_incomplete_list);
3960 p->old_type = gnu_old_desig_type;
3961 p->full_type = gnat_desig_equiv;
3962 p->next = *head;
3963 *head = p;
3967 break;
3969 case E_Access_Protected_Subprogram_Type:
3970 case E_Anonymous_Access_Protected_Subprogram_Type:
3971 if (type_annotate_only && No (gnat_equiv_type))
3972 gnu_type = ptr_void_type_node;
3973 else
3975 /* The run-time representation is the equivalent type. */
3976 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3977 maybe_present = true;
3980 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3981 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3982 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3983 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3984 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3985 NULL_TREE, 0);
3987 break;
3989 case E_Access_Subtype:
3991 /* We treat this as identical to its base type; any constraint is
3992 meaningful only to the front-end.
3994 The designated type must be elaborated as well, if it does
3995 not have its own freeze node. Designated (sub)types created
3996 for constrained components of records with discriminants are
3997 not frozen by the front-end and thus not elaborated by gigi,
3998 because their use may appear before the base type is frozen,
3999 and because it is not clear that they are needed anywhere in
4000 gigi. With the current model, there is no correct place where
4001 they could be elaborated. */
4003 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4004 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4005 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4006 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4007 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4009 /* If we are not defining this entity, and we have incomplete
4010 entities being processed above us, make a dummy type and
4011 elaborate it later. */
4012 if (!definition && defer_incomplete_level != 0)
4014 struct incomplete *p = XNEW (struct incomplete);
4016 p->old_type
4017 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4018 p->full_type = Directly_Designated_Type (gnat_entity);
4019 p->next = defer_incomplete_list;
4020 defer_incomplete_list = p;
4022 else if (!IN (Ekind (Base_Type
4023 (Directly_Designated_Type (gnat_entity))),
4024 Incomplete_Or_Private_Kind))
4025 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4026 NULL_TREE, 0);
4029 maybe_present = true;
4030 break;
4032 /* Subprogram Entities
4034 The following access functions are defined for subprograms:
4036 Etype Return type or Standard_Void_Type.
4037 First_Formal The first formal parameter.
4038 Is_Imported Indicates that the subprogram has appeared in
4039 an INTERFACE or IMPORT pragma. For now we
4040 assume that the external language is C.
4041 Is_Exported Likewise but for an EXPORT pragma.
4042 Is_Inlined True if the subprogram is to be inlined.
4044 Each parameter is first checked by calling must_pass_by_ref on its
4045 type to determine if it is passed by reference. For parameters which
4046 are copied in, if they are Ada In Out or Out parameters, their return
4047 value becomes part of a record which becomes the return type of the
4048 function (C function - note that this applies only to Ada procedures
4049 so there is no Ada return type). Additional code to store back the
4050 parameters will be generated on the caller side. This transformation
4051 is done here, not in the front-end.
4053 The intended result of the transformation can be seen from the
4054 equivalent source rewritings that follow:
4056 struct temp {int a,b};
4057 procedure P (A,B: In Out ...) is temp P (int A,B)
4058 begin {
4059 .. ..
4060 end P; return {A,B};
4063 temp t;
4064 P(X,Y); t = P(X,Y);
4065 X = t.a , Y = t.b;
4067 For subprogram types we need to perform mainly the same conversions to
4068 GCC form that are needed for procedures and function declarations. The
4069 only difference is that at the end, we make a type declaration instead
4070 of a function declaration. */
4072 case E_Subprogram_Type:
4073 case E_Function:
4074 case E_Procedure:
4076 /* The type returned by a function or else Standard_Void_Type for a
4077 procedure. */
4078 Entity_Id gnat_return_type = Etype (gnat_entity);
4079 tree gnu_return_type;
4080 /* The first GCC parameter declaration (a PARM_DECL node). The
4081 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4082 actually is the head of this parameter list. */
4083 tree gnu_param_list = NULL_TREE;
4084 /* Non-null for subprograms containing parameters passed by copy-in
4085 copy-out (Ada In Out or Out parameters not passed by reference),
4086 in which case it is the list of nodes used to specify the values
4087 of the In Out/Out parameters that are returned as a record upon
4088 procedure return. The TREE_PURPOSE of an element of this list is
4089 a field of the record and the TREE_VALUE is the PARM_DECL
4090 corresponding to that field. This list will be saved in the
4091 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4092 tree gnu_cico_list = NULL_TREE;
4093 /* List of fields in return type of procedure with copy-in copy-out
4094 parameters. */
4095 tree gnu_field_list = NULL_TREE;
4096 /* If an import pragma asks to map this subprogram to a GCC builtin,
4097 this is the builtin DECL node. */
4098 tree gnu_builtin_decl = NULL_TREE;
4099 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4100 Entity_Id gnat_param;
4101 enum inline_status_t inline_status
4102 = Has_Pragma_No_Inline (gnat_entity)
4103 ? is_suppressed
4104 : Has_Pragma_Inline_Always (gnat_entity)
4105 ? is_required
4106 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4107 bool public_flag = Is_Public (gnat_entity) || imported_p;
4108 bool extern_flag
4109 = (Is_Public (gnat_entity) && !definition) || imported_p;
4110 bool artificial_flag = !Comes_From_Source (gnat_entity);
4111 /* The semantics of "pure" in Ada essentially matches that of "const"
4112 in the back-end. In particular, both properties are orthogonal to
4113 the "nothrow" property if the EH circuitry is explicit in the
4114 internal representation of the back-end. If we are to completely
4115 hide the EH circuitry from it, we need to declare that calls to pure
4116 Ada subprograms that can throw have side effects since they can
4117 trigger an "abnormal" transfer of control flow; thus they can be
4118 neither "const" nor "pure" in the back-end sense. */
4119 bool const_flag
4120 = (Exception_Mechanism == Back_End_Exceptions
4121 && Is_Pure (gnat_entity));
4122 bool volatile_flag = No_Return (gnat_entity);
4123 bool return_by_direct_ref_p = false;
4124 bool return_by_invisi_ref_p = false;
4125 bool return_unconstrained_p = false;
4126 int parmnum;
4128 /* A parameter may refer to this type, so defer completion of any
4129 incomplete types. */
4130 if (kind == E_Subprogram_Type && !definition)
4132 defer_incomplete_level++;
4133 this_deferred = true;
4136 /* If the subprogram has an alias, it is probably inherited, so
4137 we can use the original one. If the original "subprogram"
4138 is actually an enumeration literal, it may be the first use
4139 of its type, so we must elaborate that type now. */
4140 if (Present (Alias (gnat_entity)))
4142 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4143 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4145 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4147 /* Elaborate any Itypes in the parameters of this entity. */
4148 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4149 Present (gnat_temp);
4150 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4151 if (Is_Itype (Etype (gnat_temp)))
4152 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4154 break;
4157 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4158 corresponding DECL node. Proper generation of calls later on need
4159 proper parameter associations so we don't "break;" here. */
4160 if (Convention (gnat_entity) == Convention_Intrinsic
4161 && Present (Interface_Name (gnat_entity)))
4163 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4165 /* Inability to find the builtin decl most often indicates a
4166 genuine mistake, but imports of unregistered intrinsics are
4167 sometimes issued on purpose to allow hooking in alternate
4168 bodies. We post a warning conditioned on Wshadow in this case,
4169 to let developers be notified on demand without risking false
4170 positives with common default sets of options. */
4172 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4173 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4176 /* ??? What if we don't find the builtin node above ? warn ? err ?
4177 In the current state we neither warn nor err, and calls will just
4178 be handled as for regular subprograms. */
4180 /* Look into the return type and get its associated GCC tree. If it
4181 is not void, compute various flags for the subprogram type. */
4182 if (Ekind (gnat_return_type) == E_Void)
4183 gnu_return_type = void_type_node;
4184 else
4186 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4187 context may now appear in parameter and result profiles. If
4188 we are only annotating types, break circularities here. */
4189 if (type_annotate_only
4190 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4191 && From_Limited_With (gnat_return_type)
4192 && In_Extended_Main_Code_Unit
4193 (Non_Limited_View (gnat_return_type))
4194 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4195 gnu_return_type = ptr_void_type_node;
4196 else
4197 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4199 /* If this function returns by reference, make the actual return
4200 type the pointer type and make a note of that. */
4201 if (Returns_By_Ref (gnat_entity))
4203 gnu_return_type = build_pointer_type (gnu_return_type);
4204 return_by_direct_ref_p = true;
4207 /* If we are supposed to return an unconstrained array type, make
4208 the actual return type the fat pointer type. */
4209 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4211 gnu_return_type = TREE_TYPE (gnu_return_type);
4212 return_unconstrained_p = true;
4215 /* Likewise, if the return type requires a transient scope, the
4216 return value will be allocated on the secondary stack so the
4217 actual return type is the pointer type. */
4218 else if (Requires_Transient_Scope (gnat_return_type))
4220 gnu_return_type = build_pointer_type (gnu_return_type);
4221 return_unconstrained_p = true;
4224 /* If the Mechanism is By_Reference, ensure this function uses the
4225 target's by-invisible-reference mechanism, which may not be the
4226 same as above (e.g. it might be passing an extra parameter). */
4227 else if (kind == E_Function
4228 && Mechanism (gnat_entity) == By_Reference)
4229 return_by_invisi_ref_p = true;
4231 /* Likewise, if the return type is itself By_Reference. */
4232 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4233 return_by_invisi_ref_p = true;
4235 /* If the type is a padded type and the underlying type would not
4236 be passed by reference or the function has a foreign convention,
4237 return the underlying type. */
4238 else if (TYPE_IS_PADDING_P (gnu_return_type)
4239 && (!default_pass_by_ref
4240 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4241 || Has_Foreign_Convention (gnat_entity)))
4242 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4244 /* If the return type is unconstrained, that means it must have a
4245 maximum size. Use the padded type as the effective return type.
4246 And ensure the function uses the target's by-invisible-reference
4247 mechanism to avoid copying too much data when it returns. */
4248 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4250 tree orig_type = gnu_return_type;
4252 gnu_return_type
4253 = maybe_pad_type (gnu_return_type,
4254 max_size (TYPE_SIZE (gnu_return_type),
4255 true),
4256 0, gnat_entity, false, false, false, true);
4258 /* Declare it now since it will never be declared otherwise.
4259 This is necessary to ensure that its subtrees are properly
4260 marked. */
4261 if (gnu_return_type != orig_type
4262 && !DECL_P (TYPE_NAME (gnu_return_type)))
4263 create_type_decl (TYPE_NAME (gnu_return_type),
4264 gnu_return_type, true, debug_info_p,
4265 gnat_entity);
4267 return_by_invisi_ref_p = true;
4270 /* If the return type has a size that overflows, we cannot have
4271 a function that returns that type. This usage doesn't make
4272 sense anyway, so give an error here. */
4273 if (TYPE_SIZE_UNIT (gnu_return_type)
4274 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4275 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4277 post_error ("cannot return type whose size overflows",
4278 gnat_entity);
4279 gnu_return_type = copy_node (gnu_return_type);
4280 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4281 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4282 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4283 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4287 /* Loop over the parameters and get their associated GCC tree. While
4288 doing this, build a copy-in copy-out structure if we need one. */
4289 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4290 Present (gnat_param);
4291 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4293 Entity_Id gnat_param_type = Etype (gnat_param);
4294 tree gnu_param_name = get_entity_name (gnat_param);
4295 tree gnu_param_type, gnu_param, gnu_field;
4296 Mechanism_Type mech = Mechanism (gnat_param);
4297 bool copy_in_copy_out = false, fake_param_type;
4299 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4300 context may now appear in parameter and result profiles. If
4301 we are only annotating types, break circularities here. */
4302 if (type_annotate_only
4303 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4304 && From_Limited_With (Etype (gnat_param_type))
4305 && In_Extended_Main_Code_Unit
4306 (Non_Limited_View (gnat_param_type))
4307 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4309 gnu_param_type = ptr_void_type_node;
4310 fake_param_type = true;
4312 else
4314 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4315 fake_param_type = false;
4318 /* Builtins are expanded inline and there is no real call sequence
4319 involved. So the type expected by the underlying expander is
4320 always the type of each argument "as is". */
4321 if (gnu_builtin_decl)
4322 mech = By_Copy;
4323 /* Handle the first parameter of a valued procedure specially. */
4324 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4325 mech = By_Copy_Return;
4326 /* Otherwise, see if a Mechanism was supplied that forced this
4327 parameter to be passed one way or another. */
4328 else if (mech == Default
4329 || mech == By_Copy
4330 || mech == By_Reference)
4332 else if (mech > 0)
4334 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4335 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4336 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4337 mech))
4338 mech = By_Reference;
4339 else
4340 mech = By_Copy;
4342 else
4344 post_error ("unsupported mechanism for&", gnat_param);
4345 mech = Default;
4348 /* Do not call gnat_to_gnu_param for a fake parameter type since
4349 it will try to use the real type again. */
4350 if (fake_param_type)
4352 if (Ekind (gnat_param) == E_Out_Parameter)
4353 gnu_param = NULL_TREE;
4354 else
4356 gnu_param
4357 = create_param_decl (gnu_param_name, gnu_param_type,
4358 false);
4359 Set_Mechanism (gnat_param,
4360 mech == Default ? By_Copy : mech);
4361 if (Ekind (gnat_param) == E_In_Out_Parameter)
4362 copy_in_copy_out = true;
4365 else
4366 gnu_param
4367 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4368 Has_Foreign_Convention (gnat_entity),
4369 &copy_in_copy_out);
4371 /* We are returned either a PARM_DECL or a type if no parameter
4372 needs to be passed; in either case, adjust the type. */
4373 if (DECL_P (gnu_param))
4374 gnu_param_type = TREE_TYPE (gnu_param);
4375 else
4377 gnu_param_type = gnu_param;
4378 gnu_param = NULL_TREE;
4381 /* The failure of this assertion will very likely come from an
4382 order of elaboration issue for the type of the parameter. */
4383 gcc_assert (kind == E_Subprogram_Type
4384 || !TYPE_IS_DUMMY_P (gnu_param_type)
4385 || type_annotate_only);
4387 if (gnu_param)
4389 gnu_param_list = chainon (gnu_param, gnu_param_list);
4390 Sloc_to_locus (Sloc (gnat_param),
4391 &DECL_SOURCE_LOCATION (gnu_param));
4392 save_gnu_tree (gnat_param, gnu_param, false);
4394 /* If a parameter is a pointer, this function may modify
4395 memory through it and thus shouldn't be considered
4396 a const function. Also, the memory may be modified
4397 between two calls, so they can't be CSE'ed. The latter
4398 case also handles by-ref parameters. */
4399 if (POINTER_TYPE_P (gnu_param_type)
4400 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4401 const_flag = false;
4404 if (copy_in_copy_out)
4406 if (!gnu_cico_list)
4408 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4410 /* If this is a function, we also need a field for the
4411 return value to be placed. */
4412 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4414 gnu_field
4415 = create_field_decl (get_identifier ("RETVAL"),
4416 gnu_return_type,
4417 gnu_new_ret_type, NULL_TREE,
4418 NULL_TREE, 0, 0);
4419 Sloc_to_locus (Sloc (gnat_entity),
4420 &DECL_SOURCE_LOCATION (gnu_field));
4421 gnu_field_list = gnu_field;
4422 gnu_cico_list
4423 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4426 gnu_return_type = gnu_new_ret_type;
4427 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4428 /* Set a default alignment to speed up accesses. But we
4429 shouldn't increase the size of the structure too much,
4430 lest it doesn't fit in return registers anymore. */
4431 TYPE_ALIGN (gnu_return_type)
4432 = get_mode_alignment (ptr_mode);
4435 gnu_field
4436 = create_field_decl (gnu_param_name, gnu_param_type,
4437 gnu_return_type, NULL_TREE, NULL_TREE,
4438 0, 0);
4439 Sloc_to_locus (Sloc (gnat_param),
4440 &DECL_SOURCE_LOCATION (gnu_field));
4441 DECL_CHAIN (gnu_field) = gnu_field_list;
4442 gnu_field_list = gnu_field;
4443 gnu_cico_list
4444 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4448 if (gnu_cico_list)
4450 /* If we have a CICO list but it has only one entry, we convert
4451 this function into a function that returns this object. */
4452 if (list_length (gnu_cico_list) == 1)
4453 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4455 /* Do not finalize the return type if the subprogram is stubbed
4456 since structures are incomplete for the back-end. */
4457 else if (Convention (gnat_entity) != Convention_Stubbed)
4459 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4460 0, false);
4462 /* Try to promote the mode of the return type if it is passed
4463 in registers, again to speed up accesses. */
4464 if (TYPE_MODE (gnu_return_type) == BLKmode
4465 && !targetm.calls.return_in_memory (gnu_return_type,
4466 NULL_TREE))
4468 unsigned int size
4469 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4470 unsigned int i = BITS_PER_UNIT;
4471 machine_mode mode;
4473 while (i < size)
4474 i <<= 1;
4475 mode = mode_for_size (i, MODE_INT, 0);
4476 if (mode != BLKmode)
4478 SET_TYPE_MODE (gnu_return_type, mode);
4479 TYPE_ALIGN (gnu_return_type)
4480 = GET_MODE_ALIGNMENT (mode);
4481 TYPE_SIZE (gnu_return_type)
4482 = bitsize_int (GET_MODE_BITSIZE (mode));
4483 TYPE_SIZE_UNIT (gnu_return_type)
4484 = size_int (GET_MODE_SIZE (mode));
4488 if (debug_info_p)
4489 rest_of_record_type_compilation (gnu_return_type);
4493 /* Deal with platform-specific calling conventions. */
4494 if (Has_Stdcall_Convention (gnat_entity))
4495 prepend_one_attribute
4496 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4497 get_identifier ("stdcall"), NULL_TREE,
4498 gnat_entity);
4499 else if (Has_Thiscall_Convention (gnat_entity))
4500 prepend_one_attribute
4501 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4502 get_identifier ("thiscall"), NULL_TREE,
4503 gnat_entity);
4505 /* If we should request stack realignment for a foreign convention
4506 subprogram, do so. Note that this applies to task entry points
4507 in particular. */
4508 if (FOREIGN_FORCE_REALIGN_STACK
4509 && Has_Foreign_Convention (gnat_entity))
4510 prepend_one_attribute
4511 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4512 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4513 gnat_entity);
4515 /* Deal with a pragma Linker_Section on a subprogram. */
4516 if ((kind == E_Function || kind == E_Procedure)
4517 && Present (Linker_Section_Pragma (gnat_entity)))
4518 prepend_one_attribute_pragma (&attr_list,
4519 Linker_Section_Pragma (gnat_entity));
4521 /* The lists have been built in reverse. */
4522 gnu_param_list = nreverse (gnu_param_list);
4523 gnu_cico_list = nreverse (gnu_cico_list);
4525 if (kind == E_Function)
4526 Set_Mechanism (gnat_entity, return_unconstrained_p
4527 || return_by_direct_ref_p
4528 || return_by_invisi_ref_p
4529 ? By_Reference : By_Copy);
4530 gnu_type
4531 = create_subprog_type (gnu_return_type, gnu_param_list,
4532 gnu_cico_list, return_unconstrained_p,
4533 return_by_direct_ref_p,
4534 return_by_invisi_ref_p);
4536 /* A subprogram (something that doesn't return anything) shouldn't
4537 be considered const since there would be no reason for such a
4538 subprogram. Note that procedures with Out (or In Out) parameters
4539 have already been converted into a function with a return type. */
4540 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4541 const_flag = false;
4543 if (const_flag || volatile_flag)
4545 const int quals
4546 = (const_flag ? TYPE_QUAL_CONST : 0)
4547 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4549 gnu_type = change_qualified_type (gnu_type, quals);
4552 /* If we have a builtin decl for that function, use it. Check if the
4553 profiles are compatible and warn if they are not. The checker is
4554 expected to post extra diagnostics in this case. */
4555 if (gnu_builtin_decl)
4557 intrin_binding_t inb;
4559 inb.gnat_entity = gnat_entity;
4560 inb.ada_fntype = gnu_type;
4561 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4563 if (!intrin_profiles_compatible_p (&inb))
4564 post_error
4565 ("?profile of& doesn''t match the builtin it binds!",
4566 gnat_entity);
4568 gnu_decl = gnu_builtin_decl;
4569 gnu_type = TREE_TYPE (gnu_builtin_decl);
4570 break;
4573 /* If there was no specified Interface_Name and the external and
4574 internal names of the subprogram are the same, only use the
4575 internal name to allow disambiguation of nested subprograms. */
4576 if (No (Interface_Name (gnat_entity))
4577 && gnu_ext_name == gnu_entity_name)
4578 gnu_ext_name = NULL_TREE;
4580 /* If we are defining the subprogram and it has an Address clause
4581 we must get the address expression from the saved GCC tree for the
4582 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4583 the address expression here since the front-end has guaranteed
4584 in that case that the elaboration has no effects. If there is
4585 an Address clause and we are not defining the object, just
4586 make it a constant. */
4587 if (Present (Address_Clause (gnat_entity)))
4589 tree gnu_address = NULL_TREE;
4591 if (definition)
4592 gnu_address
4593 = (present_gnu_tree (gnat_entity)
4594 ? get_gnu_tree (gnat_entity)
4595 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4597 save_gnu_tree (gnat_entity, NULL_TREE, false);
4599 /* Convert the type of the object to a reference type that can
4600 alias everything as per 13.3(19). */
4601 gnu_type
4602 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4603 if (gnu_address)
4604 gnu_address = convert (gnu_type, gnu_address);
4606 gnu_decl
4607 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4608 gnu_address, false, Is_Public (gnat_entity),
4609 extern_flag, false, NULL, gnat_entity);
4610 DECL_BY_REF_P (gnu_decl) = 1;
4613 else if (kind == E_Subprogram_Type)
4615 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4616 gnu_decl
4617 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4618 debug_info_p, gnat_entity);
4620 else
4622 gnu_decl
4623 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4624 gnu_param_list, inline_status,
4625 public_flag, extern_flag, artificial_flag,
4626 attr_list, gnat_entity);
4627 /* This is unrelated to the stub built right above. */
4628 DECL_STUBBED_P (gnu_decl)
4629 = Convention (gnat_entity) == Convention_Stubbed;
4632 break;
4634 case E_Incomplete_Type:
4635 case E_Incomplete_Subtype:
4636 case E_Private_Type:
4637 case E_Private_Subtype:
4638 case E_Limited_Private_Type:
4639 case E_Limited_Private_Subtype:
4640 case E_Record_Type_With_Private:
4641 case E_Record_Subtype_With_Private:
4643 /* Get the "full view" of this entity. If this is an incomplete
4644 entity from a limited with, treat its non-limited view as the
4645 full view. Otherwise, use either the full view or the underlying
4646 full view, whichever is present. This is used in all the tests
4647 below. */
4648 Entity_Id full_view
4649 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4650 ? Non_Limited_View (gnat_entity)
4651 : Present (Full_View (gnat_entity))
4652 ? Full_View (gnat_entity)
4653 : IN (kind, Private_Kind)
4654 ? Underlying_Full_View (gnat_entity)
4655 : Empty;
4657 /* If this is an incomplete type with no full view, it must be a Taft
4658 Amendment type, in which case we return a dummy type. Otherwise,
4659 just get the type from its Etype. */
4660 if (No (full_view))
4662 if (kind == E_Incomplete_Type)
4664 gnu_type = make_dummy_type (gnat_entity);
4665 gnu_decl = TYPE_STUB_DECL (gnu_type);
4667 else
4669 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4670 NULL_TREE, 0);
4671 maybe_present = true;
4673 break;
4676 /* If we already made a type for the full view, reuse it. */
4677 else if (present_gnu_tree (full_view))
4679 gnu_decl = get_gnu_tree (full_view);
4680 break;
4683 /* Otherwise, if we are not defining the type now, get the type
4684 from the full view. But always get the type from the full view
4685 for define on use types, since otherwise we won't see them! */
4686 else if (!definition
4687 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4688 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4690 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4691 maybe_present = true;
4692 break;
4695 /* For incomplete types, make a dummy type entry which will be
4696 replaced later. Save it as the full declaration's type so
4697 we can do any needed updates when we see it. */
4698 gnu_type = make_dummy_type (gnat_entity);
4699 gnu_decl = TYPE_STUB_DECL (gnu_type);
4700 if (Has_Completion_In_Body (gnat_entity))
4701 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4702 save_gnu_tree (full_view, gnu_decl, 0);
4703 break;
4706 case E_Class_Wide_Type:
4707 /* Class-wide types are always transformed into their root type. */
4708 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4709 maybe_present = true;
4710 break;
4712 case E_Task_Type:
4713 case E_Task_Subtype:
4714 case E_Protected_Type:
4715 case E_Protected_Subtype:
4716 /* Concurrent types are always transformed into their record type. */
4717 if (type_annotate_only && No (gnat_equiv_type))
4718 gnu_type = void_type_node;
4719 else
4720 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4721 maybe_present = true;
4722 break;
4724 case E_Label:
4725 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4726 break;
4728 case E_Block:
4729 case E_Loop:
4730 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4731 we've already saved it, so we don't try to. */
4732 gnu_decl = error_mark_node;
4733 saved = true;
4734 break;
4736 case E_Abstract_State:
4737 /* This is a SPARK annotation that only reaches here when compiling in
4738 ASIS mode and has no characteristics to annotate. */
4739 gcc_assert (type_annotate_only);
4740 return error_mark_node;
4742 default:
4743 gcc_unreachable ();
4746 /* If we had a case where we evaluated another type and it might have
4747 defined this one, handle it here. */
4748 if (maybe_present && present_gnu_tree (gnat_entity))
4750 gnu_decl = get_gnu_tree (gnat_entity);
4751 saved = true;
4754 /* If we are processing a type and there is either no decl for it or
4755 we just made one, do some common processing for the type, such as
4756 handling alignment and possible padding. */
4757 if (is_type && (!gnu_decl || this_made_decl))
4759 /* Process the attributes, if not already done. Note that the type is
4760 already defined so we cannot pass true for IN_PLACE here. */
4761 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4763 /* Tell the middle-end that objects of tagged types are guaranteed to
4764 be properly aligned. This is necessary because conversions to the
4765 class-wide type are translated into conversions to the root type,
4766 which can be less aligned than some of its derived types. */
4767 if (Is_Tagged_Type (gnat_entity)
4768 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4769 TYPE_ALIGN_OK (gnu_type) = 1;
4771 /* Record whether the type is passed by reference. */
4772 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4773 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4775 /* ??? Don't set the size for a String_Literal since it is either
4776 confirming or we don't handle it properly (if the low bound is
4777 non-constant). */
4778 if (!gnu_size && kind != E_String_Literal_Subtype)
4780 Uint gnat_size = Known_Esize (gnat_entity)
4781 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4782 gnu_size
4783 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4784 false, Has_Size_Clause (gnat_entity));
4787 /* If a size was specified, see if we can make a new type of that size
4788 by rearranging the type, for example from a fat to a thin pointer. */
4789 if (gnu_size)
4791 gnu_type
4792 = make_type_from_size (gnu_type, gnu_size,
4793 Has_Biased_Representation (gnat_entity));
4795 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4796 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4797 gnu_size = NULL_TREE;
4800 /* If the alignment has not already been processed and this is not
4801 an unconstrained array type, see if an alignment is specified.
4802 If not, we pick a default alignment for atomic objects. */
4803 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4805 else if (Known_Alignment (gnat_entity))
4807 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4808 TYPE_ALIGN (gnu_type));
4810 /* Warn on suspiciously large alignments. This should catch
4811 errors about the (alignment,byte)/(size,bit) discrepancy. */
4812 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4814 tree size;
4816 /* If a size was specified, take it into account. Otherwise
4817 use the RM size for records or unions as the type size has
4818 already been adjusted to the alignment. */
4819 if (gnu_size)
4820 size = gnu_size;
4821 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4822 && !TYPE_FAT_POINTER_P (gnu_type))
4823 size = rm_size (gnu_type);
4824 else
4825 size = TYPE_SIZE (gnu_type);
4827 /* Consider an alignment as suspicious if the alignment/size
4828 ratio is greater or equal to the byte/bit ratio. */
4829 if (tree_fits_uhwi_p (size)
4830 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4831 post_error_ne ("?suspiciously large alignment specified for&",
4832 Expression (Alignment_Clause (gnat_entity)),
4833 gnat_entity);
4836 else if (Is_Atomic (gnat_entity) && !gnu_size
4837 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4838 && integer_pow2p (TYPE_SIZE (gnu_type)))
4839 align = MIN (BIGGEST_ALIGNMENT,
4840 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4841 else if (Is_Atomic (gnat_entity) && gnu_size
4842 && tree_fits_uhwi_p (gnu_size)
4843 && integer_pow2p (gnu_size))
4844 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4846 /* See if we need to pad the type. If we did, and made a record,
4847 the name of the new type may be changed. So get it back for
4848 us when we make the new TYPE_DECL below. */
4849 if (gnu_size || align > 0)
4850 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4851 false, !gnu_decl, definition, false);
4853 if (TYPE_IS_PADDING_P (gnu_type))
4854 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4856 /* Now set the RM size of the type. We cannot do it before padding
4857 because we need to accept arbitrary RM sizes on integral types. */
4858 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4860 /* If we are at global level, GCC will have applied variable_size to
4861 the type, but that won't have done anything. So, if it's not
4862 a constant or self-referential, call elaborate_expression_1 to
4863 make a variable for the size rather than calculating it each time.
4864 Handle both the RM size and the actual size. */
4865 if (global_bindings_p ()
4866 && TYPE_SIZE (gnu_type)
4867 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4868 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4870 tree size = TYPE_SIZE (gnu_type);
4872 TYPE_SIZE (gnu_type)
4873 = elaborate_expression_1 (size, gnat_entity,
4874 get_identifier ("SIZE"),
4875 definition, false);
4877 /* ??? For now, store the size as a multiple of the alignment in
4878 bytes so that we can see the alignment from the tree. */
4879 TYPE_SIZE_UNIT (gnu_type)
4880 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4881 get_identifier ("SIZE_A_UNIT"),
4882 definition, false,
4883 TYPE_ALIGN (gnu_type));
4885 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4886 may not be marked by the call to create_type_decl below. */
4887 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4889 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4891 tree variant_part = get_variant_part (gnu_type);
4892 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4894 if (variant_part)
4896 tree union_type = TREE_TYPE (variant_part);
4897 tree offset = DECL_FIELD_OFFSET (variant_part);
4899 /* If the position of the variant part is constant, subtract
4900 it from the size of the type of the parent to get the new
4901 size. This manual CSE reduces the data size. */
4902 if (TREE_CODE (offset) == INTEGER_CST)
4904 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4905 TYPE_SIZE (union_type)
4906 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4907 bit_from_pos (offset, bitpos));
4908 TYPE_SIZE_UNIT (union_type)
4909 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4910 byte_from_pos (offset, bitpos));
4912 else
4914 TYPE_SIZE (union_type)
4915 = elaborate_expression_1 (TYPE_SIZE (union_type),
4916 gnat_entity,
4917 get_identifier ("VSIZE"),
4918 definition, false);
4920 /* ??? For now, store the size as a multiple of the
4921 alignment in bytes so that we can see the alignment
4922 from the tree. */
4923 TYPE_SIZE_UNIT (union_type)
4924 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4925 gnat_entity,
4926 get_identifier
4927 ("VSIZE_A_UNIT"),
4928 definition, false,
4929 TYPE_ALIGN (union_type));
4931 /* ??? For now, store the offset as a multiple of the
4932 alignment in bytes so that we can see the alignment
4933 from the tree. */
4934 DECL_FIELD_OFFSET (variant_part)
4935 = elaborate_expression_2 (offset,
4936 gnat_entity,
4937 get_identifier ("VOFFSET"),
4938 definition, false,
4939 DECL_OFFSET_ALIGN
4940 (variant_part));
4943 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4944 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4947 if (operand_equal_p (ada_size, size, 0))
4948 ada_size = TYPE_SIZE (gnu_type);
4949 else
4950 ada_size
4951 = elaborate_expression_1 (ada_size, gnat_entity,
4952 get_identifier ("RM_SIZE"),
4953 definition, false);
4954 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4958 /* If this is a record type or subtype, call elaborate_expression_2 on
4959 any field position. Do this for both global and local types.
4960 Skip any fields that we haven't made trees for to avoid problems with
4961 class wide types. */
4962 if (IN (kind, Record_Kind))
4963 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4964 gnat_temp = Next_Entity (gnat_temp))
4965 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4967 tree gnu_field = get_gnu_tree (gnat_temp);
4969 /* ??? For now, store the offset as a multiple of the alignment
4970 in bytes so that we can see the alignment from the tree. */
4971 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4973 DECL_FIELD_OFFSET (gnu_field)
4974 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4975 gnat_temp,
4976 get_identifier ("OFFSET"),
4977 definition, false,
4978 DECL_OFFSET_ALIGN (gnu_field));
4980 /* ??? The context of gnu_field is not necessarily gnu_type
4981 so the MULT_EXPR node built above may not be marked by
4982 the call to create_type_decl below. */
4983 if (global_bindings_p ())
4984 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4988 if (Is_Atomic (gnat_entity))
4989 check_ok_for_atomic (gnu_type, gnat_entity, false);
4991 /* If this is not an unconstrained array type, set some flags. */
4992 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4994 if (Treat_As_Volatile (gnat_entity))
4995 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
4997 if (Present (Alignment_Clause (gnat_entity)))
4998 TYPE_USER_ALIGN (gnu_type) = 1;
5000 if (Universal_Aliasing (gnat_entity))
5001 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5004 if (!gnu_decl)
5005 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5006 !Comes_From_Source (gnat_entity),
5007 debug_info_p, gnat_entity);
5008 else
5010 TREE_TYPE (gnu_decl) = gnu_type;
5011 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5015 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5017 gnu_type = TREE_TYPE (gnu_decl);
5019 /* If this is a derived type, relate its alias set to that of its parent
5020 to avoid troubles when a call to an inherited primitive is inlined in
5021 a context where a derived object is accessed. The inlined code works
5022 on the parent view so the resulting code may access the same object
5023 using both the parent and the derived alias sets, which thus have to
5024 conflict. As the same issue arises with component references, the
5025 parent alias set also has to conflict with composite types enclosing
5026 derived components. For instance, if we have:
5028 type D is new T;
5029 type R is record
5030 Component : D;
5031 end record;
5033 we want T to conflict with both D and R, in addition to R being a
5034 superset of D by record/component construction.
5036 One way to achieve this is to perform an alias set copy from the
5037 parent to the derived type. This is not quite appropriate, though,
5038 as we don't want separate derived types to conflict with each other:
5040 type I1 is new Integer;
5041 type I2 is new Integer;
5043 We want I1 and I2 to both conflict with Integer but we do not want
5044 I1 to conflict with I2, and an alias set copy on derivation would
5045 have that effect.
5047 The option chosen is to make the alias set of the derived type a
5048 superset of that of its parent type. It trivially fulfills the
5049 simple requirement for the Integer derivation example above, and
5050 the component case as well by superset transitivity:
5052 superset superset
5053 R ----------> D ----------> T
5055 However, for composite types, conversions between derived types are
5056 translated into VIEW_CONVERT_EXPRs so a sequence like:
5058 type Comp1 is new Comp;
5059 type Comp2 is new Comp;
5060 procedure Proc (C : Comp1);
5062 C : Comp2;
5063 Proc (Comp1 (C));
5065 is translated into:
5067 C : Comp2;
5068 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5070 and gimplified into:
5072 C : Comp2;
5073 Comp1 *C.0;
5074 C.0 = (Comp1 *) &C;
5075 Proc (C.0);
5077 i.e. generates code involving type punning. Therefore, Comp1 needs
5078 to conflict with Comp2 and an alias set copy is required.
5080 The language rules ensure the parent type is already frozen here. */
5081 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5083 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5084 /* For constrained packed array subtypes, the implementation type is
5085 used instead of the nominal type. */
5086 if (kind == E_Array_Subtype
5087 && Is_Constrained (gnat_entity)
5088 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5089 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5090 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5091 Is_Composite_Type (gnat_entity)
5092 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5095 /* Back-annotate the Alignment of the type if not already in the
5096 tree. Likewise for sizes. */
5097 if (Unknown_Alignment (gnat_entity))
5099 unsigned int double_align, align;
5100 bool is_capped_double, align_clause;
5102 /* If the default alignment of "double" or larger scalar types is
5103 specifically capped and this is not an array with an alignment
5104 clause on the component type, return the cap. */
5105 if ((double_align = double_float_alignment) > 0)
5106 is_capped_double
5107 = is_double_float_or_array (gnat_entity, &align_clause);
5108 else if ((double_align = double_scalar_alignment) > 0)
5109 is_capped_double
5110 = is_double_scalar_or_array (gnat_entity, &align_clause);
5111 else
5112 is_capped_double = align_clause = false;
5114 if (is_capped_double && !align_clause)
5115 align = double_align;
5116 else
5117 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5119 Set_Alignment (gnat_entity, UI_From_Int (align));
5122 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5124 tree gnu_size = TYPE_SIZE (gnu_type);
5126 /* If the size is self-referential, annotate the maximum value. */
5127 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5128 gnu_size = max_size (gnu_size, true);
5130 /* If we are just annotating types and the type is tagged, the tag
5131 and the parent components are not generated by the front-end so
5132 sizes must be adjusted if there is no representation clause. */
5133 if (type_annotate_only
5134 && Is_Tagged_Type (gnat_entity)
5135 && !VOID_TYPE_P (gnu_type)
5136 && (!TYPE_FIELDS (gnu_type)
5137 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5139 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5140 Uint uint_size;
5142 if (Is_Derived_Type (gnat_entity))
5144 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5145 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5146 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5148 else
5149 offset = pointer_size;
5151 if (TYPE_FIELDS (gnu_type))
5152 offset
5153 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5155 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5156 gnu_size = round_up (gnu_size, POINTER_SIZE);
5157 uint_size = annotate_value (gnu_size);
5158 Set_Esize (gnat_entity, uint_size);
5159 Set_RM_Size (gnat_entity, uint_size);
5161 else
5162 Set_Esize (gnat_entity, annotate_value (gnu_size));
5165 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5166 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5169 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5170 cannot do so if we are reusing the ..._DECL node made for an equivalent
5171 type or an alias or a renamed object as the predicates don't apply to it
5172 but to GNAT_ENTITY. */
5173 if (DECL_P (gnu_decl)
5174 && !(is_type && gnat_equiv_type != gnat_entity)
5175 && !Present (Alias (gnat_entity))
5176 && !(Present (Renamed_Object (gnat_entity)) && saved))
5178 if (!Comes_From_Source (gnat_entity))
5179 DECL_ARTIFICIAL (gnu_decl) = 1;
5181 if (!debug_info_p)
5182 DECL_IGNORED_P (gnu_decl) = 1;
5185 /* If we haven't already, associate the ..._DECL node that we just made with
5186 the input GNAT entity node. */
5187 if (!saved)
5188 save_gnu_tree (gnat_entity, gnu_decl, false);
5190 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5191 eliminate as many deferred computations as possible. */
5192 process_deferred_decl_context (false);
5194 /* If this is an enumeration or floating-point type, we were not able to set
5195 the bounds since they refer to the type. These are always static. */
5196 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5197 || (kind == E_Floating_Point_Type))
5199 tree gnu_scalar_type = gnu_type;
5200 tree gnu_low_bound, gnu_high_bound;
5202 /* If this is a padded type, we need to use the underlying type. */
5203 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5204 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5206 /* If this is a floating point type and we haven't set a floating
5207 point type yet, use this in the evaluation of the bounds. */
5208 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5209 longest_float_type_node = gnu_scalar_type;
5211 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5212 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5214 if (kind == E_Enumeration_Type)
5216 /* Enumeration types have specific RM bounds. */
5217 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5218 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5220 else
5222 /* Floating-point types don't have specific RM bounds. */
5223 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5224 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5228 /* If we deferred processing of incomplete types, re-enable it. If there
5229 were no other disables and we have deferred types to process, do so. */
5230 if (this_deferred
5231 && --defer_incomplete_level == 0
5232 && defer_incomplete_list)
5234 struct incomplete *p, *next;
5236 /* We are back to level 0 for the deferring of incomplete types.
5237 But processing these incomplete types below may itself require
5238 deferring, so preserve what we have and restart from scratch. */
5239 p = defer_incomplete_list;
5240 defer_incomplete_list = NULL;
5242 for (; p; p = next)
5244 next = p->next;
5246 if (p->old_type)
5247 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5248 gnat_to_gnu_type (p->full_type));
5249 free (p);
5253 /* If we are not defining this type, see if it's on one of the lists of
5254 incomplete types. If so, handle the list entry now. */
5255 if (is_type && !definition)
5257 struct incomplete *p;
5259 for (p = defer_incomplete_list; p; p = p->next)
5260 if (p->old_type && p->full_type == gnat_entity)
5262 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5263 TREE_TYPE (gnu_decl));
5264 p->old_type = NULL_TREE;
5267 for (p = defer_limited_with; p; p = p->next)
5268 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5270 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5271 TREE_TYPE (gnu_decl));
5272 p->old_type = NULL_TREE;
5276 if (this_global)
5277 force_global--;
5279 /* If this is a packed array type whose original array type is itself
5280 an Itype without freeze node, make sure the latter is processed. */
5281 if (Is_Packed_Array_Impl_Type (gnat_entity)
5282 && Is_Itype (Original_Array_Type (gnat_entity))
5283 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5284 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5285 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5287 return gnu_decl;
5290 /* Similar, but if the returned value is a COMPONENT_REF, return the
5291 FIELD_DECL. */
5293 tree
5294 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5296 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5298 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5299 gnu_field = TREE_OPERAND (gnu_field, 1);
5301 return gnu_field;
5304 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5305 the GCC type corresponding to that entity. */
5307 tree
5308 gnat_to_gnu_type (Entity_Id gnat_entity)
5310 tree gnu_decl;
5312 /* The back end never attempts to annotate generic types. */
5313 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5314 return void_type_node;
5316 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5317 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5319 return TREE_TYPE (gnu_decl);
5322 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5323 the unpadded version of the GCC type corresponding to that entity. */
5325 tree
5326 get_unpadded_type (Entity_Id gnat_entity)
5328 tree type = gnat_to_gnu_type (gnat_entity);
5330 if (TYPE_IS_PADDING_P (type))
5331 type = TREE_TYPE (TYPE_FIELDS (type));
5333 return type;
5336 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5337 type has been changed to that of the parameterless procedure, except if an
5338 alias is already present, in which case it is returned instead. */
5340 tree
5341 get_minimal_subprog_decl (Entity_Id gnat_entity)
5343 tree gnu_entity_name, gnu_ext_name;
5344 struct attrib *attr_list = NULL;
5346 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5347 of the handling applied here. */
5349 while (Present (Alias (gnat_entity)))
5351 gnat_entity = Alias (gnat_entity);
5352 if (present_gnu_tree (gnat_entity))
5353 return get_gnu_tree (gnat_entity);
5356 gnu_entity_name = get_entity_name (gnat_entity);
5357 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5359 if (Has_Stdcall_Convention (gnat_entity))
5360 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5361 get_identifier ("stdcall"), NULL_TREE,
5362 gnat_entity);
5363 else if (Has_Thiscall_Convention (gnat_entity))
5364 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5365 get_identifier ("thiscall"), NULL_TREE,
5366 gnat_entity);
5368 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5369 gnu_ext_name = NULL_TREE;
5371 return
5372 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5373 is_disabled, true, true, true, attr_list, gnat_entity);
5376 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5377 a C++ imported method or equivalent.
5379 We use the predicate on 32-bit x86/Windows to find out whether we need to
5380 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5381 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5383 bool
5384 is_cplusplus_method (Entity_Id gnat_entity)
5386 if (Convention (gnat_entity) != Convention_CPP)
5387 return false;
5389 /* This is the main case: C++ method imported as a primitive operation. */
5390 if (Is_Dispatching_Operation (gnat_entity))
5391 return true;
5393 /* A thunk needs to be handled like its associated primitive operation. */
5394 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5395 return true;
5397 /* C++ classes with no virtual functions can be imported as limited
5398 record types, but we need to return true for the constructors. */
5399 if (Is_Constructor (gnat_entity))
5400 return true;
5402 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5403 if (Is_Dispatch_Table_Entity (gnat_entity))
5404 return true;
5406 return false;
5409 /* Finalize the processing of From_Limited_With incomplete types. */
5411 void
5412 finalize_from_limited_with (void)
5414 struct incomplete *p, *next;
5416 p = defer_limited_with;
5417 defer_limited_with = NULL;
5419 for (; p; p = next)
5421 next = p->next;
5423 if (p->old_type)
5424 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5425 gnat_to_gnu_type (p->full_type));
5426 free (p);
5430 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5431 kind of type (such E_Task_Type) that has a different type which Gigi
5432 uses for its representation. If the type does not have a special type
5433 for its representation, return GNAT_ENTITY. If a type is supposed to
5434 exist, but does not, abort unless annotating types, in which case
5435 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5437 Entity_Id
5438 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5440 Entity_Id gnat_equiv = gnat_entity;
5442 if (No (gnat_entity))
5443 return gnat_entity;
5445 switch (Ekind (gnat_entity))
5447 case E_Class_Wide_Subtype:
5448 if (Present (Equivalent_Type (gnat_entity)))
5449 gnat_equiv = Equivalent_Type (gnat_entity);
5450 break;
5452 case E_Access_Protected_Subprogram_Type:
5453 case E_Anonymous_Access_Protected_Subprogram_Type:
5454 gnat_equiv = Equivalent_Type (gnat_entity);
5455 break;
5457 case E_Class_Wide_Type:
5458 gnat_equiv = Root_Type (gnat_entity);
5459 break;
5461 case E_Task_Type:
5462 case E_Task_Subtype:
5463 case E_Protected_Type:
5464 case E_Protected_Subtype:
5465 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5466 break;
5468 default:
5469 break;
5472 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5474 return gnat_equiv;
5477 /* Return a GCC tree for a type corresponding to the component type of the
5478 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5479 is for an array being defined. DEBUG_INFO_P is true if we need to write
5480 debug information for other types that we may create in the process. */
5482 static tree
5483 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5484 bool debug_info_p)
5486 const Entity_Id gnat_type = Component_Type (gnat_array);
5487 tree gnu_type = gnat_to_gnu_type (gnat_type);
5488 tree gnu_comp_size;
5490 /* Try to get a smaller form of the component if needed. */
5491 if ((Is_Packed (gnat_array)
5492 || Has_Component_Size_Clause (gnat_array))
5493 && !Is_Bit_Packed_Array (gnat_array)
5494 && !Has_Aliased_Components (gnat_array)
5495 && !Strict_Alignment (gnat_type)
5496 && RECORD_OR_UNION_TYPE_P (gnu_type)
5497 && !TYPE_FAT_POINTER_P (gnu_type)
5498 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5499 gnu_type = make_packable_type (gnu_type, false);
5501 if (Has_Atomic_Components (gnat_array))
5502 check_ok_for_atomic (gnu_type, gnat_array, true);
5504 /* Get and validate any specified Component_Size. */
5505 gnu_comp_size
5506 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5507 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5508 true, Has_Component_Size_Clause (gnat_array));
5510 /* If the array has aliased components and the component size can be zero,
5511 force at least unit size to ensure that the components have distinct
5512 addresses. */
5513 if (!gnu_comp_size
5514 && Has_Aliased_Components (gnat_array)
5515 && (integer_zerop (TYPE_SIZE (gnu_type))
5516 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5517 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5518 gnu_comp_size
5519 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5521 /* If the component type is a RECORD_TYPE that has a self-referential size,
5522 then use the maximum size for the component size. */
5523 if (!gnu_comp_size
5524 && TREE_CODE (gnu_type) == RECORD_TYPE
5525 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5526 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5528 /* Honor the component size. This is not needed for bit-packed arrays. */
5529 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5531 tree orig_type = gnu_type;
5532 unsigned int max_align;
5534 /* If an alignment is specified, use it as a cap on the component type
5535 so that it can be honored for the whole type. But ignore it for the
5536 original type of packed array types. */
5537 if (No (Packed_Array_Impl_Type (gnat_array))
5538 && Known_Alignment (gnat_array))
5539 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5540 else
5541 max_align = 0;
5543 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5544 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5545 gnu_type = orig_type;
5546 else
5547 orig_type = gnu_type;
5549 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5550 true, false, definition, true);
5552 /* If a padding record was made, declare it now since it will never be
5553 declared otherwise. This is necessary to ensure that its subtrees
5554 are properly marked. */
5555 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5556 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5557 gnat_array);
5560 if (Has_Volatile_Components (gnat_array))
5561 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5563 return gnu_type;
5566 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5567 using MECH as its passing mechanism, to be placed in the parameter
5568 list built for GNAT_SUBPROG. Assume a foreign convention for the
5569 latter if FOREIGN is true. Also set CICO to true if the parameter
5570 must use the copy-in copy-out implementation mechanism.
5572 The returned tree is a PARM_DECL, except for those cases where no
5573 parameter needs to be actually passed to the subprogram; the type
5574 of this "shadow" parameter is then returned instead. */
5576 static tree
5577 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5578 Entity_Id gnat_subprog, bool foreign, bool *cico)
5580 tree gnu_param_name = get_entity_name (gnat_param);
5581 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5582 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5583 /* The parameter can be indirectly modified if its address is taken. */
5584 bool ro_param = in_param && !Address_Taken (gnat_param);
5585 bool by_return = false, by_component_ptr = false;
5586 bool by_ref = false;
5587 tree gnu_param;
5589 /* Copy-return is used only for the first parameter of a valued procedure.
5590 It's a copy mechanism for which a parameter is never allocated. */
5591 if (mech == By_Copy_Return)
5593 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5594 mech = By_Copy;
5595 by_return = true;
5598 /* If this is either a foreign function or if the underlying type won't
5599 be passed by reference, strip off possible padding type. */
5600 if (TYPE_IS_PADDING_P (gnu_param_type))
5602 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5604 if (mech == By_Reference
5605 || foreign
5606 || (!must_pass_by_ref (unpadded_type)
5607 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5608 gnu_param_type = unpadded_type;
5611 /* If this is a read-only parameter, make a variant of the type that is
5612 read-only. ??? However, if this is an unconstrained array, that type
5613 can be very complex, so skip it for now. Likewise for any other
5614 self-referential type. */
5615 if (ro_param
5616 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5617 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5618 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5620 /* For foreign conventions, pass arrays as pointers to the element type.
5621 First check for unconstrained array and get the underlying array. */
5622 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5623 gnu_param_type
5624 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5626 /* For GCC builtins, pass Address integer types as (void *) */
5627 if (Convention (gnat_subprog) == Convention_Intrinsic
5628 && Present (Interface_Name (gnat_subprog))
5629 && Is_Descendent_Of_Address (Etype (gnat_param)))
5630 gnu_param_type = ptr_void_type_node;
5632 /* Arrays are passed as pointers to element type for foreign conventions. */
5633 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5635 /* Strip off any multi-dimensional entries, then strip
5636 off the last array to get the component type. */
5637 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5638 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5639 gnu_param_type = TREE_TYPE (gnu_param_type);
5641 by_component_ptr = true;
5642 gnu_param_type = TREE_TYPE (gnu_param_type);
5644 if (ro_param)
5645 gnu_param_type
5646 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5648 gnu_param_type = build_pointer_type (gnu_param_type);
5651 /* Fat pointers are passed as thin pointers for foreign conventions. */
5652 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5653 gnu_param_type
5654 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5656 /* If we must pass or were requested to pass by reference, do so.
5657 If we were requested to pass by copy, do so.
5658 Otherwise, for foreign conventions, pass In Out or Out parameters
5659 or aggregates by reference. For COBOL and Fortran, pass all
5660 integer and FP types that way too. For Convention Ada, use
5661 the standard Ada default. */
5662 else if (must_pass_by_ref (gnu_param_type)
5663 || mech == By_Reference
5664 || (mech != By_Copy
5665 && ((foreign
5666 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5667 || (foreign
5668 && (Convention (gnat_subprog) == Convention_Fortran
5669 || Convention (gnat_subprog) == Convention_COBOL)
5670 && (INTEGRAL_TYPE_P (gnu_param_type)
5671 || FLOAT_TYPE_P (gnu_param_type)))
5672 || (!foreign
5673 && default_pass_by_ref (gnu_param_type)))))
5675 /* We take advantage of 6.2(12) by considering that references built for
5676 parameters whose type isn't by-ref and for which the mechanism hasn't
5677 been forced to by-ref are restrict-qualified in the C sense. */
5678 bool restrict_p
5679 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5680 gnu_param_type = build_reference_type (gnu_param_type);
5681 if (restrict_p)
5682 gnu_param_type
5683 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5684 by_ref = true;
5687 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5688 else if (!in_param)
5689 *cico = true;
5691 if (mech == By_Copy && (by_ref || by_component_ptr))
5692 post_error ("?cannot pass & by copy", gnat_param);
5694 /* If this is an Out parameter that isn't passed by reference and isn't
5695 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5696 it will be a VAR_DECL created when we process the procedure, so just
5697 return its type. For the special parameter of a valued procedure,
5698 never pass it in.
5700 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5701 Out parameters with discriminants or implicit initial values to be
5702 handled like In Out parameters. These type are normally built as
5703 aggregates, hence passed by reference, except for some packed arrays
5704 which end up encoded in special integer types. Note that scalars can
5705 be given implicit initial values using the Default_Value aspect.
5707 The exception we need to make is then for packed arrays of records
5708 with discriminants or implicit initial values. We have no light/easy
5709 way to check for the latter case, so we merely check for packed arrays
5710 of records. This may lead to useless copy-in operations, but in very
5711 rare cases only, as these would be exceptions in a set of already
5712 exceptional situations. */
5713 if (Ekind (gnat_param) == E_Out_Parameter
5714 && !by_ref
5715 && (by_return
5716 || (!POINTER_TYPE_P (gnu_param_type)
5717 && !AGGREGATE_TYPE_P (gnu_param_type)
5718 && !Has_Default_Aspect (Etype (gnat_param))))
5719 && !(Is_Array_Type (Etype (gnat_param))
5720 && Is_Packed (Etype (gnat_param))
5721 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5722 return gnu_param_type;
5724 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5725 ro_param || by_ref || by_component_ptr);
5726 DECL_BY_REF_P (gnu_param) = by_ref;
5727 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5728 DECL_POINTS_TO_READONLY_P (gnu_param)
5729 = (ro_param && (by_ref || by_component_ptr));
5730 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5732 /* If no Mechanism was specified, indicate what we're using, then
5733 back-annotate it. */
5734 if (mech == Default)
5735 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5737 Set_Mechanism (gnat_param, mech);
5738 return gnu_param;
5741 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5742 qualifiers on TYPE. */
5744 static tree
5745 change_qualified_type (tree type, int type_quals)
5747 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5750 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5752 static bool
5753 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5755 while (Present (Corresponding_Discriminant (discr1)))
5756 discr1 = Corresponding_Discriminant (discr1);
5758 while (Present (Corresponding_Discriminant (discr2)))
5759 discr2 = Corresponding_Discriminant (discr2);
5761 return
5762 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5765 /* Return true if the array type GNU_TYPE, which represents a dimension of
5766 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5768 static bool
5769 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5771 /* If the array type is not the innermost dimension of the GNAT type,
5772 then it has a non-aliased component. */
5773 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5774 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5775 return true;
5777 /* If the array type has an aliased component in the front-end sense,
5778 then it also has an aliased component in the back-end sense. */
5779 if (Has_Aliased_Components (gnat_type))
5780 return false;
5782 /* If this is a derived type, then it has a non-aliased component if
5783 and only if its parent type also has one. */
5784 if (Is_Derived_Type (gnat_type))
5786 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5787 int index;
5788 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5789 gnu_parent_type
5790 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5791 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5792 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5793 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5796 /* Otherwise, rely exclusively on properties of the element type. */
5797 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5800 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5802 static bool
5803 compile_time_known_address_p (Node_Id gnat_address)
5805 /* Catch System'To_Address. */
5806 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5807 gnat_address = Expression (gnat_address);
5809 return Compile_Time_Known_Value (gnat_address);
5812 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5813 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5815 static bool
5816 cannot_be_superflat_p (Node_Id gnat_range)
5818 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5819 Node_Id scalar_range;
5820 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5822 /* If the low bound is not constant, try to find an upper bound. */
5823 while (Nkind (gnat_lb) != N_Integer_Literal
5824 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5825 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5826 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5827 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5828 || Nkind (scalar_range) == N_Range))
5829 gnat_lb = High_Bound (scalar_range);
5831 /* If the high bound is not constant, try to find a lower bound. */
5832 while (Nkind (gnat_hb) != N_Integer_Literal
5833 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5834 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5835 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5836 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5837 || Nkind (scalar_range) == N_Range))
5838 gnat_hb = Low_Bound (scalar_range);
5840 /* If we have failed to find constant bounds, punt. */
5841 if (Nkind (gnat_lb) != N_Integer_Literal
5842 || Nkind (gnat_hb) != N_Integer_Literal)
5843 return false;
5845 /* We need at least a signed 64-bit type to catch most cases. */
5846 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5847 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5848 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5849 return false;
5851 /* If the low bound is the smallest integer, nothing can be smaller. */
5852 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5853 if (TREE_OVERFLOW (gnu_lb_minus_one))
5854 return true;
5856 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5859 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5861 static bool
5862 constructor_address_p (tree gnu_expr)
5864 while (TREE_CODE (gnu_expr) == NOP_EXPR
5865 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5866 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5867 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5869 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5870 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5873 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5874 be elaborated at the point of its definition, but do nothing else. */
5876 void
5877 elaborate_entity (Entity_Id gnat_entity)
5879 switch (Ekind (gnat_entity))
5881 case E_Signed_Integer_Subtype:
5882 case E_Modular_Integer_Subtype:
5883 case E_Enumeration_Subtype:
5884 case E_Ordinary_Fixed_Point_Subtype:
5885 case E_Decimal_Fixed_Point_Subtype:
5886 case E_Floating_Point_Subtype:
5888 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5889 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5891 /* ??? Tests to avoid Constraint_Error in static expressions
5892 are needed until after the front stops generating bogus
5893 conversions on bounds of real types. */
5894 if (!Raises_Constraint_Error (gnat_lb))
5895 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5896 true, false, Needs_Debug_Info (gnat_entity));
5897 if (!Raises_Constraint_Error (gnat_hb))
5898 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5899 true, false, Needs_Debug_Info (gnat_entity));
5900 break;
5903 case E_Record_Subtype:
5904 case E_Private_Subtype:
5905 case E_Limited_Private_Subtype:
5906 case E_Record_Subtype_With_Private:
5907 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5909 Node_Id gnat_discriminant_expr;
5910 Entity_Id gnat_field;
5912 for (gnat_field
5913 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5914 gnat_discriminant_expr
5915 = First_Elmt (Discriminant_Constraint (gnat_entity));
5916 Present (gnat_field);
5917 gnat_field = Next_Discriminant (gnat_field),
5918 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5919 /* Ignore access discriminants. */
5920 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5921 elaborate_expression (Node (gnat_discriminant_expr),
5922 gnat_entity, get_entity_name (gnat_field),
5923 true, false, false);
5925 break;
5930 /* Return true if the size in units represented by GNU_SIZE can be handled by
5931 an allocation. If STATIC_P is true, consider only what can be done with a
5932 static allocation. */
5934 static bool
5935 allocatable_size_p (tree gnu_size, bool static_p)
5937 /* We can allocate a fixed size if it is a valid for the middle-end. */
5938 if (TREE_CODE (gnu_size) == INTEGER_CST)
5939 return valid_constant_size_p (gnu_size);
5941 /* We can allocate a variable size if this isn't a static allocation. */
5942 else
5943 return !static_p;
5946 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5947 NAME, ARGS and ERROR_POINT. */
5949 static void
5950 prepend_one_attribute (struct attrib **attr_list,
5951 enum attr_type attr_type,
5952 tree attr_name,
5953 tree attr_args,
5954 Node_Id attr_error_point)
5956 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5958 attr->type = attr_type;
5959 attr->name = attr_name;
5960 attr->args = attr_args;
5961 attr->error_point = attr_error_point;
5963 attr->next = *attr_list;
5964 *attr_list = attr;
5967 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
5969 static void
5970 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
5972 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
5973 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5974 enum attr_type etype;
5976 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
5977 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
5979 case Pragma_Machine_Attribute:
5980 etype = ATTR_MACHINE_ATTRIBUTE;
5981 break;
5983 case Pragma_Linker_Alias:
5984 etype = ATTR_LINK_ALIAS;
5985 break;
5987 case Pragma_Linker_Section:
5988 etype = ATTR_LINK_SECTION;
5989 break;
5991 case Pragma_Linker_Constructor:
5992 etype = ATTR_LINK_CONSTRUCTOR;
5993 break;
5995 case Pragma_Linker_Destructor:
5996 etype = ATTR_LINK_DESTRUCTOR;
5997 break;
5999 case Pragma_Weak_External:
6000 etype = ATTR_WEAK_EXTERNAL;
6001 break;
6003 case Pragma_Thread_Local_Storage:
6004 etype = ATTR_THREAD_LOCAL_STORAGE;
6005 break;
6007 default:
6008 return;
6011 /* See what arguments we have and turn them into GCC trees for attribute
6012 handlers. These expect identifier for strings. We handle at most two
6013 arguments and static expressions only. */
6014 if (Present (gnat_arg) && Present (First (gnat_arg)))
6016 Node_Id gnat_arg0 = Next (First (gnat_arg));
6017 Node_Id gnat_arg1 = Empty;
6019 if (Present (gnat_arg0)
6020 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6022 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6024 if (TREE_CODE (gnu_arg0) == STRING_CST)
6026 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6027 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6028 return;
6031 gnat_arg1 = Next (gnat_arg0);
6034 if (Present (gnat_arg1)
6035 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6037 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6039 if (TREE_CODE (gnu_arg1) == STRING_CST)
6040 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6044 /* Prepend to the list. Make a list of the argument we might have, as GCC
6045 expects it. */
6046 prepend_one_attribute (attr_list, etype, gnu_arg0,
6047 gnu_arg1
6048 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6049 Present (Next (First (gnat_arg)))
6050 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6053 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6055 static void
6056 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6058 Node_Id gnat_temp;
6060 /* Attributes are stored as Representation Item pragmas. */
6061 for (gnat_temp = First_Rep_Item (gnat_entity);
6062 Present (gnat_temp);
6063 gnat_temp = Next_Rep_Item (gnat_temp))
6064 if (Nkind (gnat_temp) == N_Pragma)
6065 prepend_one_attribute_pragma (attr_list, gnat_temp);
6068 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6069 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6070 return the GCC tree to use for that expression. GNU_NAME is the suffix
6071 to use if a variable needs to be created and DEFINITION is true if this
6072 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6073 otherwise, we are just elaborating the expression for side-effects. If
6074 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6075 isn't needed for code generation. */
6077 static tree
6078 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6079 bool definition, bool need_value, bool need_debug)
6081 tree gnu_expr;
6083 /* If we already elaborated this expression (e.g. it was involved
6084 in the definition of a private type), use the old value. */
6085 if (present_gnu_tree (gnat_expr))
6086 return get_gnu_tree (gnat_expr);
6088 /* If we don't need a value and this is static or a discriminant,
6089 we don't need to do anything. */
6090 if (!need_value
6091 && (Is_OK_Static_Expression (gnat_expr)
6092 || (Nkind (gnat_expr) == N_Identifier
6093 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6094 return NULL_TREE;
6096 /* If it's a static expression, we don't need a variable for debugging. */
6097 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6098 need_debug = false;
6100 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6101 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6102 gnu_name, definition, need_debug);
6104 /* Save the expression in case we try to elaborate this entity again. Since
6105 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6106 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6107 save_gnu_tree (gnat_expr, gnu_expr, true);
6109 return need_value ? gnu_expr : error_mark_node;
6112 /* Similar, but take a GNU expression and always return a result. */
6114 static tree
6115 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6116 bool definition, bool need_debug)
6118 const bool expr_public_p = Is_Public (gnat_entity);
6119 const bool expr_global_p = expr_public_p || global_bindings_p ();
6120 bool expr_variable_p, use_variable;
6122 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6123 reference will have been replaced with a COMPONENT_REF when the type
6124 is being elaborated. However, there are some cases involving child
6125 types where we will. So convert it to a COMPONENT_REF. We hope it
6126 will be at the highest level of the expression in these cases. */
6127 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6128 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6129 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6130 gnu_expr, NULL_TREE);
6132 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6133 that an expression cannot contain both a discriminant and a variable. */
6134 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6135 return gnu_expr;
6137 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6138 a variable that is initialized to contain the expression when the package
6139 containing the definition is elaborated. If this entity is defined at top
6140 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6141 if this is necessary. */
6142 if (CONSTANT_CLASS_P (gnu_expr))
6143 expr_variable_p = false;
6144 else
6146 /* Skip any conversions and simple constant arithmetics to see if the
6147 expression is based on a read-only variable.
6148 ??? This really should remain read-only, but we have to think about
6149 the typing of the tree here. */
6150 tree inner = remove_conversions (gnu_expr, true);
6152 inner = skip_simple_constant_arithmetic (inner);
6154 if (handled_component_p (inner))
6156 HOST_WIDE_INT bitsize, bitpos;
6157 tree offset;
6158 machine_mode mode;
6159 int unsignedp, volatilep;
6161 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6162 &mode, &unsignedp, &volatilep, false);
6163 /* If the offset is variable, err on the side of caution. */
6164 if (offset)
6165 inner = NULL_TREE;
6168 expr_variable_p
6169 = !(inner
6170 && TREE_CODE (inner) == VAR_DECL
6171 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6174 /* We only need to use the variable if we are in a global context since GCC
6175 can do the right thing in the local case. However, when not optimizing,
6176 use it for bounds of loop iteration scheme to avoid code duplication. */
6177 use_variable = expr_variable_p
6178 && (expr_global_p
6179 || (!optimize
6180 && definition
6181 && Is_Itype (gnat_entity)
6182 && Nkind (Associated_Node_For_Itype (gnat_entity))
6183 == N_Loop_Parameter_Specification));
6185 /* Now create it, possibly only for debugging purposes. */
6186 if (use_variable || need_debug)
6188 /* The following variable creation can happen when processing the body of
6189 subprograms that are defined out of the extended main unit and
6190 inlined. In this case, we are not at the global scope, and thus the
6191 new variable must not be tagged "external", as we used to do here as
6192 long as definition == 0. */
6193 const bool external_flag = !definition && expr_global_p;
6194 tree gnu_decl
6195 = create_var_decl_1
6196 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6197 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6198 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6200 DECL_ARTIFICIAL (gnu_decl) = 1;
6202 /* Using this variable at debug time (if need_debug is true) requires a
6203 proper location. The back-end will compute a location for this
6204 variable only if the variable is used by the generated code.
6205 Returning the variable ensures the caller will use it in generated
6206 code. Note that there is no need for a location if the debug info
6207 contains an integer constant.
6208 FIXME: when the encoding-based debug scheme is dropped, move this
6209 condition to the top-level IF block: we will not need to create a
6210 variable anymore in such cases, then. */
6211 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6212 return gnu_decl;
6215 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6218 /* Similar, but take an alignment factor and make it explicit in the tree. */
6220 static tree
6221 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6222 bool definition, bool need_debug, unsigned int align)
6224 tree unit_align = size_int (align / BITS_PER_UNIT);
6225 return
6226 size_binop (MULT_EXPR,
6227 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6228 gnu_expr,
6229 unit_align),
6230 gnat_entity, gnu_name, definition,
6231 need_debug),
6232 unit_align);
6235 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6236 the value passed against the list of choices. */
6238 tree
6239 choices_to_gnu (tree operand, Node_Id choices)
6241 Node_Id choice;
6242 Node_Id gnat_temp;
6243 tree result = boolean_false_node;
6244 tree this_test, low = 0, high = 0, single = 0;
6246 for (choice = First (choices); Present (choice); choice = Next (choice))
6248 switch (Nkind (choice))
6250 case N_Range:
6251 low = gnat_to_gnu (Low_Bound (choice));
6252 high = gnat_to_gnu (High_Bound (choice));
6254 this_test
6255 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6256 build_binary_op (GE_EXPR, boolean_type_node,
6257 operand, low),
6258 build_binary_op (LE_EXPR, boolean_type_node,
6259 operand, high));
6261 break;
6263 case N_Subtype_Indication:
6264 gnat_temp = Range_Expression (Constraint (choice));
6265 low = gnat_to_gnu (Low_Bound (gnat_temp));
6266 high = gnat_to_gnu (High_Bound (gnat_temp));
6268 this_test
6269 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6270 build_binary_op (GE_EXPR, boolean_type_node,
6271 operand, low),
6272 build_binary_op (LE_EXPR, boolean_type_node,
6273 operand, high));
6274 break;
6276 case N_Identifier:
6277 case N_Expanded_Name:
6278 /* This represents either a subtype range, an enumeration
6279 literal, or a constant Ekind says which. If an enumeration
6280 literal or constant, fall through to the next case. */
6281 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6282 && Ekind (Entity (choice)) != E_Constant)
6284 tree type = gnat_to_gnu_type (Entity (choice));
6286 low = TYPE_MIN_VALUE (type);
6287 high = TYPE_MAX_VALUE (type);
6289 this_test
6290 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6291 build_binary_op (GE_EXPR, boolean_type_node,
6292 operand, low),
6293 build_binary_op (LE_EXPR, boolean_type_node,
6294 operand, high));
6295 break;
6298 /* ... fall through ... */
6300 case N_Character_Literal:
6301 case N_Integer_Literal:
6302 single = gnat_to_gnu (choice);
6303 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6304 single);
6305 break;
6307 case N_Others_Choice:
6308 this_test = boolean_true_node;
6309 break;
6311 default:
6312 gcc_unreachable ();
6315 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6316 this_test);
6319 return result;
6322 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6323 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6325 static int
6326 adjust_packed (tree field_type, tree record_type, int packed)
6328 /* If the field contains an item of variable size, we cannot pack it
6329 because we cannot create temporaries of non-fixed size in case
6330 we need to take the address of the field. See addressable_p and
6331 the notes on the addressability issues for further details. */
6332 if (type_has_variable_size (field_type))
6333 return 0;
6335 /* If the alignment of the record is specified and the field type
6336 is over-aligned, request Storage_Unit alignment for the field. */
6337 if (packed == -2)
6339 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6340 return -1;
6341 else
6342 return 0;
6345 return packed;
6348 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6349 placed in GNU_RECORD_TYPE.
6351 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6352 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6353 record has a specified alignment.
6355 DEFINITION is true if this field is for a record being defined.
6357 DEBUG_INFO_P is true if we need to write debug information for types
6358 that we may create in the process. */
6360 static tree
6361 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6362 bool definition, bool debug_info_p)
6364 const Entity_Id gnat_field_type = Etype (gnat_field);
6365 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6366 tree gnu_field_id = get_entity_name (gnat_field);
6367 tree gnu_field, gnu_size, gnu_pos;
6368 bool is_volatile
6369 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6370 bool needs_strict_alignment
6371 = (is_volatile
6372 || Is_Aliased (gnat_field)
6373 || Strict_Alignment (gnat_field_type));
6375 /* If this field requires strict alignment, we cannot pack it because
6376 it would very likely be under-aligned in the record. */
6377 if (needs_strict_alignment)
6378 packed = 0;
6379 else
6380 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6382 /* If a size is specified, use it. Otherwise, if the record type is packed,
6383 use the official RM size. See "Handling of Type'Size Values" in Einfo
6384 for further details. */
6385 if (Known_Esize (gnat_field))
6386 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6387 gnat_field, FIELD_DECL, false, true);
6388 else if (packed == 1)
6389 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6390 gnat_field, FIELD_DECL, false, true);
6391 else
6392 gnu_size = NULL_TREE;
6394 /* If we have a specified size that is smaller than that of the field's type,
6395 or a position is specified, and the field's type is a record that doesn't
6396 require strict alignment, see if we can get either an integral mode form
6397 of the type or a smaller form. If we can, show a size was specified for
6398 the field if there wasn't one already, so we know to make this a bitfield
6399 and avoid making things wider.
6401 Changing to an integral mode form is useful when the record is packed as
6402 we can then place the field at a non-byte-aligned position and so achieve
6403 tighter packing. This is in addition required if the field shares a byte
6404 with another field and the front-end lets the back-end handle the access
6405 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6407 Changing to a smaller form is required if the specified size is smaller
6408 than that of the field's type and the type contains sub-fields that are
6409 padded, in order to avoid generating accesses to these sub-fields that
6410 are wider than the field.
6412 We avoid the transformation if it is not required or potentially useful,
6413 as it might entail an increase of the field's alignment and have ripple
6414 effects on the outer record type. A typical case is a field known to be
6415 byte-aligned and not to share a byte with another field. */
6416 if (!needs_strict_alignment
6417 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6418 && !TYPE_FAT_POINTER_P (gnu_field_type)
6419 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6420 && (packed == 1
6421 || (gnu_size
6422 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6423 || (Present (Component_Clause (gnat_field))
6424 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6425 % BITS_PER_UNIT == 0
6426 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6428 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6429 if (gnu_packable_type != gnu_field_type)
6431 gnu_field_type = gnu_packable_type;
6432 if (!gnu_size)
6433 gnu_size = rm_size (gnu_field_type);
6437 if (Is_Atomic (gnat_field))
6438 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6440 if (Present (Component_Clause (gnat_field)))
6442 Entity_Id gnat_parent
6443 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6445 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6446 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6447 gnat_field, FIELD_DECL, false, true);
6449 /* Ensure the position does not overlap with the parent subtype, if there
6450 is one. This test is omitted if the parent of the tagged type has a
6451 full rep clause since, in this case, component clauses are allowed to
6452 overlay the space allocated for the parent type and the front-end has
6453 checked that there are no overlapping components. */
6454 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6456 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6458 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6459 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6461 post_error_ne_tree
6462 ("offset of& must be beyond parent{, minimum allowed is ^}",
6463 First_Bit (Component_Clause (gnat_field)), gnat_field,
6464 TYPE_SIZE_UNIT (gnu_parent));
6468 /* If this field needs strict alignment, check that the record is
6469 sufficiently aligned and that position and size are consistent with
6470 the alignment. But don't do it if we are just annotating types and
6471 the field's type is tagged, since tagged types aren't fully laid out
6472 in this mode. Also, note that atomic implies volatile so the inner
6473 test sequences ordering is significant here. */
6474 if (needs_strict_alignment
6475 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6477 TYPE_ALIGN (gnu_record_type)
6478 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6480 if (gnu_size
6481 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6483 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6484 post_error_ne_tree
6485 ("atomic field& must be natural size of type{ (^)}",
6486 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6487 TYPE_SIZE (gnu_field_type));
6489 else if (is_volatile)
6490 post_error_ne_tree
6491 ("volatile field& must be natural size of type{ (^)}",
6492 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6493 TYPE_SIZE (gnu_field_type));
6495 else if (Is_Aliased (gnat_field))
6496 post_error_ne_tree
6497 ("size of aliased field& must be ^ bits",
6498 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6499 TYPE_SIZE (gnu_field_type));
6501 else if (Strict_Alignment (gnat_field_type))
6502 post_error_ne_tree
6503 ("size of & with aliased or tagged components not ^ bits",
6504 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6505 TYPE_SIZE (gnu_field_type));
6507 else
6508 gcc_unreachable ();
6510 gnu_size = NULL_TREE;
6513 if (!integer_zerop (size_binop
6514 (TRUNC_MOD_EXPR, gnu_pos,
6515 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6517 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6518 post_error_ne_num
6519 ("position of atomic field& must be multiple of ^ bits",
6520 First_Bit (Component_Clause (gnat_field)), gnat_field,
6521 TYPE_ALIGN (gnu_field_type));
6523 else if (is_volatile)
6524 post_error_ne_num
6525 ("position of volatile field& must be multiple of ^ bits",
6526 First_Bit (Component_Clause (gnat_field)), gnat_field,
6527 TYPE_ALIGN (gnu_field_type));
6529 else if (Is_Aliased (gnat_field))
6530 post_error_ne_num
6531 ("position of aliased field& must be multiple of ^ bits",
6532 First_Bit (Component_Clause (gnat_field)), gnat_field,
6533 TYPE_ALIGN (gnu_field_type));
6535 else if (Strict_Alignment (gnat_field_type))
6536 post_error_ne
6537 ("position of & is not compatible with alignment required "
6538 "by its components",
6539 First_Bit (Component_Clause (gnat_field)), gnat_field);
6541 else
6542 gcc_unreachable ();
6544 gnu_pos = NULL_TREE;
6549 /* If the record has rep clauses and this is the tag field, make a rep
6550 clause for it as well. */
6551 else if (Has_Specified_Layout (Scope (gnat_field))
6552 && Chars (gnat_field) == Name_uTag)
6554 gnu_pos = bitsize_zero_node;
6555 gnu_size = TYPE_SIZE (gnu_field_type);
6558 else
6560 gnu_pos = NULL_TREE;
6562 /* If we are packing the record and the field is BLKmode, round the
6563 size up to a byte boundary. */
6564 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6565 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6568 /* We need to make the size the maximum for the type if it is
6569 self-referential and an unconstrained type. In that case, we can't
6570 pack the field since we can't make a copy to align it. */
6571 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6572 && !gnu_size
6573 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6574 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6576 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6577 packed = 0;
6580 /* If a size is specified, adjust the field's type to it. */
6581 if (gnu_size)
6583 tree orig_field_type;
6585 /* If the field's type is justified modular, we would need to remove
6586 the wrapper to (better) meet the layout requirements. However we
6587 can do so only if the field is not aliased to preserve the unique
6588 layout and if the prescribed size is not greater than that of the
6589 packed array to preserve the justification. */
6590 if (!needs_strict_alignment
6591 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6592 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6593 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6594 <= 0)
6595 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6597 /* Similarly if the field's type is a misaligned integral type, but
6598 there is no restriction on the size as there is no justification. */
6599 if (!needs_strict_alignment
6600 && TYPE_IS_PADDING_P (gnu_field_type)
6601 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6602 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6604 gnu_field_type
6605 = make_type_from_size (gnu_field_type, gnu_size,
6606 Has_Biased_Representation (gnat_field));
6608 orig_field_type = gnu_field_type;
6609 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6610 false, false, definition, true);
6612 /* If a padding record was made, declare it now since it will never be
6613 declared otherwise. This is necessary to ensure that its subtrees
6614 are properly marked. */
6615 if (gnu_field_type != orig_field_type
6616 && !DECL_P (TYPE_NAME (gnu_field_type)))
6617 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6618 debug_info_p, gnat_field);
6621 /* Otherwise (or if there was an error), don't specify a position. */
6622 else
6623 gnu_pos = NULL_TREE;
6625 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6626 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6628 /* Now create the decl for the field. */
6629 gnu_field
6630 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6631 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6632 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6633 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6634 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6636 if (Ekind (gnat_field) == E_Discriminant)
6637 DECL_DISCRIMINANT_NUMBER (gnu_field)
6638 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6640 return gnu_field;
6643 /* Return true if at least one member of COMPONENT_LIST needs strict
6644 alignment. */
6646 static bool
6647 components_need_strict_alignment (Node_Id component_list)
6649 Node_Id component_decl;
6651 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6652 Present (component_decl);
6653 component_decl = Next_Non_Pragma (component_decl))
6655 Entity_Id gnat_field = Defining_Entity (component_decl);
6657 if (Is_Aliased (gnat_field))
6658 return true;
6660 if (Strict_Alignment (Etype (gnat_field)))
6661 return true;
6664 return false;
6667 /* Return true if TYPE is a type with variable size or a padding type with a
6668 field of variable size or a record that has a field with such a type. */
6670 static bool
6671 type_has_variable_size (tree type)
6673 tree field;
6675 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6676 return true;
6678 if (TYPE_IS_PADDING_P (type)
6679 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6680 return true;
6682 if (!RECORD_OR_UNION_TYPE_P (type))
6683 return false;
6685 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6686 if (type_has_variable_size (TREE_TYPE (field)))
6687 return true;
6689 return false;
6692 /* Return true if FIELD is an artificial field. */
6694 static bool
6695 field_is_artificial (tree field)
6697 /* These fields are generated by the front-end proper. */
6698 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6699 return true;
6701 /* These fields are generated by gigi. */
6702 if (DECL_INTERNAL_P (field))
6703 return true;
6705 return false;
6708 /* Return true if FIELD is a non-artificial aliased field. */
6710 static bool
6711 field_is_aliased (tree field)
6713 if (field_is_artificial (field))
6714 return false;
6716 return DECL_ALIASED_P (field);
6719 /* Return true if FIELD is a non-artificial field with self-referential
6720 size. */
6722 static bool
6723 field_has_self_size (tree field)
6725 if (field_is_artificial (field))
6726 return false;
6728 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6729 return false;
6731 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6734 /* Return true if FIELD is a non-artificial field with variable size. */
6736 static bool
6737 field_has_variable_size (tree field)
6739 if (field_is_artificial (field))
6740 return false;
6742 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6743 return false;
6745 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6748 /* qsort comparer for the bit positions of two record components. */
6750 static int
6751 compare_field_bitpos (const PTR rt1, const PTR rt2)
6753 const_tree const field1 = * (const_tree const *) rt1;
6754 const_tree const field2 = * (const_tree const *) rt2;
6755 const int ret
6756 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6758 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6761 /* Structure holding information for a given variant. */
6762 typedef struct vinfo
6764 /* The record type of the variant. */
6765 tree type;
6767 /* The name of the variant. */
6768 tree name;
6770 /* The qualifier of the variant. */
6771 tree qual;
6773 /* Whether the variant has a rep clause. */
6774 bool has_rep;
6776 /* Whether the variant is packed. */
6777 bool packed;
6779 } vinfo_t;
6781 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6782 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6783 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6784 When called from gnat_to_gnu_entity during the processing of a record type
6785 definition, the GCC node for the parent, if any, will be the single field
6786 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6787 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6788 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6790 PACKED is 1 if this is for a packed record, -1 if this is for a record
6791 with Component_Alignment of Storage_Unit, -2 if this is for a record
6792 with a specified alignment.
6794 DEFINITION is true if we are defining this record type.
6796 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6797 out the record. This means the alignment only serves to force fields to
6798 be bitfields, but not to require the record to be that aligned. This is
6799 used for variants.
6801 ALL_REP is true if a rep clause is present for all the fields.
6803 UNCHECKED_UNION is true if we are building this type for a record with a
6804 Pragma Unchecked_Union.
6806 ARTIFICIAL is true if this is a type that was generated by the compiler.
6808 DEBUG_INFO is true if we need to write debug information about the type.
6810 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6811 mean that its contents may be unused as well, only the container itself.
6813 REORDER is true if we are permitted to reorder components of this type.
6815 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6816 the outer record type down to this variant level. It is nonzero only if
6817 all the fields down to this level have a rep clause and ALL_REP is false.
6819 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6820 with a rep clause is to be added; in this case, that is all that should
6821 be done with such fields and the return value will be false. */
6823 static bool
6824 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6825 tree gnu_field_list, int packed, bool definition,
6826 bool cancel_alignment, bool all_rep,
6827 bool unchecked_union, bool artificial,
6828 bool debug_info, bool maybe_unused, bool reorder,
6829 tree first_free_pos, tree *p_gnu_rep_list)
6831 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6832 bool variants_have_rep = all_rep;
6833 bool layout_with_rep = false;
6834 bool has_self_field = false;
6835 bool has_aliased_after_self_field = false;
6836 Node_Id component_decl, variant_part;
6837 tree gnu_field, gnu_next, gnu_last;
6838 tree gnu_variant_part = NULL_TREE;
6839 tree gnu_rep_list = NULL_TREE;
6840 tree gnu_var_list = NULL_TREE;
6841 tree gnu_self_list = NULL_TREE;
6842 tree gnu_zero_list = NULL_TREE;
6844 /* For each component referenced in a component declaration create a GCC
6845 field and add it to the list, skipping pragmas in the GNAT list. */
6846 gnu_last = tree_last (gnu_field_list);
6847 if (Present (Component_Items (gnat_component_list)))
6848 for (component_decl
6849 = First_Non_Pragma (Component_Items (gnat_component_list));
6850 Present (component_decl);
6851 component_decl = Next_Non_Pragma (component_decl))
6853 Entity_Id gnat_field = Defining_Entity (component_decl);
6854 Name_Id gnat_name = Chars (gnat_field);
6856 /* If present, the _Parent field must have been created as the single
6857 field of the record type. Put it before any other fields. */
6858 if (gnat_name == Name_uParent)
6860 gnu_field = TYPE_FIELDS (gnu_record_type);
6861 gnu_field_list = chainon (gnu_field_list, gnu_field);
6863 else
6865 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6866 definition, debug_info);
6868 /* If this is the _Tag field, put it before any other fields. */
6869 if (gnat_name == Name_uTag)
6870 gnu_field_list = chainon (gnu_field_list, gnu_field);
6872 /* If this is the _Controller field, put it before the other
6873 fields except for the _Tag or _Parent field. */
6874 else if (gnat_name == Name_uController && gnu_last)
6876 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6877 DECL_CHAIN (gnu_last) = gnu_field;
6880 /* If this is a regular field, put it after the other fields. */
6881 else
6883 DECL_CHAIN (gnu_field) = gnu_field_list;
6884 gnu_field_list = gnu_field;
6885 if (!gnu_last)
6886 gnu_last = gnu_field;
6888 /* And record information for the final layout. */
6889 if (field_has_self_size (gnu_field))
6890 has_self_field = true;
6891 else if (has_self_field && field_is_aliased (gnu_field))
6892 has_aliased_after_self_field = true;
6896 save_gnu_tree (gnat_field, gnu_field, false);
6899 /* At the end of the component list there may be a variant part. */
6900 variant_part = Variant_Part (gnat_component_list);
6902 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6903 mutually exclusive and should go in the same memory. To do this we need
6904 to treat each variant as a record whose elements are created from the
6905 component list for the variant. So here we create the records from the
6906 lists for the variants and put them all into the QUAL_UNION_TYPE.
6907 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6908 use GNU_RECORD_TYPE if there are no fields so far. */
6909 if (Present (variant_part))
6911 Node_Id gnat_discr = Name (variant_part), variant;
6912 tree gnu_discr = gnat_to_gnu (gnat_discr);
6913 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
6914 tree gnu_var_name
6915 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6916 "XVN");
6917 tree gnu_union_type, gnu_union_name;
6918 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6919 bool union_field_needs_strict_alignment = false;
6920 auto_vec <vinfo_t, 16> variant_types;
6921 vinfo_t *gnu_variant;
6922 unsigned int variants_align = 0;
6923 unsigned int i;
6925 gnu_union_name
6926 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6928 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6929 are all in the variant part, to match the layout of C unions. There
6930 is an associated check below. */
6931 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6932 gnu_union_type = gnu_record_type;
6933 else
6935 gnu_union_type
6936 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6938 TYPE_NAME (gnu_union_type) = gnu_union_name;
6939 TYPE_ALIGN (gnu_union_type) = 0;
6940 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6943 /* If all the fields down to this level have a rep clause, find out
6944 whether all the fields at this level also have one. If so, then
6945 compute the new first free position to be passed downward. */
6946 this_first_free_pos = first_free_pos;
6947 if (this_first_free_pos)
6949 for (gnu_field = gnu_field_list;
6950 gnu_field;
6951 gnu_field = DECL_CHAIN (gnu_field))
6952 if (DECL_FIELD_OFFSET (gnu_field))
6954 tree pos = bit_position (gnu_field);
6955 if (!tree_int_cst_lt (pos, this_first_free_pos))
6956 this_first_free_pos
6957 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
6959 else
6961 this_first_free_pos = NULL_TREE;
6962 break;
6966 /* We build the variants in two passes. The bulk of the work is done in
6967 the first pass, that is to say translating the GNAT nodes, building
6968 the container types and computing the associated properties. However
6969 we cannot finish up the container types during this pass because we
6970 don't know where the variant part will be placed until the end. */
6971 for (variant = First_Non_Pragma (Variants (variant_part));
6972 Present (variant);
6973 variant = Next_Non_Pragma (variant))
6975 tree gnu_variant_type = make_node (RECORD_TYPE);
6976 tree gnu_inner_name, gnu_qual;
6977 bool has_rep;
6978 int field_packed;
6979 vinfo_t vinfo;
6981 Get_Variant_Encoding (variant);
6982 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
6983 TYPE_NAME (gnu_variant_type)
6984 = concat_name (gnu_union_name,
6985 IDENTIFIER_POINTER (gnu_inner_name));
6987 /* Set the alignment of the inner type in case we need to make
6988 inner objects into bitfields, but then clear it out so the
6989 record actually gets only the alignment required. */
6990 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6991 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6993 /* Similarly, if the outer record has a size specified and all
6994 the fields have a rep clause, we can propagate the size. */
6995 if (all_rep_and_size)
6997 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6998 TYPE_SIZE_UNIT (gnu_variant_type)
6999 = TYPE_SIZE_UNIT (gnu_record_type);
7002 /* Add the fields into the record type for the variant. Note that
7003 we aren't sure to really use it at this point, see below. */
7004 has_rep
7005 = components_to_record (gnu_variant_type, Component_List (variant),
7006 NULL_TREE, packed, definition,
7007 !all_rep_and_size, all_rep,
7008 unchecked_union,
7009 true, debug_info, true, reorder,
7010 this_first_free_pos,
7011 all_rep || this_first_free_pos
7012 ? NULL : &gnu_rep_list);
7014 /* Translate the qualifier and annotate the GNAT node. */
7015 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7016 Set_Present_Expr (variant, annotate_value (gnu_qual));
7018 /* Deal with packedness like in gnat_to_gnu_field. */
7019 if (components_need_strict_alignment (Component_List (variant)))
7021 field_packed = 0;
7022 union_field_needs_strict_alignment = true;
7024 else
7025 field_packed
7026 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7028 /* Push this variant onto the stack for the second pass. */
7029 vinfo.type = gnu_variant_type;
7030 vinfo.name = gnu_inner_name;
7031 vinfo.qual = gnu_qual;
7032 vinfo.has_rep = has_rep;
7033 vinfo.packed = field_packed;
7034 variant_types.safe_push (vinfo);
7036 /* Compute the global properties that will determine the placement of
7037 the variant part. */
7038 variants_have_rep |= has_rep;
7039 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7040 variants_align = TYPE_ALIGN (gnu_variant_type);
7043 /* Round up the first free position to the alignment of the variant part
7044 for the variants without rep clause. This will guarantee a consistent
7045 layout independently of the placement of the variant part. */
7046 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7047 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7049 /* In the second pass, the container types are adjusted if necessary and
7050 finished up, then the corresponding fields of the variant part are
7051 built with their qualifier, unless this is an unchecked union. */
7052 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7054 tree gnu_variant_type = gnu_variant->type;
7055 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7057 /* If this is an Unchecked_Union whose fields are all in the variant
7058 part and we have a single field with no representation clause or
7059 placed at offset zero, use the field directly to match the layout
7060 of C unions. */
7061 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7062 && gnu_field_list
7063 && !DECL_CHAIN (gnu_field_list)
7064 && (!DECL_FIELD_OFFSET (gnu_field_list)
7065 || integer_zerop (bit_position (gnu_field_list))))
7067 gnu_field = gnu_field_list;
7068 DECL_CONTEXT (gnu_field) = gnu_record_type;
7070 else
7072 /* Finalize the variant type now. We used to throw away empty
7073 record types but we no longer do that because we need them to
7074 generate complete debug info for the variant; otherwise, the
7075 union type definition will be lacking the fields associated
7076 with these empty variants. */
7077 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7079 /* The variant part will be at offset 0 so we need to ensure
7080 that the fields are laid out starting from the first free
7081 position at this level. */
7082 tree gnu_rep_type = make_node (RECORD_TYPE);
7083 tree gnu_rep_part;
7084 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7085 gnu_rep_part
7086 = create_rep_part (gnu_rep_type, gnu_variant_type,
7087 this_first_free_pos);
7088 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7089 gnu_field_list = gnu_rep_part;
7090 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7091 false);
7094 if (debug_info)
7095 rest_of_record_type_compilation (gnu_variant_type);
7096 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7097 true, debug_info, gnat_component_list);
7099 gnu_field
7100 = create_field_decl (gnu_variant->name, gnu_variant_type,
7101 gnu_union_type,
7102 all_rep_and_size
7103 ? TYPE_SIZE (gnu_variant_type) : 0,
7104 variants_have_rep ? bitsize_zero_node : 0,
7105 gnu_variant->packed, 0);
7107 DECL_INTERNAL_P (gnu_field) = 1;
7109 if (!unchecked_union)
7110 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7113 DECL_CHAIN (gnu_field) = gnu_variant_list;
7114 gnu_variant_list = gnu_field;
7117 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7118 if (gnu_variant_list)
7120 int union_field_packed;
7122 if (all_rep_and_size)
7124 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7125 TYPE_SIZE_UNIT (gnu_union_type)
7126 = TYPE_SIZE_UNIT (gnu_record_type);
7129 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7130 all_rep_and_size ? 1 : 0, debug_info);
7132 /* If GNU_UNION_TYPE is our record type, it means we must have an
7133 Unchecked_Union with no fields. Verify that and, if so, just
7134 return. */
7135 if (gnu_union_type == gnu_record_type)
7137 gcc_assert (unchecked_union
7138 && !gnu_field_list
7139 && !gnu_rep_list);
7140 return variants_have_rep;
7143 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7144 debug_info, gnat_component_list);
7146 /* Deal with packedness like in gnat_to_gnu_field. */
7147 if (union_field_needs_strict_alignment)
7148 union_field_packed = 0;
7149 else
7150 union_field_packed
7151 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7153 gnu_variant_part
7154 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7155 all_rep_and_size
7156 ? TYPE_SIZE (gnu_union_type) : 0,
7157 variants_have_rep ? bitsize_zero_node : 0,
7158 union_field_packed, 0);
7160 DECL_INTERNAL_P (gnu_variant_part) = 1;
7164 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7165 permitted to reorder components, self-referential sizes or variable sizes.
7166 If they do, pull them out and put them onto the appropriate list. We have
7167 to do this in a separate pass since we want to handle the discriminants
7168 but can't play with them until we've used them in debugging data above.
7170 Similarly, pull out the fields with zero size and no rep clause, as they
7171 would otherwise modify the layout and thus very likely run afoul of the
7172 Ada semantics, which are different from those of C here.
7174 ??? If we reorder them, debugging information will be wrong but there is
7175 nothing that can be done about this at the moment. */
7176 gnu_last = NULL_TREE;
7178 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7179 do { \
7180 if (gnu_last) \
7181 DECL_CHAIN (gnu_last) = gnu_next; \
7182 else \
7183 gnu_field_list = gnu_next; \
7185 DECL_CHAIN (gnu_field) = (LIST); \
7186 (LIST) = gnu_field; \
7187 } while (0)
7189 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7191 gnu_next = DECL_CHAIN (gnu_field);
7193 if (DECL_FIELD_OFFSET (gnu_field))
7195 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7196 continue;
7199 if ((reorder || has_aliased_after_self_field)
7200 && field_has_self_size (gnu_field))
7202 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7203 continue;
7206 if (reorder && field_has_variable_size (gnu_field))
7208 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7209 continue;
7212 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7214 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7215 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7216 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7217 if (field_is_aliased (gnu_field))
7218 TYPE_ALIGN (gnu_record_type)
7219 = MAX (TYPE_ALIGN (gnu_record_type),
7220 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7221 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7222 continue;
7225 gnu_last = gnu_field;
7228 #undef MOVE_FROM_FIELD_LIST_TO
7230 gnu_field_list = nreverse (gnu_field_list);
7232 /* If permitted, we reorder the fields as follows:
7234 1) all fixed length fields,
7235 2) all fields whose length doesn't depend on discriminants,
7236 3) all fields whose length depends on discriminants,
7237 4) the variant part,
7239 within the record and within each variant recursively. */
7240 if (reorder)
7241 gnu_field_list
7242 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7244 /* Otherwise, if there is an aliased field placed after a field whose length
7245 depends on discriminants, we put all the fields of the latter sort, last.
7246 We need to do this in case an object of this record type is mutable. */
7247 else if (has_aliased_after_self_field)
7248 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7250 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7251 in our REP list to the previous level because this level needs them in
7252 order to do a correct layout, i.e. avoid having overlapping fields. */
7253 if (p_gnu_rep_list && gnu_rep_list)
7254 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7256 /* Otherwise, sort the fields by bit position and put them into their own
7257 record, before the others, if we also have fields without rep clause. */
7258 else if (gnu_rep_list)
7260 tree gnu_rep_type, gnu_rep_part;
7261 int i, len = list_length (gnu_rep_list);
7262 tree *gnu_arr = XALLOCAVEC (tree, len);
7264 /* If all the fields have a rep clause, we can do a flat layout. */
7265 layout_with_rep = !gnu_field_list
7266 && (!gnu_variant_part || variants_have_rep);
7267 gnu_rep_type
7268 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7270 for (gnu_field = gnu_rep_list, i = 0;
7271 gnu_field;
7272 gnu_field = DECL_CHAIN (gnu_field), i++)
7273 gnu_arr[i] = gnu_field;
7275 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7277 /* Put the fields in the list in order of increasing position, which
7278 means we start from the end. */
7279 gnu_rep_list = NULL_TREE;
7280 for (i = len - 1; i >= 0; i--)
7282 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7283 gnu_rep_list = gnu_arr[i];
7284 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7287 if (layout_with_rep)
7288 gnu_field_list = gnu_rep_list;
7289 else
7291 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7293 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7294 without rep clause are laid out starting from this position.
7295 Therefore, we force it as a minimal size on the REP part. */
7296 gnu_rep_part
7297 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7299 /* Chain the REP part at the beginning of the field list. */
7300 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7301 gnu_field_list = gnu_rep_part;
7305 /* Chain the variant part at the end of the field list. */
7306 if (gnu_variant_part)
7307 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7309 if (cancel_alignment)
7310 TYPE_ALIGN (gnu_record_type) = 0;
7312 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7314 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7315 debug_info && !maybe_unused);
7317 /* Chain the fields with zero size at the beginning of the field list. */
7318 if (gnu_zero_list)
7319 TYPE_FIELDS (gnu_record_type)
7320 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7322 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7325 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7326 placed into an Esize, Component_Bit_Offset, or Component_Size value
7327 in the GNAT tree. */
7329 static Uint
7330 annotate_value (tree gnu_size)
7332 TCode tcode;
7333 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7334 struct tree_int_map in;
7335 int i;
7337 /* See if we've already saved the value for this node. */
7338 if (EXPR_P (gnu_size))
7340 struct tree_int_map *e;
7342 in.base.from = gnu_size;
7343 e = (struct tree_int_map *) htab_find (annotate_value_cache, &in);
7345 if (e)
7346 return (Node_Ref_Or_Val) e->to;
7348 else
7349 in.base.from = NULL_TREE;
7351 /* If we do not return inside this switch, TCODE will be set to the
7352 code to use for a Create_Node operand and LEN (set above) will be
7353 the number of recursive calls for us to make. */
7355 switch (TREE_CODE (gnu_size))
7357 case INTEGER_CST:
7358 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7360 case COMPONENT_REF:
7361 /* The only case we handle here is a simple discriminant reference. */
7362 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7364 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7366 /* Climb up the chain of successive extensions, if any. */
7367 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7368 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7369 == parent_name_id)
7370 gnu_size = TREE_OPERAND (gnu_size, 0);
7372 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7373 return
7374 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7377 return No_Uint;
7379 CASE_CONVERT: case NON_LVALUE_EXPR:
7380 return annotate_value (TREE_OPERAND (gnu_size, 0));
7382 /* Now just list the operations we handle. */
7383 case COND_EXPR: tcode = Cond_Expr; break;
7384 case PLUS_EXPR: tcode = Plus_Expr; break;
7385 case MINUS_EXPR: tcode = Minus_Expr; break;
7386 case MULT_EXPR: tcode = Mult_Expr; break;
7387 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7388 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7389 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7390 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7391 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7392 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7393 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7394 case NEGATE_EXPR: tcode = Negate_Expr; break;
7395 case MIN_EXPR: tcode = Min_Expr; break;
7396 case MAX_EXPR: tcode = Max_Expr; break;
7397 case ABS_EXPR: tcode = Abs_Expr; break;
7398 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7399 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7400 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7401 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7402 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7403 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7404 case LT_EXPR: tcode = Lt_Expr; break;
7405 case LE_EXPR: tcode = Le_Expr; break;
7406 case GT_EXPR: tcode = Gt_Expr; break;
7407 case GE_EXPR: tcode = Ge_Expr; break;
7408 case EQ_EXPR: tcode = Eq_Expr; break;
7409 case NE_EXPR: tcode = Ne_Expr; break;
7411 case BIT_AND_EXPR:
7412 tcode = Bit_And_Expr;
7413 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7414 Such values appear in expressions with aligning patterns. Note that,
7415 since sizetype is unsigned, we have to jump through some hoops. */
7416 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7418 tree op1 = TREE_OPERAND (gnu_size, 1);
7419 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7420 if (wi::neg_p (signed_op1))
7422 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7423 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7426 break;
7428 case CALL_EXPR:
7429 /* In regular mode, inline back only if symbolic annotation is requested
7430 in order to avoid memory explosion on big discriminated record types.
7431 But not in ASIS mode, as symbolic annotation is required for DDA. */
7432 if (List_Representation_Info == 3 || type_annotate_only)
7434 tree t = maybe_inline_call_in_expr (gnu_size);
7435 if (t)
7436 return annotate_value (t);
7438 else
7439 return Uint_Minus_1;
7441 /* Fall through... */
7443 default:
7444 return No_Uint;
7447 /* Now get each of the operands that's relevant for this code. If any
7448 cannot be expressed as a repinfo node, say we can't. */
7449 for (i = 0; i < 3; i++)
7450 ops[i] = No_Uint;
7452 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7454 if (i == 1 && pre_op1 != No_Uint)
7455 ops[i] = pre_op1;
7456 else
7457 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7458 if (ops[i] == No_Uint)
7459 return No_Uint;
7462 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7464 /* Save the result in the cache. */
7465 if (in.base.from)
7467 struct tree_int_map **h;
7468 /* We can't assume the hash table data hasn't moved since the initial
7469 look up, so we have to search again. Allocating and inserting an
7470 entry at that point would be an alternative, but then we'd better
7471 discard the entry if we decided not to cache it. */
7472 h = (struct tree_int_map **)
7473 htab_find_slot (annotate_value_cache, &in, INSERT);
7474 gcc_assert (!*h);
7475 *h = ggc_alloc<tree_int_map> ();
7476 (*h)->base.from = gnu_size;
7477 (*h)->to = ret;
7480 return ret;
7483 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7484 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7485 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7486 BY_REF is true if the object is used by reference. */
7488 void
7489 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7491 if (by_ref)
7493 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7494 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7495 else
7496 gnu_type = TREE_TYPE (gnu_type);
7499 if (Unknown_Esize (gnat_entity))
7501 if (TREE_CODE (gnu_type) == RECORD_TYPE
7502 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7503 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7504 else if (!size)
7505 size = TYPE_SIZE (gnu_type);
7507 if (size)
7508 Set_Esize (gnat_entity, annotate_value (size));
7511 if (Unknown_Alignment (gnat_entity))
7512 Set_Alignment (gnat_entity,
7513 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7516 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7517 Return NULL_TREE if there is no such element in the list. */
7519 static tree
7520 purpose_member_field (const_tree elem, tree list)
7522 while (list)
7524 tree field = TREE_PURPOSE (list);
7525 if (SAME_FIELD_P (field, elem))
7526 return list;
7527 list = TREE_CHAIN (list);
7529 return NULL_TREE;
7532 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7533 set Component_Bit_Offset and Esize of the components to the position and
7534 size used by Gigi. */
7536 static void
7537 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7539 Entity_Id gnat_field;
7540 tree gnu_list;
7542 /* We operate by first making a list of all fields and their position (we
7543 can get the size easily) and then update all the sizes in the tree. */
7544 gnu_list
7545 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7546 BIGGEST_ALIGNMENT, NULL_TREE);
7548 for (gnat_field = First_Entity (gnat_entity);
7549 Present (gnat_field);
7550 gnat_field = Next_Entity (gnat_field))
7551 if (Ekind (gnat_field) == E_Component
7552 || (Ekind (gnat_field) == E_Discriminant
7553 && !Is_Unchecked_Union (Scope (gnat_field))))
7555 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7556 gnu_list);
7557 if (t)
7559 tree parent_offset;
7561 /* If we are just annotating types and the type is tagged, the tag
7562 and the parent components are not generated by the front-end so
7563 we need to add the appropriate offset to each component without
7564 representation clause. */
7565 if (type_annotate_only
7566 && Is_Tagged_Type (gnat_entity)
7567 && No (Component_Clause (gnat_field)))
7569 /* For a component appearing in the current extension, the
7570 offset is the size of the parent. */
7571 if (Is_Derived_Type (gnat_entity)
7572 && Original_Record_Component (gnat_field) == gnat_field)
7573 parent_offset
7574 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7575 bitsizetype);
7576 else
7577 parent_offset = bitsize_int (POINTER_SIZE);
7579 if (TYPE_FIELDS (gnu_type))
7580 parent_offset
7581 = round_up (parent_offset,
7582 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7584 else
7585 parent_offset = bitsize_zero_node;
7587 Set_Component_Bit_Offset
7588 (gnat_field,
7589 annotate_value
7590 (size_binop (PLUS_EXPR,
7591 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7592 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7593 parent_offset)));
7595 Set_Esize (gnat_field,
7596 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7598 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7600 /* If there is no entry, this is an inherited component whose
7601 position is the same as in the parent type. */
7602 Set_Component_Bit_Offset
7603 (gnat_field,
7604 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7606 Set_Esize (gnat_field,
7607 Esize (Original_Record_Component (gnat_field)));
7612 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7613 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7614 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7615 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7616 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7617 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7618 pre-existing list to be chained to the newly created entries. */
7620 static tree
7621 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7622 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7624 tree gnu_field;
7626 for (gnu_field = TYPE_FIELDS (gnu_type);
7627 gnu_field;
7628 gnu_field = DECL_CHAIN (gnu_field))
7630 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7631 DECL_FIELD_BIT_OFFSET (gnu_field));
7632 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7633 DECL_FIELD_OFFSET (gnu_field));
7634 unsigned int our_offset_align
7635 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7636 tree v = make_tree_vec (3);
7638 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7639 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7640 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7641 gnu_list = tree_cons (gnu_field, v, gnu_list);
7643 /* Recurse on internal fields, flattening the nested fields except for
7644 those in the variant part, if requested. */
7645 if (DECL_INTERNAL_P (gnu_field))
7647 tree gnu_field_type = TREE_TYPE (gnu_field);
7648 if (do_not_flatten_variant
7649 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7650 gnu_list
7651 = build_position_list (gnu_field_type, do_not_flatten_variant,
7652 size_zero_node, bitsize_zero_node,
7653 BIGGEST_ALIGNMENT, gnu_list);
7654 else
7655 gnu_list
7656 = build_position_list (gnu_field_type, do_not_flatten_variant,
7657 gnu_our_offset, gnu_our_bitpos,
7658 our_offset_align, gnu_list);
7662 return gnu_list;
7665 /* Return a list describing the substitutions needed to reflect the
7666 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7667 be in any order. The values in an element of the list are in the form
7668 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7669 a definition of GNAT_SUBTYPE. */
7671 static vec<subst_pair>
7672 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7674 vec<subst_pair> gnu_list = vNULL;
7675 Entity_Id gnat_discrim;
7676 Node_Id gnat_constr;
7678 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7679 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7680 Present (gnat_discrim);
7681 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7682 gnat_constr = Next_Elmt (gnat_constr))
7683 /* Ignore access discriminants. */
7684 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7686 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7687 tree replacement = convert (TREE_TYPE (gnu_field),
7688 elaborate_expression
7689 (Node (gnat_constr), gnat_subtype,
7690 get_entity_name (gnat_discrim),
7691 definition, true, false));
7692 subst_pair s = {gnu_field, replacement};
7693 gnu_list.safe_push (s);
7696 return gnu_list;
7699 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7700 variants of QUAL_UNION_TYPE that are still relevant after applying
7701 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7702 list to be prepended to the newly created entries. */
7704 static vec<variant_desc>
7705 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7706 vec<variant_desc> gnu_list)
7708 tree gnu_field;
7710 for (gnu_field = TYPE_FIELDS (qual_union_type);
7711 gnu_field;
7712 gnu_field = DECL_CHAIN (gnu_field))
7714 tree qual = DECL_QUALIFIER (gnu_field);
7715 unsigned int i;
7716 subst_pair *s;
7718 FOR_EACH_VEC_ELT (subst_list, i, s)
7719 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7721 /* If the new qualifier is not unconditionally false, its variant may
7722 still be accessed. */
7723 if (!integer_zerop (qual))
7725 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7726 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7728 gnu_list.safe_push (v);
7730 /* Recurse on the variant subpart of the variant, if any. */
7731 variant_subpart = get_variant_part (variant_type);
7732 if (variant_subpart)
7733 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7734 subst_list, gnu_list);
7736 /* If the new qualifier is unconditionally true, the subsequent
7737 variants cannot be accessed. */
7738 if (integer_onep (qual))
7739 break;
7743 return gnu_list;
7746 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7747 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7748 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7749 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7750 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7751 true if we are being called to process the Component_Size of GNAT_OBJECT;
7752 this is used only for error messages. ZERO_OK is true if a size of zero
7753 is permitted; if ZERO_OK is false, it means that a size of zero should be
7754 treated as an unspecified size. */
7756 static tree
7757 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7758 enum tree_code kind, bool component_p, bool zero_ok)
7760 Node_Id gnat_error_node;
7761 tree type_size, size;
7763 /* Return 0 if no size was specified. */
7764 if (uint_size == No_Uint)
7765 return NULL_TREE;
7767 /* Ignore a negative size since that corresponds to our back-annotation. */
7768 if (UI_Lt (uint_size, Uint_0))
7769 return NULL_TREE;
7771 /* Find the node to use for error messages. */
7772 if ((Ekind (gnat_object) == E_Component
7773 || Ekind (gnat_object) == E_Discriminant)
7774 && Present (Component_Clause (gnat_object)))
7775 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7776 else if (Present (Size_Clause (gnat_object)))
7777 gnat_error_node = Expression (Size_Clause (gnat_object));
7778 else
7779 gnat_error_node = gnat_object;
7781 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7782 but cannot be represented in bitsizetype. */
7783 size = UI_To_gnu (uint_size, bitsizetype);
7784 if (TREE_OVERFLOW (size))
7786 if (component_p)
7787 post_error_ne ("component size for& is too large", gnat_error_node,
7788 gnat_object);
7789 else
7790 post_error_ne ("size for& is too large", gnat_error_node,
7791 gnat_object);
7792 return NULL_TREE;
7795 /* Ignore a zero size if it is not permitted. */
7796 if (!zero_ok && integer_zerop (size))
7797 return NULL_TREE;
7799 /* The size of objects is always a multiple of a byte. */
7800 if (kind == VAR_DECL
7801 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7803 if (component_p)
7804 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7805 gnat_error_node, gnat_object);
7806 else
7807 post_error_ne ("size for& is not a multiple of Storage_Unit",
7808 gnat_error_node, gnat_object);
7809 return NULL_TREE;
7812 /* If this is an integral type or a packed array type, the front-end has
7813 already verified the size, so we need not do it here (which would mean
7814 checking against the bounds). However, if this is an aliased object,
7815 it may not be smaller than the type of the object. */
7816 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7817 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7818 return size;
7820 /* If the object is a record that contains a template, add the size of the
7821 template to the specified size. */
7822 if (TREE_CODE (gnu_type) == RECORD_TYPE
7823 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7824 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7826 if (kind == VAR_DECL
7827 /* If a type needs strict alignment, a component of this type in
7828 a packed record cannot be packed and thus uses the type size. */
7829 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7830 type_size = TYPE_SIZE (gnu_type);
7831 else
7832 type_size = rm_size (gnu_type);
7834 /* Modify the size of a discriminated type to be the maximum size. */
7835 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7836 type_size = max_size (type_size, true);
7838 /* If this is an access type or a fat pointer, the minimum size is that given
7839 by the smallest integral mode that's valid for pointers. */
7840 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7842 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7843 while (!targetm.valid_pointer_mode (p_mode))
7844 p_mode = GET_MODE_WIDER_MODE (p_mode);
7845 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7848 /* Issue an error either if the default size of the object isn't a constant
7849 or if the new size is smaller than it. */
7850 if (TREE_CODE (type_size) != INTEGER_CST
7851 || TREE_OVERFLOW (type_size)
7852 || tree_int_cst_lt (size, type_size))
7854 if (component_p)
7855 post_error_ne_tree
7856 ("component size for& too small{, minimum allowed is ^}",
7857 gnat_error_node, gnat_object, type_size);
7858 else
7859 post_error_ne_tree
7860 ("size for& too small{, minimum allowed is ^}",
7861 gnat_error_node, gnat_object, type_size);
7862 return NULL_TREE;
7865 return size;
7868 /* Similarly, but both validate and process a value of RM size. This routine
7869 is only called for types. */
7871 static void
7872 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7874 Node_Id gnat_attr_node;
7875 tree old_size, size;
7877 /* Do nothing if no size was specified. */
7878 if (uint_size == No_Uint)
7879 return;
7881 /* Ignore a negative size since that corresponds to our back-annotation. */
7882 if (UI_Lt (uint_size, Uint_0))
7883 return;
7885 /* Only issue an error if a Value_Size clause was explicitly given.
7886 Otherwise, we'd be duplicating an error on the Size clause. */
7887 gnat_attr_node
7888 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7890 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7891 but cannot be represented in bitsizetype. */
7892 size = UI_To_gnu (uint_size, bitsizetype);
7893 if (TREE_OVERFLOW (size))
7895 if (Present (gnat_attr_node))
7896 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7897 gnat_entity);
7898 return;
7901 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7902 exists, or this is an integer type, in which case the front-end will
7903 have always set it. */
7904 if (No (gnat_attr_node)
7905 && integer_zerop (size)
7906 && !Has_Size_Clause (gnat_entity)
7907 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7908 return;
7910 old_size = rm_size (gnu_type);
7912 /* If the old size is self-referential, get the maximum size. */
7913 if (CONTAINS_PLACEHOLDER_P (old_size))
7914 old_size = max_size (old_size, true);
7916 /* Issue an error either if the old size of the object isn't a constant or
7917 if the new size is smaller than it. The front-end has already verified
7918 this for scalar and packed array types. */
7919 if (TREE_CODE (old_size) != INTEGER_CST
7920 || TREE_OVERFLOW (old_size)
7921 || (AGGREGATE_TYPE_P (gnu_type)
7922 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7923 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7924 && !(TYPE_IS_PADDING_P (gnu_type)
7925 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7926 && TYPE_PACKED_ARRAY_TYPE_P
7927 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7928 && tree_int_cst_lt (size, old_size)))
7930 if (Present (gnat_attr_node))
7931 post_error_ne_tree
7932 ("Value_Size for& too small{, minimum allowed is ^}",
7933 gnat_attr_node, gnat_entity, old_size);
7934 return;
7937 /* Otherwise, set the RM size proper for integral types... */
7938 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7939 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7940 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7941 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7942 SET_TYPE_RM_SIZE (gnu_type, size);
7944 /* ...or the Ada size for record and union types. */
7945 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
7946 && !TYPE_FAT_POINTER_P (gnu_type))
7947 SET_TYPE_ADA_SIZE (gnu_type, size);
7950 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7951 a type or object whose present alignment is ALIGN. If this alignment is
7952 valid, return it. Otherwise, give an error and return ALIGN. */
7954 static unsigned int
7955 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7957 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7958 unsigned int new_align;
7959 Node_Id gnat_error_node;
7961 /* Don't worry about checking alignment if alignment was not specified
7962 by the source program and we already posted an error for this entity. */
7963 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7964 return align;
7966 /* Post the error on the alignment clause if any. Note, for the implicit
7967 base type of an array type, the alignment clause is on the first
7968 subtype. */
7969 if (Present (Alignment_Clause (gnat_entity)))
7970 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7972 else if (Is_Itype (gnat_entity)
7973 && Is_Array_Type (gnat_entity)
7974 && Etype (gnat_entity) == gnat_entity
7975 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7976 gnat_error_node =
7977 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
7979 else
7980 gnat_error_node = gnat_entity;
7982 /* Within GCC, an alignment is an integer, so we must make sure a value is
7983 specified that fits in that range. Also, there is an upper bound to
7984 alignments we can support/allow. */
7985 if (!UI_Is_In_Int_Range (alignment)
7986 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7987 post_error_ne_num ("largest supported alignment for& is ^",
7988 gnat_error_node, gnat_entity, max_allowed_alignment);
7989 else if (!(Present (Alignment_Clause (gnat_entity))
7990 && From_At_Mod (Alignment_Clause (gnat_entity)))
7991 && new_align * BITS_PER_UNIT < align)
7993 unsigned int double_align;
7994 bool is_capped_double, align_clause;
7996 /* If the default alignment of "double" or larger scalar types is
7997 specifically capped and the new alignment is above the cap, do
7998 not post an error and change the alignment only if there is an
7999 alignment clause; this makes it possible to have the associated
8000 GCC type overaligned by default for performance reasons. */
8001 if ((double_align = double_float_alignment) > 0)
8003 Entity_Id gnat_type
8004 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8005 is_capped_double
8006 = is_double_float_or_array (gnat_type, &align_clause);
8008 else if ((double_align = double_scalar_alignment) > 0)
8010 Entity_Id gnat_type
8011 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8012 is_capped_double
8013 = is_double_scalar_or_array (gnat_type, &align_clause);
8015 else
8016 is_capped_double = align_clause = false;
8018 if (is_capped_double && new_align >= double_align)
8020 if (align_clause)
8021 align = new_align * BITS_PER_UNIT;
8023 else
8025 if (is_capped_double)
8026 align = double_align * BITS_PER_UNIT;
8028 post_error_ne_num ("alignment for& must be at least ^",
8029 gnat_error_node, gnat_entity,
8030 align / BITS_PER_UNIT);
8033 else
8035 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8036 if (new_align > align)
8037 align = new_align;
8040 return align;
8043 /* Verify that OBJECT, a type or decl, is something we can implement
8044 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8045 if we require atomic components. */
8047 static void
8048 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8050 Node_Id gnat_error_point = gnat_entity;
8051 Node_Id gnat_node;
8052 machine_mode mode;
8053 unsigned int align;
8054 tree size;
8056 /* There are three case of what OBJECT can be. It can be a type, in which
8057 case we take the size, alignment and mode from the type. It can be a
8058 declaration that was indirect, in which case the relevant values are
8059 that of the type being pointed to, or it can be a normal declaration,
8060 in which case the values are of the decl. The code below assumes that
8061 OBJECT is either a type or a decl. */
8062 if (TYPE_P (object))
8064 /* If this is an anonymous base type, nothing to check. Error will be
8065 reported on the source type. */
8066 if (!Comes_From_Source (gnat_entity))
8067 return;
8069 mode = TYPE_MODE (object);
8070 align = TYPE_ALIGN (object);
8071 size = TYPE_SIZE (object);
8073 else if (DECL_BY_REF_P (object))
8075 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8076 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8077 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8079 else
8081 mode = DECL_MODE (object);
8082 align = DECL_ALIGN (object);
8083 size = DECL_SIZE (object);
8086 /* Consider all floating-point types atomic and any types that that are
8087 represented by integers no wider than a machine word. */
8088 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8089 || ((GET_MODE_CLASS (mode) == MODE_INT
8090 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8091 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8092 return;
8094 /* For the moment, also allow anything that has an alignment equal
8095 to its size and which is smaller than a word. */
8096 if (size && TREE_CODE (size) == INTEGER_CST
8097 && compare_tree_int (size, align) == 0
8098 && align <= BITS_PER_WORD)
8099 return;
8101 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8102 gnat_node = Next_Rep_Item (gnat_node))
8104 if (!comp_p && Nkind (gnat_node) == N_Pragma
8105 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8106 == Pragma_Atomic))
8107 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8108 else if (comp_p && Nkind (gnat_node) == N_Pragma
8109 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8110 == Pragma_Atomic_Components))
8111 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8114 if (comp_p)
8115 post_error_ne ("atomic access to component of & cannot be guaranteed",
8116 gnat_error_point, gnat_entity);
8117 else
8118 post_error_ne ("atomic access to & cannot be guaranteed",
8119 gnat_error_point, gnat_entity);
8123 /* Helper for the intrin compatibility checks family. Evaluate whether
8124 two types are definitely incompatible. */
8126 static bool
8127 intrin_types_incompatible_p (tree t1, tree t2)
8129 enum tree_code code;
8131 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8132 return false;
8134 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8135 return true;
8137 if (TREE_CODE (t1) != TREE_CODE (t2))
8138 return true;
8140 code = TREE_CODE (t1);
8142 switch (code)
8144 case INTEGER_TYPE:
8145 case REAL_TYPE:
8146 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8148 case POINTER_TYPE:
8149 case REFERENCE_TYPE:
8150 /* Assume designated types are ok. We'd need to account for char * and
8151 void * variants to do better, which could rapidly get messy and isn't
8152 clearly worth the effort. */
8153 return false;
8155 default:
8156 break;
8159 return false;
8162 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8163 on the Ada/builtin argument lists for the INB binding. */
8165 static bool
8166 intrin_arglists_compatible_p (intrin_binding_t * inb)
8168 function_args_iterator ada_iter, btin_iter;
8170 function_args_iter_init (&ada_iter, inb->ada_fntype);
8171 function_args_iter_init (&btin_iter, inb->btin_fntype);
8173 /* Sequence position of the last argument we checked. */
8174 int argpos = 0;
8176 while (1)
8178 tree ada_type = function_args_iter_cond (&ada_iter);
8179 tree btin_type = function_args_iter_cond (&btin_iter);
8181 /* If we've exhausted both lists simultaneously, we're done. */
8182 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8183 break;
8185 /* If one list is shorter than the other, they fail to match. */
8186 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8187 return false;
8189 /* If we're done with the Ada args and not with the internal builtin
8190 args, or the other way around, complain. */
8191 if (ada_type == void_type_node
8192 && btin_type != void_type_node)
8194 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8195 return false;
8198 if (btin_type == void_type_node
8199 && ada_type != void_type_node)
8201 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8202 inb->gnat_entity, inb->gnat_entity, argpos);
8203 return false;
8206 /* Otherwise, check that types match for the current argument. */
8207 argpos ++;
8208 if (intrin_types_incompatible_p (ada_type, btin_type))
8210 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8211 inb->gnat_entity, inb->gnat_entity, argpos);
8212 return false;
8216 function_args_iter_next (&ada_iter);
8217 function_args_iter_next (&btin_iter);
8220 return true;
8223 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8224 on the Ada/builtin return values for the INB binding. */
8226 static bool
8227 intrin_return_compatible_p (intrin_binding_t * inb)
8229 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8230 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8232 /* Accept function imported as procedure, common and convenient. */
8233 if (VOID_TYPE_P (ada_return_type)
8234 && !VOID_TYPE_P (btin_return_type))
8235 return true;
8237 /* If return type is Address (integer type), map it to void *. */
8238 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8239 ada_return_type = ptr_void_type_node;
8241 /* Check return types compatibility otherwise. Note that this
8242 handles void/void as well. */
8243 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8245 post_error ("?intrinsic binding type mismatch on return value!",
8246 inb->gnat_entity);
8247 return false;
8250 return true;
8253 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8254 compatible. Issue relevant warnings when they are not.
8256 This is intended as a light check to diagnose the most obvious cases, not
8257 as a full fledged type compatibility predicate. It is the programmer's
8258 responsibility to ensure correctness of the Ada declarations in Imports,
8259 especially when binding straight to a compiler internal. */
8261 static bool
8262 intrin_profiles_compatible_p (intrin_binding_t * inb)
8264 /* Check compatibility on return values and argument lists, each responsible
8265 for posting warnings as appropriate. Ensure use of the proper sloc for
8266 this purpose. */
8268 bool arglists_compatible_p, return_compatible_p;
8269 location_t saved_location = input_location;
8271 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8273 return_compatible_p = intrin_return_compatible_p (inb);
8274 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8276 input_location = saved_location;
8278 return return_compatible_p && arglists_compatible_p;
8281 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8282 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8283 specified size for this field. POS_LIST is a position list describing
8284 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8285 to this layout. */
8287 static tree
8288 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8289 tree size, tree pos_list,
8290 vec<subst_pair> subst_list)
8292 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8293 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8294 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8295 tree new_pos, new_field;
8296 unsigned int i;
8297 subst_pair *s;
8299 if (CONTAINS_PLACEHOLDER_P (pos))
8300 FOR_EACH_VEC_ELT (subst_list, i, s)
8301 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8303 /* If the position is now a constant, we can set it as the position of the
8304 field when we make it. Otherwise, we need to deal with it specially. */
8305 if (TREE_CONSTANT (pos))
8306 new_pos = bit_from_pos (pos, bitpos);
8307 else
8308 new_pos = NULL_TREE;
8310 new_field
8311 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8312 size, new_pos, DECL_PACKED (old_field),
8313 !DECL_NONADDRESSABLE_P (old_field));
8315 if (!new_pos)
8317 normalize_offset (&pos, &bitpos, offset_align);
8318 /* Finalize the position. */
8319 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8320 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8321 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8322 DECL_SIZE (new_field) = size;
8323 DECL_SIZE_UNIT (new_field)
8324 = convert (sizetype,
8325 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8326 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8329 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8330 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8331 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8332 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8334 return new_field;
8337 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8338 it is the minimal size the REP_PART must have. */
8340 static tree
8341 create_rep_part (tree rep_type, tree record_type, tree min_size)
8343 tree field;
8345 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8346 min_size = NULL_TREE;
8348 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8349 min_size, NULL_TREE, 0, 1);
8350 DECL_INTERNAL_P (field) = 1;
8352 return field;
8355 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8357 static tree
8358 get_rep_part (tree record_type)
8360 tree field = TYPE_FIELDS (record_type);
8362 /* The REP part is the first field, internal, another record, and its name
8363 starts with an 'R'. */
8364 if (field
8365 && DECL_INTERNAL_P (field)
8366 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8367 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8368 return field;
8370 return NULL_TREE;
8373 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8375 tree
8376 get_variant_part (tree record_type)
8378 tree field;
8380 /* The variant part is the only internal field that is a qualified union. */
8381 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8382 if (DECL_INTERNAL_P (field)
8383 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8384 return field;
8386 return NULL_TREE;
8389 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8390 the list of variants to be used and RECORD_TYPE is the type of the parent.
8391 POS_LIST is a position list describing the layout of fields present in
8392 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8393 layout. */
8395 static tree
8396 create_variant_part_from (tree old_variant_part,
8397 vec<variant_desc> variant_list,
8398 tree record_type, tree pos_list,
8399 vec<subst_pair> subst_list)
8401 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8402 tree old_union_type = TREE_TYPE (old_variant_part);
8403 tree new_union_type, new_variant_part;
8404 tree union_field_list = NULL_TREE;
8405 variant_desc *v;
8406 unsigned int i;
8408 /* First create the type of the variant part from that of the old one. */
8409 new_union_type = make_node (QUAL_UNION_TYPE);
8410 TYPE_NAME (new_union_type)
8411 = concat_name (TYPE_NAME (record_type),
8412 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8414 /* If the position of the variant part is constant, subtract it from the
8415 size of the type of the parent to get the new size. This manual CSE
8416 reduces the code size when not optimizing. */
8417 if (TREE_CODE (offset) == INTEGER_CST)
8419 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8420 tree first_bit = bit_from_pos (offset, bitpos);
8421 TYPE_SIZE (new_union_type)
8422 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8423 TYPE_SIZE_UNIT (new_union_type)
8424 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8425 byte_from_pos (offset, bitpos));
8426 SET_TYPE_ADA_SIZE (new_union_type,
8427 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8428 first_bit));
8429 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8430 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8432 else
8433 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8435 /* Now finish up the new variants and populate the union type. */
8436 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8438 tree old_field = v->field, new_field;
8439 tree old_variant, old_variant_subpart, new_variant, field_list;
8441 /* Skip variants that don't belong to this nesting level. */
8442 if (DECL_CONTEXT (old_field) != old_union_type)
8443 continue;
8445 /* Retrieve the list of fields already added to the new variant. */
8446 new_variant = v->new_type;
8447 field_list = TYPE_FIELDS (new_variant);
8449 /* If the old variant had a variant subpart, we need to create a new
8450 variant subpart and add it to the field list. */
8451 old_variant = v->type;
8452 old_variant_subpart = get_variant_part (old_variant);
8453 if (old_variant_subpart)
8455 tree new_variant_subpart
8456 = create_variant_part_from (old_variant_subpart, variant_list,
8457 new_variant, pos_list, subst_list);
8458 DECL_CHAIN (new_variant_subpart) = field_list;
8459 field_list = new_variant_subpart;
8462 /* Finish up the new variant and create the field. No need for debug
8463 info thanks to the XVS type. */
8464 finish_record_type (new_variant, nreverse (field_list), 2, false);
8465 compute_record_mode (new_variant);
8466 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8467 Empty);
8469 new_field
8470 = create_field_decl_from (old_field, new_variant, new_union_type,
8471 TYPE_SIZE (new_variant),
8472 pos_list, subst_list);
8473 DECL_QUALIFIER (new_field) = v->qual;
8474 DECL_INTERNAL_P (new_field) = 1;
8475 DECL_CHAIN (new_field) = union_field_list;
8476 union_field_list = new_field;
8479 /* Finish up the union type and create the variant part. No need for debug
8480 info thanks to the XVS type. Note that we don't reverse the field list
8481 because VARIANT_LIST has been traversed in reverse order. */
8482 finish_record_type (new_union_type, union_field_list, 2, false);
8483 compute_record_mode (new_union_type);
8484 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8485 Empty);
8487 new_variant_part
8488 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8489 TYPE_SIZE (new_union_type),
8490 pos_list, subst_list);
8491 DECL_INTERNAL_P (new_variant_part) = 1;
8493 /* With multiple discriminants it is possible for an inner variant to be
8494 statically selected while outer ones are not; in this case, the list
8495 of fields of the inner variant is not flattened and we end up with a
8496 qualified union with a single member. Drop the useless container. */
8497 if (!DECL_CHAIN (union_field_list))
8499 DECL_CONTEXT (union_field_list) = record_type;
8500 DECL_FIELD_OFFSET (union_field_list)
8501 = DECL_FIELD_OFFSET (new_variant_part);
8502 DECL_FIELD_BIT_OFFSET (union_field_list)
8503 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8504 SET_DECL_OFFSET_ALIGN (union_field_list,
8505 DECL_OFFSET_ALIGN (new_variant_part));
8506 new_variant_part = union_field_list;
8509 return new_variant_part;
8512 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8513 which are both RECORD_TYPE, after applying the substitutions described
8514 in SUBST_LIST. */
8516 static void
8517 copy_and_substitute_in_size (tree new_type, tree old_type,
8518 vec<subst_pair> subst_list)
8520 unsigned int i;
8521 subst_pair *s;
8523 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8524 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8525 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8526 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8527 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8529 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8530 FOR_EACH_VEC_ELT (subst_list, i, s)
8531 TYPE_SIZE (new_type)
8532 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8533 s->discriminant, s->replacement);
8535 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8536 FOR_EACH_VEC_ELT (subst_list, i, s)
8537 TYPE_SIZE_UNIT (new_type)
8538 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8539 s->discriminant, s->replacement);
8541 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8542 FOR_EACH_VEC_ELT (subst_list, i, s)
8543 SET_TYPE_ADA_SIZE
8544 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8545 s->discriminant, s->replacement));
8547 /* Finalize the size. */
8548 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8549 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8552 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8553 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8554 The parallel type is the original array type if it has been translated. */
8556 static void
8557 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8559 Entity_Id gnat_original_array_type
8560 = Underlying_Type (Original_Array_Type (gnat_entity));
8561 tree gnu_original_array_type;
8563 if (!present_gnu_tree (gnat_original_array_type))
8564 return;
8566 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8568 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8569 return;
8571 add_parallel_type (gnu_type, gnu_original_array_type);
8574 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8575 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8576 updated by replacing F with R.
8578 The function doesn't update the layout of the type, i.e. it assumes
8579 that the substitution is purely formal. That's why the replacement
8580 value R must itself contain a PLACEHOLDER_EXPR. */
8582 tree
8583 substitute_in_type (tree t, tree f, tree r)
8585 tree nt;
8587 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8589 switch (TREE_CODE (t))
8591 case INTEGER_TYPE:
8592 case ENUMERAL_TYPE:
8593 case BOOLEAN_TYPE:
8594 case REAL_TYPE:
8596 /* First the domain types of arrays. */
8597 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8598 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8600 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8601 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8603 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8604 return t;
8606 nt = copy_type (t);
8607 TYPE_GCC_MIN_VALUE (nt) = low;
8608 TYPE_GCC_MAX_VALUE (nt) = high;
8610 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8611 SET_TYPE_INDEX_TYPE
8612 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8614 return nt;
8617 /* Then the subtypes. */
8618 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8619 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8621 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8622 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8624 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8625 return t;
8627 nt = copy_type (t);
8628 SET_TYPE_RM_MIN_VALUE (nt, low);
8629 SET_TYPE_RM_MAX_VALUE (nt, high);
8631 return nt;
8634 return t;
8636 case COMPLEX_TYPE:
8637 nt = substitute_in_type (TREE_TYPE (t), f, r);
8638 if (nt == TREE_TYPE (t))
8639 return t;
8641 return build_complex_type (nt);
8643 case FUNCTION_TYPE:
8644 /* These should never show up here. */
8645 gcc_unreachable ();
8647 case ARRAY_TYPE:
8649 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8650 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8652 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8653 return t;
8655 nt = build_nonshared_array_type (component, domain);
8656 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8657 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8658 SET_TYPE_MODE (nt, TYPE_MODE (t));
8659 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8660 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8661 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8662 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8663 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8664 return nt;
8667 case RECORD_TYPE:
8668 case UNION_TYPE:
8669 case QUAL_UNION_TYPE:
8671 bool changed_field = false;
8672 tree field;
8674 /* Start out with no fields, make new fields, and chain them
8675 in. If we haven't actually changed the type of any field,
8676 discard everything we've done and return the old type. */
8677 nt = copy_type (t);
8678 TYPE_FIELDS (nt) = NULL_TREE;
8680 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8682 tree new_field = copy_node (field), new_n;
8684 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8685 if (new_n != TREE_TYPE (field))
8687 TREE_TYPE (new_field) = new_n;
8688 changed_field = true;
8691 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8692 if (new_n != DECL_FIELD_OFFSET (field))
8694 DECL_FIELD_OFFSET (new_field) = new_n;
8695 changed_field = true;
8698 /* Do the substitution inside the qualifier, if any. */
8699 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8701 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8702 if (new_n != DECL_QUALIFIER (field))
8704 DECL_QUALIFIER (new_field) = new_n;
8705 changed_field = true;
8709 DECL_CONTEXT (new_field) = nt;
8710 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8712 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8713 TYPE_FIELDS (nt) = new_field;
8716 if (!changed_field)
8717 return t;
8719 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8720 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8721 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8722 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8723 return nt;
8726 default:
8727 return t;
8731 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8732 needed to represent the object. */
8734 tree
8735 rm_size (tree gnu_type)
8737 /* For integral types, we store the RM size explicitly. */
8738 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8739 return TYPE_RM_SIZE (gnu_type);
8741 /* Return the RM size of the actual data plus the size of the template. */
8742 if (TREE_CODE (gnu_type) == RECORD_TYPE
8743 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8744 return
8745 size_binop (PLUS_EXPR,
8746 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8747 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8749 /* For record or union types, we store the size explicitly. */
8750 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8751 && !TYPE_FAT_POINTER_P (gnu_type)
8752 && TYPE_ADA_SIZE (gnu_type))
8753 return TYPE_ADA_SIZE (gnu_type);
8755 /* For other types, this is just the size. */
8756 return TYPE_SIZE (gnu_type);
8759 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8760 fully-qualified name, possibly with type information encoding.
8761 Otherwise, return the name. */
8763 tree
8764 get_entity_name (Entity_Id gnat_entity)
8766 Get_Encoded_Name (gnat_entity);
8767 return get_identifier_with_length (Name_Buffer, Name_Len);
8770 /* Return an identifier representing the external name to be used for
8771 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8772 and the specified suffix. */
8774 tree
8775 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8777 const Entity_Kind kind = Ekind (gnat_entity);
8778 const bool has_suffix = (suffix != NULL);
8779 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8780 String_Pointer sp = {suffix, &temp};
8782 Get_External_Name (gnat_entity, has_suffix, sp);
8784 /* A variable using the Stdcall convention lives in a DLL. We adjust
8785 its name to use the jump table, the _imp__NAME contains the address
8786 for the NAME variable. */
8787 if ((kind == E_Variable || kind == E_Constant)
8788 && Has_Stdcall_Convention (gnat_entity))
8790 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8791 char *new_name = (char *) alloca (len + 1);
8792 strcpy (new_name, STDCALL_PREFIX);
8793 strcat (new_name, Name_Buffer);
8794 return get_identifier_with_length (new_name, len);
8797 return get_identifier_with_length (Name_Buffer, Name_Len);
8800 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8801 string, return a new IDENTIFIER_NODE that is the concatenation of
8802 the name followed by "___" and the specified suffix. */
8804 tree
8805 concat_name (tree gnu_name, const char *suffix)
8807 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8808 char *new_name = (char *) alloca (len + 1);
8809 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8810 strcat (new_name, "___");
8811 strcat (new_name, suffix);
8812 return get_identifier_with_length (new_name, len);
8815 /* Initialize data structures of the decl.c module. */
8817 void
8818 init_gnat_decl (void)
8820 /* Initialize the cache of annotated values. */
8821 annotate_value_cache
8822 = htab_create_ggc (512, tree_int_map_hash, tree_int_map_eq, 0);
8825 /* Destroy data structures of the decl.c module. */
8827 void
8828 destroy_gnat_decl (void)
8830 /* Destroy the cache of annotated values. */
8831 htab_delete (annotate_value_cache);
8832 annotate_value_cache = NULL;
8835 #include "gt-ada-decl.h"