2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob2ed68d49578f40aa85eeae077247190928157efc
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "ggc.h"
36 #include "target.h"
37 #include "tree-inline.h"
38 #include "diagnostic-core.h"
40 #include "ada.h"
41 #include "types.h"
42 #include "atree.h"
43 #include "elists.h"
44 #include "namet.h"
45 #include "nlists.h"
46 #include "repinfo.h"
47 #include "snames.h"
48 #include "stringt.h"
49 #include "uintp.h"
50 #include "fe.h"
51 #include "sinfo.h"
52 #include "einfo.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 /* "stdcall" and "thiscall" conventions should be processed in a specific way
57 on 32-bit x86/Windows only. The macros below are helpers to avoid having
58 to check for a Windows specific attribute throughout this unit. */
60 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
61 #ifdef TARGET_64BIT
62 #define Has_Stdcall_Convention(E) \
63 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
64 #define Has_Thiscall_Convention(E) \
65 (!TARGET_64BIT && is_cplusplus_method (E))
66 #else
67 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
68 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #endif
70 #else
71 #define Has_Stdcall_Convention(E) 0
72 #define Has_Thiscall_Convention(E) 0
73 #endif
75 #define STDCALL_PREFIX "_imp__"
77 /* Stack realignment is necessary for functions with foreign conventions when
78 the ABI doesn't mandate as much as what the compiler assumes - that is, up
79 to PREFERRED_STACK_BOUNDARY.
81 Such realignment can be requested with a dedicated function type attribute
82 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
83 characterize the situations where the attribute should be set. We rely on
84 compiler configuration settings for 'main' to decide. */
86 #ifdef MAIN_STACK_BOUNDARY
87 #define FOREIGN_FORCE_REALIGN_STACK \
88 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
89 #else
90 #define FOREIGN_FORCE_REALIGN_STACK 0
91 #endif
93 struct incomplete
95 struct incomplete *next;
96 tree old_type;
97 Entity_Id full_type;
100 /* These variables are used to defer recursively expanding incomplete types
101 while we are processing an array, a record or a subprogram type. */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
105 /* This variable is used to delay expanding From_Limited_With types until the
106 end of the spec. */
107 static struct incomplete *defer_limited_with;
109 typedef struct subst_pair_d {
110 tree discriminant;
111 tree replacement;
112 } subst_pair;
115 typedef struct variant_desc_d {
116 /* The type of the variant. */
117 tree type;
119 /* The associated field. */
120 tree field;
122 /* The value of the qualifier. */
123 tree qual;
125 /* The type of the variant after transformation. */
126 tree new_type;
127 } variant_desc;
130 /* A hash table used to cache the result of annotate_value. */
131 static GTY ((if_marked ("tree_int_map_marked_p"),
132 param_is (struct tree_int_map))) htab_t annotate_value_cache;
134 static bool allocatable_size_p (tree, bool);
135 static void prepend_one_attribute (struct attrib **,
136 enum attr_type, tree, tree, Node_Id);
137 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
138 static void prepend_attributes (struct attrib **, Entity_Id);
139 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
140 static bool type_has_variable_size (tree);
141 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
142 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
143 unsigned int);
144 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
145 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
146 bool *);
147 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
148 static tree change_qualified_type (tree, int);
149 static bool same_discriminant_p (Entity_Id, Entity_Id);
150 static bool array_type_has_nonaliased_component (tree, Entity_Id);
151 static bool compile_time_known_address_p (Node_Id);
152 static bool cannot_be_superflat_p (Node_Id);
153 static bool constructor_address_p (tree);
154 static int compare_field_bitpos (const PTR, const PTR);
155 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
156 bool, bool, bool, bool, bool, tree, tree *);
157 static Uint annotate_value (tree);
158 static void annotate_rep (Entity_Id, tree);
159 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
160 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
161 static vec<variant_desc> build_variant_list (tree,
162 vec<subst_pair> ,
163 vec<variant_desc> );
164 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
165 static void set_rm_size (Uint, tree, Entity_Id);
166 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
167 static void check_ok_for_atomic (tree, Entity_Id, bool);
168 static tree create_field_decl_from (tree, tree, tree, tree, tree,
169 vec<subst_pair> );
170 static tree create_rep_part (tree, tree, tree);
171 static tree get_rep_part (tree);
172 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
173 tree, vec<subst_pair> );
174 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
175 static void add_parallel_type_for_packed_array (tree, Entity_Id);
177 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
178 to pass around calls performing profile compatibility checks. */
180 typedef struct {
181 Entity_Id gnat_entity; /* The Ada subprogram entity. */
182 tree ada_fntype; /* The corresponding GCC type node. */
183 tree btin_fntype; /* The GCC builtin function type node. */
184 } intrin_binding_t;
186 static bool intrin_profiles_compatible_p (intrin_binding_t *);
188 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
189 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
190 and associate the ..._DECL node with the input GNAT defining identifier.
192 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
193 initial value (in GCC tree form). This is optional for a variable. For
194 a renamed entity, GNU_EXPR gives the object being renamed.
196 DEFINITION is nonzero if this call is intended for a definition. This is
197 used for separate compilation where it is necessary to know whether an
198 external declaration or a definition must be created if the GCC equivalent
199 was not created previously. The value of 1 is normally used for a nonzero
200 DEFINITION, but a value of 2 is used in special circumstances, defined in
201 the code. */
203 tree
204 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
206 /* Contains the kind of the input GNAT node. */
207 const Entity_Kind kind = Ekind (gnat_entity);
208 /* True if this is a type. */
209 const bool is_type = IN (kind, Type_Kind);
210 /* True if debug info is requested for this entity. */
211 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
212 /* True if this entity is to be considered as imported. */
213 const bool imported_p
214 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
215 /* For a type, contains the equivalent GNAT node to be used in gigi. */
216 Entity_Id gnat_equiv_type = Empty;
217 /* Temporary used to walk the GNAT tree. */
218 Entity_Id gnat_temp;
219 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
220 This node will be associated with the GNAT node by calling at the end
221 of the `switch' statement. */
222 tree gnu_decl = NULL_TREE;
223 /* Contains the GCC type to be used for the GCC node. */
224 tree gnu_type = NULL_TREE;
225 /* Contains the GCC size tree to be used for the GCC node. */
226 tree gnu_size = NULL_TREE;
227 /* Contains the GCC name to be used for the GCC node. */
228 tree gnu_entity_name;
229 /* True if we have already saved gnu_decl as a GNAT association. */
230 bool saved = false;
231 /* True if we incremented defer_incomplete_level. */
232 bool this_deferred = false;
233 /* True if we incremented force_global. */
234 bool this_global = false;
235 /* True if we should check to see if elaborated during processing. */
236 bool maybe_present = false;
237 /* True if we made GNU_DECL and its type here. */
238 bool this_made_decl = false;
239 /* Size and alignment of the GCC node, if meaningful. */
240 unsigned int esize = 0, align = 0;
241 /* Contains the list of attributes directly attached to the entity. */
242 struct attrib *attr_list = NULL;
244 /* Since a use of an Itype is a definition, process it as such if it
245 is not in a with'ed unit. */
246 if (!definition
247 && is_type
248 && Is_Itype (gnat_entity)
249 && !present_gnu_tree (gnat_entity)
250 && In_Extended_Main_Code_Unit (gnat_entity))
252 /* Ensure that we are in a subprogram mentioned in the Scope chain of
253 this entity, our current scope is global, or we encountered a task
254 or entry (where we can't currently accurately check scoping). */
255 if (!current_function_decl
256 || DECL_ELABORATION_PROC_P (current_function_decl))
258 process_type (gnat_entity);
259 return get_gnu_tree (gnat_entity);
262 for (gnat_temp = Scope (gnat_entity);
263 Present (gnat_temp);
264 gnat_temp = Scope (gnat_temp))
266 if (Is_Type (gnat_temp))
267 gnat_temp = Underlying_Type (gnat_temp);
269 if (Ekind (gnat_temp) == E_Subprogram_Body)
270 gnat_temp
271 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
273 if (IN (Ekind (gnat_temp), Subprogram_Kind)
274 && Present (Protected_Body_Subprogram (gnat_temp)))
275 gnat_temp = Protected_Body_Subprogram (gnat_temp);
277 if (Ekind (gnat_temp) == E_Entry
278 || Ekind (gnat_temp) == E_Entry_Family
279 || Ekind (gnat_temp) == E_Task_Type
280 || (IN (Ekind (gnat_temp), Subprogram_Kind)
281 && present_gnu_tree (gnat_temp)
282 && (current_function_decl
283 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
285 process_type (gnat_entity);
286 return get_gnu_tree (gnat_entity);
290 /* This abort means the Itype has an incorrect scope, i.e. that its
291 scope does not correspond to the subprogram it is declared in. */
292 gcc_unreachable ();
295 /* If we've already processed this entity, return what we got last time.
296 If we are defining the node, we should not have already processed it.
297 In that case, we will abort below when we try to save a new GCC tree
298 for this object. We also need to handle the case of getting a dummy
299 type when a Full_View exists but be careful so as not to trigger its
300 premature elaboration. */
301 if ((!definition || (is_type && imported_p))
302 && present_gnu_tree (gnat_entity))
304 gnu_decl = get_gnu_tree (gnat_entity);
306 if (TREE_CODE (gnu_decl) == TYPE_DECL
307 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
308 && IN (kind, Incomplete_Or_Private_Kind)
309 && Present (Full_View (gnat_entity))
310 && (present_gnu_tree (Full_View (gnat_entity))
311 || No (Freeze_Node (Full_View (gnat_entity)))))
313 gnu_decl
314 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
315 save_gnu_tree (gnat_entity, NULL_TREE, false);
316 save_gnu_tree (gnat_entity, gnu_decl, false);
319 return gnu_decl;
322 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
323 must be specified unless it was specified by the programmer. Exceptions
324 are for access-to-protected-subprogram types and all access subtypes, as
325 another GNAT type is used to lay out the GCC type for them. */
326 gcc_assert (!Unknown_Esize (gnat_entity)
327 || Has_Size_Clause (gnat_entity)
328 || (!IN (kind, Numeric_Kind)
329 && !IN (kind, Enumeration_Kind)
330 && (!IN (kind, Access_Kind)
331 || kind == E_Access_Protected_Subprogram_Type
332 || kind == E_Anonymous_Access_Protected_Subprogram_Type
333 || kind == E_Access_Subtype
334 || type_annotate_only)));
336 /* The RM size must be specified for all discrete and fixed-point types. */
337 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
338 && Unknown_RM_Size (gnat_entity)));
340 /* If we get here, it means we have not yet done anything with this entity.
341 If we are not defining it, it must be a type or an entity that is defined
342 elsewhere or externally, otherwise we should have defined it already. */
343 gcc_assert (definition
344 || type_annotate_only
345 || is_type
346 || kind == E_Discriminant
347 || kind == E_Component
348 || kind == E_Label
349 || (kind == E_Constant && Present (Full_View (gnat_entity)))
350 || Is_Public (gnat_entity));
352 /* Get the name of the entity and set up the line number and filename of
353 the original definition for use in any decl we make. Make sure we do not
354 inherit another source location. */
355 gnu_entity_name = get_entity_name (gnat_entity);
356 if (Sloc (gnat_entity) != No_Location
357 && !renaming_from_generic_instantiation_p (gnat_entity))
358 Sloc_to_locus (Sloc (gnat_entity), &input_location);
360 /* For cases when we are not defining (i.e., we are referencing from
361 another compilation unit) public entities, show we are at global level
362 for the purpose of computing scopes. Don't do this for components or
363 discriminants since the relevant test is whether or not the record is
364 being defined. */
365 if (!definition
366 && kind != E_Component
367 && kind != E_Discriminant
368 && Is_Public (gnat_entity)
369 && !Is_Statically_Allocated (gnat_entity))
370 force_global++, this_global = true;
372 /* Handle any attributes directly attached to the entity. */
373 if (Has_Gigi_Rep_Item (gnat_entity))
374 prepend_attributes (&attr_list, gnat_entity);
376 /* Do some common processing for types. */
377 if (is_type)
379 /* Compute the equivalent type to be used in gigi. */
380 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
382 /* Machine_Attributes on types are expected to be propagated to
383 subtypes. The corresponding Gigi_Rep_Items are only attached
384 to the first subtype though, so we handle the propagation here. */
385 if (Base_Type (gnat_entity) != gnat_entity
386 && !Is_First_Subtype (gnat_entity)
387 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
388 prepend_attributes (&attr_list,
389 First_Subtype (Base_Type (gnat_entity)));
391 /* Compute a default value for the size of an elementary type. */
392 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
394 unsigned int max_esize;
396 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
397 esize = UI_To_Int (Esize (gnat_entity));
399 if (IN (kind, Float_Kind))
400 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
401 else if (IN (kind, Access_Kind))
402 max_esize = POINTER_SIZE * 2;
403 else
404 max_esize = LONG_LONG_TYPE_SIZE;
406 if (esize > max_esize)
407 esize = max_esize;
411 switch (kind)
413 case E_Constant:
414 /* If this is a use of a deferred constant without address clause,
415 get its full definition. */
416 if (!definition
417 && No (Address_Clause (gnat_entity))
418 && Present (Full_View (gnat_entity)))
420 gnu_decl
421 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
422 saved = true;
423 break;
426 /* If we have an external constant that we are not defining, get the
427 expression that is was defined to represent. We may throw it away
428 later if it is not a constant. But do not retrieve the expression
429 if it is an allocator because the designated type might be dummy
430 at this point. */
431 if (!definition
432 && !No_Initialization (Declaration_Node (gnat_entity))
433 && Present (Expression (Declaration_Node (gnat_entity)))
434 && Nkind (Expression (Declaration_Node (gnat_entity)))
435 != N_Allocator)
437 bool went_into_elab_proc = false;
438 int save_force_global = force_global;
440 /* The expression may contain N_Expression_With_Actions nodes and
441 thus object declarations from other units. In this case, even
442 though the expression will eventually be discarded since not a
443 constant, the declarations would be stuck either in the global
444 varpool or in the current scope. Therefore we force the local
445 context and create a fake scope that we'll zap at the end. */
446 if (!current_function_decl)
448 current_function_decl = get_elaboration_procedure ();
449 went_into_elab_proc = true;
451 force_global = 0;
452 gnat_pushlevel ();
454 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
456 gnat_zaplevel ();
457 force_global = save_force_global;
458 if (went_into_elab_proc)
459 current_function_decl = NULL_TREE;
462 /* Ignore deferred constant definitions without address clause since
463 they are processed fully in the front-end. If No_Initialization
464 is set, this is not a deferred constant but a constant whose value
465 is built manually. And constants that are renamings are handled
466 like variables. */
467 if (definition
468 && !gnu_expr
469 && No (Address_Clause (gnat_entity))
470 && !No_Initialization (Declaration_Node (gnat_entity))
471 && No (Renamed_Object (gnat_entity)))
473 gnu_decl = error_mark_node;
474 saved = true;
475 break;
478 /* Ignore constant definitions already marked with the error node. See
479 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
480 if (definition
481 && gnu_expr
482 && present_gnu_tree (gnat_entity)
483 && get_gnu_tree (gnat_entity) == error_mark_node)
485 maybe_present = true;
486 break;
489 goto object;
491 case E_Exception:
492 goto object;
494 case E_Component:
495 case E_Discriminant:
497 /* The GNAT record where the component was defined. */
498 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
500 /* If the entity is a discriminant of an extended tagged type used to
501 rename a discriminant of the parent type, return the latter. */
502 if (Is_Tagged_Type (gnat_record)
503 && Present (Corresponding_Discriminant (gnat_entity)))
505 gnu_decl
506 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
507 gnu_expr, definition);
508 saved = true;
509 break;
512 /* If the entity is an inherited component (in the case of extended
513 tagged record types), just return the original entity, which must
514 be a FIELD_DECL. Likewise for discriminants. If the entity is a
515 non-girder discriminant (in the case of derived untagged record
516 types), return the stored discriminant it renames. */
517 else if (Present (Original_Record_Component (gnat_entity))
518 && Original_Record_Component (gnat_entity) != gnat_entity)
520 gnu_decl
521 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
522 gnu_expr, definition);
523 saved = true;
524 break;
527 /* Otherwise, if we are not defining this and we have no GCC type
528 for the containing record, make one for it. Then we should
529 have made our own equivalent. */
530 else if (!definition && !present_gnu_tree (gnat_record))
532 /* ??? If this is in a record whose scope is a protected
533 type and we have an Original_Record_Component, use it.
534 This is a workaround for major problems in protected type
535 handling. */
536 Entity_Id Scop = Scope (Scope (gnat_entity));
537 if (Is_Protected_Type (Underlying_Type (Scop))
538 && Present (Original_Record_Component (gnat_entity)))
540 gnu_decl
541 = gnat_to_gnu_entity (Original_Record_Component
542 (gnat_entity),
543 gnu_expr, 0);
544 saved = true;
545 break;
548 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
549 gnu_decl = get_gnu_tree (gnat_entity);
550 saved = true;
551 break;
554 else
555 /* Here we have no GCC type and this is a reference rather than a
556 definition. This should never happen. Most likely the cause is
557 reference before declaration in the GNAT tree for gnat_entity. */
558 gcc_unreachable ();
561 case E_Loop_Parameter:
562 case E_Out_Parameter:
563 case E_Variable:
565 /* Simple variables, loop variables, Out parameters and exceptions. */
566 object:
568 /* Always create a variable for volatile objects and variables seen
569 constant but with a Linker_Section pragma. */
570 bool const_flag
571 = ((kind == E_Constant || kind == E_Variable)
572 && Is_True_Constant (gnat_entity)
573 && !(kind == E_Variable
574 && Present (Linker_Section_Pragma (gnat_entity)))
575 && !Treat_As_Volatile (gnat_entity)
576 && (((Nkind (Declaration_Node (gnat_entity))
577 == N_Object_Declaration)
578 && Present (Expression (Declaration_Node (gnat_entity))))
579 || Present (Renamed_Object (gnat_entity))
580 || imported_p));
581 bool inner_const_flag = const_flag;
582 bool static_p = Is_Statically_Allocated (gnat_entity);
583 bool mutable_p = false;
584 bool used_by_ref = false;
585 tree gnu_ext_name = NULL_TREE;
586 tree renamed_obj = NULL_TREE;
587 tree gnu_object_size;
589 if (Present (Renamed_Object (gnat_entity)) && !definition)
591 if (kind == E_Exception)
592 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
593 NULL_TREE, 0);
594 else
595 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
598 /* Get the type after elaborating the renamed object. */
599 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
601 /* If this is a standard exception definition, then use the standard
602 exception type. This is necessary to make sure that imported and
603 exported views of exceptions are properly merged in LTO mode. */
604 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
605 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
606 gnu_type = except_type_node;
608 /* For a debug renaming declaration, build a debug-only entity. */
609 if (Present (Debug_Renaming_Link (gnat_entity)))
611 /* Force a non-null value to make sure the symbol is retained. */
612 tree value = build1 (INDIRECT_REF, gnu_type,
613 build1 (NOP_EXPR,
614 build_pointer_type (gnu_type),
615 integer_minus_one_node));
616 gnu_decl = build_decl (input_location,
617 VAR_DECL, gnu_entity_name, gnu_type);
618 SET_DECL_VALUE_EXPR (gnu_decl, value);
619 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
620 gnat_pushdecl (gnu_decl, gnat_entity);
621 break;
624 /* If this is a loop variable, its type should be the base type.
625 This is because the code for processing a loop determines whether
626 a normal loop end test can be done by comparing the bounds of the
627 loop against those of the base type, which is presumed to be the
628 size used for computation. But this is not correct when the size
629 of the subtype is smaller than the type. */
630 if (kind == E_Loop_Parameter)
631 gnu_type = get_base_type (gnu_type);
633 /* Reject non-renamed objects whose type is an unconstrained array or
634 any object whose type is a dummy type or void. */
635 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
636 && No (Renamed_Object (gnat_entity)))
637 || TYPE_IS_DUMMY_P (gnu_type)
638 || TREE_CODE (gnu_type) == VOID_TYPE)
640 gcc_assert (type_annotate_only);
641 if (this_global)
642 force_global--;
643 return error_mark_node;
646 /* If an alignment is specified, use it if valid. Note that exceptions
647 are objects but don't have an alignment. We must do this before we
648 validate the size, since the alignment can affect the size. */
649 if (kind != E_Exception && Known_Alignment (gnat_entity))
651 gcc_assert (Present (Alignment (gnat_entity)));
653 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
654 TYPE_ALIGN (gnu_type));
656 /* No point in changing the type if there is an address clause
657 as the final type of the object will be a reference type. */
658 if (Present (Address_Clause (gnat_entity)))
659 align = 0;
660 else
662 tree orig_type = gnu_type;
664 gnu_type
665 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
666 false, false, definition, true);
668 /* If a padding record was made, declare it now since it will
669 never be declared otherwise. This is necessary to ensure
670 that its subtrees are properly marked. */
671 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
672 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
673 debug_info_p, gnat_entity);
677 /* If we are defining the object, see if it has a Size and validate it
678 if so. If we are not defining the object and a Size clause applies,
679 simply retrieve the value. We don't want to ignore the clause and
680 it is expected to have been validated already. Then get the new
681 type, if any. */
682 if (definition)
683 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
684 gnat_entity, VAR_DECL, false,
685 Has_Size_Clause (gnat_entity));
686 else if (Has_Size_Clause (gnat_entity))
687 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
689 if (gnu_size)
691 gnu_type
692 = make_type_from_size (gnu_type, gnu_size,
693 Has_Biased_Representation (gnat_entity));
695 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
696 gnu_size = NULL_TREE;
699 /* If this object has self-referential size, it must be a record with
700 a default discriminant. We are supposed to allocate an object of
701 the maximum size in this case, unless it is a constant with an
702 initializing expression, in which case we can get the size from
703 that. Note that the resulting size may still be a variable, so
704 this may end up with an indirect allocation. */
705 if (No (Renamed_Object (gnat_entity))
706 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
708 if (gnu_expr && kind == E_Constant)
710 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
711 if (CONTAINS_PLACEHOLDER_P (size))
713 /* If the initializing expression is itself a constant,
714 despite having a nominal type with self-referential
715 size, we can get the size directly from it. */
716 if (TREE_CODE (gnu_expr) == COMPONENT_REF
717 && TYPE_IS_PADDING_P
718 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
719 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
720 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
721 || DECL_READONLY_ONCE_ELAB
722 (TREE_OPERAND (gnu_expr, 0))))
723 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
724 else
725 gnu_size
726 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
728 else
729 gnu_size = size;
731 /* We may have no GNU_EXPR because No_Initialization is
732 set even though there's an Expression. */
733 else if (kind == E_Constant
734 && (Nkind (Declaration_Node (gnat_entity))
735 == N_Object_Declaration)
736 && Present (Expression (Declaration_Node (gnat_entity))))
737 gnu_size
738 = TYPE_SIZE (gnat_to_gnu_type
739 (Etype
740 (Expression (Declaration_Node (gnat_entity)))));
741 else
743 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
744 mutable_p = true;
747 /* If we are at global level and the size isn't constant, call
748 elaborate_expression_1 to make a variable for it rather than
749 calculating it each time. */
750 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
751 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
752 get_identifier ("SIZE"),
753 definition, false);
756 /* If the size is zero byte, make it one byte since some linkers have
757 troubles with zero-sized objects. If the object will have a
758 template, that will make it nonzero so don't bother. Also avoid
759 doing that for an object renaming or an object with an address
760 clause, as we would lose useful information on the view size
761 (e.g. for null array slices) and we are not allocating the object
762 here anyway. */
763 if (((gnu_size
764 && integer_zerop (gnu_size)
765 && !TREE_OVERFLOW (gnu_size))
766 || (TYPE_SIZE (gnu_type)
767 && integer_zerop (TYPE_SIZE (gnu_type))
768 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
769 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
770 && No (Renamed_Object (gnat_entity))
771 && No (Address_Clause (gnat_entity)))
772 gnu_size = bitsize_unit_node;
774 /* If this is an object with no specified size and alignment, and
775 if either it is atomic or we are not optimizing alignment for
776 space and it is composite and not an exception, an Out parameter
777 or a reference to another object, and the size of its type is a
778 constant, set the alignment to the smallest one which is not
779 smaller than the size, with an appropriate cap. */
780 if (!gnu_size && align == 0
781 && (Is_Atomic (gnat_entity)
782 || (!Optimize_Alignment_Space (gnat_entity)
783 && kind != E_Exception
784 && kind != E_Out_Parameter
785 && Is_Composite_Type (Etype (gnat_entity))
786 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
787 && !Is_Exported (gnat_entity)
788 && !imported_p
789 && No (Renamed_Object (gnat_entity))
790 && No (Address_Clause (gnat_entity))))
791 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
793 unsigned int size_cap, align_cap;
795 /* No point in promoting the alignment if this doesn't prevent
796 BLKmode access to the object, in particular block copy, as
797 this will for example disable the NRV optimization for it.
798 No point in jumping through all the hoops needed in order
799 to support BIGGEST_ALIGNMENT if we don't really have to.
800 So we cap to the smallest alignment that corresponds to
801 a known efficient memory access pattern of the target. */
802 if (Is_Atomic (gnat_entity))
804 size_cap = UINT_MAX;
805 align_cap = BIGGEST_ALIGNMENT;
807 else
809 size_cap = MAX_FIXED_MODE_SIZE;
810 align_cap = get_mode_alignment (ptr_mode);
813 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
814 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
815 align = 0;
816 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
817 align = align_cap;
818 else
819 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
821 /* But make sure not to under-align the object. */
822 if (align <= TYPE_ALIGN (gnu_type))
823 align = 0;
825 /* And honor the minimum valid atomic alignment, if any. */
826 #ifdef MINIMUM_ATOMIC_ALIGNMENT
827 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
828 align = MINIMUM_ATOMIC_ALIGNMENT;
829 #endif
832 /* If the object is set to have atomic components, find the component
833 type and validate it.
835 ??? Note that we ignore Has_Volatile_Components on objects; it's
836 not at all clear what to do in that case. */
837 if (Has_Atomic_Components (gnat_entity))
839 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
840 ? TREE_TYPE (gnu_type) : gnu_type);
842 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
843 && TYPE_MULTI_ARRAY_P (gnu_inner))
844 gnu_inner = TREE_TYPE (gnu_inner);
846 check_ok_for_atomic (gnu_inner, gnat_entity, true);
849 /* Now check if the type of the object allows atomic access. Note
850 that we must test the type, even if this object has size and
851 alignment to allow such access, because we will be going inside
852 the padded record to assign to the object. We could fix this by
853 always copying via an intermediate value, but it's not clear it's
854 worth the effort. */
855 if (Is_Atomic (gnat_entity))
856 check_ok_for_atomic (gnu_type, gnat_entity, false);
858 /* If this is an aliased object with an unconstrained nominal subtype,
859 make a type that includes the template. */
860 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
861 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
862 && !type_annotate_only)
864 tree gnu_array
865 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
866 gnu_type
867 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
868 gnu_type,
869 concat_name (gnu_entity_name,
870 "UNC"),
871 debug_info_p);
874 /* ??? If this is an object of CW type initialized to a value, try to
875 ensure that the object is sufficient aligned for this value, but
876 without pessimizing the allocation. This is a kludge necessary
877 because we don't support dynamic alignment. */
878 if (align == 0
879 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
880 && No (Renamed_Object (gnat_entity))
881 && No (Address_Clause (gnat_entity)))
882 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
884 #ifdef MINIMUM_ATOMIC_ALIGNMENT
885 /* If the size is a constant and no alignment is specified, force
886 the alignment to be the minimum valid atomic alignment. The
887 restriction on constant size avoids problems with variable-size
888 temporaries; if the size is variable, there's no issue with
889 atomic access. Also don't do this for a constant, since it isn't
890 necessary and can interfere with constant replacement. Finally,
891 do not do it for Out parameters since that creates an
892 size inconsistency with In parameters. */
893 if (align == 0
894 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
895 && !FLOAT_TYPE_P (gnu_type)
896 && !const_flag && No (Renamed_Object (gnat_entity))
897 && !imported_p && No (Address_Clause (gnat_entity))
898 && kind != E_Out_Parameter
899 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
900 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
901 align = MINIMUM_ATOMIC_ALIGNMENT;
902 #endif
904 /* Make a new type with the desired size and alignment, if needed.
905 But do not take into account alignment promotions to compute the
906 size of the object. */
907 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
908 if (gnu_size || align > 0)
910 tree orig_type = gnu_type;
912 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
913 false, false, definition, true);
915 /* If a padding record was made, declare it now since it will
916 never be declared otherwise. This is necessary to ensure
917 that its subtrees are properly marked. */
918 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
919 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
920 debug_info_p, gnat_entity);
923 /* If this is a renaming, avoid as much as possible to create a new
924 object. However, in several cases, creating it is required.
925 This processing needs to be applied to the raw expression so
926 as to make it more likely to rename the underlying object. */
927 if (Present (Renamed_Object (gnat_entity)))
929 bool create_normal_object = false;
931 /* If the renamed object had padding, strip off the reference
932 to the inner object and reset our type. */
933 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
934 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
935 /* Strip useless conversions around the object. */
936 || gnat_useless_type_conversion (gnu_expr))
938 gnu_expr = TREE_OPERAND (gnu_expr, 0);
939 gnu_type = TREE_TYPE (gnu_expr);
942 /* Or else, if the renamed object has an unconstrained type with
943 default discriminant, use the padded type. */
944 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
945 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
946 == gnu_type
947 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
948 gnu_type = TREE_TYPE (gnu_expr);
950 /* Case 1: If this is a constant renaming stemming from a function
951 call, treat it as a normal object whose initial value is what is
952 being renamed. RM 3.3 says that the result of evaluating a
953 function call is a constant object. Treat constant literals
954 the same way. As a consequence, it can be the inner object of
955 a constant renaming. In this case, the renaming must be fully
956 instantiated, i.e. it cannot be a mere reference to (part of) an
957 existing object. */
958 if (const_flag)
960 tree inner_object = gnu_expr;
961 while (handled_component_p (inner_object))
962 inner_object = TREE_OPERAND (inner_object, 0);
963 if (TREE_CODE (inner_object) == CALL_EXPR
964 || CONSTANT_CLASS_P (inner_object))
965 create_normal_object = true;
968 /* Otherwise, see if we can proceed with a stabilized version of
969 the renamed entity or if we need to make a new object. */
970 if (!create_normal_object)
972 tree maybe_stable_expr = NULL_TREE;
973 bool stable = false;
975 /* Case 2: If the renaming entity need not be materialized and
976 the renamed expression is something we can stabilize, use
977 that for the renaming. At the global level, we can only do
978 this if we know no SAVE_EXPRs need be made, because the
979 expression we return might be used in arbitrary conditional
980 branches so we must force the evaluation of the SAVE_EXPRs
981 immediately and this requires a proper function context.
982 Note that an external constant is at the global level. */
983 if (!Materialize_Entity (gnat_entity)
984 && (!((!definition && kind == E_Constant)
985 || global_bindings_p ())
986 || (staticp (gnu_expr)
987 && !TREE_SIDE_EFFECTS (gnu_expr))))
989 maybe_stable_expr
990 = gnat_stabilize_reference (gnu_expr, true, &stable);
992 if (stable)
994 /* ??? No DECL_EXPR is created so we need to mark
995 the expression manually lest it is shared. */
996 if ((!definition && kind == E_Constant)
997 || global_bindings_p ())
998 MARK_VISITED (maybe_stable_expr);
999 gnu_decl = maybe_stable_expr;
1000 save_gnu_tree (gnat_entity, gnu_decl, true);
1001 saved = true;
1002 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1003 false);
1004 /* This assertion will fail if the renamed object
1005 isn't aligned enough as to make it possible to
1006 honor the alignment set on the renaming. */
1007 if (align)
1009 unsigned int renamed_align
1010 = DECL_P (gnu_decl)
1011 ? DECL_ALIGN (gnu_decl)
1012 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1013 gcc_assert (renamed_align >= align);
1015 break;
1018 /* The stabilization failed. Keep maybe_stable_expr
1019 untouched here to let the pointer case below know
1020 about that failure. */
1023 /* Case 3: Make this into a constant pointer to the object we
1024 are to rename and attach the object to the pointer if it is
1025 something we can stabilize.
1027 From the proper scope, attached objects will be referenced
1028 directly instead of indirectly via the pointer to avoid
1029 subtle aliasing problems with non-addressable entities.
1030 They have to be stable because we must not evaluate the
1031 variables in the expression every time the renaming is used.
1032 The pointer is called a "renaming" pointer in this case.
1034 In the rare cases where we cannot stabilize the renamed
1035 object, we just make a "bare" pointer and the renamed
1036 object will always be accessed indirectly through it.
1038 Note that we need to preserve the volatility of the renamed
1039 object through the indirection. */
1040 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1041 gnu_type
1042 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1043 gnu_type = build_reference_type (gnu_type);
1044 inner_const_flag = TREE_READONLY (gnu_expr);
1045 const_flag = true;
1047 /* If the previous attempt at stabilizing failed, there is
1048 no point in trying again and we reuse the result without
1049 attaching it to the pointer. In this case it will only
1050 be used as the initializing expression of the pointer and
1051 thus needs no special treatment with regard to multiple
1052 evaluations.
1054 Otherwise, try to stabilize and attach the expression to
1055 the pointer if the stabilization succeeds.
1057 Note that this might introduce SAVE_EXPRs and we don't
1058 check whether we are at the global level or not. This
1059 is fine since we are building a pointer initializer and
1060 neither the pointer nor the initializing expression can
1061 be accessed before the pointer elaboration has taken
1062 place in a correct program.
1064 These SAVE_EXPRs will be evaluated at the right place
1065 by either the evaluation of the initializer for the
1066 non-global case or the elaboration code for the global
1067 case, and will be attached to the elaboration procedure
1068 in the latter case. */
1069 if (!maybe_stable_expr)
1071 maybe_stable_expr
1072 = gnat_stabilize_reference (gnu_expr, true, &stable);
1074 if (stable)
1075 renamed_obj = maybe_stable_expr;
1078 if (type_annotate_only
1079 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1080 gnu_expr = NULL_TREE;
1081 else
1082 gnu_expr
1083 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1085 gnu_size = NULL_TREE;
1086 used_by_ref = true;
1090 /* Make a volatile version of this object's type if we are to make
1091 the object volatile. We also interpret 13.3(19) conservatively
1092 and disallow any optimizations for such a non-constant object. */
1093 if ((Treat_As_Volatile (gnat_entity)
1094 || (!const_flag
1095 && gnu_type != except_type_node
1096 && (Is_Exported (gnat_entity)
1097 || imported_p
1098 || Present (Address_Clause (gnat_entity)))))
1099 && !TYPE_VOLATILE (gnu_type))
1100 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1102 /* If we are defining an aliased object whose nominal subtype is
1103 unconstrained, the object is a record that contains both the
1104 template and the object. If there is an initializer, it will
1105 have already been converted to the right type, but we need to
1106 create the template if there is no initializer. */
1107 if (definition
1108 && !gnu_expr
1109 && TREE_CODE (gnu_type) == RECORD_TYPE
1110 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1111 /* Beware that padding might have been introduced above. */
1112 || (TYPE_PADDING_P (gnu_type)
1113 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1114 == RECORD_TYPE
1115 && TYPE_CONTAINS_TEMPLATE_P
1116 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1118 tree template_field
1119 = TYPE_PADDING_P (gnu_type)
1120 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1121 : TYPE_FIELDS (gnu_type);
1122 vec<constructor_elt, va_gc> *v;
1123 vec_alloc (v, 1);
1124 tree t = build_template (TREE_TYPE (template_field),
1125 TREE_TYPE (DECL_CHAIN (template_field)),
1126 NULL_TREE);
1127 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1128 gnu_expr = gnat_build_constructor (gnu_type, v);
1131 /* Convert the expression to the type of the object except in the
1132 case where the object's type is unconstrained or the object's type
1133 is a padded record whose field is of self-referential size. In
1134 the former case, converting will generate unnecessary evaluations
1135 of the CONSTRUCTOR to compute the size and in the latter case, we
1136 want to only copy the actual data. Also don't convert to a record
1137 type with a variant part from a record type without one, to keep
1138 the object simpler. */
1139 if (gnu_expr
1140 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1141 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1142 && !(TYPE_IS_PADDING_P (gnu_type)
1143 && CONTAINS_PLACEHOLDER_P
1144 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1145 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1146 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1147 && get_variant_part (gnu_type) != NULL_TREE
1148 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1149 gnu_expr = convert (gnu_type, gnu_expr);
1151 /* If this is a pointer that doesn't have an initializing expression,
1152 initialize it to NULL, unless the object is imported. */
1153 if (definition
1154 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1155 && !gnu_expr
1156 && !Is_Imported (gnat_entity))
1157 gnu_expr = integer_zero_node;
1159 /* If we are defining the object and it has an Address clause, we must
1160 either get the address expression from the saved GCC tree for the
1161 object if it has a Freeze node, or elaborate the address expression
1162 here since the front-end has guaranteed that the elaboration has no
1163 effects in this case. */
1164 if (definition && Present (Address_Clause (gnat_entity)))
1166 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1167 tree gnu_address
1168 = present_gnu_tree (gnat_entity)
1169 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1171 save_gnu_tree (gnat_entity, NULL_TREE, false);
1173 /* Ignore the size. It's either meaningless or was handled
1174 above. */
1175 gnu_size = NULL_TREE;
1176 /* Convert the type of the object to a reference type that can
1177 alias everything as per 13.3(19). */
1178 gnu_type
1179 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1180 gnu_address = convert (gnu_type, gnu_address);
1181 used_by_ref = true;
1182 const_flag
1183 = !Is_Public (gnat_entity)
1184 || compile_time_known_address_p (gnat_expr);
1186 /* If this is a deferred constant, the initializer is attached to
1187 the full view. */
1188 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1189 gnu_expr
1190 = gnat_to_gnu
1191 (Expression (Declaration_Node (Full_View (gnat_entity))));
1193 /* If we don't have an initializing expression for the underlying
1194 variable, the initializing expression for the pointer is the
1195 specified address. Otherwise, we have to make a COMPOUND_EXPR
1196 to assign both the address and the initial value. */
1197 if (!gnu_expr)
1198 gnu_expr = gnu_address;
1199 else
1200 gnu_expr
1201 = build2 (COMPOUND_EXPR, gnu_type,
1202 build_binary_op
1203 (MODIFY_EXPR, NULL_TREE,
1204 build_unary_op (INDIRECT_REF, NULL_TREE,
1205 gnu_address),
1206 gnu_expr),
1207 gnu_address);
1210 /* If it has an address clause and we are not defining it, mark it
1211 as an indirect object. Likewise for Stdcall objects that are
1212 imported. */
1213 if ((!definition && Present (Address_Clause (gnat_entity)))
1214 || (Is_Imported (gnat_entity)
1215 && Has_Stdcall_Convention (gnat_entity)))
1217 /* Convert the type of the object to a reference type that can
1218 alias everything as per 13.3(19). */
1219 gnu_type
1220 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1221 gnu_size = NULL_TREE;
1223 /* No point in taking the address of an initializing expression
1224 that isn't going to be used. */
1225 gnu_expr = NULL_TREE;
1227 /* If it has an address clause whose value is known at compile
1228 time, make the object a CONST_DECL. This will avoid a
1229 useless dereference. */
1230 if (Present (Address_Clause (gnat_entity)))
1232 Node_Id gnat_address
1233 = Expression (Address_Clause (gnat_entity));
1235 if (compile_time_known_address_p (gnat_address))
1237 gnu_expr = gnat_to_gnu (gnat_address);
1238 const_flag = true;
1242 used_by_ref = true;
1245 /* If we are at top level and this object is of variable size,
1246 make the actual type a hidden pointer to the real type and
1247 make the initializer be a memory allocation and initialization.
1248 Likewise for objects we aren't defining (presumed to be
1249 external references from other packages), but there we do
1250 not set up an initialization.
1252 If the object's size overflows, make an allocator too, so that
1253 Storage_Error gets raised. Note that we will never free
1254 such memory, so we presume it never will get allocated. */
1255 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1256 global_bindings_p ()
1257 || !definition
1258 || static_p)
1259 || (gnu_size
1260 && !allocatable_size_p (convert (sizetype,
1261 size_binop
1262 (CEIL_DIV_EXPR, gnu_size,
1263 bitsize_unit_node)),
1264 global_bindings_p ()
1265 || !definition
1266 || static_p)))
1268 gnu_type = build_reference_type (gnu_type);
1269 gnu_size = NULL_TREE;
1270 used_by_ref = true;
1272 /* In case this was a aliased object whose nominal subtype is
1273 unconstrained, the pointer above will be a thin pointer and
1274 build_allocator will automatically make the template.
1276 If we have a template initializer only (that we made above),
1277 pretend there is none and rely on what build_allocator creates
1278 again anyway. Otherwise (if we have a full initializer), get
1279 the data part and feed that to build_allocator.
1281 If we are elaborating a mutable object, tell build_allocator to
1282 ignore a possibly simpler size from the initializer, if any, as
1283 we must allocate the maximum possible size in this case. */
1284 if (definition && !imported_p)
1286 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1288 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1289 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1291 gnu_alloc_type
1292 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1294 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1295 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1296 gnu_expr = 0;
1297 else
1298 gnu_expr
1299 = build_component_ref
1300 (gnu_expr, NULL_TREE,
1301 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1302 false);
1305 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1306 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1307 post_error ("?`Storage_Error` will be raised at run time!",
1308 gnat_entity);
1310 gnu_expr
1311 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1312 Empty, Empty, gnat_entity, mutable_p);
1313 const_flag = true;
1315 else
1317 gnu_expr = NULL_TREE;
1318 const_flag = false;
1322 /* If this object would go into the stack and has an alignment larger
1323 than the largest stack alignment the back-end can honor, resort to
1324 a variable of "aligning type". */
1325 if (!global_bindings_p () && !static_p && definition
1326 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1328 /* Create the new variable. No need for extra room before the
1329 aligned field as this is in automatic storage. */
1330 tree gnu_new_type
1331 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1332 TYPE_SIZE_UNIT (gnu_type),
1333 BIGGEST_ALIGNMENT, 0, gnat_entity);
1334 tree gnu_new_var
1335 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1336 NULL_TREE, gnu_new_type, NULL_TREE, false,
1337 false, false, false, NULL, gnat_entity);
1339 /* Initialize the aligned field if we have an initializer. */
1340 if (gnu_expr)
1341 add_stmt_with_node
1342 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1343 build_component_ref
1344 (gnu_new_var, NULL_TREE,
1345 TYPE_FIELDS (gnu_new_type), false),
1346 gnu_expr),
1347 gnat_entity);
1349 /* And setup this entity as a reference to the aligned field. */
1350 gnu_type = build_reference_type (gnu_type);
1351 gnu_expr
1352 = build_unary_op
1353 (ADDR_EXPR, gnu_type,
1354 build_component_ref (gnu_new_var, NULL_TREE,
1355 TYPE_FIELDS (gnu_new_type), false));
1357 gnu_size = NULL_TREE;
1358 used_by_ref = true;
1359 const_flag = true;
1362 /* If this is an aliased object with an unconstrained nominal subtype,
1363 we make its type a thin reference, i.e. the reference counterpart
1364 of a thin pointer, so that it points to the array part. This is
1365 aimed at making it easier for the debugger to decode the object.
1366 Note that we have to do that this late because of the couple of
1367 allocation adjustments that might be made just above. */
1368 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1369 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1370 && !type_annotate_only)
1372 tree gnu_array
1373 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1375 /* In case the object with the template has already been allocated
1376 just above, we have nothing to do here. */
1377 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1379 tree gnu_unc_var
1380 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1381 NULL_TREE, gnu_type, gnu_expr,
1382 const_flag, Is_Public (gnat_entity),
1383 imported_p || !definition, static_p,
1384 NULL, gnat_entity);
1385 gnu_expr
1386 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1387 TREE_CONSTANT (gnu_expr) = 1;
1389 gnu_size = NULL_TREE;
1390 used_by_ref = true;
1391 const_flag = true;
1394 gnu_type
1395 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1398 if (const_flag)
1399 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1401 /* Convert the expression to the type of the object except in the
1402 case where the object's type is unconstrained or the object's type
1403 is a padded record whose field is of self-referential size. In
1404 the former case, converting will generate unnecessary evaluations
1405 of the CONSTRUCTOR to compute the size and in the latter case, we
1406 want to only copy the actual data. Also don't convert to a record
1407 type with a variant part from a record type without one, to keep
1408 the object simpler. */
1409 if (gnu_expr
1410 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1411 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1412 && !(TYPE_IS_PADDING_P (gnu_type)
1413 && CONTAINS_PLACEHOLDER_P
1414 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1415 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1416 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1417 && get_variant_part (gnu_type) != NULL_TREE
1418 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1419 gnu_expr = convert (gnu_type, gnu_expr);
1421 /* If this name is external or a name was specified, use it, but don't
1422 use the Interface_Name with an address clause (see cd30005). */
1423 if ((Present (Interface_Name (gnat_entity))
1424 && No (Address_Clause (gnat_entity)))
1425 || (Is_Public (gnat_entity)
1426 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1427 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1429 /* If this is an aggregate constant initialized to a constant, force it
1430 to be statically allocated. This saves an initialization copy. */
1431 if (!static_p
1432 && const_flag
1433 && gnu_expr && TREE_CONSTANT (gnu_expr)
1434 && AGGREGATE_TYPE_P (gnu_type)
1435 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1436 && !(TYPE_IS_PADDING_P (gnu_type)
1437 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1438 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1439 static_p = true;
1441 /* Deal with a pragma Linker_Section on a constant or variable. */
1442 if ((kind == E_Constant || kind == E_Variable)
1443 && Present (Linker_Section_Pragma (gnat_entity)))
1444 prepend_one_attribute_pragma (&attr_list,
1445 Linker_Section_Pragma (gnat_entity));
1447 /* Now create the variable or the constant and set various flags. */
1448 gnu_decl
1449 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1450 gnu_expr, const_flag, Is_Public (gnat_entity),
1451 imported_p || !definition, static_p,
1452 !renamed_obj, attr_list, gnat_entity);
1453 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1454 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1455 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1457 /* If we are defining an Out parameter and optimization isn't enabled,
1458 create a fake PARM_DECL for debugging purposes and make it point to
1459 the VAR_DECL. Suppress debug info for the latter but make sure it
1460 will live in memory so that it can be accessed from within the
1461 debugger through the PARM_DECL. */
1462 if (kind == E_Out_Parameter
1463 && definition
1464 && debug_info_p
1465 && !optimize
1466 && !flag_generate_lto)
1468 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1469 gnat_pushdecl (param, gnat_entity);
1470 SET_DECL_VALUE_EXPR (param, gnu_decl);
1471 DECL_HAS_VALUE_EXPR_P (param) = 1;
1472 DECL_IGNORED_P (gnu_decl) = 1;
1473 TREE_ADDRESSABLE (gnu_decl) = 1;
1476 /* If this is a loop parameter, set the corresponding flag. */
1477 else if (kind == E_Loop_Parameter)
1478 DECL_LOOP_PARM_P (gnu_decl) = 1;
1480 /* If this is a renaming pointer, attach the renamed object to it and
1481 register it if we are at the global level. Note that an external
1482 constant is at the global level. */
1483 if (renamed_obj)
1485 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1486 if ((!definition && kind == E_Constant) || global_bindings_p ())
1488 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1489 record_global_renaming_pointer (gnu_decl);
1493 /* If this is a constant and we are defining it or it generates a real
1494 symbol at the object level and we are referencing it, we may want
1495 or need to have a true variable to represent it:
1496 - if optimization isn't enabled, for debugging purposes,
1497 - if the constant is public and not overlaid on something else,
1498 - if its address is taken,
1499 - if either itself or its type is aliased. */
1500 if (TREE_CODE (gnu_decl) == CONST_DECL
1501 && (definition || Sloc (gnat_entity) > Standard_Location)
1502 && ((!optimize && debug_info_p)
1503 || (Is_Public (gnat_entity)
1504 && No (Address_Clause (gnat_entity)))
1505 || Address_Taken (gnat_entity)
1506 || Is_Aliased (gnat_entity)
1507 || Is_Aliased (Etype (gnat_entity))))
1509 tree gnu_corr_var
1510 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1511 gnu_expr, true, Is_Public (gnat_entity),
1512 !definition, static_p, attr_list,
1513 gnat_entity);
1515 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1517 /* As debugging information will be generated for the variable,
1518 do not generate debugging information for the constant. */
1519 if (debug_info_p)
1520 DECL_IGNORED_P (gnu_decl) = 1;
1521 else
1522 DECL_IGNORED_P (gnu_corr_var) = 1;
1525 /* If this is a constant, even if we don't need a true variable, we
1526 may need to avoid returning the initializer in every case. That
1527 can happen for the address of a (constant) constructor because,
1528 upon dereferencing it, the constructor will be reinjected in the
1529 tree, which may not be valid in every case; see lvalue_required_p
1530 for more details. */
1531 if (TREE_CODE (gnu_decl) == CONST_DECL)
1532 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1534 /* If this object is declared in a block that contains a block with an
1535 exception handler, and we aren't using the GCC exception mechanism,
1536 we must force this variable in memory in order to avoid an invalid
1537 optimization. */
1538 if (Exception_Mechanism != Back_End_Exceptions
1539 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1540 TREE_ADDRESSABLE (gnu_decl) = 1;
1542 /* If this is a local variable with non-BLKmode and aggregate type,
1543 and optimization isn't enabled, then force it in memory so that
1544 a register won't be allocated to it with possible subparts left
1545 uninitialized and reaching the register allocator. */
1546 else if (TREE_CODE (gnu_decl) == VAR_DECL
1547 && !DECL_EXTERNAL (gnu_decl)
1548 && !TREE_STATIC (gnu_decl)
1549 && DECL_MODE (gnu_decl) != BLKmode
1550 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1551 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1552 && !optimize)
1553 TREE_ADDRESSABLE (gnu_decl) = 1;
1555 /* If we are defining an object with variable size or an object with
1556 fixed size that will be dynamically allocated, and we are using the
1557 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1558 if (definition
1559 && Exception_Mechanism == Setjmp_Longjmp
1560 && get_block_jmpbuf_decl ()
1561 && DECL_SIZE_UNIT (gnu_decl)
1562 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1563 || (flag_stack_check == GENERIC_STACK_CHECK
1564 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1565 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1566 add_stmt_with_node (build_call_n_expr
1567 (update_setjmp_buf_decl, 1,
1568 build_unary_op (ADDR_EXPR, NULL_TREE,
1569 get_block_jmpbuf_decl ())),
1570 gnat_entity);
1572 /* Back-annotate Esize and Alignment of the object if not already
1573 known. Note that we pick the values of the type, not those of
1574 the object, to shield ourselves from low-level platform-dependent
1575 adjustments like alignment promotion. This is both consistent with
1576 all the treatment above, where alignment and size are set on the
1577 type of the object and not on the object directly, and makes it
1578 possible to support all confirming representation clauses. */
1579 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1580 used_by_ref);
1582 break;
1584 case E_Void:
1585 /* Return a TYPE_DECL for "void" that we previously made. */
1586 gnu_decl = TYPE_NAME (void_type_node);
1587 break;
1589 case E_Enumeration_Type:
1590 /* A special case: for the types Character and Wide_Character in
1591 Standard, we do not list all the literals. So if the literals
1592 are not specified, make this an unsigned integer type. */
1593 if (No (First_Literal (gnat_entity)))
1595 gnu_type = make_unsigned_type (esize);
1596 TYPE_NAME (gnu_type) = gnu_entity_name;
1598 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1599 This is needed by the DWARF-2 back-end to distinguish between
1600 unsigned integer types and character types. */
1601 TYPE_STRING_FLAG (gnu_type) = 1;
1603 else
1605 /* We have a list of enumeral constants in First_Literal. We make a
1606 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1607 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1608 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1609 value of the literal. But when we have a regular boolean type, we
1610 simplify this a little by using a BOOLEAN_TYPE. */
1611 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1612 && !Has_Non_Standard_Rep (gnat_entity);
1613 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1614 tree gnu_list = NULL_TREE;
1615 Entity_Id gnat_literal;
1617 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1618 TYPE_PRECISION (gnu_type) = esize;
1619 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1620 set_min_and_max_values_for_integral_type (gnu_type, esize,
1621 TYPE_SIGN (gnu_type));
1622 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1623 layout_type (gnu_type);
1625 for (gnat_literal = First_Literal (gnat_entity);
1626 Present (gnat_literal);
1627 gnat_literal = Next_Literal (gnat_literal))
1629 tree gnu_value
1630 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1631 tree gnu_literal
1632 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1633 gnu_type, gnu_value, true, false, false,
1634 false, NULL, gnat_literal);
1635 /* Do not generate debug info for individual enumerators. */
1636 DECL_IGNORED_P (gnu_literal) = 1;
1637 save_gnu_tree (gnat_literal, gnu_literal, false);
1638 gnu_list
1639 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1642 if (!is_boolean)
1643 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1645 /* Note that the bounds are updated at the end of this function
1646 to avoid an infinite recursion since they refer to the type. */
1647 goto discrete_type;
1649 break;
1651 case E_Signed_Integer_Type:
1652 case E_Ordinary_Fixed_Point_Type:
1653 case E_Decimal_Fixed_Point_Type:
1654 /* For integer types, just make a signed type the appropriate number
1655 of bits. */
1656 gnu_type = make_signed_type (esize);
1657 goto discrete_type;
1659 case E_Modular_Integer_Type:
1661 /* For modular types, make the unsigned type of the proper number
1662 of bits and then set up the modulus, if required. */
1663 tree gnu_modulus, gnu_high = NULL_TREE;
1665 /* Packed Array Impl. Types are supposed to be subtypes only. */
1666 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1668 gnu_type = make_unsigned_type (esize);
1670 /* Get the modulus in this type. If it overflows, assume it is because
1671 it is equal to 2**Esize. Note that there is no overflow checking
1672 done on unsigned type, so we detect the overflow by looking for
1673 a modulus of zero, which is otherwise invalid. */
1674 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1676 if (!integer_zerop (gnu_modulus))
1678 TYPE_MODULAR_P (gnu_type) = 1;
1679 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1680 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1681 convert (gnu_type, integer_one_node));
1684 /* If the upper bound is not maximal, make an extra subtype. */
1685 if (gnu_high
1686 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1688 tree gnu_subtype = make_unsigned_type (esize);
1689 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1690 TREE_TYPE (gnu_subtype) = gnu_type;
1691 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1692 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1693 gnu_type = gnu_subtype;
1696 goto discrete_type;
1698 case E_Signed_Integer_Subtype:
1699 case E_Enumeration_Subtype:
1700 case E_Modular_Integer_Subtype:
1701 case E_Ordinary_Fixed_Point_Subtype:
1702 case E_Decimal_Fixed_Point_Subtype:
1704 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1705 not want to call create_range_type since we would like each subtype
1706 node to be distinct. ??? Historically this was in preparation for
1707 when memory aliasing is implemented, but that's obsolete now given
1708 the call to relate_alias_sets below.
1710 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1711 this fact is used by the arithmetic conversion functions.
1713 We elaborate the Ancestor_Subtype if it is not in the current unit
1714 and one of our bounds is non-static. We do this to ensure consistent
1715 naming in the case where several subtypes share the same bounds, by
1716 elaborating the first such subtype first, thus using its name. */
1718 if (!definition
1719 && Present (Ancestor_Subtype (gnat_entity))
1720 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1721 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1722 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1723 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1725 /* Set the precision to the Esize except for bit-packed arrays. */
1726 if (Is_Packed_Array_Impl_Type (gnat_entity)
1727 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1728 esize = UI_To_Int (RM_Size (gnat_entity));
1730 /* This should be an unsigned type if the base type is unsigned or
1731 if the lower bound is constant and non-negative or if the type
1732 is biased. */
1733 if (Is_Unsigned_Type (Etype (gnat_entity))
1734 || Is_Unsigned_Type (gnat_entity)
1735 || Has_Biased_Representation (gnat_entity))
1736 gnu_type = make_unsigned_type (esize);
1737 else
1738 gnu_type = make_signed_type (esize);
1739 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1741 SET_TYPE_RM_MIN_VALUE
1742 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1743 gnat_entity, get_identifier ("L"),
1744 definition, true,
1745 Needs_Debug_Info (gnat_entity)));
1747 SET_TYPE_RM_MAX_VALUE
1748 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1749 gnat_entity, get_identifier ("U"),
1750 definition, true,
1751 Needs_Debug_Info (gnat_entity)));
1753 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1754 = Has_Biased_Representation (gnat_entity);
1756 /* Inherit our alias set from what we're a subtype of. Subtypes
1757 are not different types and a pointer can designate any instance
1758 within a subtype hierarchy. */
1759 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1761 /* One of the above calls might have caused us to be elaborated,
1762 so don't blow up if so. */
1763 if (present_gnu_tree (gnat_entity))
1765 maybe_present = true;
1766 break;
1769 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1770 TYPE_STUB_DECL (gnu_type)
1771 = create_type_stub_decl (gnu_entity_name, gnu_type);
1773 /* For a packed array, make the original array type a parallel type. */
1774 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1775 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1777 discrete_type:
1779 /* We have to handle clauses that under-align the type specially. */
1780 if ((Present (Alignment_Clause (gnat_entity))
1781 || (Is_Packed_Array_Impl_Type (gnat_entity)
1782 && Present
1783 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1784 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1786 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1787 if (align >= TYPE_ALIGN (gnu_type))
1788 align = 0;
1791 /* If the type we are dealing with represents a bit-packed array,
1792 we need to have the bits left justified on big-endian targets
1793 and right justified on little-endian targets. We also need to
1794 ensure that when the value is read (e.g. for comparison of two
1795 such values), we only get the good bits, since the unused bits
1796 are uninitialized. Both goals are accomplished by wrapping up
1797 the modular type in an enclosing record type. */
1798 if (Is_Packed_Array_Impl_Type (gnat_entity)
1799 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1801 tree gnu_field_type, gnu_field;
1803 /* Set the RM size before wrapping up the original type. */
1804 SET_TYPE_RM_SIZE (gnu_type,
1805 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1806 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1808 /* Create a stripped-down declaration, mainly for debugging. */
1809 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1810 gnat_entity);
1812 /* Now save it and build the enclosing record type. */
1813 gnu_field_type = gnu_type;
1815 gnu_type = make_node (RECORD_TYPE);
1816 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1817 TYPE_PACKED (gnu_type) = 1;
1818 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1819 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1820 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1822 /* Propagate the alignment of the modular type to the record type,
1823 unless there is an alignment clause that under-aligns the type.
1824 This means that bit-packed arrays are given "ceil" alignment for
1825 their size by default, which may seem counter-intuitive but makes
1826 it possible to overlay them on modular types easily. */
1827 TYPE_ALIGN (gnu_type)
1828 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1830 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1832 /* Don't declare the field as addressable since we won't be taking
1833 its address and this would prevent create_field_decl from making
1834 a bitfield. */
1835 gnu_field
1836 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1837 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1839 /* Do not emit debug info until after the parallel type is added. */
1840 finish_record_type (gnu_type, gnu_field, 2, false);
1841 compute_record_mode (gnu_type);
1842 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1844 if (debug_info_p)
1846 /* Make the original array type a parallel type. */
1847 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1849 rest_of_record_type_compilation (gnu_type);
1853 /* If the type we are dealing with has got a smaller alignment than the
1854 natural one, we need to wrap it up in a record type and misalign the
1855 latter; we reuse the padding machinery for this purpose. Note that,
1856 even if the record type is marked as packed because of misalignment,
1857 we don't pack the field so as to give it the size of the type. */
1858 else if (align > 0)
1860 tree gnu_field_type, gnu_field;
1862 /* Set the RM size before wrapping up the type. */
1863 SET_TYPE_RM_SIZE (gnu_type,
1864 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1866 /* Create a stripped-down declaration, mainly for debugging. */
1867 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1868 gnat_entity);
1870 /* Now save it and build the enclosing record type. */
1871 gnu_field_type = gnu_type;
1873 gnu_type = make_node (RECORD_TYPE);
1874 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1875 TYPE_PACKED (gnu_type) = 1;
1876 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1877 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1878 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1879 TYPE_ALIGN (gnu_type) = align;
1880 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1882 /* Don't declare the field as addressable since we won't be taking
1883 its address and this would prevent create_field_decl from making
1884 a bitfield. */
1885 gnu_field
1886 = create_field_decl (get_identifier ("F"), gnu_field_type,
1887 gnu_type, TYPE_SIZE (gnu_field_type),
1888 bitsize_zero_node, 0, 0);
1890 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1891 compute_record_mode (gnu_type);
1892 TYPE_PADDING_P (gnu_type) = 1;
1895 break;
1897 case E_Floating_Point_Type:
1898 /* The type of the Low and High bounds can be our type if this is
1899 a type from Standard, so set them at the end of the function. */
1900 gnu_type = make_node (REAL_TYPE);
1901 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1902 layout_type (gnu_type);
1903 break;
1905 case E_Floating_Point_Subtype:
1906 /* See the E_Signed_Integer_Subtype case for the rationale. */
1907 if (!definition
1908 && Present (Ancestor_Subtype (gnat_entity))
1909 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1910 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1911 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1912 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1914 gnu_type = make_node (REAL_TYPE);
1915 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1916 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1917 TYPE_GCC_MIN_VALUE (gnu_type)
1918 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1919 TYPE_GCC_MAX_VALUE (gnu_type)
1920 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1921 layout_type (gnu_type);
1923 SET_TYPE_RM_MIN_VALUE
1924 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1925 gnat_entity, get_identifier ("L"),
1926 definition, true,
1927 Needs_Debug_Info (gnat_entity)));
1929 SET_TYPE_RM_MAX_VALUE
1930 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1931 gnat_entity, get_identifier ("U"),
1932 definition, true,
1933 Needs_Debug_Info (gnat_entity)));
1935 /* Inherit our alias set from what we're a subtype of, as for
1936 integer subtypes. */
1937 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1939 /* One of the above calls might have caused us to be elaborated,
1940 so don't blow up if so. */
1941 maybe_present = true;
1942 break;
1944 /* Array Types and Subtypes
1946 Unconstrained array types are represented by E_Array_Type and
1947 constrained array types are represented by E_Array_Subtype. There
1948 are no actual objects of an unconstrained array type; all we have
1949 are pointers to that type.
1951 The following fields are defined on array types and subtypes:
1953 Component_Type Component type of the array.
1954 Number_Dimensions Number of dimensions (an int).
1955 First_Index Type of first index. */
1957 case E_Array_Type:
1959 const bool convention_fortran_p
1960 = (Convention (gnat_entity) == Convention_Fortran);
1961 const int ndim = Number_Dimensions (gnat_entity);
1962 tree gnu_template_type;
1963 tree gnu_ptr_template;
1964 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1965 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1966 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1967 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1968 Entity_Id gnat_index, gnat_name;
1969 int index;
1970 tree comp_type;
1972 /* Create the type for the component now, as it simplifies breaking
1973 type reference loops. */
1974 comp_type
1975 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
1976 if (present_gnu_tree (gnat_entity))
1978 /* As a side effect, the type may have been translated. */
1979 maybe_present = true;
1980 break;
1983 /* We complete an existing dummy fat pointer type in place. This both
1984 avoids further complex adjustments in update_pointer_to and yields
1985 better debugging information in DWARF by leveraging the support for
1986 incomplete declarations of "tagged" types in the DWARF back-end. */
1987 gnu_type = get_dummy_type (gnat_entity);
1988 if (gnu_type && TYPE_POINTER_TO (gnu_type))
1990 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
1991 TYPE_NAME (gnu_fat_type) = NULL_TREE;
1992 /* Save the contents of the dummy type for update_pointer_to. */
1993 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
1994 gnu_ptr_template =
1995 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1996 gnu_template_type = TREE_TYPE (gnu_ptr_template);
1998 else
2000 gnu_fat_type = make_node (RECORD_TYPE);
2001 gnu_template_type = make_node (RECORD_TYPE);
2002 gnu_ptr_template = build_pointer_type (gnu_template_type);
2005 /* Make a node for the array. If we are not defining the array
2006 suppress expanding incomplete types. */
2007 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2009 if (!definition)
2011 defer_incomplete_level++;
2012 this_deferred = true;
2015 /* Build the fat pointer type. Use a "void *" object instead of
2016 a pointer to the array type since we don't have the array type
2017 yet (it will reference the fat pointer via the bounds). */
2019 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2020 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2021 DECL_CHAIN (tem)
2022 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2023 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2025 if (COMPLETE_TYPE_P (gnu_fat_type))
2027 /* We are going to lay it out again so reset the alias set. */
2028 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2029 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2030 finish_fat_pointer_type (gnu_fat_type, tem);
2031 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2032 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2034 TYPE_FIELDS (t) = tem;
2035 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2038 else
2040 finish_fat_pointer_type (gnu_fat_type, tem);
2041 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2044 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2045 is the fat pointer. This will be used to access the individual
2046 fields once we build them. */
2047 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2048 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2049 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2050 gnu_template_reference
2051 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2052 TREE_READONLY (gnu_template_reference) = 1;
2053 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2055 /* Now create the GCC type for each index and add the fields for that
2056 index to the template. */
2057 for (index = (convention_fortran_p ? ndim - 1 : 0),
2058 gnat_index = First_Index (gnat_entity);
2059 0 <= index && index < ndim;
2060 index += (convention_fortran_p ? - 1 : 1),
2061 gnat_index = Next_Index (gnat_index))
2063 char field_name[16];
2064 tree gnu_index_base_type
2065 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2066 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2067 tree gnu_min, gnu_max, gnu_high;
2069 /* Make the FIELD_DECLs for the low and high bounds of this
2070 type and then make extractions of these fields from the
2071 template. */
2072 sprintf (field_name, "LB%d", index);
2073 gnu_lb_field = create_field_decl (get_identifier (field_name),
2074 gnu_index_base_type,
2075 gnu_template_type, NULL_TREE,
2076 NULL_TREE, 0, 0);
2077 Sloc_to_locus (Sloc (gnat_entity),
2078 &DECL_SOURCE_LOCATION (gnu_lb_field));
2080 field_name[0] = 'U';
2081 gnu_hb_field = create_field_decl (get_identifier (field_name),
2082 gnu_index_base_type,
2083 gnu_template_type, NULL_TREE,
2084 NULL_TREE, 0, 0);
2085 Sloc_to_locus (Sloc (gnat_entity),
2086 &DECL_SOURCE_LOCATION (gnu_hb_field));
2088 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2090 /* We can't use build_component_ref here since the template type
2091 isn't complete yet. */
2092 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2093 gnu_template_reference, gnu_lb_field,
2094 NULL_TREE);
2095 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2096 gnu_template_reference, gnu_hb_field,
2097 NULL_TREE);
2098 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2100 gnu_min = convert (sizetype, gnu_orig_min);
2101 gnu_max = convert (sizetype, gnu_orig_max);
2103 /* Compute the size of this dimension. See the E_Array_Subtype
2104 case below for the rationale. */
2105 gnu_high
2106 = build3 (COND_EXPR, sizetype,
2107 build2 (GE_EXPR, boolean_type_node,
2108 gnu_orig_max, gnu_orig_min),
2109 gnu_max,
2110 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2112 /* Make a range type with the new range in the Ada base type.
2113 Then make an index type with the size range in sizetype. */
2114 gnu_index_types[index]
2115 = create_index_type (gnu_min, gnu_high,
2116 create_range_type (gnu_index_base_type,
2117 gnu_orig_min,
2118 gnu_orig_max),
2119 gnat_entity);
2121 /* Update the maximum size of the array in elements. */
2122 if (gnu_max_size)
2124 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2125 tree gnu_min
2126 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2127 tree gnu_max
2128 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2129 tree gnu_this_max
2130 = size_binop (PLUS_EXPR, size_one_node,
2131 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2133 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2134 && TREE_OVERFLOW (gnu_this_max))
2135 gnu_max_size = NULL_TREE;
2136 else
2137 gnu_max_size
2138 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2141 TYPE_NAME (gnu_index_types[index])
2142 = create_concat_name (gnat_entity, field_name);
2145 /* Install all the fields into the template. */
2146 TYPE_NAME (gnu_template_type)
2147 = create_concat_name (gnat_entity, "XUB");
2148 gnu_template_fields = NULL_TREE;
2149 for (index = 0; index < ndim; index++)
2150 gnu_template_fields
2151 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2152 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2153 debug_info_p);
2154 TYPE_READONLY (gnu_template_type) = 1;
2156 /* If Component_Size is not already specified, annotate it with the
2157 size of the component. */
2158 if (Unknown_Component_Size (gnat_entity))
2159 Set_Component_Size (gnat_entity,
2160 annotate_value (TYPE_SIZE (comp_type)));
2162 /* Compute the maximum size of the array in units and bits. */
2163 if (gnu_max_size)
2165 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2166 TYPE_SIZE_UNIT (comp_type));
2167 gnu_max_size = size_binop (MULT_EXPR,
2168 convert (bitsizetype, gnu_max_size),
2169 TYPE_SIZE (comp_type));
2171 else
2172 gnu_max_size_unit = NULL_TREE;
2174 /* Now build the array type. */
2175 tem = comp_type;
2176 for (index = ndim - 1; index >= 0; index--)
2178 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2179 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2180 sorry ("non-default Scalar_Storage_Order");
2181 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2182 if (array_type_has_nonaliased_component (tem, gnat_entity))
2183 TYPE_NONALIASED_COMPONENT (tem) = 1;
2185 /* If it is passed by reference, force BLKmode to ensure that
2186 objects of this type will always be put in memory. */
2187 if (TYPE_MODE (tem) != BLKmode
2188 && Is_By_Reference_Type (gnat_entity))
2189 SET_TYPE_MODE (tem, BLKmode);
2192 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2194 /* If an alignment is specified, use it if valid. But ignore it
2195 for the original type of packed array types. If the alignment
2196 was requested with an explicit alignment clause, state so. */
2197 if (No (Packed_Array_Impl_Type (gnat_entity))
2198 && Known_Alignment (gnat_entity))
2200 TYPE_ALIGN (tem)
2201 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2202 TYPE_ALIGN (tem));
2203 if (Present (Alignment_Clause (gnat_entity)))
2204 TYPE_USER_ALIGN (tem) = 1;
2207 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2209 /* Adjust the type of the pointer-to-array field of the fat pointer
2210 and record the aliasing relationships if necessary. */
2211 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2212 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2213 record_component_aliases (gnu_fat_type);
2215 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2216 corresponding fat pointer. */
2217 TREE_TYPE (gnu_type) = gnu_fat_type;
2218 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2219 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2220 SET_TYPE_MODE (gnu_type, BLKmode);
2221 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2223 /* If the maximum size doesn't overflow, use it. */
2224 if (gnu_max_size
2225 && TREE_CODE (gnu_max_size) == INTEGER_CST
2226 && !TREE_OVERFLOW (gnu_max_size)
2227 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2228 && !TREE_OVERFLOW (gnu_max_size_unit))
2230 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2231 TYPE_SIZE (tem));
2232 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2233 TYPE_SIZE_UNIT (tem));
2236 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2237 !Comes_From_Source (gnat_entity), debug_info_p,
2238 gnat_entity);
2240 /* Give the fat pointer type a name. If this is a packed array, tell
2241 the debugger how to interpret the underlying bits. */
2242 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2243 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2244 else
2245 gnat_name = gnat_entity;
2246 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2247 !Comes_From_Source (gnat_entity), debug_info_p,
2248 gnat_entity);
2250 /* Create the type to be designated by thin pointers: a record type for
2251 the array and its template. We used to shift the fields to have the
2252 template at a negative offset, but this was somewhat of a kludge; we
2253 now shift thin pointer values explicitly but only those which have a
2254 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2255 tem = build_unc_object_type (gnu_template_type, tem,
2256 create_concat_name (gnat_name, "XUT"),
2257 debug_info_p);
2259 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2260 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2262 break;
2264 case E_Array_Subtype:
2266 /* This is the actual data type for array variables. Multidimensional
2267 arrays are implemented as arrays of arrays. Note that arrays which
2268 have sparse enumeration subtypes as index components create sparse
2269 arrays, which is obviously space inefficient but so much easier to
2270 code for now.
2272 Also note that the subtype never refers to the unconstrained array
2273 type, which is somewhat at variance with Ada semantics.
2275 First check to see if this is simply a renaming of the array type.
2276 If so, the result is the array type. */
2278 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2279 if (!Is_Constrained (gnat_entity))
2281 else
2283 Entity_Id gnat_index, gnat_base_index;
2284 const bool convention_fortran_p
2285 = (Convention (gnat_entity) == Convention_Fortran);
2286 const int ndim = Number_Dimensions (gnat_entity);
2287 tree gnu_base_type = gnu_type;
2288 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2289 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2290 bool need_index_type_struct = false;
2291 int index;
2293 /* First create the GCC type for each index and find out whether
2294 special types are needed for debugging information. */
2295 for (index = (convention_fortran_p ? ndim - 1 : 0),
2296 gnat_index = First_Index (gnat_entity),
2297 gnat_base_index
2298 = First_Index (Implementation_Base_Type (gnat_entity));
2299 0 <= index && index < ndim;
2300 index += (convention_fortran_p ? - 1 : 1),
2301 gnat_index = Next_Index (gnat_index),
2302 gnat_base_index = Next_Index (gnat_base_index))
2304 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2305 tree gnu_index_base_type = get_base_type (gnu_index_type);
2306 tree gnu_orig_min
2307 = convert (gnu_index_base_type,
2308 TYPE_MIN_VALUE (gnu_index_type));
2309 tree gnu_orig_max
2310 = convert (gnu_index_base_type,
2311 TYPE_MAX_VALUE (gnu_index_type));
2312 tree gnu_min = convert (sizetype, gnu_orig_min);
2313 tree gnu_max = convert (sizetype, gnu_orig_max);
2314 tree gnu_base_index_type
2315 = get_unpadded_type (Etype (gnat_base_index));
2316 tree gnu_base_index_base_type
2317 = get_base_type (gnu_base_index_type);
2318 tree gnu_base_orig_min
2319 = convert (gnu_base_index_base_type,
2320 TYPE_MIN_VALUE (gnu_base_index_type));
2321 tree gnu_base_orig_max
2322 = convert (gnu_base_index_base_type,
2323 TYPE_MAX_VALUE (gnu_base_index_type));
2324 tree gnu_high;
2326 /* See if the base array type is already flat. If it is, we
2327 are probably compiling an ACATS test but it will cause the
2328 code below to malfunction if we don't handle it specially. */
2329 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2330 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2331 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2333 gnu_min = size_one_node;
2334 gnu_max = size_zero_node;
2335 gnu_high = gnu_max;
2338 /* Similarly, if one of the values overflows in sizetype and the
2339 range is null, use 1..0 for the sizetype bounds. */
2340 else if (TREE_CODE (gnu_min) == INTEGER_CST
2341 && TREE_CODE (gnu_max) == INTEGER_CST
2342 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2343 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2345 gnu_min = size_one_node;
2346 gnu_max = size_zero_node;
2347 gnu_high = gnu_max;
2350 /* If the minimum and maximum values both overflow in sizetype,
2351 but the difference in the original type does not overflow in
2352 sizetype, ignore the overflow indication. */
2353 else if (TREE_CODE (gnu_min) == INTEGER_CST
2354 && TREE_CODE (gnu_max) == INTEGER_CST
2355 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2356 && !TREE_OVERFLOW
2357 (convert (sizetype,
2358 fold_build2 (MINUS_EXPR, gnu_index_type,
2359 gnu_orig_max,
2360 gnu_orig_min))))
2362 TREE_OVERFLOW (gnu_min) = 0;
2363 TREE_OVERFLOW (gnu_max) = 0;
2364 gnu_high = gnu_max;
2367 /* Compute the size of this dimension in the general case. We
2368 need to provide GCC with an upper bound to use but have to
2369 deal with the "superflat" case. There are three ways to do
2370 this. If we can prove that the array can never be superflat,
2371 we can just use the high bound of the index type. */
2372 else if ((Nkind (gnat_index) == N_Range
2373 && cannot_be_superflat_p (gnat_index))
2374 /* Bit-Packed Array Impl. Types are never superflat. */
2375 || (Is_Packed_Array_Impl_Type (gnat_entity)
2376 && Is_Bit_Packed_Array
2377 (Original_Array_Type (gnat_entity))))
2378 gnu_high = gnu_max;
2380 /* Otherwise, if the high bound is constant but the low bound is
2381 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2382 lower bound. Note that the comparison must be done in the
2383 original type to avoid any overflow during the conversion. */
2384 else if (TREE_CODE (gnu_max) == INTEGER_CST
2385 && TREE_CODE (gnu_min) != INTEGER_CST)
2387 gnu_high = gnu_max;
2388 gnu_min
2389 = build_cond_expr (sizetype,
2390 build_binary_op (GE_EXPR,
2391 boolean_type_node,
2392 gnu_orig_max,
2393 gnu_orig_min),
2394 gnu_min,
2395 int_const_binop (PLUS_EXPR, gnu_max,
2396 size_one_node));
2399 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2400 in all the other cases. Note that, here as well as above,
2401 the condition used in the comparison must be equivalent to
2402 the condition (length != 0). This is relied upon in order
2403 to optimize array comparisons in compare_arrays. Moreover
2404 we use int_const_binop for the shift by 1 if the bound is
2405 constant to avoid any unwanted overflow. */
2406 else
2407 gnu_high
2408 = build_cond_expr (sizetype,
2409 build_binary_op (GE_EXPR,
2410 boolean_type_node,
2411 gnu_orig_max,
2412 gnu_orig_min),
2413 gnu_max,
2414 TREE_CODE (gnu_min) == INTEGER_CST
2415 ? int_const_binop (MINUS_EXPR, gnu_min,
2416 size_one_node)
2417 : size_binop (MINUS_EXPR, gnu_min,
2418 size_one_node));
2420 /* Reuse the index type for the range type. Then make an index
2421 type with the size range in sizetype. */
2422 gnu_index_types[index]
2423 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2424 gnat_entity);
2426 /* Update the maximum size of the array in elements. Here we
2427 see if any constraint on the index type of the base type
2428 can be used in the case of self-referential bound on the
2429 index type of the subtype. We look for a non-"infinite"
2430 and non-self-referential bound from any type involved and
2431 handle each bound separately. */
2432 if (gnu_max_size)
2434 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2435 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2436 tree gnu_base_index_base_type
2437 = get_base_type (gnu_base_index_type);
2438 tree gnu_base_base_min
2439 = convert (sizetype,
2440 TYPE_MIN_VALUE (gnu_base_index_base_type));
2441 tree gnu_base_base_max
2442 = convert (sizetype,
2443 TYPE_MAX_VALUE (gnu_base_index_base_type));
2445 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2446 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2447 && !TREE_OVERFLOW (gnu_base_min)))
2448 gnu_base_min = gnu_min;
2450 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2451 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2452 && !TREE_OVERFLOW (gnu_base_max)))
2453 gnu_base_max = gnu_max;
2455 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2456 && TREE_OVERFLOW (gnu_base_min))
2457 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2458 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2459 && TREE_OVERFLOW (gnu_base_max))
2460 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2461 gnu_max_size = NULL_TREE;
2462 else
2464 tree gnu_this_max;
2466 /* Use int_const_binop if the bounds are constant to
2467 avoid any unwanted overflow. */
2468 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2469 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2470 gnu_this_max
2471 = int_const_binop (PLUS_EXPR, size_one_node,
2472 int_const_binop (MINUS_EXPR,
2473 gnu_base_max,
2474 gnu_base_min));
2475 else
2476 gnu_this_max
2477 = size_binop (PLUS_EXPR, size_one_node,
2478 size_binop (MINUS_EXPR,
2479 gnu_base_max,
2480 gnu_base_min));
2482 gnu_max_size
2483 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2487 /* We need special types for debugging information to point to
2488 the index types if they have variable bounds, are not integer
2489 types or are biased. */
2490 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2491 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2492 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2493 || (TREE_TYPE (gnu_index_type)
2494 && TREE_CODE (TREE_TYPE (gnu_index_type))
2495 != INTEGER_TYPE)
2496 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2497 need_index_type_struct = true;
2500 /* Then flatten: create the array of arrays. For an array type
2501 used to implement a packed array, get the component type from
2502 the original array type since the representation clauses that
2503 can affect it are on the latter. */
2504 if (Is_Packed_Array_Impl_Type (gnat_entity)
2505 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2507 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2508 for (index = ndim - 1; index >= 0; index--)
2509 gnu_type = TREE_TYPE (gnu_type);
2511 /* One of the above calls might have caused us to be elaborated,
2512 so don't blow up if so. */
2513 if (present_gnu_tree (gnat_entity))
2515 maybe_present = true;
2516 break;
2519 else
2521 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2522 debug_info_p);
2524 /* One of the above calls might have caused us to be elaborated,
2525 so don't blow up if so. */
2526 if (present_gnu_tree (gnat_entity))
2528 maybe_present = true;
2529 break;
2533 /* Compute the maximum size of the array in units and bits. */
2534 if (gnu_max_size)
2536 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2537 TYPE_SIZE_UNIT (gnu_type));
2538 gnu_max_size = size_binop (MULT_EXPR,
2539 convert (bitsizetype, gnu_max_size),
2540 TYPE_SIZE (gnu_type));
2542 else
2543 gnu_max_size_unit = NULL_TREE;
2545 /* Now build the array type. */
2546 for (index = ndim - 1; index >= 0; index --)
2548 gnu_type = build_nonshared_array_type (gnu_type,
2549 gnu_index_types[index]);
2550 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2551 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2552 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2554 /* See the E_Array_Type case for the rationale. */
2555 if (TYPE_MODE (gnu_type) != BLKmode
2556 && Is_By_Reference_Type (gnat_entity))
2557 SET_TYPE_MODE (gnu_type, BLKmode);
2560 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2562 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2563 TYPE_STUB_DECL (gnu_type)
2564 = create_type_stub_decl (gnu_entity_name, gnu_type);
2566 /* If we are at file level and this is a multi-dimensional array,
2567 we need to make a variable corresponding to the stride of the
2568 inner dimensions. */
2569 if (global_bindings_p () && ndim > 1)
2571 tree gnu_st_name = get_identifier ("ST");
2572 tree gnu_arr_type;
2574 for (gnu_arr_type = TREE_TYPE (gnu_type);
2575 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2576 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2577 gnu_st_name = concat_name (gnu_st_name, "ST"))
2579 tree eltype = TREE_TYPE (gnu_arr_type);
2581 TYPE_SIZE (gnu_arr_type)
2582 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2583 gnat_entity, gnu_st_name,
2584 definition, false);
2586 /* ??? For now, store the size as a multiple of the
2587 alignment of the element type in bytes so that we
2588 can see the alignment from the tree. */
2589 TYPE_SIZE_UNIT (gnu_arr_type)
2590 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2591 gnat_entity,
2592 concat_name (gnu_st_name, "A_U"),
2593 definition, false,
2594 TYPE_ALIGN (eltype));
2596 /* ??? create_type_decl is not invoked on the inner types so
2597 the MULT_EXPR node built above will never be marked. */
2598 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2602 /* If we need to write out a record type giving the names of the
2603 bounds for debugging purposes, do it now and make the record
2604 type a parallel type. This is not needed for a packed array
2605 since the bounds are conveyed by the original array type. */
2606 if (need_index_type_struct
2607 && debug_info_p
2608 && !Is_Packed_Array_Impl_Type (gnat_entity))
2610 tree gnu_bound_rec = make_node (RECORD_TYPE);
2611 tree gnu_field_list = NULL_TREE;
2612 tree gnu_field;
2614 TYPE_NAME (gnu_bound_rec)
2615 = create_concat_name (gnat_entity, "XA");
2617 for (index = ndim - 1; index >= 0; index--)
2619 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2620 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2622 /* Make sure to reference the types themselves, and not just
2623 their names, as the debugger may fall back on them. */
2624 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2625 gnu_bound_rec, NULL_TREE,
2626 NULL_TREE, 0, 0);
2627 DECL_CHAIN (gnu_field) = gnu_field_list;
2628 gnu_field_list = gnu_field;
2631 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2632 add_parallel_type (gnu_type, gnu_bound_rec);
2635 /* If this is a packed array type, make the original array type a
2636 parallel type. Otherwise, do it for the base array type if it
2637 isn't artificial to make sure it is kept in the debug info. */
2638 if (debug_info_p)
2640 if (Is_Packed_Array_Impl_Type (gnat_entity))
2641 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2642 else
2644 tree gnu_base_decl
2645 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2646 if (!DECL_ARTIFICIAL (gnu_base_decl))
2647 add_parallel_type (gnu_type,
2648 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2652 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2653 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2654 = (Is_Packed_Array_Impl_Type (gnat_entity)
2655 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2657 /* If the size is self-referential and the maximum size doesn't
2658 overflow, use it. */
2659 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2660 && gnu_max_size
2661 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2662 && TREE_OVERFLOW (gnu_max_size))
2663 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2664 && TREE_OVERFLOW (gnu_max_size_unit)))
2666 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2667 TYPE_SIZE (gnu_type));
2668 TYPE_SIZE_UNIT (gnu_type)
2669 = size_binop (MIN_EXPR, gnu_max_size_unit,
2670 TYPE_SIZE_UNIT (gnu_type));
2673 /* Set our alias set to that of our base type. This gives all
2674 array subtypes the same alias set. */
2675 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2677 /* If this is a packed type, make this type the same as the packed
2678 array type, but do some adjusting in the type first. */
2679 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2681 Entity_Id gnat_index;
2682 tree gnu_inner;
2684 /* First finish the type we had been making so that we output
2685 debugging information for it. */
2686 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2687 if (Treat_As_Volatile (gnat_entity))
2688 gnu_type
2689 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2690 /* Make it artificial only if the base type was artificial too.
2691 That's sort of "morally" true and will make it possible for
2692 the debugger to look it up by name in DWARF, which is needed
2693 in order to decode the packed array type. */
2694 gnu_decl
2695 = create_type_decl (gnu_entity_name, gnu_type,
2696 !Comes_From_Source (Etype (gnat_entity))
2697 && !Comes_From_Source (gnat_entity),
2698 debug_info_p, gnat_entity);
2700 /* Save it as our equivalent in case the call below elaborates
2701 this type again. */
2702 save_gnu_tree (gnat_entity, gnu_decl, false);
2704 gnu_decl
2705 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2706 NULL_TREE, 0);
2707 this_made_decl = true;
2708 gnu_type = TREE_TYPE (gnu_decl);
2709 save_gnu_tree (gnat_entity, NULL_TREE, false);
2711 gnu_inner = gnu_type;
2712 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2713 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2714 || TYPE_PADDING_P (gnu_inner)))
2715 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2717 /* We need to attach the index type to the type we just made so
2718 that the actual bounds can later be put into a template. */
2719 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2720 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2721 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2722 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2724 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2726 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2727 TYPE_MODULUS for modular types so we make an extra
2728 subtype if necessary. */
2729 if (TYPE_MODULAR_P (gnu_inner))
2731 tree gnu_subtype
2732 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2733 TREE_TYPE (gnu_subtype) = gnu_inner;
2734 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2735 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2736 TYPE_MIN_VALUE (gnu_inner));
2737 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2738 TYPE_MAX_VALUE (gnu_inner));
2739 gnu_inner = gnu_subtype;
2742 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2744 #ifdef ENABLE_CHECKING
2745 /* Check for other cases of overloading. */
2746 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2747 #endif
2750 for (gnat_index = First_Index (gnat_entity);
2751 Present (gnat_index);
2752 gnat_index = Next_Index (gnat_index))
2753 SET_TYPE_ACTUAL_BOUNDS
2754 (gnu_inner,
2755 tree_cons (NULL_TREE,
2756 get_unpadded_type (Etype (gnat_index)),
2757 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2759 if (Convention (gnat_entity) != Convention_Fortran)
2760 SET_TYPE_ACTUAL_BOUNDS
2761 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2763 if (TREE_CODE (gnu_type) == RECORD_TYPE
2764 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2765 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2769 else
2770 /* Abort if packed array with no Packed_Array_Impl_Type. */
2771 gcc_assert (!Is_Packed (gnat_entity));
2773 break;
2775 case E_String_Literal_Subtype:
2776 /* Create the type for a string literal. */
2778 Entity_Id gnat_full_type
2779 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2780 && Present (Full_View (Etype (gnat_entity)))
2781 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2782 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2783 tree gnu_string_array_type
2784 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2785 tree gnu_string_index_type
2786 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2787 (TYPE_DOMAIN (gnu_string_array_type))));
2788 tree gnu_lower_bound
2789 = convert (gnu_string_index_type,
2790 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2791 tree gnu_length
2792 = UI_To_gnu (String_Literal_Length (gnat_entity),
2793 gnu_string_index_type);
2794 tree gnu_upper_bound
2795 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2796 gnu_lower_bound,
2797 int_const_binop (MINUS_EXPR, gnu_length,
2798 convert (gnu_string_index_type,
2799 integer_one_node)));
2800 tree gnu_index_type
2801 = create_index_type (convert (sizetype, gnu_lower_bound),
2802 convert (sizetype, gnu_upper_bound),
2803 create_range_type (gnu_string_index_type,
2804 gnu_lower_bound,
2805 gnu_upper_bound),
2806 gnat_entity);
2808 gnu_type
2809 = build_nonshared_array_type (gnat_to_gnu_type
2810 (Component_Type (gnat_entity)),
2811 gnu_index_type);
2812 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2813 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2814 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2816 break;
2818 /* Record Types and Subtypes
2820 The following fields are defined on record types:
2822 Has_Discriminants True if the record has discriminants
2823 First_Discriminant Points to head of list of discriminants
2824 First_Entity Points to head of list of fields
2825 Is_Tagged_Type True if the record is tagged
2827 Implementation of Ada records and discriminated records:
2829 A record type definition is transformed into the equivalent of a C
2830 struct definition. The fields that are the discriminants which are
2831 found in the Full_Type_Declaration node and the elements of the
2832 Component_List found in the Record_Type_Definition node. The
2833 Component_List can be a recursive structure since each Variant of
2834 the Variant_Part of the Component_List has a Component_List.
2836 Processing of a record type definition comprises starting the list of
2837 field declarations here from the discriminants and the calling the
2838 function components_to_record to add the rest of the fields from the
2839 component list and return the gnu type node. The function
2840 components_to_record will call itself recursively as it traverses
2841 the tree. */
2843 case E_Record_Type:
2844 if (Has_Complex_Representation (gnat_entity))
2846 gnu_type
2847 = build_complex_type
2848 (get_unpadded_type
2849 (Etype (Defining_Entity
2850 (First (Component_Items
2851 (Component_List
2852 (Type_Definition
2853 (Declaration_Node (gnat_entity)))))))));
2855 break;
2859 Node_Id full_definition = Declaration_Node (gnat_entity);
2860 Node_Id record_definition = Type_Definition (full_definition);
2861 Node_Id gnat_constr;
2862 Entity_Id gnat_field;
2863 tree gnu_field, gnu_field_list = NULL_TREE;
2864 tree gnu_get_parent;
2865 /* Set PACKED in keeping with gnat_to_gnu_field. */
2866 const int packed
2867 = Is_Packed (gnat_entity)
2869 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2870 ? -1
2871 : (Known_Alignment (gnat_entity)
2872 || (Strict_Alignment (gnat_entity)
2873 && Known_RM_Size (gnat_entity)))
2874 ? -2
2875 : 0;
2876 const bool has_discr = Has_Discriminants (gnat_entity);
2877 const bool has_rep = Has_Specified_Layout (gnat_entity);
2878 const bool is_extension
2879 = (Is_Tagged_Type (gnat_entity)
2880 && Nkind (record_definition) == N_Derived_Type_Definition);
2881 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2882 bool all_rep = has_rep;
2884 /* See if all fields have a rep clause. Stop when we find one
2885 that doesn't. */
2886 if (all_rep)
2887 for (gnat_field = First_Entity (gnat_entity);
2888 Present (gnat_field);
2889 gnat_field = Next_Entity (gnat_field))
2890 if ((Ekind (gnat_field) == E_Component
2891 || Ekind (gnat_field) == E_Discriminant)
2892 && No (Component_Clause (gnat_field)))
2894 all_rep = false;
2895 break;
2898 /* If this is a record extension, go a level further to find the
2899 record definition. Also, verify we have a Parent_Subtype. */
2900 if (is_extension)
2902 if (!type_annotate_only
2903 || Present (Record_Extension_Part (record_definition)))
2904 record_definition = Record_Extension_Part (record_definition);
2906 gcc_assert (type_annotate_only
2907 || Present (Parent_Subtype (gnat_entity)));
2910 /* Make a node for the record. If we are not defining the record,
2911 suppress expanding incomplete types. */
2912 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2913 TYPE_NAME (gnu_type) = gnu_entity_name;
2914 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2915 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2916 sorry ("non-default Scalar_Storage_Order");
2917 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2919 if (!definition)
2921 defer_incomplete_level++;
2922 this_deferred = true;
2925 /* If both a size and rep clause was specified, put the size in
2926 the record type now so that it can get the proper mode. */
2927 if (has_rep && Known_RM_Size (gnat_entity))
2928 TYPE_SIZE (gnu_type)
2929 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2931 /* Always set the alignment here so that it can be used to
2932 set the mode, if it is making the alignment stricter. If
2933 it is invalid, it will be checked again below. If this is to
2934 be Atomic, choose a default alignment of a word unless we know
2935 the size and it's smaller. */
2936 if (Known_Alignment (gnat_entity))
2937 TYPE_ALIGN (gnu_type)
2938 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2939 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2941 unsigned int size = UI_To_Int (Esize (gnat_entity));
2942 TYPE_ALIGN (gnu_type)
2943 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2945 /* If a type needs strict alignment, the minimum size will be the
2946 type size instead of the RM size (see validate_size). Cap the
2947 alignment, lest it causes this type size to become too large. */
2948 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2950 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2951 unsigned int raw_align = raw_size & -raw_size;
2952 if (raw_align < BIGGEST_ALIGNMENT)
2953 TYPE_ALIGN (gnu_type) = raw_align;
2955 else
2956 TYPE_ALIGN (gnu_type) = 0;
2958 /* If we have a Parent_Subtype, make a field for the parent. If
2959 this record has rep clauses, force the position to zero. */
2960 if (Present (Parent_Subtype (gnat_entity)))
2962 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2963 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2964 tree gnu_parent;
2966 /* A major complexity here is that the parent subtype will
2967 reference our discriminants in its Stored_Constraint list.
2968 But those must reference the parent component of this record
2969 which is precisely of the parent subtype we have not built yet!
2970 To break the circle we first build a dummy COMPONENT_REF which
2971 represents the "get to the parent" operation and initialize
2972 each of those discriminants to a COMPONENT_REF of the above
2973 dummy parent referencing the corresponding discriminant of the
2974 base type of the parent subtype. */
2975 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
2976 build0 (PLACEHOLDER_EXPR, gnu_type),
2977 build_decl (input_location,
2978 FIELD_DECL, NULL_TREE,
2979 gnu_dummy_parent_type),
2980 NULL_TREE);
2982 if (has_discr)
2983 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2984 Present (gnat_field);
2985 gnat_field = Next_Stored_Discriminant (gnat_field))
2986 if (Present (Corresponding_Discriminant (gnat_field)))
2988 tree gnu_field
2989 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2990 (gnat_field));
2991 save_gnu_tree
2992 (gnat_field,
2993 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2994 gnu_get_parent, gnu_field, NULL_TREE),
2995 true);
2998 /* Then we build the parent subtype. If it has discriminants but
2999 the type itself has unknown discriminants, this means that it
3000 doesn't contain information about how the discriminants are
3001 derived from those of the ancestor type, so it cannot be used
3002 directly. Instead it is built by cloning the parent subtype
3003 of the underlying record view of the type, for which the above
3004 derivation of discriminants has been made explicit. */
3005 if (Has_Discriminants (gnat_parent)
3006 && Has_Unknown_Discriminants (gnat_entity))
3008 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3010 /* If we are defining the type, the underlying record
3011 view must already have been elaborated at this point.
3012 Otherwise do it now as its parent subtype cannot be
3013 technically elaborated on its own. */
3014 if (definition)
3015 gcc_assert (present_gnu_tree (gnat_uview));
3016 else
3017 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3019 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3021 /* Substitute the "get to the parent" of the type for that
3022 of its underlying record view in the cloned type. */
3023 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3024 Present (gnat_field);
3025 gnat_field = Next_Stored_Discriminant (gnat_field))
3026 if (Present (Corresponding_Discriminant (gnat_field)))
3028 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3029 tree gnu_ref
3030 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3031 gnu_get_parent, gnu_field, NULL_TREE);
3032 gnu_parent
3033 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3036 else
3037 gnu_parent = gnat_to_gnu_type (gnat_parent);
3039 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3040 initially built. The discriminants must reference the fields
3041 of the parent subtype and not those of its base type for the
3042 placeholder machinery to properly work. */
3043 if (has_discr)
3045 /* The actual parent subtype is the full view. */
3046 if (IN (Ekind (gnat_parent), Private_Kind))
3048 if (Present (Full_View (gnat_parent)))
3049 gnat_parent = Full_View (gnat_parent);
3050 else
3051 gnat_parent = Underlying_Full_View (gnat_parent);
3054 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3055 Present (gnat_field);
3056 gnat_field = Next_Stored_Discriminant (gnat_field))
3057 if (Present (Corresponding_Discriminant (gnat_field)))
3059 Entity_Id field;
3060 for (field = First_Stored_Discriminant (gnat_parent);
3061 Present (field);
3062 field = Next_Stored_Discriminant (field))
3063 if (same_discriminant_p (gnat_field, field))
3064 break;
3065 gcc_assert (Present (field));
3066 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3067 = gnat_to_gnu_field_decl (field);
3071 /* The "get to the parent" COMPONENT_REF must be given its
3072 proper type... */
3073 TREE_TYPE (gnu_get_parent) = gnu_parent;
3075 /* ...and reference the _Parent field of this record. */
3076 gnu_field
3077 = create_field_decl (parent_name_id,
3078 gnu_parent, gnu_type,
3079 has_rep
3080 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3081 has_rep
3082 ? bitsize_zero_node : NULL_TREE,
3083 0, 1);
3084 DECL_INTERNAL_P (gnu_field) = 1;
3085 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3086 TYPE_FIELDS (gnu_type) = gnu_field;
3089 /* Make the fields for the discriminants and put them into the record
3090 unless it's an Unchecked_Union. */
3091 if (has_discr)
3092 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3093 Present (gnat_field);
3094 gnat_field = Next_Stored_Discriminant (gnat_field))
3096 /* If this is a record extension and this discriminant is the
3097 renaming of another discriminant, we've handled it above. */
3098 if (Present (Parent_Subtype (gnat_entity))
3099 && Present (Corresponding_Discriminant (gnat_field)))
3100 continue;
3102 gnu_field
3103 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3104 debug_info_p);
3106 /* Make an expression using a PLACEHOLDER_EXPR from the
3107 FIELD_DECL node just created and link that with the
3108 corresponding GNAT defining identifier. */
3109 save_gnu_tree (gnat_field,
3110 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3111 build0 (PLACEHOLDER_EXPR, gnu_type),
3112 gnu_field, NULL_TREE),
3113 true);
3115 if (!is_unchecked_union)
3117 DECL_CHAIN (gnu_field) = gnu_field_list;
3118 gnu_field_list = gnu_field;
3122 /* If we have a derived untagged type that renames discriminants in
3123 the root type, the (stored) discriminants are a just copy of the
3124 discriminants of the root type. This means that any constraints
3125 added by the renaming in the derivation are disregarded as far
3126 as the layout of the derived type is concerned. To rescue them,
3127 we change the type of the (stored) discriminants to a subtype
3128 with the bounds of the type of the visible discriminants. */
3129 if (has_discr
3130 && !is_extension
3131 && Stored_Constraint (gnat_entity) != No_Elist)
3132 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3133 gnat_constr != No_Elmt;
3134 gnat_constr = Next_Elmt (gnat_constr))
3135 if (Nkind (Node (gnat_constr)) == N_Identifier
3136 /* Ignore access discriminants. */
3137 && !Is_Access_Type (Etype (Node (gnat_constr)))
3138 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3140 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3141 tree gnu_discr_type, gnu_ref;
3143 /* If the scope of the discriminant is not the record type,
3144 this means that we're processing the implicit full view
3145 of a type derived from a private discriminated type: in
3146 this case, the Stored_Constraint list is simply copied
3147 from the partial view, see Build_Derived_Private_Type.
3148 So we need to retrieve the corresponding discriminant
3149 of the implicit full view, otherwise we will abort. */
3150 if (Scope (gnat_discr) != gnat_entity)
3152 Entity_Id field;
3153 for (field = First_Entity (gnat_entity);
3154 Present (field);
3155 field = Next_Entity (field))
3156 if (Ekind (field) == E_Discriminant
3157 && same_discriminant_p (gnat_discr, field))
3158 break;
3159 gcc_assert (Present (field));
3160 gnat_discr = field;
3163 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3164 gnu_ref
3165 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3166 NULL_TREE, 0);
3168 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3169 just above for one of the stored discriminants. */
3170 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3172 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3174 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3175 tree gnu_subtype
3176 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3177 ? make_unsigned_type (prec) : make_signed_type (prec);
3178 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3179 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3180 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3181 TYPE_MIN_VALUE (gnu_discr_type));
3182 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3183 TYPE_MAX_VALUE (gnu_discr_type));
3184 TREE_TYPE (gnu_ref)
3185 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3189 /* Add the fields into the record type and finish it up. */
3190 components_to_record (gnu_type, Component_List (record_definition),
3191 gnu_field_list, packed, definition, false,
3192 all_rep, is_unchecked_union,
3193 !Comes_From_Source (gnat_entity), debug_info_p,
3194 false, OK_To_Reorder_Components (gnat_entity),
3195 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3197 /* If it is passed by reference, force BLKmode to ensure that objects
3198 of this type will always be put in memory. */
3199 if (TYPE_MODE (gnu_type) != BLKmode
3200 && Is_By_Reference_Type (gnat_entity))
3201 SET_TYPE_MODE (gnu_type, BLKmode);
3203 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3205 /* Fill in locations of fields. */
3206 annotate_rep (gnat_entity, gnu_type);
3208 /* If there are any entities in the chain corresponding to components
3209 that we did not elaborate, ensure we elaborate their types if they
3210 are Itypes. */
3211 for (gnat_temp = First_Entity (gnat_entity);
3212 Present (gnat_temp);
3213 gnat_temp = Next_Entity (gnat_temp))
3214 if ((Ekind (gnat_temp) == E_Component
3215 || Ekind (gnat_temp) == E_Discriminant)
3216 && Is_Itype (Etype (gnat_temp))
3217 && !present_gnu_tree (gnat_temp))
3218 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3220 /* If this is a record type associated with an exception definition,
3221 equate its fields to those of the standard exception type. This
3222 will make it possible to convert between them. */
3223 if (gnu_entity_name == exception_data_name_id)
3225 tree gnu_std_field;
3226 for (gnu_field = TYPE_FIELDS (gnu_type),
3227 gnu_std_field = TYPE_FIELDS (except_type_node);
3228 gnu_field;
3229 gnu_field = DECL_CHAIN (gnu_field),
3230 gnu_std_field = DECL_CHAIN (gnu_std_field))
3231 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3232 gcc_assert (!gnu_std_field);
3235 break;
3237 case E_Class_Wide_Subtype:
3238 /* If an equivalent type is present, that is what we should use.
3239 Otherwise, fall through to handle this like a record subtype
3240 since it may have constraints. */
3241 if (gnat_equiv_type != gnat_entity)
3243 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3244 maybe_present = true;
3245 break;
3248 /* ... fall through ... */
3250 case E_Record_Subtype:
3251 /* If Cloned_Subtype is Present it means this record subtype has
3252 identical layout to that type or subtype and we should use
3253 that GCC type for this one. The front end guarantees that
3254 the component list is shared. */
3255 if (Present (Cloned_Subtype (gnat_entity)))
3257 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3258 NULL_TREE, 0);
3259 maybe_present = true;
3260 break;
3263 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3264 changing the type, make a new type with each field having the type of
3265 the field in the new subtype but the position computed by transforming
3266 every discriminant reference according to the constraints. We don't
3267 see any difference between private and non-private type here since
3268 derivations from types should have been deferred until the completion
3269 of the private type. */
3270 else
3272 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3273 tree gnu_base_type;
3275 if (!definition)
3277 defer_incomplete_level++;
3278 this_deferred = true;
3281 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3283 if (present_gnu_tree (gnat_entity))
3285 maybe_present = true;
3286 break;
3289 /* If this is a record subtype associated with a dispatch table,
3290 strip the suffix. This is necessary to make sure 2 different
3291 subtypes associated with the imported and exported views of a
3292 dispatch table are properly merged in LTO mode. */
3293 if (Is_Dispatch_Table_Entity (gnat_entity))
3295 char *p;
3296 Get_Encoded_Name (gnat_entity);
3297 p = strchr (Name_Buffer, '_');
3298 gcc_assert (p);
3299 strcpy (p+2, "dtS");
3300 gnu_entity_name = get_identifier (Name_Buffer);
3303 /* When the subtype has discriminants and these discriminants affect
3304 the initial shape it has inherited, factor them in. But for an
3305 Unchecked_Union (it must be an Itype), just return the type.
3306 We can't just test Is_Constrained because private subtypes without
3307 discriminants of types with discriminants with default expressions
3308 are Is_Constrained but aren't constrained! */
3309 if (IN (Ekind (gnat_base_type), Record_Kind)
3310 && !Is_Unchecked_Union (gnat_base_type)
3311 && !Is_For_Access_Subtype (gnat_entity)
3312 && Has_Discriminants (gnat_entity)
3313 && Is_Constrained (gnat_entity)
3314 && Stored_Constraint (gnat_entity) != No_Elist)
3316 vec<subst_pair> gnu_subst_list
3317 = build_subst_list (gnat_entity, gnat_base_type, definition);
3318 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3319 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3320 bool selected_variant = false, all_constant_pos = true;
3321 Entity_Id gnat_field;
3322 vec<variant_desc> gnu_variant_list;
3324 gnu_type = make_node (RECORD_TYPE);
3325 TYPE_NAME (gnu_type) = gnu_entity_name;
3326 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3327 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3329 /* Set the size, alignment and alias set of the new type to
3330 match that of the old one, doing required substitutions. */
3331 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3332 gnu_subst_list);
3334 if (TYPE_IS_PADDING_P (gnu_base_type))
3335 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3336 else
3337 gnu_unpad_base_type = gnu_base_type;
3339 /* Look for REP and variant parts in the base type. */
3340 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3341 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3343 /* If there is a variant part, we must compute whether the
3344 constraints statically select a particular variant. If
3345 so, we simply drop the qualified union and flatten the
3346 list of fields. Otherwise we'll build a new qualified
3347 union for the variants that are still relevant. */
3348 if (gnu_variant_part)
3350 variant_desc *v;
3351 unsigned int i;
3353 gnu_variant_list
3354 = build_variant_list (TREE_TYPE (gnu_variant_part),
3355 gnu_subst_list,
3356 vNULL);
3358 /* If all the qualifiers are unconditionally true, the
3359 innermost variant is statically selected. */
3360 selected_variant = true;
3361 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3362 if (!integer_onep (v->qual))
3364 selected_variant = false;
3365 break;
3368 /* Otherwise, create the new variants. */
3369 if (!selected_variant)
3370 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3372 tree old_variant = v->type;
3373 tree new_variant = make_node (RECORD_TYPE);
3374 tree suffix
3375 = concat_name (DECL_NAME (gnu_variant_part),
3376 IDENTIFIER_POINTER
3377 (DECL_NAME (v->field)));
3378 TYPE_NAME (new_variant)
3379 = concat_name (TYPE_NAME (gnu_type),
3380 IDENTIFIER_POINTER (suffix));
3381 copy_and_substitute_in_size (new_variant, old_variant,
3382 gnu_subst_list);
3383 v->new_type = new_variant;
3386 else
3388 gnu_variant_list.create (0);
3389 selected_variant = false;
3392 /* Make a list of fields and their position in the base type. */
3393 gnu_pos_list
3394 = build_position_list (gnu_unpad_base_type,
3395 gnu_variant_list.exists ()
3396 && !selected_variant,
3397 size_zero_node, bitsize_zero_node,
3398 BIGGEST_ALIGNMENT, NULL_TREE);
3400 /* Now go down every component in the subtype and compute its
3401 size and position from those of the component in the base
3402 type and from the constraints of the subtype. */
3403 for (gnat_field = First_Entity (gnat_entity);
3404 Present (gnat_field);
3405 gnat_field = Next_Entity (gnat_field))
3406 if ((Ekind (gnat_field) == E_Component
3407 || Ekind (gnat_field) == E_Discriminant)
3408 && !(Present (Corresponding_Discriminant (gnat_field))
3409 && Is_Tagged_Type (gnat_base_type))
3410 && Underlying_Type
3411 (Scope (Original_Record_Component (gnat_field)))
3412 == gnat_base_type)
3414 Name_Id gnat_name = Chars (gnat_field);
3415 Entity_Id gnat_old_field
3416 = Original_Record_Component (gnat_field);
3417 tree gnu_old_field
3418 = gnat_to_gnu_field_decl (gnat_old_field);
3419 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3420 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3421 tree gnu_cont_type, gnu_last = NULL_TREE;
3423 /* If the type is the same, retrieve the GCC type from the
3424 old field to take into account possible adjustments. */
3425 if (Etype (gnat_field) == Etype (gnat_old_field))
3426 gnu_field_type = TREE_TYPE (gnu_old_field);
3427 else
3428 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3430 /* If there was a component clause, the field types must be
3431 the same for the type and subtype, so copy the data from
3432 the old field to avoid recomputation here. Also if the
3433 field is justified modular and the optimization in
3434 gnat_to_gnu_field was applied. */
3435 if (Present (Component_Clause (gnat_old_field))
3436 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3437 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3438 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3439 == TREE_TYPE (gnu_old_field)))
3441 gnu_size = DECL_SIZE (gnu_old_field);
3442 gnu_field_type = TREE_TYPE (gnu_old_field);
3445 /* If the old field was packed and of constant size, we
3446 have to get the old size here, as it might differ from
3447 what the Etype conveys and the latter might overlap
3448 onto the following field. Try to arrange the type for
3449 possible better packing along the way. */
3450 else if (DECL_PACKED (gnu_old_field)
3451 && TREE_CODE (DECL_SIZE (gnu_old_field))
3452 == INTEGER_CST)
3454 gnu_size = DECL_SIZE (gnu_old_field);
3455 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3456 && !TYPE_FAT_POINTER_P (gnu_field_type)
3457 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3458 gnu_field_type
3459 = make_packable_type (gnu_field_type, true);
3462 else
3463 gnu_size = TYPE_SIZE (gnu_field_type);
3465 /* If the context of the old field is the base type or its
3466 REP part (if any), put the field directly in the new
3467 type; otherwise look up the context in the variant list
3468 and put the field either in the new type if there is a
3469 selected variant or in one of the new variants. */
3470 if (gnu_context == gnu_unpad_base_type
3471 || (gnu_rep_part
3472 && gnu_context == TREE_TYPE (gnu_rep_part)))
3473 gnu_cont_type = gnu_type;
3474 else
3476 variant_desc *v;
3477 unsigned int i;
3478 tree rep_part;
3480 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3481 if (gnu_context == v->type
3482 || ((rep_part = get_rep_part (v->type))
3483 && gnu_context == TREE_TYPE (rep_part)))
3484 break;
3485 if (v)
3487 if (selected_variant)
3488 gnu_cont_type = gnu_type;
3489 else
3490 gnu_cont_type = v->new_type;
3492 else
3493 /* The front-end may pass us "ghost" components if
3494 it fails to recognize that a constrained subtype
3495 is statically constrained. Discard them. */
3496 continue;
3499 /* Now create the new field modeled on the old one. */
3500 gnu_field
3501 = create_field_decl_from (gnu_old_field, gnu_field_type,
3502 gnu_cont_type, gnu_size,
3503 gnu_pos_list, gnu_subst_list);
3504 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3506 /* Put it in one of the new variants directly. */
3507 if (gnu_cont_type != gnu_type)
3509 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3510 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3513 /* To match the layout crafted in components_to_record,
3514 if this is the _Tag or _Parent field, put it before
3515 any other fields. */
3516 else if (gnat_name == Name_uTag
3517 || gnat_name == Name_uParent)
3518 gnu_field_list = chainon (gnu_field_list, gnu_field);
3520 /* Similarly, if this is the _Controller field, put
3521 it before the other fields except for the _Tag or
3522 _Parent field. */
3523 else if (gnat_name == Name_uController && gnu_last)
3525 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3526 DECL_CHAIN (gnu_last) = gnu_field;
3529 /* Otherwise, if this is a regular field, put it after
3530 the other fields. */
3531 else
3533 DECL_CHAIN (gnu_field) = gnu_field_list;
3534 gnu_field_list = gnu_field;
3535 if (!gnu_last)
3536 gnu_last = gnu_field;
3537 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3538 all_constant_pos = false;
3541 save_gnu_tree (gnat_field, gnu_field, false);
3544 /* If there is a variant list, a selected variant and the fields
3545 all have a constant position, put them in order of increasing
3546 position to match that of constant CONSTRUCTORs. Likewise if
3547 there is no variant list but a REP part, since the latter has
3548 been flattened in the process. */
3549 if (((gnu_variant_list.exists () && selected_variant)
3550 || (!gnu_variant_list.exists () && gnu_rep_part))
3551 && all_constant_pos)
3553 const int len = list_length (gnu_field_list);
3554 tree *field_arr = XALLOCAVEC (tree, len), t;
3555 int i;
3557 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3558 field_arr[i] = t;
3560 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3562 gnu_field_list = NULL_TREE;
3563 for (i = 0; i < len; i++)
3565 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3566 gnu_field_list = field_arr[i];
3570 /* If there is a variant list and no selected variant, we need
3571 to create the nest of variant parts from the old nest. */
3572 else if (gnu_variant_list.exists () && !selected_variant)
3574 tree new_variant_part
3575 = create_variant_part_from (gnu_variant_part,
3576 gnu_variant_list, gnu_type,
3577 gnu_pos_list, gnu_subst_list);
3578 DECL_CHAIN (new_variant_part) = gnu_field_list;
3579 gnu_field_list = new_variant_part;
3582 /* Now go through the entities again looking for Itypes that
3583 we have not elaborated but should (e.g., Etypes of fields
3584 that have Original_Components). */
3585 for (gnat_field = First_Entity (gnat_entity);
3586 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3587 if ((Ekind (gnat_field) == E_Discriminant
3588 || Ekind (gnat_field) == E_Component)
3589 && !present_gnu_tree (Etype (gnat_field)))
3590 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3592 /* Do not emit debug info for the type yet since we're going to
3593 modify it below. */
3594 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3595 false);
3596 compute_record_mode (gnu_type);
3598 /* See the E_Record_Type case for the rationale. */
3599 if (TYPE_MODE (gnu_type) != BLKmode
3600 && Is_By_Reference_Type (gnat_entity))
3601 SET_TYPE_MODE (gnu_type, BLKmode);
3603 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3605 /* Fill in locations of fields. */
3606 annotate_rep (gnat_entity, gnu_type);
3608 /* If debugging information is being written for the type, write
3609 a record that shows what we are a subtype of and also make a
3610 variable that indicates our size, if still variable. */
3611 if (debug_info_p)
3613 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3614 tree gnu_unpad_base_name
3615 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3616 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3618 TYPE_NAME (gnu_subtype_marker)
3619 = create_concat_name (gnat_entity, "XVS");
3620 finish_record_type (gnu_subtype_marker,
3621 create_field_decl (gnu_unpad_base_name,
3622 build_reference_type
3623 (gnu_unpad_base_type),
3624 gnu_subtype_marker,
3625 NULL_TREE, NULL_TREE,
3626 0, 0),
3627 0, true);
3629 add_parallel_type (gnu_type, gnu_subtype_marker);
3631 if (definition
3632 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3633 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3634 TYPE_SIZE_UNIT (gnu_subtype_marker)
3635 = create_var_decl (create_concat_name (gnat_entity,
3636 "XVZ"),
3637 NULL_TREE, sizetype, gnu_size_unit,
3638 false, false, false, false, NULL,
3639 gnat_entity);
3642 gnu_variant_list.release ();
3643 gnu_subst_list.release ();
3645 /* Now we can finalize it. */
3646 rest_of_record_type_compilation (gnu_type);
3649 /* Otherwise, go down all the components in the new type and make
3650 them equivalent to those in the base type. */
3651 else
3653 gnu_type = gnu_base_type;
3655 for (gnat_temp = First_Entity (gnat_entity);
3656 Present (gnat_temp);
3657 gnat_temp = Next_Entity (gnat_temp))
3658 if ((Ekind (gnat_temp) == E_Discriminant
3659 && !Is_Unchecked_Union (gnat_base_type))
3660 || Ekind (gnat_temp) == E_Component)
3661 save_gnu_tree (gnat_temp,
3662 gnat_to_gnu_field_decl
3663 (Original_Record_Component (gnat_temp)),
3664 false);
3667 break;
3669 case E_Access_Subprogram_Type:
3670 /* Use the special descriptor type for dispatch tables if needed,
3671 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3672 Note that we are only required to do so for static tables in
3673 order to be compatible with the C++ ABI, but Ada 2005 allows
3674 to extend library level tagged types at the local level so
3675 we do it in the non-static case as well. */
3676 if (TARGET_VTABLE_USES_DESCRIPTORS
3677 && Is_Dispatch_Table_Entity (gnat_entity))
3679 gnu_type = fdesc_type_node;
3680 gnu_size = TYPE_SIZE (gnu_type);
3681 break;
3684 /* ... fall through ... */
3686 case E_Anonymous_Access_Subprogram_Type:
3687 /* If we are not defining this entity, and we have incomplete
3688 entities being processed above us, make a dummy type and
3689 fill it in later. */
3690 if (!definition && defer_incomplete_level != 0)
3692 struct incomplete *p = XNEW (struct incomplete);
3694 gnu_type
3695 = build_pointer_type
3696 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3697 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3698 !Comes_From_Source (gnat_entity),
3699 debug_info_p, gnat_entity);
3700 this_made_decl = true;
3701 gnu_type = TREE_TYPE (gnu_decl);
3702 save_gnu_tree (gnat_entity, gnu_decl, false);
3703 saved = true;
3705 p->old_type = TREE_TYPE (gnu_type);
3706 p->full_type = Directly_Designated_Type (gnat_entity);
3707 p->next = defer_incomplete_list;
3708 defer_incomplete_list = p;
3709 break;
3712 /* ... fall through ... */
3714 case E_Allocator_Type:
3715 case E_Access_Type:
3716 case E_Access_Attribute_Type:
3717 case E_Anonymous_Access_Type:
3718 case E_General_Access_Type:
3720 /* The designated type and its equivalent type for gigi. */
3721 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3722 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3723 /* Whether it comes from a limited with. */
3724 bool is_from_limited_with
3725 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3726 && From_Limited_With (gnat_desig_equiv));
3727 /* The "full view" of the designated type. If this is an incomplete
3728 entity from a limited with, treat its non-limited view as the full
3729 view. Otherwise, if this is an incomplete or private type, use the
3730 full view. In the former case, we might point to a private type,
3731 in which case, we need its full view. Also, we want to look at the
3732 actual type used for the representation, so this takes a total of
3733 three steps. */
3734 Entity_Id gnat_desig_full_direct_first
3735 = (is_from_limited_with
3736 ? Non_Limited_View (gnat_desig_equiv)
3737 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3738 ? Full_View (gnat_desig_equiv) : Empty));
3739 Entity_Id gnat_desig_full_direct
3740 = ((is_from_limited_with
3741 && Present (gnat_desig_full_direct_first)
3742 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3743 ? Full_View (gnat_desig_full_direct_first)
3744 : gnat_desig_full_direct_first);
3745 Entity_Id gnat_desig_full
3746 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3747 /* The type actually used to represent the designated type, either
3748 gnat_desig_full or gnat_desig_equiv. */
3749 Entity_Id gnat_desig_rep;
3750 /* True if this is a pointer to an unconstrained array. */
3751 bool is_unconstrained_array;
3752 /* We want to know if we'll be seeing the freeze node for any
3753 incomplete type we may be pointing to. */
3754 bool in_main_unit
3755 = (Present (gnat_desig_full)
3756 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3757 : In_Extended_Main_Code_Unit (gnat_desig_type));
3758 /* True if we make a dummy type here. */
3759 bool made_dummy = false;
3760 /* The mode to be used for the pointer type. */
3761 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3762 /* The GCC type used for the designated type. */
3763 tree gnu_desig_type = NULL_TREE;
3765 if (!targetm.valid_pointer_mode (p_mode))
3766 p_mode = ptr_mode;
3768 /* If either the designated type or its full view is an unconstrained
3769 array subtype, replace it with the type it's a subtype of. This
3770 avoids problems with multiple copies of unconstrained array types.
3771 Likewise, if the designated type is a subtype of an incomplete
3772 record type, use the parent type to avoid order of elaboration
3773 issues. This can lose some code efficiency, but there is no
3774 alternative. */
3775 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3776 && !Is_Constrained (gnat_desig_equiv))
3777 gnat_desig_equiv = Etype (gnat_desig_equiv);
3778 if (Present (gnat_desig_full)
3779 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3780 && !Is_Constrained (gnat_desig_full))
3781 || (Ekind (gnat_desig_full) == E_Record_Subtype
3782 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3783 gnat_desig_full = Etype (gnat_desig_full);
3785 /* Set the type that's actually the representation of the designated
3786 type and also flag whether we have a unconstrained array. */
3787 gnat_desig_rep
3788 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3789 is_unconstrained_array
3790 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3792 /* If we are pointing to an incomplete type whose completion is an
3793 unconstrained array, make dummy fat and thin pointer types to it.
3794 Likewise if the type itself is dummy or an unconstrained array. */
3795 if (is_unconstrained_array
3796 && (Present (gnat_desig_full)
3797 || (present_gnu_tree (gnat_desig_equiv)
3798 && TYPE_IS_DUMMY_P
3799 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3800 || (!in_main_unit
3801 && defer_incomplete_level != 0
3802 && !present_gnu_tree (gnat_desig_equiv))
3803 || (in_main_unit
3804 && is_from_limited_with
3805 && Present (Freeze_Node (gnat_desig_equiv)))))
3807 if (present_gnu_tree (gnat_desig_rep))
3808 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3809 else
3811 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3812 made_dummy = true;
3815 /* If the call above got something that has a pointer, the pointer
3816 is our type. This could have happened either because the type
3817 was elaborated or because somebody else executed the code. */
3818 if (!TYPE_POINTER_TO (gnu_desig_type))
3819 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3820 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3823 /* If we already know what the full type is, use it. */
3824 else if (Present (gnat_desig_full)
3825 && present_gnu_tree (gnat_desig_full))
3826 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3828 /* Get the type of the thing we are to point to and build a pointer to
3829 it. If it is a reference to an incomplete or private type with a
3830 full view that is a record, make a dummy type node and get the
3831 actual type later when we have verified it is safe. */
3832 else if ((!in_main_unit
3833 && !present_gnu_tree (gnat_desig_equiv)
3834 && Present (gnat_desig_full)
3835 && !present_gnu_tree (gnat_desig_full)
3836 && Is_Record_Type (gnat_desig_full))
3837 /* Likewise if we are pointing to a record or array and we are
3838 to defer elaborating incomplete types. We do this as this
3839 access type may be the full view of a private type. Note
3840 that the unconstrained array case is handled above. */
3841 || ((!in_main_unit || imported_p)
3842 && defer_incomplete_level != 0
3843 && !present_gnu_tree (gnat_desig_equiv)
3844 && (Is_Record_Type (gnat_desig_rep)
3845 || Is_Array_Type (gnat_desig_rep)))
3846 /* If this is a reference from a limited_with type back to our
3847 main unit and there's a freeze node for it, either we have
3848 already processed the declaration and made the dummy type,
3849 in which case we just reuse the latter, or we have not yet,
3850 in which case we make the dummy type and it will be reused
3851 when the declaration is finally processed. In both cases,
3852 the pointer eventually created below will be automatically
3853 adjusted when the freeze node is processed. Note that the
3854 unconstrained array case is handled above. */
3855 || (in_main_unit
3856 && is_from_limited_with
3857 && Present (Freeze_Node (gnat_desig_rep))))
3859 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3860 made_dummy = true;
3863 /* Otherwise handle the case of a pointer to itself. */
3864 else if (gnat_desig_equiv == gnat_entity)
3866 gnu_type
3867 = build_pointer_type_for_mode (void_type_node, p_mode,
3868 No_Strict_Aliasing (gnat_entity));
3869 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3872 /* If expansion is disabled, the equivalent type of a concurrent type
3873 is absent, so build a dummy pointer type. */
3874 else if (type_annotate_only && No (gnat_desig_equiv))
3875 gnu_type = ptr_void_type_node;
3877 /* Finally, handle the default case where we can just elaborate our
3878 designated type. */
3879 else
3880 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3882 /* It is possible that a call to gnat_to_gnu_type above resolved our
3883 type. If so, just return it. */
3884 if (present_gnu_tree (gnat_entity))
3886 maybe_present = true;
3887 break;
3890 /* If we haven't done it yet, build the pointer type the usual way. */
3891 if (!gnu_type)
3893 /* Modify the designated type if we are pointing only to constant
3894 objects, but don't do it for unconstrained arrays. */
3895 if (Is_Access_Constant (gnat_entity)
3896 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3898 gnu_desig_type
3899 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3901 /* Some extra processing is required if we are building a
3902 pointer to an incomplete type (in the GCC sense). We might
3903 have such a type if we just made a dummy, or directly out
3904 of the call to gnat_to_gnu_type above if we are processing
3905 an access type for a record component designating the
3906 record type itself. */
3907 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3909 /* We must ensure that the pointer to variant we make will
3910 be processed by update_pointer_to when the initial type
3911 is completed. Pretend we made a dummy and let further
3912 processing act as usual. */
3913 made_dummy = true;
3915 /* We must ensure that update_pointer_to will not retrieve
3916 the dummy variant when building a properly qualified
3917 version of the complete type. We take advantage of the
3918 fact that get_qualified_type is requiring TYPE_NAMEs to
3919 match to influence build_qualified_type and then also
3920 update_pointer_to here. */
3921 TYPE_NAME (gnu_desig_type)
3922 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3926 gnu_type
3927 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3928 No_Strict_Aliasing (gnat_entity));
3931 /* If we are not defining this object and we have made a dummy pointer,
3932 save our current definition, evaluate the actual type, and replace
3933 the tentative type we made with the actual one. If we are to defer
3934 actually looking up the actual type, make an entry in the deferred
3935 list. If this is from a limited with, we may have to defer to the
3936 end of the current unit. */
3937 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3939 tree gnu_old_desig_type;
3941 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3943 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3944 if (esize == POINTER_SIZE)
3945 gnu_type = build_pointer_type
3946 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3948 else
3949 gnu_old_desig_type = TREE_TYPE (gnu_type);
3951 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3952 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3953 !Comes_From_Source (gnat_entity),
3954 debug_info_p, gnat_entity);
3955 this_made_decl = true;
3956 gnu_type = TREE_TYPE (gnu_decl);
3957 save_gnu_tree (gnat_entity, gnu_decl, false);
3958 saved = true;
3960 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3961 update gnu_old_desig_type directly, in which case it will not be
3962 a dummy type any more when we get into update_pointer_to.
3964 This can happen e.g. when the designated type is a record type,
3965 because their elaboration starts with an initial node from
3966 make_dummy_type, which may be the same node as the one we got.
3968 Besides, variants of this non-dummy type might have been created
3969 along the way. update_pointer_to is expected to properly take
3970 care of those situations. */
3971 if (defer_incomplete_level == 0 && !is_from_limited_with)
3973 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3974 gnat_to_gnu_type (gnat_desig_equiv));
3976 else
3978 struct incomplete *p = XNEW (struct incomplete);
3979 struct incomplete **head
3980 = (is_from_limited_with
3981 ? &defer_limited_with : &defer_incomplete_list);
3982 p->old_type = gnu_old_desig_type;
3983 p->full_type = gnat_desig_equiv;
3984 p->next = *head;
3985 *head = p;
3989 break;
3991 case E_Access_Protected_Subprogram_Type:
3992 case E_Anonymous_Access_Protected_Subprogram_Type:
3993 if (type_annotate_only && No (gnat_equiv_type))
3994 gnu_type = ptr_void_type_node;
3995 else
3997 /* The run-time representation is the equivalent type. */
3998 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3999 maybe_present = true;
4002 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4003 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4004 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4005 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4006 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4007 NULL_TREE, 0);
4009 break;
4011 case E_Access_Subtype:
4013 /* We treat this as identical to its base type; any constraint is
4014 meaningful only to the front-end.
4016 The designated type must be elaborated as well, if it does
4017 not have its own freeze node. Designated (sub)types created
4018 for constrained components of records with discriminants are
4019 not frozen by the front-end and thus not elaborated by gigi,
4020 because their use may appear before the base type is frozen,
4021 and because it is not clear that they are needed anywhere in
4022 gigi. With the current model, there is no correct place where
4023 they could be elaborated. */
4025 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4026 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4027 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4028 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4029 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4031 /* If we are not defining this entity, and we have incomplete
4032 entities being processed above us, make a dummy type and
4033 elaborate it later. */
4034 if (!definition && defer_incomplete_level != 0)
4036 struct incomplete *p = XNEW (struct incomplete);
4038 p->old_type
4039 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4040 p->full_type = Directly_Designated_Type (gnat_entity);
4041 p->next = defer_incomplete_list;
4042 defer_incomplete_list = p;
4044 else if (!IN (Ekind (Base_Type
4045 (Directly_Designated_Type (gnat_entity))),
4046 Incomplete_Or_Private_Kind))
4047 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4048 NULL_TREE, 0);
4051 maybe_present = true;
4052 break;
4054 /* Subprogram Entities
4056 The following access functions are defined for subprograms:
4058 Etype Return type or Standard_Void_Type.
4059 First_Formal The first formal parameter.
4060 Is_Imported Indicates that the subprogram has appeared in
4061 an INTERFACE or IMPORT pragma. For now we
4062 assume that the external language is C.
4063 Is_Exported Likewise but for an EXPORT pragma.
4064 Is_Inlined True if the subprogram is to be inlined.
4066 Each parameter is first checked by calling must_pass_by_ref on its
4067 type to determine if it is passed by reference. For parameters which
4068 are copied in, if they are Ada In Out or Out parameters, their return
4069 value becomes part of a record which becomes the return type of the
4070 function (C function - note that this applies only to Ada procedures
4071 so there is no Ada return type). Additional code to store back the
4072 parameters will be generated on the caller side. This transformation
4073 is done here, not in the front-end.
4075 The intended result of the transformation can be seen from the
4076 equivalent source rewritings that follow:
4078 struct temp {int a,b};
4079 procedure P (A,B: In Out ...) is temp P (int A,B)
4080 begin {
4081 .. ..
4082 end P; return {A,B};
4085 temp t;
4086 P(X,Y); t = P(X,Y);
4087 X = t.a , Y = t.b;
4089 For subprogram types we need to perform mainly the same conversions to
4090 GCC form that are needed for procedures and function declarations. The
4091 only difference is that at the end, we make a type declaration instead
4092 of a function declaration. */
4094 case E_Subprogram_Type:
4095 case E_Function:
4096 case E_Procedure:
4098 /* The type returned by a function or else Standard_Void_Type for a
4099 procedure. */
4100 Entity_Id gnat_return_type = Etype (gnat_entity);
4101 tree gnu_return_type;
4102 /* The first GCC parameter declaration (a PARM_DECL node). The
4103 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4104 actually is the head of this parameter list. */
4105 tree gnu_param_list = NULL_TREE;
4106 /* Non-null for subprograms containing parameters passed by copy-in
4107 copy-out (Ada In Out or Out parameters not passed by reference),
4108 in which case it is the list of nodes used to specify the values
4109 of the In Out/Out parameters that are returned as a record upon
4110 procedure return. The TREE_PURPOSE of an element of this list is
4111 a field of the record and the TREE_VALUE is the PARM_DECL
4112 corresponding to that field. This list will be saved in the
4113 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4114 tree gnu_cico_list = NULL_TREE;
4115 /* List of fields in return type of procedure with copy-in copy-out
4116 parameters. */
4117 tree gnu_field_list = NULL_TREE;
4118 /* If an import pragma asks to map this subprogram to a GCC builtin,
4119 this is the builtin DECL node. */
4120 tree gnu_builtin_decl = NULL_TREE;
4121 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4122 Entity_Id gnat_param;
4123 enum inline_status_t inline_status
4124 = Has_Pragma_No_Inline (gnat_entity)
4125 ? is_suppressed
4126 : Has_Pragma_Inline_Always (gnat_entity)
4127 ? is_required
4128 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4129 bool public_flag = Is_Public (gnat_entity) || imported_p;
4130 bool extern_flag
4131 = (Is_Public (gnat_entity) && !definition) || imported_p;
4132 bool artificial_flag = !Comes_From_Source (gnat_entity);
4133 /* The semantics of "pure" in Ada essentially matches that of "const"
4134 in the back-end. In particular, both properties are orthogonal to
4135 the "nothrow" property if the EH circuitry is explicit in the
4136 internal representation of the back-end. If we are to completely
4137 hide the EH circuitry from it, we need to declare that calls to pure
4138 Ada subprograms that can throw have side effects since they can
4139 trigger an "abnormal" transfer of control flow; thus they can be
4140 neither "const" nor "pure" in the back-end sense. */
4141 bool const_flag
4142 = (Exception_Mechanism == Back_End_Exceptions
4143 && Is_Pure (gnat_entity));
4144 bool volatile_flag = No_Return (gnat_entity);
4145 bool return_by_direct_ref_p = false;
4146 bool return_by_invisi_ref_p = false;
4147 bool return_unconstrained_p = false;
4148 int parmnum;
4150 /* A parameter may refer to this type, so defer completion of any
4151 incomplete types. */
4152 if (kind == E_Subprogram_Type && !definition)
4154 defer_incomplete_level++;
4155 this_deferred = true;
4158 /* If the subprogram has an alias, it is probably inherited, so
4159 we can use the original one. If the original "subprogram"
4160 is actually an enumeration literal, it may be the first use
4161 of its type, so we must elaborate that type now. */
4162 if (Present (Alias (gnat_entity)))
4164 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4165 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4167 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4169 /* Elaborate any Itypes in the parameters of this entity. */
4170 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4171 Present (gnat_temp);
4172 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4173 if (Is_Itype (Etype (gnat_temp)))
4174 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4176 break;
4179 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4180 corresponding DECL node. Proper generation of calls later on need
4181 proper parameter associations so we don't "break;" here. */
4182 if (Convention (gnat_entity) == Convention_Intrinsic
4183 && Present (Interface_Name (gnat_entity)))
4185 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4187 /* Inability to find the builtin decl most often indicates a
4188 genuine mistake, but imports of unregistered intrinsics are
4189 sometimes issued on purpose to allow hooking in alternate
4190 bodies. We post a warning conditioned on Wshadow in this case,
4191 to let developers be notified on demand without risking false
4192 positives with common default sets of options. */
4194 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4195 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4198 /* ??? What if we don't find the builtin node above ? warn ? err ?
4199 In the current state we neither warn nor err, and calls will just
4200 be handled as for regular subprograms. */
4202 /* Look into the return type and get its associated GCC tree. If it
4203 is not void, compute various flags for the subprogram type. */
4204 if (Ekind (gnat_return_type) == E_Void)
4205 gnu_return_type = void_type_node;
4206 else
4208 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4209 context may now appear in parameter and result profiles. If
4210 we are only annotating types, break circularities here. */
4211 if (type_annotate_only
4212 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4213 && From_Limited_With (gnat_return_type)
4214 && In_Extended_Main_Code_Unit
4215 (Non_Limited_View (gnat_return_type))
4216 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4217 gnu_return_type = ptr_void_type_node;
4218 else
4219 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4221 /* If this function returns by reference, make the actual return
4222 type the pointer type and make a note of that. */
4223 if (Returns_By_Ref (gnat_entity))
4225 gnu_return_type = build_pointer_type (gnu_return_type);
4226 return_by_direct_ref_p = true;
4229 /* If we are supposed to return an unconstrained array type, make
4230 the actual return type the fat pointer type. */
4231 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4233 gnu_return_type = TREE_TYPE (gnu_return_type);
4234 return_unconstrained_p = true;
4237 /* Likewise, if the return type requires a transient scope, the
4238 return value will be allocated on the secondary stack so the
4239 actual return type is the pointer type. */
4240 else if (Requires_Transient_Scope (gnat_return_type))
4242 gnu_return_type = build_pointer_type (gnu_return_type);
4243 return_unconstrained_p = true;
4246 /* If the Mechanism is By_Reference, ensure this function uses the
4247 target's by-invisible-reference mechanism, which may not be the
4248 same as above (e.g. it might be passing an extra parameter). */
4249 else if (kind == E_Function
4250 && Mechanism (gnat_entity) == By_Reference)
4251 return_by_invisi_ref_p = true;
4253 /* Likewise, if the return type is itself By_Reference. */
4254 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4255 return_by_invisi_ref_p = true;
4257 /* If the type is a padded type and the underlying type would not
4258 be passed by reference or the function has a foreign convention,
4259 return the underlying type. */
4260 else if (TYPE_IS_PADDING_P (gnu_return_type)
4261 && (!default_pass_by_ref
4262 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4263 || Has_Foreign_Convention (gnat_entity)))
4264 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4266 /* If the return type is unconstrained, that means it must have a
4267 maximum size. Use the padded type as the effective return type.
4268 And ensure the function uses the target's by-invisible-reference
4269 mechanism to avoid copying too much data when it returns. */
4270 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4272 tree orig_type = gnu_return_type;
4274 gnu_return_type
4275 = maybe_pad_type (gnu_return_type,
4276 max_size (TYPE_SIZE (gnu_return_type),
4277 true),
4278 0, gnat_entity, false, false, false, true);
4280 /* Declare it now since it will never be declared otherwise.
4281 This is necessary to ensure that its subtrees are properly
4282 marked. */
4283 if (gnu_return_type != orig_type
4284 && !DECL_P (TYPE_NAME (gnu_return_type)))
4285 create_type_decl (TYPE_NAME (gnu_return_type),
4286 gnu_return_type, true, debug_info_p,
4287 gnat_entity);
4289 return_by_invisi_ref_p = true;
4292 /* If the return type has a size that overflows, we cannot have
4293 a function that returns that type. This usage doesn't make
4294 sense anyway, so give an error here. */
4295 if (TYPE_SIZE_UNIT (gnu_return_type)
4296 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4297 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4299 post_error ("cannot return type whose size overflows",
4300 gnat_entity);
4301 gnu_return_type = copy_node (gnu_return_type);
4302 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4303 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4304 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4305 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4309 /* Loop over the parameters and get their associated GCC tree. While
4310 doing this, build a copy-in copy-out structure if we need one. */
4311 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4312 Present (gnat_param);
4313 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4315 Entity_Id gnat_param_type = Etype (gnat_param);
4316 tree gnu_param_name = get_entity_name (gnat_param);
4317 tree gnu_param_type, gnu_param, gnu_field;
4318 Mechanism_Type mech = Mechanism (gnat_param);
4319 bool copy_in_copy_out = false, fake_param_type;
4321 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4322 context may now appear in parameter and result profiles. If
4323 we are only annotating types, break circularities here. */
4324 if (type_annotate_only
4325 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4326 && From_Limited_With (Etype (gnat_param_type))
4327 && In_Extended_Main_Code_Unit
4328 (Non_Limited_View (gnat_param_type))
4329 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4331 gnu_param_type = ptr_void_type_node;
4332 fake_param_type = true;
4334 else
4336 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4337 fake_param_type = false;
4340 /* Builtins are expanded inline and there is no real call sequence
4341 involved. So the type expected by the underlying expander is
4342 always the type of each argument "as is". */
4343 if (gnu_builtin_decl)
4344 mech = By_Copy;
4345 /* Handle the first parameter of a valued procedure specially. */
4346 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4347 mech = By_Copy_Return;
4348 /* Otherwise, see if a Mechanism was supplied that forced this
4349 parameter to be passed one way or another. */
4350 else if (mech == Default
4351 || mech == By_Copy
4352 || mech == By_Reference)
4354 else if (mech > 0)
4356 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4357 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4358 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4359 mech))
4360 mech = By_Reference;
4361 else
4362 mech = By_Copy;
4364 else
4366 post_error ("unsupported mechanism for&", gnat_param);
4367 mech = Default;
4370 /* Do not call gnat_to_gnu_param for a fake parameter type since
4371 it will try to use the real type again. */
4372 if (fake_param_type)
4374 if (Ekind (gnat_param) == E_Out_Parameter)
4375 gnu_param = NULL_TREE;
4376 else
4378 gnu_param
4379 = create_param_decl (gnu_param_name, gnu_param_type,
4380 false);
4381 Set_Mechanism (gnat_param,
4382 mech == Default ? By_Copy : mech);
4383 if (Ekind (gnat_param) == E_In_Out_Parameter)
4384 copy_in_copy_out = true;
4387 else
4388 gnu_param
4389 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4390 Has_Foreign_Convention (gnat_entity),
4391 &copy_in_copy_out);
4393 /* We are returned either a PARM_DECL or a type if no parameter
4394 needs to be passed; in either case, adjust the type. */
4395 if (DECL_P (gnu_param))
4396 gnu_param_type = TREE_TYPE (gnu_param);
4397 else
4399 gnu_param_type = gnu_param;
4400 gnu_param = NULL_TREE;
4403 /* The failure of this assertion will very likely come from an
4404 order of elaboration issue for the type of the parameter. */
4405 gcc_assert (kind == E_Subprogram_Type
4406 || !TYPE_IS_DUMMY_P (gnu_param_type)
4407 || type_annotate_only);
4409 if (gnu_param)
4411 gnu_param_list = chainon (gnu_param, gnu_param_list);
4412 Sloc_to_locus (Sloc (gnat_param),
4413 &DECL_SOURCE_LOCATION (gnu_param));
4414 save_gnu_tree (gnat_param, gnu_param, false);
4416 /* If a parameter is a pointer, this function may modify
4417 memory through it and thus shouldn't be considered
4418 a const function. Also, the memory may be modified
4419 between two calls, so they can't be CSE'ed. The latter
4420 case also handles by-ref parameters. */
4421 if (POINTER_TYPE_P (gnu_param_type)
4422 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4423 const_flag = false;
4426 if (copy_in_copy_out)
4428 if (!gnu_cico_list)
4430 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4432 /* If this is a function, we also need a field for the
4433 return value to be placed. */
4434 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4436 gnu_field
4437 = create_field_decl (get_identifier ("RETVAL"),
4438 gnu_return_type,
4439 gnu_new_ret_type, NULL_TREE,
4440 NULL_TREE, 0, 0);
4441 Sloc_to_locus (Sloc (gnat_entity),
4442 &DECL_SOURCE_LOCATION (gnu_field));
4443 gnu_field_list = gnu_field;
4444 gnu_cico_list
4445 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4448 gnu_return_type = gnu_new_ret_type;
4449 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4450 /* Set a default alignment to speed up accesses. But we
4451 shouldn't increase the size of the structure too much,
4452 lest it doesn't fit in return registers anymore. */
4453 TYPE_ALIGN (gnu_return_type)
4454 = get_mode_alignment (ptr_mode);
4457 gnu_field
4458 = create_field_decl (gnu_param_name, gnu_param_type,
4459 gnu_return_type, NULL_TREE, NULL_TREE,
4460 0, 0);
4461 Sloc_to_locus (Sloc (gnat_param),
4462 &DECL_SOURCE_LOCATION (gnu_field));
4463 DECL_CHAIN (gnu_field) = gnu_field_list;
4464 gnu_field_list = gnu_field;
4465 gnu_cico_list
4466 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4470 if (gnu_cico_list)
4472 /* If we have a CICO list but it has only one entry, we convert
4473 this function into a function that returns this object. */
4474 if (list_length (gnu_cico_list) == 1)
4475 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4477 /* Do not finalize the return type if the subprogram is stubbed
4478 since structures are incomplete for the back-end. */
4479 else if (Convention (gnat_entity) != Convention_Stubbed)
4481 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4482 0, false);
4484 /* Try to promote the mode of the return type if it is passed
4485 in registers, again to speed up accesses. */
4486 if (TYPE_MODE (gnu_return_type) == BLKmode
4487 && !targetm.calls.return_in_memory (gnu_return_type,
4488 NULL_TREE))
4490 unsigned int size
4491 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4492 unsigned int i = BITS_PER_UNIT;
4493 machine_mode mode;
4495 while (i < size)
4496 i <<= 1;
4497 mode = mode_for_size (i, MODE_INT, 0);
4498 if (mode != BLKmode)
4500 SET_TYPE_MODE (gnu_return_type, mode);
4501 TYPE_ALIGN (gnu_return_type)
4502 = GET_MODE_ALIGNMENT (mode);
4503 TYPE_SIZE (gnu_return_type)
4504 = bitsize_int (GET_MODE_BITSIZE (mode));
4505 TYPE_SIZE_UNIT (gnu_return_type)
4506 = size_int (GET_MODE_SIZE (mode));
4510 if (debug_info_p)
4511 rest_of_record_type_compilation (gnu_return_type);
4515 /* Deal with platform-specific calling conventions. */
4516 if (Has_Stdcall_Convention (gnat_entity))
4517 prepend_one_attribute
4518 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4519 get_identifier ("stdcall"), NULL_TREE,
4520 gnat_entity);
4521 else if (Has_Thiscall_Convention (gnat_entity))
4522 prepend_one_attribute
4523 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4524 get_identifier ("thiscall"), NULL_TREE,
4525 gnat_entity);
4527 /* If we should request stack realignment for a foreign convention
4528 subprogram, do so. Note that this applies to task entry points
4529 in particular. */
4530 if (FOREIGN_FORCE_REALIGN_STACK
4531 && Has_Foreign_Convention (gnat_entity))
4532 prepend_one_attribute
4533 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4534 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4535 gnat_entity);
4537 /* Deal with a pragma Linker_Section on a subprogram. */
4538 if ((kind == E_Function || kind == E_Procedure)
4539 && Present (Linker_Section_Pragma (gnat_entity)))
4540 prepend_one_attribute_pragma (&attr_list,
4541 Linker_Section_Pragma (gnat_entity));
4543 /* The lists have been built in reverse. */
4544 gnu_param_list = nreverse (gnu_param_list);
4545 gnu_cico_list = nreverse (gnu_cico_list);
4547 if (kind == E_Function)
4548 Set_Mechanism (gnat_entity, return_unconstrained_p
4549 || return_by_direct_ref_p
4550 || return_by_invisi_ref_p
4551 ? By_Reference : By_Copy);
4552 gnu_type
4553 = create_subprog_type (gnu_return_type, gnu_param_list,
4554 gnu_cico_list, return_unconstrained_p,
4555 return_by_direct_ref_p,
4556 return_by_invisi_ref_p);
4558 /* A subprogram (something that doesn't return anything) shouldn't
4559 be considered const since there would be no reason for such a
4560 subprogram. Note that procedures with Out (or In Out) parameters
4561 have already been converted into a function with a return type. */
4562 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4563 const_flag = false;
4565 if (const_flag || volatile_flag)
4567 const int quals
4568 = (const_flag ? TYPE_QUAL_CONST : 0)
4569 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4571 gnu_type = change_qualified_type (gnu_type, quals);
4574 /* If we have a builtin decl for that function, use it. Check if the
4575 profiles are compatible and warn if they are not. The checker is
4576 expected to post extra diagnostics in this case. */
4577 if (gnu_builtin_decl)
4579 intrin_binding_t inb;
4581 inb.gnat_entity = gnat_entity;
4582 inb.ada_fntype = gnu_type;
4583 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4585 if (!intrin_profiles_compatible_p (&inb))
4586 post_error
4587 ("?profile of& doesn''t match the builtin it binds!",
4588 gnat_entity);
4590 gnu_decl = gnu_builtin_decl;
4591 gnu_type = TREE_TYPE (gnu_builtin_decl);
4592 break;
4595 /* If there was no specified Interface_Name and the external and
4596 internal names of the subprogram are the same, only use the
4597 internal name to allow disambiguation of nested subprograms. */
4598 if (No (Interface_Name (gnat_entity))
4599 && gnu_ext_name == gnu_entity_name)
4600 gnu_ext_name = NULL_TREE;
4602 /* If we are defining the subprogram and it has an Address clause
4603 we must get the address expression from the saved GCC tree for the
4604 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4605 the address expression here since the front-end has guaranteed
4606 in that case that the elaboration has no effects. If there is
4607 an Address clause and we are not defining the object, just
4608 make it a constant. */
4609 if (Present (Address_Clause (gnat_entity)))
4611 tree gnu_address = NULL_TREE;
4613 if (definition)
4614 gnu_address
4615 = (present_gnu_tree (gnat_entity)
4616 ? get_gnu_tree (gnat_entity)
4617 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4619 save_gnu_tree (gnat_entity, NULL_TREE, false);
4621 /* Convert the type of the object to a reference type that can
4622 alias everything as per 13.3(19). */
4623 gnu_type
4624 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4625 if (gnu_address)
4626 gnu_address = convert (gnu_type, gnu_address);
4628 gnu_decl
4629 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4630 gnu_address, false, Is_Public (gnat_entity),
4631 extern_flag, false, NULL, gnat_entity);
4632 DECL_BY_REF_P (gnu_decl) = 1;
4635 else if (kind == E_Subprogram_Type)
4637 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4638 gnu_decl
4639 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4640 debug_info_p, gnat_entity);
4642 else
4644 gnu_decl
4645 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4646 gnu_param_list, inline_status,
4647 public_flag, extern_flag, artificial_flag,
4648 attr_list, gnat_entity);
4649 /* This is unrelated to the stub built right above. */
4650 DECL_STUBBED_P (gnu_decl)
4651 = Convention (gnat_entity) == Convention_Stubbed;
4654 break;
4656 case E_Incomplete_Type:
4657 case E_Incomplete_Subtype:
4658 case E_Private_Type:
4659 case E_Private_Subtype:
4660 case E_Limited_Private_Type:
4661 case E_Limited_Private_Subtype:
4662 case E_Record_Type_With_Private:
4663 case E_Record_Subtype_With_Private:
4665 /* Get the "full view" of this entity. If this is an incomplete
4666 entity from a limited with, treat its non-limited view as the
4667 full view. Otherwise, use either the full view or the underlying
4668 full view, whichever is present. This is used in all the tests
4669 below. */
4670 Entity_Id full_view
4671 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4672 ? Non_Limited_View (gnat_entity)
4673 : Present (Full_View (gnat_entity))
4674 ? Full_View (gnat_entity)
4675 : IN (kind, Private_Kind)
4676 ? Underlying_Full_View (gnat_entity)
4677 : Empty;
4679 /* If this is an incomplete type with no full view, it must be a Taft
4680 Amendment type, in which case we return a dummy type. Otherwise,
4681 just get the type from its Etype. */
4682 if (No (full_view))
4684 if (kind == E_Incomplete_Type)
4686 gnu_type = make_dummy_type (gnat_entity);
4687 gnu_decl = TYPE_STUB_DECL (gnu_type);
4689 else
4691 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4692 NULL_TREE, 0);
4693 maybe_present = true;
4695 break;
4698 /* If we already made a type for the full view, reuse it. */
4699 else if (present_gnu_tree (full_view))
4701 gnu_decl = get_gnu_tree (full_view);
4702 break;
4705 /* Otherwise, if we are not defining the type now, get the type
4706 from the full view. But always get the type from the full view
4707 for define on use types, since otherwise we won't see them! */
4708 else if (!definition
4709 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4710 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4712 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4713 maybe_present = true;
4714 break;
4717 /* For incomplete types, make a dummy type entry which will be
4718 replaced later. Save it as the full declaration's type so
4719 we can do any needed updates when we see it. */
4720 gnu_type = make_dummy_type (gnat_entity);
4721 gnu_decl = TYPE_STUB_DECL (gnu_type);
4722 if (Has_Completion_In_Body (gnat_entity))
4723 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4724 save_gnu_tree (full_view, gnu_decl, 0);
4725 break;
4728 case E_Class_Wide_Type:
4729 /* Class-wide types are always transformed into their root type. */
4730 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4731 maybe_present = true;
4732 break;
4734 case E_Task_Type:
4735 case E_Task_Subtype:
4736 case E_Protected_Type:
4737 case E_Protected_Subtype:
4738 /* Concurrent types are always transformed into their record type. */
4739 if (type_annotate_only && No (gnat_equiv_type))
4740 gnu_type = void_type_node;
4741 else
4742 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4743 maybe_present = true;
4744 break;
4746 case E_Label:
4747 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4748 break;
4750 case E_Block:
4751 case E_Loop:
4752 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4753 we've already saved it, so we don't try to. */
4754 gnu_decl = error_mark_node;
4755 saved = true;
4756 break;
4758 case E_Abstract_State:
4759 /* This is a SPARK annotation that only reaches here when compiling in
4760 ASIS mode and has no characteristics to annotate. */
4761 gcc_assert (type_annotate_only);
4762 return error_mark_node;
4764 default:
4765 gcc_unreachable ();
4768 /* If we had a case where we evaluated another type and it might have
4769 defined this one, handle it here. */
4770 if (maybe_present && present_gnu_tree (gnat_entity))
4772 gnu_decl = get_gnu_tree (gnat_entity);
4773 saved = true;
4776 /* If we are processing a type and there is either no decl for it or
4777 we just made one, do some common processing for the type, such as
4778 handling alignment and possible padding. */
4779 if (is_type && (!gnu_decl || this_made_decl))
4781 /* Process the attributes, if not already done. Note that the type is
4782 already defined so we cannot pass true for IN_PLACE here. */
4783 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4785 /* Tell the middle-end that objects of tagged types are guaranteed to
4786 be properly aligned. This is necessary because conversions to the
4787 class-wide type are translated into conversions to the root type,
4788 which can be less aligned than some of its derived types. */
4789 if (Is_Tagged_Type (gnat_entity)
4790 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4791 TYPE_ALIGN_OK (gnu_type) = 1;
4793 /* Record whether the type is passed by reference. */
4794 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4795 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4797 /* ??? Don't set the size for a String_Literal since it is either
4798 confirming or we don't handle it properly (if the low bound is
4799 non-constant). */
4800 if (!gnu_size && kind != E_String_Literal_Subtype)
4802 Uint gnat_size = Known_Esize (gnat_entity)
4803 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4804 gnu_size
4805 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4806 false, Has_Size_Clause (gnat_entity));
4809 /* If a size was specified, see if we can make a new type of that size
4810 by rearranging the type, for example from a fat to a thin pointer. */
4811 if (gnu_size)
4813 gnu_type
4814 = make_type_from_size (gnu_type, gnu_size,
4815 Has_Biased_Representation (gnat_entity));
4817 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4818 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4819 gnu_size = NULL_TREE;
4822 /* If the alignment has not already been processed and this is not
4823 an unconstrained array type, see if an alignment is specified.
4824 If not, we pick a default alignment for atomic objects. */
4825 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4827 else if (Known_Alignment (gnat_entity))
4829 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4830 TYPE_ALIGN (gnu_type));
4832 /* Warn on suspiciously large alignments. This should catch
4833 errors about the (alignment,byte)/(size,bit) discrepancy. */
4834 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4836 tree size;
4838 /* If a size was specified, take it into account. Otherwise
4839 use the RM size for records or unions as the type size has
4840 already been adjusted to the alignment. */
4841 if (gnu_size)
4842 size = gnu_size;
4843 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4844 && !TYPE_FAT_POINTER_P (gnu_type))
4845 size = rm_size (gnu_type);
4846 else
4847 size = TYPE_SIZE (gnu_type);
4849 /* Consider an alignment as suspicious if the alignment/size
4850 ratio is greater or equal to the byte/bit ratio. */
4851 if (tree_fits_uhwi_p (size)
4852 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4853 post_error_ne ("?suspiciously large alignment specified for&",
4854 Expression (Alignment_Clause (gnat_entity)),
4855 gnat_entity);
4858 else if (Is_Atomic (gnat_entity) && !gnu_size
4859 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4860 && integer_pow2p (TYPE_SIZE (gnu_type)))
4861 align = MIN (BIGGEST_ALIGNMENT,
4862 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4863 else if (Is_Atomic (gnat_entity) && gnu_size
4864 && tree_fits_uhwi_p (gnu_size)
4865 && integer_pow2p (gnu_size))
4866 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4868 /* See if we need to pad the type. If we did, and made a record,
4869 the name of the new type may be changed. So get it back for
4870 us when we make the new TYPE_DECL below. */
4871 if (gnu_size || align > 0)
4872 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4873 false, !gnu_decl, definition, false);
4875 if (TYPE_IS_PADDING_P (gnu_type))
4876 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4878 /* Now set the RM size of the type. We cannot do it before padding
4879 because we need to accept arbitrary RM sizes on integral types. */
4880 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4882 /* If we are at global level, GCC will have applied variable_size to
4883 the type, but that won't have done anything. So, if it's not
4884 a constant or self-referential, call elaborate_expression_1 to
4885 make a variable for the size rather than calculating it each time.
4886 Handle both the RM size and the actual size. */
4887 if (global_bindings_p ()
4888 && TYPE_SIZE (gnu_type)
4889 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4890 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4892 tree size = TYPE_SIZE (gnu_type);
4894 TYPE_SIZE (gnu_type)
4895 = elaborate_expression_1 (size, gnat_entity,
4896 get_identifier ("SIZE"),
4897 definition, false);
4899 /* ??? For now, store the size as a multiple of the alignment in
4900 bytes so that we can see the alignment from the tree. */
4901 TYPE_SIZE_UNIT (gnu_type)
4902 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4903 get_identifier ("SIZE_A_UNIT"),
4904 definition, false,
4905 TYPE_ALIGN (gnu_type));
4907 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4908 may not be marked by the call to create_type_decl below. */
4909 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4911 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4913 tree variant_part = get_variant_part (gnu_type);
4914 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4916 if (variant_part)
4918 tree union_type = TREE_TYPE (variant_part);
4919 tree offset = DECL_FIELD_OFFSET (variant_part);
4921 /* If the position of the variant part is constant, subtract
4922 it from the size of the type of the parent to get the new
4923 size. This manual CSE reduces the data size. */
4924 if (TREE_CODE (offset) == INTEGER_CST)
4926 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4927 TYPE_SIZE (union_type)
4928 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4929 bit_from_pos (offset, bitpos));
4930 TYPE_SIZE_UNIT (union_type)
4931 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4932 byte_from_pos (offset, bitpos));
4934 else
4936 TYPE_SIZE (union_type)
4937 = elaborate_expression_1 (TYPE_SIZE (union_type),
4938 gnat_entity,
4939 get_identifier ("VSIZE"),
4940 definition, false);
4942 /* ??? For now, store the size as a multiple of the
4943 alignment in bytes so that we can see the alignment
4944 from the tree. */
4945 TYPE_SIZE_UNIT (union_type)
4946 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4947 gnat_entity,
4948 get_identifier
4949 ("VSIZE_A_UNIT"),
4950 definition, false,
4951 TYPE_ALIGN (union_type));
4953 /* ??? For now, store the offset as a multiple of the
4954 alignment in bytes so that we can see the alignment
4955 from the tree. */
4956 DECL_FIELD_OFFSET (variant_part)
4957 = elaborate_expression_2 (offset,
4958 gnat_entity,
4959 get_identifier ("VOFFSET"),
4960 definition, false,
4961 DECL_OFFSET_ALIGN
4962 (variant_part));
4965 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4966 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4969 if (operand_equal_p (ada_size, size, 0))
4970 ada_size = TYPE_SIZE (gnu_type);
4971 else
4972 ada_size
4973 = elaborate_expression_1 (ada_size, gnat_entity,
4974 get_identifier ("RM_SIZE"),
4975 definition, false);
4976 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4980 /* If this is a record type or subtype, call elaborate_expression_2 on
4981 any field position. Do this for both global and local types.
4982 Skip any fields that we haven't made trees for to avoid problems with
4983 class wide types. */
4984 if (IN (kind, Record_Kind))
4985 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4986 gnat_temp = Next_Entity (gnat_temp))
4987 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4989 tree gnu_field = get_gnu_tree (gnat_temp);
4991 /* ??? For now, store the offset as a multiple of the alignment
4992 in bytes so that we can see the alignment from the tree. */
4993 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4995 DECL_FIELD_OFFSET (gnu_field)
4996 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4997 gnat_temp,
4998 get_identifier ("OFFSET"),
4999 definition, false,
5000 DECL_OFFSET_ALIGN (gnu_field));
5002 /* ??? The context of gnu_field is not necessarily gnu_type
5003 so the MULT_EXPR node built above may not be marked by
5004 the call to create_type_decl below. */
5005 if (global_bindings_p ())
5006 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5010 if (Is_Atomic (gnat_entity))
5011 check_ok_for_atomic (gnu_type, gnat_entity, false);
5013 /* If this is not an unconstrained array type, set some flags. */
5014 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5016 if (Treat_As_Volatile (gnat_entity))
5017 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5019 if (Present (Alignment_Clause (gnat_entity)))
5020 TYPE_USER_ALIGN (gnu_type) = 1;
5022 if (Universal_Aliasing (gnat_entity))
5023 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5026 if (!gnu_decl)
5027 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5028 !Comes_From_Source (gnat_entity),
5029 debug_info_p, gnat_entity);
5030 else
5032 TREE_TYPE (gnu_decl) = gnu_type;
5033 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5037 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5039 gnu_type = TREE_TYPE (gnu_decl);
5041 /* If this is a derived type, relate its alias set to that of its parent
5042 to avoid troubles when a call to an inherited primitive is inlined in
5043 a context where a derived object is accessed. The inlined code works
5044 on the parent view so the resulting code may access the same object
5045 using both the parent and the derived alias sets, which thus have to
5046 conflict. As the same issue arises with component references, the
5047 parent alias set also has to conflict with composite types enclosing
5048 derived components. For instance, if we have:
5050 type D is new T;
5051 type R is record
5052 Component : D;
5053 end record;
5055 we want T to conflict with both D and R, in addition to R being a
5056 superset of D by record/component construction.
5058 One way to achieve this is to perform an alias set copy from the
5059 parent to the derived type. This is not quite appropriate, though,
5060 as we don't want separate derived types to conflict with each other:
5062 type I1 is new Integer;
5063 type I2 is new Integer;
5065 We want I1 and I2 to both conflict with Integer but we do not want
5066 I1 to conflict with I2, and an alias set copy on derivation would
5067 have that effect.
5069 The option chosen is to make the alias set of the derived type a
5070 superset of that of its parent type. It trivially fulfills the
5071 simple requirement for the Integer derivation example above, and
5072 the component case as well by superset transitivity:
5074 superset superset
5075 R ----------> D ----------> T
5077 However, for composite types, conversions between derived types are
5078 translated into VIEW_CONVERT_EXPRs so a sequence like:
5080 type Comp1 is new Comp;
5081 type Comp2 is new Comp;
5082 procedure Proc (C : Comp1);
5084 C : Comp2;
5085 Proc (Comp1 (C));
5087 is translated into:
5089 C : Comp2;
5090 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5092 and gimplified into:
5094 C : Comp2;
5095 Comp1 *C.0;
5096 C.0 = (Comp1 *) &C;
5097 Proc (C.0);
5099 i.e. generates code involving type punning. Therefore, Comp1 needs
5100 to conflict with Comp2 and an alias set copy is required.
5102 The language rules ensure the parent type is already frozen here. */
5103 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5105 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5106 /* For constrained packed array subtypes, the implementation type is
5107 used instead of the nominal type. */
5108 if (kind == E_Array_Subtype
5109 && Is_Constrained (gnat_entity)
5110 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5111 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5112 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5113 Is_Composite_Type (gnat_entity)
5114 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5117 /* Back-annotate the Alignment of the type if not already in the
5118 tree. Likewise for sizes. */
5119 if (Unknown_Alignment (gnat_entity))
5121 unsigned int double_align, align;
5122 bool is_capped_double, align_clause;
5124 /* If the default alignment of "double" or larger scalar types is
5125 specifically capped and this is not an array with an alignment
5126 clause on the component type, return the cap. */
5127 if ((double_align = double_float_alignment) > 0)
5128 is_capped_double
5129 = is_double_float_or_array (gnat_entity, &align_clause);
5130 else if ((double_align = double_scalar_alignment) > 0)
5131 is_capped_double
5132 = is_double_scalar_or_array (gnat_entity, &align_clause);
5133 else
5134 is_capped_double = align_clause = false;
5136 if (is_capped_double && !align_clause)
5137 align = double_align;
5138 else
5139 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5141 Set_Alignment (gnat_entity, UI_From_Int (align));
5144 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5146 tree gnu_size = TYPE_SIZE (gnu_type);
5148 /* If the size is self-referential, annotate the maximum value. */
5149 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5150 gnu_size = max_size (gnu_size, true);
5152 /* If we are just annotating types and the type is tagged, the tag
5153 and the parent components are not generated by the front-end so
5154 sizes must be adjusted if there is no representation clause. */
5155 if (type_annotate_only
5156 && Is_Tagged_Type (gnat_entity)
5157 && !VOID_TYPE_P (gnu_type)
5158 && (!TYPE_FIELDS (gnu_type)
5159 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5161 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5162 Uint uint_size;
5164 if (Is_Derived_Type (gnat_entity))
5166 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5167 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5168 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5170 else
5171 offset = pointer_size;
5173 if (TYPE_FIELDS (gnu_type))
5174 offset
5175 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5177 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5178 gnu_size = round_up (gnu_size, POINTER_SIZE);
5179 uint_size = annotate_value (gnu_size);
5180 Set_Esize (gnat_entity, uint_size);
5181 Set_RM_Size (gnat_entity, uint_size);
5183 else
5184 Set_Esize (gnat_entity, annotate_value (gnu_size));
5187 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5188 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5191 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5192 cannot do so if we are reusing the ..._DECL node made for an equivalent
5193 type or an alias or a renamed object as the predicates don't apply to it
5194 but to GNAT_ENTITY. */
5195 if (DECL_P (gnu_decl)
5196 && !(is_type && gnat_equiv_type != gnat_entity)
5197 && !Present (Alias (gnat_entity))
5198 && !(Present (Renamed_Object (gnat_entity)) && saved))
5200 if (!Comes_From_Source (gnat_entity))
5201 DECL_ARTIFICIAL (gnu_decl) = 1;
5203 if (!debug_info_p)
5204 DECL_IGNORED_P (gnu_decl) = 1;
5207 /* If we haven't already, associate the ..._DECL node that we just made with
5208 the input GNAT entity node. */
5209 if (!saved)
5210 save_gnu_tree (gnat_entity, gnu_decl, false);
5212 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5213 eliminate as many deferred computations as possible. */
5214 process_deferred_decl_context (false);
5216 /* If this is an enumeration or floating-point type, we were not able to set
5217 the bounds since they refer to the type. These are always static. */
5218 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5219 || (kind == E_Floating_Point_Type))
5221 tree gnu_scalar_type = gnu_type;
5222 tree gnu_low_bound, gnu_high_bound;
5224 /* If this is a padded type, we need to use the underlying type. */
5225 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5226 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5228 /* If this is a floating point type and we haven't set a floating
5229 point type yet, use this in the evaluation of the bounds. */
5230 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5231 longest_float_type_node = gnu_scalar_type;
5233 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5234 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5236 if (kind == E_Enumeration_Type)
5238 /* Enumeration types have specific RM bounds. */
5239 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5240 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5242 else
5244 /* Floating-point types don't have specific RM bounds. */
5245 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5246 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5250 /* If we deferred processing of incomplete types, re-enable it. If there
5251 were no other disables and we have deferred types to process, do so. */
5252 if (this_deferred
5253 && --defer_incomplete_level == 0
5254 && defer_incomplete_list)
5256 struct incomplete *p, *next;
5258 /* We are back to level 0 for the deferring of incomplete types.
5259 But processing these incomplete types below may itself require
5260 deferring, so preserve what we have and restart from scratch. */
5261 p = defer_incomplete_list;
5262 defer_incomplete_list = NULL;
5264 for (; p; p = next)
5266 next = p->next;
5268 if (p->old_type)
5269 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5270 gnat_to_gnu_type (p->full_type));
5271 free (p);
5275 /* If we are not defining this type, see if it's on one of the lists of
5276 incomplete types. If so, handle the list entry now. */
5277 if (is_type && !definition)
5279 struct incomplete *p;
5281 for (p = defer_incomplete_list; p; p = p->next)
5282 if (p->old_type && p->full_type == gnat_entity)
5284 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5285 TREE_TYPE (gnu_decl));
5286 p->old_type = NULL_TREE;
5289 for (p = defer_limited_with; p; p = p->next)
5290 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5292 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5293 TREE_TYPE (gnu_decl));
5294 p->old_type = NULL_TREE;
5298 if (this_global)
5299 force_global--;
5301 /* If this is a packed array type whose original array type is itself
5302 an Itype without freeze node, make sure the latter is processed. */
5303 if (Is_Packed_Array_Impl_Type (gnat_entity)
5304 && Is_Itype (Original_Array_Type (gnat_entity))
5305 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5306 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5307 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5309 return gnu_decl;
5312 /* Similar, but if the returned value is a COMPONENT_REF, return the
5313 FIELD_DECL. */
5315 tree
5316 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5318 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5320 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5321 gnu_field = TREE_OPERAND (gnu_field, 1);
5323 return gnu_field;
5326 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5327 the GCC type corresponding to that entity. */
5329 tree
5330 gnat_to_gnu_type (Entity_Id gnat_entity)
5332 tree gnu_decl;
5334 /* The back end never attempts to annotate generic types. */
5335 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5336 return void_type_node;
5338 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5339 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5341 return TREE_TYPE (gnu_decl);
5344 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5345 the unpadded version of the GCC type corresponding to that entity. */
5347 tree
5348 get_unpadded_type (Entity_Id gnat_entity)
5350 tree type = gnat_to_gnu_type (gnat_entity);
5352 if (TYPE_IS_PADDING_P (type))
5353 type = TREE_TYPE (TYPE_FIELDS (type));
5355 return type;
5358 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5359 type has been changed to that of the parameterless procedure, except if an
5360 alias is already present, in which case it is returned instead. */
5362 tree
5363 get_minimal_subprog_decl (Entity_Id gnat_entity)
5365 tree gnu_entity_name, gnu_ext_name;
5366 struct attrib *attr_list = NULL;
5368 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5369 of the handling applied here. */
5371 while (Present (Alias (gnat_entity)))
5373 gnat_entity = Alias (gnat_entity);
5374 if (present_gnu_tree (gnat_entity))
5375 return get_gnu_tree (gnat_entity);
5378 gnu_entity_name = get_entity_name (gnat_entity);
5379 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5381 if (Has_Stdcall_Convention (gnat_entity))
5382 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5383 get_identifier ("stdcall"), NULL_TREE,
5384 gnat_entity);
5385 else if (Has_Thiscall_Convention (gnat_entity))
5386 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5387 get_identifier ("thiscall"), NULL_TREE,
5388 gnat_entity);
5390 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5391 gnu_ext_name = NULL_TREE;
5393 return
5394 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5395 is_disabled, true, true, true, attr_list, gnat_entity);
5398 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5399 a C++ imported method or equivalent.
5401 We use the predicate on 32-bit x86/Windows to find out whether we need to
5402 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5403 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5405 bool
5406 is_cplusplus_method (Entity_Id gnat_entity)
5408 if (Convention (gnat_entity) != Convention_CPP)
5409 return false;
5411 /* This is the main case: C++ method imported as a primitive operation. */
5412 if (Is_Dispatching_Operation (gnat_entity))
5413 return true;
5415 /* A thunk needs to be handled like its associated primitive operation. */
5416 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5417 return true;
5419 /* C++ classes with no virtual functions can be imported as limited
5420 record types, but we need to return true for the constructors. */
5421 if (Is_Constructor (gnat_entity))
5422 return true;
5424 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5425 if (Is_Dispatch_Table_Entity (gnat_entity))
5426 return true;
5428 return false;
5431 /* Finalize the processing of From_Limited_With incomplete types. */
5433 void
5434 finalize_from_limited_with (void)
5436 struct incomplete *p, *next;
5438 p = defer_limited_with;
5439 defer_limited_with = NULL;
5441 for (; p; p = next)
5443 next = p->next;
5445 if (p->old_type)
5446 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5447 gnat_to_gnu_type (p->full_type));
5448 free (p);
5452 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5453 kind of type (such E_Task_Type) that has a different type which Gigi
5454 uses for its representation. If the type does not have a special type
5455 for its representation, return GNAT_ENTITY. If a type is supposed to
5456 exist, but does not, abort unless annotating types, in which case
5457 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5459 Entity_Id
5460 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5462 Entity_Id gnat_equiv = gnat_entity;
5464 if (No (gnat_entity))
5465 return gnat_entity;
5467 switch (Ekind (gnat_entity))
5469 case E_Class_Wide_Subtype:
5470 if (Present (Equivalent_Type (gnat_entity)))
5471 gnat_equiv = Equivalent_Type (gnat_entity);
5472 break;
5474 case E_Access_Protected_Subprogram_Type:
5475 case E_Anonymous_Access_Protected_Subprogram_Type:
5476 gnat_equiv = Equivalent_Type (gnat_entity);
5477 break;
5479 case E_Class_Wide_Type:
5480 gnat_equiv = Root_Type (gnat_entity);
5481 break;
5483 case E_Task_Type:
5484 case E_Task_Subtype:
5485 case E_Protected_Type:
5486 case E_Protected_Subtype:
5487 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5488 break;
5490 default:
5491 break;
5494 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5496 return gnat_equiv;
5499 /* Return a GCC tree for a type corresponding to the component type of the
5500 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5501 is for an array being defined. DEBUG_INFO_P is true if we need to write
5502 debug information for other types that we may create in the process. */
5504 static tree
5505 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5506 bool debug_info_p)
5508 const Entity_Id gnat_type = Component_Type (gnat_array);
5509 tree gnu_type = gnat_to_gnu_type (gnat_type);
5510 tree gnu_comp_size;
5512 /* Try to get a smaller form of the component if needed. */
5513 if ((Is_Packed (gnat_array)
5514 || Has_Component_Size_Clause (gnat_array))
5515 && !Is_Bit_Packed_Array (gnat_array)
5516 && !Has_Aliased_Components (gnat_array)
5517 && !Strict_Alignment (gnat_type)
5518 && RECORD_OR_UNION_TYPE_P (gnu_type)
5519 && !TYPE_FAT_POINTER_P (gnu_type)
5520 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5521 gnu_type = make_packable_type (gnu_type, false);
5523 if (Has_Atomic_Components (gnat_array))
5524 check_ok_for_atomic (gnu_type, gnat_array, true);
5526 /* Get and validate any specified Component_Size. */
5527 gnu_comp_size
5528 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5529 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5530 true, Has_Component_Size_Clause (gnat_array));
5532 /* If the array has aliased components and the component size can be zero,
5533 force at least unit size to ensure that the components have distinct
5534 addresses. */
5535 if (!gnu_comp_size
5536 && Has_Aliased_Components (gnat_array)
5537 && (integer_zerop (TYPE_SIZE (gnu_type))
5538 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5539 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5540 gnu_comp_size
5541 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5543 /* If the component type is a RECORD_TYPE that has a self-referential size,
5544 then use the maximum size for the component size. */
5545 if (!gnu_comp_size
5546 && TREE_CODE (gnu_type) == RECORD_TYPE
5547 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5548 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5550 /* Honor the component size. This is not needed for bit-packed arrays. */
5551 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5553 tree orig_type = gnu_type;
5554 unsigned int max_align;
5556 /* If an alignment is specified, use it as a cap on the component type
5557 so that it can be honored for the whole type. But ignore it for the
5558 original type of packed array types. */
5559 if (No (Packed_Array_Impl_Type (gnat_array))
5560 && Known_Alignment (gnat_array))
5561 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5562 else
5563 max_align = 0;
5565 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5566 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5567 gnu_type = orig_type;
5568 else
5569 orig_type = gnu_type;
5571 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5572 true, false, definition, true);
5574 /* If a padding record was made, declare it now since it will never be
5575 declared otherwise. This is necessary to ensure that its subtrees
5576 are properly marked. */
5577 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5578 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5579 gnat_array);
5582 if (Has_Volatile_Components (gnat_array))
5583 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5585 return gnu_type;
5588 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5589 using MECH as its passing mechanism, to be placed in the parameter
5590 list built for GNAT_SUBPROG. Assume a foreign convention for the
5591 latter if FOREIGN is true. Also set CICO to true if the parameter
5592 must use the copy-in copy-out implementation mechanism.
5594 The returned tree is a PARM_DECL, except for those cases where no
5595 parameter needs to be actually passed to the subprogram; the type
5596 of this "shadow" parameter is then returned instead. */
5598 static tree
5599 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5600 Entity_Id gnat_subprog, bool foreign, bool *cico)
5602 tree gnu_param_name = get_entity_name (gnat_param);
5603 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5604 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5605 /* The parameter can be indirectly modified if its address is taken. */
5606 bool ro_param = in_param && !Address_Taken (gnat_param);
5607 bool by_return = false, by_component_ptr = false;
5608 bool by_ref = false;
5609 tree gnu_param;
5611 /* Copy-return is used only for the first parameter of a valued procedure.
5612 It's a copy mechanism for which a parameter is never allocated. */
5613 if (mech == By_Copy_Return)
5615 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5616 mech = By_Copy;
5617 by_return = true;
5620 /* If this is either a foreign function or if the underlying type won't
5621 be passed by reference, strip off possible padding type. */
5622 if (TYPE_IS_PADDING_P (gnu_param_type))
5624 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5626 if (mech == By_Reference
5627 || foreign
5628 || (!must_pass_by_ref (unpadded_type)
5629 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5630 gnu_param_type = unpadded_type;
5633 /* If this is a read-only parameter, make a variant of the type that is
5634 read-only. ??? However, if this is an unconstrained array, that type
5635 can be very complex, so skip it for now. Likewise for any other
5636 self-referential type. */
5637 if (ro_param
5638 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5639 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5640 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5642 /* For foreign conventions, pass arrays as pointers to the element type.
5643 First check for unconstrained array and get the underlying array. */
5644 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5645 gnu_param_type
5646 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5648 /* For GCC builtins, pass Address integer types as (void *) */
5649 if (Convention (gnat_subprog) == Convention_Intrinsic
5650 && Present (Interface_Name (gnat_subprog))
5651 && Is_Descendent_Of_Address (Etype (gnat_param)))
5652 gnu_param_type = ptr_void_type_node;
5654 /* Arrays are passed as pointers to element type for foreign conventions. */
5655 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5657 /* Strip off any multi-dimensional entries, then strip
5658 off the last array to get the component type. */
5659 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5660 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5661 gnu_param_type = TREE_TYPE (gnu_param_type);
5663 by_component_ptr = true;
5664 gnu_param_type = TREE_TYPE (gnu_param_type);
5666 if (ro_param)
5667 gnu_param_type
5668 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5670 gnu_param_type = build_pointer_type (gnu_param_type);
5673 /* Fat pointers are passed as thin pointers for foreign conventions. */
5674 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5675 gnu_param_type
5676 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5678 /* If we must pass or were requested to pass by reference, do so.
5679 If we were requested to pass by copy, do so.
5680 Otherwise, for foreign conventions, pass In Out or Out parameters
5681 or aggregates by reference. For COBOL and Fortran, pass all
5682 integer and FP types that way too. For Convention Ada, use
5683 the standard Ada default. */
5684 else if (must_pass_by_ref (gnu_param_type)
5685 || mech == By_Reference
5686 || (mech != By_Copy
5687 && ((foreign
5688 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5689 || (foreign
5690 && (Convention (gnat_subprog) == Convention_Fortran
5691 || Convention (gnat_subprog) == Convention_COBOL)
5692 && (INTEGRAL_TYPE_P (gnu_param_type)
5693 || FLOAT_TYPE_P (gnu_param_type)))
5694 || (!foreign
5695 && default_pass_by_ref (gnu_param_type)))))
5697 /* We take advantage of 6.2(12) by considering that references built for
5698 parameters whose type isn't by-ref and for which the mechanism hasn't
5699 been forced to by-ref are restrict-qualified in the C sense. */
5700 bool restrict_p
5701 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5702 gnu_param_type = build_reference_type (gnu_param_type);
5703 if (restrict_p)
5704 gnu_param_type
5705 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5706 by_ref = true;
5709 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5710 else if (!in_param)
5711 *cico = true;
5713 if (mech == By_Copy && (by_ref || by_component_ptr))
5714 post_error ("?cannot pass & by copy", gnat_param);
5716 /* If this is an Out parameter that isn't passed by reference and isn't
5717 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5718 it will be a VAR_DECL created when we process the procedure, so just
5719 return its type. For the special parameter of a valued procedure,
5720 never pass it in.
5722 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5723 Out parameters with discriminants or implicit initial values to be
5724 handled like In Out parameters. These type are normally built as
5725 aggregates, hence passed by reference, except for some packed arrays
5726 which end up encoded in special integer types. Note that scalars can
5727 be given implicit initial values using the Default_Value aspect.
5729 The exception we need to make is then for packed arrays of records
5730 with discriminants or implicit initial values. We have no light/easy
5731 way to check for the latter case, so we merely check for packed arrays
5732 of records. This may lead to useless copy-in operations, but in very
5733 rare cases only, as these would be exceptions in a set of already
5734 exceptional situations. */
5735 if (Ekind (gnat_param) == E_Out_Parameter
5736 && !by_ref
5737 && (by_return
5738 || (!POINTER_TYPE_P (gnu_param_type)
5739 && !AGGREGATE_TYPE_P (gnu_param_type)
5740 && !Has_Default_Aspect (Etype (gnat_param))))
5741 && !(Is_Array_Type (Etype (gnat_param))
5742 && Is_Packed (Etype (gnat_param))
5743 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5744 return gnu_param_type;
5746 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5747 ro_param || by_ref || by_component_ptr);
5748 DECL_BY_REF_P (gnu_param) = by_ref;
5749 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5750 DECL_POINTS_TO_READONLY_P (gnu_param)
5751 = (ro_param && (by_ref || by_component_ptr));
5752 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5754 /* If no Mechanism was specified, indicate what we're using, then
5755 back-annotate it. */
5756 if (mech == Default)
5757 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5759 Set_Mechanism (gnat_param, mech);
5760 return gnu_param;
5763 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5764 qualifiers on TYPE. */
5766 static tree
5767 change_qualified_type (tree type, int type_quals)
5769 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5772 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5774 static bool
5775 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5777 while (Present (Corresponding_Discriminant (discr1)))
5778 discr1 = Corresponding_Discriminant (discr1);
5780 while (Present (Corresponding_Discriminant (discr2)))
5781 discr2 = Corresponding_Discriminant (discr2);
5783 return
5784 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5787 /* Return true if the array type GNU_TYPE, which represents a dimension of
5788 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5790 static bool
5791 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5793 /* If the array type is not the innermost dimension of the GNAT type,
5794 then it has a non-aliased component. */
5795 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5796 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5797 return true;
5799 /* If the array type has an aliased component in the front-end sense,
5800 then it also has an aliased component in the back-end sense. */
5801 if (Has_Aliased_Components (gnat_type))
5802 return false;
5804 /* If this is a derived type, then it has a non-aliased component if
5805 and only if its parent type also has one. */
5806 if (Is_Derived_Type (gnat_type))
5808 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5809 int index;
5810 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5811 gnu_parent_type
5812 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5813 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5814 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5815 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5818 /* Otherwise, rely exclusively on properties of the element type. */
5819 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5822 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5824 static bool
5825 compile_time_known_address_p (Node_Id gnat_address)
5827 /* Catch System'To_Address. */
5828 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5829 gnat_address = Expression (gnat_address);
5831 return Compile_Time_Known_Value (gnat_address);
5834 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5835 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5837 static bool
5838 cannot_be_superflat_p (Node_Id gnat_range)
5840 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5841 Node_Id scalar_range;
5842 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5844 /* If the low bound is not constant, try to find an upper bound. */
5845 while (Nkind (gnat_lb) != N_Integer_Literal
5846 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5847 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5848 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5849 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5850 || Nkind (scalar_range) == N_Range))
5851 gnat_lb = High_Bound (scalar_range);
5853 /* If the high bound is not constant, try to find a lower bound. */
5854 while (Nkind (gnat_hb) != N_Integer_Literal
5855 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5856 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5857 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5858 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5859 || Nkind (scalar_range) == N_Range))
5860 gnat_hb = Low_Bound (scalar_range);
5862 /* If we have failed to find constant bounds, punt. */
5863 if (Nkind (gnat_lb) != N_Integer_Literal
5864 || Nkind (gnat_hb) != N_Integer_Literal)
5865 return false;
5867 /* We need at least a signed 64-bit type to catch most cases. */
5868 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5869 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5870 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5871 return false;
5873 /* If the low bound is the smallest integer, nothing can be smaller. */
5874 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5875 if (TREE_OVERFLOW (gnu_lb_minus_one))
5876 return true;
5878 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5881 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5883 static bool
5884 constructor_address_p (tree gnu_expr)
5886 while (TREE_CODE (gnu_expr) == NOP_EXPR
5887 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5888 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5889 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5891 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5892 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5895 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5896 be elaborated at the point of its definition, but do nothing else. */
5898 void
5899 elaborate_entity (Entity_Id gnat_entity)
5901 switch (Ekind (gnat_entity))
5903 case E_Signed_Integer_Subtype:
5904 case E_Modular_Integer_Subtype:
5905 case E_Enumeration_Subtype:
5906 case E_Ordinary_Fixed_Point_Subtype:
5907 case E_Decimal_Fixed_Point_Subtype:
5908 case E_Floating_Point_Subtype:
5910 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5911 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5913 /* ??? Tests to avoid Constraint_Error in static expressions
5914 are needed until after the front stops generating bogus
5915 conversions on bounds of real types. */
5916 if (!Raises_Constraint_Error (gnat_lb))
5917 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5918 true, false, Needs_Debug_Info (gnat_entity));
5919 if (!Raises_Constraint_Error (gnat_hb))
5920 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5921 true, false, Needs_Debug_Info (gnat_entity));
5922 break;
5925 case E_Record_Subtype:
5926 case E_Private_Subtype:
5927 case E_Limited_Private_Subtype:
5928 case E_Record_Subtype_With_Private:
5929 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5931 Node_Id gnat_discriminant_expr;
5932 Entity_Id gnat_field;
5934 for (gnat_field
5935 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5936 gnat_discriminant_expr
5937 = First_Elmt (Discriminant_Constraint (gnat_entity));
5938 Present (gnat_field);
5939 gnat_field = Next_Discriminant (gnat_field),
5940 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5941 /* Ignore access discriminants. */
5942 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5943 elaborate_expression (Node (gnat_discriminant_expr),
5944 gnat_entity, get_entity_name (gnat_field),
5945 true, false, false);
5947 break;
5952 /* Return true if the size in units represented by GNU_SIZE can be handled by
5953 an allocation. If STATIC_P is true, consider only what can be done with a
5954 static allocation. */
5956 static bool
5957 allocatable_size_p (tree gnu_size, bool static_p)
5959 /* We can allocate a fixed size if it is a valid for the middle-end. */
5960 if (TREE_CODE (gnu_size) == INTEGER_CST)
5961 return valid_constant_size_p (gnu_size);
5963 /* We can allocate a variable size if this isn't a static allocation. */
5964 else
5965 return !static_p;
5968 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5969 NAME, ARGS and ERROR_POINT. */
5971 static void
5972 prepend_one_attribute (struct attrib **attr_list,
5973 enum attr_type attr_type,
5974 tree attr_name,
5975 tree attr_args,
5976 Node_Id attr_error_point)
5978 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5980 attr->type = attr_type;
5981 attr->name = attr_name;
5982 attr->args = attr_args;
5983 attr->error_point = attr_error_point;
5985 attr->next = *attr_list;
5986 *attr_list = attr;
5989 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
5991 static void
5992 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
5994 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
5995 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5996 enum attr_type etype;
5998 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
5999 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6001 case Pragma_Machine_Attribute:
6002 etype = ATTR_MACHINE_ATTRIBUTE;
6003 break;
6005 case Pragma_Linker_Alias:
6006 etype = ATTR_LINK_ALIAS;
6007 break;
6009 case Pragma_Linker_Section:
6010 etype = ATTR_LINK_SECTION;
6011 break;
6013 case Pragma_Linker_Constructor:
6014 etype = ATTR_LINK_CONSTRUCTOR;
6015 break;
6017 case Pragma_Linker_Destructor:
6018 etype = ATTR_LINK_DESTRUCTOR;
6019 break;
6021 case Pragma_Weak_External:
6022 etype = ATTR_WEAK_EXTERNAL;
6023 break;
6025 case Pragma_Thread_Local_Storage:
6026 etype = ATTR_THREAD_LOCAL_STORAGE;
6027 break;
6029 default:
6030 return;
6033 /* See what arguments we have and turn them into GCC trees for attribute
6034 handlers. These expect identifier for strings. We handle at most two
6035 arguments and static expressions only. */
6036 if (Present (gnat_arg) && Present (First (gnat_arg)))
6038 Node_Id gnat_arg0 = Next (First (gnat_arg));
6039 Node_Id gnat_arg1 = Empty;
6041 if (Present (gnat_arg0)
6042 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6044 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6046 if (TREE_CODE (gnu_arg0) == STRING_CST)
6048 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6049 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6050 return;
6053 gnat_arg1 = Next (gnat_arg0);
6056 if (Present (gnat_arg1)
6057 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6059 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6061 if (TREE_CODE (gnu_arg1) == STRING_CST)
6062 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6066 /* Prepend to the list. Make a list of the argument we might have, as GCC
6067 expects it. */
6068 prepend_one_attribute (attr_list, etype, gnu_arg0,
6069 gnu_arg1
6070 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6071 Present (Next (First (gnat_arg)))
6072 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6075 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6077 static void
6078 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6080 Node_Id gnat_temp;
6082 /* Attributes are stored as Representation Item pragmas. */
6083 for (gnat_temp = First_Rep_Item (gnat_entity);
6084 Present (gnat_temp);
6085 gnat_temp = Next_Rep_Item (gnat_temp))
6086 if (Nkind (gnat_temp) == N_Pragma)
6087 prepend_one_attribute_pragma (attr_list, gnat_temp);
6090 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6091 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6092 return the GCC tree to use for that expression. GNU_NAME is the suffix
6093 to use if a variable needs to be created and DEFINITION is true if this
6094 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6095 otherwise, we are just elaborating the expression for side-effects. If
6096 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6097 isn't needed for code generation. */
6099 static tree
6100 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6101 bool definition, bool need_value, bool need_debug)
6103 tree gnu_expr;
6105 /* If we already elaborated this expression (e.g. it was involved
6106 in the definition of a private type), use the old value. */
6107 if (present_gnu_tree (gnat_expr))
6108 return get_gnu_tree (gnat_expr);
6110 /* If we don't need a value and this is static or a discriminant,
6111 we don't need to do anything. */
6112 if (!need_value
6113 && (Is_OK_Static_Expression (gnat_expr)
6114 || (Nkind (gnat_expr) == N_Identifier
6115 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6116 return NULL_TREE;
6118 /* If it's a static expression, we don't need a variable for debugging. */
6119 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6120 need_debug = false;
6122 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6123 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6124 gnu_name, definition, need_debug);
6126 /* Save the expression in case we try to elaborate this entity again. Since
6127 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6128 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6129 save_gnu_tree (gnat_expr, gnu_expr, true);
6131 return need_value ? gnu_expr : error_mark_node;
6134 /* Similar, but take a GNU expression and always return a result. */
6136 static tree
6137 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6138 bool definition, bool need_debug)
6140 const bool expr_public_p = Is_Public (gnat_entity);
6141 const bool expr_global_p = expr_public_p || global_bindings_p ();
6142 bool expr_variable_p, use_variable;
6144 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6145 reference will have been replaced with a COMPONENT_REF when the type
6146 is being elaborated. However, there are some cases involving child
6147 types where we will. So convert it to a COMPONENT_REF. We hope it
6148 will be at the highest level of the expression in these cases. */
6149 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6150 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6151 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6152 gnu_expr, NULL_TREE);
6154 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6155 that an expression cannot contain both a discriminant and a variable. */
6156 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6157 return gnu_expr;
6159 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6160 a variable that is initialized to contain the expression when the package
6161 containing the definition is elaborated. If this entity is defined at top
6162 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6163 if this is necessary. */
6164 if (CONSTANT_CLASS_P (gnu_expr))
6165 expr_variable_p = false;
6166 else
6168 /* Skip any conversions and simple constant arithmetics to see if the
6169 expression is based on a read-only variable.
6170 ??? This really should remain read-only, but we have to think about
6171 the typing of the tree here. */
6172 tree inner = remove_conversions (gnu_expr, true);
6174 inner = skip_simple_constant_arithmetic (inner);
6176 if (handled_component_p (inner))
6178 HOST_WIDE_INT bitsize, bitpos;
6179 tree offset;
6180 machine_mode mode;
6181 int unsignedp, volatilep;
6183 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6184 &mode, &unsignedp, &volatilep, false);
6185 /* If the offset is variable, err on the side of caution. */
6186 if (offset)
6187 inner = NULL_TREE;
6190 expr_variable_p
6191 = !(inner
6192 && TREE_CODE (inner) == VAR_DECL
6193 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6196 /* We only need to use the variable if we are in a global context since GCC
6197 can do the right thing in the local case. However, when not optimizing,
6198 use it for bounds of loop iteration scheme to avoid code duplication. */
6199 use_variable = expr_variable_p
6200 && (expr_global_p
6201 || (!optimize
6202 && definition
6203 && Is_Itype (gnat_entity)
6204 && Nkind (Associated_Node_For_Itype (gnat_entity))
6205 == N_Loop_Parameter_Specification));
6207 /* Now create it, possibly only for debugging purposes. */
6208 if (use_variable || need_debug)
6210 /* The following variable creation can happen when processing the body of
6211 subprograms that are defined out of the extended main unit and
6212 inlined. In this case, we are not at the global scope, and thus the
6213 new variable must not be tagged "external", as we used to do here as
6214 long as definition == 0. */
6215 const bool external_flag = !definition && expr_global_p;
6216 tree gnu_decl
6217 = create_var_decl_1
6218 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6219 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6220 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6222 DECL_ARTIFICIAL (gnu_decl) = 1;
6224 /* Using this variable at debug time (if need_debug is true) requires a
6225 proper location. The back-end will compute a location for this
6226 variable only if the variable is used by the generated code.
6227 Returning the variable ensures the caller will use it in generated
6228 code. Note that there is no need for a location if the debug info
6229 contains an integer constant.
6230 FIXME: when the encoding-based debug scheme is dropped, move this
6231 condition to the top-level IF block: we will not need to create a
6232 variable anymore in such cases, then. */
6233 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6234 return gnu_decl;
6237 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6240 /* Similar, but take an alignment factor and make it explicit in the tree. */
6242 static tree
6243 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6244 bool definition, bool need_debug, unsigned int align)
6246 tree unit_align = size_int (align / BITS_PER_UNIT);
6247 return
6248 size_binop (MULT_EXPR,
6249 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6250 gnu_expr,
6251 unit_align),
6252 gnat_entity, gnu_name, definition,
6253 need_debug),
6254 unit_align);
6257 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6258 the value passed against the list of choices. */
6260 tree
6261 choices_to_gnu (tree operand, Node_Id choices)
6263 Node_Id choice;
6264 Node_Id gnat_temp;
6265 tree result = boolean_false_node;
6266 tree this_test, low = 0, high = 0, single = 0;
6268 for (choice = First (choices); Present (choice); choice = Next (choice))
6270 switch (Nkind (choice))
6272 case N_Range:
6273 low = gnat_to_gnu (Low_Bound (choice));
6274 high = gnat_to_gnu (High_Bound (choice));
6276 this_test
6277 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6278 build_binary_op (GE_EXPR, boolean_type_node,
6279 operand, low),
6280 build_binary_op (LE_EXPR, boolean_type_node,
6281 operand, high));
6283 break;
6285 case N_Subtype_Indication:
6286 gnat_temp = Range_Expression (Constraint (choice));
6287 low = gnat_to_gnu (Low_Bound (gnat_temp));
6288 high = gnat_to_gnu (High_Bound (gnat_temp));
6290 this_test
6291 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6292 build_binary_op (GE_EXPR, boolean_type_node,
6293 operand, low),
6294 build_binary_op (LE_EXPR, boolean_type_node,
6295 operand, high));
6296 break;
6298 case N_Identifier:
6299 case N_Expanded_Name:
6300 /* This represents either a subtype range, an enumeration
6301 literal, or a constant Ekind says which. If an enumeration
6302 literal or constant, fall through to the next case. */
6303 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6304 && Ekind (Entity (choice)) != E_Constant)
6306 tree type = gnat_to_gnu_type (Entity (choice));
6308 low = TYPE_MIN_VALUE (type);
6309 high = TYPE_MAX_VALUE (type);
6311 this_test
6312 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6313 build_binary_op (GE_EXPR, boolean_type_node,
6314 operand, low),
6315 build_binary_op (LE_EXPR, boolean_type_node,
6316 operand, high));
6317 break;
6320 /* ... fall through ... */
6322 case N_Character_Literal:
6323 case N_Integer_Literal:
6324 single = gnat_to_gnu (choice);
6325 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6326 single);
6327 break;
6329 case N_Others_Choice:
6330 this_test = boolean_true_node;
6331 break;
6333 default:
6334 gcc_unreachable ();
6337 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6338 this_test);
6341 return result;
6344 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6345 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6347 static int
6348 adjust_packed (tree field_type, tree record_type, int packed)
6350 /* If the field contains an item of variable size, we cannot pack it
6351 because we cannot create temporaries of non-fixed size in case
6352 we need to take the address of the field. See addressable_p and
6353 the notes on the addressability issues for further details. */
6354 if (type_has_variable_size (field_type))
6355 return 0;
6357 /* If the alignment of the record is specified and the field type
6358 is over-aligned, request Storage_Unit alignment for the field. */
6359 if (packed == -2)
6361 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6362 return -1;
6363 else
6364 return 0;
6367 return packed;
6370 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6371 placed in GNU_RECORD_TYPE.
6373 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6374 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6375 record has a specified alignment.
6377 DEFINITION is true if this field is for a record being defined.
6379 DEBUG_INFO_P is true if we need to write debug information for types
6380 that we may create in the process. */
6382 static tree
6383 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6384 bool definition, bool debug_info_p)
6386 const Entity_Id gnat_field_type = Etype (gnat_field);
6387 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6388 tree gnu_field_id = get_entity_name (gnat_field);
6389 tree gnu_field, gnu_size, gnu_pos;
6390 bool is_volatile
6391 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6392 bool needs_strict_alignment
6393 = (is_volatile
6394 || Is_Aliased (gnat_field)
6395 || Strict_Alignment (gnat_field_type));
6397 /* If this field requires strict alignment, we cannot pack it because
6398 it would very likely be under-aligned in the record. */
6399 if (needs_strict_alignment)
6400 packed = 0;
6401 else
6402 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6404 /* If a size is specified, use it. Otherwise, if the record type is packed,
6405 use the official RM size. See "Handling of Type'Size Values" in Einfo
6406 for further details. */
6407 if (Known_Esize (gnat_field))
6408 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6409 gnat_field, FIELD_DECL, false, true);
6410 else if (packed == 1)
6411 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6412 gnat_field, FIELD_DECL, false, true);
6413 else
6414 gnu_size = NULL_TREE;
6416 /* If we have a specified size that is smaller than that of the field's type,
6417 or a position is specified, and the field's type is a record that doesn't
6418 require strict alignment, see if we can get either an integral mode form
6419 of the type or a smaller form. If we can, show a size was specified for
6420 the field if there wasn't one already, so we know to make this a bitfield
6421 and avoid making things wider.
6423 Changing to an integral mode form is useful when the record is packed as
6424 we can then place the field at a non-byte-aligned position and so achieve
6425 tighter packing. This is in addition required if the field shares a byte
6426 with another field and the front-end lets the back-end handle the access
6427 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6429 Changing to a smaller form is required if the specified size is smaller
6430 than that of the field's type and the type contains sub-fields that are
6431 padded, in order to avoid generating accesses to these sub-fields that
6432 are wider than the field.
6434 We avoid the transformation if it is not required or potentially useful,
6435 as it might entail an increase of the field's alignment and have ripple
6436 effects on the outer record type. A typical case is a field known to be
6437 byte-aligned and not to share a byte with another field. */
6438 if (!needs_strict_alignment
6439 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6440 && !TYPE_FAT_POINTER_P (gnu_field_type)
6441 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6442 && (packed == 1
6443 || (gnu_size
6444 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6445 || (Present (Component_Clause (gnat_field))
6446 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6447 % BITS_PER_UNIT == 0
6448 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6450 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6451 if (gnu_packable_type != gnu_field_type)
6453 gnu_field_type = gnu_packable_type;
6454 if (!gnu_size)
6455 gnu_size = rm_size (gnu_field_type);
6459 if (Is_Atomic (gnat_field))
6460 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6462 if (Present (Component_Clause (gnat_field)))
6464 Entity_Id gnat_parent
6465 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6467 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6468 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6469 gnat_field, FIELD_DECL, false, true);
6471 /* Ensure the position does not overlap with the parent subtype, if there
6472 is one. This test is omitted if the parent of the tagged type has a
6473 full rep clause since, in this case, component clauses are allowed to
6474 overlay the space allocated for the parent type and the front-end has
6475 checked that there are no overlapping components. */
6476 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6478 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6480 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6481 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6483 post_error_ne_tree
6484 ("offset of& must be beyond parent{, minimum allowed is ^}",
6485 First_Bit (Component_Clause (gnat_field)), gnat_field,
6486 TYPE_SIZE_UNIT (gnu_parent));
6490 /* If this field needs strict alignment, check that the record is
6491 sufficiently aligned and that position and size are consistent with
6492 the alignment. But don't do it if we are just annotating types and
6493 the field's type is tagged, since tagged types aren't fully laid out
6494 in this mode. Also, note that atomic implies volatile so the inner
6495 test sequences ordering is significant here. */
6496 if (needs_strict_alignment
6497 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6499 TYPE_ALIGN (gnu_record_type)
6500 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6502 if (gnu_size
6503 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6505 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6506 post_error_ne_tree
6507 ("atomic field& must be natural size of type{ (^)}",
6508 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6509 TYPE_SIZE (gnu_field_type));
6511 else if (is_volatile)
6512 post_error_ne_tree
6513 ("volatile field& must be natural size of type{ (^)}",
6514 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6515 TYPE_SIZE (gnu_field_type));
6517 else if (Is_Aliased (gnat_field))
6518 post_error_ne_tree
6519 ("size of aliased field& must be ^ bits",
6520 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6521 TYPE_SIZE (gnu_field_type));
6523 else if (Strict_Alignment (gnat_field_type))
6524 post_error_ne_tree
6525 ("size of & with aliased or tagged components not ^ bits",
6526 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6527 TYPE_SIZE (gnu_field_type));
6529 else
6530 gcc_unreachable ();
6532 gnu_size = NULL_TREE;
6535 if (!integer_zerop (size_binop
6536 (TRUNC_MOD_EXPR, gnu_pos,
6537 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6539 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6540 post_error_ne_num
6541 ("position of atomic field& must be multiple of ^ bits",
6542 First_Bit (Component_Clause (gnat_field)), gnat_field,
6543 TYPE_ALIGN (gnu_field_type));
6545 else if (is_volatile)
6546 post_error_ne_num
6547 ("position of volatile field& must be multiple of ^ bits",
6548 First_Bit (Component_Clause (gnat_field)), gnat_field,
6549 TYPE_ALIGN (gnu_field_type));
6551 else if (Is_Aliased (gnat_field))
6552 post_error_ne_num
6553 ("position of aliased field& must be multiple of ^ bits",
6554 First_Bit (Component_Clause (gnat_field)), gnat_field,
6555 TYPE_ALIGN (gnu_field_type));
6557 else if (Strict_Alignment (gnat_field_type))
6558 post_error_ne
6559 ("position of & is not compatible with alignment required "
6560 "by its components",
6561 First_Bit (Component_Clause (gnat_field)), gnat_field);
6563 else
6564 gcc_unreachable ();
6566 gnu_pos = NULL_TREE;
6571 /* If the record has rep clauses and this is the tag field, make a rep
6572 clause for it as well. */
6573 else if (Has_Specified_Layout (Scope (gnat_field))
6574 && Chars (gnat_field) == Name_uTag)
6576 gnu_pos = bitsize_zero_node;
6577 gnu_size = TYPE_SIZE (gnu_field_type);
6580 else
6582 gnu_pos = NULL_TREE;
6584 /* If we are packing the record and the field is BLKmode, round the
6585 size up to a byte boundary. */
6586 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6587 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6590 /* We need to make the size the maximum for the type if it is
6591 self-referential and an unconstrained type. In that case, we can't
6592 pack the field since we can't make a copy to align it. */
6593 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6594 && !gnu_size
6595 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6596 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6598 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6599 packed = 0;
6602 /* If a size is specified, adjust the field's type to it. */
6603 if (gnu_size)
6605 tree orig_field_type;
6607 /* If the field's type is justified modular, we would need to remove
6608 the wrapper to (better) meet the layout requirements. However we
6609 can do so only if the field is not aliased to preserve the unique
6610 layout and if the prescribed size is not greater than that of the
6611 packed array to preserve the justification. */
6612 if (!needs_strict_alignment
6613 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6614 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6615 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6616 <= 0)
6617 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6619 /* Similarly if the field's type is a misaligned integral type, but
6620 there is no restriction on the size as there is no justification. */
6621 if (!needs_strict_alignment
6622 && TYPE_IS_PADDING_P (gnu_field_type)
6623 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6624 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6626 gnu_field_type
6627 = make_type_from_size (gnu_field_type, gnu_size,
6628 Has_Biased_Representation (gnat_field));
6630 orig_field_type = gnu_field_type;
6631 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6632 false, false, definition, true);
6634 /* If a padding record was made, declare it now since it will never be
6635 declared otherwise. This is necessary to ensure that its subtrees
6636 are properly marked. */
6637 if (gnu_field_type != orig_field_type
6638 && !DECL_P (TYPE_NAME (gnu_field_type)))
6639 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6640 debug_info_p, gnat_field);
6643 /* Otherwise (or if there was an error), don't specify a position. */
6644 else
6645 gnu_pos = NULL_TREE;
6647 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6648 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6650 /* Now create the decl for the field. */
6651 gnu_field
6652 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6653 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6654 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6655 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6656 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6658 if (Ekind (gnat_field) == E_Discriminant)
6659 DECL_DISCRIMINANT_NUMBER (gnu_field)
6660 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6662 return gnu_field;
6665 /* Return true if at least one member of COMPONENT_LIST needs strict
6666 alignment. */
6668 static bool
6669 components_need_strict_alignment (Node_Id component_list)
6671 Node_Id component_decl;
6673 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6674 Present (component_decl);
6675 component_decl = Next_Non_Pragma (component_decl))
6677 Entity_Id gnat_field = Defining_Entity (component_decl);
6679 if (Is_Aliased (gnat_field))
6680 return true;
6682 if (Strict_Alignment (Etype (gnat_field)))
6683 return true;
6686 return false;
6689 /* Return true if TYPE is a type with variable size or a padding type with a
6690 field of variable size or a record that has a field with such a type. */
6692 static bool
6693 type_has_variable_size (tree type)
6695 tree field;
6697 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6698 return true;
6700 if (TYPE_IS_PADDING_P (type)
6701 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6702 return true;
6704 if (!RECORD_OR_UNION_TYPE_P (type))
6705 return false;
6707 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6708 if (type_has_variable_size (TREE_TYPE (field)))
6709 return true;
6711 return false;
6714 /* Return true if FIELD is an artificial field. */
6716 static bool
6717 field_is_artificial (tree field)
6719 /* These fields are generated by the front-end proper. */
6720 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6721 return true;
6723 /* These fields are generated by gigi. */
6724 if (DECL_INTERNAL_P (field))
6725 return true;
6727 return false;
6730 /* Return true if FIELD is a non-artificial aliased field. */
6732 static bool
6733 field_is_aliased (tree field)
6735 if (field_is_artificial (field))
6736 return false;
6738 return DECL_ALIASED_P (field);
6741 /* Return true if FIELD is a non-artificial field with self-referential
6742 size. */
6744 static bool
6745 field_has_self_size (tree field)
6747 if (field_is_artificial (field))
6748 return false;
6750 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6751 return false;
6753 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6756 /* Return true if FIELD is a non-artificial field with variable size. */
6758 static bool
6759 field_has_variable_size (tree field)
6761 if (field_is_artificial (field))
6762 return false;
6764 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6765 return false;
6767 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6770 /* qsort comparer for the bit positions of two record components. */
6772 static int
6773 compare_field_bitpos (const PTR rt1, const PTR rt2)
6775 const_tree const field1 = * (const_tree const *) rt1;
6776 const_tree const field2 = * (const_tree const *) rt2;
6777 const int ret
6778 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6780 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6783 /* Structure holding information for a given variant. */
6784 typedef struct vinfo
6786 /* The record type of the variant. */
6787 tree type;
6789 /* The name of the variant. */
6790 tree name;
6792 /* The qualifier of the variant. */
6793 tree qual;
6795 /* Whether the variant has a rep clause. */
6796 bool has_rep;
6798 /* Whether the variant is packed. */
6799 bool packed;
6801 } vinfo_t;
6803 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6804 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6805 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6806 When called from gnat_to_gnu_entity during the processing of a record type
6807 definition, the GCC node for the parent, if any, will be the single field
6808 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6809 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6810 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6812 PACKED is 1 if this is for a packed record, -1 if this is for a record
6813 with Component_Alignment of Storage_Unit, -2 if this is for a record
6814 with a specified alignment.
6816 DEFINITION is true if we are defining this record type.
6818 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6819 out the record. This means the alignment only serves to force fields to
6820 be bitfields, but not to require the record to be that aligned. This is
6821 used for variants.
6823 ALL_REP is true if a rep clause is present for all the fields.
6825 UNCHECKED_UNION is true if we are building this type for a record with a
6826 Pragma Unchecked_Union.
6828 ARTIFICIAL is true if this is a type that was generated by the compiler.
6830 DEBUG_INFO is true if we need to write debug information about the type.
6832 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6833 mean that its contents may be unused as well, only the container itself.
6835 REORDER is true if we are permitted to reorder components of this type.
6837 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6838 the outer record type down to this variant level. It is nonzero only if
6839 all the fields down to this level have a rep clause and ALL_REP is false.
6841 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6842 with a rep clause is to be added; in this case, that is all that should
6843 be done with such fields and the return value will be false. */
6845 static bool
6846 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6847 tree gnu_field_list, int packed, bool definition,
6848 bool cancel_alignment, bool all_rep,
6849 bool unchecked_union, bool artificial,
6850 bool debug_info, bool maybe_unused, bool reorder,
6851 tree first_free_pos, tree *p_gnu_rep_list)
6853 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6854 bool variants_have_rep = all_rep;
6855 bool layout_with_rep = false;
6856 bool has_self_field = false;
6857 bool has_aliased_after_self_field = false;
6858 Node_Id component_decl, variant_part;
6859 tree gnu_field, gnu_next, gnu_last;
6860 tree gnu_variant_part = NULL_TREE;
6861 tree gnu_rep_list = NULL_TREE;
6862 tree gnu_var_list = NULL_TREE;
6863 tree gnu_self_list = NULL_TREE;
6864 tree gnu_zero_list = NULL_TREE;
6866 /* For each component referenced in a component declaration create a GCC
6867 field and add it to the list, skipping pragmas in the GNAT list. */
6868 gnu_last = tree_last (gnu_field_list);
6869 if (Present (Component_Items (gnat_component_list)))
6870 for (component_decl
6871 = First_Non_Pragma (Component_Items (gnat_component_list));
6872 Present (component_decl);
6873 component_decl = Next_Non_Pragma (component_decl))
6875 Entity_Id gnat_field = Defining_Entity (component_decl);
6876 Name_Id gnat_name = Chars (gnat_field);
6878 /* If present, the _Parent field must have been created as the single
6879 field of the record type. Put it before any other fields. */
6880 if (gnat_name == Name_uParent)
6882 gnu_field = TYPE_FIELDS (gnu_record_type);
6883 gnu_field_list = chainon (gnu_field_list, gnu_field);
6885 else
6887 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6888 definition, debug_info);
6890 /* If this is the _Tag field, put it before any other fields. */
6891 if (gnat_name == Name_uTag)
6892 gnu_field_list = chainon (gnu_field_list, gnu_field);
6894 /* If this is the _Controller field, put it before the other
6895 fields except for the _Tag or _Parent field. */
6896 else if (gnat_name == Name_uController && gnu_last)
6898 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6899 DECL_CHAIN (gnu_last) = gnu_field;
6902 /* If this is a regular field, put it after the other fields. */
6903 else
6905 DECL_CHAIN (gnu_field) = gnu_field_list;
6906 gnu_field_list = gnu_field;
6907 if (!gnu_last)
6908 gnu_last = gnu_field;
6910 /* And record information for the final layout. */
6911 if (field_has_self_size (gnu_field))
6912 has_self_field = true;
6913 else if (has_self_field && field_is_aliased (gnu_field))
6914 has_aliased_after_self_field = true;
6918 save_gnu_tree (gnat_field, gnu_field, false);
6921 /* At the end of the component list there may be a variant part. */
6922 variant_part = Variant_Part (gnat_component_list);
6924 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6925 mutually exclusive and should go in the same memory. To do this we need
6926 to treat each variant as a record whose elements are created from the
6927 component list for the variant. So here we create the records from the
6928 lists for the variants and put them all into the QUAL_UNION_TYPE.
6929 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6930 use GNU_RECORD_TYPE if there are no fields so far. */
6931 if (Present (variant_part))
6933 Node_Id gnat_discr = Name (variant_part), variant;
6934 tree gnu_discr = gnat_to_gnu (gnat_discr);
6935 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
6936 tree gnu_var_name
6937 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6938 "XVN");
6939 tree gnu_union_type, gnu_union_name;
6940 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6941 bool union_field_needs_strict_alignment = false;
6942 auto_vec <vinfo_t, 16> variant_types;
6943 vinfo_t *gnu_variant;
6944 unsigned int variants_align = 0;
6945 unsigned int i;
6947 gnu_union_name
6948 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6950 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6951 are all in the variant part, to match the layout of C unions. There
6952 is an associated check below. */
6953 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6954 gnu_union_type = gnu_record_type;
6955 else
6957 gnu_union_type
6958 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6960 TYPE_NAME (gnu_union_type) = gnu_union_name;
6961 TYPE_ALIGN (gnu_union_type) = 0;
6962 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6965 /* If all the fields down to this level have a rep clause, find out
6966 whether all the fields at this level also have one. If so, then
6967 compute the new first free position to be passed downward. */
6968 this_first_free_pos = first_free_pos;
6969 if (this_first_free_pos)
6971 for (gnu_field = gnu_field_list;
6972 gnu_field;
6973 gnu_field = DECL_CHAIN (gnu_field))
6974 if (DECL_FIELD_OFFSET (gnu_field))
6976 tree pos = bit_position (gnu_field);
6977 if (!tree_int_cst_lt (pos, this_first_free_pos))
6978 this_first_free_pos
6979 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
6981 else
6983 this_first_free_pos = NULL_TREE;
6984 break;
6988 /* We build the variants in two passes. The bulk of the work is done in
6989 the first pass, that is to say translating the GNAT nodes, building
6990 the container types and computing the associated properties. However
6991 we cannot finish up the container types during this pass because we
6992 don't know where the variant part will be placed until the end. */
6993 for (variant = First_Non_Pragma (Variants (variant_part));
6994 Present (variant);
6995 variant = Next_Non_Pragma (variant))
6997 tree gnu_variant_type = make_node (RECORD_TYPE);
6998 tree gnu_inner_name, gnu_qual;
6999 bool has_rep;
7000 int field_packed;
7001 vinfo_t vinfo;
7003 Get_Variant_Encoding (variant);
7004 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7005 TYPE_NAME (gnu_variant_type)
7006 = concat_name (gnu_union_name,
7007 IDENTIFIER_POINTER (gnu_inner_name));
7009 /* Set the alignment of the inner type in case we need to make
7010 inner objects into bitfields, but then clear it out so the
7011 record actually gets only the alignment required. */
7012 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7013 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7015 /* Similarly, if the outer record has a size specified and all
7016 the fields have a rep clause, we can propagate the size. */
7017 if (all_rep_and_size)
7019 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7020 TYPE_SIZE_UNIT (gnu_variant_type)
7021 = TYPE_SIZE_UNIT (gnu_record_type);
7024 /* Add the fields into the record type for the variant. Note that
7025 we aren't sure to really use it at this point, see below. */
7026 has_rep
7027 = components_to_record (gnu_variant_type, Component_List (variant),
7028 NULL_TREE, packed, definition,
7029 !all_rep_and_size, all_rep,
7030 unchecked_union,
7031 true, debug_info, true, reorder,
7032 this_first_free_pos,
7033 all_rep || this_first_free_pos
7034 ? NULL : &gnu_rep_list);
7036 /* Translate the qualifier and annotate the GNAT node. */
7037 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7038 Set_Present_Expr (variant, annotate_value (gnu_qual));
7040 /* Deal with packedness like in gnat_to_gnu_field. */
7041 if (components_need_strict_alignment (Component_List (variant)))
7043 field_packed = 0;
7044 union_field_needs_strict_alignment = true;
7046 else
7047 field_packed
7048 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7050 /* Push this variant onto the stack for the second pass. */
7051 vinfo.type = gnu_variant_type;
7052 vinfo.name = gnu_inner_name;
7053 vinfo.qual = gnu_qual;
7054 vinfo.has_rep = has_rep;
7055 vinfo.packed = field_packed;
7056 variant_types.safe_push (vinfo);
7058 /* Compute the global properties that will determine the placement of
7059 the variant part. */
7060 variants_have_rep |= has_rep;
7061 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7062 variants_align = TYPE_ALIGN (gnu_variant_type);
7065 /* Round up the first free position to the alignment of the variant part
7066 for the variants without rep clause. This will guarantee a consistent
7067 layout independently of the placement of the variant part. */
7068 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7069 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7071 /* In the second pass, the container types are adjusted if necessary and
7072 finished up, then the corresponding fields of the variant part are
7073 built with their qualifier, unless this is an unchecked union. */
7074 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7076 tree gnu_variant_type = gnu_variant->type;
7077 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7079 /* If this is an Unchecked_Union whose fields are all in the variant
7080 part and we have a single field with no representation clause or
7081 placed at offset zero, use the field directly to match the layout
7082 of C unions. */
7083 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7084 && gnu_field_list
7085 && !DECL_CHAIN (gnu_field_list)
7086 && (!DECL_FIELD_OFFSET (gnu_field_list)
7087 || integer_zerop (bit_position (gnu_field_list))))
7089 gnu_field = gnu_field_list;
7090 DECL_CONTEXT (gnu_field) = gnu_record_type;
7092 else
7094 /* Finalize the variant type now. We used to throw away empty
7095 record types but we no longer do that because we need them to
7096 generate complete debug info for the variant; otherwise, the
7097 union type definition will be lacking the fields associated
7098 with these empty variants. */
7099 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7101 /* The variant part will be at offset 0 so we need to ensure
7102 that the fields are laid out starting from the first free
7103 position at this level. */
7104 tree gnu_rep_type = make_node (RECORD_TYPE);
7105 tree gnu_rep_part;
7106 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7107 gnu_rep_part
7108 = create_rep_part (gnu_rep_type, gnu_variant_type,
7109 this_first_free_pos);
7110 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7111 gnu_field_list = gnu_rep_part;
7112 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7113 false);
7116 if (debug_info)
7117 rest_of_record_type_compilation (gnu_variant_type);
7118 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7119 true, debug_info, gnat_component_list);
7121 gnu_field
7122 = create_field_decl (gnu_variant->name, gnu_variant_type,
7123 gnu_union_type,
7124 all_rep_and_size
7125 ? TYPE_SIZE (gnu_variant_type) : 0,
7126 variants_have_rep ? bitsize_zero_node : 0,
7127 gnu_variant->packed, 0);
7129 DECL_INTERNAL_P (gnu_field) = 1;
7131 if (!unchecked_union)
7132 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7135 DECL_CHAIN (gnu_field) = gnu_variant_list;
7136 gnu_variant_list = gnu_field;
7139 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7140 if (gnu_variant_list)
7142 int union_field_packed;
7144 if (all_rep_and_size)
7146 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7147 TYPE_SIZE_UNIT (gnu_union_type)
7148 = TYPE_SIZE_UNIT (gnu_record_type);
7151 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7152 all_rep_and_size ? 1 : 0, debug_info);
7154 /* If GNU_UNION_TYPE is our record type, it means we must have an
7155 Unchecked_Union with no fields. Verify that and, if so, just
7156 return. */
7157 if (gnu_union_type == gnu_record_type)
7159 gcc_assert (unchecked_union
7160 && !gnu_field_list
7161 && !gnu_rep_list);
7162 return variants_have_rep;
7165 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7166 debug_info, gnat_component_list);
7168 /* Deal with packedness like in gnat_to_gnu_field. */
7169 if (union_field_needs_strict_alignment)
7170 union_field_packed = 0;
7171 else
7172 union_field_packed
7173 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7175 gnu_variant_part
7176 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7177 all_rep_and_size
7178 ? TYPE_SIZE (gnu_union_type) : 0,
7179 variants_have_rep ? bitsize_zero_node : 0,
7180 union_field_packed, 0);
7182 DECL_INTERNAL_P (gnu_variant_part) = 1;
7186 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7187 permitted to reorder components, self-referential sizes or variable sizes.
7188 If they do, pull them out and put them onto the appropriate list. We have
7189 to do this in a separate pass since we want to handle the discriminants
7190 but can't play with them until we've used them in debugging data above.
7192 Similarly, pull out the fields with zero size and no rep clause, as they
7193 would otherwise modify the layout and thus very likely run afoul of the
7194 Ada semantics, which are different from those of C here.
7196 ??? If we reorder them, debugging information will be wrong but there is
7197 nothing that can be done about this at the moment. */
7198 gnu_last = NULL_TREE;
7200 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7201 do { \
7202 if (gnu_last) \
7203 DECL_CHAIN (gnu_last) = gnu_next; \
7204 else \
7205 gnu_field_list = gnu_next; \
7207 DECL_CHAIN (gnu_field) = (LIST); \
7208 (LIST) = gnu_field; \
7209 } while (0)
7211 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7213 gnu_next = DECL_CHAIN (gnu_field);
7215 if (DECL_FIELD_OFFSET (gnu_field))
7217 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7218 continue;
7221 if ((reorder || has_aliased_after_self_field)
7222 && field_has_self_size (gnu_field))
7224 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7225 continue;
7228 if (reorder && field_has_variable_size (gnu_field))
7230 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7231 continue;
7234 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7236 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7237 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7238 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7239 if (field_is_aliased (gnu_field))
7240 TYPE_ALIGN (gnu_record_type)
7241 = MAX (TYPE_ALIGN (gnu_record_type),
7242 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7243 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7244 continue;
7247 gnu_last = gnu_field;
7250 #undef MOVE_FROM_FIELD_LIST_TO
7252 gnu_field_list = nreverse (gnu_field_list);
7254 /* If permitted, we reorder the fields as follows:
7256 1) all fixed length fields,
7257 2) all fields whose length doesn't depend on discriminants,
7258 3) all fields whose length depends on discriminants,
7259 4) the variant part,
7261 within the record and within each variant recursively. */
7262 if (reorder)
7263 gnu_field_list
7264 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7266 /* Otherwise, if there is an aliased field placed after a field whose length
7267 depends on discriminants, we put all the fields of the latter sort, last.
7268 We need to do this in case an object of this record type is mutable. */
7269 else if (has_aliased_after_self_field)
7270 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7272 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7273 in our REP list to the previous level because this level needs them in
7274 order to do a correct layout, i.e. avoid having overlapping fields. */
7275 if (p_gnu_rep_list && gnu_rep_list)
7276 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7278 /* Otherwise, sort the fields by bit position and put them into their own
7279 record, before the others, if we also have fields without rep clause. */
7280 else if (gnu_rep_list)
7282 tree gnu_rep_type, gnu_rep_part;
7283 int i, len = list_length (gnu_rep_list);
7284 tree *gnu_arr = XALLOCAVEC (tree, len);
7286 /* If all the fields have a rep clause, we can do a flat layout. */
7287 layout_with_rep = !gnu_field_list
7288 && (!gnu_variant_part || variants_have_rep);
7289 gnu_rep_type
7290 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7292 for (gnu_field = gnu_rep_list, i = 0;
7293 gnu_field;
7294 gnu_field = DECL_CHAIN (gnu_field), i++)
7295 gnu_arr[i] = gnu_field;
7297 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7299 /* Put the fields in the list in order of increasing position, which
7300 means we start from the end. */
7301 gnu_rep_list = NULL_TREE;
7302 for (i = len - 1; i >= 0; i--)
7304 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7305 gnu_rep_list = gnu_arr[i];
7306 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7309 if (layout_with_rep)
7310 gnu_field_list = gnu_rep_list;
7311 else
7313 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7315 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7316 without rep clause are laid out starting from this position.
7317 Therefore, we force it as a minimal size on the REP part. */
7318 gnu_rep_part
7319 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7321 /* Chain the REP part at the beginning of the field list. */
7322 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7323 gnu_field_list = gnu_rep_part;
7327 /* Chain the variant part at the end of the field list. */
7328 if (gnu_variant_part)
7329 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7331 if (cancel_alignment)
7332 TYPE_ALIGN (gnu_record_type) = 0;
7334 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7336 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7337 debug_info && !maybe_unused);
7339 /* Chain the fields with zero size at the beginning of the field list. */
7340 if (gnu_zero_list)
7341 TYPE_FIELDS (gnu_record_type)
7342 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7344 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7347 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7348 placed into an Esize, Component_Bit_Offset, or Component_Size value
7349 in the GNAT tree. */
7351 static Uint
7352 annotate_value (tree gnu_size)
7354 TCode tcode;
7355 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7356 struct tree_int_map in;
7357 int i;
7359 /* See if we've already saved the value for this node. */
7360 if (EXPR_P (gnu_size))
7362 struct tree_int_map *e;
7364 in.base.from = gnu_size;
7365 e = (struct tree_int_map *) htab_find (annotate_value_cache, &in);
7367 if (e)
7368 return (Node_Ref_Or_Val) e->to;
7370 else
7371 in.base.from = NULL_TREE;
7373 /* If we do not return inside this switch, TCODE will be set to the
7374 code to use for a Create_Node operand and LEN (set above) will be
7375 the number of recursive calls for us to make. */
7377 switch (TREE_CODE (gnu_size))
7379 case INTEGER_CST:
7380 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7382 case COMPONENT_REF:
7383 /* The only case we handle here is a simple discriminant reference. */
7384 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7386 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7388 /* Climb up the chain of successive extensions, if any. */
7389 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7390 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7391 == parent_name_id)
7392 gnu_size = TREE_OPERAND (gnu_size, 0);
7394 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7395 return
7396 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7399 return No_Uint;
7401 CASE_CONVERT: case NON_LVALUE_EXPR:
7402 return annotate_value (TREE_OPERAND (gnu_size, 0));
7404 /* Now just list the operations we handle. */
7405 case COND_EXPR: tcode = Cond_Expr; break;
7406 case PLUS_EXPR: tcode = Plus_Expr; break;
7407 case MINUS_EXPR: tcode = Minus_Expr; break;
7408 case MULT_EXPR: tcode = Mult_Expr; break;
7409 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7410 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7411 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7412 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7413 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7414 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7415 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7416 case NEGATE_EXPR: tcode = Negate_Expr; break;
7417 case MIN_EXPR: tcode = Min_Expr; break;
7418 case MAX_EXPR: tcode = Max_Expr; break;
7419 case ABS_EXPR: tcode = Abs_Expr; break;
7420 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7421 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7422 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7423 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7424 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7425 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7426 case LT_EXPR: tcode = Lt_Expr; break;
7427 case LE_EXPR: tcode = Le_Expr; break;
7428 case GT_EXPR: tcode = Gt_Expr; break;
7429 case GE_EXPR: tcode = Ge_Expr; break;
7430 case EQ_EXPR: tcode = Eq_Expr; break;
7431 case NE_EXPR: tcode = Ne_Expr; break;
7433 case BIT_AND_EXPR:
7434 tcode = Bit_And_Expr;
7435 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7436 Such values appear in expressions with aligning patterns. Note that,
7437 since sizetype is unsigned, we have to jump through some hoops. */
7438 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7440 tree op1 = TREE_OPERAND (gnu_size, 1);
7441 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7442 if (wi::neg_p (signed_op1))
7444 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7445 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7448 break;
7450 case CALL_EXPR:
7451 /* In regular mode, inline back only if symbolic annotation is requested
7452 in order to avoid memory explosion on big discriminated record types.
7453 But not in ASIS mode, as symbolic annotation is required for DDA. */
7454 if (List_Representation_Info == 3 || type_annotate_only)
7456 tree t = maybe_inline_call_in_expr (gnu_size);
7457 if (t)
7458 return annotate_value (t);
7460 else
7461 return Uint_Minus_1;
7463 /* Fall through... */
7465 default:
7466 return No_Uint;
7469 /* Now get each of the operands that's relevant for this code. If any
7470 cannot be expressed as a repinfo node, say we can't. */
7471 for (i = 0; i < 3; i++)
7472 ops[i] = No_Uint;
7474 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7476 if (i == 1 && pre_op1 != No_Uint)
7477 ops[i] = pre_op1;
7478 else
7479 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7480 if (ops[i] == No_Uint)
7481 return No_Uint;
7484 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7486 /* Save the result in the cache. */
7487 if (in.base.from)
7489 struct tree_int_map **h;
7490 /* We can't assume the hash table data hasn't moved since the initial
7491 look up, so we have to search again. Allocating and inserting an
7492 entry at that point would be an alternative, but then we'd better
7493 discard the entry if we decided not to cache it. */
7494 h = (struct tree_int_map **)
7495 htab_find_slot (annotate_value_cache, &in, INSERT);
7496 gcc_assert (!*h);
7497 *h = ggc_alloc<tree_int_map> ();
7498 (*h)->base.from = gnu_size;
7499 (*h)->to = ret;
7502 return ret;
7505 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7506 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7507 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7508 BY_REF is true if the object is used by reference. */
7510 void
7511 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7513 if (by_ref)
7515 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7516 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7517 else
7518 gnu_type = TREE_TYPE (gnu_type);
7521 if (Unknown_Esize (gnat_entity))
7523 if (TREE_CODE (gnu_type) == RECORD_TYPE
7524 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7525 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7526 else if (!size)
7527 size = TYPE_SIZE (gnu_type);
7529 if (size)
7530 Set_Esize (gnat_entity, annotate_value (size));
7533 if (Unknown_Alignment (gnat_entity))
7534 Set_Alignment (gnat_entity,
7535 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7538 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7539 Return NULL_TREE if there is no such element in the list. */
7541 static tree
7542 purpose_member_field (const_tree elem, tree list)
7544 while (list)
7546 tree field = TREE_PURPOSE (list);
7547 if (SAME_FIELD_P (field, elem))
7548 return list;
7549 list = TREE_CHAIN (list);
7551 return NULL_TREE;
7554 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7555 set Component_Bit_Offset and Esize of the components to the position and
7556 size used by Gigi. */
7558 static void
7559 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7561 Entity_Id gnat_field;
7562 tree gnu_list;
7564 /* We operate by first making a list of all fields and their position (we
7565 can get the size easily) and then update all the sizes in the tree. */
7566 gnu_list
7567 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7568 BIGGEST_ALIGNMENT, NULL_TREE);
7570 for (gnat_field = First_Entity (gnat_entity);
7571 Present (gnat_field);
7572 gnat_field = Next_Entity (gnat_field))
7573 if (Ekind (gnat_field) == E_Component
7574 || (Ekind (gnat_field) == E_Discriminant
7575 && !Is_Unchecked_Union (Scope (gnat_field))))
7577 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7578 gnu_list);
7579 if (t)
7581 tree parent_offset;
7583 /* If we are just annotating types and the type is tagged, the tag
7584 and the parent components are not generated by the front-end so
7585 we need to add the appropriate offset to each component without
7586 representation clause. */
7587 if (type_annotate_only
7588 && Is_Tagged_Type (gnat_entity)
7589 && No (Component_Clause (gnat_field)))
7591 /* For a component appearing in the current extension, the
7592 offset is the size of the parent. */
7593 if (Is_Derived_Type (gnat_entity)
7594 && Original_Record_Component (gnat_field) == gnat_field)
7595 parent_offset
7596 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7597 bitsizetype);
7598 else
7599 parent_offset = bitsize_int (POINTER_SIZE);
7601 if (TYPE_FIELDS (gnu_type))
7602 parent_offset
7603 = round_up (parent_offset,
7604 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7606 else
7607 parent_offset = bitsize_zero_node;
7609 Set_Component_Bit_Offset
7610 (gnat_field,
7611 annotate_value
7612 (size_binop (PLUS_EXPR,
7613 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7614 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7615 parent_offset)));
7617 Set_Esize (gnat_field,
7618 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7620 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7622 /* If there is no entry, this is an inherited component whose
7623 position is the same as in the parent type. */
7624 Set_Component_Bit_Offset
7625 (gnat_field,
7626 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7628 Set_Esize (gnat_field,
7629 Esize (Original_Record_Component (gnat_field)));
7634 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7635 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7636 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7637 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7638 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7639 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7640 pre-existing list to be chained to the newly created entries. */
7642 static tree
7643 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7644 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7646 tree gnu_field;
7648 for (gnu_field = TYPE_FIELDS (gnu_type);
7649 gnu_field;
7650 gnu_field = DECL_CHAIN (gnu_field))
7652 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7653 DECL_FIELD_BIT_OFFSET (gnu_field));
7654 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7655 DECL_FIELD_OFFSET (gnu_field));
7656 unsigned int our_offset_align
7657 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7658 tree v = make_tree_vec (3);
7660 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7661 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7662 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7663 gnu_list = tree_cons (gnu_field, v, gnu_list);
7665 /* Recurse on internal fields, flattening the nested fields except for
7666 those in the variant part, if requested. */
7667 if (DECL_INTERNAL_P (gnu_field))
7669 tree gnu_field_type = TREE_TYPE (gnu_field);
7670 if (do_not_flatten_variant
7671 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7672 gnu_list
7673 = build_position_list (gnu_field_type, do_not_flatten_variant,
7674 size_zero_node, bitsize_zero_node,
7675 BIGGEST_ALIGNMENT, gnu_list);
7676 else
7677 gnu_list
7678 = build_position_list (gnu_field_type, do_not_flatten_variant,
7679 gnu_our_offset, gnu_our_bitpos,
7680 our_offset_align, gnu_list);
7684 return gnu_list;
7687 /* Return a list describing the substitutions needed to reflect the
7688 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7689 be in any order. The values in an element of the list are in the form
7690 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7691 a definition of GNAT_SUBTYPE. */
7693 static vec<subst_pair>
7694 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7696 vec<subst_pair> gnu_list = vNULL;
7697 Entity_Id gnat_discrim;
7698 Node_Id gnat_constr;
7700 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7701 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7702 Present (gnat_discrim);
7703 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7704 gnat_constr = Next_Elmt (gnat_constr))
7705 /* Ignore access discriminants. */
7706 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7708 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7709 tree replacement = convert (TREE_TYPE (gnu_field),
7710 elaborate_expression
7711 (Node (gnat_constr), gnat_subtype,
7712 get_entity_name (gnat_discrim),
7713 definition, true, false));
7714 subst_pair s = {gnu_field, replacement};
7715 gnu_list.safe_push (s);
7718 return gnu_list;
7721 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7722 variants of QUAL_UNION_TYPE that are still relevant after applying
7723 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7724 list to be prepended to the newly created entries. */
7726 static vec<variant_desc>
7727 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7728 vec<variant_desc> gnu_list)
7730 tree gnu_field;
7732 for (gnu_field = TYPE_FIELDS (qual_union_type);
7733 gnu_field;
7734 gnu_field = DECL_CHAIN (gnu_field))
7736 tree qual = DECL_QUALIFIER (gnu_field);
7737 unsigned int i;
7738 subst_pair *s;
7740 FOR_EACH_VEC_ELT (subst_list, i, s)
7741 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7743 /* If the new qualifier is not unconditionally false, its variant may
7744 still be accessed. */
7745 if (!integer_zerop (qual))
7747 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7748 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7750 gnu_list.safe_push (v);
7752 /* Recurse on the variant subpart of the variant, if any. */
7753 variant_subpart = get_variant_part (variant_type);
7754 if (variant_subpart)
7755 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7756 subst_list, gnu_list);
7758 /* If the new qualifier is unconditionally true, the subsequent
7759 variants cannot be accessed. */
7760 if (integer_onep (qual))
7761 break;
7765 return gnu_list;
7768 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7769 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7770 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7771 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7772 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7773 true if we are being called to process the Component_Size of GNAT_OBJECT;
7774 this is used only for error messages. ZERO_OK is true if a size of zero
7775 is permitted; if ZERO_OK is false, it means that a size of zero should be
7776 treated as an unspecified size. */
7778 static tree
7779 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7780 enum tree_code kind, bool component_p, bool zero_ok)
7782 Node_Id gnat_error_node;
7783 tree type_size, size;
7785 /* Return 0 if no size was specified. */
7786 if (uint_size == No_Uint)
7787 return NULL_TREE;
7789 /* Ignore a negative size since that corresponds to our back-annotation. */
7790 if (UI_Lt (uint_size, Uint_0))
7791 return NULL_TREE;
7793 /* Find the node to use for error messages. */
7794 if ((Ekind (gnat_object) == E_Component
7795 || Ekind (gnat_object) == E_Discriminant)
7796 && Present (Component_Clause (gnat_object)))
7797 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7798 else if (Present (Size_Clause (gnat_object)))
7799 gnat_error_node = Expression (Size_Clause (gnat_object));
7800 else
7801 gnat_error_node = gnat_object;
7803 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7804 but cannot be represented in bitsizetype. */
7805 size = UI_To_gnu (uint_size, bitsizetype);
7806 if (TREE_OVERFLOW (size))
7808 if (component_p)
7809 post_error_ne ("component size for& is too large", gnat_error_node,
7810 gnat_object);
7811 else
7812 post_error_ne ("size for& is too large", gnat_error_node,
7813 gnat_object);
7814 return NULL_TREE;
7817 /* Ignore a zero size if it is not permitted. */
7818 if (!zero_ok && integer_zerop (size))
7819 return NULL_TREE;
7821 /* The size of objects is always a multiple of a byte. */
7822 if (kind == VAR_DECL
7823 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7825 if (component_p)
7826 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7827 gnat_error_node, gnat_object);
7828 else
7829 post_error_ne ("size for& is not a multiple of Storage_Unit",
7830 gnat_error_node, gnat_object);
7831 return NULL_TREE;
7834 /* If this is an integral type or a packed array type, the front-end has
7835 already verified the size, so we need not do it here (which would mean
7836 checking against the bounds). However, if this is an aliased object,
7837 it may not be smaller than the type of the object. */
7838 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7839 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7840 return size;
7842 /* If the object is a record that contains a template, add the size of the
7843 template to the specified size. */
7844 if (TREE_CODE (gnu_type) == RECORD_TYPE
7845 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7846 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7848 if (kind == VAR_DECL
7849 /* If a type needs strict alignment, a component of this type in
7850 a packed record cannot be packed and thus uses the type size. */
7851 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7852 type_size = TYPE_SIZE (gnu_type);
7853 else
7854 type_size = rm_size (gnu_type);
7856 /* Modify the size of a discriminated type to be the maximum size. */
7857 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7858 type_size = max_size (type_size, true);
7860 /* If this is an access type or a fat pointer, the minimum size is that given
7861 by the smallest integral mode that's valid for pointers. */
7862 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7864 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7865 while (!targetm.valid_pointer_mode (p_mode))
7866 p_mode = GET_MODE_WIDER_MODE (p_mode);
7867 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7870 /* Issue an error either if the default size of the object isn't a constant
7871 or if the new size is smaller than it. */
7872 if (TREE_CODE (type_size) != INTEGER_CST
7873 || TREE_OVERFLOW (type_size)
7874 || tree_int_cst_lt (size, type_size))
7876 if (component_p)
7877 post_error_ne_tree
7878 ("component size for& too small{, minimum allowed is ^}",
7879 gnat_error_node, gnat_object, type_size);
7880 else
7881 post_error_ne_tree
7882 ("size for& too small{, minimum allowed is ^}",
7883 gnat_error_node, gnat_object, type_size);
7884 return NULL_TREE;
7887 return size;
7890 /* Similarly, but both validate and process a value of RM size. This routine
7891 is only called for types. */
7893 static void
7894 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7896 Node_Id gnat_attr_node;
7897 tree old_size, size;
7899 /* Do nothing if no size was specified. */
7900 if (uint_size == No_Uint)
7901 return;
7903 /* Ignore a negative size since that corresponds to our back-annotation. */
7904 if (UI_Lt (uint_size, Uint_0))
7905 return;
7907 /* Only issue an error if a Value_Size clause was explicitly given.
7908 Otherwise, we'd be duplicating an error on the Size clause. */
7909 gnat_attr_node
7910 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7912 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7913 but cannot be represented in bitsizetype. */
7914 size = UI_To_gnu (uint_size, bitsizetype);
7915 if (TREE_OVERFLOW (size))
7917 if (Present (gnat_attr_node))
7918 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7919 gnat_entity);
7920 return;
7923 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7924 exists, or this is an integer type, in which case the front-end will
7925 have always set it. */
7926 if (No (gnat_attr_node)
7927 && integer_zerop (size)
7928 && !Has_Size_Clause (gnat_entity)
7929 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7930 return;
7932 old_size = rm_size (gnu_type);
7934 /* If the old size is self-referential, get the maximum size. */
7935 if (CONTAINS_PLACEHOLDER_P (old_size))
7936 old_size = max_size (old_size, true);
7938 /* Issue an error either if the old size of the object isn't a constant or
7939 if the new size is smaller than it. The front-end has already verified
7940 this for scalar and packed array types. */
7941 if (TREE_CODE (old_size) != INTEGER_CST
7942 || TREE_OVERFLOW (old_size)
7943 || (AGGREGATE_TYPE_P (gnu_type)
7944 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7945 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7946 && !(TYPE_IS_PADDING_P (gnu_type)
7947 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7948 && TYPE_PACKED_ARRAY_TYPE_P
7949 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7950 && tree_int_cst_lt (size, old_size)))
7952 if (Present (gnat_attr_node))
7953 post_error_ne_tree
7954 ("Value_Size for& too small{, minimum allowed is ^}",
7955 gnat_attr_node, gnat_entity, old_size);
7956 return;
7959 /* Otherwise, set the RM size proper for integral types... */
7960 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7961 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7962 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7963 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7964 SET_TYPE_RM_SIZE (gnu_type, size);
7966 /* ...or the Ada size for record and union types. */
7967 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
7968 && !TYPE_FAT_POINTER_P (gnu_type))
7969 SET_TYPE_ADA_SIZE (gnu_type, size);
7972 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7973 a type or object whose present alignment is ALIGN. If this alignment is
7974 valid, return it. Otherwise, give an error and return ALIGN. */
7976 static unsigned int
7977 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7979 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7980 unsigned int new_align;
7981 Node_Id gnat_error_node;
7983 /* Don't worry about checking alignment if alignment was not specified
7984 by the source program and we already posted an error for this entity. */
7985 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7986 return align;
7988 /* Post the error on the alignment clause if any. Note, for the implicit
7989 base type of an array type, the alignment clause is on the first
7990 subtype. */
7991 if (Present (Alignment_Clause (gnat_entity)))
7992 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7994 else if (Is_Itype (gnat_entity)
7995 && Is_Array_Type (gnat_entity)
7996 && Etype (gnat_entity) == gnat_entity
7997 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7998 gnat_error_node =
7999 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8001 else
8002 gnat_error_node = gnat_entity;
8004 /* Within GCC, an alignment is an integer, so we must make sure a value is
8005 specified that fits in that range. Also, there is an upper bound to
8006 alignments we can support/allow. */
8007 if (!UI_Is_In_Int_Range (alignment)
8008 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8009 post_error_ne_num ("largest supported alignment for& is ^",
8010 gnat_error_node, gnat_entity, max_allowed_alignment);
8011 else if (!(Present (Alignment_Clause (gnat_entity))
8012 && From_At_Mod (Alignment_Clause (gnat_entity)))
8013 && new_align * BITS_PER_UNIT < align)
8015 unsigned int double_align;
8016 bool is_capped_double, align_clause;
8018 /* If the default alignment of "double" or larger scalar types is
8019 specifically capped and the new alignment is above the cap, do
8020 not post an error and change the alignment only if there is an
8021 alignment clause; this makes it possible to have the associated
8022 GCC type overaligned by default for performance reasons. */
8023 if ((double_align = double_float_alignment) > 0)
8025 Entity_Id gnat_type
8026 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8027 is_capped_double
8028 = is_double_float_or_array (gnat_type, &align_clause);
8030 else if ((double_align = double_scalar_alignment) > 0)
8032 Entity_Id gnat_type
8033 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8034 is_capped_double
8035 = is_double_scalar_or_array (gnat_type, &align_clause);
8037 else
8038 is_capped_double = align_clause = false;
8040 if (is_capped_double && new_align >= double_align)
8042 if (align_clause)
8043 align = new_align * BITS_PER_UNIT;
8045 else
8047 if (is_capped_double)
8048 align = double_align * BITS_PER_UNIT;
8050 post_error_ne_num ("alignment for& must be at least ^",
8051 gnat_error_node, gnat_entity,
8052 align / BITS_PER_UNIT);
8055 else
8057 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8058 if (new_align > align)
8059 align = new_align;
8062 return align;
8065 /* Verify that OBJECT, a type or decl, is something we can implement
8066 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8067 if we require atomic components. */
8069 static void
8070 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8072 Node_Id gnat_error_point = gnat_entity;
8073 Node_Id gnat_node;
8074 machine_mode mode;
8075 unsigned int align;
8076 tree size;
8078 /* There are three case of what OBJECT can be. It can be a type, in which
8079 case we take the size, alignment and mode from the type. It can be a
8080 declaration that was indirect, in which case the relevant values are
8081 that of the type being pointed to, or it can be a normal declaration,
8082 in which case the values are of the decl. The code below assumes that
8083 OBJECT is either a type or a decl. */
8084 if (TYPE_P (object))
8086 /* If this is an anonymous base type, nothing to check. Error will be
8087 reported on the source type. */
8088 if (!Comes_From_Source (gnat_entity))
8089 return;
8091 mode = TYPE_MODE (object);
8092 align = TYPE_ALIGN (object);
8093 size = TYPE_SIZE (object);
8095 else if (DECL_BY_REF_P (object))
8097 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8098 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8099 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8101 else
8103 mode = DECL_MODE (object);
8104 align = DECL_ALIGN (object);
8105 size = DECL_SIZE (object);
8108 /* Consider all floating-point types atomic and any types that that are
8109 represented by integers no wider than a machine word. */
8110 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8111 || ((GET_MODE_CLASS (mode) == MODE_INT
8112 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8113 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8114 return;
8116 /* For the moment, also allow anything that has an alignment equal
8117 to its size and which is smaller than a word. */
8118 if (size && TREE_CODE (size) == INTEGER_CST
8119 && compare_tree_int (size, align) == 0
8120 && align <= BITS_PER_WORD)
8121 return;
8123 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8124 gnat_node = Next_Rep_Item (gnat_node))
8126 if (!comp_p && Nkind (gnat_node) == N_Pragma
8127 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8128 == Pragma_Atomic))
8129 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8130 else if (comp_p && Nkind (gnat_node) == N_Pragma
8131 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8132 == Pragma_Atomic_Components))
8133 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8136 if (comp_p)
8137 post_error_ne ("atomic access to component of & cannot be guaranteed",
8138 gnat_error_point, gnat_entity);
8139 else
8140 post_error_ne ("atomic access to & cannot be guaranteed",
8141 gnat_error_point, gnat_entity);
8145 /* Helper for the intrin compatibility checks family. Evaluate whether
8146 two types are definitely incompatible. */
8148 static bool
8149 intrin_types_incompatible_p (tree t1, tree t2)
8151 enum tree_code code;
8153 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8154 return false;
8156 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8157 return true;
8159 if (TREE_CODE (t1) != TREE_CODE (t2))
8160 return true;
8162 code = TREE_CODE (t1);
8164 switch (code)
8166 case INTEGER_TYPE:
8167 case REAL_TYPE:
8168 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8170 case POINTER_TYPE:
8171 case REFERENCE_TYPE:
8172 /* Assume designated types are ok. We'd need to account for char * and
8173 void * variants to do better, which could rapidly get messy and isn't
8174 clearly worth the effort. */
8175 return false;
8177 default:
8178 break;
8181 return false;
8184 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8185 on the Ada/builtin argument lists for the INB binding. */
8187 static bool
8188 intrin_arglists_compatible_p (intrin_binding_t * inb)
8190 function_args_iterator ada_iter, btin_iter;
8192 function_args_iter_init (&ada_iter, inb->ada_fntype);
8193 function_args_iter_init (&btin_iter, inb->btin_fntype);
8195 /* Sequence position of the last argument we checked. */
8196 int argpos = 0;
8198 while (1)
8200 tree ada_type = function_args_iter_cond (&ada_iter);
8201 tree btin_type = function_args_iter_cond (&btin_iter);
8203 /* If we've exhausted both lists simultaneously, we're done. */
8204 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8205 break;
8207 /* If one list is shorter than the other, they fail to match. */
8208 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8209 return false;
8211 /* If we're done with the Ada args and not with the internal builtin
8212 args, or the other way around, complain. */
8213 if (ada_type == void_type_node
8214 && btin_type != void_type_node)
8216 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8217 return false;
8220 if (btin_type == void_type_node
8221 && ada_type != void_type_node)
8223 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8224 inb->gnat_entity, inb->gnat_entity, argpos);
8225 return false;
8228 /* Otherwise, check that types match for the current argument. */
8229 argpos ++;
8230 if (intrin_types_incompatible_p (ada_type, btin_type))
8232 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8233 inb->gnat_entity, inb->gnat_entity, argpos);
8234 return false;
8238 function_args_iter_next (&ada_iter);
8239 function_args_iter_next (&btin_iter);
8242 return true;
8245 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8246 on the Ada/builtin return values for the INB binding. */
8248 static bool
8249 intrin_return_compatible_p (intrin_binding_t * inb)
8251 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8252 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8254 /* Accept function imported as procedure, common and convenient. */
8255 if (VOID_TYPE_P (ada_return_type)
8256 && !VOID_TYPE_P (btin_return_type))
8257 return true;
8259 /* If return type is Address (integer type), map it to void *. */
8260 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8261 ada_return_type = ptr_void_type_node;
8263 /* Check return types compatibility otherwise. Note that this
8264 handles void/void as well. */
8265 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8267 post_error ("?intrinsic binding type mismatch on return value!",
8268 inb->gnat_entity);
8269 return false;
8272 return true;
8275 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8276 compatible. Issue relevant warnings when they are not.
8278 This is intended as a light check to diagnose the most obvious cases, not
8279 as a full fledged type compatibility predicate. It is the programmer's
8280 responsibility to ensure correctness of the Ada declarations in Imports,
8281 especially when binding straight to a compiler internal. */
8283 static bool
8284 intrin_profiles_compatible_p (intrin_binding_t * inb)
8286 /* Check compatibility on return values and argument lists, each responsible
8287 for posting warnings as appropriate. Ensure use of the proper sloc for
8288 this purpose. */
8290 bool arglists_compatible_p, return_compatible_p;
8291 location_t saved_location = input_location;
8293 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8295 return_compatible_p = intrin_return_compatible_p (inb);
8296 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8298 input_location = saved_location;
8300 return return_compatible_p && arglists_compatible_p;
8303 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8304 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8305 specified size for this field. POS_LIST is a position list describing
8306 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8307 to this layout. */
8309 static tree
8310 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8311 tree size, tree pos_list,
8312 vec<subst_pair> subst_list)
8314 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8315 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8316 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8317 tree new_pos, new_field;
8318 unsigned int i;
8319 subst_pair *s;
8321 if (CONTAINS_PLACEHOLDER_P (pos))
8322 FOR_EACH_VEC_ELT (subst_list, i, s)
8323 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8325 /* If the position is now a constant, we can set it as the position of the
8326 field when we make it. Otherwise, we need to deal with it specially. */
8327 if (TREE_CONSTANT (pos))
8328 new_pos = bit_from_pos (pos, bitpos);
8329 else
8330 new_pos = NULL_TREE;
8332 new_field
8333 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8334 size, new_pos, DECL_PACKED (old_field),
8335 !DECL_NONADDRESSABLE_P (old_field));
8337 if (!new_pos)
8339 normalize_offset (&pos, &bitpos, offset_align);
8340 /* Finalize the position. */
8341 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8342 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8343 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8344 DECL_SIZE (new_field) = size;
8345 DECL_SIZE_UNIT (new_field)
8346 = convert (sizetype,
8347 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8348 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8351 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8352 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8353 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8354 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8356 return new_field;
8359 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8360 it is the minimal size the REP_PART must have. */
8362 static tree
8363 create_rep_part (tree rep_type, tree record_type, tree min_size)
8365 tree field;
8367 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8368 min_size = NULL_TREE;
8370 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8371 min_size, NULL_TREE, 0, 1);
8372 DECL_INTERNAL_P (field) = 1;
8374 return field;
8377 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8379 static tree
8380 get_rep_part (tree record_type)
8382 tree field = TYPE_FIELDS (record_type);
8384 /* The REP part is the first field, internal, another record, and its name
8385 starts with an 'R'. */
8386 if (field
8387 && DECL_INTERNAL_P (field)
8388 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8389 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8390 return field;
8392 return NULL_TREE;
8395 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8397 tree
8398 get_variant_part (tree record_type)
8400 tree field;
8402 /* The variant part is the only internal field that is a qualified union. */
8403 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8404 if (DECL_INTERNAL_P (field)
8405 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8406 return field;
8408 return NULL_TREE;
8411 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8412 the list of variants to be used and RECORD_TYPE is the type of the parent.
8413 POS_LIST is a position list describing the layout of fields present in
8414 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8415 layout. */
8417 static tree
8418 create_variant_part_from (tree old_variant_part,
8419 vec<variant_desc> variant_list,
8420 tree record_type, tree pos_list,
8421 vec<subst_pair> subst_list)
8423 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8424 tree old_union_type = TREE_TYPE (old_variant_part);
8425 tree new_union_type, new_variant_part;
8426 tree union_field_list = NULL_TREE;
8427 variant_desc *v;
8428 unsigned int i;
8430 /* First create the type of the variant part from that of the old one. */
8431 new_union_type = make_node (QUAL_UNION_TYPE);
8432 TYPE_NAME (new_union_type)
8433 = concat_name (TYPE_NAME (record_type),
8434 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8436 /* If the position of the variant part is constant, subtract it from the
8437 size of the type of the parent to get the new size. This manual CSE
8438 reduces the code size when not optimizing. */
8439 if (TREE_CODE (offset) == INTEGER_CST)
8441 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8442 tree first_bit = bit_from_pos (offset, bitpos);
8443 TYPE_SIZE (new_union_type)
8444 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8445 TYPE_SIZE_UNIT (new_union_type)
8446 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8447 byte_from_pos (offset, bitpos));
8448 SET_TYPE_ADA_SIZE (new_union_type,
8449 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8450 first_bit));
8451 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8452 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8454 else
8455 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8457 /* Now finish up the new variants and populate the union type. */
8458 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8460 tree old_field = v->field, new_field;
8461 tree old_variant, old_variant_subpart, new_variant, field_list;
8463 /* Skip variants that don't belong to this nesting level. */
8464 if (DECL_CONTEXT (old_field) != old_union_type)
8465 continue;
8467 /* Retrieve the list of fields already added to the new variant. */
8468 new_variant = v->new_type;
8469 field_list = TYPE_FIELDS (new_variant);
8471 /* If the old variant had a variant subpart, we need to create a new
8472 variant subpart and add it to the field list. */
8473 old_variant = v->type;
8474 old_variant_subpart = get_variant_part (old_variant);
8475 if (old_variant_subpart)
8477 tree new_variant_subpart
8478 = create_variant_part_from (old_variant_subpart, variant_list,
8479 new_variant, pos_list, subst_list);
8480 DECL_CHAIN (new_variant_subpart) = field_list;
8481 field_list = new_variant_subpart;
8484 /* Finish up the new variant and create the field. No need for debug
8485 info thanks to the XVS type. */
8486 finish_record_type (new_variant, nreverse (field_list), 2, false);
8487 compute_record_mode (new_variant);
8488 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8489 Empty);
8491 new_field
8492 = create_field_decl_from (old_field, new_variant, new_union_type,
8493 TYPE_SIZE (new_variant),
8494 pos_list, subst_list);
8495 DECL_QUALIFIER (new_field) = v->qual;
8496 DECL_INTERNAL_P (new_field) = 1;
8497 DECL_CHAIN (new_field) = union_field_list;
8498 union_field_list = new_field;
8501 /* Finish up the union type and create the variant part. No need for debug
8502 info thanks to the XVS type. Note that we don't reverse the field list
8503 because VARIANT_LIST has been traversed in reverse order. */
8504 finish_record_type (new_union_type, union_field_list, 2, false);
8505 compute_record_mode (new_union_type);
8506 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8507 Empty);
8509 new_variant_part
8510 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8511 TYPE_SIZE (new_union_type),
8512 pos_list, subst_list);
8513 DECL_INTERNAL_P (new_variant_part) = 1;
8515 /* With multiple discriminants it is possible for an inner variant to be
8516 statically selected while outer ones are not; in this case, the list
8517 of fields of the inner variant is not flattened and we end up with a
8518 qualified union with a single member. Drop the useless container. */
8519 if (!DECL_CHAIN (union_field_list))
8521 DECL_CONTEXT (union_field_list) = record_type;
8522 DECL_FIELD_OFFSET (union_field_list)
8523 = DECL_FIELD_OFFSET (new_variant_part);
8524 DECL_FIELD_BIT_OFFSET (union_field_list)
8525 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8526 SET_DECL_OFFSET_ALIGN (union_field_list,
8527 DECL_OFFSET_ALIGN (new_variant_part));
8528 new_variant_part = union_field_list;
8531 return new_variant_part;
8534 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8535 which are both RECORD_TYPE, after applying the substitutions described
8536 in SUBST_LIST. */
8538 static void
8539 copy_and_substitute_in_size (tree new_type, tree old_type,
8540 vec<subst_pair> subst_list)
8542 unsigned int i;
8543 subst_pair *s;
8545 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8546 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8547 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8548 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8549 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8551 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8552 FOR_EACH_VEC_ELT (subst_list, i, s)
8553 TYPE_SIZE (new_type)
8554 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8555 s->discriminant, s->replacement);
8557 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8558 FOR_EACH_VEC_ELT (subst_list, i, s)
8559 TYPE_SIZE_UNIT (new_type)
8560 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8561 s->discriminant, s->replacement);
8563 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8564 FOR_EACH_VEC_ELT (subst_list, i, s)
8565 SET_TYPE_ADA_SIZE
8566 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8567 s->discriminant, s->replacement));
8569 /* Finalize the size. */
8570 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8571 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8574 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8575 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8576 The parallel type is the original array type if it has been translated. */
8578 static void
8579 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8581 Entity_Id gnat_original_array_type
8582 = Underlying_Type (Original_Array_Type (gnat_entity));
8583 tree gnu_original_array_type;
8585 if (!present_gnu_tree (gnat_original_array_type))
8586 return;
8588 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8590 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8591 return;
8593 add_parallel_type (gnu_type, gnu_original_array_type);
8596 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8597 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8598 updated by replacing F with R.
8600 The function doesn't update the layout of the type, i.e. it assumes
8601 that the substitution is purely formal. That's why the replacement
8602 value R must itself contain a PLACEHOLDER_EXPR. */
8604 tree
8605 substitute_in_type (tree t, tree f, tree r)
8607 tree nt;
8609 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8611 switch (TREE_CODE (t))
8613 case INTEGER_TYPE:
8614 case ENUMERAL_TYPE:
8615 case BOOLEAN_TYPE:
8616 case REAL_TYPE:
8618 /* First the domain types of arrays. */
8619 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8620 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8622 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8623 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8625 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8626 return t;
8628 nt = copy_type (t);
8629 TYPE_GCC_MIN_VALUE (nt) = low;
8630 TYPE_GCC_MAX_VALUE (nt) = high;
8632 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8633 SET_TYPE_INDEX_TYPE
8634 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8636 return nt;
8639 /* Then the subtypes. */
8640 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8641 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8643 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8644 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8646 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8647 return t;
8649 nt = copy_type (t);
8650 SET_TYPE_RM_MIN_VALUE (nt, low);
8651 SET_TYPE_RM_MAX_VALUE (nt, high);
8653 return nt;
8656 return t;
8658 case COMPLEX_TYPE:
8659 nt = substitute_in_type (TREE_TYPE (t), f, r);
8660 if (nt == TREE_TYPE (t))
8661 return t;
8663 return build_complex_type (nt);
8665 case FUNCTION_TYPE:
8666 /* These should never show up here. */
8667 gcc_unreachable ();
8669 case ARRAY_TYPE:
8671 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8672 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8674 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8675 return t;
8677 nt = build_nonshared_array_type (component, domain);
8678 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8679 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8680 SET_TYPE_MODE (nt, TYPE_MODE (t));
8681 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8682 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8683 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8684 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8685 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8686 return nt;
8689 case RECORD_TYPE:
8690 case UNION_TYPE:
8691 case QUAL_UNION_TYPE:
8693 bool changed_field = false;
8694 tree field;
8696 /* Start out with no fields, make new fields, and chain them
8697 in. If we haven't actually changed the type of any field,
8698 discard everything we've done and return the old type. */
8699 nt = copy_type (t);
8700 TYPE_FIELDS (nt) = NULL_TREE;
8702 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8704 tree new_field = copy_node (field), new_n;
8706 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8707 if (new_n != TREE_TYPE (field))
8709 TREE_TYPE (new_field) = new_n;
8710 changed_field = true;
8713 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8714 if (new_n != DECL_FIELD_OFFSET (field))
8716 DECL_FIELD_OFFSET (new_field) = new_n;
8717 changed_field = true;
8720 /* Do the substitution inside the qualifier, if any. */
8721 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8723 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8724 if (new_n != DECL_QUALIFIER (field))
8726 DECL_QUALIFIER (new_field) = new_n;
8727 changed_field = true;
8731 DECL_CONTEXT (new_field) = nt;
8732 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8734 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8735 TYPE_FIELDS (nt) = new_field;
8738 if (!changed_field)
8739 return t;
8741 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8742 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8743 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8744 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8745 return nt;
8748 default:
8749 return t;
8753 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8754 needed to represent the object. */
8756 tree
8757 rm_size (tree gnu_type)
8759 /* For integral types, we store the RM size explicitly. */
8760 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8761 return TYPE_RM_SIZE (gnu_type);
8763 /* Return the RM size of the actual data plus the size of the template. */
8764 if (TREE_CODE (gnu_type) == RECORD_TYPE
8765 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8766 return
8767 size_binop (PLUS_EXPR,
8768 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8769 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8771 /* For record or union types, we store the size explicitly. */
8772 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8773 && !TYPE_FAT_POINTER_P (gnu_type)
8774 && TYPE_ADA_SIZE (gnu_type))
8775 return TYPE_ADA_SIZE (gnu_type);
8777 /* For other types, this is just the size. */
8778 return TYPE_SIZE (gnu_type);
8781 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8782 fully-qualified name, possibly with type information encoding.
8783 Otherwise, return the name. */
8785 tree
8786 get_entity_name (Entity_Id gnat_entity)
8788 Get_Encoded_Name (gnat_entity);
8789 return get_identifier_with_length (Name_Buffer, Name_Len);
8792 /* Return an identifier representing the external name to be used for
8793 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8794 and the specified suffix. */
8796 tree
8797 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8799 const Entity_Kind kind = Ekind (gnat_entity);
8800 const bool has_suffix = (suffix != NULL);
8801 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8802 String_Pointer sp = {suffix, &temp};
8804 Get_External_Name (gnat_entity, has_suffix, sp);
8806 /* A variable using the Stdcall convention lives in a DLL. We adjust
8807 its name to use the jump table, the _imp__NAME contains the address
8808 for the NAME variable. */
8809 if ((kind == E_Variable || kind == E_Constant)
8810 && Has_Stdcall_Convention (gnat_entity))
8812 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8813 char *new_name = (char *) alloca (len + 1);
8814 strcpy (new_name, STDCALL_PREFIX);
8815 strcat (new_name, Name_Buffer);
8816 return get_identifier_with_length (new_name, len);
8819 return get_identifier_with_length (Name_Buffer, Name_Len);
8822 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8823 string, return a new IDENTIFIER_NODE that is the concatenation of
8824 the name followed by "___" and the specified suffix. */
8826 tree
8827 concat_name (tree gnu_name, const char *suffix)
8829 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8830 char *new_name = (char *) alloca (len + 1);
8831 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8832 strcat (new_name, "___");
8833 strcat (new_name, suffix);
8834 return get_identifier_with_length (new_name, len);
8837 /* Initialize data structures of the decl.c module. */
8839 void
8840 init_gnat_decl (void)
8842 /* Initialize the cache of annotated values. */
8843 annotate_value_cache
8844 = htab_create_ggc (512, tree_int_map_hash, tree_int_map_eq, 0);
8847 /* Destroy data structures of the decl.c module. */
8849 void
8850 destroy_gnat_decl (void)
8852 /* Destroy the cache of annotated values. */
8853 htab_delete (annotate_value_cache);
8854 annotate_value_cache = NULL;
8857 #include "gt-ada-decl.h"