gcc/ada/
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobed50294f2bae2ae1353c00f06f19ddaa986cc9d2
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, are biased or are wider than sizetype. */
2490 if (!integer_onep (gnu_orig_min)
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 || compare_tree_int (rm_size (gnu_index_type),
2498 TYPE_PRECISION (sizetype)) > 0)
2499 need_index_type_struct = true;
2502 /* Then flatten: create the array of arrays. For an array type
2503 used to implement a packed array, get the component type from
2504 the original array type since the representation clauses that
2505 can affect it are on the latter. */
2506 if (Is_Packed_Array_Impl_Type (gnat_entity)
2507 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2509 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2510 for (index = ndim - 1; index >= 0; index--)
2511 gnu_type = TREE_TYPE (gnu_type);
2513 /* One of the above calls might have caused us to be elaborated,
2514 so don't blow up if so. */
2515 if (present_gnu_tree (gnat_entity))
2517 maybe_present = true;
2518 break;
2521 else
2523 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2524 debug_info_p);
2526 /* One of the above calls might have caused us to be elaborated,
2527 so don't blow up if so. */
2528 if (present_gnu_tree (gnat_entity))
2530 maybe_present = true;
2531 break;
2535 /* Compute the maximum size of the array in units and bits. */
2536 if (gnu_max_size)
2538 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2539 TYPE_SIZE_UNIT (gnu_type));
2540 gnu_max_size = size_binop (MULT_EXPR,
2541 convert (bitsizetype, gnu_max_size),
2542 TYPE_SIZE (gnu_type));
2544 else
2545 gnu_max_size_unit = NULL_TREE;
2547 /* Now build the array type. */
2548 for (index = ndim - 1; index >= 0; index --)
2550 gnu_type = build_nonshared_array_type (gnu_type,
2551 gnu_index_types[index]);
2552 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2553 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2554 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2556 /* See the E_Array_Type case for the rationale. */
2557 if (TYPE_MODE (gnu_type) != BLKmode
2558 && Is_By_Reference_Type (gnat_entity))
2559 SET_TYPE_MODE (gnu_type, BLKmode);
2562 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2564 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2565 TYPE_STUB_DECL (gnu_type)
2566 = create_type_stub_decl (gnu_entity_name, gnu_type);
2568 /* If we are at file level and this is a multi-dimensional array,
2569 we need to make a variable corresponding to the stride of the
2570 inner dimensions. */
2571 if (global_bindings_p () && ndim > 1)
2573 tree gnu_st_name = get_identifier ("ST");
2574 tree gnu_arr_type;
2576 for (gnu_arr_type = TREE_TYPE (gnu_type);
2577 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2578 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2579 gnu_st_name = concat_name (gnu_st_name, "ST"))
2581 tree eltype = TREE_TYPE (gnu_arr_type);
2583 TYPE_SIZE (gnu_arr_type)
2584 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2585 gnat_entity, gnu_st_name,
2586 definition, false);
2588 /* ??? For now, store the size as a multiple of the
2589 alignment of the element type in bytes so that we
2590 can see the alignment from the tree. */
2591 TYPE_SIZE_UNIT (gnu_arr_type)
2592 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2593 gnat_entity,
2594 concat_name (gnu_st_name, "A_U"),
2595 definition, false,
2596 TYPE_ALIGN (eltype));
2598 /* ??? create_type_decl is not invoked on the inner types so
2599 the MULT_EXPR node built above will never be marked. */
2600 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2604 /* If we need to write out a record type giving the names of the
2605 bounds for debugging purposes, do it now and make the record
2606 type a parallel type. This is not needed for a packed array
2607 since the bounds are conveyed by the original array type. */
2608 if (need_index_type_struct
2609 && debug_info_p
2610 && !Is_Packed_Array_Impl_Type (gnat_entity))
2612 tree gnu_bound_rec = make_node (RECORD_TYPE);
2613 tree gnu_field_list = NULL_TREE;
2614 tree gnu_field;
2616 TYPE_NAME (gnu_bound_rec)
2617 = create_concat_name (gnat_entity, "XA");
2619 for (index = ndim - 1; index >= 0; index--)
2621 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2622 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2624 /* Make sure to reference the types themselves, and not just
2625 their names, as the debugger may fall back on them. */
2626 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2627 gnu_bound_rec, NULL_TREE,
2628 NULL_TREE, 0, 0);
2629 DECL_CHAIN (gnu_field) = gnu_field_list;
2630 gnu_field_list = gnu_field;
2633 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2634 add_parallel_type (gnu_type, gnu_bound_rec);
2637 /* If this is a packed array type, make the original array type a
2638 parallel type. Otherwise, do it for the base array type if it
2639 isn't artificial to make sure it is kept in the debug info. */
2640 if (debug_info_p)
2642 if (Is_Packed_Array_Impl_Type (gnat_entity))
2643 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2644 else
2646 tree gnu_base_decl
2647 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2648 if (!DECL_ARTIFICIAL (gnu_base_decl))
2649 add_parallel_type (gnu_type,
2650 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2654 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2655 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2656 = (Is_Packed_Array_Impl_Type (gnat_entity)
2657 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2659 /* If the size is self-referential and the maximum size doesn't
2660 overflow, use it. */
2661 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2662 && gnu_max_size
2663 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2664 && TREE_OVERFLOW (gnu_max_size))
2665 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2666 && TREE_OVERFLOW (gnu_max_size_unit)))
2668 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2669 TYPE_SIZE (gnu_type));
2670 TYPE_SIZE_UNIT (gnu_type)
2671 = size_binop (MIN_EXPR, gnu_max_size_unit,
2672 TYPE_SIZE_UNIT (gnu_type));
2675 /* Set our alias set to that of our base type. This gives all
2676 array subtypes the same alias set. */
2677 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2679 /* If this is a packed type, make this type the same as the packed
2680 array type, but do some adjusting in the type first. */
2681 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2683 Entity_Id gnat_index;
2684 tree gnu_inner;
2686 /* First finish the type we had been making so that we output
2687 debugging information for it. */
2688 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2689 if (Treat_As_Volatile (gnat_entity))
2690 gnu_type
2691 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2692 /* Make it artificial only if the base type was artificial too.
2693 That's sort of "morally" true and will make it possible for
2694 the debugger to look it up by name in DWARF, which is needed
2695 in order to decode the packed array type. */
2696 gnu_decl
2697 = create_type_decl (gnu_entity_name, gnu_type,
2698 !Comes_From_Source (Etype (gnat_entity))
2699 && !Comes_From_Source (gnat_entity),
2700 debug_info_p, gnat_entity);
2702 /* Save it as our equivalent in case the call below elaborates
2703 this type again. */
2704 save_gnu_tree (gnat_entity, gnu_decl, false);
2706 gnu_decl
2707 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2708 NULL_TREE, 0);
2709 this_made_decl = true;
2710 gnu_type = TREE_TYPE (gnu_decl);
2711 save_gnu_tree (gnat_entity, NULL_TREE, false);
2713 gnu_inner = gnu_type;
2714 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2715 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2716 || TYPE_PADDING_P (gnu_inner)))
2717 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2719 /* We need to attach the index type to the type we just made so
2720 that the actual bounds can later be put into a template. */
2721 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2722 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2723 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2724 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2726 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2728 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2729 TYPE_MODULUS for modular types so we make an extra
2730 subtype if necessary. */
2731 if (TYPE_MODULAR_P (gnu_inner))
2733 tree gnu_subtype
2734 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2735 TREE_TYPE (gnu_subtype) = gnu_inner;
2736 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2737 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2738 TYPE_MIN_VALUE (gnu_inner));
2739 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2740 TYPE_MAX_VALUE (gnu_inner));
2741 gnu_inner = gnu_subtype;
2744 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2746 #ifdef ENABLE_CHECKING
2747 /* Check for other cases of overloading. */
2748 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2749 #endif
2752 for (gnat_index = First_Index (gnat_entity);
2753 Present (gnat_index);
2754 gnat_index = Next_Index (gnat_index))
2755 SET_TYPE_ACTUAL_BOUNDS
2756 (gnu_inner,
2757 tree_cons (NULL_TREE,
2758 get_unpadded_type (Etype (gnat_index)),
2759 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2761 if (Convention (gnat_entity) != Convention_Fortran)
2762 SET_TYPE_ACTUAL_BOUNDS
2763 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2765 if (TREE_CODE (gnu_type) == RECORD_TYPE
2766 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2767 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2771 else
2772 /* Abort if packed array with no Packed_Array_Impl_Type. */
2773 gcc_assert (!Is_Packed (gnat_entity));
2775 break;
2777 case E_String_Literal_Subtype:
2778 /* Create the type for a string literal. */
2780 Entity_Id gnat_full_type
2781 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2782 && Present (Full_View (Etype (gnat_entity)))
2783 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2784 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2785 tree gnu_string_array_type
2786 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2787 tree gnu_string_index_type
2788 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2789 (TYPE_DOMAIN (gnu_string_array_type))));
2790 tree gnu_lower_bound
2791 = convert (gnu_string_index_type,
2792 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2793 tree gnu_length
2794 = UI_To_gnu (String_Literal_Length (gnat_entity),
2795 gnu_string_index_type);
2796 tree gnu_upper_bound
2797 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2798 gnu_lower_bound,
2799 int_const_binop (MINUS_EXPR, gnu_length,
2800 convert (gnu_string_index_type,
2801 integer_one_node)));
2802 tree gnu_index_type
2803 = create_index_type (convert (sizetype, gnu_lower_bound),
2804 convert (sizetype, gnu_upper_bound),
2805 create_range_type (gnu_string_index_type,
2806 gnu_lower_bound,
2807 gnu_upper_bound),
2808 gnat_entity);
2810 gnu_type
2811 = build_nonshared_array_type (gnat_to_gnu_type
2812 (Component_Type (gnat_entity)),
2813 gnu_index_type);
2814 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2815 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2816 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2818 break;
2820 /* Record Types and Subtypes
2822 The following fields are defined on record types:
2824 Has_Discriminants True if the record has discriminants
2825 First_Discriminant Points to head of list of discriminants
2826 First_Entity Points to head of list of fields
2827 Is_Tagged_Type True if the record is tagged
2829 Implementation of Ada records and discriminated records:
2831 A record type definition is transformed into the equivalent of a C
2832 struct definition. The fields that are the discriminants which are
2833 found in the Full_Type_Declaration node and the elements of the
2834 Component_List found in the Record_Type_Definition node. The
2835 Component_List can be a recursive structure since each Variant of
2836 the Variant_Part of the Component_List has a Component_List.
2838 Processing of a record type definition comprises starting the list of
2839 field declarations here from the discriminants and the calling the
2840 function components_to_record to add the rest of the fields from the
2841 component list and return the gnu type node. The function
2842 components_to_record will call itself recursively as it traverses
2843 the tree. */
2845 case E_Record_Type:
2846 if (Has_Complex_Representation (gnat_entity))
2848 gnu_type
2849 = build_complex_type
2850 (get_unpadded_type
2851 (Etype (Defining_Entity
2852 (First (Component_Items
2853 (Component_List
2854 (Type_Definition
2855 (Declaration_Node (gnat_entity)))))))));
2857 break;
2861 Node_Id full_definition = Declaration_Node (gnat_entity);
2862 Node_Id record_definition = Type_Definition (full_definition);
2863 Node_Id gnat_constr;
2864 Entity_Id gnat_field;
2865 tree gnu_field, gnu_field_list = NULL_TREE;
2866 tree gnu_get_parent;
2867 /* Set PACKED in keeping with gnat_to_gnu_field. */
2868 const int packed
2869 = Is_Packed (gnat_entity)
2871 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2872 ? -1
2873 : (Known_Alignment (gnat_entity)
2874 || (Strict_Alignment (gnat_entity)
2875 && Known_RM_Size (gnat_entity)))
2876 ? -2
2877 : 0;
2878 const bool has_discr = Has_Discriminants (gnat_entity);
2879 const bool has_rep = Has_Specified_Layout (gnat_entity);
2880 const bool is_extension
2881 = (Is_Tagged_Type (gnat_entity)
2882 && Nkind (record_definition) == N_Derived_Type_Definition);
2883 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2884 bool all_rep = has_rep;
2886 /* See if all fields have a rep clause. Stop when we find one
2887 that doesn't. */
2888 if (all_rep)
2889 for (gnat_field = First_Entity (gnat_entity);
2890 Present (gnat_field);
2891 gnat_field = Next_Entity (gnat_field))
2892 if ((Ekind (gnat_field) == E_Component
2893 || Ekind (gnat_field) == E_Discriminant)
2894 && No (Component_Clause (gnat_field)))
2896 all_rep = false;
2897 break;
2900 /* If this is a record extension, go a level further to find the
2901 record definition. Also, verify we have a Parent_Subtype. */
2902 if (is_extension)
2904 if (!type_annotate_only
2905 || Present (Record_Extension_Part (record_definition)))
2906 record_definition = Record_Extension_Part (record_definition);
2908 gcc_assert (type_annotate_only
2909 || Present (Parent_Subtype (gnat_entity)));
2912 /* Make a node for the record. If we are not defining the record,
2913 suppress expanding incomplete types. */
2914 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2915 TYPE_NAME (gnu_type) = gnu_entity_name;
2916 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2917 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2918 sorry ("non-default Scalar_Storage_Order");
2919 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2921 if (!definition)
2923 defer_incomplete_level++;
2924 this_deferred = true;
2927 /* If both a size and rep clause was specified, put the size in
2928 the record type now so that it can get the proper mode. */
2929 if (has_rep && Known_RM_Size (gnat_entity))
2930 TYPE_SIZE (gnu_type)
2931 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2933 /* Always set the alignment here so that it can be used to
2934 set the mode, if it is making the alignment stricter. If
2935 it is invalid, it will be checked again below. If this is to
2936 be Atomic, choose a default alignment of a word unless we know
2937 the size and it's smaller. */
2938 if (Known_Alignment (gnat_entity))
2939 TYPE_ALIGN (gnu_type)
2940 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2941 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2943 unsigned int size = UI_To_Int (Esize (gnat_entity));
2944 TYPE_ALIGN (gnu_type)
2945 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2947 /* If a type needs strict alignment, the minimum size will be the
2948 type size instead of the RM size (see validate_size). Cap the
2949 alignment, lest it causes this type size to become too large. */
2950 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2952 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2953 unsigned int raw_align = raw_size & -raw_size;
2954 if (raw_align < BIGGEST_ALIGNMENT)
2955 TYPE_ALIGN (gnu_type) = raw_align;
2957 else
2958 TYPE_ALIGN (gnu_type) = 0;
2960 /* If we have a Parent_Subtype, make a field for the parent. If
2961 this record has rep clauses, force the position to zero. */
2962 if (Present (Parent_Subtype (gnat_entity)))
2964 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2965 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2966 tree gnu_parent;
2968 /* A major complexity here is that the parent subtype will
2969 reference our discriminants in its Stored_Constraint list.
2970 But those must reference the parent component of this record
2971 which is precisely of the parent subtype we have not built yet!
2972 To break the circle we first build a dummy COMPONENT_REF which
2973 represents the "get to the parent" operation and initialize
2974 each of those discriminants to a COMPONENT_REF of the above
2975 dummy parent referencing the corresponding discriminant of the
2976 base type of the parent subtype. */
2977 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
2978 build0 (PLACEHOLDER_EXPR, gnu_type),
2979 build_decl (input_location,
2980 FIELD_DECL, NULL_TREE,
2981 gnu_dummy_parent_type),
2982 NULL_TREE);
2984 if (has_discr)
2985 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2986 Present (gnat_field);
2987 gnat_field = Next_Stored_Discriminant (gnat_field))
2988 if (Present (Corresponding_Discriminant (gnat_field)))
2990 tree gnu_field
2991 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2992 (gnat_field));
2993 save_gnu_tree
2994 (gnat_field,
2995 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2996 gnu_get_parent, gnu_field, NULL_TREE),
2997 true);
3000 /* Then we build the parent subtype. If it has discriminants but
3001 the type itself has unknown discriminants, this means that it
3002 doesn't contain information about how the discriminants are
3003 derived from those of the ancestor type, so it cannot be used
3004 directly. Instead it is built by cloning the parent subtype
3005 of the underlying record view of the type, for which the above
3006 derivation of discriminants has been made explicit. */
3007 if (Has_Discriminants (gnat_parent)
3008 && Has_Unknown_Discriminants (gnat_entity))
3010 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3012 /* If we are defining the type, the underlying record
3013 view must already have been elaborated at this point.
3014 Otherwise do it now as its parent subtype cannot be
3015 technically elaborated on its own. */
3016 if (definition)
3017 gcc_assert (present_gnu_tree (gnat_uview));
3018 else
3019 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3021 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3023 /* Substitute the "get to the parent" of the type for that
3024 of its underlying record view in the cloned type. */
3025 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3026 Present (gnat_field);
3027 gnat_field = Next_Stored_Discriminant (gnat_field))
3028 if (Present (Corresponding_Discriminant (gnat_field)))
3030 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3031 tree gnu_ref
3032 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3033 gnu_get_parent, gnu_field, NULL_TREE);
3034 gnu_parent
3035 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3038 else
3039 gnu_parent = gnat_to_gnu_type (gnat_parent);
3041 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3042 initially built. The discriminants must reference the fields
3043 of the parent subtype and not those of its base type for the
3044 placeholder machinery to properly work. */
3045 if (has_discr)
3047 /* The actual parent subtype is the full view. */
3048 if (IN (Ekind (gnat_parent), Private_Kind))
3050 if (Present (Full_View (gnat_parent)))
3051 gnat_parent = Full_View (gnat_parent);
3052 else
3053 gnat_parent = Underlying_Full_View (gnat_parent);
3056 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3057 Present (gnat_field);
3058 gnat_field = Next_Stored_Discriminant (gnat_field))
3059 if (Present (Corresponding_Discriminant (gnat_field)))
3061 Entity_Id field = Empty;
3062 for (field = First_Stored_Discriminant (gnat_parent);
3063 Present (field);
3064 field = Next_Stored_Discriminant (field))
3065 if (same_discriminant_p (gnat_field, field))
3066 break;
3067 gcc_assert (Present (field));
3068 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3069 = gnat_to_gnu_field_decl (field);
3073 /* The "get to the parent" COMPONENT_REF must be given its
3074 proper type... */
3075 TREE_TYPE (gnu_get_parent) = gnu_parent;
3077 /* ...and reference the _Parent field of this record. */
3078 gnu_field
3079 = create_field_decl (parent_name_id,
3080 gnu_parent, gnu_type,
3081 has_rep
3082 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3083 has_rep
3084 ? bitsize_zero_node : NULL_TREE,
3085 0, 1);
3086 DECL_INTERNAL_P (gnu_field) = 1;
3087 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3088 TYPE_FIELDS (gnu_type) = gnu_field;
3091 /* Make the fields for the discriminants and put them into the record
3092 unless it's an Unchecked_Union. */
3093 if (has_discr)
3094 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3095 Present (gnat_field);
3096 gnat_field = Next_Stored_Discriminant (gnat_field))
3098 /* If this is a record extension and this discriminant is the
3099 renaming of another discriminant, we've handled it above. */
3100 if (Present (Parent_Subtype (gnat_entity))
3101 && Present (Corresponding_Discriminant (gnat_field)))
3102 continue;
3104 gnu_field
3105 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3106 debug_info_p);
3108 /* Make an expression using a PLACEHOLDER_EXPR from the
3109 FIELD_DECL node just created and link that with the
3110 corresponding GNAT defining identifier. */
3111 save_gnu_tree (gnat_field,
3112 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3113 build0 (PLACEHOLDER_EXPR, gnu_type),
3114 gnu_field, NULL_TREE),
3115 true);
3117 if (!is_unchecked_union)
3119 DECL_CHAIN (gnu_field) = gnu_field_list;
3120 gnu_field_list = gnu_field;
3124 /* If we have a derived untagged type that renames discriminants in
3125 the root type, the (stored) discriminants are a just copy of the
3126 discriminants of the root type. This means that any constraints
3127 added by the renaming in the derivation are disregarded as far
3128 as the layout of the derived type is concerned. To rescue them,
3129 we change the type of the (stored) discriminants to a subtype
3130 with the bounds of the type of the visible discriminants. */
3131 if (has_discr
3132 && !is_extension
3133 && Stored_Constraint (gnat_entity) != No_Elist)
3134 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3135 gnat_constr != No_Elmt;
3136 gnat_constr = Next_Elmt (gnat_constr))
3137 if (Nkind (Node (gnat_constr)) == N_Identifier
3138 /* Ignore access discriminants. */
3139 && !Is_Access_Type (Etype (Node (gnat_constr)))
3140 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3142 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3143 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3144 tree gnu_ref
3145 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3146 NULL_TREE, 0);
3148 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3149 just above for one of the stored discriminants. */
3150 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3152 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3154 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3155 tree gnu_subtype
3156 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3157 ? make_unsigned_type (prec) : make_signed_type (prec);
3158 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3159 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3160 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3161 TYPE_MIN_VALUE (gnu_discr_type));
3162 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3163 TYPE_MAX_VALUE (gnu_discr_type));
3164 TREE_TYPE (gnu_ref)
3165 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3169 /* Add the fields into the record type and finish it up. */
3170 components_to_record (gnu_type, Component_List (record_definition),
3171 gnu_field_list, packed, definition, false,
3172 all_rep, is_unchecked_union,
3173 !Comes_From_Source (gnat_entity), debug_info_p,
3174 false, OK_To_Reorder_Components (gnat_entity),
3175 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3177 /* If it is passed by reference, force BLKmode to ensure that objects
3178 of this type will always be put in memory. */
3179 if (TYPE_MODE (gnu_type) != BLKmode
3180 && Is_By_Reference_Type (gnat_entity))
3181 SET_TYPE_MODE (gnu_type, BLKmode);
3183 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3185 /* Fill in locations of fields. */
3186 annotate_rep (gnat_entity, gnu_type);
3188 /* If there are any entities in the chain corresponding to components
3189 that we did not elaborate, ensure we elaborate their types if they
3190 are Itypes. */
3191 for (gnat_temp = First_Entity (gnat_entity);
3192 Present (gnat_temp);
3193 gnat_temp = Next_Entity (gnat_temp))
3194 if ((Ekind (gnat_temp) == E_Component
3195 || Ekind (gnat_temp) == E_Discriminant)
3196 && Is_Itype (Etype (gnat_temp))
3197 && !present_gnu_tree (gnat_temp))
3198 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3200 /* If this is a record type associated with an exception definition,
3201 equate its fields to those of the standard exception type. This
3202 will make it possible to convert between them. */
3203 if (gnu_entity_name == exception_data_name_id)
3205 tree gnu_std_field;
3206 for (gnu_field = TYPE_FIELDS (gnu_type),
3207 gnu_std_field = TYPE_FIELDS (except_type_node);
3208 gnu_field;
3209 gnu_field = DECL_CHAIN (gnu_field),
3210 gnu_std_field = DECL_CHAIN (gnu_std_field))
3211 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3212 gcc_assert (!gnu_std_field);
3215 break;
3217 case E_Class_Wide_Subtype:
3218 /* If an equivalent type is present, that is what we should use.
3219 Otherwise, fall through to handle this like a record subtype
3220 since it may have constraints. */
3221 if (gnat_equiv_type != gnat_entity)
3223 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3224 maybe_present = true;
3225 break;
3228 /* ... fall through ... */
3230 case E_Record_Subtype:
3231 /* If Cloned_Subtype is Present it means this record subtype has
3232 identical layout to that type or subtype and we should use
3233 that GCC type for this one. The front end guarantees that
3234 the component list is shared. */
3235 if (Present (Cloned_Subtype (gnat_entity)))
3237 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3238 NULL_TREE, 0);
3239 maybe_present = true;
3240 break;
3243 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3244 changing the type, make a new type with each field having the type of
3245 the field in the new subtype but the position computed by transforming
3246 every discriminant reference according to the constraints. We don't
3247 see any difference between private and non-private type here since
3248 derivations from types should have been deferred until the completion
3249 of the private type. */
3250 else
3252 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3253 tree gnu_base_type;
3255 if (!definition)
3257 defer_incomplete_level++;
3258 this_deferred = true;
3261 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3263 if (present_gnu_tree (gnat_entity))
3265 maybe_present = true;
3266 break;
3269 /* If this is a record subtype associated with a dispatch table,
3270 strip the suffix. This is necessary to make sure 2 different
3271 subtypes associated with the imported and exported views of a
3272 dispatch table are properly merged in LTO mode. */
3273 if (Is_Dispatch_Table_Entity (gnat_entity))
3275 char *p;
3276 Get_Encoded_Name (gnat_entity);
3277 p = strchr (Name_Buffer, '_');
3278 gcc_assert (p);
3279 strcpy (p+2, "dtS");
3280 gnu_entity_name = get_identifier (Name_Buffer);
3283 /* When the subtype has discriminants and these discriminants affect
3284 the initial shape it has inherited, factor them in. But for an
3285 Unchecked_Union (it must be an Itype), just return the type.
3286 We can't just test Is_Constrained because private subtypes without
3287 discriminants of types with discriminants with default expressions
3288 are Is_Constrained but aren't constrained! */
3289 if (IN (Ekind (gnat_base_type), Record_Kind)
3290 && !Is_Unchecked_Union (gnat_base_type)
3291 && !Is_For_Access_Subtype (gnat_entity)
3292 && Has_Discriminants (gnat_entity)
3293 && Is_Constrained (gnat_entity)
3294 && Stored_Constraint (gnat_entity) != No_Elist)
3296 vec<subst_pair> gnu_subst_list
3297 = build_subst_list (gnat_entity, gnat_base_type, definition);
3298 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3299 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3300 bool selected_variant = false, all_constant_pos = true;
3301 Entity_Id gnat_field;
3302 vec<variant_desc> gnu_variant_list;
3304 gnu_type = make_node (RECORD_TYPE);
3305 TYPE_NAME (gnu_type) = gnu_entity_name;
3306 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3307 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3309 /* Set the size, alignment and alias set of the new type to
3310 match that of the old one, doing required substitutions. */
3311 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3312 gnu_subst_list);
3314 if (TYPE_IS_PADDING_P (gnu_base_type))
3315 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3316 else
3317 gnu_unpad_base_type = gnu_base_type;
3319 /* Look for REP and variant parts in the base type. */
3320 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3321 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3323 /* If there is a variant part, we must compute whether the
3324 constraints statically select a particular variant. If
3325 so, we simply drop the qualified union and flatten the
3326 list of fields. Otherwise we'll build a new qualified
3327 union for the variants that are still relevant. */
3328 if (gnu_variant_part)
3330 variant_desc *v;
3331 unsigned int i;
3333 gnu_variant_list
3334 = build_variant_list (TREE_TYPE (gnu_variant_part),
3335 gnu_subst_list,
3336 vNULL);
3338 /* If all the qualifiers are unconditionally true, the
3339 innermost variant is statically selected. */
3340 selected_variant = true;
3341 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3342 if (!integer_onep (v->qual))
3344 selected_variant = false;
3345 break;
3348 /* Otherwise, create the new variants. */
3349 if (!selected_variant)
3350 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3352 tree old_variant = v->type;
3353 tree new_variant = make_node (RECORD_TYPE);
3354 tree suffix
3355 = concat_name (DECL_NAME (gnu_variant_part),
3356 IDENTIFIER_POINTER
3357 (DECL_NAME (v->field)));
3358 TYPE_NAME (new_variant)
3359 = concat_name (TYPE_NAME (gnu_type),
3360 IDENTIFIER_POINTER (suffix));
3361 copy_and_substitute_in_size (new_variant, old_variant,
3362 gnu_subst_list);
3363 v->new_type = new_variant;
3366 else
3368 gnu_variant_list.create (0);
3369 selected_variant = false;
3372 /* Make a list of fields and their position in the base type. */
3373 gnu_pos_list
3374 = build_position_list (gnu_unpad_base_type,
3375 gnu_variant_list.exists ()
3376 && !selected_variant,
3377 size_zero_node, bitsize_zero_node,
3378 BIGGEST_ALIGNMENT, NULL_TREE);
3380 /* Now go down every component in the subtype and compute its
3381 size and position from those of the component in the base
3382 type and from the constraints of the subtype. */
3383 for (gnat_field = First_Entity (gnat_entity);
3384 Present (gnat_field);
3385 gnat_field = Next_Entity (gnat_field))
3386 if ((Ekind (gnat_field) == E_Component
3387 || Ekind (gnat_field) == E_Discriminant)
3388 && !(Present (Corresponding_Discriminant (gnat_field))
3389 && Is_Tagged_Type (gnat_base_type))
3390 && Underlying_Type
3391 (Scope (Original_Record_Component (gnat_field)))
3392 == gnat_base_type)
3394 Name_Id gnat_name = Chars (gnat_field);
3395 Entity_Id gnat_old_field
3396 = Original_Record_Component (gnat_field);
3397 tree gnu_old_field
3398 = gnat_to_gnu_field_decl (gnat_old_field);
3399 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3400 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3401 tree gnu_cont_type, gnu_last = NULL_TREE;
3403 /* If the type is the same, retrieve the GCC type from the
3404 old field to take into account possible adjustments. */
3405 if (Etype (gnat_field) == Etype (gnat_old_field))
3406 gnu_field_type = TREE_TYPE (gnu_old_field);
3407 else
3408 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3410 /* If there was a component clause, the field types must be
3411 the same for the type and subtype, so copy the data from
3412 the old field to avoid recomputation here. Also if the
3413 field is justified modular and the optimization in
3414 gnat_to_gnu_field was applied. */
3415 if (Present (Component_Clause (gnat_old_field))
3416 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3417 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3418 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3419 == TREE_TYPE (gnu_old_field)))
3421 gnu_size = DECL_SIZE (gnu_old_field);
3422 gnu_field_type = TREE_TYPE (gnu_old_field);
3425 /* If the old field was packed and of constant size, we
3426 have to get the old size here, as it might differ from
3427 what the Etype conveys and the latter might overlap
3428 onto the following field. Try to arrange the type for
3429 possible better packing along the way. */
3430 else if (DECL_PACKED (gnu_old_field)
3431 && TREE_CODE (DECL_SIZE (gnu_old_field))
3432 == INTEGER_CST)
3434 gnu_size = DECL_SIZE (gnu_old_field);
3435 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3436 && !TYPE_FAT_POINTER_P (gnu_field_type)
3437 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3438 gnu_field_type
3439 = make_packable_type (gnu_field_type, true);
3442 else
3443 gnu_size = TYPE_SIZE (gnu_field_type);
3445 /* If the context of the old field is the base type or its
3446 REP part (if any), put the field directly in the new
3447 type; otherwise look up the context in the variant list
3448 and put the field either in the new type if there is a
3449 selected variant or in one of the new variants. */
3450 if (gnu_context == gnu_unpad_base_type
3451 || (gnu_rep_part
3452 && gnu_context == TREE_TYPE (gnu_rep_part)))
3453 gnu_cont_type = gnu_type;
3454 else
3456 variant_desc *v;
3457 unsigned int i;
3458 tree rep_part;
3460 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3461 if (gnu_context == v->type
3462 || ((rep_part = get_rep_part (v->type))
3463 && gnu_context == TREE_TYPE (rep_part)))
3464 break;
3465 if (v)
3467 if (selected_variant)
3468 gnu_cont_type = gnu_type;
3469 else
3470 gnu_cont_type = v->new_type;
3472 else
3473 /* The front-end may pass us "ghost" components if
3474 it fails to recognize that a constrained subtype
3475 is statically constrained. Discard them. */
3476 continue;
3479 /* Now create the new field modeled on the old one. */
3480 gnu_field
3481 = create_field_decl_from (gnu_old_field, gnu_field_type,
3482 gnu_cont_type, gnu_size,
3483 gnu_pos_list, gnu_subst_list);
3484 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3486 /* Put it in one of the new variants directly. */
3487 if (gnu_cont_type != gnu_type)
3489 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3490 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3493 /* To match the layout crafted in components_to_record,
3494 if this is the _Tag or _Parent field, put it before
3495 any other fields. */
3496 else if (gnat_name == Name_uTag
3497 || gnat_name == Name_uParent)
3498 gnu_field_list = chainon (gnu_field_list, gnu_field);
3500 /* Similarly, if this is the _Controller field, put
3501 it before the other fields except for the _Tag or
3502 _Parent field. */
3503 else if (gnat_name == Name_uController && gnu_last)
3505 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3506 DECL_CHAIN (gnu_last) = gnu_field;
3509 /* Otherwise, if this is a regular field, put it after
3510 the other fields. */
3511 else
3513 DECL_CHAIN (gnu_field) = gnu_field_list;
3514 gnu_field_list = gnu_field;
3515 if (!gnu_last)
3516 gnu_last = gnu_field;
3517 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3518 all_constant_pos = false;
3521 save_gnu_tree (gnat_field, gnu_field, false);
3524 /* If there is a variant list, a selected variant and the fields
3525 all have a constant position, put them in order of increasing
3526 position to match that of constant CONSTRUCTORs. Likewise if
3527 there is no variant list but a REP part, since the latter has
3528 been flattened in the process. */
3529 if (((gnu_variant_list.exists () && selected_variant)
3530 || (!gnu_variant_list.exists () && gnu_rep_part))
3531 && all_constant_pos)
3533 const int len = list_length (gnu_field_list);
3534 tree *field_arr = XALLOCAVEC (tree, len), t;
3535 int i;
3537 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3538 field_arr[i] = t;
3540 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3542 gnu_field_list = NULL_TREE;
3543 for (i = 0; i < len; i++)
3545 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3546 gnu_field_list = field_arr[i];
3550 /* If there is a variant list and no selected variant, we need
3551 to create the nest of variant parts from the old nest. */
3552 else if (gnu_variant_list.exists () && !selected_variant)
3554 tree new_variant_part
3555 = create_variant_part_from (gnu_variant_part,
3556 gnu_variant_list, gnu_type,
3557 gnu_pos_list, gnu_subst_list);
3558 DECL_CHAIN (new_variant_part) = gnu_field_list;
3559 gnu_field_list = new_variant_part;
3562 /* Now go through the entities again looking for Itypes that
3563 we have not elaborated but should (e.g., Etypes of fields
3564 that have Original_Components). */
3565 for (gnat_field = First_Entity (gnat_entity);
3566 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3567 if ((Ekind (gnat_field) == E_Discriminant
3568 || Ekind (gnat_field) == E_Component)
3569 && !present_gnu_tree (Etype (gnat_field)))
3570 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3572 /* Do not emit debug info for the type yet since we're going to
3573 modify it below. */
3574 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3575 false);
3576 compute_record_mode (gnu_type);
3578 /* See the E_Record_Type case for the rationale. */
3579 if (TYPE_MODE (gnu_type) != BLKmode
3580 && Is_By_Reference_Type (gnat_entity))
3581 SET_TYPE_MODE (gnu_type, BLKmode);
3583 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3585 /* Fill in locations of fields. */
3586 annotate_rep (gnat_entity, gnu_type);
3588 /* If debugging information is being written for the type, write
3589 a record that shows what we are a subtype of and also make a
3590 variable that indicates our size, if still variable. */
3591 if (debug_info_p)
3593 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3594 tree gnu_unpad_base_name
3595 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3596 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3598 TYPE_NAME (gnu_subtype_marker)
3599 = create_concat_name (gnat_entity, "XVS");
3600 finish_record_type (gnu_subtype_marker,
3601 create_field_decl (gnu_unpad_base_name,
3602 build_reference_type
3603 (gnu_unpad_base_type),
3604 gnu_subtype_marker,
3605 NULL_TREE, NULL_TREE,
3606 0, 0),
3607 0, true);
3609 add_parallel_type (gnu_type, gnu_subtype_marker);
3611 if (definition
3612 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3613 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3614 TYPE_SIZE_UNIT (gnu_subtype_marker)
3615 = create_var_decl (create_concat_name (gnat_entity,
3616 "XVZ"),
3617 NULL_TREE, sizetype, gnu_size_unit,
3618 false, false, false, false, NULL,
3619 gnat_entity);
3622 gnu_variant_list.release ();
3623 gnu_subst_list.release ();
3625 /* Now we can finalize it. */
3626 rest_of_record_type_compilation (gnu_type);
3629 /* Otherwise, go down all the components in the new type and make
3630 them equivalent to those in the base type. */
3631 else
3633 gnu_type = gnu_base_type;
3635 for (gnat_temp = First_Entity (gnat_entity);
3636 Present (gnat_temp);
3637 gnat_temp = Next_Entity (gnat_temp))
3638 if ((Ekind (gnat_temp) == E_Discriminant
3639 && !Is_Unchecked_Union (gnat_base_type))
3640 || Ekind (gnat_temp) == E_Component)
3641 save_gnu_tree (gnat_temp,
3642 gnat_to_gnu_field_decl
3643 (Original_Record_Component (gnat_temp)),
3644 false);
3647 break;
3649 case E_Access_Subprogram_Type:
3650 /* Use the special descriptor type for dispatch tables if needed,
3651 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3652 Note that we are only required to do so for static tables in
3653 order to be compatible with the C++ ABI, but Ada 2005 allows
3654 to extend library level tagged types at the local level so
3655 we do it in the non-static case as well. */
3656 if (TARGET_VTABLE_USES_DESCRIPTORS
3657 && Is_Dispatch_Table_Entity (gnat_entity))
3659 gnu_type = fdesc_type_node;
3660 gnu_size = TYPE_SIZE (gnu_type);
3661 break;
3664 /* ... fall through ... */
3666 case E_Anonymous_Access_Subprogram_Type:
3667 /* If we are not defining this entity, and we have incomplete
3668 entities being processed above us, make a dummy type and
3669 fill it in later. */
3670 if (!definition && defer_incomplete_level != 0)
3672 struct incomplete *p = XNEW (struct incomplete);
3674 gnu_type
3675 = build_pointer_type
3676 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3677 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3678 !Comes_From_Source (gnat_entity),
3679 debug_info_p, gnat_entity);
3680 this_made_decl = true;
3681 gnu_type = TREE_TYPE (gnu_decl);
3682 save_gnu_tree (gnat_entity, gnu_decl, false);
3683 saved = true;
3685 p->old_type = TREE_TYPE (gnu_type);
3686 p->full_type = Directly_Designated_Type (gnat_entity);
3687 p->next = defer_incomplete_list;
3688 defer_incomplete_list = p;
3689 break;
3692 /* ... fall through ... */
3694 case E_Allocator_Type:
3695 case E_Access_Type:
3696 case E_Access_Attribute_Type:
3697 case E_Anonymous_Access_Type:
3698 case E_General_Access_Type:
3700 /* The designated type and its equivalent type for gigi. */
3701 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3702 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3703 /* Whether it comes from a limited with. */
3704 bool is_from_limited_with
3705 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3706 && From_Limited_With (gnat_desig_equiv));
3707 /* The "full view" of the designated type. If this is an incomplete
3708 entity from a limited with, treat its non-limited view as the full
3709 view. Otherwise, if this is an incomplete or private type, use the
3710 full view. In the former case, we might point to a private type,
3711 in which case, we need its full view. Also, we want to look at the
3712 actual type used for the representation, so this takes a total of
3713 three steps. */
3714 Entity_Id gnat_desig_full_direct_first
3715 = (is_from_limited_with
3716 ? Non_Limited_View (gnat_desig_equiv)
3717 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3718 ? Full_View (gnat_desig_equiv) : Empty));
3719 Entity_Id gnat_desig_full_direct
3720 = ((is_from_limited_with
3721 && Present (gnat_desig_full_direct_first)
3722 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3723 ? Full_View (gnat_desig_full_direct_first)
3724 : gnat_desig_full_direct_first);
3725 Entity_Id gnat_desig_full
3726 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3727 /* The type actually used to represent the designated type, either
3728 gnat_desig_full or gnat_desig_equiv. */
3729 Entity_Id gnat_desig_rep;
3730 /* True if this is a pointer to an unconstrained array. */
3731 bool is_unconstrained_array;
3732 /* We want to know if we'll be seeing the freeze node for any
3733 incomplete type we may be pointing to. */
3734 bool in_main_unit
3735 = (Present (gnat_desig_full)
3736 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3737 : In_Extended_Main_Code_Unit (gnat_desig_type));
3738 /* True if we make a dummy type here. */
3739 bool made_dummy = false;
3740 /* The mode to be used for the pointer type. */
3741 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3742 /* The GCC type used for the designated type. */
3743 tree gnu_desig_type = NULL_TREE;
3745 if (!targetm.valid_pointer_mode (p_mode))
3746 p_mode = ptr_mode;
3748 /* If either the designated type or its full view is an unconstrained
3749 array subtype, replace it with the type it's a subtype of. This
3750 avoids problems with multiple copies of unconstrained array types.
3751 Likewise, if the designated type is a subtype of an incomplete
3752 record type, use the parent type to avoid order of elaboration
3753 issues. This can lose some code efficiency, but there is no
3754 alternative. */
3755 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3756 && !Is_Constrained (gnat_desig_equiv))
3757 gnat_desig_equiv = Etype (gnat_desig_equiv);
3758 if (Present (gnat_desig_full)
3759 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3760 && !Is_Constrained (gnat_desig_full))
3761 || (Ekind (gnat_desig_full) == E_Record_Subtype
3762 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3763 gnat_desig_full = Etype (gnat_desig_full);
3765 /* Set the type that's actually the representation of the designated
3766 type and also flag whether we have a unconstrained array. */
3767 gnat_desig_rep
3768 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3769 is_unconstrained_array
3770 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3772 /* If we are pointing to an incomplete type whose completion is an
3773 unconstrained array, make dummy fat and thin pointer types to it.
3774 Likewise if the type itself is dummy or an unconstrained array. */
3775 if (is_unconstrained_array
3776 && (Present (gnat_desig_full)
3777 || (present_gnu_tree (gnat_desig_equiv)
3778 && TYPE_IS_DUMMY_P
3779 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3780 || (!in_main_unit
3781 && defer_incomplete_level != 0
3782 && !present_gnu_tree (gnat_desig_equiv))
3783 || (in_main_unit
3784 && is_from_limited_with
3785 && Present (Freeze_Node (gnat_desig_equiv)))))
3787 if (present_gnu_tree (gnat_desig_rep))
3788 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3789 else
3791 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3792 made_dummy = true;
3795 /* If the call above got something that has a pointer, the pointer
3796 is our type. This could have happened either because the type
3797 was elaborated or because somebody else executed the code. */
3798 if (!TYPE_POINTER_TO (gnu_desig_type))
3799 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3800 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3803 /* If we already know what the full type is, use it. */
3804 else if (Present (gnat_desig_full)
3805 && present_gnu_tree (gnat_desig_full))
3806 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3808 /* Get the type of the thing we are to point to and build a pointer to
3809 it. If it is a reference to an incomplete or private type with a
3810 full view that is a record, make a dummy type node and get the
3811 actual type later when we have verified it is safe. */
3812 else if ((!in_main_unit
3813 && !present_gnu_tree (gnat_desig_equiv)
3814 && Present (gnat_desig_full)
3815 && !present_gnu_tree (gnat_desig_full)
3816 && Is_Record_Type (gnat_desig_full))
3817 /* Likewise if we are pointing to a record or array and we are
3818 to defer elaborating incomplete types. We do this as this
3819 access type may be the full view of a private type. Note
3820 that the unconstrained array case is handled above. */
3821 || ((!in_main_unit || imported_p)
3822 && defer_incomplete_level != 0
3823 && !present_gnu_tree (gnat_desig_equiv)
3824 && (Is_Record_Type (gnat_desig_rep)
3825 || Is_Array_Type (gnat_desig_rep)))
3826 /* If this is a reference from a limited_with type back to our
3827 main unit and there's a freeze node for it, either we have
3828 already processed the declaration and made the dummy type,
3829 in which case we just reuse the latter, or we have not yet,
3830 in which case we make the dummy type and it will be reused
3831 when the declaration is finally processed. In both cases,
3832 the pointer eventually created below will be automatically
3833 adjusted when the freeze node is processed. Note that the
3834 unconstrained array case is handled above. */
3835 || (in_main_unit
3836 && is_from_limited_with
3837 && Present (Freeze_Node (gnat_desig_rep))))
3839 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3840 made_dummy = true;
3843 /* Otherwise handle the case of a pointer to itself. */
3844 else if (gnat_desig_equiv == gnat_entity)
3846 gnu_type
3847 = build_pointer_type_for_mode (void_type_node, p_mode,
3848 No_Strict_Aliasing (gnat_entity));
3849 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3852 /* If expansion is disabled, the equivalent type of a concurrent type
3853 is absent, so build a dummy pointer type. */
3854 else if (type_annotate_only && No (gnat_desig_equiv))
3855 gnu_type = ptr_void_type_node;
3857 /* Finally, handle the default case where we can just elaborate our
3858 designated type. */
3859 else
3860 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3862 /* It is possible that a call to gnat_to_gnu_type above resolved our
3863 type. If so, just return it. */
3864 if (present_gnu_tree (gnat_entity))
3866 maybe_present = true;
3867 break;
3870 /* If we haven't done it yet, build the pointer type the usual way. */
3871 if (!gnu_type)
3873 /* Modify the designated type if we are pointing only to constant
3874 objects, but don't do it for unconstrained arrays. */
3875 if (Is_Access_Constant (gnat_entity)
3876 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3878 gnu_desig_type
3879 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3881 /* Some extra processing is required if we are building a
3882 pointer to an incomplete type (in the GCC sense). We might
3883 have such a type if we just made a dummy, or directly out
3884 of the call to gnat_to_gnu_type above if we are processing
3885 an access type for a record component designating the
3886 record type itself. */
3887 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3889 /* We must ensure that the pointer to variant we make will
3890 be processed by update_pointer_to when the initial type
3891 is completed. Pretend we made a dummy and let further
3892 processing act as usual. */
3893 made_dummy = true;
3895 /* We must ensure that update_pointer_to will not retrieve
3896 the dummy variant when building a properly qualified
3897 version of the complete type. We take advantage of the
3898 fact that get_qualified_type is requiring TYPE_NAMEs to
3899 match to influence build_qualified_type and then also
3900 update_pointer_to here. */
3901 TYPE_NAME (gnu_desig_type)
3902 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3906 gnu_type
3907 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3908 No_Strict_Aliasing (gnat_entity));
3911 /* If we are not defining this object and we have made a dummy pointer,
3912 save our current definition, evaluate the actual type, and replace
3913 the tentative type we made with the actual one. If we are to defer
3914 actually looking up the actual type, make an entry in the deferred
3915 list. If this is from a limited with, we may have to defer to the
3916 end of the current unit. */
3917 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3919 tree gnu_old_desig_type;
3921 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3923 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3924 if (esize == POINTER_SIZE)
3925 gnu_type = build_pointer_type
3926 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3928 else
3929 gnu_old_desig_type = TREE_TYPE (gnu_type);
3931 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3932 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3933 !Comes_From_Source (gnat_entity),
3934 debug_info_p, gnat_entity);
3935 this_made_decl = true;
3936 gnu_type = TREE_TYPE (gnu_decl);
3937 save_gnu_tree (gnat_entity, gnu_decl, false);
3938 saved = true;
3940 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3941 update gnu_old_desig_type directly, in which case it will not be
3942 a dummy type any more when we get into update_pointer_to.
3944 This can happen e.g. when the designated type is a record type,
3945 because their elaboration starts with an initial node from
3946 make_dummy_type, which may be the same node as the one we got.
3948 Besides, variants of this non-dummy type might have been created
3949 along the way. update_pointer_to is expected to properly take
3950 care of those situations. */
3951 if (defer_incomplete_level == 0 && !is_from_limited_with)
3953 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3954 gnat_to_gnu_type (gnat_desig_equiv));
3956 else
3958 struct incomplete *p = XNEW (struct incomplete);
3959 struct incomplete **head
3960 = (is_from_limited_with
3961 ? &defer_limited_with : &defer_incomplete_list);
3962 p->old_type = gnu_old_desig_type;
3963 p->full_type = gnat_desig_equiv;
3964 p->next = *head;
3965 *head = p;
3969 break;
3971 case E_Access_Protected_Subprogram_Type:
3972 case E_Anonymous_Access_Protected_Subprogram_Type:
3973 if (type_annotate_only && No (gnat_equiv_type))
3974 gnu_type = ptr_void_type_node;
3975 else
3977 /* The run-time representation is the equivalent type. */
3978 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3979 maybe_present = true;
3982 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3983 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3984 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3985 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3986 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3987 NULL_TREE, 0);
3989 break;
3991 case E_Access_Subtype:
3993 /* We treat this as identical to its base type; any constraint is
3994 meaningful only to the front-end.
3996 The designated type must be elaborated as well, if it does
3997 not have its own freeze node. Designated (sub)types created
3998 for constrained components of records with discriminants are
3999 not frozen by the front-end and thus not elaborated by gigi,
4000 because their use may appear before the base type is frozen,
4001 and because it is not clear that they are needed anywhere in
4002 gigi. With the current model, there is no correct place where
4003 they could be elaborated. */
4005 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4006 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4007 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4008 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4009 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4011 /* If we are not defining this entity, and we have incomplete
4012 entities being processed above us, make a dummy type and
4013 elaborate it later. */
4014 if (!definition && defer_incomplete_level != 0)
4016 struct incomplete *p = XNEW (struct incomplete);
4018 p->old_type
4019 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4020 p->full_type = Directly_Designated_Type (gnat_entity);
4021 p->next = defer_incomplete_list;
4022 defer_incomplete_list = p;
4024 else if (!IN (Ekind (Base_Type
4025 (Directly_Designated_Type (gnat_entity))),
4026 Incomplete_Or_Private_Kind))
4027 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4028 NULL_TREE, 0);
4031 maybe_present = true;
4032 break;
4034 /* Subprogram Entities
4036 The following access functions are defined for subprograms:
4038 Etype Return type or Standard_Void_Type.
4039 First_Formal The first formal parameter.
4040 Is_Imported Indicates that the subprogram has appeared in
4041 an INTERFACE or IMPORT pragma. For now we
4042 assume that the external language is C.
4043 Is_Exported Likewise but for an EXPORT pragma.
4044 Is_Inlined True if the subprogram is to be inlined.
4046 Each parameter is first checked by calling must_pass_by_ref on its
4047 type to determine if it is passed by reference. For parameters which
4048 are copied in, if they are Ada In Out or Out parameters, their return
4049 value becomes part of a record which becomes the return type of the
4050 function (C function - note that this applies only to Ada procedures
4051 so there is no Ada return type). Additional code to store back the
4052 parameters will be generated on the caller side. This transformation
4053 is done here, not in the front-end.
4055 The intended result of the transformation can be seen from the
4056 equivalent source rewritings that follow:
4058 struct temp {int a,b};
4059 procedure P (A,B: In Out ...) is temp P (int A,B)
4060 begin {
4061 .. ..
4062 end P; return {A,B};
4065 temp t;
4066 P(X,Y); t = P(X,Y);
4067 X = t.a , Y = t.b;
4069 For subprogram types we need to perform mainly the same conversions to
4070 GCC form that are needed for procedures and function declarations. The
4071 only difference is that at the end, we make a type declaration instead
4072 of a function declaration. */
4074 case E_Subprogram_Type:
4075 case E_Function:
4076 case E_Procedure:
4078 /* The type returned by a function or else Standard_Void_Type for a
4079 procedure. */
4080 Entity_Id gnat_return_type = Etype (gnat_entity);
4081 tree gnu_return_type;
4082 /* The first GCC parameter declaration (a PARM_DECL node). The
4083 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4084 actually is the head of this parameter list. */
4085 tree gnu_param_list = NULL_TREE;
4086 /* Non-null for subprograms containing parameters passed by copy-in
4087 copy-out (Ada In Out or Out parameters not passed by reference),
4088 in which case it is the list of nodes used to specify the values
4089 of the In Out/Out parameters that are returned as a record upon
4090 procedure return. The TREE_PURPOSE of an element of this list is
4091 a field of the record and the TREE_VALUE is the PARM_DECL
4092 corresponding to that field. This list will be saved in the
4093 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4094 tree gnu_cico_list = NULL_TREE;
4095 /* List of fields in return type of procedure with copy-in copy-out
4096 parameters. */
4097 tree gnu_field_list = NULL_TREE;
4098 /* If an import pragma asks to map this subprogram to a GCC builtin,
4099 this is the builtin DECL node. */
4100 tree gnu_builtin_decl = NULL_TREE;
4101 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4102 Entity_Id gnat_param;
4103 enum inline_status_t inline_status
4104 = Has_Pragma_No_Inline (gnat_entity)
4105 ? is_suppressed
4106 : Has_Pragma_Inline_Always (gnat_entity)
4107 ? is_required
4108 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4109 bool public_flag = Is_Public (gnat_entity) || imported_p;
4110 bool extern_flag
4111 = (Is_Public (gnat_entity) && !definition) || imported_p;
4112 bool artificial_flag = !Comes_From_Source (gnat_entity);
4113 /* The semantics of "pure" in Ada essentially matches that of "const"
4114 in the back-end. In particular, both properties are orthogonal to
4115 the "nothrow" property if the EH circuitry is explicit in the
4116 internal representation of the back-end. If we are to completely
4117 hide the EH circuitry from it, we need to declare that calls to pure
4118 Ada subprograms that can throw have side effects since they can
4119 trigger an "abnormal" transfer of control flow; thus they can be
4120 neither "const" nor "pure" in the back-end sense. */
4121 bool const_flag
4122 = (Exception_Mechanism == Back_End_Exceptions
4123 && Is_Pure (gnat_entity));
4124 bool volatile_flag = No_Return (gnat_entity);
4125 bool return_by_direct_ref_p = false;
4126 bool return_by_invisi_ref_p = false;
4127 bool return_unconstrained_p = false;
4128 int parmnum;
4130 /* A parameter may refer to this type, so defer completion of any
4131 incomplete types. */
4132 if (kind == E_Subprogram_Type && !definition)
4134 defer_incomplete_level++;
4135 this_deferred = true;
4138 /* If the subprogram has an alias, it is probably inherited, so
4139 we can use the original one. If the original "subprogram"
4140 is actually an enumeration literal, it may be the first use
4141 of its type, so we must elaborate that type now. */
4142 if (Present (Alias (gnat_entity)))
4144 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4145 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4147 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4149 /* Elaborate any Itypes in the parameters of this entity. */
4150 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4151 Present (gnat_temp);
4152 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4153 if (Is_Itype (Etype (gnat_temp)))
4154 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4156 break;
4159 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4160 corresponding DECL node. Proper generation of calls later on need
4161 proper parameter associations so we don't "break;" here. */
4162 if (Convention (gnat_entity) == Convention_Intrinsic
4163 && Present (Interface_Name (gnat_entity)))
4165 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4167 /* Inability to find the builtin decl most often indicates a
4168 genuine mistake, but imports of unregistered intrinsics are
4169 sometimes issued on purpose to allow hooking in alternate
4170 bodies. We post a warning conditioned on Wshadow in this case,
4171 to let developers be notified on demand without risking false
4172 positives with common default sets of options. */
4174 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4175 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4178 /* ??? What if we don't find the builtin node above ? warn ? err ?
4179 In the current state we neither warn nor err, and calls will just
4180 be handled as for regular subprograms. */
4182 /* Look into the return type and get its associated GCC tree. If it
4183 is not void, compute various flags for the subprogram type. */
4184 if (Ekind (gnat_return_type) == E_Void)
4185 gnu_return_type = void_type_node;
4186 else
4188 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4189 context may now appear in parameter and result profiles. If
4190 we are only annotating types, break circularities here. */
4191 if (type_annotate_only
4192 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4193 && From_Limited_With (gnat_return_type)
4194 && In_Extended_Main_Code_Unit
4195 (Non_Limited_View (gnat_return_type))
4196 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4197 gnu_return_type = ptr_void_type_node;
4198 else
4199 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4201 /* If this function returns by reference, make the actual return
4202 type the pointer type and make a note of that. */
4203 if (Returns_By_Ref (gnat_entity))
4205 gnu_return_type = build_pointer_type (gnu_return_type);
4206 return_by_direct_ref_p = true;
4209 /* If we are supposed to return an unconstrained array type, make
4210 the actual return type the fat pointer type. */
4211 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4213 gnu_return_type = TREE_TYPE (gnu_return_type);
4214 return_unconstrained_p = true;
4217 /* Likewise, if the return type requires a transient scope, the
4218 return value will be allocated on the secondary stack so the
4219 actual return type is the pointer type. */
4220 else if (Requires_Transient_Scope (gnat_return_type))
4222 gnu_return_type = build_pointer_type (gnu_return_type);
4223 return_unconstrained_p = true;
4226 /* If the Mechanism is By_Reference, ensure this function uses the
4227 target's by-invisible-reference mechanism, which may not be the
4228 same as above (e.g. it might be passing an extra parameter). */
4229 else if (kind == E_Function
4230 && Mechanism (gnat_entity) == By_Reference)
4231 return_by_invisi_ref_p = true;
4233 /* Likewise, if the return type is itself By_Reference. */
4234 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4235 return_by_invisi_ref_p = true;
4237 /* If the type is a padded type and the underlying type would not
4238 be passed by reference or the function has a foreign convention,
4239 return the underlying type. */
4240 else if (TYPE_IS_PADDING_P (gnu_return_type)
4241 && (!default_pass_by_ref
4242 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4243 || Has_Foreign_Convention (gnat_entity)))
4244 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4246 /* If the return type is unconstrained, that means it must have a
4247 maximum size. Use the padded type as the effective return type.
4248 And ensure the function uses the target's by-invisible-reference
4249 mechanism to avoid copying too much data when it returns. */
4250 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4252 tree orig_type = gnu_return_type;
4254 gnu_return_type
4255 = maybe_pad_type (gnu_return_type,
4256 max_size (TYPE_SIZE (gnu_return_type),
4257 true),
4258 0, gnat_entity, false, false, false, true);
4260 /* Declare it now since it will never be declared otherwise.
4261 This is necessary to ensure that its subtrees are properly
4262 marked. */
4263 if (gnu_return_type != orig_type
4264 && !DECL_P (TYPE_NAME (gnu_return_type)))
4265 create_type_decl (TYPE_NAME (gnu_return_type),
4266 gnu_return_type, true, debug_info_p,
4267 gnat_entity);
4269 return_by_invisi_ref_p = true;
4272 /* If the return type has a size that overflows, we cannot have
4273 a function that returns that type. This usage doesn't make
4274 sense anyway, so give an error here. */
4275 if (TYPE_SIZE_UNIT (gnu_return_type)
4276 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4277 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4279 post_error ("cannot return type whose size overflows",
4280 gnat_entity);
4281 gnu_return_type = copy_node (gnu_return_type);
4282 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4283 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4284 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4285 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4289 /* Loop over the parameters and get their associated GCC tree. While
4290 doing this, build a copy-in copy-out structure if we need one. */
4291 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4292 Present (gnat_param);
4293 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4295 Entity_Id gnat_param_type = Etype (gnat_param);
4296 tree gnu_param_name = get_entity_name (gnat_param);
4297 tree gnu_param_type, gnu_param, gnu_field;
4298 Mechanism_Type mech = Mechanism (gnat_param);
4299 bool copy_in_copy_out = false, fake_param_type;
4301 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4302 context may now appear in parameter and result profiles. If
4303 we are only annotating types, break circularities here. */
4304 if (type_annotate_only
4305 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4306 && From_Limited_With (Etype (gnat_param_type))
4307 && In_Extended_Main_Code_Unit
4308 (Non_Limited_View (gnat_param_type))
4309 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4311 gnu_param_type = ptr_void_type_node;
4312 fake_param_type = true;
4314 else
4316 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4317 fake_param_type = false;
4320 /* Builtins are expanded inline and there is no real call sequence
4321 involved. So the type expected by the underlying expander is
4322 always the type of each argument "as is". */
4323 if (gnu_builtin_decl)
4324 mech = By_Copy;
4325 /* Handle the first parameter of a valued procedure specially. */
4326 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4327 mech = By_Copy_Return;
4328 /* Otherwise, see if a Mechanism was supplied that forced this
4329 parameter to be passed one way or another. */
4330 else if (mech == Default
4331 || mech == By_Copy
4332 || mech == By_Reference)
4334 else if (mech > 0)
4336 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4337 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4338 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4339 mech))
4340 mech = By_Reference;
4341 else
4342 mech = By_Copy;
4344 else
4346 post_error ("unsupported mechanism for&", gnat_param);
4347 mech = Default;
4350 /* Do not call gnat_to_gnu_param for a fake parameter type since
4351 it will try to use the real type again. */
4352 if (fake_param_type)
4354 if (Ekind (gnat_param) == E_Out_Parameter)
4355 gnu_param = NULL_TREE;
4356 else
4358 gnu_param
4359 = create_param_decl (gnu_param_name, gnu_param_type,
4360 false);
4361 Set_Mechanism (gnat_param,
4362 mech == Default ? By_Copy : mech);
4363 if (Ekind (gnat_param) == E_In_Out_Parameter)
4364 copy_in_copy_out = true;
4367 else
4368 gnu_param
4369 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4370 Has_Foreign_Convention (gnat_entity),
4371 &copy_in_copy_out);
4373 /* We are returned either a PARM_DECL or a type if no parameter
4374 needs to be passed; in either case, adjust the type. */
4375 if (DECL_P (gnu_param))
4376 gnu_param_type = TREE_TYPE (gnu_param);
4377 else
4379 gnu_param_type = gnu_param;
4380 gnu_param = NULL_TREE;
4383 /* The failure of this assertion will very likely come from an
4384 order of elaboration issue for the type of the parameter. */
4385 gcc_assert (kind == E_Subprogram_Type
4386 || !TYPE_IS_DUMMY_P (gnu_param_type)
4387 || type_annotate_only);
4389 if (gnu_param)
4391 gnu_param_list = chainon (gnu_param, gnu_param_list);
4392 Sloc_to_locus (Sloc (gnat_param),
4393 &DECL_SOURCE_LOCATION (gnu_param));
4394 save_gnu_tree (gnat_param, gnu_param, false);
4396 /* If a parameter is a pointer, this function may modify
4397 memory through it and thus shouldn't be considered
4398 a const function. Also, the memory may be modified
4399 between two calls, so they can't be CSE'ed. The latter
4400 case also handles by-ref parameters. */
4401 if (POINTER_TYPE_P (gnu_param_type)
4402 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4403 const_flag = false;
4406 if (copy_in_copy_out)
4408 if (!gnu_cico_list)
4410 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4412 /* If this is a function, we also need a field for the
4413 return value to be placed. */
4414 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4416 gnu_field
4417 = create_field_decl (get_identifier ("RETVAL"),
4418 gnu_return_type,
4419 gnu_new_ret_type, NULL_TREE,
4420 NULL_TREE, 0, 0);
4421 Sloc_to_locus (Sloc (gnat_entity),
4422 &DECL_SOURCE_LOCATION (gnu_field));
4423 gnu_field_list = gnu_field;
4424 gnu_cico_list
4425 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4428 gnu_return_type = gnu_new_ret_type;
4429 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4430 /* Set a default alignment to speed up accesses. But we
4431 shouldn't increase the size of the structure too much,
4432 lest it doesn't fit in return registers anymore. */
4433 TYPE_ALIGN (gnu_return_type)
4434 = get_mode_alignment (ptr_mode);
4437 gnu_field
4438 = create_field_decl (gnu_param_name, gnu_param_type,
4439 gnu_return_type, NULL_TREE, NULL_TREE,
4440 0, 0);
4441 Sloc_to_locus (Sloc (gnat_param),
4442 &DECL_SOURCE_LOCATION (gnu_field));
4443 DECL_CHAIN (gnu_field) = gnu_field_list;
4444 gnu_field_list = gnu_field;
4445 gnu_cico_list
4446 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4450 if (gnu_cico_list)
4452 /* If we have a CICO list but it has only one entry, we convert
4453 this function into a function that returns this object. */
4454 if (list_length (gnu_cico_list) == 1)
4455 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4457 /* Do not finalize the return type if the subprogram is stubbed
4458 since structures are incomplete for the back-end. */
4459 else if (Convention (gnat_entity) != Convention_Stubbed)
4461 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4462 0, false);
4464 /* Try to promote the mode of the return type if it is passed
4465 in registers, again to speed up accesses. */
4466 if (TYPE_MODE (gnu_return_type) == BLKmode
4467 && !targetm.calls.return_in_memory (gnu_return_type,
4468 NULL_TREE))
4470 unsigned int size
4471 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4472 unsigned int i = BITS_PER_UNIT;
4473 machine_mode mode;
4475 while (i < size)
4476 i <<= 1;
4477 mode = mode_for_size (i, MODE_INT, 0);
4478 if (mode != BLKmode)
4480 SET_TYPE_MODE (gnu_return_type, mode);
4481 TYPE_ALIGN (gnu_return_type)
4482 = GET_MODE_ALIGNMENT (mode);
4483 TYPE_SIZE (gnu_return_type)
4484 = bitsize_int (GET_MODE_BITSIZE (mode));
4485 TYPE_SIZE_UNIT (gnu_return_type)
4486 = size_int (GET_MODE_SIZE (mode));
4490 if (debug_info_p)
4491 rest_of_record_type_compilation (gnu_return_type);
4495 /* Deal with platform-specific calling conventions. */
4496 if (Has_Stdcall_Convention (gnat_entity))
4497 prepend_one_attribute
4498 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4499 get_identifier ("stdcall"), NULL_TREE,
4500 gnat_entity);
4501 else if (Has_Thiscall_Convention (gnat_entity))
4502 prepend_one_attribute
4503 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4504 get_identifier ("thiscall"), NULL_TREE,
4505 gnat_entity);
4507 /* If we should request stack realignment for a foreign convention
4508 subprogram, do so. Note that this applies to task entry points
4509 in particular. */
4510 if (FOREIGN_FORCE_REALIGN_STACK
4511 && Has_Foreign_Convention (gnat_entity))
4512 prepend_one_attribute
4513 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4514 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4515 gnat_entity);
4517 /* Deal with a pragma Linker_Section on a subprogram. */
4518 if ((kind == E_Function || kind == E_Procedure)
4519 && Present (Linker_Section_Pragma (gnat_entity)))
4520 prepend_one_attribute_pragma (&attr_list,
4521 Linker_Section_Pragma (gnat_entity));
4523 /* The lists have been built in reverse. */
4524 gnu_param_list = nreverse (gnu_param_list);
4525 gnu_cico_list = nreverse (gnu_cico_list);
4527 if (kind == E_Function)
4528 Set_Mechanism (gnat_entity, return_unconstrained_p
4529 || return_by_direct_ref_p
4530 || return_by_invisi_ref_p
4531 ? By_Reference : By_Copy);
4532 gnu_type
4533 = create_subprog_type (gnu_return_type, gnu_param_list,
4534 gnu_cico_list, return_unconstrained_p,
4535 return_by_direct_ref_p,
4536 return_by_invisi_ref_p);
4538 /* A subprogram (something that doesn't return anything) shouldn't
4539 be considered const since there would be no reason for such a
4540 subprogram. Note that procedures with Out (or In Out) parameters
4541 have already been converted into a function with a return type. */
4542 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4543 const_flag = false;
4545 if (const_flag || volatile_flag)
4547 const int quals
4548 = (const_flag ? TYPE_QUAL_CONST : 0)
4549 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4551 gnu_type = change_qualified_type (gnu_type, quals);
4554 /* If we have a builtin decl for that function, use it. Check if the
4555 profiles are compatible and warn if they are not. The checker is
4556 expected to post extra diagnostics in this case. */
4557 if (gnu_builtin_decl)
4559 intrin_binding_t inb;
4561 inb.gnat_entity = gnat_entity;
4562 inb.ada_fntype = gnu_type;
4563 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4565 if (!intrin_profiles_compatible_p (&inb))
4566 post_error
4567 ("?profile of& doesn''t match the builtin it binds!",
4568 gnat_entity);
4570 gnu_decl = gnu_builtin_decl;
4571 gnu_type = TREE_TYPE (gnu_builtin_decl);
4572 break;
4575 /* If there was no specified Interface_Name and the external and
4576 internal names of the subprogram are the same, only use the
4577 internal name to allow disambiguation of nested subprograms. */
4578 if (No (Interface_Name (gnat_entity))
4579 && gnu_ext_name == gnu_entity_name)
4580 gnu_ext_name = NULL_TREE;
4582 /* If we are defining the subprogram and it has an Address clause
4583 we must get the address expression from the saved GCC tree for the
4584 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4585 the address expression here since the front-end has guaranteed
4586 in that case that the elaboration has no effects. If there is
4587 an Address clause and we are not defining the object, just
4588 make it a constant. */
4589 if (Present (Address_Clause (gnat_entity)))
4591 tree gnu_address = NULL_TREE;
4593 if (definition)
4594 gnu_address
4595 = (present_gnu_tree (gnat_entity)
4596 ? get_gnu_tree (gnat_entity)
4597 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4599 save_gnu_tree (gnat_entity, NULL_TREE, false);
4601 /* Convert the type of the object to a reference type that can
4602 alias everything as per 13.3(19). */
4603 gnu_type
4604 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4605 if (gnu_address)
4606 gnu_address = convert (gnu_type, gnu_address);
4608 gnu_decl
4609 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4610 gnu_address, false, Is_Public (gnat_entity),
4611 extern_flag, false, NULL, gnat_entity);
4612 DECL_BY_REF_P (gnu_decl) = 1;
4615 else if (kind == E_Subprogram_Type)
4617 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4618 gnu_decl
4619 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4620 debug_info_p, gnat_entity);
4622 else
4624 gnu_decl
4625 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4626 gnu_param_list, inline_status,
4627 public_flag, extern_flag, artificial_flag,
4628 attr_list, gnat_entity);
4629 /* This is unrelated to the stub built right above. */
4630 DECL_STUBBED_P (gnu_decl)
4631 = Convention (gnat_entity) == Convention_Stubbed;
4634 break;
4636 case E_Incomplete_Type:
4637 case E_Incomplete_Subtype:
4638 case E_Private_Type:
4639 case E_Private_Subtype:
4640 case E_Limited_Private_Type:
4641 case E_Limited_Private_Subtype:
4642 case E_Record_Type_With_Private:
4643 case E_Record_Subtype_With_Private:
4645 /* Get the "full view" of this entity. If this is an incomplete
4646 entity from a limited with, treat its non-limited view as the
4647 full view. Otherwise, use either the full view or the underlying
4648 full view, whichever is present. This is used in all the tests
4649 below. */
4650 Entity_Id full_view
4651 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4652 ? Non_Limited_View (gnat_entity)
4653 : Present (Full_View (gnat_entity))
4654 ? Full_View (gnat_entity)
4655 : IN (kind, Private_Kind)
4656 ? Underlying_Full_View (gnat_entity)
4657 : Empty;
4659 /* If this is an incomplete type with no full view, it must be a Taft
4660 Amendment type, in which case we return a dummy type. Otherwise,
4661 just get the type from its Etype. */
4662 if (No (full_view))
4664 if (kind == E_Incomplete_Type)
4666 gnu_type = make_dummy_type (gnat_entity);
4667 gnu_decl = TYPE_STUB_DECL (gnu_type);
4669 else
4671 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4672 NULL_TREE, 0);
4673 maybe_present = true;
4675 break;
4678 /* If we already made a type for the full view, reuse it. */
4679 else if (present_gnu_tree (full_view))
4681 gnu_decl = get_gnu_tree (full_view);
4682 break;
4685 /* Otherwise, if we are not defining the type now, get the type
4686 from the full view. But always get the type from the full view
4687 for define on use types, since otherwise we won't see them! */
4688 else if (!definition
4689 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4690 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4692 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4693 maybe_present = true;
4694 break;
4697 /* For incomplete types, make a dummy type entry which will be
4698 replaced later. Save it as the full declaration's type so
4699 we can do any needed updates when we see it. */
4700 gnu_type = make_dummy_type (gnat_entity);
4701 gnu_decl = TYPE_STUB_DECL (gnu_type);
4702 if (Has_Completion_In_Body (gnat_entity))
4703 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4704 save_gnu_tree (full_view, gnu_decl, 0);
4705 break;
4708 case E_Class_Wide_Type:
4709 /* Class-wide types are always transformed into their root type. */
4710 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4711 maybe_present = true;
4712 break;
4714 case E_Task_Type:
4715 case E_Task_Subtype:
4716 case E_Protected_Type:
4717 case E_Protected_Subtype:
4718 /* Concurrent types are always transformed into their record type. */
4719 if (type_annotate_only && No (gnat_equiv_type))
4720 gnu_type = void_type_node;
4721 else
4722 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4723 maybe_present = true;
4724 break;
4726 case E_Label:
4727 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4728 break;
4730 case E_Block:
4731 case E_Loop:
4732 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4733 we've already saved it, so we don't try to. */
4734 gnu_decl = error_mark_node;
4735 saved = true;
4736 break;
4738 case E_Abstract_State:
4739 /* This is a SPARK annotation that only reaches here when compiling in
4740 ASIS mode and has no characteristics to annotate. */
4741 gcc_assert (type_annotate_only);
4742 return error_mark_node;
4744 default:
4745 gcc_unreachable ();
4748 /* If we had a case where we evaluated another type and it might have
4749 defined this one, handle it here. */
4750 if (maybe_present && present_gnu_tree (gnat_entity))
4752 gnu_decl = get_gnu_tree (gnat_entity);
4753 saved = true;
4756 /* If we are processing a type and there is either no decl for it or
4757 we just made one, do some common processing for the type, such as
4758 handling alignment and possible padding. */
4759 if (is_type && (!gnu_decl || this_made_decl))
4761 /* Process the attributes, if not already done. Note that the type is
4762 already defined so we cannot pass true for IN_PLACE here. */
4763 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4765 /* Tell the middle-end that objects of tagged types are guaranteed to
4766 be properly aligned. This is necessary because conversions to the
4767 class-wide type are translated into conversions to the root type,
4768 which can be less aligned than some of its derived types. */
4769 if (Is_Tagged_Type (gnat_entity)
4770 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4771 TYPE_ALIGN_OK (gnu_type) = 1;
4773 /* Record whether the type is passed by reference. */
4774 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4775 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4777 /* ??? Don't set the size for a String_Literal since it is either
4778 confirming or we don't handle it properly (if the low bound is
4779 non-constant). */
4780 if (!gnu_size && kind != E_String_Literal_Subtype)
4782 Uint gnat_size = Known_Esize (gnat_entity)
4783 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4784 gnu_size
4785 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4786 false, Has_Size_Clause (gnat_entity));
4789 /* If a size was specified, see if we can make a new type of that size
4790 by rearranging the type, for example from a fat to a thin pointer. */
4791 if (gnu_size)
4793 gnu_type
4794 = make_type_from_size (gnu_type, gnu_size,
4795 Has_Biased_Representation (gnat_entity));
4797 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4798 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4799 gnu_size = NULL_TREE;
4802 /* If the alignment has not already been processed and this is not
4803 an unconstrained array type, see if an alignment is specified.
4804 If not, we pick a default alignment for atomic objects. */
4805 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4807 else if (Known_Alignment (gnat_entity))
4809 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4810 TYPE_ALIGN (gnu_type));
4812 /* Warn on suspiciously large alignments. This should catch
4813 errors about the (alignment,byte)/(size,bit) discrepancy. */
4814 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4816 tree size;
4818 /* If a size was specified, take it into account. Otherwise
4819 use the RM size for records or unions as the type size has
4820 already been adjusted to the alignment. */
4821 if (gnu_size)
4822 size = gnu_size;
4823 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4824 && !TYPE_FAT_POINTER_P (gnu_type))
4825 size = rm_size (gnu_type);
4826 else
4827 size = TYPE_SIZE (gnu_type);
4829 /* Consider an alignment as suspicious if the alignment/size
4830 ratio is greater or equal to the byte/bit ratio. */
4831 if (tree_fits_uhwi_p (size)
4832 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4833 post_error_ne ("?suspiciously large alignment specified for&",
4834 Expression (Alignment_Clause (gnat_entity)),
4835 gnat_entity);
4838 else if (Is_Atomic (gnat_entity) && !gnu_size
4839 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4840 && integer_pow2p (TYPE_SIZE (gnu_type)))
4841 align = MIN (BIGGEST_ALIGNMENT,
4842 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4843 else if (Is_Atomic (gnat_entity) && gnu_size
4844 && tree_fits_uhwi_p (gnu_size)
4845 && integer_pow2p (gnu_size))
4846 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4848 /* See if we need to pad the type. If we did, and made a record,
4849 the name of the new type may be changed. So get it back for
4850 us when we make the new TYPE_DECL below. */
4851 if (gnu_size || align > 0)
4852 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4853 false, !gnu_decl, definition, false);
4855 if (TYPE_IS_PADDING_P (gnu_type))
4856 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4858 /* Now set the RM size of the type. We cannot do it before padding
4859 because we need to accept arbitrary RM sizes on integral types. */
4860 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4862 /* If we are at global level, GCC will have applied variable_size to
4863 the type, but that won't have done anything. So, if it's not
4864 a constant or self-referential, call elaborate_expression_1 to
4865 make a variable for the size rather than calculating it each time.
4866 Handle both the RM size and the actual size. */
4867 if (global_bindings_p ()
4868 && TYPE_SIZE (gnu_type)
4869 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4870 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4872 tree size = TYPE_SIZE (gnu_type);
4874 TYPE_SIZE (gnu_type)
4875 = elaborate_expression_1 (size, gnat_entity,
4876 get_identifier ("SIZE"),
4877 definition, false);
4879 /* ??? For now, store the size as a multiple of the alignment in
4880 bytes so that we can see the alignment from the tree. */
4881 TYPE_SIZE_UNIT (gnu_type)
4882 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4883 get_identifier ("SIZE_A_UNIT"),
4884 definition, false,
4885 TYPE_ALIGN (gnu_type));
4887 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4888 may not be marked by the call to create_type_decl below. */
4889 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4891 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4893 tree variant_part = get_variant_part (gnu_type);
4894 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4896 if (variant_part)
4898 tree union_type = TREE_TYPE (variant_part);
4899 tree offset = DECL_FIELD_OFFSET (variant_part);
4901 /* If the position of the variant part is constant, subtract
4902 it from the size of the type of the parent to get the new
4903 size. This manual CSE reduces the data size. */
4904 if (TREE_CODE (offset) == INTEGER_CST)
4906 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4907 TYPE_SIZE (union_type)
4908 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4909 bit_from_pos (offset, bitpos));
4910 TYPE_SIZE_UNIT (union_type)
4911 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4912 byte_from_pos (offset, bitpos));
4914 else
4916 TYPE_SIZE (union_type)
4917 = elaborate_expression_1 (TYPE_SIZE (union_type),
4918 gnat_entity,
4919 get_identifier ("VSIZE"),
4920 definition, false);
4922 /* ??? For now, store the size as a multiple of the
4923 alignment in bytes so that we can see the alignment
4924 from the tree. */
4925 TYPE_SIZE_UNIT (union_type)
4926 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4927 gnat_entity,
4928 get_identifier
4929 ("VSIZE_A_UNIT"),
4930 definition, false,
4931 TYPE_ALIGN (union_type));
4933 /* ??? For now, store the offset as a multiple of the
4934 alignment in bytes so that we can see the alignment
4935 from the tree. */
4936 DECL_FIELD_OFFSET (variant_part)
4937 = elaborate_expression_2 (offset,
4938 gnat_entity,
4939 get_identifier ("VOFFSET"),
4940 definition, false,
4941 DECL_OFFSET_ALIGN
4942 (variant_part));
4945 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4946 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4949 if (operand_equal_p (ada_size, size, 0))
4950 ada_size = TYPE_SIZE (gnu_type);
4951 else
4952 ada_size
4953 = elaborate_expression_1 (ada_size, gnat_entity,
4954 get_identifier ("RM_SIZE"),
4955 definition, false);
4956 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4960 /* If this is a record type or subtype, call elaborate_expression_2 on
4961 any field position. Do this for both global and local types.
4962 Skip any fields that we haven't made trees for to avoid problems with
4963 class wide types. */
4964 if (IN (kind, Record_Kind))
4965 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4966 gnat_temp = Next_Entity (gnat_temp))
4967 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4969 tree gnu_field = get_gnu_tree (gnat_temp);
4971 /* ??? For now, store the offset as a multiple of the alignment
4972 in bytes so that we can see the alignment from the tree. */
4973 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4975 DECL_FIELD_OFFSET (gnu_field)
4976 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4977 gnat_temp,
4978 get_identifier ("OFFSET"),
4979 definition, false,
4980 DECL_OFFSET_ALIGN (gnu_field));
4982 /* ??? The context of gnu_field is not necessarily gnu_type
4983 so the MULT_EXPR node built above may not be marked by
4984 the call to create_type_decl below. */
4985 if (global_bindings_p ())
4986 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4990 if (Is_Atomic (gnat_entity))
4991 check_ok_for_atomic (gnu_type, gnat_entity, false);
4993 /* If this is not an unconstrained array type, set some flags. */
4994 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4996 if (Treat_As_Volatile (gnat_entity))
4997 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
4999 if (Present (Alignment_Clause (gnat_entity)))
5000 TYPE_USER_ALIGN (gnu_type) = 1;
5002 if (Universal_Aliasing (gnat_entity))
5003 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5006 if (!gnu_decl)
5007 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5008 !Comes_From_Source (gnat_entity),
5009 debug_info_p, gnat_entity);
5010 else
5012 TREE_TYPE (gnu_decl) = gnu_type;
5013 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5017 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5019 gnu_type = TREE_TYPE (gnu_decl);
5021 /* If this is a derived type, relate its alias set to that of its parent
5022 to avoid troubles when a call to an inherited primitive is inlined in
5023 a context where a derived object is accessed. The inlined code works
5024 on the parent view so the resulting code may access the same object
5025 using both the parent and the derived alias sets, which thus have to
5026 conflict. As the same issue arises with component references, the
5027 parent alias set also has to conflict with composite types enclosing
5028 derived components. For instance, if we have:
5030 type D is new T;
5031 type R is record
5032 Component : D;
5033 end record;
5035 we want T to conflict with both D and R, in addition to R being a
5036 superset of D by record/component construction.
5038 One way to achieve this is to perform an alias set copy from the
5039 parent to the derived type. This is not quite appropriate, though,
5040 as we don't want separate derived types to conflict with each other:
5042 type I1 is new Integer;
5043 type I2 is new Integer;
5045 We want I1 and I2 to both conflict with Integer but we do not want
5046 I1 to conflict with I2, and an alias set copy on derivation would
5047 have that effect.
5049 The option chosen is to make the alias set of the derived type a
5050 superset of that of its parent type. It trivially fulfills the
5051 simple requirement for the Integer derivation example above, and
5052 the component case as well by superset transitivity:
5054 superset superset
5055 R ----------> D ----------> T
5057 However, for composite types, conversions between derived types are
5058 translated into VIEW_CONVERT_EXPRs so a sequence like:
5060 type Comp1 is new Comp;
5061 type Comp2 is new Comp;
5062 procedure Proc (C : Comp1);
5064 C : Comp2;
5065 Proc (Comp1 (C));
5067 is translated into:
5069 C : Comp2;
5070 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5072 and gimplified into:
5074 C : Comp2;
5075 Comp1 *C.0;
5076 C.0 = (Comp1 *) &C;
5077 Proc (C.0);
5079 i.e. generates code involving type punning. Therefore, Comp1 needs
5080 to conflict with Comp2 and an alias set copy is required.
5082 The language rules ensure the parent type is already frozen here. */
5083 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5085 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5086 /* For constrained packed array subtypes, the implementation type is
5087 used instead of the nominal type. */
5088 if (kind == E_Array_Subtype
5089 && Is_Constrained (gnat_entity)
5090 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5091 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5092 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5093 Is_Composite_Type (gnat_entity)
5094 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5097 /* Back-annotate the Alignment of the type if not already in the
5098 tree. Likewise for sizes. */
5099 if (Unknown_Alignment (gnat_entity))
5101 unsigned int double_align, align;
5102 bool is_capped_double, align_clause;
5104 /* If the default alignment of "double" or larger scalar types is
5105 specifically capped and this is not an array with an alignment
5106 clause on the component type, return the cap. */
5107 if ((double_align = double_float_alignment) > 0)
5108 is_capped_double
5109 = is_double_float_or_array (gnat_entity, &align_clause);
5110 else if ((double_align = double_scalar_alignment) > 0)
5111 is_capped_double
5112 = is_double_scalar_or_array (gnat_entity, &align_clause);
5113 else
5114 is_capped_double = align_clause = false;
5116 if (is_capped_double && !align_clause)
5117 align = double_align;
5118 else
5119 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5121 Set_Alignment (gnat_entity, UI_From_Int (align));
5124 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5126 tree gnu_size = TYPE_SIZE (gnu_type);
5128 /* If the size is self-referential, annotate the maximum value. */
5129 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5130 gnu_size = max_size (gnu_size, true);
5132 /* If we are just annotating types and the type is tagged, the tag
5133 and the parent components are not generated by the front-end so
5134 sizes must be adjusted if there is no representation clause. */
5135 if (type_annotate_only
5136 && Is_Tagged_Type (gnat_entity)
5137 && !VOID_TYPE_P (gnu_type)
5138 && (!TYPE_FIELDS (gnu_type)
5139 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5141 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5142 Uint uint_size;
5144 if (Is_Derived_Type (gnat_entity))
5146 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5147 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5148 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5150 else
5151 offset = pointer_size;
5153 if (TYPE_FIELDS (gnu_type))
5154 offset
5155 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5157 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5158 gnu_size = round_up (gnu_size, POINTER_SIZE);
5159 uint_size = annotate_value (gnu_size);
5160 Set_Esize (gnat_entity, uint_size);
5161 Set_RM_Size (gnat_entity, uint_size);
5163 else
5164 Set_Esize (gnat_entity, annotate_value (gnu_size));
5167 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5168 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5171 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5172 cannot do so if we are reusing the ..._DECL node made for an equivalent
5173 type or an alias or a renamed object as the predicates don't apply to it
5174 but to GNAT_ENTITY. */
5175 if (DECL_P (gnu_decl)
5176 && !(is_type && gnat_equiv_type != gnat_entity)
5177 && !Present (Alias (gnat_entity))
5178 && !(Present (Renamed_Object (gnat_entity)) && saved))
5180 if (!Comes_From_Source (gnat_entity))
5181 DECL_ARTIFICIAL (gnu_decl) = 1;
5183 if (!debug_info_p)
5184 DECL_IGNORED_P (gnu_decl) = 1;
5187 /* If we haven't already, associate the ..._DECL node that we just made with
5188 the input GNAT entity node. */
5189 if (!saved)
5190 save_gnu_tree (gnat_entity, gnu_decl, false);
5192 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5193 eliminate as many deferred computations as possible. */
5194 process_deferred_decl_context (false);
5196 /* If this is an enumeration or floating-point type, we were not able to set
5197 the bounds since they refer to the type. These are always static. */
5198 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5199 || (kind == E_Floating_Point_Type))
5201 tree gnu_scalar_type = gnu_type;
5202 tree gnu_low_bound, gnu_high_bound;
5204 /* If this is a padded type, we need to use the underlying type. */
5205 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5206 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5208 /* If this is a floating point type and we haven't set a floating
5209 point type yet, use this in the evaluation of the bounds. */
5210 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5211 longest_float_type_node = gnu_scalar_type;
5213 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5214 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5216 if (kind == E_Enumeration_Type)
5218 /* Enumeration types have specific RM bounds. */
5219 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5220 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5222 else
5224 /* Floating-point types don't have specific RM bounds. */
5225 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5226 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5230 /* If we deferred processing of incomplete types, re-enable it. If there
5231 were no other disables and we have deferred types to process, do so. */
5232 if (this_deferred
5233 && --defer_incomplete_level == 0
5234 && defer_incomplete_list)
5236 struct incomplete *p, *next;
5238 /* We are back to level 0 for the deferring of incomplete types.
5239 But processing these incomplete types below may itself require
5240 deferring, so preserve what we have and restart from scratch. */
5241 p = defer_incomplete_list;
5242 defer_incomplete_list = NULL;
5244 for (; p; p = next)
5246 next = p->next;
5248 if (p->old_type)
5249 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5250 gnat_to_gnu_type (p->full_type));
5251 free (p);
5255 /* If we are not defining this type, see if it's on one of the lists of
5256 incomplete types. If so, handle the list entry now. */
5257 if (is_type && !definition)
5259 struct incomplete *p;
5261 for (p = defer_incomplete_list; p; p = p->next)
5262 if (p->old_type && p->full_type == gnat_entity)
5264 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5265 TREE_TYPE (gnu_decl));
5266 p->old_type = NULL_TREE;
5269 for (p = defer_limited_with; p; p = p->next)
5270 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5272 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5273 TREE_TYPE (gnu_decl));
5274 p->old_type = NULL_TREE;
5278 if (this_global)
5279 force_global--;
5281 /* If this is a packed array type whose original array type is itself
5282 an Itype without freeze node, make sure the latter is processed. */
5283 if (Is_Packed_Array_Impl_Type (gnat_entity)
5284 && Is_Itype (Original_Array_Type (gnat_entity))
5285 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5286 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5287 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5289 return gnu_decl;
5292 /* Similar, but if the returned value is a COMPONENT_REF, return the
5293 FIELD_DECL. */
5295 tree
5296 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5298 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5300 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5301 gnu_field = TREE_OPERAND (gnu_field, 1);
5303 return gnu_field;
5306 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5307 the GCC type corresponding to that entity. */
5309 tree
5310 gnat_to_gnu_type (Entity_Id gnat_entity)
5312 tree gnu_decl;
5314 /* The back end never attempts to annotate generic types. */
5315 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5316 return void_type_node;
5318 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5319 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5321 return TREE_TYPE (gnu_decl);
5324 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5325 the unpadded version of the GCC type corresponding to that entity. */
5327 tree
5328 get_unpadded_type (Entity_Id gnat_entity)
5330 tree type = gnat_to_gnu_type (gnat_entity);
5332 if (TYPE_IS_PADDING_P (type))
5333 type = TREE_TYPE (TYPE_FIELDS (type));
5335 return type;
5338 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5339 type has been changed to that of the parameterless procedure, except if an
5340 alias is already present, in which case it is returned instead. */
5342 tree
5343 get_minimal_subprog_decl (Entity_Id gnat_entity)
5345 tree gnu_entity_name, gnu_ext_name;
5346 struct attrib *attr_list = NULL;
5348 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5349 of the handling applied here. */
5351 while (Present (Alias (gnat_entity)))
5353 gnat_entity = Alias (gnat_entity);
5354 if (present_gnu_tree (gnat_entity))
5355 return get_gnu_tree (gnat_entity);
5358 gnu_entity_name = get_entity_name (gnat_entity);
5359 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5361 if (Has_Stdcall_Convention (gnat_entity))
5362 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5363 get_identifier ("stdcall"), NULL_TREE,
5364 gnat_entity);
5365 else if (Has_Thiscall_Convention (gnat_entity))
5366 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5367 get_identifier ("thiscall"), NULL_TREE,
5368 gnat_entity);
5370 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5371 gnu_ext_name = NULL_TREE;
5373 return
5374 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5375 is_disabled, true, true, true, attr_list, gnat_entity);
5378 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5379 a C++ imported method or equivalent.
5381 We use the predicate on 32-bit x86/Windows to find out whether we need to
5382 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5383 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5385 bool
5386 is_cplusplus_method (Entity_Id gnat_entity)
5388 if (Convention (gnat_entity) != Convention_CPP)
5389 return false;
5391 /* This is the main case: C++ method imported as a primitive operation. */
5392 if (Is_Dispatching_Operation (gnat_entity))
5393 return true;
5395 /* A thunk needs to be handled like its associated primitive operation. */
5396 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5397 return true;
5399 /* C++ classes with no virtual functions can be imported as limited
5400 record types, but we need to return true for the constructors. */
5401 if (Is_Constructor (gnat_entity))
5402 return true;
5404 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5405 if (Is_Dispatch_Table_Entity (gnat_entity))
5406 return true;
5408 return false;
5411 /* Finalize the processing of From_Limited_With incomplete types. */
5413 void
5414 finalize_from_limited_with (void)
5416 struct incomplete *p, *next;
5418 p = defer_limited_with;
5419 defer_limited_with = NULL;
5421 for (; p; p = next)
5423 next = p->next;
5425 if (p->old_type)
5426 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5427 gnat_to_gnu_type (p->full_type));
5428 free (p);
5432 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5433 kind of type (such E_Task_Type) that has a different type which Gigi
5434 uses for its representation. If the type does not have a special type
5435 for its representation, return GNAT_ENTITY. If a type is supposed to
5436 exist, but does not, abort unless annotating types, in which case
5437 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5439 Entity_Id
5440 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5442 Entity_Id gnat_equiv = gnat_entity;
5444 if (No (gnat_entity))
5445 return gnat_entity;
5447 switch (Ekind (gnat_entity))
5449 case E_Class_Wide_Subtype:
5450 if (Present (Equivalent_Type (gnat_entity)))
5451 gnat_equiv = Equivalent_Type (gnat_entity);
5452 break;
5454 case E_Access_Protected_Subprogram_Type:
5455 case E_Anonymous_Access_Protected_Subprogram_Type:
5456 gnat_equiv = Equivalent_Type (gnat_entity);
5457 break;
5459 case E_Class_Wide_Type:
5460 gnat_equiv = Root_Type (gnat_entity);
5461 break;
5463 case E_Task_Type:
5464 case E_Task_Subtype:
5465 case E_Protected_Type:
5466 case E_Protected_Subtype:
5467 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5468 break;
5470 default:
5471 break;
5474 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5476 return gnat_equiv;
5479 /* Return a GCC tree for a type corresponding to the component type of the
5480 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5481 is for an array being defined. DEBUG_INFO_P is true if we need to write
5482 debug information for other types that we may create in the process. */
5484 static tree
5485 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5486 bool debug_info_p)
5488 const Entity_Id gnat_type = Component_Type (gnat_array);
5489 tree gnu_type = gnat_to_gnu_type (gnat_type);
5490 tree gnu_comp_size;
5492 /* Try to get a smaller form of the component if needed. */
5493 if ((Is_Packed (gnat_array)
5494 || Has_Component_Size_Clause (gnat_array))
5495 && !Is_Bit_Packed_Array (gnat_array)
5496 && !Has_Aliased_Components (gnat_array)
5497 && !Strict_Alignment (gnat_type)
5498 && RECORD_OR_UNION_TYPE_P (gnu_type)
5499 && !TYPE_FAT_POINTER_P (gnu_type)
5500 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5501 gnu_type = make_packable_type (gnu_type, false);
5503 if (Has_Atomic_Components (gnat_array))
5504 check_ok_for_atomic (gnu_type, gnat_array, true);
5506 /* Get and validate any specified Component_Size. */
5507 gnu_comp_size
5508 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5509 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5510 true, Has_Component_Size_Clause (gnat_array));
5512 /* If the array has aliased components and the component size can be zero,
5513 force at least unit size to ensure that the components have distinct
5514 addresses. */
5515 if (!gnu_comp_size
5516 && Has_Aliased_Components (gnat_array)
5517 && (integer_zerop (TYPE_SIZE (gnu_type))
5518 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5519 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5520 gnu_comp_size
5521 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5523 /* If the component type is a RECORD_TYPE that has a self-referential size,
5524 then use the maximum size for the component size. */
5525 if (!gnu_comp_size
5526 && TREE_CODE (gnu_type) == RECORD_TYPE
5527 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5528 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5530 /* Honor the component size. This is not needed for bit-packed arrays. */
5531 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5533 tree orig_type = gnu_type;
5534 unsigned int max_align;
5536 /* If an alignment is specified, use it as a cap on the component type
5537 so that it can be honored for the whole type. But ignore it for the
5538 original type of packed array types. */
5539 if (No (Packed_Array_Impl_Type (gnat_array))
5540 && Known_Alignment (gnat_array))
5541 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5542 else
5543 max_align = 0;
5545 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5546 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5547 gnu_type = orig_type;
5548 else
5549 orig_type = gnu_type;
5551 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5552 true, false, definition, true);
5554 /* If a padding record was made, declare it now since it will never be
5555 declared otherwise. This is necessary to ensure that its subtrees
5556 are properly marked. */
5557 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5558 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5559 gnat_array);
5562 if (Has_Volatile_Components (gnat_array))
5563 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5565 return gnu_type;
5568 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5569 using MECH as its passing mechanism, to be placed in the parameter
5570 list built for GNAT_SUBPROG. Assume a foreign convention for the
5571 latter if FOREIGN is true. Also set CICO to true if the parameter
5572 must use the copy-in copy-out implementation mechanism.
5574 The returned tree is a PARM_DECL, except for those cases where no
5575 parameter needs to be actually passed to the subprogram; the type
5576 of this "shadow" parameter is then returned instead. */
5578 static tree
5579 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5580 Entity_Id gnat_subprog, bool foreign, bool *cico)
5582 tree gnu_param_name = get_entity_name (gnat_param);
5583 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5584 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5585 /* The parameter can be indirectly modified if its address is taken. */
5586 bool ro_param = in_param && !Address_Taken (gnat_param);
5587 bool by_return = false, by_component_ptr = false;
5588 bool by_ref = false;
5589 tree gnu_param;
5591 /* Copy-return is used only for the first parameter of a valued procedure.
5592 It's a copy mechanism for which a parameter is never allocated. */
5593 if (mech == By_Copy_Return)
5595 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5596 mech = By_Copy;
5597 by_return = true;
5600 /* If this is either a foreign function or if the underlying type won't
5601 be passed by reference, strip off possible padding type. */
5602 if (TYPE_IS_PADDING_P (gnu_param_type))
5604 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5606 if (mech == By_Reference
5607 || foreign
5608 || (!must_pass_by_ref (unpadded_type)
5609 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5610 gnu_param_type = unpadded_type;
5613 /* If this is a read-only parameter, make a variant of the type that is
5614 read-only. ??? However, if this is an unconstrained array, that type
5615 can be very complex, so skip it for now. Likewise for any other
5616 self-referential type. */
5617 if (ro_param
5618 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5619 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5620 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5622 /* For foreign conventions, pass arrays as pointers to the element type.
5623 First check for unconstrained array and get the underlying array. */
5624 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5625 gnu_param_type
5626 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5628 /* For GCC builtins, pass Address integer types as (void *) */
5629 if (Convention (gnat_subprog) == Convention_Intrinsic
5630 && Present (Interface_Name (gnat_subprog))
5631 && Is_Descendent_Of_Address (Etype (gnat_param)))
5632 gnu_param_type = ptr_void_type_node;
5634 /* Arrays are passed as pointers to element type for foreign conventions. */
5635 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5637 /* Strip off any multi-dimensional entries, then strip
5638 off the last array to get the component type. */
5639 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5640 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5641 gnu_param_type = TREE_TYPE (gnu_param_type);
5643 by_component_ptr = true;
5644 gnu_param_type = TREE_TYPE (gnu_param_type);
5646 if (ro_param)
5647 gnu_param_type
5648 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5650 gnu_param_type = build_pointer_type (gnu_param_type);
5653 /* Fat pointers are passed as thin pointers for foreign conventions. */
5654 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5655 gnu_param_type
5656 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5658 /* If we must pass or were requested to pass by reference, do so.
5659 If we were requested to pass by copy, do so.
5660 Otherwise, for foreign conventions, pass In Out or Out parameters
5661 or aggregates by reference. For COBOL and Fortran, pass all
5662 integer and FP types that way too. For Convention Ada, use
5663 the standard Ada default. */
5664 else if (must_pass_by_ref (gnu_param_type)
5665 || mech == By_Reference
5666 || (mech != By_Copy
5667 && ((foreign
5668 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5669 || (foreign
5670 && (Convention (gnat_subprog) == Convention_Fortran
5671 || Convention (gnat_subprog) == Convention_COBOL)
5672 && (INTEGRAL_TYPE_P (gnu_param_type)
5673 || FLOAT_TYPE_P (gnu_param_type)))
5674 || (!foreign
5675 && default_pass_by_ref (gnu_param_type)))))
5677 /* We take advantage of 6.2(12) by considering that references built for
5678 parameters whose type isn't by-ref and for which the mechanism hasn't
5679 been forced to by-ref are restrict-qualified in the C sense. */
5680 bool restrict_p
5681 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5682 gnu_param_type = build_reference_type (gnu_param_type);
5683 if (restrict_p)
5684 gnu_param_type
5685 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5686 by_ref = true;
5689 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5690 else if (!in_param)
5691 *cico = true;
5693 if (mech == By_Copy && (by_ref || by_component_ptr))
5694 post_error ("?cannot pass & by copy", gnat_param);
5696 /* If this is an Out parameter that isn't passed by reference and isn't
5697 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5698 it will be a VAR_DECL created when we process the procedure, so just
5699 return its type. For the special parameter of a valued procedure,
5700 never pass it in.
5702 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5703 Out parameters with discriminants or implicit initial values to be
5704 handled like In Out parameters. These type are normally built as
5705 aggregates, hence passed by reference, except for some packed arrays
5706 which end up encoded in special integer types. Note that scalars can
5707 be given implicit initial values using the Default_Value aspect.
5709 The exception we need to make is then for packed arrays of records
5710 with discriminants or implicit initial values. We have no light/easy
5711 way to check for the latter case, so we merely check for packed arrays
5712 of records. This may lead to useless copy-in operations, but in very
5713 rare cases only, as these would be exceptions in a set of already
5714 exceptional situations. */
5715 if (Ekind (gnat_param) == E_Out_Parameter
5716 && !by_ref
5717 && (by_return
5718 || (!POINTER_TYPE_P (gnu_param_type)
5719 && !AGGREGATE_TYPE_P (gnu_param_type)
5720 && !Has_Default_Aspect (Etype (gnat_param))))
5721 && !(Is_Array_Type (Etype (gnat_param))
5722 && Is_Packed (Etype (gnat_param))
5723 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5724 return gnu_param_type;
5726 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5727 ro_param || by_ref || by_component_ptr);
5728 DECL_BY_REF_P (gnu_param) = by_ref;
5729 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5730 DECL_POINTS_TO_READONLY_P (gnu_param)
5731 = (ro_param && (by_ref || by_component_ptr));
5732 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5734 /* If no Mechanism was specified, indicate what we're using, then
5735 back-annotate it. */
5736 if (mech == Default)
5737 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5739 Set_Mechanism (gnat_param, mech);
5740 return gnu_param;
5743 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5744 qualifiers on TYPE. */
5746 static tree
5747 change_qualified_type (tree type, int type_quals)
5749 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5752 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5754 static bool
5755 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5757 while (Present (Corresponding_Discriminant (discr1)))
5758 discr1 = Corresponding_Discriminant (discr1);
5760 while (Present (Corresponding_Discriminant (discr2)))
5761 discr2 = Corresponding_Discriminant (discr2);
5763 return
5764 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5767 /* Return true if the array type GNU_TYPE, which represents a dimension of
5768 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5770 static bool
5771 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5773 /* If the array type is not the innermost dimension of the GNAT type,
5774 then it has a non-aliased component. */
5775 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5776 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5777 return true;
5779 /* If the array type has an aliased component in the front-end sense,
5780 then it also has an aliased component in the back-end sense. */
5781 if (Has_Aliased_Components (gnat_type))
5782 return false;
5784 /* If this is a derived type, then it has a non-aliased component if
5785 and only if its parent type also has one. */
5786 if (Is_Derived_Type (gnat_type))
5788 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5789 int index;
5790 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5791 gnu_parent_type
5792 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5793 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5794 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5795 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5798 /* Otherwise, rely exclusively on properties of the element type. */
5799 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5802 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5804 static bool
5805 compile_time_known_address_p (Node_Id gnat_address)
5807 /* Catch System'To_Address. */
5808 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5809 gnat_address = Expression (gnat_address);
5811 return Compile_Time_Known_Value (gnat_address);
5814 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5815 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5817 static bool
5818 cannot_be_superflat_p (Node_Id gnat_range)
5820 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5821 Node_Id scalar_range;
5822 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5824 /* If the low bound is not constant, try to find an upper bound. */
5825 while (Nkind (gnat_lb) != N_Integer_Literal
5826 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5827 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5828 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5829 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5830 || Nkind (scalar_range) == N_Range))
5831 gnat_lb = High_Bound (scalar_range);
5833 /* If the high bound is not constant, try to find a lower bound. */
5834 while (Nkind (gnat_hb) != N_Integer_Literal
5835 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5836 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5837 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5838 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5839 || Nkind (scalar_range) == N_Range))
5840 gnat_hb = Low_Bound (scalar_range);
5842 /* If we have failed to find constant bounds, punt. */
5843 if (Nkind (gnat_lb) != N_Integer_Literal
5844 || Nkind (gnat_hb) != N_Integer_Literal)
5845 return false;
5847 /* We need at least a signed 64-bit type to catch most cases. */
5848 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5849 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5850 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5851 return false;
5853 /* If the low bound is the smallest integer, nothing can be smaller. */
5854 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5855 if (TREE_OVERFLOW (gnu_lb_minus_one))
5856 return true;
5858 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5861 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5863 static bool
5864 constructor_address_p (tree gnu_expr)
5866 while (TREE_CODE (gnu_expr) == NOP_EXPR
5867 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5868 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5869 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5871 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5872 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5875 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5876 be elaborated at the point of its definition, but do nothing else. */
5878 void
5879 elaborate_entity (Entity_Id gnat_entity)
5881 switch (Ekind (gnat_entity))
5883 case E_Signed_Integer_Subtype:
5884 case E_Modular_Integer_Subtype:
5885 case E_Enumeration_Subtype:
5886 case E_Ordinary_Fixed_Point_Subtype:
5887 case E_Decimal_Fixed_Point_Subtype:
5888 case E_Floating_Point_Subtype:
5890 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5891 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5893 /* ??? Tests to avoid Constraint_Error in static expressions
5894 are needed until after the front stops generating bogus
5895 conversions on bounds of real types. */
5896 if (!Raises_Constraint_Error (gnat_lb))
5897 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5898 true, false, Needs_Debug_Info (gnat_entity));
5899 if (!Raises_Constraint_Error (gnat_hb))
5900 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5901 true, false, Needs_Debug_Info (gnat_entity));
5902 break;
5905 case E_Record_Subtype:
5906 case E_Private_Subtype:
5907 case E_Limited_Private_Subtype:
5908 case E_Record_Subtype_With_Private:
5909 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5911 Node_Id gnat_discriminant_expr;
5912 Entity_Id gnat_field;
5914 for (gnat_field
5915 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5916 gnat_discriminant_expr
5917 = First_Elmt (Discriminant_Constraint (gnat_entity));
5918 Present (gnat_field);
5919 gnat_field = Next_Discriminant (gnat_field),
5920 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5921 /* Ignore access discriminants. */
5922 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5923 elaborate_expression (Node (gnat_discriminant_expr),
5924 gnat_entity, get_entity_name (gnat_field),
5925 true, false, false);
5927 break;
5932 /* Return true if the size in units represented by GNU_SIZE can be handled by
5933 an allocation. If STATIC_P is true, consider only what can be done with a
5934 static allocation. */
5936 static bool
5937 allocatable_size_p (tree gnu_size, bool static_p)
5939 /* We can allocate a fixed size if it is a valid for the middle-end. */
5940 if (TREE_CODE (gnu_size) == INTEGER_CST)
5941 return valid_constant_size_p (gnu_size);
5943 /* We can allocate a variable size if this isn't a static allocation. */
5944 else
5945 return !static_p;
5948 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5949 NAME, ARGS and ERROR_POINT. */
5951 static void
5952 prepend_one_attribute (struct attrib **attr_list,
5953 enum attr_type attr_type,
5954 tree attr_name,
5955 tree attr_args,
5956 Node_Id attr_error_point)
5958 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5960 attr->type = attr_type;
5961 attr->name = attr_name;
5962 attr->args = attr_args;
5963 attr->error_point = attr_error_point;
5965 attr->next = *attr_list;
5966 *attr_list = attr;
5969 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
5971 static void
5972 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
5974 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
5975 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5976 enum attr_type etype;
5978 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
5979 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
5981 case Pragma_Machine_Attribute:
5982 etype = ATTR_MACHINE_ATTRIBUTE;
5983 break;
5985 case Pragma_Linker_Alias:
5986 etype = ATTR_LINK_ALIAS;
5987 break;
5989 case Pragma_Linker_Section:
5990 etype = ATTR_LINK_SECTION;
5991 break;
5993 case Pragma_Linker_Constructor:
5994 etype = ATTR_LINK_CONSTRUCTOR;
5995 break;
5997 case Pragma_Linker_Destructor:
5998 etype = ATTR_LINK_DESTRUCTOR;
5999 break;
6001 case Pragma_Weak_External:
6002 etype = ATTR_WEAK_EXTERNAL;
6003 break;
6005 case Pragma_Thread_Local_Storage:
6006 etype = ATTR_THREAD_LOCAL_STORAGE;
6007 break;
6009 default:
6010 return;
6013 /* See what arguments we have and turn them into GCC trees for attribute
6014 handlers. These expect identifier for strings. We handle at most two
6015 arguments and static expressions only. */
6016 if (Present (gnat_arg) && Present (First (gnat_arg)))
6018 Node_Id gnat_arg0 = Next (First (gnat_arg));
6019 Node_Id gnat_arg1 = Empty;
6021 if (Present (gnat_arg0)
6022 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6024 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6026 if (TREE_CODE (gnu_arg0) == STRING_CST)
6028 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6029 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6030 return;
6033 gnat_arg1 = Next (gnat_arg0);
6036 if (Present (gnat_arg1)
6037 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6039 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6041 if (TREE_CODE (gnu_arg1) == STRING_CST)
6042 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6046 /* Prepend to the list. Make a list of the argument we might have, as GCC
6047 expects it. */
6048 prepend_one_attribute (attr_list, etype, gnu_arg0,
6049 gnu_arg1
6050 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6051 Present (Next (First (gnat_arg)))
6052 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6055 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6057 static void
6058 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6060 Node_Id gnat_temp;
6062 /* Attributes are stored as Representation Item pragmas. */
6063 for (gnat_temp = First_Rep_Item (gnat_entity);
6064 Present (gnat_temp);
6065 gnat_temp = Next_Rep_Item (gnat_temp))
6066 if (Nkind (gnat_temp) == N_Pragma)
6067 prepend_one_attribute_pragma (attr_list, gnat_temp);
6070 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6071 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6072 return the GCC tree to use for that expression. GNU_NAME is the suffix
6073 to use if a variable needs to be created and DEFINITION is true if this
6074 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6075 otherwise, we are just elaborating the expression for side-effects. If
6076 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6077 isn't needed for code generation. */
6079 static tree
6080 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6081 bool definition, bool need_value, bool need_debug)
6083 tree gnu_expr;
6085 /* If we already elaborated this expression (e.g. it was involved
6086 in the definition of a private type), use the old value. */
6087 if (present_gnu_tree (gnat_expr))
6088 return get_gnu_tree (gnat_expr);
6090 /* If we don't need a value and this is static or a discriminant,
6091 we don't need to do anything. */
6092 if (!need_value
6093 && (Is_OK_Static_Expression (gnat_expr)
6094 || (Nkind (gnat_expr) == N_Identifier
6095 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6096 return NULL_TREE;
6098 /* If it's a static expression, we don't need a variable for debugging. */
6099 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6100 need_debug = false;
6102 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6103 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6104 gnu_name, definition, need_debug);
6106 /* Save the expression in case we try to elaborate this entity again. Since
6107 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6108 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6109 save_gnu_tree (gnat_expr, gnu_expr, true);
6111 return need_value ? gnu_expr : error_mark_node;
6114 /* Similar, but take a GNU expression and always return a result. */
6116 static tree
6117 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6118 bool definition, bool need_debug)
6120 const bool expr_public_p = Is_Public (gnat_entity);
6121 const bool expr_global_p = expr_public_p || global_bindings_p ();
6122 bool expr_variable_p, use_variable;
6124 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6125 reference will have been replaced with a COMPONENT_REF when the type
6126 is being elaborated. However, there are some cases involving child
6127 types where we will. So convert it to a COMPONENT_REF. We hope it
6128 will be at the highest level of the expression in these cases. */
6129 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6130 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6131 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6132 gnu_expr, NULL_TREE);
6134 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6135 that an expression cannot contain both a discriminant and a variable. */
6136 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6137 return gnu_expr;
6139 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6140 a variable that is initialized to contain the expression when the package
6141 containing the definition is elaborated. If this entity is defined at top
6142 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6143 if this is necessary. */
6144 if (CONSTANT_CLASS_P (gnu_expr))
6145 expr_variable_p = false;
6146 else
6148 /* Skip any conversions and simple constant arithmetics to see if the
6149 expression is based on a read-only variable.
6150 ??? This really should remain read-only, but we have to think about
6151 the typing of the tree here. */
6152 tree inner = remove_conversions (gnu_expr, true);
6154 inner = skip_simple_constant_arithmetic (inner);
6156 if (handled_component_p (inner))
6158 HOST_WIDE_INT bitsize, bitpos;
6159 tree offset;
6160 machine_mode mode;
6161 int unsignedp, volatilep;
6163 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6164 &mode, &unsignedp, &volatilep, false);
6165 /* If the offset is variable, err on the side of caution. */
6166 if (offset)
6167 inner = NULL_TREE;
6170 expr_variable_p
6171 = !(inner
6172 && TREE_CODE (inner) == VAR_DECL
6173 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6176 /* We only need to use the variable if we are in a global context since GCC
6177 can do the right thing in the local case. However, when not optimizing,
6178 use it for bounds of loop iteration scheme to avoid code duplication. */
6179 use_variable = expr_variable_p
6180 && (expr_global_p
6181 || (!optimize
6182 && definition
6183 && Is_Itype (gnat_entity)
6184 && Nkind (Associated_Node_For_Itype (gnat_entity))
6185 == N_Loop_Parameter_Specification));
6187 /* Now create it, possibly only for debugging purposes. */
6188 if (use_variable || need_debug)
6190 /* The following variable creation can happen when processing the body of
6191 subprograms that are defined out of the extended main unit and
6192 inlined. In this case, we are not at the global scope, and thus the
6193 new variable must not be tagged "external", as we used to do here as
6194 long as definition == 0. */
6195 const bool external_flag = !definition && expr_global_p;
6196 tree gnu_decl
6197 = create_var_decl_1
6198 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6199 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6200 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6202 DECL_ARTIFICIAL (gnu_decl) = 1;
6204 /* Using this variable at debug time (if need_debug is true) requires a
6205 proper location. The back-end will compute a location for this
6206 variable only if the variable is used by the generated code.
6207 Returning the variable ensures the caller will use it in generated
6208 code. Note that there is no need for a location if the debug info
6209 contains an integer constant.
6210 FIXME: when the encoding-based debug scheme is dropped, move this
6211 condition to the top-level IF block: we will not need to create a
6212 variable anymore in such cases, then. */
6213 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6214 return gnu_decl;
6217 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6220 /* Similar, but take an alignment factor and make it explicit in the tree. */
6222 static tree
6223 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6224 bool definition, bool need_debug, unsigned int align)
6226 tree unit_align = size_int (align / BITS_PER_UNIT);
6227 return
6228 size_binop (MULT_EXPR,
6229 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6230 gnu_expr,
6231 unit_align),
6232 gnat_entity, gnu_name, definition,
6233 need_debug),
6234 unit_align);
6237 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6238 the value passed against the list of choices. */
6240 tree
6241 choices_to_gnu (tree operand, Node_Id choices)
6243 Node_Id choice;
6244 Node_Id gnat_temp;
6245 tree result = boolean_false_node;
6246 tree this_test, low = 0, high = 0, single = 0;
6248 for (choice = First (choices); Present (choice); choice = Next (choice))
6250 switch (Nkind (choice))
6252 case N_Range:
6253 low = gnat_to_gnu (Low_Bound (choice));
6254 high = gnat_to_gnu (High_Bound (choice));
6256 this_test
6257 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6258 build_binary_op (GE_EXPR, boolean_type_node,
6259 operand, low),
6260 build_binary_op (LE_EXPR, boolean_type_node,
6261 operand, high));
6263 break;
6265 case N_Subtype_Indication:
6266 gnat_temp = Range_Expression (Constraint (choice));
6267 low = gnat_to_gnu (Low_Bound (gnat_temp));
6268 high = gnat_to_gnu (High_Bound (gnat_temp));
6270 this_test
6271 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6272 build_binary_op (GE_EXPR, boolean_type_node,
6273 operand, low),
6274 build_binary_op (LE_EXPR, boolean_type_node,
6275 operand, high));
6276 break;
6278 case N_Identifier:
6279 case N_Expanded_Name:
6280 /* This represents either a subtype range, an enumeration
6281 literal, or a constant Ekind says which. If an enumeration
6282 literal or constant, fall through to the next case. */
6283 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6284 && Ekind (Entity (choice)) != E_Constant)
6286 tree type = gnat_to_gnu_type (Entity (choice));
6288 low = TYPE_MIN_VALUE (type);
6289 high = TYPE_MAX_VALUE (type);
6291 this_test
6292 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6293 build_binary_op (GE_EXPR, boolean_type_node,
6294 operand, low),
6295 build_binary_op (LE_EXPR, boolean_type_node,
6296 operand, high));
6297 break;
6300 /* ... fall through ... */
6302 case N_Character_Literal:
6303 case N_Integer_Literal:
6304 single = gnat_to_gnu (choice);
6305 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6306 single);
6307 break;
6309 case N_Others_Choice:
6310 this_test = boolean_true_node;
6311 break;
6313 default:
6314 gcc_unreachable ();
6317 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6318 this_test);
6321 return result;
6324 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6325 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6327 static int
6328 adjust_packed (tree field_type, tree record_type, int packed)
6330 /* If the field contains an item of variable size, we cannot pack it
6331 because we cannot create temporaries of non-fixed size in case
6332 we need to take the address of the field. See addressable_p and
6333 the notes on the addressability issues for further details. */
6334 if (type_has_variable_size (field_type))
6335 return 0;
6337 /* If the alignment of the record is specified and the field type
6338 is over-aligned, request Storage_Unit alignment for the field. */
6339 if (packed == -2)
6341 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6342 return -1;
6343 else
6344 return 0;
6347 return packed;
6350 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6351 placed in GNU_RECORD_TYPE.
6353 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6354 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6355 record has a specified alignment.
6357 DEFINITION is true if this field is for a record being defined.
6359 DEBUG_INFO_P is true if we need to write debug information for types
6360 that we may create in the process. */
6362 static tree
6363 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6364 bool definition, bool debug_info_p)
6366 const Entity_Id gnat_field_type = Etype (gnat_field);
6367 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6368 tree gnu_field_id = get_entity_name (gnat_field);
6369 tree gnu_field, gnu_size, gnu_pos;
6370 bool is_volatile
6371 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6372 bool needs_strict_alignment
6373 = (is_volatile
6374 || Is_Aliased (gnat_field)
6375 || Strict_Alignment (gnat_field_type));
6377 /* If this field requires strict alignment, we cannot pack it because
6378 it would very likely be under-aligned in the record. */
6379 if (needs_strict_alignment)
6380 packed = 0;
6381 else
6382 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6384 /* If a size is specified, use it. Otherwise, if the record type is packed,
6385 use the official RM size. See "Handling of Type'Size Values" in Einfo
6386 for further details. */
6387 if (Known_Esize (gnat_field))
6388 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6389 gnat_field, FIELD_DECL, false, true);
6390 else if (packed == 1)
6391 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6392 gnat_field, FIELD_DECL, false, true);
6393 else
6394 gnu_size = NULL_TREE;
6396 /* If we have a specified size that is smaller than that of the field's type,
6397 or a position is specified, and the field's type is a record that doesn't
6398 require strict alignment, see if we can get either an integral mode form
6399 of the type or a smaller form. If we can, show a size was specified for
6400 the field if there wasn't one already, so we know to make this a bitfield
6401 and avoid making things wider.
6403 Changing to an integral mode form is useful when the record is packed as
6404 we can then place the field at a non-byte-aligned position and so achieve
6405 tighter packing. This is in addition required if the field shares a byte
6406 with another field and the front-end lets the back-end handle the access
6407 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6409 Changing to a smaller form is required if the specified size is smaller
6410 than that of the field's type and the type contains sub-fields that are
6411 padded, in order to avoid generating accesses to these sub-fields that
6412 are wider than the field.
6414 We avoid the transformation if it is not required or potentially useful,
6415 as it might entail an increase of the field's alignment and have ripple
6416 effects on the outer record type. A typical case is a field known to be
6417 byte-aligned and not to share a byte with another field. */
6418 if (!needs_strict_alignment
6419 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6420 && !TYPE_FAT_POINTER_P (gnu_field_type)
6421 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6422 && (packed == 1
6423 || (gnu_size
6424 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6425 || (Present (Component_Clause (gnat_field))
6426 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6427 % BITS_PER_UNIT == 0
6428 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6430 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6431 if (gnu_packable_type != gnu_field_type)
6433 gnu_field_type = gnu_packable_type;
6434 if (!gnu_size)
6435 gnu_size = rm_size (gnu_field_type);
6439 if (Is_Atomic (gnat_field))
6440 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6442 if (Present (Component_Clause (gnat_field)))
6444 Entity_Id gnat_parent
6445 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6447 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6448 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6449 gnat_field, FIELD_DECL, false, true);
6451 /* Ensure the position does not overlap with the parent subtype, if there
6452 is one. This test is omitted if the parent of the tagged type has a
6453 full rep clause since, in this case, component clauses are allowed to
6454 overlay the space allocated for the parent type and the front-end has
6455 checked that there are no overlapping components. */
6456 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6458 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6460 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6461 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6463 post_error_ne_tree
6464 ("offset of& must be beyond parent{, minimum allowed is ^}",
6465 First_Bit (Component_Clause (gnat_field)), gnat_field,
6466 TYPE_SIZE_UNIT (gnu_parent));
6470 /* If this field needs strict alignment, check that the record is
6471 sufficiently aligned and that position and size are consistent with
6472 the alignment. But don't do it if we are just annotating types and
6473 the field's type is tagged, since tagged types aren't fully laid out
6474 in this mode. Also, note that atomic implies volatile so the inner
6475 test sequences ordering is significant here. */
6476 if (needs_strict_alignment
6477 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6479 TYPE_ALIGN (gnu_record_type)
6480 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6482 if (gnu_size
6483 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6485 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6486 post_error_ne_tree
6487 ("atomic field& must be natural size of type{ (^)}",
6488 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6489 TYPE_SIZE (gnu_field_type));
6491 else if (is_volatile)
6492 post_error_ne_tree
6493 ("volatile field& must be natural size of type{ (^)}",
6494 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6495 TYPE_SIZE (gnu_field_type));
6497 else if (Is_Aliased (gnat_field))
6498 post_error_ne_tree
6499 ("size of aliased field& must be ^ bits",
6500 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6501 TYPE_SIZE (gnu_field_type));
6503 else if (Strict_Alignment (gnat_field_type))
6504 post_error_ne_tree
6505 ("size of & with aliased or tagged components not ^ bits",
6506 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6507 TYPE_SIZE (gnu_field_type));
6509 else
6510 gcc_unreachable ();
6512 gnu_size = NULL_TREE;
6515 if (!integer_zerop (size_binop
6516 (TRUNC_MOD_EXPR, gnu_pos,
6517 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6519 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6520 post_error_ne_num
6521 ("position of atomic field& must be multiple of ^ bits",
6522 First_Bit (Component_Clause (gnat_field)), gnat_field,
6523 TYPE_ALIGN (gnu_field_type));
6525 else if (is_volatile)
6526 post_error_ne_num
6527 ("position of volatile field& must be multiple of ^ bits",
6528 First_Bit (Component_Clause (gnat_field)), gnat_field,
6529 TYPE_ALIGN (gnu_field_type));
6531 else if (Is_Aliased (gnat_field))
6532 post_error_ne_num
6533 ("position of aliased field& must be multiple of ^ bits",
6534 First_Bit (Component_Clause (gnat_field)), gnat_field,
6535 TYPE_ALIGN (gnu_field_type));
6537 else if (Strict_Alignment (gnat_field_type))
6538 post_error_ne
6539 ("position of & is not compatible with alignment required "
6540 "by its components",
6541 First_Bit (Component_Clause (gnat_field)), gnat_field);
6543 else
6544 gcc_unreachable ();
6546 gnu_pos = NULL_TREE;
6551 /* If the record has rep clauses and this is the tag field, make a rep
6552 clause for it as well. */
6553 else if (Has_Specified_Layout (Scope (gnat_field))
6554 && Chars (gnat_field) == Name_uTag)
6556 gnu_pos = bitsize_zero_node;
6557 gnu_size = TYPE_SIZE (gnu_field_type);
6560 else
6562 gnu_pos = NULL_TREE;
6564 /* If we are packing the record and the field is BLKmode, round the
6565 size up to a byte boundary. */
6566 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6567 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6570 /* We need to make the size the maximum for the type if it is
6571 self-referential and an unconstrained type. In that case, we can't
6572 pack the field since we can't make a copy to align it. */
6573 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6574 && !gnu_size
6575 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6576 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6578 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6579 packed = 0;
6582 /* If a size is specified, adjust the field's type to it. */
6583 if (gnu_size)
6585 tree orig_field_type;
6587 /* If the field's type is justified modular, we would need to remove
6588 the wrapper to (better) meet the layout requirements. However we
6589 can do so only if the field is not aliased to preserve the unique
6590 layout and if the prescribed size is not greater than that of the
6591 packed array to preserve the justification. */
6592 if (!needs_strict_alignment
6593 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6594 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6595 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6596 <= 0)
6597 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6599 /* Similarly if the field's type is a misaligned integral type, but
6600 there is no restriction on the size as there is no justification. */
6601 if (!needs_strict_alignment
6602 && TYPE_IS_PADDING_P (gnu_field_type)
6603 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6604 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6606 gnu_field_type
6607 = make_type_from_size (gnu_field_type, gnu_size,
6608 Has_Biased_Representation (gnat_field));
6610 orig_field_type = gnu_field_type;
6611 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6612 false, false, definition, true);
6614 /* If a padding record was made, declare it now since it will never be
6615 declared otherwise. This is necessary to ensure that its subtrees
6616 are properly marked. */
6617 if (gnu_field_type != orig_field_type
6618 && !DECL_P (TYPE_NAME (gnu_field_type)))
6619 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6620 debug_info_p, gnat_field);
6623 /* Otherwise (or if there was an error), don't specify a position. */
6624 else
6625 gnu_pos = NULL_TREE;
6627 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6628 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6630 /* Now create the decl for the field. */
6631 gnu_field
6632 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6633 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6634 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6635 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6636 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6638 if (Ekind (gnat_field) == E_Discriminant)
6639 DECL_DISCRIMINANT_NUMBER (gnu_field)
6640 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6642 return gnu_field;
6645 /* Return true if at least one member of COMPONENT_LIST needs strict
6646 alignment. */
6648 static bool
6649 components_need_strict_alignment (Node_Id component_list)
6651 Node_Id component_decl;
6653 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6654 Present (component_decl);
6655 component_decl = Next_Non_Pragma (component_decl))
6657 Entity_Id gnat_field = Defining_Entity (component_decl);
6659 if (Is_Aliased (gnat_field))
6660 return true;
6662 if (Strict_Alignment (Etype (gnat_field)))
6663 return true;
6666 return false;
6669 /* Return true if TYPE is a type with variable size or a padding type with a
6670 field of variable size or a record that has a field with such a type. */
6672 static bool
6673 type_has_variable_size (tree type)
6675 tree field;
6677 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6678 return true;
6680 if (TYPE_IS_PADDING_P (type)
6681 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6682 return true;
6684 if (!RECORD_OR_UNION_TYPE_P (type))
6685 return false;
6687 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6688 if (type_has_variable_size (TREE_TYPE (field)))
6689 return true;
6691 return false;
6694 /* Return true if FIELD is an artificial field. */
6696 static bool
6697 field_is_artificial (tree field)
6699 /* These fields are generated by the front-end proper. */
6700 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6701 return true;
6703 /* These fields are generated by gigi. */
6704 if (DECL_INTERNAL_P (field))
6705 return true;
6707 return false;
6710 /* Return true if FIELD is a non-artificial aliased field. */
6712 static bool
6713 field_is_aliased (tree field)
6715 if (field_is_artificial (field))
6716 return false;
6718 return DECL_ALIASED_P (field);
6721 /* Return true if FIELD is a non-artificial field with self-referential
6722 size. */
6724 static bool
6725 field_has_self_size (tree field)
6727 if (field_is_artificial (field))
6728 return false;
6730 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6731 return false;
6733 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6736 /* Return true if FIELD is a non-artificial field with variable size. */
6738 static bool
6739 field_has_variable_size (tree field)
6741 if (field_is_artificial (field))
6742 return false;
6744 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6745 return false;
6747 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6750 /* qsort comparer for the bit positions of two record components. */
6752 static int
6753 compare_field_bitpos (const PTR rt1, const PTR rt2)
6755 const_tree const field1 = * (const_tree const *) rt1;
6756 const_tree const field2 = * (const_tree const *) rt2;
6757 const int ret
6758 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6760 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6763 /* Structure holding information for a given variant. */
6764 typedef struct vinfo
6766 /* The record type of the variant. */
6767 tree type;
6769 /* The name of the variant. */
6770 tree name;
6772 /* The qualifier of the variant. */
6773 tree qual;
6775 /* Whether the variant has a rep clause. */
6776 bool has_rep;
6778 /* Whether the variant is packed. */
6779 bool packed;
6781 } vinfo_t;
6783 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6784 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6785 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6786 When called from gnat_to_gnu_entity during the processing of a record type
6787 definition, the GCC node for the parent, if any, will be the single field
6788 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6789 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6790 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6792 PACKED is 1 if this is for a packed record, -1 if this is for a record
6793 with Component_Alignment of Storage_Unit, -2 if this is for a record
6794 with a specified alignment.
6796 DEFINITION is true if we are defining this record type.
6798 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6799 out the record. This means the alignment only serves to force fields to
6800 be bitfields, but not to require the record to be that aligned. This is
6801 used for variants.
6803 ALL_REP is true if a rep clause is present for all the fields.
6805 UNCHECKED_UNION is true if we are building this type for a record with a
6806 Pragma Unchecked_Union.
6808 ARTIFICIAL is true if this is a type that was generated by the compiler.
6810 DEBUG_INFO is true if we need to write debug information about the type.
6812 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6813 mean that its contents may be unused as well, only the container itself.
6815 REORDER is true if we are permitted to reorder components of this type.
6817 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6818 the outer record type down to this variant level. It is nonzero only if
6819 all the fields down to this level have a rep clause and ALL_REP is false.
6821 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6822 with a rep clause is to be added; in this case, that is all that should
6823 be done with such fields and the return value will be false. */
6825 static bool
6826 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6827 tree gnu_field_list, int packed, bool definition,
6828 bool cancel_alignment, bool all_rep,
6829 bool unchecked_union, bool artificial,
6830 bool debug_info, bool maybe_unused, bool reorder,
6831 tree first_free_pos, tree *p_gnu_rep_list)
6833 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6834 bool variants_have_rep = all_rep;
6835 bool layout_with_rep = false;
6836 bool has_self_field = false;
6837 bool has_aliased_after_self_field = false;
6838 Node_Id component_decl, variant_part;
6839 tree gnu_field, gnu_next, gnu_last;
6840 tree gnu_variant_part = NULL_TREE;
6841 tree gnu_rep_list = NULL_TREE;
6842 tree gnu_var_list = NULL_TREE;
6843 tree gnu_self_list = NULL_TREE;
6844 tree gnu_zero_list = NULL_TREE;
6846 /* For each component referenced in a component declaration create a GCC
6847 field and add it to the list, skipping pragmas in the GNAT list. */
6848 gnu_last = tree_last (gnu_field_list);
6849 if (Present (Component_Items (gnat_component_list)))
6850 for (component_decl
6851 = First_Non_Pragma (Component_Items (gnat_component_list));
6852 Present (component_decl);
6853 component_decl = Next_Non_Pragma (component_decl))
6855 Entity_Id gnat_field = Defining_Entity (component_decl);
6856 Name_Id gnat_name = Chars (gnat_field);
6858 /* If present, the _Parent field must have been created as the single
6859 field of the record type. Put it before any other fields. */
6860 if (gnat_name == Name_uParent)
6862 gnu_field = TYPE_FIELDS (gnu_record_type);
6863 gnu_field_list = chainon (gnu_field_list, gnu_field);
6865 else
6867 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6868 definition, debug_info);
6870 /* If this is the _Tag field, put it before any other fields. */
6871 if (gnat_name == Name_uTag)
6872 gnu_field_list = chainon (gnu_field_list, gnu_field);
6874 /* If this is the _Controller field, put it before the other
6875 fields except for the _Tag or _Parent field. */
6876 else if (gnat_name == Name_uController && gnu_last)
6878 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6879 DECL_CHAIN (gnu_last) = gnu_field;
6882 /* If this is a regular field, put it after the other fields. */
6883 else
6885 DECL_CHAIN (gnu_field) = gnu_field_list;
6886 gnu_field_list = gnu_field;
6887 if (!gnu_last)
6888 gnu_last = gnu_field;
6890 /* And record information for the final layout. */
6891 if (field_has_self_size (gnu_field))
6892 has_self_field = true;
6893 else if (has_self_field && field_is_aliased (gnu_field))
6894 has_aliased_after_self_field = true;
6898 save_gnu_tree (gnat_field, gnu_field, false);
6901 /* At the end of the component list there may be a variant part. */
6902 variant_part = Variant_Part (gnat_component_list);
6904 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6905 mutually exclusive and should go in the same memory. To do this we need
6906 to treat each variant as a record whose elements are created from the
6907 component list for the variant. So here we create the records from the
6908 lists for the variants and put them all into the QUAL_UNION_TYPE.
6909 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6910 use GNU_RECORD_TYPE if there are no fields so far. */
6911 if (Present (variant_part))
6913 Node_Id gnat_discr = Name (variant_part), variant;
6914 tree gnu_discr = gnat_to_gnu (gnat_discr);
6915 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
6916 tree gnu_var_name
6917 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6918 "XVN");
6919 tree gnu_union_type, gnu_union_name;
6920 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6921 bool union_field_needs_strict_alignment = false;
6922 auto_vec <vinfo_t, 16> variant_types;
6923 vinfo_t *gnu_variant;
6924 unsigned int variants_align = 0;
6925 unsigned int i;
6927 gnu_union_name
6928 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6930 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6931 are all in the variant part, to match the layout of C unions. There
6932 is an associated check below. */
6933 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6934 gnu_union_type = gnu_record_type;
6935 else
6937 gnu_union_type
6938 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6940 TYPE_NAME (gnu_union_type) = gnu_union_name;
6941 TYPE_ALIGN (gnu_union_type) = 0;
6942 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6945 /* If all the fields down to this level have a rep clause, find out
6946 whether all the fields at this level also have one. If so, then
6947 compute the new first free position to be passed downward. */
6948 this_first_free_pos = first_free_pos;
6949 if (this_first_free_pos)
6951 for (gnu_field = gnu_field_list;
6952 gnu_field;
6953 gnu_field = DECL_CHAIN (gnu_field))
6954 if (DECL_FIELD_OFFSET (gnu_field))
6956 tree pos = bit_position (gnu_field);
6957 if (!tree_int_cst_lt (pos, this_first_free_pos))
6958 this_first_free_pos
6959 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
6961 else
6963 this_first_free_pos = NULL_TREE;
6964 break;
6968 /* We build the variants in two passes. The bulk of the work is done in
6969 the first pass, that is to say translating the GNAT nodes, building
6970 the container types and computing the associated properties. However
6971 we cannot finish up the container types during this pass because we
6972 don't know where the variant part will be placed until the end. */
6973 for (variant = First_Non_Pragma (Variants (variant_part));
6974 Present (variant);
6975 variant = Next_Non_Pragma (variant))
6977 tree gnu_variant_type = make_node (RECORD_TYPE);
6978 tree gnu_inner_name, gnu_qual;
6979 bool has_rep;
6980 int field_packed;
6981 vinfo_t vinfo;
6983 Get_Variant_Encoding (variant);
6984 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
6985 TYPE_NAME (gnu_variant_type)
6986 = concat_name (gnu_union_name,
6987 IDENTIFIER_POINTER (gnu_inner_name));
6989 /* Set the alignment of the inner type in case we need to make
6990 inner objects into bitfields, but then clear it out so the
6991 record actually gets only the alignment required. */
6992 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6993 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6995 /* Similarly, if the outer record has a size specified and all
6996 the fields have a rep clause, we can propagate the size. */
6997 if (all_rep_and_size)
6999 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7000 TYPE_SIZE_UNIT (gnu_variant_type)
7001 = TYPE_SIZE_UNIT (gnu_record_type);
7004 /* Add the fields into the record type for the variant. Note that
7005 we aren't sure to really use it at this point, see below. */
7006 has_rep
7007 = components_to_record (gnu_variant_type, Component_List (variant),
7008 NULL_TREE, packed, definition,
7009 !all_rep_and_size, all_rep,
7010 unchecked_union,
7011 true, debug_info, true, reorder,
7012 this_first_free_pos,
7013 all_rep || this_first_free_pos
7014 ? NULL : &gnu_rep_list);
7016 /* Translate the qualifier and annotate the GNAT node. */
7017 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7018 Set_Present_Expr (variant, annotate_value (gnu_qual));
7020 /* Deal with packedness like in gnat_to_gnu_field. */
7021 if (components_need_strict_alignment (Component_List (variant)))
7023 field_packed = 0;
7024 union_field_needs_strict_alignment = true;
7026 else
7027 field_packed
7028 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7030 /* Push this variant onto the stack for the second pass. */
7031 vinfo.type = gnu_variant_type;
7032 vinfo.name = gnu_inner_name;
7033 vinfo.qual = gnu_qual;
7034 vinfo.has_rep = has_rep;
7035 vinfo.packed = field_packed;
7036 variant_types.safe_push (vinfo);
7038 /* Compute the global properties that will determine the placement of
7039 the variant part. */
7040 variants_have_rep |= has_rep;
7041 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7042 variants_align = TYPE_ALIGN (gnu_variant_type);
7045 /* Round up the first free position to the alignment of the variant part
7046 for the variants without rep clause. This will guarantee a consistent
7047 layout independently of the placement of the variant part. */
7048 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7049 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7051 /* In the second pass, the container types are adjusted if necessary and
7052 finished up, then the corresponding fields of the variant part are
7053 built with their qualifier, unless this is an unchecked union. */
7054 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7056 tree gnu_variant_type = gnu_variant->type;
7057 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7059 /* If this is an Unchecked_Union whose fields are all in the variant
7060 part and we have a single field with no representation clause or
7061 placed at offset zero, use the field directly to match the layout
7062 of C unions. */
7063 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7064 && gnu_field_list
7065 && !DECL_CHAIN (gnu_field_list)
7066 && (!DECL_FIELD_OFFSET (gnu_field_list)
7067 || integer_zerop (bit_position (gnu_field_list))))
7069 gnu_field = gnu_field_list;
7070 DECL_CONTEXT (gnu_field) = gnu_record_type;
7072 else
7074 /* Finalize the variant type now. We used to throw away empty
7075 record types but we no longer do that because we need them to
7076 generate complete debug info for the variant; otherwise, the
7077 union type definition will be lacking the fields associated
7078 with these empty variants. */
7079 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7081 /* The variant part will be at offset 0 so we need to ensure
7082 that the fields are laid out starting from the first free
7083 position at this level. */
7084 tree gnu_rep_type = make_node (RECORD_TYPE);
7085 tree gnu_rep_part;
7086 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7087 gnu_rep_part
7088 = create_rep_part (gnu_rep_type, gnu_variant_type,
7089 this_first_free_pos);
7090 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7091 gnu_field_list = gnu_rep_part;
7092 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7093 false);
7096 if (debug_info)
7097 rest_of_record_type_compilation (gnu_variant_type);
7098 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7099 true, debug_info, gnat_component_list);
7101 gnu_field
7102 = create_field_decl (gnu_variant->name, gnu_variant_type,
7103 gnu_union_type,
7104 all_rep_and_size
7105 ? TYPE_SIZE (gnu_variant_type) : 0,
7106 variants_have_rep ? bitsize_zero_node : 0,
7107 gnu_variant->packed, 0);
7109 DECL_INTERNAL_P (gnu_field) = 1;
7111 if (!unchecked_union)
7112 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7115 DECL_CHAIN (gnu_field) = gnu_variant_list;
7116 gnu_variant_list = gnu_field;
7119 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7120 if (gnu_variant_list)
7122 int union_field_packed;
7124 if (all_rep_and_size)
7126 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7127 TYPE_SIZE_UNIT (gnu_union_type)
7128 = TYPE_SIZE_UNIT (gnu_record_type);
7131 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7132 all_rep_and_size ? 1 : 0, debug_info);
7134 /* If GNU_UNION_TYPE is our record type, it means we must have an
7135 Unchecked_Union with no fields. Verify that and, if so, just
7136 return. */
7137 if (gnu_union_type == gnu_record_type)
7139 gcc_assert (unchecked_union
7140 && !gnu_field_list
7141 && !gnu_rep_list);
7142 return variants_have_rep;
7145 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7146 debug_info, gnat_component_list);
7148 /* Deal with packedness like in gnat_to_gnu_field. */
7149 if (union_field_needs_strict_alignment)
7150 union_field_packed = 0;
7151 else
7152 union_field_packed
7153 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7155 gnu_variant_part
7156 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7157 all_rep_and_size
7158 ? TYPE_SIZE (gnu_union_type) : 0,
7159 variants_have_rep ? bitsize_zero_node : 0,
7160 union_field_packed, 0);
7162 DECL_INTERNAL_P (gnu_variant_part) = 1;
7166 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7167 permitted to reorder components, self-referential sizes or variable sizes.
7168 If they do, pull them out and put them onto the appropriate list. We have
7169 to do this in a separate pass since we want to handle the discriminants
7170 but can't play with them until we've used them in debugging data above.
7172 Similarly, pull out the fields with zero size and no rep clause, as they
7173 would otherwise modify the layout and thus very likely run afoul of the
7174 Ada semantics, which are different from those of C here.
7176 ??? If we reorder them, debugging information will be wrong but there is
7177 nothing that can be done about this at the moment. */
7178 gnu_last = NULL_TREE;
7180 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7181 do { \
7182 if (gnu_last) \
7183 DECL_CHAIN (gnu_last) = gnu_next; \
7184 else \
7185 gnu_field_list = gnu_next; \
7187 DECL_CHAIN (gnu_field) = (LIST); \
7188 (LIST) = gnu_field; \
7189 } while (0)
7191 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7193 gnu_next = DECL_CHAIN (gnu_field);
7195 if (DECL_FIELD_OFFSET (gnu_field))
7197 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7198 continue;
7201 if ((reorder || has_aliased_after_self_field)
7202 && field_has_self_size (gnu_field))
7204 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7205 continue;
7208 if (reorder && field_has_variable_size (gnu_field))
7210 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7211 continue;
7214 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7216 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7217 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7218 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7219 if (field_is_aliased (gnu_field))
7220 TYPE_ALIGN (gnu_record_type)
7221 = MAX (TYPE_ALIGN (gnu_record_type),
7222 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7223 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7224 continue;
7227 gnu_last = gnu_field;
7230 #undef MOVE_FROM_FIELD_LIST_TO
7232 gnu_field_list = nreverse (gnu_field_list);
7234 /* If permitted, we reorder the fields as follows:
7236 1) all fixed length fields,
7237 2) all fields whose length doesn't depend on discriminants,
7238 3) all fields whose length depends on discriminants,
7239 4) the variant part,
7241 within the record and within each variant recursively. */
7242 if (reorder)
7243 gnu_field_list
7244 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7246 /* Otherwise, if there is an aliased field placed after a field whose length
7247 depends on discriminants, we put all the fields of the latter sort, last.
7248 We need to do this in case an object of this record type is mutable. */
7249 else if (has_aliased_after_self_field)
7250 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7252 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7253 in our REP list to the previous level because this level needs them in
7254 order to do a correct layout, i.e. avoid having overlapping fields. */
7255 if (p_gnu_rep_list && gnu_rep_list)
7256 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7258 /* Otherwise, sort the fields by bit position and put them into their own
7259 record, before the others, if we also have fields without rep clause. */
7260 else if (gnu_rep_list)
7262 tree gnu_rep_type, gnu_rep_part;
7263 int i, len = list_length (gnu_rep_list);
7264 tree *gnu_arr = XALLOCAVEC (tree, len);
7266 /* If all the fields have a rep clause, we can do a flat layout. */
7267 layout_with_rep = !gnu_field_list
7268 && (!gnu_variant_part || variants_have_rep);
7269 gnu_rep_type
7270 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7272 for (gnu_field = gnu_rep_list, i = 0;
7273 gnu_field;
7274 gnu_field = DECL_CHAIN (gnu_field), i++)
7275 gnu_arr[i] = gnu_field;
7277 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7279 /* Put the fields in the list in order of increasing position, which
7280 means we start from the end. */
7281 gnu_rep_list = NULL_TREE;
7282 for (i = len - 1; i >= 0; i--)
7284 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7285 gnu_rep_list = gnu_arr[i];
7286 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7289 if (layout_with_rep)
7290 gnu_field_list = gnu_rep_list;
7291 else
7293 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7295 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7296 without rep clause are laid out starting from this position.
7297 Therefore, we force it as a minimal size on the REP part. */
7298 gnu_rep_part
7299 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7301 /* Chain the REP part at the beginning of the field list. */
7302 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7303 gnu_field_list = gnu_rep_part;
7307 /* Chain the variant part at the end of the field list. */
7308 if (gnu_variant_part)
7309 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7311 if (cancel_alignment)
7312 TYPE_ALIGN (gnu_record_type) = 0;
7314 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7316 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7317 debug_info && !maybe_unused);
7319 /* Chain the fields with zero size at the beginning of the field list. */
7320 if (gnu_zero_list)
7321 TYPE_FIELDS (gnu_record_type)
7322 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7324 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7327 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7328 placed into an Esize, Component_Bit_Offset, or Component_Size value
7329 in the GNAT tree. */
7331 static Uint
7332 annotate_value (tree gnu_size)
7334 TCode tcode;
7335 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7336 struct tree_int_map in;
7337 int i;
7339 /* See if we've already saved the value for this node. */
7340 if (EXPR_P (gnu_size))
7342 struct tree_int_map *e;
7344 in.base.from = gnu_size;
7345 e = (struct tree_int_map *) htab_find (annotate_value_cache, &in);
7347 if (e)
7348 return (Node_Ref_Or_Val) e->to;
7350 else
7351 in.base.from = NULL_TREE;
7353 /* If we do not return inside this switch, TCODE will be set to the
7354 code to use for a Create_Node operand and LEN (set above) will be
7355 the number of recursive calls for us to make. */
7357 switch (TREE_CODE (gnu_size))
7359 case INTEGER_CST:
7360 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7362 case COMPONENT_REF:
7363 /* The only case we handle here is a simple discriminant reference. */
7364 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7366 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7368 /* Climb up the chain of successive extensions, if any. */
7369 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7370 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7371 == parent_name_id)
7372 gnu_size = TREE_OPERAND (gnu_size, 0);
7374 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7375 return
7376 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7379 return No_Uint;
7381 CASE_CONVERT: case NON_LVALUE_EXPR:
7382 return annotate_value (TREE_OPERAND (gnu_size, 0));
7384 /* Now just list the operations we handle. */
7385 case COND_EXPR: tcode = Cond_Expr; break;
7386 case PLUS_EXPR: tcode = Plus_Expr; break;
7387 case MINUS_EXPR: tcode = Minus_Expr; break;
7388 case MULT_EXPR: tcode = Mult_Expr; break;
7389 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7390 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7391 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7392 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7393 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7394 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7395 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7396 case NEGATE_EXPR: tcode = Negate_Expr; break;
7397 case MIN_EXPR: tcode = Min_Expr; break;
7398 case MAX_EXPR: tcode = Max_Expr; break;
7399 case ABS_EXPR: tcode = Abs_Expr; break;
7400 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7401 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7402 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7403 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7404 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7405 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7406 case LT_EXPR: tcode = Lt_Expr; break;
7407 case LE_EXPR: tcode = Le_Expr; break;
7408 case GT_EXPR: tcode = Gt_Expr; break;
7409 case GE_EXPR: tcode = Ge_Expr; break;
7410 case EQ_EXPR: tcode = Eq_Expr; break;
7411 case NE_EXPR: tcode = Ne_Expr; break;
7413 case BIT_AND_EXPR:
7414 tcode = Bit_And_Expr;
7415 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7416 Such values appear in expressions with aligning patterns. Note that,
7417 since sizetype is unsigned, we have to jump through some hoops. */
7418 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7420 tree op1 = TREE_OPERAND (gnu_size, 1);
7421 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7422 if (wi::neg_p (signed_op1))
7424 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7425 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7428 break;
7430 case CALL_EXPR:
7431 /* In regular mode, inline back only if symbolic annotation is requested
7432 in order to avoid memory explosion on big discriminated record types.
7433 But not in ASIS mode, as symbolic annotation is required for DDA. */
7434 if (List_Representation_Info == 3 || type_annotate_only)
7436 tree t = maybe_inline_call_in_expr (gnu_size);
7437 if (t)
7438 return annotate_value (t);
7440 else
7441 return Uint_Minus_1;
7443 /* Fall through... */
7445 default:
7446 return No_Uint;
7449 /* Now get each of the operands that's relevant for this code. If any
7450 cannot be expressed as a repinfo node, say we can't. */
7451 for (i = 0; i < 3; i++)
7452 ops[i] = No_Uint;
7454 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7456 if (i == 1 && pre_op1 != No_Uint)
7457 ops[i] = pre_op1;
7458 else
7459 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7460 if (ops[i] == No_Uint)
7461 return No_Uint;
7464 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7466 /* Save the result in the cache. */
7467 if (in.base.from)
7469 struct tree_int_map **h;
7470 /* We can't assume the hash table data hasn't moved since the initial
7471 look up, so we have to search again. Allocating and inserting an
7472 entry at that point would be an alternative, but then we'd better
7473 discard the entry if we decided not to cache it. */
7474 h = (struct tree_int_map **)
7475 htab_find_slot (annotate_value_cache, &in, INSERT);
7476 gcc_assert (!*h);
7477 *h = ggc_alloc<tree_int_map> ();
7478 (*h)->base.from = gnu_size;
7479 (*h)->to = ret;
7482 return ret;
7485 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7486 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7487 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7488 BY_REF is true if the object is used by reference. */
7490 void
7491 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7493 if (by_ref)
7495 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7496 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7497 else
7498 gnu_type = TREE_TYPE (gnu_type);
7501 if (Unknown_Esize (gnat_entity))
7503 if (TREE_CODE (gnu_type) == RECORD_TYPE
7504 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7505 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7506 else if (!size)
7507 size = TYPE_SIZE (gnu_type);
7509 if (size)
7510 Set_Esize (gnat_entity, annotate_value (size));
7513 if (Unknown_Alignment (gnat_entity))
7514 Set_Alignment (gnat_entity,
7515 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7518 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7519 Return NULL_TREE if there is no such element in the list. */
7521 static tree
7522 purpose_member_field (const_tree elem, tree list)
7524 while (list)
7526 tree field = TREE_PURPOSE (list);
7527 if (SAME_FIELD_P (field, elem))
7528 return list;
7529 list = TREE_CHAIN (list);
7531 return NULL_TREE;
7534 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7535 set Component_Bit_Offset and Esize of the components to the position and
7536 size used by Gigi. */
7538 static void
7539 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7541 Entity_Id gnat_field;
7542 tree gnu_list;
7544 /* We operate by first making a list of all fields and their position (we
7545 can get the size easily) and then update all the sizes in the tree. */
7546 gnu_list
7547 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7548 BIGGEST_ALIGNMENT, NULL_TREE);
7550 for (gnat_field = First_Entity (gnat_entity);
7551 Present (gnat_field);
7552 gnat_field = Next_Entity (gnat_field))
7553 if (Ekind (gnat_field) == E_Component
7554 || (Ekind (gnat_field) == E_Discriminant
7555 && !Is_Unchecked_Union (Scope (gnat_field))))
7557 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7558 gnu_list);
7559 if (t)
7561 tree parent_offset;
7563 /* If we are just annotating types and the type is tagged, the tag
7564 and the parent components are not generated by the front-end so
7565 we need to add the appropriate offset to each component without
7566 representation clause. */
7567 if (type_annotate_only
7568 && Is_Tagged_Type (gnat_entity)
7569 && No (Component_Clause (gnat_field)))
7571 /* For a component appearing in the current extension, the
7572 offset is the size of the parent. */
7573 if (Is_Derived_Type (gnat_entity)
7574 && Original_Record_Component (gnat_field) == gnat_field)
7575 parent_offset
7576 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7577 bitsizetype);
7578 else
7579 parent_offset = bitsize_int (POINTER_SIZE);
7581 if (TYPE_FIELDS (gnu_type))
7582 parent_offset
7583 = round_up (parent_offset,
7584 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7586 else
7587 parent_offset = bitsize_zero_node;
7589 Set_Component_Bit_Offset
7590 (gnat_field,
7591 annotate_value
7592 (size_binop (PLUS_EXPR,
7593 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7594 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7595 parent_offset)));
7597 Set_Esize (gnat_field,
7598 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7600 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7602 /* If there is no entry, this is an inherited component whose
7603 position is the same as in the parent type. */
7604 Set_Component_Bit_Offset
7605 (gnat_field,
7606 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7608 Set_Esize (gnat_field,
7609 Esize (Original_Record_Component (gnat_field)));
7614 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7615 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7616 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7617 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7618 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7619 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7620 pre-existing list to be chained to the newly created entries. */
7622 static tree
7623 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7624 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7626 tree gnu_field;
7628 for (gnu_field = TYPE_FIELDS (gnu_type);
7629 gnu_field;
7630 gnu_field = DECL_CHAIN (gnu_field))
7632 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7633 DECL_FIELD_BIT_OFFSET (gnu_field));
7634 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7635 DECL_FIELD_OFFSET (gnu_field));
7636 unsigned int our_offset_align
7637 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7638 tree v = make_tree_vec (3);
7640 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7641 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7642 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7643 gnu_list = tree_cons (gnu_field, v, gnu_list);
7645 /* Recurse on internal fields, flattening the nested fields except for
7646 those in the variant part, if requested. */
7647 if (DECL_INTERNAL_P (gnu_field))
7649 tree gnu_field_type = TREE_TYPE (gnu_field);
7650 if (do_not_flatten_variant
7651 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7652 gnu_list
7653 = build_position_list (gnu_field_type, do_not_flatten_variant,
7654 size_zero_node, bitsize_zero_node,
7655 BIGGEST_ALIGNMENT, gnu_list);
7656 else
7657 gnu_list
7658 = build_position_list (gnu_field_type, do_not_flatten_variant,
7659 gnu_our_offset, gnu_our_bitpos,
7660 our_offset_align, gnu_list);
7664 return gnu_list;
7667 /* Return a list describing the substitutions needed to reflect the
7668 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7669 be in any order. The values in an element of the list are in the form
7670 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7671 a definition of GNAT_SUBTYPE. */
7673 static vec<subst_pair>
7674 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7676 vec<subst_pair> gnu_list = vNULL;
7677 Entity_Id gnat_discrim;
7678 Node_Id gnat_constr;
7680 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7681 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7682 Present (gnat_discrim);
7683 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7684 gnat_constr = Next_Elmt (gnat_constr))
7685 /* Ignore access discriminants. */
7686 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7688 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7689 tree replacement = convert (TREE_TYPE (gnu_field),
7690 elaborate_expression
7691 (Node (gnat_constr), gnat_subtype,
7692 get_entity_name (gnat_discrim),
7693 definition, true, false));
7694 subst_pair s = {gnu_field, replacement};
7695 gnu_list.safe_push (s);
7698 return gnu_list;
7701 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7702 variants of QUAL_UNION_TYPE that are still relevant after applying
7703 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7704 list to be prepended to the newly created entries. */
7706 static vec<variant_desc>
7707 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7708 vec<variant_desc> gnu_list)
7710 tree gnu_field;
7712 for (gnu_field = TYPE_FIELDS (qual_union_type);
7713 gnu_field;
7714 gnu_field = DECL_CHAIN (gnu_field))
7716 tree qual = DECL_QUALIFIER (gnu_field);
7717 unsigned int i;
7718 subst_pair *s;
7720 FOR_EACH_VEC_ELT (subst_list, i, s)
7721 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7723 /* If the new qualifier is not unconditionally false, its variant may
7724 still be accessed. */
7725 if (!integer_zerop (qual))
7727 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7728 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7730 gnu_list.safe_push (v);
7732 /* Recurse on the variant subpart of the variant, if any. */
7733 variant_subpart = get_variant_part (variant_type);
7734 if (variant_subpart)
7735 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7736 subst_list, gnu_list);
7738 /* If the new qualifier is unconditionally true, the subsequent
7739 variants cannot be accessed. */
7740 if (integer_onep (qual))
7741 break;
7745 return gnu_list;
7748 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7749 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7750 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7751 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7752 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7753 true if we are being called to process the Component_Size of GNAT_OBJECT;
7754 this is used only for error messages. ZERO_OK is true if a size of zero
7755 is permitted; if ZERO_OK is false, it means that a size of zero should be
7756 treated as an unspecified size. */
7758 static tree
7759 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7760 enum tree_code kind, bool component_p, bool zero_ok)
7762 Node_Id gnat_error_node;
7763 tree type_size, size;
7765 /* Return 0 if no size was specified. */
7766 if (uint_size == No_Uint)
7767 return NULL_TREE;
7769 /* Ignore a negative size since that corresponds to our back-annotation. */
7770 if (UI_Lt (uint_size, Uint_0))
7771 return NULL_TREE;
7773 /* Find the node to use for error messages. */
7774 if ((Ekind (gnat_object) == E_Component
7775 || Ekind (gnat_object) == E_Discriminant)
7776 && Present (Component_Clause (gnat_object)))
7777 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7778 else if (Present (Size_Clause (gnat_object)))
7779 gnat_error_node = Expression (Size_Clause (gnat_object));
7780 else
7781 gnat_error_node = gnat_object;
7783 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7784 but cannot be represented in bitsizetype. */
7785 size = UI_To_gnu (uint_size, bitsizetype);
7786 if (TREE_OVERFLOW (size))
7788 if (component_p)
7789 post_error_ne ("component size for& is too large", gnat_error_node,
7790 gnat_object);
7791 else
7792 post_error_ne ("size for& is too large", gnat_error_node,
7793 gnat_object);
7794 return NULL_TREE;
7797 /* Ignore a zero size if it is not permitted. */
7798 if (!zero_ok && integer_zerop (size))
7799 return NULL_TREE;
7801 /* The size of objects is always a multiple of a byte. */
7802 if (kind == VAR_DECL
7803 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7805 if (component_p)
7806 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7807 gnat_error_node, gnat_object);
7808 else
7809 post_error_ne ("size for& is not a multiple of Storage_Unit",
7810 gnat_error_node, gnat_object);
7811 return NULL_TREE;
7814 /* If this is an integral type or a packed array type, the front-end has
7815 already verified the size, so we need not do it here (which would mean
7816 checking against the bounds). However, if this is an aliased object,
7817 it may not be smaller than the type of the object. */
7818 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7819 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7820 return size;
7822 /* If the object is a record that contains a template, add the size of the
7823 template to the specified size. */
7824 if (TREE_CODE (gnu_type) == RECORD_TYPE
7825 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7826 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7828 if (kind == VAR_DECL
7829 /* If a type needs strict alignment, a component of this type in
7830 a packed record cannot be packed and thus uses the type size. */
7831 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7832 type_size = TYPE_SIZE (gnu_type);
7833 else
7834 type_size = rm_size (gnu_type);
7836 /* Modify the size of a discriminated type to be the maximum size. */
7837 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7838 type_size = max_size (type_size, true);
7840 /* If this is an access type or a fat pointer, the minimum size is that given
7841 by the smallest integral mode that's valid for pointers. */
7842 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7844 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7845 while (!targetm.valid_pointer_mode (p_mode))
7846 p_mode = GET_MODE_WIDER_MODE (p_mode);
7847 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7850 /* Issue an error either if the default size of the object isn't a constant
7851 or if the new size is smaller than it. */
7852 if (TREE_CODE (type_size) != INTEGER_CST
7853 || TREE_OVERFLOW (type_size)
7854 || tree_int_cst_lt (size, type_size))
7856 if (component_p)
7857 post_error_ne_tree
7858 ("component size for& too small{, minimum allowed is ^}",
7859 gnat_error_node, gnat_object, type_size);
7860 else
7861 post_error_ne_tree
7862 ("size for& too small{, minimum allowed is ^}",
7863 gnat_error_node, gnat_object, type_size);
7864 return NULL_TREE;
7867 return size;
7870 /* Similarly, but both validate and process a value of RM size. This routine
7871 is only called for types. */
7873 static void
7874 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7876 Node_Id gnat_attr_node;
7877 tree old_size, size;
7879 /* Do nothing if no size was specified. */
7880 if (uint_size == No_Uint)
7881 return;
7883 /* Ignore a negative size since that corresponds to our back-annotation. */
7884 if (UI_Lt (uint_size, Uint_0))
7885 return;
7887 /* Only issue an error if a Value_Size clause was explicitly given.
7888 Otherwise, we'd be duplicating an error on the Size clause. */
7889 gnat_attr_node
7890 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7892 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7893 but cannot be represented in bitsizetype. */
7894 size = UI_To_gnu (uint_size, bitsizetype);
7895 if (TREE_OVERFLOW (size))
7897 if (Present (gnat_attr_node))
7898 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7899 gnat_entity);
7900 return;
7903 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7904 exists, or this is an integer type, in which case the front-end will
7905 have always set it. */
7906 if (No (gnat_attr_node)
7907 && integer_zerop (size)
7908 && !Has_Size_Clause (gnat_entity)
7909 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7910 return;
7912 old_size = rm_size (gnu_type);
7914 /* If the old size is self-referential, get the maximum size. */
7915 if (CONTAINS_PLACEHOLDER_P (old_size))
7916 old_size = max_size (old_size, true);
7918 /* Issue an error either if the old size of the object isn't a constant or
7919 if the new size is smaller than it. The front-end has already verified
7920 this for scalar and packed array types. */
7921 if (TREE_CODE (old_size) != INTEGER_CST
7922 || TREE_OVERFLOW (old_size)
7923 || (AGGREGATE_TYPE_P (gnu_type)
7924 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7925 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7926 && !(TYPE_IS_PADDING_P (gnu_type)
7927 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7928 && TYPE_PACKED_ARRAY_TYPE_P
7929 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7930 && tree_int_cst_lt (size, old_size)))
7932 if (Present (gnat_attr_node))
7933 post_error_ne_tree
7934 ("Value_Size for& too small{, minimum allowed is ^}",
7935 gnat_attr_node, gnat_entity, old_size);
7936 return;
7939 /* Otherwise, set the RM size proper for integral types... */
7940 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7941 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7942 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7943 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7944 SET_TYPE_RM_SIZE (gnu_type, size);
7946 /* ...or the Ada size for record and union types. */
7947 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
7948 && !TYPE_FAT_POINTER_P (gnu_type))
7949 SET_TYPE_ADA_SIZE (gnu_type, size);
7952 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7953 a type or object whose present alignment is ALIGN. If this alignment is
7954 valid, return it. Otherwise, give an error and return ALIGN. */
7956 static unsigned int
7957 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7959 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7960 unsigned int new_align;
7961 Node_Id gnat_error_node;
7963 /* Don't worry about checking alignment if alignment was not specified
7964 by the source program and we already posted an error for this entity. */
7965 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7966 return align;
7968 /* Post the error on the alignment clause if any. Note, for the implicit
7969 base type of an array type, the alignment clause is on the first
7970 subtype. */
7971 if (Present (Alignment_Clause (gnat_entity)))
7972 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7974 else if (Is_Itype (gnat_entity)
7975 && Is_Array_Type (gnat_entity)
7976 && Etype (gnat_entity) == gnat_entity
7977 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7978 gnat_error_node =
7979 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
7981 else
7982 gnat_error_node = gnat_entity;
7984 /* Within GCC, an alignment is an integer, so we must make sure a value is
7985 specified that fits in that range. Also, there is an upper bound to
7986 alignments we can support/allow. */
7987 if (!UI_Is_In_Int_Range (alignment)
7988 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7989 post_error_ne_num ("largest supported alignment for& is ^",
7990 gnat_error_node, gnat_entity, max_allowed_alignment);
7991 else if (!(Present (Alignment_Clause (gnat_entity))
7992 && From_At_Mod (Alignment_Clause (gnat_entity)))
7993 && new_align * BITS_PER_UNIT < align)
7995 unsigned int double_align;
7996 bool is_capped_double, align_clause;
7998 /* If the default alignment of "double" or larger scalar types is
7999 specifically capped and the new alignment is above the cap, do
8000 not post an error and change the alignment only if there is an
8001 alignment clause; this makes it possible to have the associated
8002 GCC type overaligned by default for performance reasons. */
8003 if ((double_align = double_float_alignment) > 0)
8005 Entity_Id gnat_type
8006 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8007 is_capped_double
8008 = is_double_float_or_array (gnat_type, &align_clause);
8010 else if ((double_align = double_scalar_alignment) > 0)
8012 Entity_Id gnat_type
8013 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8014 is_capped_double
8015 = is_double_scalar_or_array (gnat_type, &align_clause);
8017 else
8018 is_capped_double = align_clause = false;
8020 if (is_capped_double && new_align >= double_align)
8022 if (align_clause)
8023 align = new_align * BITS_PER_UNIT;
8025 else
8027 if (is_capped_double)
8028 align = double_align * BITS_PER_UNIT;
8030 post_error_ne_num ("alignment for& must be at least ^",
8031 gnat_error_node, gnat_entity,
8032 align / BITS_PER_UNIT);
8035 else
8037 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8038 if (new_align > align)
8039 align = new_align;
8042 return align;
8045 /* Verify that OBJECT, a type or decl, is something we can implement
8046 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8047 if we require atomic components. */
8049 static void
8050 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8052 Node_Id gnat_error_point = gnat_entity;
8053 Node_Id gnat_node;
8054 machine_mode mode;
8055 unsigned int align;
8056 tree size;
8058 /* There are three case of what OBJECT can be. It can be a type, in which
8059 case we take the size, alignment and mode from the type. It can be a
8060 declaration that was indirect, in which case the relevant values are
8061 that of the type being pointed to, or it can be a normal declaration,
8062 in which case the values are of the decl. The code below assumes that
8063 OBJECT is either a type or a decl. */
8064 if (TYPE_P (object))
8066 /* If this is an anonymous base type, nothing to check. Error will be
8067 reported on the source type. */
8068 if (!Comes_From_Source (gnat_entity))
8069 return;
8071 mode = TYPE_MODE (object);
8072 align = TYPE_ALIGN (object);
8073 size = TYPE_SIZE (object);
8075 else if (DECL_BY_REF_P (object))
8077 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8078 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8079 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8081 else
8083 mode = DECL_MODE (object);
8084 align = DECL_ALIGN (object);
8085 size = DECL_SIZE (object);
8088 /* Consider all floating-point types atomic and any types that that are
8089 represented by integers no wider than a machine word. */
8090 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8091 || ((GET_MODE_CLASS (mode) == MODE_INT
8092 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8093 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8094 return;
8096 /* For the moment, also allow anything that has an alignment equal
8097 to its size and which is smaller than a word. */
8098 if (size && TREE_CODE (size) == INTEGER_CST
8099 && compare_tree_int (size, align) == 0
8100 && align <= BITS_PER_WORD)
8101 return;
8103 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8104 gnat_node = Next_Rep_Item (gnat_node))
8106 if (!comp_p && Nkind (gnat_node) == N_Pragma
8107 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8108 == Pragma_Atomic))
8109 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8110 else if (comp_p && Nkind (gnat_node) == N_Pragma
8111 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8112 == Pragma_Atomic_Components))
8113 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8116 if (comp_p)
8117 post_error_ne ("atomic access to component of & cannot be guaranteed",
8118 gnat_error_point, gnat_entity);
8119 else
8120 post_error_ne ("atomic access to & cannot be guaranteed",
8121 gnat_error_point, gnat_entity);
8125 /* Helper for the intrin compatibility checks family. Evaluate whether
8126 two types are definitely incompatible. */
8128 static bool
8129 intrin_types_incompatible_p (tree t1, tree t2)
8131 enum tree_code code;
8133 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8134 return false;
8136 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8137 return true;
8139 if (TREE_CODE (t1) != TREE_CODE (t2))
8140 return true;
8142 code = TREE_CODE (t1);
8144 switch (code)
8146 case INTEGER_TYPE:
8147 case REAL_TYPE:
8148 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8150 case POINTER_TYPE:
8151 case REFERENCE_TYPE:
8152 /* Assume designated types are ok. We'd need to account for char * and
8153 void * variants to do better, which could rapidly get messy and isn't
8154 clearly worth the effort. */
8155 return false;
8157 default:
8158 break;
8161 return false;
8164 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8165 on the Ada/builtin argument lists for the INB binding. */
8167 static bool
8168 intrin_arglists_compatible_p (intrin_binding_t * inb)
8170 function_args_iterator ada_iter, btin_iter;
8172 function_args_iter_init (&ada_iter, inb->ada_fntype);
8173 function_args_iter_init (&btin_iter, inb->btin_fntype);
8175 /* Sequence position of the last argument we checked. */
8176 int argpos = 0;
8178 while (1)
8180 tree ada_type = function_args_iter_cond (&ada_iter);
8181 tree btin_type = function_args_iter_cond (&btin_iter);
8183 /* If we've exhausted both lists simultaneously, we're done. */
8184 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8185 break;
8187 /* If one list is shorter than the other, they fail to match. */
8188 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8189 return false;
8191 /* If we're done with the Ada args and not with the internal builtin
8192 args, or the other way around, complain. */
8193 if (ada_type == void_type_node
8194 && btin_type != void_type_node)
8196 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8197 return false;
8200 if (btin_type == void_type_node
8201 && ada_type != void_type_node)
8203 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8204 inb->gnat_entity, inb->gnat_entity, argpos);
8205 return false;
8208 /* Otherwise, check that types match for the current argument. */
8209 argpos ++;
8210 if (intrin_types_incompatible_p (ada_type, btin_type))
8212 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8213 inb->gnat_entity, inb->gnat_entity, argpos);
8214 return false;
8218 function_args_iter_next (&ada_iter);
8219 function_args_iter_next (&btin_iter);
8222 return true;
8225 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8226 on the Ada/builtin return values for the INB binding. */
8228 static bool
8229 intrin_return_compatible_p (intrin_binding_t * inb)
8231 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8232 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8234 /* Accept function imported as procedure, common and convenient. */
8235 if (VOID_TYPE_P (ada_return_type)
8236 && !VOID_TYPE_P (btin_return_type))
8237 return true;
8239 /* If return type is Address (integer type), map it to void *. */
8240 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8241 ada_return_type = ptr_void_type_node;
8243 /* Check return types compatibility otherwise. Note that this
8244 handles void/void as well. */
8245 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8247 post_error ("?intrinsic binding type mismatch on return value!",
8248 inb->gnat_entity);
8249 return false;
8252 return true;
8255 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8256 compatible. Issue relevant warnings when they are not.
8258 This is intended as a light check to diagnose the most obvious cases, not
8259 as a full fledged type compatibility predicate. It is the programmer's
8260 responsibility to ensure correctness of the Ada declarations in Imports,
8261 especially when binding straight to a compiler internal. */
8263 static bool
8264 intrin_profiles_compatible_p (intrin_binding_t * inb)
8266 /* Check compatibility on return values and argument lists, each responsible
8267 for posting warnings as appropriate. Ensure use of the proper sloc for
8268 this purpose. */
8270 bool arglists_compatible_p, return_compatible_p;
8271 location_t saved_location = input_location;
8273 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8275 return_compatible_p = intrin_return_compatible_p (inb);
8276 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8278 input_location = saved_location;
8280 return return_compatible_p && arglists_compatible_p;
8283 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8284 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8285 specified size for this field. POS_LIST is a position list describing
8286 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8287 to this layout. */
8289 static tree
8290 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8291 tree size, tree pos_list,
8292 vec<subst_pair> subst_list)
8294 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8295 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8296 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8297 tree new_pos, new_field;
8298 unsigned int i;
8299 subst_pair *s;
8301 if (CONTAINS_PLACEHOLDER_P (pos))
8302 FOR_EACH_VEC_ELT (subst_list, i, s)
8303 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8305 /* If the position is now a constant, we can set it as the position of the
8306 field when we make it. Otherwise, we need to deal with it specially. */
8307 if (TREE_CONSTANT (pos))
8308 new_pos = bit_from_pos (pos, bitpos);
8309 else
8310 new_pos = NULL_TREE;
8312 new_field
8313 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8314 size, new_pos, DECL_PACKED (old_field),
8315 !DECL_NONADDRESSABLE_P (old_field));
8317 if (!new_pos)
8319 normalize_offset (&pos, &bitpos, offset_align);
8320 /* Finalize the position. */
8321 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8322 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8323 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8324 DECL_SIZE (new_field) = size;
8325 DECL_SIZE_UNIT (new_field)
8326 = convert (sizetype,
8327 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8328 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8331 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8332 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8333 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8334 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8336 return new_field;
8339 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8340 it is the minimal size the REP_PART must have. */
8342 static tree
8343 create_rep_part (tree rep_type, tree record_type, tree min_size)
8345 tree field;
8347 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8348 min_size = NULL_TREE;
8350 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8351 min_size, NULL_TREE, 0, 1);
8352 DECL_INTERNAL_P (field) = 1;
8354 return field;
8357 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8359 static tree
8360 get_rep_part (tree record_type)
8362 tree field = TYPE_FIELDS (record_type);
8364 /* The REP part is the first field, internal, another record, and its name
8365 starts with an 'R'. */
8366 if (field
8367 && DECL_INTERNAL_P (field)
8368 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8369 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8370 return field;
8372 return NULL_TREE;
8375 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8377 tree
8378 get_variant_part (tree record_type)
8380 tree field;
8382 /* The variant part is the only internal field that is a qualified union. */
8383 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8384 if (DECL_INTERNAL_P (field)
8385 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8386 return field;
8388 return NULL_TREE;
8391 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8392 the list of variants to be used and RECORD_TYPE is the type of the parent.
8393 POS_LIST is a position list describing the layout of fields present in
8394 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8395 layout. */
8397 static tree
8398 create_variant_part_from (tree old_variant_part,
8399 vec<variant_desc> variant_list,
8400 tree record_type, tree pos_list,
8401 vec<subst_pair> subst_list)
8403 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8404 tree old_union_type = TREE_TYPE (old_variant_part);
8405 tree new_union_type, new_variant_part;
8406 tree union_field_list = NULL_TREE;
8407 variant_desc *v;
8408 unsigned int i;
8410 /* First create the type of the variant part from that of the old one. */
8411 new_union_type = make_node (QUAL_UNION_TYPE);
8412 TYPE_NAME (new_union_type)
8413 = concat_name (TYPE_NAME (record_type),
8414 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8416 /* If the position of the variant part is constant, subtract it from the
8417 size of the type of the parent to get the new size. This manual CSE
8418 reduces the code size when not optimizing. */
8419 if (TREE_CODE (offset) == INTEGER_CST)
8421 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8422 tree first_bit = bit_from_pos (offset, bitpos);
8423 TYPE_SIZE (new_union_type)
8424 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8425 TYPE_SIZE_UNIT (new_union_type)
8426 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8427 byte_from_pos (offset, bitpos));
8428 SET_TYPE_ADA_SIZE (new_union_type,
8429 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8430 first_bit));
8431 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8432 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8434 else
8435 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8437 /* Now finish up the new variants and populate the union type. */
8438 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8440 tree old_field = v->field, new_field;
8441 tree old_variant, old_variant_subpart, new_variant, field_list;
8443 /* Skip variants that don't belong to this nesting level. */
8444 if (DECL_CONTEXT (old_field) != old_union_type)
8445 continue;
8447 /* Retrieve the list of fields already added to the new variant. */
8448 new_variant = v->new_type;
8449 field_list = TYPE_FIELDS (new_variant);
8451 /* If the old variant had a variant subpart, we need to create a new
8452 variant subpart and add it to the field list. */
8453 old_variant = v->type;
8454 old_variant_subpart = get_variant_part (old_variant);
8455 if (old_variant_subpart)
8457 tree new_variant_subpart
8458 = create_variant_part_from (old_variant_subpart, variant_list,
8459 new_variant, pos_list, subst_list);
8460 DECL_CHAIN (new_variant_subpart) = field_list;
8461 field_list = new_variant_subpart;
8464 /* Finish up the new variant and create the field. No need for debug
8465 info thanks to the XVS type. */
8466 finish_record_type (new_variant, nreverse (field_list), 2, false);
8467 compute_record_mode (new_variant);
8468 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8469 Empty);
8471 new_field
8472 = create_field_decl_from (old_field, new_variant, new_union_type,
8473 TYPE_SIZE (new_variant),
8474 pos_list, subst_list);
8475 DECL_QUALIFIER (new_field) = v->qual;
8476 DECL_INTERNAL_P (new_field) = 1;
8477 DECL_CHAIN (new_field) = union_field_list;
8478 union_field_list = new_field;
8481 /* Finish up the union type and create the variant part. No need for debug
8482 info thanks to the XVS type. Note that we don't reverse the field list
8483 because VARIANT_LIST has been traversed in reverse order. */
8484 finish_record_type (new_union_type, union_field_list, 2, false);
8485 compute_record_mode (new_union_type);
8486 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8487 Empty);
8489 new_variant_part
8490 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8491 TYPE_SIZE (new_union_type),
8492 pos_list, subst_list);
8493 DECL_INTERNAL_P (new_variant_part) = 1;
8495 /* With multiple discriminants it is possible for an inner variant to be
8496 statically selected while outer ones are not; in this case, the list
8497 of fields of the inner variant is not flattened and we end up with a
8498 qualified union with a single member. Drop the useless container. */
8499 if (!DECL_CHAIN (union_field_list))
8501 DECL_CONTEXT (union_field_list) = record_type;
8502 DECL_FIELD_OFFSET (union_field_list)
8503 = DECL_FIELD_OFFSET (new_variant_part);
8504 DECL_FIELD_BIT_OFFSET (union_field_list)
8505 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8506 SET_DECL_OFFSET_ALIGN (union_field_list,
8507 DECL_OFFSET_ALIGN (new_variant_part));
8508 new_variant_part = union_field_list;
8511 return new_variant_part;
8514 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8515 which are both RECORD_TYPE, after applying the substitutions described
8516 in SUBST_LIST. */
8518 static void
8519 copy_and_substitute_in_size (tree new_type, tree old_type,
8520 vec<subst_pair> subst_list)
8522 unsigned int i;
8523 subst_pair *s;
8525 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8526 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8527 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8528 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8529 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8531 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8532 FOR_EACH_VEC_ELT (subst_list, i, s)
8533 TYPE_SIZE (new_type)
8534 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8535 s->discriminant, s->replacement);
8537 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8538 FOR_EACH_VEC_ELT (subst_list, i, s)
8539 TYPE_SIZE_UNIT (new_type)
8540 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8541 s->discriminant, s->replacement);
8543 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8544 FOR_EACH_VEC_ELT (subst_list, i, s)
8545 SET_TYPE_ADA_SIZE
8546 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8547 s->discriminant, s->replacement));
8549 /* Finalize the size. */
8550 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8551 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8554 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8555 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8556 The parallel type is the original array type if it has been translated. */
8558 static void
8559 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8561 Entity_Id gnat_original_array_type
8562 = Underlying_Type (Original_Array_Type (gnat_entity));
8563 tree gnu_original_array_type;
8565 if (!present_gnu_tree (gnat_original_array_type))
8566 return;
8568 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8570 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8571 return;
8573 add_parallel_type (gnu_type, gnu_original_array_type);
8576 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8577 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8578 updated by replacing F with R.
8580 The function doesn't update the layout of the type, i.e. it assumes
8581 that the substitution is purely formal. That's why the replacement
8582 value R must itself contain a PLACEHOLDER_EXPR. */
8584 tree
8585 substitute_in_type (tree t, tree f, tree r)
8587 tree nt;
8589 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8591 switch (TREE_CODE (t))
8593 case INTEGER_TYPE:
8594 case ENUMERAL_TYPE:
8595 case BOOLEAN_TYPE:
8596 case REAL_TYPE:
8598 /* First the domain types of arrays. */
8599 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8600 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8602 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8603 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8605 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8606 return t;
8608 nt = copy_type (t);
8609 TYPE_GCC_MIN_VALUE (nt) = low;
8610 TYPE_GCC_MAX_VALUE (nt) = high;
8612 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8613 SET_TYPE_INDEX_TYPE
8614 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8616 return nt;
8619 /* Then the subtypes. */
8620 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8621 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8623 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8624 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8626 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8627 return t;
8629 nt = copy_type (t);
8630 SET_TYPE_RM_MIN_VALUE (nt, low);
8631 SET_TYPE_RM_MAX_VALUE (nt, high);
8633 return nt;
8636 return t;
8638 case COMPLEX_TYPE:
8639 nt = substitute_in_type (TREE_TYPE (t), f, r);
8640 if (nt == TREE_TYPE (t))
8641 return t;
8643 return build_complex_type (nt);
8645 case FUNCTION_TYPE:
8646 /* These should never show up here. */
8647 gcc_unreachable ();
8649 case ARRAY_TYPE:
8651 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8652 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8654 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8655 return t;
8657 nt = build_nonshared_array_type (component, domain);
8658 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8659 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8660 SET_TYPE_MODE (nt, TYPE_MODE (t));
8661 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8662 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8663 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8664 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8665 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8666 return nt;
8669 case RECORD_TYPE:
8670 case UNION_TYPE:
8671 case QUAL_UNION_TYPE:
8673 bool changed_field = false;
8674 tree field;
8676 /* Start out with no fields, make new fields, and chain them
8677 in. If we haven't actually changed the type of any field,
8678 discard everything we've done and return the old type. */
8679 nt = copy_type (t);
8680 TYPE_FIELDS (nt) = NULL_TREE;
8682 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8684 tree new_field = copy_node (field), new_n;
8686 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8687 if (new_n != TREE_TYPE (field))
8689 TREE_TYPE (new_field) = new_n;
8690 changed_field = true;
8693 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8694 if (new_n != DECL_FIELD_OFFSET (field))
8696 DECL_FIELD_OFFSET (new_field) = new_n;
8697 changed_field = true;
8700 /* Do the substitution inside the qualifier, if any. */
8701 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8703 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8704 if (new_n != DECL_QUALIFIER (field))
8706 DECL_QUALIFIER (new_field) = new_n;
8707 changed_field = true;
8711 DECL_CONTEXT (new_field) = nt;
8712 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8714 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8715 TYPE_FIELDS (nt) = new_field;
8718 if (!changed_field)
8719 return t;
8721 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8722 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8723 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8724 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8725 return nt;
8728 default:
8729 return t;
8733 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8734 needed to represent the object. */
8736 tree
8737 rm_size (tree gnu_type)
8739 /* For integral types, we store the RM size explicitly. */
8740 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8741 return TYPE_RM_SIZE (gnu_type);
8743 /* Return the RM size of the actual data plus the size of the template. */
8744 if (TREE_CODE (gnu_type) == RECORD_TYPE
8745 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8746 return
8747 size_binop (PLUS_EXPR,
8748 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8749 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8751 /* For record or union types, we store the size explicitly. */
8752 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8753 && !TYPE_FAT_POINTER_P (gnu_type)
8754 && TYPE_ADA_SIZE (gnu_type))
8755 return TYPE_ADA_SIZE (gnu_type);
8757 /* For other types, this is just the size. */
8758 return TYPE_SIZE (gnu_type);
8761 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8762 fully-qualified name, possibly with type information encoding.
8763 Otherwise, return the name. */
8765 tree
8766 get_entity_name (Entity_Id gnat_entity)
8768 Get_Encoded_Name (gnat_entity);
8769 return get_identifier_with_length (Name_Buffer, Name_Len);
8772 /* Return an identifier representing the external name to be used for
8773 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8774 and the specified suffix. */
8776 tree
8777 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8779 const Entity_Kind kind = Ekind (gnat_entity);
8780 const bool has_suffix = (suffix != NULL);
8781 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8782 String_Pointer sp = {suffix, &temp};
8784 Get_External_Name (gnat_entity, has_suffix, sp);
8786 /* A variable using the Stdcall convention lives in a DLL. We adjust
8787 its name to use the jump table, the _imp__NAME contains the address
8788 for the NAME variable. */
8789 if ((kind == E_Variable || kind == E_Constant)
8790 && Has_Stdcall_Convention (gnat_entity))
8792 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8793 char *new_name = (char *) alloca (len + 1);
8794 strcpy (new_name, STDCALL_PREFIX);
8795 strcat (new_name, Name_Buffer);
8796 return get_identifier_with_length (new_name, len);
8799 return get_identifier_with_length (Name_Buffer, Name_Len);
8802 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8803 string, return a new IDENTIFIER_NODE that is the concatenation of
8804 the name followed by "___" and the specified suffix. */
8806 tree
8807 concat_name (tree gnu_name, const char *suffix)
8809 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8810 char *new_name = (char *) alloca (len + 1);
8811 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8812 strcat (new_name, "___");
8813 strcat (new_name, suffix);
8814 return get_identifier_with_length (new_name, len);
8817 /* Initialize data structures of the decl.c module. */
8819 void
8820 init_gnat_decl (void)
8822 /* Initialize the cache of annotated values. */
8823 annotate_value_cache
8824 = htab_create_ggc (512, tree_int_map_hash, tree_int_map_eq, 0);
8827 /* Destroy data structures of the decl.c module. */
8829 void
8830 destroy_gnat_decl (void)
8832 /* Destroy the cache of annotated values. */
8833 htab_delete (annotate_value_cache);
8834 annotate_value_cache = NULL;
8837 #include "gt-ada-decl.h"