2015-01-06 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobb6df1ae79ef3bc87af147e4c58ca52dd1e0fa9a6
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. */
132 struct value_annotation_hasher : ggc_cache_hasher<tree_int_map *>
134 static inline hashval_t
135 hash (tree_int_map *m)
137 return htab_hash_pointer (m->base.from);
140 static inline bool
141 equal (tree_int_map *a, tree_int_map *b)
143 return a->base.from == b->base.from;
146 static void
147 handle_cache_entry (tree_int_map *&m)
149 extern void gt_ggc_mx (tree_int_map *&);
150 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
151 return;
152 else if (ggc_marked_p (m->base.from))
153 gt_ggc_mx (m);
154 else
155 m = static_cast<tree_int_map *> (HTAB_DELETED_ENTRY);
159 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
161 static bool allocatable_size_p (tree, bool);
162 static void prepend_one_attribute (struct attrib **,
163 enum attr_type, tree, tree, Node_Id);
164 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
165 static void prepend_attributes (struct attrib **, Entity_Id);
166 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
167 static bool type_has_variable_size (tree);
168 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
169 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
170 unsigned int);
171 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
172 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
173 bool *);
174 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
175 static tree change_qualified_type (tree, int);
176 static bool same_discriminant_p (Entity_Id, Entity_Id);
177 static bool array_type_has_nonaliased_component (tree, Entity_Id);
178 static bool compile_time_known_address_p (Node_Id);
179 static bool cannot_be_superflat_p (Node_Id);
180 static bool constructor_address_p (tree);
181 static int compare_field_bitpos (const PTR, const PTR);
182 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
183 bool, bool, bool, bool, bool, tree, tree *);
184 static Uint annotate_value (tree);
185 static void annotate_rep (Entity_Id, tree);
186 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
187 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
188 static vec<variant_desc> build_variant_list (tree,
189 vec<subst_pair> ,
190 vec<variant_desc> );
191 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
192 static void set_rm_size (Uint, tree, Entity_Id);
193 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
194 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
195 static tree create_field_decl_from (tree, tree, tree, tree, tree,
196 vec<subst_pair> );
197 static tree create_rep_part (tree, tree, tree);
198 static tree get_rep_part (tree);
199 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
200 tree, vec<subst_pair> );
201 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
202 static void add_parallel_type_for_packed_array (tree, Entity_Id);
204 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
205 to pass around calls performing profile compatibility checks. */
207 typedef struct {
208 Entity_Id gnat_entity; /* The Ada subprogram entity. */
209 tree ada_fntype; /* The corresponding GCC type node. */
210 tree btin_fntype; /* The GCC builtin function type node. */
211 } intrin_binding_t;
213 static bool intrin_profiles_compatible_p (intrin_binding_t *);
215 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
216 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
217 and associate the ..._DECL node with the input GNAT defining identifier.
219 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
220 initial value (in GCC tree form). This is optional for a variable. For
221 a renamed entity, GNU_EXPR gives the object being renamed.
223 DEFINITION is nonzero if this call is intended for a definition. This is
224 used for separate compilation where it is necessary to know whether an
225 external declaration or a definition must be created if the GCC equivalent
226 was not created previously. The value of 1 is normally used for a nonzero
227 DEFINITION, but a value of 2 is used in special circumstances, defined in
228 the code. */
230 tree
231 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
233 /* Contains the kind of the input GNAT node. */
234 const Entity_Kind kind = Ekind (gnat_entity);
235 /* True if this is a type. */
236 const bool is_type = IN (kind, Type_Kind);
237 /* True if debug info is requested for this entity. */
238 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
239 /* True if this entity is to be considered as imported. */
240 const bool imported_p
241 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
242 /* For a type, contains the equivalent GNAT node to be used in gigi. */
243 Entity_Id gnat_equiv_type = Empty;
244 /* Temporary used to walk the GNAT tree. */
245 Entity_Id gnat_temp;
246 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
247 This node will be associated with the GNAT node by calling at the end
248 of the `switch' statement. */
249 tree gnu_decl = NULL_TREE;
250 /* Contains the GCC type to be used for the GCC node. */
251 tree gnu_type = NULL_TREE;
252 /* Contains the GCC size tree to be used for the GCC node. */
253 tree gnu_size = NULL_TREE;
254 /* Contains the GCC name to be used for the GCC node. */
255 tree gnu_entity_name;
256 /* True if we have already saved gnu_decl as a GNAT association. */
257 bool saved = false;
258 /* True if we incremented defer_incomplete_level. */
259 bool this_deferred = false;
260 /* True if we incremented force_global. */
261 bool this_global = false;
262 /* True if we should check to see if elaborated during processing. */
263 bool maybe_present = false;
264 /* True if we made GNU_DECL and its type here. */
265 bool this_made_decl = false;
266 /* Size and alignment of the GCC node, if meaningful. */
267 unsigned int esize = 0, align = 0;
268 /* Contains the list of attributes directly attached to the entity. */
269 struct attrib *attr_list = NULL;
271 /* Since a use of an Itype is a definition, process it as such if it
272 is not in a with'ed unit. */
273 if (!definition
274 && is_type
275 && Is_Itype (gnat_entity)
276 && !present_gnu_tree (gnat_entity)
277 && In_Extended_Main_Code_Unit (gnat_entity))
279 /* Ensure that we are in a subprogram mentioned in the Scope chain of
280 this entity, our current scope is global, or we encountered a task
281 or entry (where we can't currently accurately check scoping). */
282 if (!current_function_decl
283 || DECL_ELABORATION_PROC_P (current_function_decl))
285 process_type (gnat_entity);
286 return get_gnu_tree (gnat_entity);
289 for (gnat_temp = Scope (gnat_entity);
290 Present (gnat_temp);
291 gnat_temp = Scope (gnat_temp))
293 if (Is_Type (gnat_temp))
294 gnat_temp = Underlying_Type (gnat_temp);
296 if (Ekind (gnat_temp) == E_Subprogram_Body)
297 gnat_temp
298 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
300 if (IN (Ekind (gnat_temp), Subprogram_Kind)
301 && Present (Protected_Body_Subprogram (gnat_temp)))
302 gnat_temp = Protected_Body_Subprogram (gnat_temp);
304 if (Ekind (gnat_temp) == E_Entry
305 || Ekind (gnat_temp) == E_Entry_Family
306 || Ekind (gnat_temp) == E_Task_Type
307 || (IN (Ekind (gnat_temp), Subprogram_Kind)
308 && present_gnu_tree (gnat_temp)
309 && (current_function_decl
310 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
312 process_type (gnat_entity);
313 return get_gnu_tree (gnat_entity);
317 /* This abort means the Itype has an incorrect scope, i.e. that its
318 scope does not correspond to the subprogram it is declared in. */
319 gcc_unreachable ();
322 /* If we've already processed this entity, return what we got last time.
323 If we are defining the node, we should not have already processed it.
324 In that case, we will abort below when we try to save a new GCC tree
325 for this object. We also need to handle the case of getting a dummy
326 type when a Full_View exists but be careful so as not to trigger its
327 premature elaboration. */
328 if ((!definition || (is_type && imported_p))
329 && present_gnu_tree (gnat_entity))
331 gnu_decl = get_gnu_tree (gnat_entity);
333 if (TREE_CODE (gnu_decl) == TYPE_DECL
334 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
335 && IN (kind, Incomplete_Or_Private_Kind)
336 && Present (Full_View (gnat_entity))
337 && (present_gnu_tree (Full_View (gnat_entity))
338 || No (Freeze_Node (Full_View (gnat_entity)))))
340 gnu_decl
341 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
342 save_gnu_tree (gnat_entity, NULL_TREE, false);
343 save_gnu_tree (gnat_entity, gnu_decl, false);
346 return gnu_decl;
349 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
350 must be specified unless it was specified by the programmer. Exceptions
351 are for access-to-protected-subprogram types and all access subtypes, as
352 another GNAT type is used to lay out the GCC type for them. */
353 gcc_assert (!Unknown_Esize (gnat_entity)
354 || Has_Size_Clause (gnat_entity)
355 || (!IN (kind, Numeric_Kind)
356 && !IN (kind, Enumeration_Kind)
357 && (!IN (kind, Access_Kind)
358 || kind == E_Access_Protected_Subprogram_Type
359 || kind == E_Anonymous_Access_Protected_Subprogram_Type
360 || kind == E_Access_Subtype
361 || type_annotate_only)));
363 /* The RM size must be specified for all discrete and fixed-point types. */
364 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
365 && Unknown_RM_Size (gnat_entity)));
367 /* If we get here, it means we have not yet done anything with this entity.
368 If we are not defining it, it must be a type or an entity that is defined
369 elsewhere or externally, otherwise we should have defined it already. */
370 gcc_assert (definition
371 || type_annotate_only
372 || is_type
373 || kind == E_Discriminant
374 || kind == E_Component
375 || kind == E_Label
376 || (kind == E_Constant && Present (Full_View (gnat_entity)))
377 || Is_Public (gnat_entity));
379 /* Get the name of the entity and set up the line number and filename of
380 the original definition for use in any decl we make. Make sure we do not
381 inherit another source location. */
382 gnu_entity_name = get_entity_name (gnat_entity);
383 if (Sloc (gnat_entity) != No_Location
384 && !renaming_from_generic_instantiation_p (gnat_entity))
385 Sloc_to_locus (Sloc (gnat_entity), &input_location);
387 /* For cases when we are not defining (i.e., we are referencing from
388 another compilation unit) public entities, show we are at global level
389 for the purpose of computing scopes. Don't do this for components or
390 discriminants since the relevant test is whether or not the record is
391 being defined. */
392 if (!definition
393 && kind != E_Component
394 && kind != E_Discriminant
395 && Is_Public (gnat_entity)
396 && !Is_Statically_Allocated (gnat_entity))
397 force_global++, this_global = true;
399 /* Handle any attributes directly attached to the entity. */
400 if (Has_Gigi_Rep_Item (gnat_entity))
401 prepend_attributes (&attr_list, gnat_entity);
403 /* Do some common processing for types. */
404 if (is_type)
406 /* Compute the equivalent type to be used in gigi. */
407 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
409 /* Machine_Attributes on types are expected to be propagated to
410 subtypes. The corresponding Gigi_Rep_Items are only attached
411 to the first subtype though, so we handle the propagation here. */
412 if (Base_Type (gnat_entity) != gnat_entity
413 && !Is_First_Subtype (gnat_entity)
414 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
415 prepend_attributes (&attr_list,
416 First_Subtype (Base_Type (gnat_entity)));
418 /* Compute a default value for the size of an elementary type. */
419 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
421 unsigned int max_esize;
423 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
424 esize = UI_To_Int (Esize (gnat_entity));
426 if (IN (kind, Float_Kind))
427 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
428 else if (IN (kind, Access_Kind))
429 max_esize = POINTER_SIZE * 2;
430 else
431 max_esize = LONG_LONG_TYPE_SIZE;
433 if (esize > max_esize)
434 esize = max_esize;
438 switch (kind)
440 case E_Constant:
441 /* If this is a use of a deferred constant without address clause,
442 get its full definition. */
443 if (!definition
444 && No (Address_Clause (gnat_entity))
445 && Present (Full_View (gnat_entity)))
447 gnu_decl
448 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
449 saved = true;
450 break;
453 /* If we have an external constant that we are not defining, get the
454 expression that is was defined to represent. We may throw it away
455 later if it is not a constant. But do not retrieve the expression
456 if it is an allocator because the designated type might be dummy
457 at this point. */
458 if (!definition
459 && !No_Initialization (Declaration_Node (gnat_entity))
460 && Present (Expression (Declaration_Node (gnat_entity)))
461 && Nkind (Expression (Declaration_Node (gnat_entity)))
462 != N_Allocator)
464 bool went_into_elab_proc = false;
465 int save_force_global = force_global;
467 /* The expression may contain N_Expression_With_Actions nodes and
468 thus object declarations from other units. In this case, even
469 though the expression will eventually be discarded since not a
470 constant, the declarations would be stuck either in the global
471 varpool or in the current scope. Therefore we force the local
472 context and create a fake scope that we'll zap at the end. */
473 if (!current_function_decl)
475 current_function_decl = get_elaboration_procedure ();
476 went_into_elab_proc = true;
478 force_global = 0;
479 gnat_pushlevel ();
481 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
483 gnat_zaplevel ();
484 force_global = save_force_global;
485 if (went_into_elab_proc)
486 current_function_decl = NULL_TREE;
489 /* Ignore deferred constant definitions without address clause since
490 they are processed fully in the front-end. If No_Initialization
491 is set, this is not a deferred constant but a constant whose value
492 is built manually. And constants that are renamings are handled
493 like variables. */
494 if (definition
495 && !gnu_expr
496 && No (Address_Clause (gnat_entity))
497 && !No_Initialization (Declaration_Node (gnat_entity))
498 && No (Renamed_Object (gnat_entity)))
500 gnu_decl = error_mark_node;
501 saved = true;
502 break;
505 /* Ignore constant definitions already marked with the error node. See
506 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
507 if (definition
508 && gnu_expr
509 && present_gnu_tree (gnat_entity)
510 && get_gnu_tree (gnat_entity) == error_mark_node)
512 maybe_present = true;
513 break;
516 goto object;
518 case E_Exception:
519 goto object;
521 case E_Component:
522 case E_Discriminant:
524 /* The GNAT record where the component was defined. */
525 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
527 /* If the entity is a discriminant of an extended tagged type used to
528 rename a discriminant of the parent type, return the latter. */
529 if (Is_Tagged_Type (gnat_record)
530 && Present (Corresponding_Discriminant (gnat_entity)))
532 gnu_decl
533 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
534 gnu_expr, definition);
535 saved = true;
536 break;
539 /* If the entity is an inherited component (in the case of extended
540 tagged record types), just return the original entity, which must
541 be a FIELD_DECL. Likewise for discriminants. If the entity is a
542 non-girder discriminant (in the case of derived untagged record
543 types), return the stored discriminant it renames. */
544 else if (Present (Original_Record_Component (gnat_entity))
545 && Original_Record_Component (gnat_entity) != gnat_entity)
547 gnu_decl
548 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
549 gnu_expr, definition);
550 saved = true;
551 break;
554 /* Otherwise, if we are not defining this and we have no GCC type
555 for the containing record, make one for it. Then we should
556 have made our own equivalent. */
557 else if (!definition && !present_gnu_tree (gnat_record))
559 /* ??? If this is in a record whose scope is a protected
560 type and we have an Original_Record_Component, use it.
561 This is a workaround for major problems in protected type
562 handling. */
563 Entity_Id Scop = Scope (Scope (gnat_entity));
564 if (Is_Protected_Type (Underlying_Type (Scop))
565 && Present (Original_Record_Component (gnat_entity)))
567 gnu_decl
568 = gnat_to_gnu_entity (Original_Record_Component
569 (gnat_entity),
570 gnu_expr, 0);
571 saved = true;
572 break;
575 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
576 gnu_decl = get_gnu_tree (gnat_entity);
577 saved = true;
578 break;
581 else
582 /* Here we have no GCC type and this is a reference rather than a
583 definition. This should never happen. Most likely the cause is
584 reference before declaration in the GNAT tree for gnat_entity. */
585 gcc_unreachable ();
588 case E_Loop_Parameter:
589 case E_Out_Parameter:
590 case E_Variable:
592 /* Simple variables, loop variables, Out parameters and exceptions. */
593 object:
595 /* Always create a variable for volatile objects and variables seen
596 constant but with a Linker_Section pragma. */
597 bool const_flag
598 = ((kind == E_Constant || kind == E_Variable)
599 && Is_True_Constant (gnat_entity)
600 && !(kind == E_Variable
601 && Present (Linker_Section_Pragma (gnat_entity)))
602 && !Treat_As_Volatile (gnat_entity)
603 && (((Nkind (Declaration_Node (gnat_entity))
604 == N_Object_Declaration)
605 && Present (Expression (Declaration_Node (gnat_entity))))
606 || Present (Renamed_Object (gnat_entity))
607 || imported_p));
608 bool inner_const_flag = const_flag;
609 bool static_p = Is_Statically_Allocated (gnat_entity);
610 bool mutable_p = false;
611 bool used_by_ref = false;
612 tree gnu_ext_name = NULL_TREE;
613 tree renamed_obj = NULL_TREE;
614 tree gnu_object_size;
616 if (Present (Renamed_Object (gnat_entity)) && !definition)
618 if (kind == E_Exception)
619 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
620 NULL_TREE, 0);
621 else
622 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
625 /* Get the type after elaborating the renamed object. */
626 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
628 /* If this is a standard exception definition, then use the standard
629 exception type. This is necessary to make sure that imported and
630 exported views of exceptions are properly merged in LTO mode. */
631 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
632 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
633 gnu_type = except_type_node;
635 /* For a debug renaming declaration, build a debug-only entity. */
636 if (Present (Debug_Renaming_Link (gnat_entity)))
638 /* Force a non-null value to make sure the symbol is retained. */
639 tree value = build1 (INDIRECT_REF, gnu_type,
640 build1 (NOP_EXPR,
641 build_pointer_type (gnu_type),
642 integer_minus_one_node));
643 gnu_decl = build_decl (input_location,
644 VAR_DECL, gnu_entity_name, gnu_type);
645 SET_DECL_VALUE_EXPR (gnu_decl, value);
646 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
647 gnat_pushdecl (gnu_decl, gnat_entity);
648 break;
651 /* If this is a loop variable, its type should be the base type.
652 This is because the code for processing a loop determines whether
653 a normal loop end test can be done by comparing the bounds of the
654 loop against those of the base type, which is presumed to be the
655 size used for computation. But this is not correct when the size
656 of the subtype is smaller than the type. */
657 if (kind == E_Loop_Parameter)
658 gnu_type = get_base_type (gnu_type);
660 /* Reject non-renamed objects whose type is an unconstrained array or
661 any object whose type is a dummy type or void. */
662 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
663 && No (Renamed_Object (gnat_entity)))
664 || TYPE_IS_DUMMY_P (gnu_type)
665 || TREE_CODE (gnu_type) == VOID_TYPE)
667 gcc_assert (type_annotate_only);
668 if (this_global)
669 force_global--;
670 return error_mark_node;
673 /* If an alignment is specified, use it if valid. Note that exceptions
674 are objects but don't have an alignment. We must do this before we
675 validate the size, since the alignment can affect the size. */
676 if (kind != E_Exception && Known_Alignment (gnat_entity))
678 gcc_assert (Present (Alignment (gnat_entity)));
680 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
681 TYPE_ALIGN (gnu_type));
683 /* No point in changing the type if there is an address clause
684 as the final type of the object will be a reference type. */
685 if (Present (Address_Clause (gnat_entity)))
686 align = 0;
687 else
689 tree orig_type = gnu_type;
691 gnu_type
692 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
693 false, false, definition, true);
695 /* If a padding record was made, declare it now since it will
696 never be declared otherwise. This is necessary to ensure
697 that its subtrees are properly marked. */
698 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
699 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
700 debug_info_p, gnat_entity);
704 /* If we are defining the object, see if it has a Size and validate it
705 if so. If we are not defining the object and a Size clause applies,
706 simply retrieve the value. We don't want to ignore the clause and
707 it is expected to have been validated already. Then get the new
708 type, if any. */
709 if (definition)
710 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
711 gnat_entity, VAR_DECL, false,
712 Has_Size_Clause (gnat_entity));
713 else if (Has_Size_Clause (gnat_entity))
714 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
716 if (gnu_size)
718 gnu_type
719 = make_type_from_size (gnu_type, gnu_size,
720 Has_Biased_Representation (gnat_entity));
722 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
723 gnu_size = NULL_TREE;
726 /* If this object has self-referential size, it must be a record with
727 a default discriminant. We are supposed to allocate an object of
728 the maximum size in this case, unless it is a constant with an
729 initializing expression, in which case we can get the size from
730 that. Note that the resulting size may still be a variable, so
731 this may end up with an indirect allocation. */
732 if (No (Renamed_Object (gnat_entity))
733 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
735 if (gnu_expr && kind == E_Constant)
737 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
738 if (CONTAINS_PLACEHOLDER_P (size))
740 /* If the initializing expression is itself a constant,
741 despite having a nominal type with self-referential
742 size, we can get the size directly from it. */
743 if (TREE_CODE (gnu_expr) == COMPONENT_REF
744 && TYPE_IS_PADDING_P
745 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
746 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
747 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
748 || DECL_READONLY_ONCE_ELAB
749 (TREE_OPERAND (gnu_expr, 0))))
750 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
751 else
752 gnu_size
753 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
755 else
756 gnu_size = size;
758 /* We may have no GNU_EXPR because No_Initialization is
759 set even though there's an Expression. */
760 else if (kind == E_Constant
761 && (Nkind (Declaration_Node (gnat_entity))
762 == N_Object_Declaration)
763 && Present (Expression (Declaration_Node (gnat_entity))))
764 gnu_size
765 = TYPE_SIZE (gnat_to_gnu_type
766 (Etype
767 (Expression (Declaration_Node (gnat_entity)))));
768 else
770 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
771 mutable_p = true;
774 /* If we are at global level and the size isn't constant, call
775 elaborate_expression_1 to make a variable for it rather than
776 calculating it each time. */
777 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
778 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
779 get_identifier ("SIZE"),
780 definition, false);
783 /* If the size is zero byte, make it one byte since some linkers have
784 troubles with zero-sized objects. If the object will have a
785 template, that will make it nonzero so don't bother. Also avoid
786 doing that for an object renaming or an object with an address
787 clause, as we would lose useful information on the view size
788 (e.g. for null array slices) and we are not allocating the object
789 here anyway. */
790 if (((gnu_size
791 && integer_zerop (gnu_size)
792 && !TREE_OVERFLOW (gnu_size))
793 || (TYPE_SIZE (gnu_type)
794 && integer_zerop (TYPE_SIZE (gnu_type))
795 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
796 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
797 && No (Renamed_Object (gnat_entity))
798 && No (Address_Clause (gnat_entity)))
799 gnu_size = bitsize_unit_node;
801 /* If this is an object with no specified size and alignment, and
802 if either it is atomic or we are not optimizing alignment for
803 space and it is composite and not an exception, an Out parameter
804 or a reference to another object, and the size of its type is a
805 constant, set the alignment to the smallest one which is not
806 smaller than the size, with an appropriate cap. */
807 if (!gnu_size && align == 0
808 && (Is_Atomic (gnat_entity)
809 || (!Optimize_Alignment_Space (gnat_entity)
810 && kind != E_Exception
811 && kind != E_Out_Parameter
812 && Is_Composite_Type (Etype (gnat_entity))
813 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
814 && !Is_Exported (gnat_entity)
815 && !imported_p
816 && No (Renamed_Object (gnat_entity))
817 && No (Address_Clause (gnat_entity))))
818 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
820 unsigned int size_cap, align_cap;
822 /* No point in promoting the alignment if this doesn't prevent
823 BLKmode access to the object, in particular block copy, as
824 this will for example disable the NRV optimization for it.
825 No point in jumping through all the hoops needed in order
826 to support BIGGEST_ALIGNMENT if we don't really have to.
827 So we cap to the smallest alignment that corresponds to
828 a known efficient memory access pattern of the target. */
829 if (Is_Atomic (gnat_entity))
831 size_cap = UINT_MAX;
832 align_cap = BIGGEST_ALIGNMENT;
834 else
836 size_cap = MAX_FIXED_MODE_SIZE;
837 align_cap = get_mode_alignment (ptr_mode);
840 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
841 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
842 align = 0;
843 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
844 align = align_cap;
845 else
846 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
848 /* But make sure not to under-align the object. */
849 if (align <= TYPE_ALIGN (gnu_type))
850 align = 0;
852 /* And honor the minimum valid atomic alignment, if any. */
853 #ifdef MINIMUM_ATOMIC_ALIGNMENT
854 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
855 align = MINIMUM_ATOMIC_ALIGNMENT;
856 #endif
859 /* If the object is set to have atomic components, find the component
860 type and validate it.
862 ??? Note that we ignore Has_Volatile_Components on objects; it's
863 not at all clear what to do in that case. */
864 if (Has_Atomic_Components (gnat_entity))
866 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
867 ? TREE_TYPE (gnu_type) : gnu_type);
869 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
870 && TYPE_MULTI_ARRAY_P (gnu_inner))
871 gnu_inner = TREE_TYPE (gnu_inner);
873 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
876 /* Now check if the type of the object allows atomic access. Note
877 that we must test the type, even if this object has size and
878 alignment to allow such access, because we will be going inside
879 the padded record to assign to the object. We could fix this by
880 always copying via an intermediate value, but it's not clear it's
881 worth the effort. */
882 if (Is_Atomic (gnat_entity))
883 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
885 /* If this is an aliased object with an unconstrained nominal subtype,
886 make a type that includes the template. */
887 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
888 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
889 && !type_annotate_only)
891 tree gnu_array
892 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
893 gnu_type
894 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
895 gnu_type,
896 concat_name (gnu_entity_name,
897 "UNC"),
898 debug_info_p);
901 /* ??? If this is an object of CW type initialized to a value, try to
902 ensure that the object is sufficient aligned for this value, but
903 without pessimizing the allocation. This is a kludge necessary
904 because we don't support dynamic alignment. */
905 if (align == 0
906 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
907 && No (Renamed_Object (gnat_entity))
908 && No (Address_Clause (gnat_entity)))
909 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
911 #ifdef MINIMUM_ATOMIC_ALIGNMENT
912 /* If the size is a constant and no alignment is specified, force
913 the alignment to be the minimum valid atomic alignment. The
914 restriction on constant size avoids problems with variable-size
915 temporaries; if the size is variable, there's no issue with
916 atomic access. Also don't do this for a constant, since it isn't
917 necessary and can interfere with constant replacement. Finally,
918 do not do it for Out parameters since that creates an
919 size inconsistency with In parameters. */
920 if (align == 0
921 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
922 && !FLOAT_TYPE_P (gnu_type)
923 && !const_flag && No (Renamed_Object (gnat_entity))
924 && !imported_p && No (Address_Clause (gnat_entity))
925 && kind != E_Out_Parameter
926 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
927 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
928 align = MINIMUM_ATOMIC_ALIGNMENT;
929 #endif
931 /* Make a new type with the desired size and alignment, if needed.
932 But do not take into account alignment promotions to compute the
933 size of the object. */
934 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
935 if (gnu_size || align > 0)
937 tree orig_type = gnu_type;
939 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
940 false, false, definition, true);
942 /* If a padding record was made, declare it now since it will
943 never be declared otherwise. This is necessary to ensure
944 that its subtrees are properly marked. */
945 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
946 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
947 debug_info_p, gnat_entity);
950 /* If this is a renaming, avoid as much as possible to create a new
951 object. However, in several cases, creating it is required.
952 This processing needs to be applied to the raw expression so
953 as to make it more likely to rename the underlying object. */
954 if (Present (Renamed_Object (gnat_entity)))
956 bool create_normal_object = false;
958 /* If the renamed object had padding, strip off the reference
959 to the inner object and reset our type. */
960 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
961 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
962 /* Strip useless conversions around the object. */
963 || gnat_useless_type_conversion (gnu_expr))
965 gnu_expr = TREE_OPERAND (gnu_expr, 0);
966 gnu_type = TREE_TYPE (gnu_expr);
969 /* Or else, if the renamed object has an unconstrained type with
970 default discriminant, use the padded type. */
971 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
972 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
973 == gnu_type
974 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
975 gnu_type = TREE_TYPE (gnu_expr);
977 /* Case 1: If this is a constant renaming stemming from a function
978 call, treat it as a normal object whose initial value is what is
979 being renamed. RM 3.3 says that the result of evaluating a
980 function call is a constant object. Treat constant literals
981 the same way. As a consequence, it can be the inner object of
982 a constant renaming. In this case, the renaming must be fully
983 instantiated, i.e. it cannot be a mere reference to (part of) an
984 existing object. */
985 if (const_flag)
987 tree inner_object = gnu_expr;
988 while (handled_component_p (inner_object))
989 inner_object = TREE_OPERAND (inner_object, 0);
990 if (TREE_CODE (inner_object) == CALL_EXPR
991 || CONSTANT_CLASS_P (inner_object))
992 create_normal_object = true;
995 /* Otherwise, see if we can proceed with a stabilized version of
996 the renamed entity or if we need to make a new object. */
997 if (!create_normal_object)
999 tree maybe_stable_expr = NULL_TREE;
1000 bool stable = false;
1002 /* Case 2: If the renaming entity need not be materialized and
1003 the renamed expression is something we can stabilize, use
1004 that for the renaming. At the global level, we can only do
1005 this if we know no SAVE_EXPRs need be made, because the
1006 expression we return might be used in arbitrary conditional
1007 branches so we must force the evaluation of the SAVE_EXPRs
1008 immediately and this requires a proper function context.
1009 Note that an external constant is at the global level. */
1010 if (!Materialize_Entity (gnat_entity)
1011 && (!((!definition && kind == E_Constant)
1012 || global_bindings_p ())
1013 || (staticp (gnu_expr)
1014 && !TREE_SIDE_EFFECTS (gnu_expr))))
1016 maybe_stable_expr
1017 = gnat_stabilize_reference (gnu_expr, true, &stable);
1019 if (stable)
1021 /* ??? No DECL_EXPR is created so we need to mark
1022 the expression manually lest it is shared. */
1023 if ((!definition && kind == E_Constant)
1024 || global_bindings_p ())
1025 MARK_VISITED (maybe_stable_expr);
1026 gnu_decl = maybe_stable_expr;
1027 save_gnu_tree (gnat_entity, gnu_decl, true);
1028 saved = true;
1029 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1030 false);
1031 /* This assertion will fail if the renamed object
1032 isn't aligned enough as to make it possible to
1033 honor the alignment set on the renaming. */
1034 if (align)
1036 unsigned int renamed_align
1037 = DECL_P (gnu_decl)
1038 ? DECL_ALIGN (gnu_decl)
1039 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1040 gcc_assert (renamed_align >= align);
1042 break;
1045 /* The stabilization failed. Keep maybe_stable_expr
1046 untouched here to let the pointer case below know
1047 about that failure. */
1050 /* Case 3: Make this into a constant pointer to the object we
1051 are to rename and attach the object to the pointer if it is
1052 something we can stabilize.
1054 From the proper scope, attached objects will be referenced
1055 directly instead of indirectly via the pointer to avoid
1056 subtle aliasing problems with non-addressable entities.
1057 They have to be stable because we must not evaluate the
1058 variables in the expression every time the renaming is used.
1059 The pointer is called a "renaming" pointer in this case.
1061 In the rare cases where we cannot stabilize the renamed
1062 object, we just make a "bare" pointer and the renamed
1063 object will always be accessed indirectly through it.
1065 Note that we need to preserve the volatility of the renamed
1066 object through the indirection. */
1067 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1068 gnu_type
1069 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1070 gnu_type = build_reference_type (gnu_type);
1071 inner_const_flag = TREE_READONLY (gnu_expr);
1072 const_flag = true;
1074 /* If the previous attempt at stabilizing failed, there is
1075 no point in trying again and we reuse the result without
1076 attaching it to the pointer. In this case it will only
1077 be used as the initializing expression of the pointer and
1078 thus needs no special treatment with regard to multiple
1079 evaluations.
1081 Otherwise, try to stabilize and attach the expression to
1082 the pointer if the stabilization succeeds.
1084 Note that this might introduce SAVE_EXPRs and we don't
1085 check whether we are at the global level or not. This
1086 is fine since we are building a pointer initializer and
1087 neither the pointer nor the initializing expression can
1088 be accessed before the pointer elaboration has taken
1089 place in a correct program.
1091 These SAVE_EXPRs will be evaluated at the right place
1092 by either the evaluation of the initializer for the
1093 non-global case or the elaboration code for the global
1094 case, and will be attached to the elaboration procedure
1095 in the latter case. */
1096 if (!maybe_stable_expr)
1098 maybe_stable_expr
1099 = gnat_stabilize_reference (gnu_expr, true, &stable);
1101 if (stable)
1102 renamed_obj = maybe_stable_expr;
1105 if (type_annotate_only
1106 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1107 gnu_expr = NULL_TREE;
1108 else
1109 gnu_expr
1110 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1112 gnu_size = NULL_TREE;
1113 used_by_ref = true;
1117 /* Make a volatile version of this object's type if we are to make
1118 the object volatile. We also interpret 13.3(19) conservatively
1119 and disallow any optimizations for such a non-constant object. */
1120 if ((Treat_As_Volatile (gnat_entity)
1121 || (!const_flag
1122 && gnu_type != except_type_node
1123 && (Is_Exported (gnat_entity)
1124 || imported_p
1125 || Present (Address_Clause (gnat_entity)))))
1126 && !TYPE_VOLATILE (gnu_type))
1127 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1129 /* If we are defining an aliased object whose nominal subtype is
1130 unconstrained, the object is a record that contains both the
1131 template and the object. If there is an initializer, it will
1132 have already been converted to the right type, but we need to
1133 create the template if there is no initializer. */
1134 if (definition
1135 && !gnu_expr
1136 && TREE_CODE (gnu_type) == RECORD_TYPE
1137 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1138 /* Beware that padding might have been introduced above. */
1139 || (TYPE_PADDING_P (gnu_type)
1140 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1141 == RECORD_TYPE
1142 && TYPE_CONTAINS_TEMPLATE_P
1143 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1145 tree template_field
1146 = TYPE_PADDING_P (gnu_type)
1147 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1148 : TYPE_FIELDS (gnu_type);
1149 vec<constructor_elt, va_gc> *v;
1150 vec_alloc (v, 1);
1151 tree t = build_template (TREE_TYPE (template_field),
1152 TREE_TYPE (DECL_CHAIN (template_field)),
1153 NULL_TREE);
1154 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1155 gnu_expr = gnat_build_constructor (gnu_type, v);
1158 /* Convert the expression to the type of the object except in the
1159 case where the object's type is unconstrained or the object's type
1160 is a padded record whose field is of self-referential size. In
1161 the former case, converting will generate unnecessary evaluations
1162 of the CONSTRUCTOR to compute the size and in the latter case, we
1163 want to only copy the actual data. Also don't convert to a record
1164 type with a variant part from a record type without one, to keep
1165 the object simpler. */
1166 if (gnu_expr
1167 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1168 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1169 && !(TYPE_IS_PADDING_P (gnu_type)
1170 && CONTAINS_PLACEHOLDER_P
1171 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1172 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1173 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1174 && get_variant_part (gnu_type) != NULL_TREE
1175 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1176 gnu_expr = convert (gnu_type, gnu_expr);
1178 /* If this is a pointer that doesn't have an initializing expression,
1179 initialize it to NULL, unless the object is imported. */
1180 if (definition
1181 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1182 && !gnu_expr
1183 && !Is_Imported (gnat_entity))
1184 gnu_expr = integer_zero_node;
1186 /* If we are defining the object and it has an Address clause, we must
1187 either get the address expression from the saved GCC tree for the
1188 object if it has a Freeze node, or elaborate the address expression
1189 here since the front-end has guaranteed that the elaboration has no
1190 effects in this case. */
1191 if (definition && Present (Address_Clause (gnat_entity)))
1193 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1194 tree gnu_address
1195 = present_gnu_tree (gnat_entity)
1196 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1198 save_gnu_tree (gnat_entity, NULL_TREE, false);
1200 /* Ignore the size. It's either meaningless or was handled
1201 above. */
1202 gnu_size = NULL_TREE;
1203 /* Convert the type of the object to a reference type that can
1204 alias everything as per 13.3(19). */
1205 gnu_type
1206 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1207 gnu_address = convert (gnu_type, gnu_address);
1208 used_by_ref = true;
1209 const_flag
1210 = !Is_Public (gnat_entity)
1211 || compile_time_known_address_p (gnat_expr);
1213 /* If this is a deferred constant, the initializer is attached to
1214 the full view. */
1215 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1216 gnu_expr
1217 = gnat_to_gnu
1218 (Expression (Declaration_Node (Full_View (gnat_entity))));
1220 /* If we don't have an initializing expression for the underlying
1221 variable, the initializing expression for the pointer is the
1222 specified address. Otherwise, we have to make a COMPOUND_EXPR
1223 to assign both the address and the initial value. */
1224 if (!gnu_expr)
1225 gnu_expr = gnu_address;
1226 else
1227 gnu_expr
1228 = build2 (COMPOUND_EXPR, gnu_type,
1229 build_binary_op
1230 (MODIFY_EXPR, NULL_TREE,
1231 build_unary_op (INDIRECT_REF, NULL_TREE,
1232 gnu_address),
1233 gnu_expr),
1234 gnu_address);
1237 /* If it has an address clause and we are not defining it, mark it
1238 as an indirect object. Likewise for Stdcall objects that are
1239 imported. */
1240 if ((!definition && Present (Address_Clause (gnat_entity)))
1241 || (Is_Imported (gnat_entity)
1242 && Has_Stdcall_Convention (gnat_entity)))
1244 /* Convert the type of the object to a reference type that can
1245 alias everything as per 13.3(19). */
1246 gnu_type
1247 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1248 gnu_size = NULL_TREE;
1250 /* No point in taking the address of an initializing expression
1251 that isn't going to be used. */
1252 gnu_expr = NULL_TREE;
1254 /* If it has an address clause whose value is known at compile
1255 time, make the object a CONST_DECL. This will avoid a
1256 useless dereference. */
1257 if (Present (Address_Clause (gnat_entity)))
1259 Node_Id gnat_address
1260 = Expression (Address_Clause (gnat_entity));
1262 if (compile_time_known_address_p (gnat_address))
1264 gnu_expr = gnat_to_gnu (gnat_address);
1265 const_flag = true;
1269 used_by_ref = true;
1272 /* If we are at top level and this object is of variable size,
1273 make the actual type a hidden pointer to the real type and
1274 make the initializer be a memory allocation and initialization.
1275 Likewise for objects we aren't defining (presumed to be
1276 external references from other packages), but there we do
1277 not set up an initialization.
1279 If the object's size overflows, make an allocator too, so that
1280 Storage_Error gets raised. Note that we will never free
1281 such memory, so we presume it never will get allocated. */
1282 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1283 global_bindings_p ()
1284 || !definition
1285 || static_p)
1286 || (gnu_size
1287 && !allocatable_size_p (convert (sizetype,
1288 size_binop
1289 (CEIL_DIV_EXPR, gnu_size,
1290 bitsize_unit_node)),
1291 global_bindings_p ()
1292 || !definition
1293 || static_p)))
1295 gnu_type = build_reference_type (gnu_type);
1296 gnu_size = NULL_TREE;
1297 used_by_ref = true;
1299 /* In case this was a aliased object whose nominal subtype is
1300 unconstrained, the pointer above will be a thin pointer and
1301 build_allocator will automatically make the template.
1303 If we have a template initializer only (that we made above),
1304 pretend there is none and rely on what build_allocator creates
1305 again anyway. Otherwise (if we have a full initializer), get
1306 the data part and feed that to build_allocator.
1308 If we are elaborating a mutable object, tell build_allocator to
1309 ignore a possibly simpler size from the initializer, if any, as
1310 we must allocate the maximum possible size in this case. */
1311 if (definition && !imported_p)
1313 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1315 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1316 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1318 gnu_alloc_type
1319 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1321 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1322 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1323 gnu_expr = 0;
1324 else
1325 gnu_expr
1326 = build_component_ref
1327 (gnu_expr, NULL_TREE,
1328 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1329 false);
1332 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1333 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1334 post_error ("?`Storage_Error` will be raised at run time!",
1335 gnat_entity);
1337 gnu_expr
1338 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1339 Empty, Empty, gnat_entity, mutable_p);
1340 const_flag = true;
1342 else
1344 gnu_expr = NULL_TREE;
1345 const_flag = false;
1349 /* If this object would go into the stack and has an alignment larger
1350 than the largest stack alignment the back-end can honor, resort to
1351 a variable of "aligning type". */
1352 if (!global_bindings_p () && !static_p && definition
1353 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1355 /* Create the new variable. No need for extra room before the
1356 aligned field as this is in automatic storage. */
1357 tree gnu_new_type
1358 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1359 TYPE_SIZE_UNIT (gnu_type),
1360 BIGGEST_ALIGNMENT, 0, gnat_entity);
1361 tree gnu_new_var
1362 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1363 NULL_TREE, gnu_new_type, NULL_TREE, false,
1364 false, false, false, NULL, gnat_entity);
1366 /* Initialize the aligned field if we have an initializer. */
1367 if (gnu_expr)
1368 add_stmt_with_node
1369 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1370 build_component_ref
1371 (gnu_new_var, NULL_TREE,
1372 TYPE_FIELDS (gnu_new_type), false),
1373 gnu_expr),
1374 gnat_entity);
1376 /* And setup this entity as a reference to the aligned field. */
1377 gnu_type = build_reference_type (gnu_type);
1378 gnu_expr
1379 = build_unary_op
1380 (ADDR_EXPR, gnu_type,
1381 build_component_ref (gnu_new_var, NULL_TREE,
1382 TYPE_FIELDS (gnu_new_type), false));
1384 gnu_size = NULL_TREE;
1385 used_by_ref = true;
1386 const_flag = true;
1389 /* If this is an aliased object with an unconstrained nominal subtype,
1390 we make its type a thin reference, i.e. the reference counterpart
1391 of a thin pointer, so that it points to the array part. This is
1392 aimed at making it easier for the debugger to decode the object.
1393 Note that we have to do that this late because of the couple of
1394 allocation adjustments that might be made just above. */
1395 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1396 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1397 && !type_annotate_only)
1399 tree gnu_array
1400 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1402 /* In case the object with the template has already been allocated
1403 just above, we have nothing to do here. */
1404 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1406 tree gnu_unc_var
1407 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1408 NULL_TREE, gnu_type, gnu_expr,
1409 const_flag, Is_Public (gnat_entity),
1410 imported_p || !definition, static_p,
1411 NULL, gnat_entity);
1412 gnu_expr
1413 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1414 TREE_CONSTANT (gnu_expr) = 1;
1416 gnu_size = NULL_TREE;
1417 used_by_ref = true;
1418 inner_const_flag = TREE_READONLY (gnu_unc_var);
1419 const_flag = true;
1422 gnu_type
1423 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1426 if (const_flag)
1427 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1429 /* Convert the expression to the type of the object except in the
1430 case where the object's type is unconstrained or the object's type
1431 is a padded record whose field is of self-referential size. In
1432 the former case, converting will generate unnecessary evaluations
1433 of the CONSTRUCTOR to compute the size and in the latter case, we
1434 want to only copy the actual data. Also don't convert to a record
1435 type with a variant part from a record type without one, to keep
1436 the object simpler. */
1437 if (gnu_expr
1438 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1439 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1440 && !(TYPE_IS_PADDING_P (gnu_type)
1441 && CONTAINS_PLACEHOLDER_P
1442 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1443 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1444 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1445 && get_variant_part (gnu_type) != NULL_TREE
1446 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1447 gnu_expr = convert (gnu_type, gnu_expr);
1449 /* If this name is external or a name was specified, use it, but don't
1450 use the Interface_Name with an address clause (see cd30005). */
1451 if ((Present (Interface_Name (gnat_entity))
1452 && No (Address_Clause (gnat_entity)))
1453 || (Is_Public (gnat_entity)
1454 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1455 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1457 /* If this is an aggregate constant initialized to a constant, force it
1458 to be statically allocated. This saves an initialization copy. */
1459 if (!static_p
1460 && const_flag
1461 && gnu_expr && TREE_CONSTANT (gnu_expr)
1462 && AGGREGATE_TYPE_P (gnu_type)
1463 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1464 && !(TYPE_IS_PADDING_P (gnu_type)
1465 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1466 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1467 static_p = true;
1469 /* Deal with a pragma Linker_Section on a constant or variable. */
1470 if ((kind == E_Constant || kind == E_Variable)
1471 && Present (Linker_Section_Pragma (gnat_entity)))
1472 prepend_one_attribute_pragma (&attr_list,
1473 Linker_Section_Pragma (gnat_entity));
1475 /* Now create the variable or the constant and set various flags. */
1476 gnu_decl
1477 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1478 gnu_expr, const_flag, Is_Public (gnat_entity),
1479 imported_p || !definition, static_p,
1480 !renamed_obj, attr_list, gnat_entity);
1481 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1482 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1483 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1485 /* If we are defining an Out parameter and optimization isn't enabled,
1486 create a fake PARM_DECL for debugging purposes and make it point to
1487 the VAR_DECL. Suppress debug info for the latter but make sure it
1488 will live in memory so that it can be accessed from within the
1489 debugger through the PARM_DECL. */
1490 if (kind == E_Out_Parameter
1491 && definition
1492 && debug_info_p
1493 && !optimize
1494 && !flag_generate_lto)
1496 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1497 gnat_pushdecl (param, gnat_entity);
1498 SET_DECL_VALUE_EXPR (param, gnu_decl);
1499 DECL_HAS_VALUE_EXPR_P (param) = 1;
1500 DECL_IGNORED_P (gnu_decl) = 1;
1501 TREE_ADDRESSABLE (gnu_decl) = 1;
1504 /* If this is a loop parameter, set the corresponding flag. */
1505 else if (kind == E_Loop_Parameter)
1506 DECL_LOOP_PARM_P (gnu_decl) = 1;
1508 /* If this is a renaming pointer, attach the renamed object to it and
1509 register it if we are at the global level. Note that an external
1510 constant is at the global level. */
1511 if (renamed_obj)
1513 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1514 if ((!definition && kind == E_Constant) || global_bindings_p ())
1516 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1517 record_global_renaming_pointer (gnu_decl);
1521 /* If this is a constant and we are defining it or it generates a real
1522 symbol at the object level and we are referencing it, we may want
1523 or need to have a true variable to represent it:
1524 - if optimization isn't enabled, for debugging purposes,
1525 - if the constant is public and not overlaid on something else,
1526 - if its address is taken,
1527 - if either itself or its type is aliased. */
1528 if (TREE_CODE (gnu_decl) == CONST_DECL
1529 && (definition || Sloc (gnat_entity) > Standard_Location)
1530 && ((!optimize && debug_info_p)
1531 || (Is_Public (gnat_entity)
1532 && No (Address_Clause (gnat_entity)))
1533 || Address_Taken (gnat_entity)
1534 || Is_Aliased (gnat_entity)
1535 || Is_Aliased (Etype (gnat_entity))))
1537 tree gnu_corr_var
1538 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1539 gnu_expr, true, Is_Public (gnat_entity),
1540 !definition, static_p, attr_list,
1541 gnat_entity);
1543 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1545 /* As debugging information will be generated for the variable,
1546 do not generate debugging information for the constant. */
1547 if (debug_info_p)
1548 DECL_IGNORED_P (gnu_decl) = 1;
1549 else
1550 DECL_IGNORED_P (gnu_corr_var) = 1;
1553 /* If this is a constant, even if we don't need a true variable, we
1554 may need to avoid returning the initializer in every case. That
1555 can happen for the address of a (constant) constructor because,
1556 upon dereferencing it, the constructor will be reinjected in the
1557 tree, which may not be valid in every case; see lvalue_required_p
1558 for more details. */
1559 if (TREE_CODE (gnu_decl) == CONST_DECL)
1560 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1562 /* If this object is declared in a block that contains a block with an
1563 exception handler, and we aren't using the GCC exception mechanism,
1564 we must force this variable in memory in order to avoid an invalid
1565 optimization. */
1566 if (Exception_Mechanism != Back_End_Exceptions
1567 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1568 TREE_ADDRESSABLE (gnu_decl) = 1;
1570 /* If this is a local variable with non-BLKmode and aggregate type,
1571 and optimization isn't enabled, then force it in memory so that
1572 a register won't be allocated to it with possible subparts left
1573 uninitialized and reaching the register allocator. */
1574 else if (TREE_CODE (gnu_decl) == VAR_DECL
1575 && !DECL_EXTERNAL (gnu_decl)
1576 && !TREE_STATIC (gnu_decl)
1577 && DECL_MODE (gnu_decl) != BLKmode
1578 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1579 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1580 && !optimize)
1581 TREE_ADDRESSABLE (gnu_decl) = 1;
1583 /* If we are defining an object with variable size or an object with
1584 fixed size that will be dynamically allocated, and we are using the
1585 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1586 if (definition
1587 && Exception_Mechanism == Setjmp_Longjmp
1588 && get_block_jmpbuf_decl ()
1589 && DECL_SIZE_UNIT (gnu_decl)
1590 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1591 || (flag_stack_check == GENERIC_STACK_CHECK
1592 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1593 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1594 add_stmt_with_node (build_call_n_expr
1595 (update_setjmp_buf_decl, 1,
1596 build_unary_op (ADDR_EXPR, NULL_TREE,
1597 get_block_jmpbuf_decl ())),
1598 gnat_entity);
1600 /* Back-annotate Esize and Alignment of the object if not already
1601 known. Note that we pick the values of the type, not those of
1602 the object, to shield ourselves from low-level platform-dependent
1603 adjustments like alignment promotion. This is both consistent with
1604 all the treatment above, where alignment and size are set on the
1605 type of the object and not on the object directly, and makes it
1606 possible to support all confirming representation clauses. */
1607 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1608 used_by_ref);
1610 break;
1612 case E_Void:
1613 /* Return a TYPE_DECL for "void" that we previously made. */
1614 gnu_decl = TYPE_NAME (void_type_node);
1615 break;
1617 case E_Enumeration_Type:
1618 /* A special case: for the types Character and Wide_Character in
1619 Standard, we do not list all the literals. So if the literals
1620 are not specified, make this an unsigned integer type. */
1621 if (No (First_Literal (gnat_entity)))
1623 gnu_type = make_unsigned_type (esize);
1624 TYPE_NAME (gnu_type) = gnu_entity_name;
1626 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1627 This is needed by the DWARF-2 back-end to distinguish between
1628 unsigned integer types and character types. */
1629 TYPE_STRING_FLAG (gnu_type) = 1;
1631 else
1633 /* We have a list of enumeral constants in First_Literal. We make a
1634 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1635 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1636 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1637 value of the literal. But when we have a regular boolean type, we
1638 simplify this a little by using a BOOLEAN_TYPE. */
1639 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1640 && !Has_Non_Standard_Rep (gnat_entity);
1641 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1642 tree gnu_list = NULL_TREE;
1643 Entity_Id gnat_literal;
1645 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1646 TYPE_PRECISION (gnu_type) = esize;
1647 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1648 set_min_and_max_values_for_integral_type (gnu_type, esize,
1649 TYPE_SIGN (gnu_type));
1650 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1651 layout_type (gnu_type);
1653 for (gnat_literal = First_Literal (gnat_entity);
1654 Present (gnat_literal);
1655 gnat_literal = Next_Literal (gnat_literal))
1657 tree gnu_value
1658 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1659 tree gnu_literal
1660 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1661 gnu_type, gnu_value, true, false, false,
1662 false, NULL, gnat_literal);
1663 /* Do not generate debug info for individual enumerators. */
1664 DECL_IGNORED_P (gnu_literal) = 1;
1665 save_gnu_tree (gnat_literal, gnu_literal, false);
1666 gnu_list
1667 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1670 if (!is_boolean)
1671 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1673 /* Note that the bounds are updated at the end of this function
1674 to avoid an infinite recursion since they refer to the type. */
1675 goto discrete_type;
1677 break;
1679 case E_Signed_Integer_Type:
1680 case E_Ordinary_Fixed_Point_Type:
1681 case E_Decimal_Fixed_Point_Type:
1682 /* For integer types, just make a signed type the appropriate number
1683 of bits. */
1684 gnu_type = make_signed_type (esize);
1685 goto discrete_type;
1687 case E_Modular_Integer_Type:
1689 /* For modular types, make the unsigned type of the proper number
1690 of bits and then set up the modulus, if required. */
1691 tree gnu_modulus, gnu_high = NULL_TREE;
1693 /* Packed Array Impl. Types are supposed to be subtypes only. */
1694 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1696 gnu_type = make_unsigned_type (esize);
1698 /* Get the modulus in this type. If it overflows, assume it is because
1699 it is equal to 2**Esize. Note that there is no overflow checking
1700 done on unsigned type, so we detect the overflow by looking for
1701 a modulus of zero, which is otherwise invalid. */
1702 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1704 if (!integer_zerop (gnu_modulus))
1706 TYPE_MODULAR_P (gnu_type) = 1;
1707 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1708 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1709 convert (gnu_type, integer_one_node));
1712 /* If the upper bound is not maximal, make an extra subtype. */
1713 if (gnu_high
1714 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1716 tree gnu_subtype = make_unsigned_type (esize);
1717 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1718 TREE_TYPE (gnu_subtype) = gnu_type;
1719 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1720 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1721 gnu_type = gnu_subtype;
1724 goto discrete_type;
1726 case E_Signed_Integer_Subtype:
1727 case E_Enumeration_Subtype:
1728 case E_Modular_Integer_Subtype:
1729 case E_Ordinary_Fixed_Point_Subtype:
1730 case E_Decimal_Fixed_Point_Subtype:
1732 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1733 not want to call create_range_type since we would like each subtype
1734 node to be distinct. ??? Historically this was in preparation for
1735 when memory aliasing is implemented, but that's obsolete now given
1736 the call to relate_alias_sets below.
1738 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1739 this fact is used by the arithmetic conversion functions.
1741 We elaborate the Ancestor_Subtype if it is not in the current unit
1742 and one of our bounds is non-static. We do this to ensure consistent
1743 naming in the case where several subtypes share the same bounds, by
1744 elaborating the first such subtype first, thus using its name. */
1746 if (!definition
1747 && Present (Ancestor_Subtype (gnat_entity))
1748 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1749 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1750 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1751 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1753 /* Set the precision to the Esize except for bit-packed arrays. */
1754 if (Is_Packed_Array_Impl_Type (gnat_entity)
1755 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1756 esize = UI_To_Int (RM_Size (gnat_entity));
1758 /* This should be an unsigned type if the base type is unsigned or
1759 if the lower bound is constant and non-negative or if the type
1760 is biased. */
1761 if (Is_Unsigned_Type (Etype (gnat_entity))
1762 || Is_Unsigned_Type (gnat_entity)
1763 || Has_Biased_Representation (gnat_entity))
1764 gnu_type = make_unsigned_type (esize);
1765 else
1766 gnu_type = make_signed_type (esize);
1767 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1769 SET_TYPE_RM_MIN_VALUE
1770 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1771 gnat_entity, get_identifier ("L"),
1772 definition, true,
1773 Needs_Debug_Info (gnat_entity)));
1775 SET_TYPE_RM_MAX_VALUE
1776 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1777 gnat_entity, get_identifier ("U"),
1778 definition, true,
1779 Needs_Debug_Info (gnat_entity)));
1781 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1782 = Has_Biased_Representation (gnat_entity);
1784 /* Inherit our alias set from what we're a subtype of. Subtypes
1785 are not different types and a pointer can designate any instance
1786 within a subtype hierarchy. */
1787 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1789 /* One of the above calls might have caused us to be elaborated,
1790 so don't blow up if so. */
1791 if (present_gnu_tree (gnat_entity))
1793 maybe_present = true;
1794 break;
1797 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1798 TYPE_STUB_DECL (gnu_type)
1799 = create_type_stub_decl (gnu_entity_name, gnu_type);
1801 /* For a packed array, make the original array type a parallel type. */
1802 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1803 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1805 discrete_type:
1807 /* We have to handle clauses that under-align the type specially. */
1808 if ((Present (Alignment_Clause (gnat_entity))
1809 || (Is_Packed_Array_Impl_Type (gnat_entity)
1810 && Present
1811 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1812 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1814 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1815 if (align >= TYPE_ALIGN (gnu_type))
1816 align = 0;
1819 /* If the type we are dealing with represents a bit-packed array,
1820 we need to have the bits left justified on big-endian targets
1821 and right justified on little-endian targets. We also need to
1822 ensure that when the value is read (e.g. for comparison of two
1823 such values), we only get the good bits, since the unused bits
1824 are uninitialized. Both goals are accomplished by wrapping up
1825 the modular type in an enclosing record type. */
1826 if (Is_Packed_Array_Impl_Type (gnat_entity)
1827 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1829 tree gnu_field_type, gnu_field;
1831 /* Set the RM size before wrapping up the original type. */
1832 SET_TYPE_RM_SIZE (gnu_type,
1833 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1834 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1836 /* Create a stripped-down declaration, mainly for debugging. */
1837 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1838 gnat_entity);
1840 /* Now save it and build the enclosing record type. */
1841 gnu_field_type = gnu_type;
1843 gnu_type = make_node (RECORD_TYPE);
1844 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1845 TYPE_PACKED (gnu_type) = 1;
1846 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1847 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1848 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1850 /* Propagate the alignment of the modular type to the record type,
1851 unless there is an alignment clause that under-aligns the type.
1852 This means that bit-packed arrays are given "ceil" alignment for
1853 their size by default, which may seem counter-intuitive but makes
1854 it possible to overlay them on modular types easily. */
1855 TYPE_ALIGN (gnu_type)
1856 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1858 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1860 /* Don't declare the field as addressable since we won't be taking
1861 its address and this would prevent create_field_decl from making
1862 a bitfield. */
1863 gnu_field
1864 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1865 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1867 /* Do not emit debug info until after the parallel type is added. */
1868 finish_record_type (gnu_type, gnu_field, 2, false);
1869 compute_record_mode (gnu_type);
1870 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1872 if (debug_info_p)
1874 /* Make the original array type a parallel type. */
1875 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1877 rest_of_record_type_compilation (gnu_type);
1881 /* If the type we are dealing with has got a smaller alignment than the
1882 natural one, we need to wrap it up in a record type and misalign the
1883 latter; we reuse the padding machinery for this purpose. Note that,
1884 even if the record type is marked as packed because of misalignment,
1885 we don't pack the field so as to give it the size of the type. */
1886 else if (align > 0)
1888 tree gnu_field_type, gnu_field;
1890 /* Set the RM size before wrapping up the type. */
1891 SET_TYPE_RM_SIZE (gnu_type,
1892 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1894 /* Create a stripped-down declaration, mainly for debugging. */
1895 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1896 gnat_entity);
1898 /* Now save it and build the enclosing record type. */
1899 gnu_field_type = gnu_type;
1901 gnu_type = make_node (RECORD_TYPE);
1902 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1903 TYPE_PACKED (gnu_type) = 1;
1904 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1905 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1906 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1907 TYPE_ALIGN (gnu_type) = align;
1908 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1910 /* Don't declare the field as addressable since we won't be taking
1911 its address and this would prevent create_field_decl from making
1912 a bitfield. */
1913 gnu_field
1914 = create_field_decl (get_identifier ("F"), gnu_field_type,
1915 gnu_type, TYPE_SIZE (gnu_field_type),
1916 bitsize_zero_node, 0, 0);
1918 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1919 compute_record_mode (gnu_type);
1920 TYPE_PADDING_P (gnu_type) = 1;
1923 break;
1925 case E_Floating_Point_Type:
1926 /* The type of the Low and High bounds can be our type if this is
1927 a type from Standard, so set them at the end of the function. */
1928 gnu_type = make_node (REAL_TYPE);
1929 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1930 layout_type (gnu_type);
1931 break;
1933 case E_Floating_Point_Subtype:
1934 /* See the E_Signed_Integer_Subtype case for the rationale. */
1935 if (!definition
1936 && Present (Ancestor_Subtype (gnat_entity))
1937 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1938 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1939 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1940 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1942 gnu_type = make_node (REAL_TYPE);
1943 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1944 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1945 TYPE_GCC_MIN_VALUE (gnu_type)
1946 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1947 TYPE_GCC_MAX_VALUE (gnu_type)
1948 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1949 layout_type (gnu_type);
1951 SET_TYPE_RM_MIN_VALUE
1952 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1953 gnat_entity, get_identifier ("L"),
1954 definition, true,
1955 Needs_Debug_Info (gnat_entity)));
1957 SET_TYPE_RM_MAX_VALUE
1958 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1959 gnat_entity, get_identifier ("U"),
1960 definition, true,
1961 Needs_Debug_Info (gnat_entity)));
1963 /* Inherit our alias set from what we're a subtype of, as for
1964 integer subtypes. */
1965 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1967 /* One of the above calls might have caused us to be elaborated,
1968 so don't blow up if so. */
1969 maybe_present = true;
1970 break;
1972 /* Array Types and Subtypes
1974 Unconstrained array types are represented by E_Array_Type and
1975 constrained array types are represented by E_Array_Subtype. There
1976 are no actual objects of an unconstrained array type; all we have
1977 are pointers to that type.
1979 The following fields are defined on array types and subtypes:
1981 Component_Type Component type of the array.
1982 Number_Dimensions Number of dimensions (an int).
1983 First_Index Type of first index. */
1985 case E_Array_Type:
1987 const bool convention_fortran_p
1988 = (Convention (gnat_entity) == Convention_Fortran);
1989 const int ndim = Number_Dimensions (gnat_entity);
1990 tree gnu_template_type;
1991 tree gnu_ptr_template;
1992 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1993 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1994 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1995 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1996 Entity_Id gnat_index, gnat_name;
1997 int index;
1998 tree comp_type;
2000 /* Create the type for the component now, as it simplifies breaking
2001 type reference loops. */
2002 comp_type
2003 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2004 if (present_gnu_tree (gnat_entity))
2006 /* As a side effect, the type may have been translated. */
2007 maybe_present = true;
2008 break;
2011 /* We complete an existing dummy fat pointer type in place. This both
2012 avoids further complex adjustments in update_pointer_to and yields
2013 better debugging information in DWARF by leveraging the support for
2014 incomplete declarations of "tagged" types in the DWARF back-end. */
2015 gnu_type = get_dummy_type (gnat_entity);
2016 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2018 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2019 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2020 /* Save the contents of the dummy type for update_pointer_to. */
2021 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2022 gnu_ptr_template =
2023 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2024 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2026 else
2028 gnu_fat_type = make_node (RECORD_TYPE);
2029 gnu_template_type = make_node (RECORD_TYPE);
2030 gnu_ptr_template = build_pointer_type (gnu_template_type);
2033 /* Make a node for the array. If we are not defining the array
2034 suppress expanding incomplete types. */
2035 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2037 if (!definition)
2039 defer_incomplete_level++;
2040 this_deferred = true;
2043 /* Build the fat pointer type. Use a "void *" object instead of
2044 a pointer to the array type since we don't have the array type
2045 yet (it will reference the fat pointer via the bounds). */
2047 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2048 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2049 DECL_CHAIN (tem)
2050 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2051 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2053 if (COMPLETE_TYPE_P (gnu_fat_type))
2055 /* We are going to lay it out again so reset the alias set. */
2056 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2057 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2058 finish_fat_pointer_type (gnu_fat_type, tem);
2059 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2060 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2062 TYPE_FIELDS (t) = tem;
2063 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2066 else
2068 finish_fat_pointer_type (gnu_fat_type, tem);
2069 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2072 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2073 is the fat pointer. This will be used to access the individual
2074 fields once we build them. */
2075 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2076 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2077 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2078 gnu_template_reference
2079 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2080 TREE_READONLY (gnu_template_reference) = 1;
2081 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2083 /* Now create the GCC type for each index and add the fields for that
2084 index to the template. */
2085 for (index = (convention_fortran_p ? ndim - 1 : 0),
2086 gnat_index = First_Index (gnat_entity);
2087 0 <= index && index < ndim;
2088 index += (convention_fortran_p ? - 1 : 1),
2089 gnat_index = Next_Index (gnat_index))
2091 char field_name[16];
2092 tree gnu_index_base_type
2093 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2094 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2095 tree gnu_min, gnu_max, gnu_high;
2097 /* Make the FIELD_DECLs for the low and high bounds of this
2098 type and then make extractions of these fields from the
2099 template. */
2100 sprintf (field_name, "LB%d", index);
2101 gnu_lb_field = create_field_decl (get_identifier (field_name),
2102 gnu_index_base_type,
2103 gnu_template_type, NULL_TREE,
2104 NULL_TREE, 0, 0);
2105 Sloc_to_locus (Sloc (gnat_entity),
2106 &DECL_SOURCE_LOCATION (gnu_lb_field));
2108 field_name[0] = 'U';
2109 gnu_hb_field = create_field_decl (get_identifier (field_name),
2110 gnu_index_base_type,
2111 gnu_template_type, NULL_TREE,
2112 NULL_TREE, 0, 0);
2113 Sloc_to_locus (Sloc (gnat_entity),
2114 &DECL_SOURCE_LOCATION (gnu_hb_field));
2116 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2118 /* We can't use build_component_ref here since the template type
2119 isn't complete yet. */
2120 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2121 gnu_template_reference, gnu_lb_field,
2122 NULL_TREE);
2123 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2124 gnu_template_reference, gnu_hb_field,
2125 NULL_TREE);
2126 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2128 gnu_min = convert (sizetype, gnu_orig_min);
2129 gnu_max = convert (sizetype, gnu_orig_max);
2131 /* Compute the size of this dimension. See the E_Array_Subtype
2132 case below for the rationale. */
2133 gnu_high
2134 = build3 (COND_EXPR, sizetype,
2135 build2 (GE_EXPR, boolean_type_node,
2136 gnu_orig_max, gnu_orig_min),
2137 gnu_max,
2138 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2140 /* Make a range type with the new range in the Ada base type.
2141 Then make an index type with the size range in sizetype. */
2142 gnu_index_types[index]
2143 = create_index_type (gnu_min, gnu_high,
2144 create_range_type (gnu_index_base_type,
2145 gnu_orig_min,
2146 gnu_orig_max),
2147 gnat_entity);
2149 /* Update the maximum size of the array in elements. */
2150 if (gnu_max_size)
2152 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2153 tree gnu_min
2154 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2155 tree gnu_max
2156 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2157 tree gnu_this_max
2158 = size_binop (PLUS_EXPR, size_one_node,
2159 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2161 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2162 && TREE_OVERFLOW (gnu_this_max))
2163 gnu_max_size = NULL_TREE;
2164 else
2165 gnu_max_size
2166 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2169 TYPE_NAME (gnu_index_types[index])
2170 = create_concat_name (gnat_entity, field_name);
2173 /* Install all the fields into the template. */
2174 TYPE_NAME (gnu_template_type)
2175 = create_concat_name (gnat_entity, "XUB");
2176 gnu_template_fields = NULL_TREE;
2177 for (index = 0; index < ndim; index++)
2178 gnu_template_fields
2179 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2180 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2181 debug_info_p);
2182 TYPE_READONLY (gnu_template_type) = 1;
2184 /* If Component_Size is not already specified, annotate it with the
2185 size of the component. */
2186 if (Unknown_Component_Size (gnat_entity))
2187 Set_Component_Size (gnat_entity,
2188 annotate_value (TYPE_SIZE (comp_type)));
2190 /* Compute the maximum size of the array in units and bits. */
2191 if (gnu_max_size)
2193 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2194 TYPE_SIZE_UNIT (comp_type));
2195 gnu_max_size = size_binop (MULT_EXPR,
2196 convert (bitsizetype, gnu_max_size),
2197 TYPE_SIZE (comp_type));
2199 else
2200 gnu_max_size_unit = NULL_TREE;
2202 /* Now build the array type. */
2203 tem = comp_type;
2204 for (index = ndim - 1; index >= 0; index--)
2206 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2207 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2208 sorry ("non-default Scalar_Storage_Order");
2209 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2210 if (array_type_has_nonaliased_component (tem, gnat_entity))
2211 TYPE_NONALIASED_COMPONENT (tem) = 1;
2213 /* If it is passed by reference, force BLKmode to ensure that
2214 objects of this type will always be put in memory. */
2215 if (TYPE_MODE (tem) != BLKmode
2216 && Is_By_Reference_Type (gnat_entity))
2217 SET_TYPE_MODE (tem, BLKmode);
2220 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2222 /* If an alignment is specified, use it if valid. But ignore it
2223 for the original type of packed array types. If the alignment
2224 was requested with an explicit alignment clause, state so. */
2225 if (No (Packed_Array_Impl_Type (gnat_entity))
2226 && Known_Alignment (gnat_entity))
2228 TYPE_ALIGN (tem)
2229 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2230 TYPE_ALIGN (tem));
2231 if (Present (Alignment_Clause (gnat_entity)))
2232 TYPE_USER_ALIGN (tem) = 1;
2235 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2237 /* Adjust the type of the pointer-to-array field of the fat pointer
2238 and record the aliasing relationships if necessary. */
2239 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2240 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2241 record_component_aliases (gnu_fat_type);
2243 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2244 corresponding fat pointer. */
2245 TREE_TYPE (gnu_type) = gnu_fat_type;
2246 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2247 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2248 SET_TYPE_MODE (gnu_type, BLKmode);
2249 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2251 /* If the maximum size doesn't overflow, use it. */
2252 if (gnu_max_size
2253 && TREE_CODE (gnu_max_size) == INTEGER_CST
2254 && !TREE_OVERFLOW (gnu_max_size)
2255 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2256 && !TREE_OVERFLOW (gnu_max_size_unit))
2258 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2259 TYPE_SIZE (tem));
2260 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2261 TYPE_SIZE_UNIT (tem));
2264 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2265 !Comes_From_Source (gnat_entity), debug_info_p,
2266 gnat_entity);
2268 /* Give the fat pointer type a name. If this is a packed array, tell
2269 the debugger how to interpret the underlying bits. */
2270 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2271 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2272 else
2273 gnat_name = gnat_entity;
2274 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2275 !Comes_From_Source (gnat_entity), debug_info_p,
2276 gnat_entity);
2278 /* Create the type to be designated by thin pointers: a record type for
2279 the array and its template. We used to shift the fields to have the
2280 template at a negative offset, but this was somewhat of a kludge; we
2281 now shift thin pointer values explicitly but only those which have a
2282 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2283 tem = build_unc_object_type (gnu_template_type, tem,
2284 create_concat_name (gnat_name, "XUT"),
2285 debug_info_p);
2287 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2288 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2290 break;
2292 case E_Array_Subtype:
2294 /* This is the actual data type for array variables. Multidimensional
2295 arrays are implemented as arrays of arrays. Note that arrays which
2296 have sparse enumeration subtypes as index components create sparse
2297 arrays, which is obviously space inefficient but so much easier to
2298 code for now.
2300 Also note that the subtype never refers to the unconstrained array
2301 type, which is somewhat at variance with Ada semantics.
2303 First check to see if this is simply a renaming of the array type.
2304 If so, the result is the array type. */
2306 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2307 if (!Is_Constrained (gnat_entity))
2309 else
2311 Entity_Id gnat_index, gnat_base_index;
2312 const bool convention_fortran_p
2313 = (Convention (gnat_entity) == Convention_Fortran);
2314 const int ndim = Number_Dimensions (gnat_entity);
2315 tree gnu_base_type = gnu_type;
2316 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2317 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2318 bool need_index_type_struct = false;
2319 int index;
2321 /* First create the GCC type for each index and find out whether
2322 special types are needed for debugging information. */
2323 for (index = (convention_fortran_p ? ndim - 1 : 0),
2324 gnat_index = First_Index (gnat_entity),
2325 gnat_base_index
2326 = First_Index (Implementation_Base_Type (gnat_entity));
2327 0 <= index && index < ndim;
2328 index += (convention_fortran_p ? - 1 : 1),
2329 gnat_index = Next_Index (gnat_index),
2330 gnat_base_index = Next_Index (gnat_base_index))
2332 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2333 tree gnu_index_base_type = get_base_type (gnu_index_type);
2334 tree gnu_orig_min
2335 = convert (gnu_index_base_type,
2336 TYPE_MIN_VALUE (gnu_index_type));
2337 tree gnu_orig_max
2338 = convert (gnu_index_base_type,
2339 TYPE_MAX_VALUE (gnu_index_type));
2340 tree gnu_min = convert (sizetype, gnu_orig_min);
2341 tree gnu_max = convert (sizetype, gnu_orig_max);
2342 tree gnu_base_index_type
2343 = get_unpadded_type (Etype (gnat_base_index));
2344 tree gnu_base_index_base_type
2345 = get_base_type (gnu_base_index_type);
2346 tree gnu_base_orig_min
2347 = convert (gnu_base_index_base_type,
2348 TYPE_MIN_VALUE (gnu_base_index_type));
2349 tree gnu_base_orig_max
2350 = convert (gnu_base_index_base_type,
2351 TYPE_MAX_VALUE (gnu_base_index_type));
2352 tree gnu_high;
2354 /* See if the base array type is already flat. If it is, we
2355 are probably compiling an ACATS test but it will cause the
2356 code below to malfunction if we don't handle it specially. */
2357 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2358 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2359 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2361 gnu_min = size_one_node;
2362 gnu_max = size_zero_node;
2363 gnu_high = gnu_max;
2366 /* Similarly, if one of the values overflows in sizetype and the
2367 range is null, use 1..0 for the sizetype bounds. */
2368 else if (TREE_CODE (gnu_min) == INTEGER_CST
2369 && TREE_CODE (gnu_max) == INTEGER_CST
2370 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2371 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2373 gnu_min = size_one_node;
2374 gnu_max = size_zero_node;
2375 gnu_high = gnu_max;
2378 /* If the minimum and maximum values both overflow in sizetype,
2379 but the difference in the original type does not overflow in
2380 sizetype, ignore the overflow indication. */
2381 else if (TREE_CODE (gnu_min) == INTEGER_CST
2382 && TREE_CODE (gnu_max) == INTEGER_CST
2383 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2384 && !TREE_OVERFLOW
2385 (convert (sizetype,
2386 fold_build2 (MINUS_EXPR, gnu_index_type,
2387 gnu_orig_max,
2388 gnu_orig_min))))
2390 TREE_OVERFLOW (gnu_min) = 0;
2391 TREE_OVERFLOW (gnu_max) = 0;
2392 gnu_high = gnu_max;
2395 /* Compute the size of this dimension in the general case. We
2396 need to provide GCC with an upper bound to use but have to
2397 deal with the "superflat" case. There are three ways to do
2398 this. If we can prove that the array can never be superflat,
2399 we can just use the high bound of the index type. */
2400 else if ((Nkind (gnat_index) == N_Range
2401 && cannot_be_superflat_p (gnat_index))
2402 /* Bit-Packed Array Impl. Types are never superflat. */
2403 || (Is_Packed_Array_Impl_Type (gnat_entity)
2404 && Is_Bit_Packed_Array
2405 (Original_Array_Type (gnat_entity))))
2406 gnu_high = gnu_max;
2408 /* Otherwise, if the high bound is constant but the low bound is
2409 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2410 lower bound. Note that the comparison must be done in the
2411 original type to avoid any overflow during the conversion. */
2412 else if (TREE_CODE (gnu_max) == INTEGER_CST
2413 && TREE_CODE (gnu_min) != INTEGER_CST)
2415 gnu_high = gnu_max;
2416 gnu_min
2417 = build_cond_expr (sizetype,
2418 build_binary_op (GE_EXPR,
2419 boolean_type_node,
2420 gnu_orig_max,
2421 gnu_orig_min),
2422 gnu_min,
2423 int_const_binop (PLUS_EXPR, gnu_max,
2424 size_one_node));
2427 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2428 in all the other cases. Note that, here as well as above,
2429 the condition used in the comparison must be equivalent to
2430 the condition (length != 0). This is relied upon in order
2431 to optimize array comparisons in compare_arrays. Moreover
2432 we use int_const_binop for the shift by 1 if the bound is
2433 constant to avoid any unwanted overflow. */
2434 else
2435 gnu_high
2436 = build_cond_expr (sizetype,
2437 build_binary_op (GE_EXPR,
2438 boolean_type_node,
2439 gnu_orig_max,
2440 gnu_orig_min),
2441 gnu_max,
2442 TREE_CODE (gnu_min) == INTEGER_CST
2443 ? int_const_binop (MINUS_EXPR, gnu_min,
2444 size_one_node)
2445 : size_binop (MINUS_EXPR, gnu_min,
2446 size_one_node));
2448 /* Reuse the index type for the range type. Then make an index
2449 type with the size range in sizetype. */
2450 gnu_index_types[index]
2451 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2452 gnat_entity);
2454 /* Update the maximum size of the array in elements. Here we
2455 see if any constraint on the index type of the base type
2456 can be used in the case of self-referential bound on the
2457 index type of the subtype. We look for a non-"infinite"
2458 and non-self-referential bound from any type involved and
2459 handle each bound separately. */
2460 if (gnu_max_size)
2462 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2463 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2464 tree gnu_base_index_base_type
2465 = get_base_type (gnu_base_index_type);
2466 tree gnu_base_base_min
2467 = convert (sizetype,
2468 TYPE_MIN_VALUE (gnu_base_index_base_type));
2469 tree gnu_base_base_max
2470 = convert (sizetype,
2471 TYPE_MAX_VALUE (gnu_base_index_base_type));
2473 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2474 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2475 && !TREE_OVERFLOW (gnu_base_min)))
2476 gnu_base_min = gnu_min;
2478 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2479 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2480 && !TREE_OVERFLOW (gnu_base_max)))
2481 gnu_base_max = gnu_max;
2483 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2484 && TREE_OVERFLOW (gnu_base_min))
2485 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2486 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2487 && TREE_OVERFLOW (gnu_base_max))
2488 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2489 gnu_max_size = NULL_TREE;
2490 else
2492 tree gnu_this_max;
2494 /* Use int_const_binop if the bounds are constant to
2495 avoid any unwanted overflow. */
2496 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2497 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2498 gnu_this_max
2499 = int_const_binop (PLUS_EXPR, size_one_node,
2500 int_const_binop (MINUS_EXPR,
2501 gnu_base_max,
2502 gnu_base_min));
2503 else
2504 gnu_this_max
2505 = size_binop (PLUS_EXPR, size_one_node,
2506 size_binop (MINUS_EXPR,
2507 gnu_base_max,
2508 gnu_base_min));
2510 gnu_max_size
2511 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2515 /* We need special types for debugging information to point to
2516 the index types if they have variable bounds, are not integer
2517 types or are biased. */
2518 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2519 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2520 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2521 || (TREE_TYPE (gnu_index_type)
2522 && TREE_CODE (TREE_TYPE (gnu_index_type))
2523 != INTEGER_TYPE)
2524 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2525 need_index_type_struct = true;
2528 /* Then flatten: create the array of arrays. For an array type
2529 used to implement a packed array, get the component type from
2530 the original array type since the representation clauses that
2531 can affect it are on the latter. */
2532 if (Is_Packed_Array_Impl_Type (gnat_entity)
2533 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2535 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2536 for (index = ndim - 1; index >= 0; index--)
2537 gnu_type = TREE_TYPE (gnu_type);
2539 /* One of the above calls might have caused us to be elaborated,
2540 so don't blow up if so. */
2541 if (present_gnu_tree (gnat_entity))
2543 maybe_present = true;
2544 break;
2547 else
2549 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2550 debug_info_p);
2552 /* One of the above calls might have caused us to be elaborated,
2553 so don't blow up if so. */
2554 if (present_gnu_tree (gnat_entity))
2556 maybe_present = true;
2557 break;
2561 /* Compute the maximum size of the array in units and bits. */
2562 if (gnu_max_size)
2564 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2565 TYPE_SIZE_UNIT (gnu_type));
2566 gnu_max_size = size_binop (MULT_EXPR,
2567 convert (bitsizetype, gnu_max_size),
2568 TYPE_SIZE (gnu_type));
2570 else
2571 gnu_max_size_unit = NULL_TREE;
2573 /* Now build the array type. */
2574 for (index = ndim - 1; index >= 0; index --)
2576 gnu_type = build_nonshared_array_type (gnu_type,
2577 gnu_index_types[index]);
2578 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2579 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2580 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2582 /* See the E_Array_Type case for the rationale. */
2583 if (TYPE_MODE (gnu_type) != BLKmode
2584 && Is_By_Reference_Type (gnat_entity))
2585 SET_TYPE_MODE (gnu_type, BLKmode);
2588 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2590 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2591 TYPE_STUB_DECL (gnu_type)
2592 = create_type_stub_decl (gnu_entity_name, gnu_type);
2594 /* If we are at file level and this is a multi-dimensional array,
2595 we need to make a variable corresponding to the stride of the
2596 inner dimensions. */
2597 if (global_bindings_p () && ndim > 1)
2599 tree gnu_st_name = get_identifier ("ST");
2600 tree gnu_arr_type;
2602 for (gnu_arr_type = TREE_TYPE (gnu_type);
2603 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2604 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2605 gnu_st_name = concat_name (gnu_st_name, "ST"))
2607 tree eltype = TREE_TYPE (gnu_arr_type);
2609 TYPE_SIZE (gnu_arr_type)
2610 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2611 gnat_entity, gnu_st_name,
2612 definition, false);
2614 /* ??? For now, store the size as a multiple of the
2615 alignment of the element type in bytes so that we
2616 can see the alignment from the tree. */
2617 TYPE_SIZE_UNIT (gnu_arr_type)
2618 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2619 gnat_entity,
2620 concat_name (gnu_st_name, "A_U"),
2621 definition, false,
2622 TYPE_ALIGN (eltype));
2624 /* ??? create_type_decl is not invoked on the inner types so
2625 the MULT_EXPR node built above will never be marked. */
2626 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2630 /* If we need to write out a record type giving the names of the
2631 bounds for debugging purposes, do it now and make the record
2632 type a parallel type. This is not needed for a packed array
2633 since the bounds are conveyed by the original array type. */
2634 if (need_index_type_struct
2635 && debug_info_p
2636 && !Is_Packed_Array_Impl_Type (gnat_entity))
2638 tree gnu_bound_rec = make_node (RECORD_TYPE);
2639 tree gnu_field_list = NULL_TREE;
2640 tree gnu_field;
2642 TYPE_NAME (gnu_bound_rec)
2643 = create_concat_name (gnat_entity, "XA");
2645 for (index = ndim - 1; index >= 0; index--)
2647 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2648 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2650 /* Make sure to reference the types themselves, and not just
2651 their names, as the debugger may fall back on them. */
2652 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2653 gnu_bound_rec, NULL_TREE,
2654 NULL_TREE, 0, 0);
2655 DECL_CHAIN (gnu_field) = gnu_field_list;
2656 gnu_field_list = gnu_field;
2659 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2660 add_parallel_type (gnu_type, gnu_bound_rec);
2663 /* If this is a packed array type, make the original array type a
2664 parallel type. Otherwise, do it for the base array type if it
2665 isn't artificial to make sure it is kept in the debug info. */
2666 if (debug_info_p)
2668 if (Is_Packed_Array_Impl_Type (gnat_entity))
2669 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2670 else
2672 tree gnu_base_decl
2673 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2674 if (!DECL_ARTIFICIAL (gnu_base_decl))
2675 add_parallel_type (gnu_type,
2676 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2680 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2681 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2682 = (Is_Packed_Array_Impl_Type (gnat_entity)
2683 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2685 /* If the size is self-referential and the maximum size doesn't
2686 overflow, use it. */
2687 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2688 && gnu_max_size
2689 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2690 && TREE_OVERFLOW (gnu_max_size))
2691 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2692 && TREE_OVERFLOW (gnu_max_size_unit)))
2694 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2695 TYPE_SIZE (gnu_type));
2696 TYPE_SIZE_UNIT (gnu_type)
2697 = size_binop (MIN_EXPR, gnu_max_size_unit,
2698 TYPE_SIZE_UNIT (gnu_type));
2701 /* Set our alias set to that of our base type. This gives all
2702 array subtypes the same alias set. */
2703 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2705 /* If this is a packed type, make this type the same as the packed
2706 array type, but do some adjusting in the type first. */
2707 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2709 Entity_Id gnat_index;
2710 tree gnu_inner;
2712 /* First finish the type we had been making so that we output
2713 debugging information for it. */
2714 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2715 if (Treat_As_Volatile (gnat_entity))
2716 gnu_type
2717 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2718 /* Make it artificial only if the base type was artificial too.
2719 That's sort of "morally" true and will make it possible for
2720 the debugger to look it up by name in DWARF, which is needed
2721 in order to decode the packed array type. */
2722 gnu_decl
2723 = create_type_decl (gnu_entity_name, gnu_type,
2724 !Comes_From_Source (Etype (gnat_entity))
2725 && !Comes_From_Source (gnat_entity),
2726 debug_info_p, gnat_entity);
2728 /* Save it as our equivalent in case the call below elaborates
2729 this type again. */
2730 save_gnu_tree (gnat_entity, gnu_decl, false);
2732 gnu_decl
2733 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2734 NULL_TREE, 0);
2735 this_made_decl = true;
2736 gnu_type = TREE_TYPE (gnu_decl);
2737 save_gnu_tree (gnat_entity, NULL_TREE, false);
2739 gnu_inner = gnu_type;
2740 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2741 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2742 || TYPE_PADDING_P (gnu_inner)))
2743 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2745 /* We need to attach the index type to the type we just made so
2746 that the actual bounds can later be put into a template. */
2747 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2748 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2749 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2750 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2752 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2754 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2755 TYPE_MODULUS for modular types so we make an extra
2756 subtype if necessary. */
2757 if (TYPE_MODULAR_P (gnu_inner))
2759 tree gnu_subtype
2760 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2761 TREE_TYPE (gnu_subtype) = gnu_inner;
2762 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2763 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2764 TYPE_MIN_VALUE (gnu_inner));
2765 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2766 TYPE_MAX_VALUE (gnu_inner));
2767 gnu_inner = gnu_subtype;
2770 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2772 #ifdef ENABLE_CHECKING
2773 /* Check for other cases of overloading. */
2774 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2775 #endif
2778 for (gnat_index = First_Index (gnat_entity);
2779 Present (gnat_index);
2780 gnat_index = Next_Index (gnat_index))
2781 SET_TYPE_ACTUAL_BOUNDS
2782 (gnu_inner,
2783 tree_cons (NULL_TREE,
2784 get_unpadded_type (Etype (gnat_index)),
2785 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2787 if (Convention (gnat_entity) != Convention_Fortran)
2788 SET_TYPE_ACTUAL_BOUNDS
2789 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2791 if (TREE_CODE (gnu_type) == RECORD_TYPE
2792 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2793 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2797 else
2798 /* Abort if packed array with no Packed_Array_Impl_Type. */
2799 gcc_assert (!Is_Packed (gnat_entity));
2801 break;
2803 case E_String_Literal_Subtype:
2804 /* Create the type for a string literal. */
2806 Entity_Id gnat_full_type
2807 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2808 && Present (Full_View (Etype (gnat_entity)))
2809 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2810 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2811 tree gnu_string_array_type
2812 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2813 tree gnu_string_index_type
2814 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2815 (TYPE_DOMAIN (gnu_string_array_type))));
2816 tree gnu_lower_bound
2817 = convert (gnu_string_index_type,
2818 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2819 tree gnu_length
2820 = UI_To_gnu (String_Literal_Length (gnat_entity),
2821 gnu_string_index_type);
2822 tree gnu_upper_bound
2823 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2824 gnu_lower_bound,
2825 int_const_binop (MINUS_EXPR, gnu_length,
2826 convert (gnu_string_index_type,
2827 integer_one_node)));
2828 tree gnu_index_type
2829 = create_index_type (convert (sizetype, gnu_lower_bound),
2830 convert (sizetype, gnu_upper_bound),
2831 create_range_type (gnu_string_index_type,
2832 gnu_lower_bound,
2833 gnu_upper_bound),
2834 gnat_entity);
2836 gnu_type
2837 = build_nonshared_array_type (gnat_to_gnu_type
2838 (Component_Type (gnat_entity)),
2839 gnu_index_type);
2840 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2841 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2842 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2844 break;
2846 /* Record Types and Subtypes
2848 The following fields are defined on record types:
2850 Has_Discriminants True if the record has discriminants
2851 First_Discriminant Points to head of list of discriminants
2852 First_Entity Points to head of list of fields
2853 Is_Tagged_Type True if the record is tagged
2855 Implementation of Ada records and discriminated records:
2857 A record type definition is transformed into the equivalent of a C
2858 struct definition. The fields that are the discriminants which are
2859 found in the Full_Type_Declaration node and the elements of the
2860 Component_List found in the Record_Type_Definition node. The
2861 Component_List can be a recursive structure since each Variant of
2862 the Variant_Part of the Component_List has a Component_List.
2864 Processing of a record type definition comprises starting the list of
2865 field declarations here from the discriminants and the calling the
2866 function components_to_record to add the rest of the fields from the
2867 component list and return the gnu type node. The function
2868 components_to_record will call itself recursively as it traverses
2869 the tree. */
2871 case E_Record_Type:
2872 if (Has_Complex_Representation (gnat_entity))
2874 gnu_type
2875 = build_complex_type
2876 (get_unpadded_type
2877 (Etype (Defining_Entity
2878 (First (Component_Items
2879 (Component_List
2880 (Type_Definition
2881 (Declaration_Node (gnat_entity)))))))));
2883 break;
2887 Node_Id full_definition = Declaration_Node (gnat_entity);
2888 Node_Id record_definition = Type_Definition (full_definition);
2889 Node_Id gnat_constr;
2890 Entity_Id gnat_field;
2891 tree gnu_field, gnu_field_list = NULL_TREE;
2892 tree gnu_get_parent;
2893 /* Set PACKED in keeping with gnat_to_gnu_field. */
2894 const int packed
2895 = Is_Packed (gnat_entity)
2897 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2898 ? -1
2899 : (Known_Alignment (gnat_entity)
2900 || (Strict_Alignment (gnat_entity)
2901 && Known_RM_Size (gnat_entity)))
2902 ? -2
2903 : 0;
2904 const bool has_discr = Has_Discriminants (gnat_entity);
2905 const bool has_rep = Has_Specified_Layout (gnat_entity);
2906 const bool is_extension
2907 = (Is_Tagged_Type (gnat_entity)
2908 && Nkind (record_definition) == N_Derived_Type_Definition);
2909 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2910 bool all_rep = has_rep;
2912 /* See if all fields have a rep clause. Stop when we find one
2913 that doesn't. */
2914 if (all_rep)
2915 for (gnat_field = First_Entity (gnat_entity);
2916 Present (gnat_field);
2917 gnat_field = Next_Entity (gnat_field))
2918 if ((Ekind (gnat_field) == E_Component
2919 || Ekind (gnat_field) == E_Discriminant)
2920 && No (Component_Clause (gnat_field)))
2922 all_rep = false;
2923 break;
2926 /* If this is a record extension, go a level further to find the
2927 record definition. Also, verify we have a Parent_Subtype. */
2928 if (is_extension)
2930 if (!type_annotate_only
2931 || Present (Record_Extension_Part (record_definition)))
2932 record_definition = Record_Extension_Part (record_definition);
2934 gcc_assert (type_annotate_only
2935 || Present (Parent_Subtype (gnat_entity)));
2938 /* Make a node for the record. If we are not defining the record,
2939 suppress expanding incomplete types. */
2940 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2941 TYPE_NAME (gnu_type) = gnu_entity_name;
2942 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2943 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2944 sorry ("non-default Scalar_Storage_Order");
2945 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2947 if (!definition)
2949 defer_incomplete_level++;
2950 this_deferred = true;
2953 /* If both a size and rep clause was specified, put the size in
2954 the record type now so that it can get the proper mode. */
2955 if (has_rep && Known_RM_Size (gnat_entity))
2956 TYPE_SIZE (gnu_type)
2957 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2959 /* Always set the alignment here so that it can be used to
2960 set the mode, if it is making the alignment stricter. If
2961 it is invalid, it will be checked again below. If this is to
2962 be Atomic, choose a default alignment of a word unless we know
2963 the size and it's smaller. */
2964 if (Known_Alignment (gnat_entity))
2965 TYPE_ALIGN (gnu_type)
2966 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2967 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2969 unsigned int size = UI_To_Int (Esize (gnat_entity));
2970 TYPE_ALIGN (gnu_type)
2971 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2973 /* If a type needs strict alignment, the minimum size will be the
2974 type size instead of the RM size (see validate_size). Cap the
2975 alignment, lest it causes this type size to become too large. */
2976 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2978 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2979 unsigned int raw_align = raw_size & -raw_size;
2980 if (raw_align < BIGGEST_ALIGNMENT)
2981 TYPE_ALIGN (gnu_type) = raw_align;
2983 else
2984 TYPE_ALIGN (gnu_type) = 0;
2986 /* If we have a Parent_Subtype, make a field for the parent. If
2987 this record has rep clauses, force the position to zero. */
2988 if (Present (Parent_Subtype (gnat_entity)))
2990 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2991 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2992 tree gnu_parent;
2994 /* A major complexity here is that the parent subtype will
2995 reference our discriminants in its Stored_Constraint list.
2996 But those must reference the parent component of this record
2997 which is precisely of the parent subtype we have not built yet!
2998 To break the circle we first build a dummy COMPONENT_REF which
2999 represents the "get to the parent" operation and initialize
3000 each of those discriminants to a COMPONENT_REF of the above
3001 dummy parent referencing the corresponding discriminant of the
3002 base type of the parent subtype. */
3003 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3004 build0 (PLACEHOLDER_EXPR, gnu_type),
3005 build_decl (input_location,
3006 FIELD_DECL, NULL_TREE,
3007 gnu_dummy_parent_type),
3008 NULL_TREE);
3010 if (has_discr)
3011 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3012 Present (gnat_field);
3013 gnat_field = Next_Stored_Discriminant (gnat_field))
3014 if (Present (Corresponding_Discriminant (gnat_field)))
3016 tree gnu_field
3017 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3018 (gnat_field));
3019 save_gnu_tree
3020 (gnat_field,
3021 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3022 gnu_get_parent, gnu_field, NULL_TREE),
3023 true);
3026 /* Then we build the parent subtype. If it has discriminants but
3027 the type itself has unknown discriminants, this means that it
3028 doesn't contain information about how the discriminants are
3029 derived from those of the ancestor type, so it cannot be used
3030 directly. Instead it is built by cloning the parent subtype
3031 of the underlying record view of the type, for which the above
3032 derivation of discriminants has been made explicit. */
3033 if (Has_Discriminants (gnat_parent)
3034 && Has_Unknown_Discriminants (gnat_entity))
3036 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3038 /* If we are defining the type, the underlying record
3039 view must already have been elaborated at this point.
3040 Otherwise do it now as its parent subtype cannot be
3041 technically elaborated on its own. */
3042 if (definition)
3043 gcc_assert (present_gnu_tree (gnat_uview));
3044 else
3045 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3047 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3049 /* Substitute the "get to the parent" of the type for that
3050 of its underlying record view in the cloned type. */
3051 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3052 Present (gnat_field);
3053 gnat_field = Next_Stored_Discriminant (gnat_field))
3054 if (Present (Corresponding_Discriminant (gnat_field)))
3056 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3057 tree gnu_ref
3058 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3059 gnu_get_parent, gnu_field, NULL_TREE);
3060 gnu_parent
3061 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3064 else
3065 gnu_parent = gnat_to_gnu_type (gnat_parent);
3067 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3068 initially built. The discriminants must reference the fields
3069 of the parent subtype and not those of its base type for the
3070 placeholder machinery to properly work. */
3071 if (has_discr)
3073 /* The actual parent subtype is the full view. */
3074 if (IN (Ekind (gnat_parent), Private_Kind))
3076 if (Present (Full_View (gnat_parent)))
3077 gnat_parent = Full_View (gnat_parent);
3078 else
3079 gnat_parent = Underlying_Full_View (gnat_parent);
3082 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3083 Present (gnat_field);
3084 gnat_field = Next_Stored_Discriminant (gnat_field))
3085 if (Present (Corresponding_Discriminant (gnat_field)))
3087 Entity_Id field;
3088 for (field = First_Stored_Discriminant (gnat_parent);
3089 Present (field);
3090 field = Next_Stored_Discriminant (field))
3091 if (same_discriminant_p (gnat_field, field))
3092 break;
3093 gcc_assert (Present (field));
3094 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3095 = gnat_to_gnu_field_decl (field);
3099 /* The "get to the parent" COMPONENT_REF must be given its
3100 proper type... */
3101 TREE_TYPE (gnu_get_parent) = gnu_parent;
3103 /* ...and reference the _Parent field of this record. */
3104 gnu_field
3105 = create_field_decl (parent_name_id,
3106 gnu_parent, gnu_type,
3107 has_rep
3108 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3109 has_rep
3110 ? bitsize_zero_node : NULL_TREE,
3111 0, 1);
3112 DECL_INTERNAL_P (gnu_field) = 1;
3113 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3114 TYPE_FIELDS (gnu_type) = gnu_field;
3117 /* Make the fields for the discriminants and put them into the record
3118 unless it's an Unchecked_Union. */
3119 if (has_discr)
3120 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3121 Present (gnat_field);
3122 gnat_field = Next_Stored_Discriminant (gnat_field))
3124 /* If this is a record extension and this discriminant is the
3125 renaming of another discriminant, we've handled it above. */
3126 if (Present (Parent_Subtype (gnat_entity))
3127 && Present (Corresponding_Discriminant (gnat_field)))
3128 continue;
3130 gnu_field
3131 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3132 debug_info_p);
3134 /* Make an expression using a PLACEHOLDER_EXPR from the
3135 FIELD_DECL node just created and link that with the
3136 corresponding GNAT defining identifier. */
3137 save_gnu_tree (gnat_field,
3138 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3139 build0 (PLACEHOLDER_EXPR, gnu_type),
3140 gnu_field, NULL_TREE),
3141 true);
3143 if (!is_unchecked_union)
3145 DECL_CHAIN (gnu_field) = gnu_field_list;
3146 gnu_field_list = gnu_field;
3150 /* If we have a derived untagged type that renames discriminants in
3151 the root type, the (stored) discriminants are a just copy of the
3152 discriminants of the root type. This means that any constraints
3153 added by the renaming in the derivation are disregarded as far
3154 as the layout of the derived type is concerned. To rescue them,
3155 we change the type of the (stored) discriminants to a subtype
3156 with the bounds of the type of the visible discriminants. */
3157 if (has_discr
3158 && !is_extension
3159 && Stored_Constraint (gnat_entity) != No_Elist)
3160 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3161 gnat_constr != No_Elmt;
3162 gnat_constr = Next_Elmt (gnat_constr))
3163 if (Nkind (Node (gnat_constr)) == N_Identifier
3164 /* Ignore access discriminants. */
3165 && !Is_Access_Type (Etype (Node (gnat_constr)))
3166 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3168 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3169 tree gnu_discr_type, gnu_ref;
3171 /* If the scope of the discriminant is not the record type,
3172 this means that we're processing the implicit full view
3173 of a type derived from a private discriminated type: in
3174 this case, the Stored_Constraint list is simply copied
3175 from the partial view, see Build_Derived_Private_Type.
3176 So we need to retrieve the corresponding discriminant
3177 of the implicit full view, otherwise we will abort. */
3178 if (Scope (gnat_discr) != gnat_entity)
3180 Entity_Id field;
3181 for (field = First_Entity (gnat_entity);
3182 Present (field);
3183 field = Next_Entity (field))
3184 if (Ekind (field) == E_Discriminant
3185 && same_discriminant_p (gnat_discr, field))
3186 break;
3187 gcc_assert (Present (field));
3188 gnat_discr = field;
3191 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3192 gnu_ref
3193 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3194 NULL_TREE, 0);
3196 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3197 just above for one of the stored discriminants. */
3198 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3200 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3202 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3203 tree gnu_subtype
3204 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3205 ? make_unsigned_type (prec) : make_signed_type (prec);
3206 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3207 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3208 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3209 TYPE_MIN_VALUE (gnu_discr_type));
3210 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3211 TYPE_MAX_VALUE (gnu_discr_type));
3212 TREE_TYPE (gnu_ref)
3213 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3217 /* Add the fields into the record type and finish it up. */
3218 components_to_record (gnu_type, Component_List (record_definition),
3219 gnu_field_list, packed, definition, false,
3220 all_rep, is_unchecked_union,
3221 !Comes_From_Source (gnat_entity), debug_info_p,
3222 false, OK_To_Reorder_Components (gnat_entity),
3223 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3225 /* If it is passed by reference, force BLKmode to ensure that objects
3226 of this type will always be put in memory. */
3227 if (TYPE_MODE (gnu_type) != BLKmode
3228 && Is_By_Reference_Type (gnat_entity))
3229 SET_TYPE_MODE (gnu_type, BLKmode);
3231 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3233 /* Fill in locations of fields. */
3234 annotate_rep (gnat_entity, gnu_type);
3236 /* If there are any entities in the chain corresponding to components
3237 that we did not elaborate, ensure we elaborate their types if they
3238 are Itypes. */
3239 for (gnat_temp = First_Entity (gnat_entity);
3240 Present (gnat_temp);
3241 gnat_temp = Next_Entity (gnat_temp))
3242 if ((Ekind (gnat_temp) == E_Component
3243 || Ekind (gnat_temp) == E_Discriminant)
3244 && Is_Itype (Etype (gnat_temp))
3245 && !present_gnu_tree (gnat_temp))
3246 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3248 /* If this is a record type associated with an exception definition,
3249 equate its fields to those of the standard exception type. This
3250 will make it possible to convert between them. */
3251 if (gnu_entity_name == exception_data_name_id)
3253 tree gnu_std_field;
3254 for (gnu_field = TYPE_FIELDS (gnu_type),
3255 gnu_std_field = TYPE_FIELDS (except_type_node);
3256 gnu_field;
3257 gnu_field = DECL_CHAIN (gnu_field),
3258 gnu_std_field = DECL_CHAIN (gnu_std_field))
3259 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3260 gcc_assert (!gnu_std_field);
3263 break;
3265 case E_Class_Wide_Subtype:
3266 /* If an equivalent type is present, that is what we should use.
3267 Otherwise, fall through to handle this like a record subtype
3268 since it may have constraints. */
3269 if (gnat_equiv_type != gnat_entity)
3271 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3272 maybe_present = true;
3273 break;
3276 /* ... fall through ... */
3278 case E_Record_Subtype:
3279 /* If Cloned_Subtype is Present it means this record subtype has
3280 identical layout to that type or subtype and we should use
3281 that GCC type for this one. The front end guarantees that
3282 the component list is shared. */
3283 if (Present (Cloned_Subtype (gnat_entity)))
3285 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3286 NULL_TREE, 0);
3287 maybe_present = true;
3288 break;
3291 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3292 changing the type, make a new type with each field having the type of
3293 the field in the new subtype but the position computed by transforming
3294 every discriminant reference according to the constraints. We don't
3295 see any difference between private and non-private type here since
3296 derivations from types should have been deferred until the completion
3297 of the private type. */
3298 else
3300 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3301 tree gnu_base_type;
3303 if (!definition)
3305 defer_incomplete_level++;
3306 this_deferred = true;
3309 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3311 if (present_gnu_tree (gnat_entity))
3313 maybe_present = true;
3314 break;
3317 /* If this is a record subtype associated with a dispatch table,
3318 strip the suffix. This is necessary to make sure 2 different
3319 subtypes associated with the imported and exported views of a
3320 dispatch table are properly merged in LTO mode. */
3321 if (Is_Dispatch_Table_Entity (gnat_entity))
3323 char *p;
3324 Get_Encoded_Name (gnat_entity);
3325 p = strchr (Name_Buffer, '_');
3326 gcc_assert (p);
3327 strcpy (p+2, "dtS");
3328 gnu_entity_name = get_identifier (Name_Buffer);
3331 /* When the subtype has discriminants and these discriminants affect
3332 the initial shape it has inherited, factor them in. But for an
3333 Unchecked_Union (it must be an Itype), just return the type.
3334 We can't just test Is_Constrained because private subtypes without
3335 discriminants of types with discriminants with default expressions
3336 are Is_Constrained but aren't constrained! */
3337 if (IN (Ekind (gnat_base_type), Record_Kind)
3338 && !Is_Unchecked_Union (gnat_base_type)
3339 && !Is_For_Access_Subtype (gnat_entity)
3340 && Has_Discriminants (gnat_entity)
3341 && Is_Constrained (gnat_entity)
3342 && Stored_Constraint (gnat_entity) != No_Elist)
3344 vec<subst_pair> gnu_subst_list
3345 = build_subst_list (gnat_entity, gnat_base_type, definition);
3346 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3347 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3348 bool selected_variant = false, all_constant_pos = true;
3349 Entity_Id gnat_field;
3350 vec<variant_desc> gnu_variant_list;
3352 gnu_type = make_node (RECORD_TYPE);
3353 TYPE_NAME (gnu_type) = gnu_entity_name;
3354 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3355 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3357 /* Set the size, alignment and alias set of the new type to
3358 match that of the old one, doing required substitutions. */
3359 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3360 gnu_subst_list);
3362 if (TYPE_IS_PADDING_P (gnu_base_type))
3363 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3364 else
3365 gnu_unpad_base_type = gnu_base_type;
3367 /* Look for REP and variant parts in the base type. */
3368 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3369 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3371 /* If there is a variant part, we must compute whether the
3372 constraints statically select a particular variant. If
3373 so, we simply drop the qualified union and flatten the
3374 list of fields. Otherwise we'll build a new qualified
3375 union for the variants that are still relevant. */
3376 if (gnu_variant_part)
3378 variant_desc *v;
3379 unsigned int i;
3381 gnu_variant_list
3382 = build_variant_list (TREE_TYPE (gnu_variant_part),
3383 gnu_subst_list,
3384 vNULL);
3386 /* If all the qualifiers are unconditionally true, the
3387 innermost variant is statically selected. */
3388 selected_variant = true;
3389 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3390 if (!integer_onep (v->qual))
3392 selected_variant = false;
3393 break;
3396 /* Otherwise, create the new variants. */
3397 if (!selected_variant)
3398 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3400 tree old_variant = v->type;
3401 tree new_variant = make_node (RECORD_TYPE);
3402 tree suffix
3403 = concat_name (DECL_NAME (gnu_variant_part),
3404 IDENTIFIER_POINTER
3405 (DECL_NAME (v->field)));
3406 TYPE_NAME (new_variant)
3407 = concat_name (TYPE_NAME (gnu_type),
3408 IDENTIFIER_POINTER (suffix));
3409 copy_and_substitute_in_size (new_variant, old_variant,
3410 gnu_subst_list);
3411 v->new_type = new_variant;
3414 else
3416 gnu_variant_list.create (0);
3417 selected_variant = false;
3420 /* Make a list of fields and their position in the base type. */
3421 gnu_pos_list
3422 = build_position_list (gnu_unpad_base_type,
3423 gnu_variant_list.exists ()
3424 && !selected_variant,
3425 size_zero_node, bitsize_zero_node,
3426 BIGGEST_ALIGNMENT, NULL_TREE);
3428 /* Now go down every component in the subtype and compute its
3429 size and position from those of the component in the base
3430 type and from the constraints of the subtype. */
3431 for (gnat_field = First_Entity (gnat_entity);
3432 Present (gnat_field);
3433 gnat_field = Next_Entity (gnat_field))
3434 if ((Ekind (gnat_field) == E_Component
3435 || Ekind (gnat_field) == E_Discriminant)
3436 && !(Present (Corresponding_Discriminant (gnat_field))
3437 && Is_Tagged_Type (gnat_base_type))
3438 && Underlying_Type
3439 (Scope (Original_Record_Component (gnat_field)))
3440 == gnat_base_type)
3442 Name_Id gnat_name = Chars (gnat_field);
3443 Entity_Id gnat_old_field
3444 = Original_Record_Component (gnat_field);
3445 tree gnu_old_field
3446 = gnat_to_gnu_field_decl (gnat_old_field);
3447 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3448 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3449 tree gnu_cont_type, gnu_last = NULL_TREE;
3451 /* If the type is the same, retrieve the GCC type from the
3452 old field to take into account possible adjustments. */
3453 if (Etype (gnat_field) == Etype (gnat_old_field))
3454 gnu_field_type = TREE_TYPE (gnu_old_field);
3455 else
3456 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3458 /* If there was a component clause, the field types must be
3459 the same for the type and subtype, so copy the data from
3460 the old field to avoid recomputation here. Also if the
3461 field is justified modular and the optimization in
3462 gnat_to_gnu_field was applied. */
3463 if (Present (Component_Clause (gnat_old_field))
3464 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3465 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3466 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3467 == TREE_TYPE (gnu_old_field)))
3469 gnu_size = DECL_SIZE (gnu_old_field);
3470 gnu_field_type = TREE_TYPE (gnu_old_field);
3473 /* If the old field was packed and of constant size, we
3474 have to get the old size here, as it might differ from
3475 what the Etype conveys and the latter might overlap
3476 onto the following field. Try to arrange the type for
3477 possible better packing along the way. */
3478 else if (DECL_PACKED (gnu_old_field)
3479 && TREE_CODE (DECL_SIZE (gnu_old_field))
3480 == INTEGER_CST)
3482 gnu_size = DECL_SIZE (gnu_old_field);
3483 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3484 && !TYPE_FAT_POINTER_P (gnu_field_type)
3485 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3486 gnu_field_type
3487 = make_packable_type (gnu_field_type, true);
3490 else
3491 gnu_size = TYPE_SIZE (gnu_field_type);
3493 /* If the context of the old field is the base type or its
3494 REP part (if any), put the field directly in the new
3495 type; otherwise look up the context in the variant list
3496 and put the field either in the new type if there is a
3497 selected variant or in one of the new variants. */
3498 if (gnu_context == gnu_unpad_base_type
3499 || (gnu_rep_part
3500 && gnu_context == TREE_TYPE (gnu_rep_part)))
3501 gnu_cont_type = gnu_type;
3502 else
3504 variant_desc *v;
3505 unsigned int i;
3506 tree rep_part;
3508 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3509 if (gnu_context == v->type
3510 || ((rep_part = get_rep_part (v->type))
3511 && gnu_context == TREE_TYPE (rep_part)))
3512 break;
3513 if (v)
3515 if (selected_variant)
3516 gnu_cont_type = gnu_type;
3517 else
3518 gnu_cont_type = v->new_type;
3520 else
3521 /* The front-end may pass us "ghost" components if
3522 it fails to recognize that a constrained subtype
3523 is statically constrained. Discard them. */
3524 continue;
3527 /* Now create the new field modeled on the old one. */
3528 gnu_field
3529 = create_field_decl_from (gnu_old_field, gnu_field_type,
3530 gnu_cont_type, gnu_size,
3531 gnu_pos_list, gnu_subst_list);
3532 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3534 /* Put it in one of the new variants directly. */
3535 if (gnu_cont_type != gnu_type)
3537 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3538 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3541 /* To match the layout crafted in components_to_record,
3542 if this is the _Tag or _Parent field, put it before
3543 any other fields. */
3544 else if (gnat_name == Name_uTag
3545 || gnat_name == Name_uParent)
3546 gnu_field_list = chainon (gnu_field_list, gnu_field);
3548 /* Similarly, if this is the _Controller field, put
3549 it before the other fields except for the _Tag or
3550 _Parent field. */
3551 else if (gnat_name == Name_uController && gnu_last)
3553 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3554 DECL_CHAIN (gnu_last) = gnu_field;
3557 /* Otherwise, if this is a regular field, put it after
3558 the other fields. */
3559 else
3561 DECL_CHAIN (gnu_field) = gnu_field_list;
3562 gnu_field_list = gnu_field;
3563 if (!gnu_last)
3564 gnu_last = gnu_field;
3565 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3566 all_constant_pos = false;
3569 save_gnu_tree (gnat_field, gnu_field, false);
3572 /* If there is a variant list, a selected variant and the fields
3573 all have a constant position, put them in order of increasing
3574 position to match that of constant CONSTRUCTORs. Likewise if
3575 there is no variant list but a REP part, since the latter has
3576 been flattened in the process. */
3577 if (((gnu_variant_list.exists () && selected_variant)
3578 || (!gnu_variant_list.exists () && gnu_rep_part))
3579 && all_constant_pos)
3581 const int len = list_length (gnu_field_list);
3582 tree *field_arr = XALLOCAVEC (tree, len), t;
3583 int i;
3585 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3586 field_arr[i] = t;
3588 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3590 gnu_field_list = NULL_TREE;
3591 for (i = 0; i < len; i++)
3593 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3594 gnu_field_list = field_arr[i];
3598 /* If there is a variant list and no selected variant, we need
3599 to create the nest of variant parts from the old nest. */
3600 else if (gnu_variant_list.exists () && !selected_variant)
3602 tree new_variant_part
3603 = create_variant_part_from (gnu_variant_part,
3604 gnu_variant_list, gnu_type,
3605 gnu_pos_list, gnu_subst_list);
3606 DECL_CHAIN (new_variant_part) = gnu_field_list;
3607 gnu_field_list = new_variant_part;
3610 /* Now go through the entities again looking for Itypes that
3611 we have not elaborated but should (e.g., Etypes of fields
3612 that have Original_Components). */
3613 for (gnat_field = First_Entity (gnat_entity);
3614 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3615 if ((Ekind (gnat_field) == E_Discriminant
3616 || Ekind (gnat_field) == E_Component)
3617 && !present_gnu_tree (Etype (gnat_field)))
3618 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3620 /* Do not emit debug info for the type yet since we're going to
3621 modify it below. */
3622 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3623 false);
3624 compute_record_mode (gnu_type);
3626 /* See the E_Record_Type case for the rationale. */
3627 if (TYPE_MODE (gnu_type) != BLKmode
3628 && Is_By_Reference_Type (gnat_entity))
3629 SET_TYPE_MODE (gnu_type, BLKmode);
3631 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3633 /* Fill in locations of fields. */
3634 annotate_rep (gnat_entity, gnu_type);
3636 /* If debugging information is being written for the type, write
3637 a record that shows what we are a subtype of and also make a
3638 variable that indicates our size, if still variable. */
3639 if (debug_info_p)
3641 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3642 tree gnu_unpad_base_name
3643 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3644 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3646 TYPE_NAME (gnu_subtype_marker)
3647 = create_concat_name (gnat_entity, "XVS");
3648 finish_record_type (gnu_subtype_marker,
3649 create_field_decl (gnu_unpad_base_name,
3650 build_reference_type
3651 (gnu_unpad_base_type),
3652 gnu_subtype_marker,
3653 NULL_TREE, NULL_TREE,
3654 0, 0),
3655 0, true);
3657 add_parallel_type (gnu_type, gnu_subtype_marker);
3659 if (definition
3660 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3661 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3662 TYPE_SIZE_UNIT (gnu_subtype_marker)
3663 = create_var_decl (create_concat_name (gnat_entity,
3664 "XVZ"),
3665 NULL_TREE, sizetype, gnu_size_unit,
3666 false, false, false, false, NULL,
3667 gnat_entity);
3670 gnu_variant_list.release ();
3671 gnu_subst_list.release ();
3673 /* Now we can finalize it. */
3674 rest_of_record_type_compilation (gnu_type);
3677 /* Otherwise, go down all the components in the new type and make
3678 them equivalent to those in the base type. */
3679 else
3681 gnu_type = gnu_base_type;
3683 for (gnat_temp = First_Entity (gnat_entity);
3684 Present (gnat_temp);
3685 gnat_temp = Next_Entity (gnat_temp))
3686 if ((Ekind (gnat_temp) == E_Discriminant
3687 && !Is_Unchecked_Union (gnat_base_type))
3688 || Ekind (gnat_temp) == E_Component)
3689 save_gnu_tree (gnat_temp,
3690 gnat_to_gnu_field_decl
3691 (Original_Record_Component (gnat_temp)),
3692 false);
3695 break;
3697 case E_Access_Subprogram_Type:
3698 /* Use the special descriptor type for dispatch tables if needed,
3699 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3700 Note that we are only required to do so for static tables in
3701 order to be compatible with the C++ ABI, but Ada 2005 allows
3702 to extend library level tagged types at the local level so
3703 we do it in the non-static case as well. */
3704 if (TARGET_VTABLE_USES_DESCRIPTORS
3705 && Is_Dispatch_Table_Entity (gnat_entity))
3707 gnu_type = fdesc_type_node;
3708 gnu_size = TYPE_SIZE (gnu_type);
3709 break;
3712 /* ... fall through ... */
3714 case E_Anonymous_Access_Subprogram_Type:
3715 /* If we are not defining this entity, and we have incomplete
3716 entities being processed above us, make a dummy type and
3717 fill it in later. */
3718 if (!definition && defer_incomplete_level != 0)
3720 struct incomplete *p = XNEW (struct incomplete);
3722 gnu_type
3723 = build_pointer_type
3724 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3725 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3726 !Comes_From_Source (gnat_entity),
3727 debug_info_p, gnat_entity);
3728 this_made_decl = true;
3729 gnu_type = TREE_TYPE (gnu_decl);
3730 save_gnu_tree (gnat_entity, gnu_decl, false);
3731 saved = true;
3733 p->old_type = TREE_TYPE (gnu_type);
3734 p->full_type = Directly_Designated_Type (gnat_entity);
3735 p->next = defer_incomplete_list;
3736 defer_incomplete_list = p;
3737 break;
3740 /* ... fall through ... */
3742 case E_Allocator_Type:
3743 case E_Access_Type:
3744 case E_Access_Attribute_Type:
3745 case E_Anonymous_Access_Type:
3746 case E_General_Access_Type:
3748 /* The designated type and its equivalent type for gigi. */
3749 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3750 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3751 /* Whether it comes from a limited with. */
3752 bool is_from_limited_with
3753 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3754 && From_Limited_With (gnat_desig_equiv));
3755 /* The "full view" of the designated type. If this is an incomplete
3756 entity from a limited with, treat its non-limited view as the full
3757 view. Otherwise, if this is an incomplete or private type, use the
3758 full view. In the former case, we might point to a private type,
3759 in which case, we need its full view. Also, we want to look at the
3760 actual type used for the representation, so this takes a total of
3761 three steps. */
3762 Entity_Id gnat_desig_full_direct_first
3763 = (is_from_limited_with
3764 ? Non_Limited_View (gnat_desig_equiv)
3765 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3766 ? Full_View (gnat_desig_equiv) : Empty));
3767 Entity_Id gnat_desig_full_direct
3768 = ((is_from_limited_with
3769 && Present (gnat_desig_full_direct_first)
3770 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3771 ? Full_View (gnat_desig_full_direct_first)
3772 : gnat_desig_full_direct_first);
3773 Entity_Id gnat_desig_full
3774 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3775 /* The type actually used to represent the designated type, either
3776 gnat_desig_full or gnat_desig_equiv. */
3777 Entity_Id gnat_desig_rep;
3778 /* True if this is a pointer to an unconstrained array. */
3779 bool is_unconstrained_array;
3780 /* We want to know if we'll be seeing the freeze node for any
3781 incomplete type we may be pointing to. */
3782 bool in_main_unit
3783 = (Present (gnat_desig_full)
3784 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3785 : In_Extended_Main_Code_Unit (gnat_desig_type));
3786 /* True if we make a dummy type here. */
3787 bool made_dummy = false;
3788 /* The mode to be used for the pointer type. */
3789 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3790 /* The GCC type used for the designated type. */
3791 tree gnu_desig_type = NULL_TREE;
3793 if (!targetm.valid_pointer_mode (p_mode))
3794 p_mode = ptr_mode;
3796 /* If either the designated type or its full view is an unconstrained
3797 array subtype, replace it with the type it's a subtype of. This
3798 avoids problems with multiple copies of unconstrained array types.
3799 Likewise, if the designated type is a subtype of an incomplete
3800 record type, use the parent type to avoid order of elaboration
3801 issues. This can lose some code efficiency, but there is no
3802 alternative. */
3803 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3804 && !Is_Constrained (gnat_desig_equiv))
3805 gnat_desig_equiv = Etype (gnat_desig_equiv);
3806 if (Present (gnat_desig_full)
3807 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3808 && !Is_Constrained (gnat_desig_full))
3809 || (Ekind (gnat_desig_full) == E_Record_Subtype
3810 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3811 gnat_desig_full = Etype (gnat_desig_full);
3813 /* Set the type that's actually the representation of the designated
3814 type and also flag whether we have a unconstrained array. */
3815 gnat_desig_rep
3816 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3817 is_unconstrained_array
3818 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3820 /* If we are pointing to an incomplete type whose completion is an
3821 unconstrained array, make dummy fat and thin pointer types to it.
3822 Likewise if the type itself is dummy or an unconstrained array. */
3823 if (is_unconstrained_array
3824 && (Present (gnat_desig_full)
3825 || (present_gnu_tree (gnat_desig_equiv)
3826 && TYPE_IS_DUMMY_P
3827 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3828 || (!in_main_unit
3829 && defer_incomplete_level != 0
3830 && !present_gnu_tree (gnat_desig_equiv))
3831 || (in_main_unit
3832 && is_from_limited_with
3833 && Present (Freeze_Node (gnat_desig_equiv)))))
3835 if (present_gnu_tree (gnat_desig_rep))
3836 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3837 else
3839 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3840 made_dummy = true;
3843 /* If the call above got something that has a pointer, the pointer
3844 is our type. This could have happened either because the type
3845 was elaborated or because somebody else executed the code. */
3846 if (!TYPE_POINTER_TO (gnu_desig_type))
3847 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3848 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3851 /* If we already know what the full type is, use it. */
3852 else if (Present (gnat_desig_full)
3853 && present_gnu_tree (gnat_desig_full))
3854 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3856 /* Get the type of the thing we are to point to and build a pointer to
3857 it. If it is a reference to an incomplete or private type with a
3858 full view that is a record, make a dummy type node and get the
3859 actual type later when we have verified it is safe. */
3860 else if ((!in_main_unit
3861 && !present_gnu_tree (gnat_desig_equiv)
3862 && Present (gnat_desig_full)
3863 && !present_gnu_tree (gnat_desig_full)
3864 && Is_Record_Type (gnat_desig_full))
3865 /* Likewise if we are pointing to a record or array and we are
3866 to defer elaborating incomplete types. We do this as this
3867 access type may be the full view of a private type. Note
3868 that the unconstrained array case is handled above. */
3869 || ((!in_main_unit || imported_p)
3870 && defer_incomplete_level != 0
3871 && !present_gnu_tree (gnat_desig_equiv)
3872 && (Is_Record_Type (gnat_desig_rep)
3873 || Is_Array_Type (gnat_desig_rep)))
3874 /* If this is a reference from a limited_with type back to our
3875 main unit and there's a freeze node for it, either we have
3876 already processed the declaration and made the dummy type,
3877 in which case we just reuse the latter, or we have not yet,
3878 in which case we make the dummy type and it will be reused
3879 when the declaration is finally processed. In both cases,
3880 the pointer eventually created below will be automatically
3881 adjusted when the freeze node is processed. Note that the
3882 unconstrained array case is handled above. */
3883 || (in_main_unit
3884 && is_from_limited_with
3885 && Present (Freeze_Node (gnat_desig_rep))))
3887 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3888 made_dummy = true;
3891 /* Otherwise handle the case of a pointer to itself. */
3892 else if (gnat_desig_equiv == gnat_entity)
3894 gnu_type
3895 = build_pointer_type_for_mode (void_type_node, p_mode,
3896 No_Strict_Aliasing (gnat_entity));
3897 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3900 /* If expansion is disabled, the equivalent type of a concurrent type
3901 is absent, so build a dummy pointer type. */
3902 else if (type_annotate_only && No (gnat_desig_equiv))
3903 gnu_type = ptr_void_type_node;
3905 /* Finally, handle the default case where we can just elaborate our
3906 designated type. */
3907 else
3908 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3910 /* It is possible that a call to gnat_to_gnu_type above resolved our
3911 type. If so, just return it. */
3912 if (present_gnu_tree (gnat_entity))
3914 maybe_present = true;
3915 break;
3918 /* If we haven't done it yet, build the pointer type the usual way. */
3919 if (!gnu_type)
3921 /* Modify the designated type if we are pointing only to constant
3922 objects, but don't do it for unconstrained arrays. */
3923 if (Is_Access_Constant (gnat_entity)
3924 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3926 gnu_desig_type
3927 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3929 /* Some extra processing is required if we are building a
3930 pointer to an incomplete type (in the GCC sense). We might
3931 have such a type if we just made a dummy, or directly out
3932 of the call to gnat_to_gnu_type above if we are processing
3933 an access type for a record component designating the
3934 record type itself. */
3935 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3937 /* We must ensure that the pointer to variant we make will
3938 be processed by update_pointer_to when the initial type
3939 is completed. Pretend we made a dummy and let further
3940 processing act as usual. */
3941 made_dummy = true;
3943 /* We must ensure that update_pointer_to will not retrieve
3944 the dummy variant when building a properly qualified
3945 version of the complete type. We take advantage of the
3946 fact that get_qualified_type is requiring TYPE_NAMEs to
3947 match to influence build_qualified_type and then also
3948 update_pointer_to here. */
3949 TYPE_NAME (gnu_desig_type)
3950 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3954 gnu_type
3955 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3956 No_Strict_Aliasing (gnat_entity));
3959 /* If we are not defining this object and we have made a dummy pointer,
3960 save our current definition, evaluate the actual type, and replace
3961 the tentative type we made with the actual one. If we are to defer
3962 actually looking up the actual type, make an entry in the deferred
3963 list. If this is from a limited with, we may have to defer to the
3964 end of the current unit. */
3965 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3967 tree gnu_old_desig_type;
3969 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3971 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3972 if (esize == POINTER_SIZE)
3973 gnu_type = build_pointer_type
3974 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3976 else
3977 gnu_old_desig_type = TREE_TYPE (gnu_type);
3979 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3980 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3981 !Comes_From_Source (gnat_entity),
3982 debug_info_p, gnat_entity);
3983 this_made_decl = true;
3984 gnu_type = TREE_TYPE (gnu_decl);
3985 save_gnu_tree (gnat_entity, gnu_decl, false);
3986 saved = true;
3988 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3989 update gnu_old_desig_type directly, in which case it will not be
3990 a dummy type any more when we get into update_pointer_to.
3992 This can happen e.g. when the designated type is a record type,
3993 because their elaboration starts with an initial node from
3994 make_dummy_type, which may be the same node as the one we got.
3996 Besides, variants of this non-dummy type might have been created
3997 along the way. update_pointer_to is expected to properly take
3998 care of those situations. */
3999 if (defer_incomplete_level == 0 && !is_from_limited_with)
4001 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4002 gnat_to_gnu_type (gnat_desig_equiv));
4004 else
4006 struct incomplete *p = XNEW (struct incomplete);
4007 struct incomplete **head
4008 = (is_from_limited_with
4009 ? &defer_limited_with : &defer_incomplete_list);
4010 p->old_type = gnu_old_desig_type;
4011 p->full_type = gnat_desig_equiv;
4012 p->next = *head;
4013 *head = p;
4017 break;
4019 case E_Access_Protected_Subprogram_Type:
4020 case E_Anonymous_Access_Protected_Subprogram_Type:
4021 if (type_annotate_only && No (gnat_equiv_type))
4022 gnu_type = ptr_void_type_node;
4023 else
4025 /* The run-time representation is the equivalent type. */
4026 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4027 maybe_present = true;
4030 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4031 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4032 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4033 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4034 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4035 NULL_TREE, 0);
4037 break;
4039 case E_Access_Subtype:
4041 /* We treat this as identical to its base type; any constraint is
4042 meaningful only to the front-end.
4044 The designated type must be elaborated as well, if it does
4045 not have its own freeze node. Designated (sub)types created
4046 for constrained components of records with discriminants are
4047 not frozen by the front-end and thus not elaborated by gigi,
4048 because their use may appear before the base type is frozen,
4049 and because it is not clear that they are needed anywhere in
4050 gigi. With the current model, there is no correct place where
4051 they could be elaborated. */
4053 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4054 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4055 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4056 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4057 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4059 /* If we are not defining this entity, and we have incomplete
4060 entities being processed above us, make a dummy type and
4061 elaborate it later. */
4062 if (!definition && defer_incomplete_level != 0)
4064 struct incomplete *p = XNEW (struct incomplete);
4066 p->old_type
4067 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4068 p->full_type = Directly_Designated_Type (gnat_entity);
4069 p->next = defer_incomplete_list;
4070 defer_incomplete_list = p;
4072 else if (!IN (Ekind (Base_Type
4073 (Directly_Designated_Type (gnat_entity))),
4074 Incomplete_Or_Private_Kind))
4075 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4076 NULL_TREE, 0);
4079 maybe_present = true;
4080 break;
4082 /* Subprogram Entities
4084 The following access functions are defined for subprograms:
4086 Etype Return type or Standard_Void_Type.
4087 First_Formal The first formal parameter.
4088 Is_Imported Indicates that the subprogram has appeared in
4089 an INTERFACE or IMPORT pragma. For now we
4090 assume that the external language is C.
4091 Is_Exported Likewise but for an EXPORT pragma.
4092 Is_Inlined True if the subprogram is to be inlined.
4094 Each parameter is first checked by calling must_pass_by_ref on its
4095 type to determine if it is passed by reference. For parameters which
4096 are copied in, if they are Ada In Out or Out parameters, their return
4097 value becomes part of a record which becomes the return type of the
4098 function (C function - note that this applies only to Ada procedures
4099 so there is no Ada return type). Additional code to store back the
4100 parameters will be generated on the caller side. This transformation
4101 is done here, not in the front-end.
4103 The intended result of the transformation can be seen from the
4104 equivalent source rewritings that follow:
4106 struct temp {int a,b};
4107 procedure P (A,B: In Out ...) is temp P (int A,B)
4108 begin {
4109 .. ..
4110 end P; return {A,B};
4113 temp t;
4114 P(X,Y); t = P(X,Y);
4115 X = t.a , Y = t.b;
4117 For subprogram types we need to perform mainly the same conversions to
4118 GCC form that are needed for procedures and function declarations. The
4119 only difference is that at the end, we make a type declaration instead
4120 of a function declaration. */
4122 case E_Subprogram_Type:
4123 case E_Function:
4124 case E_Procedure:
4126 /* The type returned by a function or else Standard_Void_Type for a
4127 procedure. */
4128 Entity_Id gnat_return_type = Etype (gnat_entity);
4129 tree gnu_return_type;
4130 /* The first GCC parameter declaration (a PARM_DECL node). The
4131 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4132 actually is the head of this parameter list. */
4133 tree gnu_param_list = NULL_TREE;
4134 /* Non-null for subprograms containing parameters passed by copy-in
4135 copy-out (Ada In Out or Out parameters not passed by reference),
4136 in which case it is the list of nodes used to specify the values
4137 of the In Out/Out parameters that are returned as a record upon
4138 procedure return. The TREE_PURPOSE of an element of this list is
4139 a field of the record and the TREE_VALUE is the PARM_DECL
4140 corresponding to that field. This list will be saved in the
4141 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4142 tree gnu_cico_list = NULL_TREE;
4143 /* List of fields in return type of procedure with copy-in copy-out
4144 parameters. */
4145 tree gnu_field_list = NULL_TREE;
4146 /* If an import pragma asks to map this subprogram to a GCC builtin,
4147 this is the builtin DECL node. */
4148 tree gnu_builtin_decl = NULL_TREE;
4149 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4150 Entity_Id gnat_param;
4151 enum inline_status_t inline_status
4152 = Has_Pragma_No_Inline (gnat_entity)
4153 ? is_suppressed
4154 : Has_Pragma_Inline_Always (gnat_entity)
4155 ? is_required
4156 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4157 bool public_flag = Is_Public (gnat_entity) || imported_p;
4158 bool extern_flag
4159 = (Is_Public (gnat_entity) && !definition) || imported_p;
4160 bool artificial_flag = !Comes_From_Source (gnat_entity);
4161 /* The semantics of "pure" in Ada essentially matches that of "const"
4162 in the back-end. In particular, both properties are orthogonal to
4163 the "nothrow" property if the EH circuitry is explicit in the
4164 internal representation of the back-end. If we are to completely
4165 hide the EH circuitry from it, we need to declare that calls to pure
4166 Ada subprograms that can throw have side effects since they can
4167 trigger an "abnormal" transfer of control flow; thus they can be
4168 neither "const" nor "pure" in the back-end sense. */
4169 bool const_flag
4170 = (Exception_Mechanism == Back_End_Exceptions
4171 && Is_Pure (gnat_entity));
4172 bool volatile_flag = No_Return (gnat_entity);
4173 bool return_by_direct_ref_p = false;
4174 bool return_by_invisi_ref_p = false;
4175 bool return_unconstrained_p = false;
4176 int parmnum;
4178 /* A parameter may refer to this type, so defer completion of any
4179 incomplete types. */
4180 if (kind == E_Subprogram_Type && !definition)
4182 defer_incomplete_level++;
4183 this_deferred = true;
4186 /* If the subprogram has an alias, it is probably inherited, so
4187 we can use the original one. If the original "subprogram"
4188 is actually an enumeration literal, it may be the first use
4189 of its type, so we must elaborate that type now. */
4190 if (Present (Alias (gnat_entity)))
4192 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4193 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4195 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4197 /* Elaborate any Itypes in the parameters of this entity. */
4198 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4199 Present (gnat_temp);
4200 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4201 if (Is_Itype (Etype (gnat_temp)))
4202 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4204 break;
4207 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4208 corresponding DECL node. Proper generation of calls later on need
4209 proper parameter associations so we don't "break;" here. */
4210 if (Convention (gnat_entity) == Convention_Intrinsic
4211 && Present (Interface_Name (gnat_entity)))
4213 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4215 /* Inability to find the builtin decl most often indicates a
4216 genuine mistake, but imports of unregistered intrinsics are
4217 sometimes issued on purpose to allow hooking in alternate
4218 bodies. We post a warning conditioned on Wshadow in this case,
4219 to let developers be notified on demand without risking false
4220 positives with common default sets of options. */
4222 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4223 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4226 /* ??? What if we don't find the builtin node above ? warn ? err ?
4227 In the current state we neither warn nor err, and calls will just
4228 be handled as for regular subprograms. */
4230 /* Look into the return type and get its associated GCC tree. If it
4231 is not void, compute various flags for the subprogram type. */
4232 if (Ekind (gnat_return_type) == E_Void)
4233 gnu_return_type = void_type_node;
4234 else
4236 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4237 context may now appear in parameter and result profiles. If
4238 we are only annotating types, break circularities here. */
4239 if (type_annotate_only
4240 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4241 && From_Limited_With (gnat_return_type)
4242 && In_Extended_Main_Code_Unit
4243 (Non_Limited_View (gnat_return_type))
4244 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4245 gnu_return_type = ptr_void_type_node;
4246 else
4247 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4249 /* If this function returns by reference, make the actual return
4250 type the pointer type and make a note of that. */
4251 if (Returns_By_Ref (gnat_entity))
4253 gnu_return_type = build_pointer_type (gnu_return_type);
4254 return_by_direct_ref_p = true;
4257 /* If we are supposed to return an unconstrained array type, make
4258 the actual return type the fat pointer type. */
4259 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4261 gnu_return_type = TREE_TYPE (gnu_return_type);
4262 return_unconstrained_p = true;
4265 /* Likewise, if the return type requires a transient scope, the
4266 return value will be allocated on the secondary stack so the
4267 actual return type is the pointer type. */
4268 else if (Requires_Transient_Scope (gnat_return_type))
4270 gnu_return_type = build_pointer_type (gnu_return_type);
4271 return_unconstrained_p = true;
4274 /* If the Mechanism is By_Reference, ensure this function uses the
4275 target's by-invisible-reference mechanism, which may not be the
4276 same as above (e.g. it might be passing an extra parameter). */
4277 else if (kind == E_Function
4278 && Mechanism (gnat_entity) == By_Reference)
4279 return_by_invisi_ref_p = true;
4281 /* Likewise, if the return type is itself By_Reference. */
4282 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4283 return_by_invisi_ref_p = true;
4285 /* If the type is a padded type and the underlying type would not
4286 be passed by reference or the function has a foreign convention,
4287 return the underlying type. */
4288 else if (TYPE_IS_PADDING_P (gnu_return_type)
4289 && (!default_pass_by_ref
4290 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4291 || Has_Foreign_Convention (gnat_entity)))
4292 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4294 /* If the return type is unconstrained, that means it must have a
4295 maximum size. Use the padded type as the effective return type.
4296 And ensure the function uses the target's by-invisible-reference
4297 mechanism to avoid copying too much data when it returns. */
4298 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4300 tree orig_type = gnu_return_type;
4302 gnu_return_type
4303 = maybe_pad_type (gnu_return_type,
4304 max_size (TYPE_SIZE (gnu_return_type),
4305 true),
4306 0, gnat_entity, false, false, false, true);
4308 /* Declare it now since it will never be declared otherwise.
4309 This is necessary to ensure that its subtrees are properly
4310 marked. */
4311 if (gnu_return_type != orig_type
4312 && !DECL_P (TYPE_NAME (gnu_return_type)))
4313 create_type_decl (TYPE_NAME (gnu_return_type),
4314 gnu_return_type, true, debug_info_p,
4315 gnat_entity);
4317 return_by_invisi_ref_p = true;
4320 /* If the return type has a size that overflows, we cannot have
4321 a function that returns that type. This usage doesn't make
4322 sense anyway, so give an error here. */
4323 if (TYPE_SIZE_UNIT (gnu_return_type)
4324 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4325 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4327 post_error ("cannot return type whose size overflows",
4328 gnat_entity);
4329 gnu_return_type = copy_node (gnu_return_type);
4330 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4331 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4332 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4333 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4337 /* Loop over the parameters and get their associated GCC tree. While
4338 doing this, build a copy-in copy-out structure if we need one. */
4339 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4340 Present (gnat_param);
4341 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4343 Entity_Id gnat_param_type = Etype (gnat_param);
4344 tree gnu_param_name = get_entity_name (gnat_param);
4345 tree gnu_param_type, gnu_param, gnu_field;
4346 Mechanism_Type mech = Mechanism (gnat_param);
4347 bool copy_in_copy_out = false, fake_param_type;
4349 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4350 context may now appear in parameter and result profiles. If
4351 we are only annotating types, break circularities here. */
4352 if (type_annotate_only
4353 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4354 && From_Limited_With (Etype (gnat_param_type))
4355 && In_Extended_Main_Code_Unit
4356 (Non_Limited_View (gnat_param_type))
4357 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4359 gnu_param_type = ptr_void_type_node;
4360 fake_param_type = true;
4362 else
4364 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4365 fake_param_type = false;
4368 /* Builtins are expanded inline and there is no real call sequence
4369 involved. So the type expected by the underlying expander is
4370 always the type of each argument "as is". */
4371 if (gnu_builtin_decl)
4372 mech = By_Copy;
4373 /* Handle the first parameter of a valued procedure specially. */
4374 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4375 mech = By_Copy_Return;
4376 /* Otherwise, see if a Mechanism was supplied that forced this
4377 parameter to be passed one way or another. */
4378 else if (mech == Default
4379 || mech == By_Copy
4380 || mech == By_Reference)
4382 else if (mech > 0)
4384 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4385 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4386 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4387 mech))
4388 mech = By_Reference;
4389 else
4390 mech = By_Copy;
4392 else
4394 post_error ("unsupported mechanism for&", gnat_param);
4395 mech = Default;
4398 /* Do not call gnat_to_gnu_param for a fake parameter type since
4399 it will try to use the real type again. */
4400 if (fake_param_type)
4402 if (Ekind (gnat_param) == E_Out_Parameter)
4403 gnu_param = NULL_TREE;
4404 else
4406 gnu_param
4407 = create_param_decl (gnu_param_name, gnu_param_type,
4408 false);
4409 Set_Mechanism (gnat_param,
4410 mech == Default ? By_Copy : mech);
4411 if (Ekind (gnat_param) == E_In_Out_Parameter)
4412 copy_in_copy_out = true;
4415 else
4416 gnu_param
4417 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4418 Has_Foreign_Convention (gnat_entity),
4419 &copy_in_copy_out);
4421 /* We are returned either a PARM_DECL or a type if no parameter
4422 needs to be passed; in either case, adjust the type. */
4423 if (DECL_P (gnu_param))
4424 gnu_param_type = TREE_TYPE (gnu_param);
4425 else
4427 gnu_param_type = gnu_param;
4428 gnu_param = NULL_TREE;
4431 /* The failure of this assertion will very likely come from an
4432 order of elaboration issue for the type of the parameter. */
4433 gcc_assert (kind == E_Subprogram_Type
4434 || !TYPE_IS_DUMMY_P (gnu_param_type)
4435 || type_annotate_only);
4437 if (gnu_param)
4439 gnu_param_list = chainon (gnu_param, gnu_param_list);
4440 Sloc_to_locus (Sloc (gnat_param),
4441 &DECL_SOURCE_LOCATION (gnu_param));
4442 save_gnu_tree (gnat_param, gnu_param, false);
4444 /* If a parameter is a pointer, this function may modify
4445 memory through it and thus shouldn't be considered
4446 a const function. Also, the memory may be modified
4447 between two calls, so they can't be CSE'ed. The latter
4448 case also handles by-ref parameters. */
4449 if (POINTER_TYPE_P (gnu_param_type)
4450 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4451 const_flag = false;
4454 if (copy_in_copy_out)
4456 if (!gnu_cico_list)
4458 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4460 /* If this is a function, we also need a field for the
4461 return value to be placed. */
4462 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4464 gnu_field
4465 = create_field_decl (get_identifier ("RETVAL"),
4466 gnu_return_type,
4467 gnu_new_ret_type, NULL_TREE,
4468 NULL_TREE, 0, 0);
4469 Sloc_to_locus (Sloc (gnat_entity),
4470 &DECL_SOURCE_LOCATION (gnu_field));
4471 gnu_field_list = gnu_field;
4472 gnu_cico_list
4473 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4476 gnu_return_type = gnu_new_ret_type;
4477 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4478 /* Set a default alignment to speed up accesses. But we
4479 shouldn't increase the size of the structure too much,
4480 lest it doesn't fit in return registers anymore. */
4481 TYPE_ALIGN (gnu_return_type)
4482 = get_mode_alignment (ptr_mode);
4485 gnu_field
4486 = create_field_decl (gnu_param_name, gnu_param_type,
4487 gnu_return_type, NULL_TREE, NULL_TREE,
4488 0, 0);
4489 Sloc_to_locus (Sloc (gnat_param),
4490 &DECL_SOURCE_LOCATION (gnu_field));
4491 DECL_CHAIN (gnu_field) = gnu_field_list;
4492 gnu_field_list = gnu_field;
4493 gnu_cico_list
4494 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4498 if (gnu_cico_list)
4500 /* If we have a CICO list but it has only one entry, we convert
4501 this function into a function that returns this object. */
4502 if (list_length (gnu_cico_list) == 1)
4503 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4505 /* Do not finalize the return type if the subprogram is stubbed
4506 since structures are incomplete for the back-end. */
4507 else if (Convention (gnat_entity) != Convention_Stubbed)
4509 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4510 0, false);
4512 /* Try to promote the mode of the return type if it is passed
4513 in registers, again to speed up accesses. */
4514 if (TYPE_MODE (gnu_return_type) == BLKmode
4515 && !targetm.calls.return_in_memory (gnu_return_type,
4516 NULL_TREE))
4518 unsigned int size
4519 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4520 unsigned int i = BITS_PER_UNIT;
4521 machine_mode mode;
4523 while (i < size)
4524 i <<= 1;
4525 mode = mode_for_size (i, MODE_INT, 0);
4526 if (mode != BLKmode)
4528 SET_TYPE_MODE (gnu_return_type, mode);
4529 TYPE_ALIGN (gnu_return_type)
4530 = GET_MODE_ALIGNMENT (mode);
4531 TYPE_SIZE (gnu_return_type)
4532 = bitsize_int (GET_MODE_BITSIZE (mode));
4533 TYPE_SIZE_UNIT (gnu_return_type)
4534 = size_int (GET_MODE_SIZE (mode));
4538 if (debug_info_p)
4539 rest_of_record_type_compilation (gnu_return_type);
4543 /* Deal with platform-specific calling conventions. */
4544 if (Has_Stdcall_Convention (gnat_entity))
4545 prepend_one_attribute
4546 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4547 get_identifier ("stdcall"), NULL_TREE,
4548 gnat_entity);
4549 else if (Has_Thiscall_Convention (gnat_entity))
4550 prepend_one_attribute
4551 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4552 get_identifier ("thiscall"), NULL_TREE,
4553 gnat_entity);
4555 /* If we should request stack realignment for a foreign convention
4556 subprogram, do so. Note that this applies to task entry points
4557 in particular. */
4558 if (FOREIGN_FORCE_REALIGN_STACK
4559 && Has_Foreign_Convention (gnat_entity))
4560 prepend_one_attribute
4561 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4562 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4563 gnat_entity);
4565 /* Deal with a pragma Linker_Section on a subprogram. */
4566 if ((kind == E_Function || kind == E_Procedure)
4567 && Present (Linker_Section_Pragma (gnat_entity)))
4568 prepend_one_attribute_pragma (&attr_list,
4569 Linker_Section_Pragma (gnat_entity));
4571 /* The lists have been built in reverse. */
4572 gnu_param_list = nreverse (gnu_param_list);
4573 gnu_cico_list = nreverse (gnu_cico_list);
4575 if (kind == E_Function)
4576 Set_Mechanism (gnat_entity, return_unconstrained_p
4577 || return_by_direct_ref_p
4578 || return_by_invisi_ref_p
4579 ? By_Reference : By_Copy);
4580 gnu_type
4581 = create_subprog_type (gnu_return_type, gnu_param_list,
4582 gnu_cico_list, return_unconstrained_p,
4583 return_by_direct_ref_p,
4584 return_by_invisi_ref_p);
4586 /* A subprogram (something that doesn't return anything) shouldn't
4587 be considered const since there would be no reason for such a
4588 subprogram. Note that procedures with Out (or In Out) parameters
4589 have already been converted into a function with a return type. */
4590 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4591 const_flag = false;
4593 if (const_flag || volatile_flag)
4595 const int quals
4596 = (const_flag ? TYPE_QUAL_CONST : 0)
4597 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4599 gnu_type = change_qualified_type (gnu_type, quals);
4602 /* If we have a builtin decl for that function, use it. Check if the
4603 profiles are compatible and warn if they are not. The checker is
4604 expected to post extra diagnostics in this case. */
4605 if (gnu_builtin_decl)
4607 intrin_binding_t inb;
4609 inb.gnat_entity = gnat_entity;
4610 inb.ada_fntype = gnu_type;
4611 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4613 if (!intrin_profiles_compatible_p (&inb))
4614 post_error
4615 ("?profile of& doesn''t match the builtin it binds!",
4616 gnat_entity);
4618 gnu_decl = gnu_builtin_decl;
4619 gnu_type = TREE_TYPE (gnu_builtin_decl);
4620 break;
4623 /* If there was no specified Interface_Name and the external and
4624 internal names of the subprogram are the same, only use the
4625 internal name to allow disambiguation of nested subprograms. */
4626 if (No (Interface_Name (gnat_entity))
4627 && gnu_ext_name == gnu_entity_name)
4628 gnu_ext_name = NULL_TREE;
4630 /* If we are defining the subprogram and it has an Address clause
4631 we must get the address expression from the saved GCC tree for the
4632 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4633 the address expression here since the front-end has guaranteed
4634 in that case that the elaboration has no effects. If there is
4635 an Address clause and we are not defining the object, just
4636 make it a constant. */
4637 if (Present (Address_Clause (gnat_entity)))
4639 tree gnu_address = NULL_TREE;
4641 if (definition)
4642 gnu_address
4643 = (present_gnu_tree (gnat_entity)
4644 ? get_gnu_tree (gnat_entity)
4645 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4647 save_gnu_tree (gnat_entity, NULL_TREE, false);
4649 /* Convert the type of the object to a reference type that can
4650 alias everything as per 13.3(19). */
4651 gnu_type
4652 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4653 if (gnu_address)
4654 gnu_address = convert (gnu_type, gnu_address);
4656 gnu_decl
4657 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4658 gnu_address, false, Is_Public (gnat_entity),
4659 extern_flag, false, NULL, gnat_entity);
4660 DECL_BY_REF_P (gnu_decl) = 1;
4663 else if (kind == E_Subprogram_Type)
4665 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4666 gnu_decl
4667 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4668 debug_info_p, gnat_entity);
4670 else
4672 gnu_decl
4673 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4674 gnu_param_list, inline_status,
4675 public_flag, extern_flag, artificial_flag,
4676 attr_list, gnat_entity);
4677 /* This is unrelated to the stub built right above. */
4678 DECL_STUBBED_P (gnu_decl)
4679 = Convention (gnat_entity) == Convention_Stubbed;
4682 break;
4684 case E_Incomplete_Type:
4685 case E_Incomplete_Subtype:
4686 case E_Private_Type:
4687 case E_Private_Subtype:
4688 case E_Limited_Private_Type:
4689 case E_Limited_Private_Subtype:
4690 case E_Record_Type_With_Private:
4691 case E_Record_Subtype_With_Private:
4693 /* Get the "full view" of this entity. If this is an incomplete
4694 entity from a limited with, treat its non-limited view as the
4695 full view. Otherwise, use either the full view or the underlying
4696 full view, whichever is present. This is used in all the tests
4697 below. */
4698 Entity_Id full_view
4699 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4700 ? Non_Limited_View (gnat_entity)
4701 : Present (Full_View (gnat_entity))
4702 ? Full_View (gnat_entity)
4703 : IN (kind, Private_Kind)
4704 ? Underlying_Full_View (gnat_entity)
4705 : Empty;
4707 /* If this is an incomplete type with no full view, it must be a Taft
4708 Amendment type, in which case we return a dummy type. Otherwise,
4709 just get the type from its Etype. */
4710 if (No (full_view))
4712 if (kind == E_Incomplete_Type)
4714 gnu_type = make_dummy_type (gnat_entity);
4715 gnu_decl = TYPE_STUB_DECL (gnu_type);
4717 else
4719 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4720 NULL_TREE, 0);
4721 maybe_present = true;
4723 break;
4726 /* If we already made a type for the full view, reuse it. */
4727 else if (present_gnu_tree (full_view))
4729 gnu_decl = get_gnu_tree (full_view);
4730 break;
4733 /* Otherwise, if we are not defining the type now, get the type
4734 from the full view. But always get the type from the full view
4735 for define on use types, since otherwise we won't see them! */
4736 else if (!definition
4737 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4738 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4740 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4741 maybe_present = true;
4742 break;
4745 /* For incomplete types, make a dummy type entry which will be
4746 replaced later. Save it as the full declaration's type so
4747 we can do any needed updates when we see it. */
4748 gnu_type = make_dummy_type (gnat_entity);
4749 gnu_decl = TYPE_STUB_DECL (gnu_type);
4750 if (Has_Completion_In_Body (gnat_entity))
4751 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4752 save_gnu_tree (full_view, gnu_decl, 0);
4753 break;
4756 case E_Class_Wide_Type:
4757 /* Class-wide types are always transformed into their root type. */
4758 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4759 maybe_present = true;
4760 break;
4762 case E_Task_Type:
4763 case E_Task_Subtype:
4764 case E_Protected_Type:
4765 case E_Protected_Subtype:
4766 /* Concurrent types are always transformed into their record type. */
4767 if (type_annotate_only && No (gnat_equiv_type))
4768 gnu_type = void_type_node;
4769 else
4770 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4771 maybe_present = true;
4772 break;
4774 case E_Label:
4775 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4776 break;
4778 case E_Block:
4779 case E_Loop:
4780 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4781 we've already saved it, so we don't try to. */
4782 gnu_decl = error_mark_node;
4783 saved = true;
4784 break;
4786 case E_Abstract_State:
4787 /* This is a SPARK annotation that only reaches here when compiling in
4788 ASIS mode and has no characteristics to annotate. */
4789 gcc_assert (type_annotate_only);
4790 return error_mark_node;
4792 default:
4793 gcc_unreachable ();
4796 /* If we had a case where we evaluated another type and it might have
4797 defined this one, handle it here. */
4798 if (maybe_present && present_gnu_tree (gnat_entity))
4800 gnu_decl = get_gnu_tree (gnat_entity);
4801 saved = true;
4804 /* If we are processing a type and there is either no decl for it or
4805 we just made one, do some common processing for the type, such as
4806 handling alignment and possible padding. */
4807 if (is_type && (!gnu_decl || this_made_decl))
4809 /* Process the attributes, if not already done. Note that the type is
4810 already defined so we cannot pass true for IN_PLACE here. */
4811 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4813 /* Tell the middle-end that objects of tagged types are guaranteed to
4814 be properly aligned. This is necessary because conversions to the
4815 class-wide type are translated into conversions to the root type,
4816 which can be less aligned than some of its derived types. */
4817 if (Is_Tagged_Type (gnat_entity)
4818 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4819 TYPE_ALIGN_OK (gnu_type) = 1;
4821 /* Record whether the type is passed by reference. */
4822 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4823 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4825 /* ??? Don't set the size for a String_Literal since it is either
4826 confirming or we don't handle it properly (if the low bound is
4827 non-constant). */
4828 if (!gnu_size && kind != E_String_Literal_Subtype)
4830 Uint gnat_size = Known_Esize (gnat_entity)
4831 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4832 gnu_size
4833 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4834 false, Has_Size_Clause (gnat_entity));
4837 /* If a size was specified, see if we can make a new type of that size
4838 by rearranging the type, for example from a fat to a thin pointer. */
4839 if (gnu_size)
4841 gnu_type
4842 = make_type_from_size (gnu_type, gnu_size,
4843 Has_Biased_Representation (gnat_entity));
4845 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4846 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4847 gnu_size = NULL_TREE;
4850 /* If the alignment has not already been processed and this is not
4851 an unconstrained array type, see if an alignment is specified.
4852 If not, we pick a default alignment for atomic objects. */
4853 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4855 else if (Known_Alignment (gnat_entity))
4857 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4858 TYPE_ALIGN (gnu_type));
4860 /* Warn on suspiciously large alignments. This should catch
4861 errors about the (alignment,byte)/(size,bit) discrepancy. */
4862 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4864 tree size;
4866 /* If a size was specified, take it into account. Otherwise
4867 use the RM size for records or unions as the type size has
4868 already been adjusted to the alignment. */
4869 if (gnu_size)
4870 size = gnu_size;
4871 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4872 && !TYPE_FAT_POINTER_P (gnu_type))
4873 size = rm_size (gnu_type);
4874 else
4875 size = TYPE_SIZE (gnu_type);
4877 /* Consider an alignment as suspicious if the alignment/size
4878 ratio is greater or equal to the byte/bit ratio. */
4879 if (tree_fits_uhwi_p (size)
4880 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4881 post_error_ne ("?suspiciously large alignment specified for&",
4882 Expression (Alignment_Clause (gnat_entity)),
4883 gnat_entity);
4886 else if (Is_Atomic (gnat_entity) && !gnu_size
4887 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4888 && integer_pow2p (TYPE_SIZE (gnu_type)))
4889 align = MIN (BIGGEST_ALIGNMENT,
4890 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4891 else if (Is_Atomic (gnat_entity) && gnu_size
4892 && tree_fits_uhwi_p (gnu_size)
4893 && integer_pow2p (gnu_size))
4894 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4896 /* See if we need to pad the type. If we did, and made a record,
4897 the name of the new type may be changed. So get it back for
4898 us when we make the new TYPE_DECL below. */
4899 if (gnu_size || align > 0)
4900 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4901 false, !gnu_decl, definition, false);
4903 if (TYPE_IS_PADDING_P (gnu_type))
4904 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4906 /* Now set the RM size of the type. We cannot do it before padding
4907 because we need to accept arbitrary RM sizes on integral types. */
4908 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4910 /* If we are at global level, GCC will have applied variable_size to
4911 the type, but that won't have done anything. So, if it's not
4912 a constant or self-referential, call elaborate_expression_1 to
4913 make a variable for the size rather than calculating it each time.
4914 Handle both the RM size and the actual size. */
4915 if (global_bindings_p ()
4916 && TYPE_SIZE (gnu_type)
4917 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4918 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4920 tree size = TYPE_SIZE (gnu_type);
4922 TYPE_SIZE (gnu_type)
4923 = elaborate_expression_1 (size, gnat_entity,
4924 get_identifier ("SIZE"),
4925 definition, false);
4927 /* ??? For now, store the size as a multiple of the alignment in
4928 bytes so that we can see the alignment from the tree. */
4929 TYPE_SIZE_UNIT (gnu_type)
4930 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4931 get_identifier ("SIZE_A_UNIT"),
4932 definition, false,
4933 TYPE_ALIGN (gnu_type));
4935 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4936 may not be marked by the call to create_type_decl below. */
4937 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4939 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4941 tree variant_part = get_variant_part (gnu_type);
4942 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4944 if (variant_part)
4946 tree union_type = TREE_TYPE (variant_part);
4947 tree offset = DECL_FIELD_OFFSET (variant_part);
4949 /* If the position of the variant part is constant, subtract
4950 it from the size of the type of the parent to get the new
4951 size. This manual CSE reduces the data size. */
4952 if (TREE_CODE (offset) == INTEGER_CST)
4954 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4955 TYPE_SIZE (union_type)
4956 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4957 bit_from_pos (offset, bitpos));
4958 TYPE_SIZE_UNIT (union_type)
4959 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4960 byte_from_pos (offset, bitpos));
4962 else
4964 TYPE_SIZE (union_type)
4965 = elaborate_expression_1 (TYPE_SIZE (union_type),
4966 gnat_entity,
4967 get_identifier ("VSIZE"),
4968 definition, false);
4970 /* ??? For now, store the size as a multiple of the
4971 alignment in bytes so that we can see the alignment
4972 from the tree. */
4973 TYPE_SIZE_UNIT (union_type)
4974 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4975 gnat_entity,
4976 get_identifier
4977 ("VSIZE_A_UNIT"),
4978 definition, false,
4979 TYPE_ALIGN (union_type));
4981 /* ??? For now, store the offset as a multiple of the
4982 alignment in bytes so that we can see the alignment
4983 from the tree. */
4984 DECL_FIELD_OFFSET (variant_part)
4985 = elaborate_expression_2 (offset,
4986 gnat_entity,
4987 get_identifier ("VOFFSET"),
4988 definition, false,
4989 DECL_OFFSET_ALIGN
4990 (variant_part));
4993 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4994 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4997 if (operand_equal_p (ada_size, size, 0))
4998 ada_size = TYPE_SIZE (gnu_type);
4999 else
5000 ada_size
5001 = elaborate_expression_1 (ada_size, gnat_entity,
5002 get_identifier ("RM_SIZE"),
5003 definition, false);
5004 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5008 /* If this is a record type or subtype, call elaborate_expression_2 on
5009 any field position. Do this for both global and local types.
5010 Skip any fields that we haven't made trees for to avoid problems with
5011 class wide types. */
5012 if (IN (kind, Record_Kind))
5013 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5014 gnat_temp = Next_Entity (gnat_temp))
5015 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5017 tree gnu_field = get_gnu_tree (gnat_temp);
5019 /* ??? For now, store the offset as a multiple of the alignment
5020 in bytes so that we can see the alignment from the tree. */
5021 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5023 DECL_FIELD_OFFSET (gnu_field)
5024 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5025 gnat_temp,
5026 get_identifier ("OFFSET"),
5027 definition, false,
5028 DECL_OFFSET_ALIGN (gnu_field));
5030 /* ??? The context of gnu_field is not necessarily gnu_type
5031 so the MULT_EXPR node built above may not be marked by
5032 the call to create_type_decl below. */
5033 if (global_bindings_p ())
5034 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5038 if (Is_Atomic (gnat_entity))
5039 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5041 /* If this is not an unconstrained array type, set some flags. */
5042 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5044 if (Treat_As_Volatile (gnat_entity))
5045 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5047 if (Present (Alignment_Clause (gnat_entity)))
5048 TYPE_USER_ALIGN (gnu_type) = 1;
5050 if (Universal_Aliasing (gnat_entity))
5051 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5054 if (!gnu_decl)
5055 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5056 !Comes_From_Source (gnat_entity),
5057 debug_info_p, gnat_entity);
5058 else
5060 TREE_TYPE (gnu_decl) = gnu_type;
5061 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5065 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5067 gnu_type = TREE_TYPE (gnu_decl);
5069 /* If this is a derived type, relate its alias set to that of its parent
5070 to avoid troubles when a call to an inherited primitive is inlined in
5071 a context where a derived object is accessed. The inlined code works
5072 on the parent view so the resulting code may access the same object
5073 using both the parent and the derived alias sets, which thus have to
5074 conflict. As the same issue arises with component references, the
5075 parent alias set also has to conflict with composite types enclosing
5076 derived components. For instance, if we have:
5078 type D is new T;
5079 type R is record
5080 Component : D;
5081 end record;
5083 we want T to conflict with both D and R, in addition to R being a
5084 superset of D by record/component construction.
5086 One way to achieve this is to perform an alias set copy from the
5087 parent to the derived type. This is not quite appropriate, though,
5088 as we don't want separate derived types to conflict with each other:
5090 type I1 is new Integer;
5091 type I2 is new Integer;
5093 We want I1 and I2 to both conflict with Integer but we do not want
5094 I1 to conflict with I2, and an alias set copy on derivation would
5095 have that effect.
5097 The option chosen is to make the alias set of the derived type a
5098 superset of that of its parent type. It trivially fulfills the
5099 simple requirement for the Integer derivation example above, and
5100 the component case as well by superset transitivity:
5102 superset superset
5103 R ----------> D ----------> T
5105 However, for composite types, conversions between derived types are
5106 translated into VIEW_CONVERT_EXPRs so a sequence like:
5108 type Comp1 is new Comp;
5109 type Comp2 is new Comp;
5110 procedure Proc (C : Comp1);
5112 C : Comp2;
5113 Proc (Comp1 (C));
5115 is translated into:
5117 C : Comp2;
5118 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5120 and gimplified into:
5122 C : Comp2;
5123 Comp1 *C.0;
5124 C.0 = (Comp1 *) &C;
5125 Proc (C.0);
5127 i.e. generates code involving type punning. Therefore, Comp1 needs
5128 to conflict with Comp2 and an alias set copy is required.
5130 The language rules ensure the parent type is already frozen here. */
5131 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5133 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5134 /* For constrained packed array subtypes, the implementation type is
5135 used instead of the nominal type. */
5136 if (kind == E_Array_Subtype
5137 && Is_Constrained (gnat_entity)
5138 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5139 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5140 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5141 Is_Composite_Type (gnat_entity)
5142 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5145 /* Back-annotate the Alignment of the type if not already in the
5146 tree. Likewise for sizes. */
5147 if (Unknown_Alignment (gnat_entity))
5149 unsigned int double_align, align;
5150 bool is_capped_double, align_clause;
5152 /* If the default alignment of "double" or larger scalar types is
5153 specifically capped and this is not an array with an alignment
5154 clause on the component type, return the cap. */
5155 if ((double_align = double_float_alignment) > 0)
5156 is_capped_double
5157 = is_double_float_or_array (gnat_entity, &align_clause);
5158 else if ((double_align = double_scalar_alignment) > 0)
5159 is_capped_double
5160 = is_double_scalar_or_array (gnat_entity, &align_clause);
5161 else
5162 is_capped_double = align_clause = false;
5164 if (is_capped_double && !align_clause)
5165 align = double_align;
5166 else
5167 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5169 Set_Alignment (gnat_entity, UI_From_Int (align));
5172 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5174 tree gnu_size = TYPE_SIZE (gnu_type);
5176 /* If the size is self-referential, annotate the maximum value. */
5177 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5178 gnu_size = max_size (gnu_size, true);
5180 /* If we are just annotating types and the type is tagged, the tag
5181 and the parent components are not generated by the front-end so
5182 sizes must be adjusted if there is no representation clause. */
5183 if (type_annotate_only
5184 && Is_Tagged_Type (gnat_entity)
5185 && !VOID_TYPE_P (gnu_type)
5186 && (!TYPE_FIELDS (gnu_type)
5187 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5189 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5190 Uint uint_size;
5192 if (Is_Derived_Type (gnat_entity))
5194 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5195 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5196 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5198 else
5199 offset = pointer_size;
5201 if (TYPE_FIELDS (gnu_type))
5202 offset
5203 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5205 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5206 gnu_size = round_up (gnu_size, POINTER_SIZE);
5207 uint_size = annotate_value (gnu_size);
5208 Set_Esize (gnat_entity, uint_size);
5209 Set_RM_Size (gnat_entity, uint_size);
5211 else
5212 Set_Esize (gnat_entity, annotate_value (gnu_size));
5215 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5216 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5219 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5220 cannot do so if we are reusing the ..._DECL node made for an equivalent
5221 type or an alias or a renamed object as the predicates don't apply to it
5222 but to GNAT_ENTITY. */
5223 if (DECL_P (gnu_decl)
5224 && !(is_type && gnat_equiv_type != gnat_entity)
5225 && !Present (Alias (gnat_entity))
5226 && !(Present (Renamed_Object (gnat_entity)) && saved))
5228 if (!Comes_From_Source (gnat_entity))
5229 DECL_ARTIFICIAL (gnu_decl) = 1;
5231 if (!debug_info_p)
5232 DECL_IGNORED_P (gnu_decl) = 1;
5235 /* If we haven't already, associate the ..._DECL node that we just made with
5236 the input GNAT entity node. */
5237 if (!saved)
5238 save_gnu_tree (gnat_entity, gnu_decl, false);
5240 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5241 eliminate as many deferred computations as possible. */
5242 process_deferred_decl_context (false);
5244 /* If this is an enumeration or floating-point type, we were not able to set
5245 the bounds since they refer to the type. These are always static. */
5246 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5247 || (kind == E_Floating_Point_Type))
5249 tree gnu_scalar_type = gnu_type;
5250 tree gnu_low_bound, gnu_high_bound;
5252 /* If this is a padded type, we need to use the underlying type. */
5253 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5254 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5256 /* If this is a floating point type and we haven't set a floating
5257 point type yet, use this in the evaluation of the bounds. */
5258 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5259 longest_float_type_node = gnu_scalar_type;
5261 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5262 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5264 if (kind == E_Enumeration_Type)
5266 /* Enumeration types have specific RM bounds. */
5267 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5268 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5270 else
5272 /* Floating-point types don't have specific RM bounds. */
5273 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5274 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5278 /* If we deferred processing of incomplete types, re-enable it. If there
5279 were no other disables and we have deferred types to process, do so. */
5280 if (this_deferred
5281 && --defer_incomplete_level == 0
5282 && defer_incomplete_list)
5284 struct incomplete *p, *next;
5286 /* We are back to level 0 for the deferring of incomplete types.
5287 But processing these incomplete types below may itself require
5288 deferring, so preserve what we have and restart from scratch. */
5289 p = defer_incomplete_list;
5290 defer_incomplete_list = NULL;
5292 for (; p; p = next)
5294 next = p->next;
5296 if (p->old_type)
5297 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5298 gnat_to_gnu_type (p->full_type));
5299 free (p);
5303 /* If we are not defining this type, see if it's on one of the lists of
5304 incomplete types. If so, handle the list entry now. */
5305 if (is_type && !definition)
5307 struct incomplete *p;
5309 for (p = defer_incomplete_list; p; p = p->next)
5310 if (p->old_type && p->full_type == gnat_entity)
5312 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5313 TREE_TYPE (gnu_decl));
5314 p->old_type = NULL_TREE;
5317 for (p = defer_limited_with; p; p = p->next)
5318 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5320 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5321 TREE_TYPE (gnu_decl));
5322 p->old_type = NULL_TREE;
5326 if (this_global)
5327 force_global--;
5329 /* If this is a packed array type whose original array type is itself
5330 an Itype without freeze node, make sure the latter is processed. */
5331 if (Is_Packed_Array_Impl_Type (gnat_entity)
5332 && Is_Itype (Original_Array_Type (gnat_entity))
5333 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5334 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5335 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5337 return gnu_decl;
5340 /* Similar, but if the returned value is a COMPONENT_REF, return the
5341 FIELD_DECL. */
5343 tree
5344 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5346 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5348 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5349 gnu_field = TREE_OPERAND (gnu_field, 1);
5351 return gnu_field;
5354 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5355 the GCC type corresponding to that entity. */
5357 tree
5358 gnat_to_gnu_type (Entity_Id gnat_entity)
5360 tree gnu_decl;
5362 /* The back end never attempts to annotate generic types. */
5363 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5364 return void_type_node;
5366 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5367 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5369 return TREE_TYPE (gnu_decl);
5372 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5373 the unpadded version of the GCC type corresponding to that entity. */
5375 tree
5376 get_unpadded_type (Entity_Id gnat_entity)
5378 tree type = gnat_to_gnu_type (gnat_entity);
5380 if (TYPE_IS_PADDING_P (type))
5381 type = TREE_TYPE (TYPE_FIELDS (type));
5383 return type;
5386 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5387 type has been changed to that of the parameterless procedure, except if an
5388 alias is already present, in which case it is returned instead. */
5390 tree
5391 get_minimal_subprog_decl (Entity_Id gnat_entity)
5393 tree gnu_entity_name, gnu_ext_name;
5394 struct attrib *attr_list = NULL;
5396 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5397 of the handling applied here. */
5399 while (Present (Alias (gnat_entity)))
5401 gnat_entity = Alias (gnat_entity);
5402 if (present_gnu_tree (gnat_entity))
5403 return get_gnu_tree (gnat_entity);
5406 gnu_entity_name = get_entity_name (gnat_entity);
5407 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5409 if (Has_Stdcall_Convention (gnat_entity))
5410 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5411 get_identifier ("stdcall"), NULL_TREE,
5412 gnat_entity);
5413 else if (Has_Thiscall_Convention (gnat_entity))
5414 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5415 get_identifier ("thiscall"), NULL_TREE,
5416 gnat_entity);
5418 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5419 gnu_ext_name = NULL_TREE;
5421 return
5422 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5423 is_disabled, true, true, true, attr_list, gnat_entity);
5426 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5427 a C++ imported method or equivalent.
5429 We use the predicate on 32-bit x86/Windows to find out whether we need to
5430 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5431 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5433 bool
5434 is_cplusplus_method (Entity_Id gnat_entity)
5436 if (Convention (gnat_entity) != Convention_CPP)
5437 return false;
5439 /* This is the main case: C++ method imported as a primitive operation. */
5440 if (Is_Dispatching_Operation (gnat_entity))
5441 return true;
5443 /* A thunk needs to be handled like its associated primitive operation. */
5444 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5445 return true;
5447 /* C++ classes with no virtual functions can be imported as limited
5448 record types, but we need to return true for the constructors. */
5449 if (Is_Constructor (gnat_entity))
5450 return true;
5452 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5453 if (Is_Dispatch_Table_Entity (gnat_entity))
5454 return true;
5456 return false;
5459 /* Finalize the processing of From_Limited_With incomplete types. */
5461 void
5462 finalize_from_limited_with (void)
5464 struct incomplete *p, *next;
5466 p = defer_limited_with;
5467 defer_limited_with = NULL;
5469 for (; p; p = next)
5471 next = p->next;
5473 if (p->old_type)
5474 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5475 gnat_to_gnu_type (p->full_type));
5476 free (p);
5480 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5481 kind of type (such E_Task_Type) that has a different type which Gigi
5482 uses for its representation. If the type does not have a special type
5483 for its representation, return GNAT_ENTITY. If a type is supposed to
5484 exist, but does not, abort unless annotating types, in which case
5485 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5487 Entity_Id
5488 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5490 Entity_Id gnat_equiv = gnat_entity;
5492 if (No (gnat_entity))
5493 return gnat_entity;
5495 switch (Ekind (gnat_entity))
5497 case E_Class_Wide_Subtype:
5498 if (Present (Equivalent_Type (gnat_entity)))
5499 gnat_equiv = Equivalent_Type (gnat_entity);
5500 break;
5502 case E_Access_Protected_Subprogram_Type:
5503 case E_Anonymous_Access_Protected_Subprogram_Type:
5504 gnat_equiv = Equivalent_Type (gnat_entity);
5505 break;
5507 case E_Class_Wide_Type:
5508 gnat_equiv = Root_Type (gnat_entity);
5509 break;
5511 case E_Task_Type:
5512 case E_Task_Subtype:
5513 case E_Protected_Type:
5514 case E_Protected_Subtype:
5515 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5516 break;
5518 default:
5519 break;
5522 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5524 return gnat_equiv;
5527 /* Return a GCC tree for a type corresponding to the component type of the
5528 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5529 is for an array being defined. DEBUG_INFO_P is true if we need to write
5530 debug information for other types that we may create in the process. */
5532 static tree
5533 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5534 bool debug_info_p)
5536 const Entity_Id gnat_type = Component_Type (gnat_array);
5537 tree gnu_type = gnat_to_gnu_type (gnat_type);
5538 tree gnu_comp_size;
5540 /* Try to get a smaller form of the component if needed. */
5541 if ((Is_Packed (gnat_array)
5542 || Has_Component_Size_Clause (gnat_array))
5543 && !Is_Bit_Packed_Array (gnat_array)
5544 && !Has_Aliased_Components (gnat_array)
5545 && !Strict_Alignment (gnat_type)
5546 && RECORD_OR_UNION_TYPE_P (gnu_type)
5547 && !TYPE_FAT_POINTER_P (gnu_type)
5548 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5549 gnu_type = make_packable_type (gnu_type, false);
5551 if (Has_Atomic_Components (gnat_array))
5552 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5554 /* Get and validate any specified Component_Size. */
5555 gnu_comp_size
5556 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5557 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5558 true, Has_Component_Size_Clause (gnat_array));
5560 /* If the array has aliased components and the component size can be zero,
5561 force at least unit size to ensure that the components have distinct
5562 addresses. */
5563 if (!gnu_comp_size
5564 && Has_Aliased_Components (gnat_array)
5565 && (integer_zerop (TYPE_SIZE (gnu_type))
5566 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5567 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5568 gnu_comp_size
5569 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5571 /* If the component type is a RECORD_TYPE that has a self-referential size,
5572 then use the maximum size for the component size. */
5573 if (!gnu_comp_size
5574 && TREE_CODE (gnu_type) == RECORD_TYPE
5575 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5576 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5578 /* Honor the component size. This is not needed for bit-packed arrays. */
5579 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5581 tree orig_type = gnu_type;
5582 unsigned int max_align;
5584 /* If an alignment is specified, use it as a cap on the component type
5585 so that it can be honored for the whole type. But ignore it for the
5586 original type of packed array types. */
5587 if (No (Packed_Array_Impl_Type (gnat_array))
5588 && Known_Alignment (gnat_array))
5589 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5590 else
5591 max_align = 0;
5593 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5594 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5595 gnu_type = orig_type;
5596 else
5597 orig_type = gnu_type;
5599 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5600 true, false, definition, true);
5602 /* If a padding record was made, declare it now since it will never be
5603 declared otherwise. This is necessary to ensure that its subtrees
5604 are properly marked. */
5605 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5606 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5607 gnat_array);
5610 if (Has_Volatile_Components (gnat_array))
5611 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5613 return gnu_type;
5616 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5617 using MECH as its passing mechanism, to be placed in the parameter
5618 list built for GNAT_SUBPROG. Assume a foreign convention for the
5619 latter if FOREIGN is true. Also set CICO to true if the parameter
5620 must use the copy-in copy-out implementation mechanism.
5622 The returned tree is a PARM_DECL, except for those cases where no
5623 parameter needs to be actually passed to the subprogram; the type
5624 of this "shadow" parameter is then returned instead. */
5626 static tree
5627 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5628 Entity_Id gnat_subprog, bool foreign, bool *cico)
5630 tree gnu_param_name = get_entity_name (gnat_param);
5631 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5632 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5633 /* The parameter can be indirectly modified if its address is taken. */
5634 bool ro_param = in_param && !Address_Taken (gnat_param);
5635 bool by_return = false, by_component_ptr = false;
5636 bool by_ref = false;
5637 tree gnu_param;
5639 /* Copy-return is used only for the first parameter of a valued procedure.
5640 It's a copy mechanism for which a parameter is never allocated. */
5641 if (mech == By_Copy_Return)
5643 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5644 mech = By_Copy;
5645 by_return = true;
5648 /* If this is either a foreign function or if the underlying type won't
5649 be passed by reference, strip off possible padding type. */
5650 if (TYPE_IS_PADDING_P (gnu_param_type))
5652 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5654 if (mech == By_Reference
5655 || foreign
5656 || (!must_pass_by_ref (unpadded_type)
5657 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5658 gnu_param_type = unpadded_type;
5661 /* If this is a read-only parameter, make a variant of the type that is
5662 read-only. ??? However, if this is an unconstrained array, that type
5663 can be very complex, so skip it for now. Likewise for any other
5664 self-referential type. */
5665 if (ro_param
5666 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5667 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5668 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5670 /* For foreign conventions, pass arrays as pointers to the element type.
5671 First check for unconstrained array and get the underlying array. */
5672 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5673 gnu_param_type
5674 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5676 /* For GCC builtins, pass Address integer types as (void *) */
5677 if (Convention (gnat_subprog) == Convention_Intrinsic
5678 && Present (Interface_Name (gnat_subprog))
5679 && Is_Descendent_Of_Address (Etype (gnat_param)))
5680 gnu_param_type = ptr_void_type_node;
5682 /* Arrays are passed as pointers to element type for foreign conventions. */
5683 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5685 /* Strip off any multi-dimensional entries, then strip
5686 off the last array to get the component type. */
5687 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5688 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5689 gnu_param_type = TREE_TYPE (gnu_param_type);
5691 by_component_ptr = true;
5692 gnu_param_type = TREE_TYPE (gnu_param_type);
5694 if (ro_param)
5695 gnu_param_type
5696 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5698 gnu_param_type = build_pointer_type (gnu_param_type);
5701 /* Fat pointers are passed as thin pointers for foreign conventions. */
5702 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5703 gnu_param_type
5704 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5706 /* If we must pass or were requested to pass by reference, do so.
5707 If we were requested to pass by copy, do so.
5708 Otherwise, for foreign conventions, pass In Out or Out parameters
5709 or aggregates by reference. For COBOL and Fortran, pass all
5710 integer and FP types that way too. For Convention Ada, use
5711 the standard Ada default. */
5712 else if (must_pass_by_ref (gnu_param_type)
5713 || mech == By_Reference
5714 || (mech != By_Copy
5715 && ((foreign
5716 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5717 || (foreign
5718 && (Convention (gnat_subprog) == Convention_Fortran
5719 || Convention (gnat_subprog) == Convention_COBOL)
5720 && (INTEGRAL_TYPE_P (gnu_param_type)
5721 || FLOAT_TYPE_P (gnu_param_type)))
5722 || (!foreign
5723 && default_pass_by_ref (gnu_param_type)))))
5725 /* We take advantage of 6.2(12) by considering that references built for
5726 parameters whose type isn't by-ref and for which the mechanism hasn't
5727 been forced to by-ref are restrict-qualified in the C sense. */
5728 bool restrict_p
5729 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5730 gnu_param_type = build_reference_type (gnu_param_type);
5731 if (restrict_p)
5732 gnu_param_type
5733 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5734 by_ref = true;
5737 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5738 else if (!in_param)
5739 *cico = true;
5741 if (mech == By_Copy && (by_ref || by_component_ptr))
5742 post_error ("?cannot pass & by copy", gnat_param);
5744 /* If this is an Out parameter that isn't passed by reference and isn't
5745 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5746 it will be a VAR_DECL created when we process the procedure, so just
5747 return its type. For the special parameter of a valued procedure,
5748 never pass it in.
5750 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5751 Out parameters with discriminants or implicit initial values to be
5752 handled like In Out parameters. These type are normally built as
5753 aggregates, hence passed by reference, except for some packed arrays
5754 which end up encoded in special integer types. Note that scalars can
5755 be given implicit initial values using the Default_Value aspect.
5757 The exception we need to make is then for packed arrays of records
5758 with discriminants or implicit initial values. We have no light/easy
5759 way to check for the latter case, so we merely check for packed arrays
5760 of records. This may lead to useless copy-in operations, but in very
5761 rare cases only, as these would be exceptions in a set of already
5762 exceptional situations. */
5763 if (Ekind (gnat_param) == E_Out_Parameter
5764 && !by_ref
5765 && (by_return
5766 || (!POINTER_TYPE_P (gnu_param_type)
5767 && !AGGREGATE_TYPE_P (gnu_param_type)
5768 && !Has_Default_Aspect (Etype (gnat_param))))
5769 && !(Is_Array_Type (Etype (gnat_param))
5770 && Is_Packed (Etype (gnat_param))
5771 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5772 return gnu_param_type;
5774 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5775 ro_param || by_ref || by_component_ptr);
5776 DECL_BY_REF_P (gnu_param) = by_ref;
5777 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5778 DECL_POINTS_TO_READONLY_P (gnu_param)
5779 = (ro_param && (by_ref || by_component_ptr));
5780 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5782 /* If no Mechanism was specified, indicate what we're using, then
5783 back-annotate it. */
5784 if (mech == Default)
5785 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5787 Set_Mechanism (gnat_param, mech);
5788 return gnu_param;
5791 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5792 qualifiers on TYPE. */
5794 static tree
5795 change_qualified_type (tree type, int type_quals)
5797 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5800 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5802 static bool
5803 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5805 while (Present (Corresponding_Discriminant (discr1)))
5806 discr1 = Corresponding_Discriminant (discr1);
5808 while (Present (Corresponding_Discriminant (discr2)))
5809 discr2 = Corresponding_Discriminant (discr2);
5811 return
5812 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5815 /* Return true if the array type GNU_TYPE, which represents a dimension of
5816 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5818 static bool
5819 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5821 /* If the array type is not the innermost dimension of the GNAT type,
5822 then it has a non-aliased component. */
5823 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5824 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5825 return true;
5827 /* If the array type has an aliased component in the front-end sense,
5828 then it also has an aliased component in the back-end sense. */
5829 if (Has_Aliased_Components (gnat_type))
5830 return false;
5832 /* If this is a derived type, then it has a non-aliased component if
5833 and only if its parent type also has one. */
5834 if (Is_Derived_Type (gnat_type))
5836 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5837 int index;
5838 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5839 gnu_parent_type
5840 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5841 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5842 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5843 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5846 /* Otherwise, rely exclusively on properties of the element type. */
5847 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5850 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5852 static bool
5853 compile_time_known_address_p (Node_Id gnat_address)
5855 /* Catch System'To_Address. */
5856 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5857 gnat_address = Expression (gnat_address);
5859 return Compile_Time_Known_Value (gnat_address);
5862 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5863 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5865 static bool
5866 cannot_be_superflat_p (Node_Id gnat_range)
5868 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5869 Node_Id scalar_range;
5870 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5872 /* If the low bound is not constant, try to find an upper bound. */
5873 while (Nkind (gnat_lb) != N_Integer_Literal
5874 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5875 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5876 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5877 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5878 || Nkind (scalar_range) == N_Range))
5879 gnat_lb = High_Bound (scalar_range);
5881 /* If the high bound is not constant, try to find a lower bound. */
5882 while (Nkind (gnat_hb) != N_Integer_Literal
5883 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5884 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5885 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5886 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5887 || Nkind (scalar_range) == N_Range))
5888 gnat_hb = Low_Bound (scalar_range);
5890 /* If we have failed to find constant bounds, punt. */
5891 if (Nkind (gnat_lb) != N_Integer_Literal
5892 || Nkind (gnat_hb) != N_Integer_Literal)
5893 return false;
5895 /* We need at least a signed 64-bit type to catch most cases. */
5896 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5897 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5898 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5899 return false;
5901 /* If the low bound is the smallest integer, nothing can be smaller. */
5902 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5903 if (TREE_OVERFLOW (gnu_lb_minus_one))
5904 return true;
5906 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5909 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5911 static bool
5912 constructor_address_p (tree gnu_expr)
5914 while (TREE_CODE (gnu_expr) == NOP_EXPR
5915 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5916 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5917 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5919 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5920 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5923 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5924 be elaborated at the point of its definition, but do nothing else. */
5926 void
5927 elaborate_entity (Entity_Id gnat_entity)
5929 switch (Ekind (gnat_entity))
5931 case E_Signed_Integer_Subtype:
5932 case E_Modular_Integer_Subtype:
5933 case E_Enumeration_Subtype:
5934 case E_Ordinary_Fixed_Point_Subtype:
5935 case E_Decimal_Fixed_Point_Subtype:
5936 case E_Floating_Point_Subtype:
5938 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5939 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5941 /* ??? Tests to avoid Constraint_Error in static expressions
5942 are needed until after the front stops generating bogus
5943 conversions on bounds of real types. */
5944 if (!Raises_Constraint_Error (gnat_lb))
5945 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5946 true, false, Needs_Debug_Info (gnat_entity));
5947 if (!Raises_Constraint_Error (gnat_hb))
5948 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5949 true, false, Needs_Debug_Info (gnat_entity));
5950 break;
5953 case E_Record_Subtype:
5954 case E_Private_Subtype:
5955 case E_Limited_Private_Subtype:
5956 case E_Record_Subtype_With_Private:
5957 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5959 Node_Id gnat_discriminant_expr;
5960 Entity_Id gnat_field;
5962 for (gnat_field
5963 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5964 gnat_discriminant_expr
5965 = First_Elmt (Discriminant_Constraint (gnat_entity));
5966 Present (gnat_field);
5967 gnat_field = Next_Discriminant (gnat_field),
5968 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5969 /* Ignore access discriminants. */
5970 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5971 elaborate_expression (Node (gnat_discriminant_expr),
5972 gnat_entity, get_entity_name (gnat_field),
5973 true, false, false);
5975 break;
5980 /* Return true if the size in units represented by GNU_SIZE can be handled by
5981 an allocation. If STATIC_P is true, consider only what can be done with a
5982 static allocation. */
5984 static bool
5985 allocatable_size_p (tree gnu_size, bool static_p)
5987 /* We can allocate a fixed size if it is a valid for the middle-end. */
5988 if (TREE_CODE (gnu_size) == INTEGER_CST)
5989 return valid_constant_size_p (gnu_size);
5991 /* We can allocate a variable size if this isn't a static allocation. */
5992 else
5993 return !static_p;
5996 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5997 NAME, ARGS and ERROR_POINT. */
5999 static void
6000 prepend_one_attribute (struct attrib **attr_list,
6001 enum attr_type attr_type,
6002 tree attr_name,
6003 tree attr_args,
6004 Node_Id attr_error_point)
6006 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6008 attr->type = attr_type;
6009 attr->name = attr_name;
6010 attr->args = attr_args;
6011 attr->error_point = attr_error_point;
6013 attr->next = *attr_list;
6014 *attr_list = attr;
6017 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6019 static void
6020 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6022 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6023 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6024 enum attr_type etype;
6026 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6027 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6029 case Pragma_Machine_Attribute:
6030 etype = ATTR_MACHINE_ATTRIBUTE;
6031 break;
6033 case Pragma_Linker_Alias:
6034 etype = ATTR_LINK_ALIAS;
6035 break;
6037 case Pragma_Linker_Section:
6038 etype = ATTR_LINK_SECTION;
6039 break;
6041 case Pragma_Linker_Constructor:
6042 etype = ATTR_LINK_CONSTRUCTOR;
6043 break;
6045 case Pragma_Linker_Destructor:
6046 etype = ATTR_LINK_DESTRUCTOR;
6047 break;
6049 case Pragma_Weak_External:
6050 etype = ATTR_WEAK_EXTERNAL;
6051 break;
6053 case Pragma_Thread_Local_Storage:
6054 etype = ATTR_THREAD_LOCAL_STORAGE;
6055 break;
6057 default:
6058 return;
6061 /* See what arguments we have and turn them into GCC trees for attribute
6062 handlers. These expect identifier for strings. We handle at most two
6063 arguments and static expressions only. */
6064 if (Present (gnat_arg) && Present (First (gnat_arg)))
6066 Node_Id gnat_arg0 = Next (First (gnat_arg));
6067 Node_Id gnat_arg1 = Empty;
6069 if (Present (gnat_arg0)
6070 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6072 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6074 if (TREE_CODE (gnu_arg0) == STRING_CST)
6076 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6077 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6078 return;
6081 gnat_arg1 = Next (gnat_arg0);
6084 if (Present (gnat_arg1)
6085 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6087 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6089 if (TREE_CODE (gnu_arg1) == STRING_CST)
6090 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6094 /* Prepend to the list. Make a list of the argument we might have, as GCC
6095 expects it. */
6096 prepend_one_attribute (attr_list, etype, gnu_arg0,
6097 gnu_arg1
6098 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6099 Present (Next (First (gnat_arg)))
6100 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6103 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6105 static void
6106 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6108 Node_Id gnat_temp;
6110 /* Attributes are stored as Representation Item pragmas. */
6111 for (gnat_temp = First_Rep_Item (gnat_entity);
6112 Present (gnat_temp);
6113 gnat_temp = Next_Rep_Item (gnat_temp))
6114 if (Nkind (gnat_temp) == N_Pragma)
6115 prepend_one_attribute_pragma (attr_list, gnat_temp);
6118 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6119 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6120 return the GCC tree to use for that expression. GNU_NAME is the suffix
6121 to use if a variable needs to be created and DEFINITION is true if this
6122 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6123 otherwise, we are just elaborating the expression for side-effects. If
6124 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6125 isn't needed for code generation. */
6127 static tree
6128 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6129 bool definition, bool need_value, bool need_debug)
6131 tree gnu_expr;
6133 /* If we already elaborated this expression (e.g. it was involved
6134 in the definition of a private type), use the old value. */
6135 if (present_gnu_tree (gnat_expr))
6136 return get_gnu_tree (gnat_expr);
6138 /* If we don't need a value and this is static or a discriminant,
6139 we don't need to do anything. */
6140 if (!need_value
6141 && (Is_OK_Static_Expression (gnat_expr)
6142 || (Nkind (gnat_expr) == N_Identifier
6143 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6144 return NULL_TREE;
6146 /* If it's a static expression, we don't need a variable for debugging. */
6147 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6148 need_debug = false;
6150 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6151 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6152 gnu_name, definition, need_debug);
6154 /* Save the expression in case we try to elaborate this entity again. Since
6155 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6156 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6157 save_gnu_tree (gnat_expr, gnu_expr, true);
6159 return need_value ? gnu_expr : error_mark_node;
6162 /* Similar, but take a GNU expression and always return a result. */
6164 static tree
6165 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6166 bool definition, bool need_debug)
6168 const bool expr_public_p = Is_Public (gnat_entity);
6169 const bool expr_global_p = expr_public_p || global_bindings_p ();
6170 bool expr_variable_p, use_variable;
6172 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6173 reference will have been replaced with a COMPONENT_REF when the type
6174 is being elaborated. However, there are some cases involving child
6175 types where we will. So convert it to a COMPONENT_REF. We hope it
6176 will be at the highest level of the expression in these cases. */
6177 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6178 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6179 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6180 gnu_expr, NULL_TREE);
6182 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6183 that an expression cannot contain both a discriminant and a variable. */
6184 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6185 return gnu_expr;
6187 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6188 a variable that is initialized to contain the expression when the package
6189 containing the definition is elaborated. If this entity is defined at top
6190 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6191 if this is necessary. */
6192 if (CONSTANT_CLASS_P (gnu_expr))
6193 expr_variable_p = false;
6194 else
6196 /* Skip any conversions and simple constant arithmetics to see if the
6197 expression is based on a read-only variable.
6198 ??? This really should remain read-only, but we have to think about
6199 the typing of the tree here. */
6200 tree inner = remove_conversions (gnu_expr, true);
6202 inner = skip_simple_constant_arithmetic (inner);
6204 if (handled_component_p (inner))
6206 HOST_WIDE_INT bitsize, bitpos;
6207 tree offset;
6208 machine_mode mode;
6209 int unsignedp, volatilep;
6211 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6212 &mode, &unsignedp, &volatilep, false);
6213 /* If the offset is variable, err on the side of caution. */
6214 if (offset)
6215 inner = NULL_TREE;
6218 expr_variable_p
6219 = !(inner
6220 && TREE_CODE (inner) == VAR_DECL
6221 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6224 /* We only need to use the variable if we are in a global context since GCC
6225 can do the right thing in the local case. However, when not optimizing,
6226 use it for bounds of loop iteration scheme to avoid code duplication. */
6227 use_variable = expr_variable_p
6228 && (expr_global_p
6229 || (!optimize
6230 && definition
6231 && Is_Itype (gnat_entity)
6232 && Nkind (Associated_Node_For_Itype (gnat_entity))
6233 == N_Loop_Parameter_Specification));
6235 /* Now create it, possibly only for debugging purposes. */
6236 if (use_variable || need_debug)
6238 /* The following variable creation can happen when processing the body of
6239 subprograms that are defined out of the extended main unit and
6240 inlined. In this case, we are not at the global scope, and thus the
6241 new variable must not be tagged "external", as we used to do here as
6242 long as definition == 0. */
6243 const bool external_flag = !definition && expr_global_p;
6244 tree gnu_decl
6245 = create_var_decl_1
6246 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6247 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6248 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6250 DECL_ARTIFICIAL (gnu_decl) = 1;
6252 /* Using this variable at debug time (if need_debug is true) requires a
6253 proper location. The back-end will compute a location for this
6254 variable only if the variable is used by the generated code.
6255 Returning the variable ensures the caller will use it in generated
6256 code. Note that there is no need for a location if the debug info
6257 contains an integer constant.
6258 FIXME: when the encoding-based debug scheme is dropped, move this
6259 condition to the top-level IF block: we will not need to create a
6260 variable anymore in such cases, then. */
6261 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6262 return gnu_decl;
6265 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6268 /* Similar, but take an alignment factor and make it explicit in the tree. */
6270 static tree
6271 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6272 bool definition, bool need_debug, unsigned int align)
6274 tree unit_align = size_int (align / BITS_PER_UNIT);
6275 return
6276 size_binop (MULT_EXPR,
6277 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6278 gnu_expr,
6279 unit_align),
6280 gnat_entity, gnu_name, definition,
6281 need_debug),
6282 unit_align);
6285 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6286 the value passed against the list of choices. */
6288 tree
6289 choices_to_gnu (tree operand, Node_Id choices)
6291 Node_Id choice;
6292 Node_Id gnat_temp;
6293 tree result = boolean_false_node;
6294 tree this_test, low = 0, high = 0, single = 0;
6296 for (choice = First (choices); Present (choice); choice = Next (choice))
6298 switch (Nkind (choice))
6300 case N_Range:
6301 low = gnat_to_gnu (Low_Bound (choice));
6302 high = gnat_to_gnu (High_Bound (choice));
6304 this_test
6305 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6306 build_binary_op (GE_EXPR, boolean_type_node,
6307 operand, low),
6308 build_binary_op (LE_EXPR, boolean_type_node,
6309 operand, high));
6311 break;
6313 case N_Subtype_Indication:
6314 gnat_temp = Range_Expression (Constraint (choice));
6315 low = gnat_to_gnu (Low_Bound (gnat_temp));
6316 high = gnat_to_gnu (High_Bound (gnat_temp));
6318 this_test
6319 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6320 build_binary_op (GE_EXPR, boolean_type_node,
6321 operand, low),
6322 build_binary_op (LE_EXPR, boolean_type_node,
6323 operand, high));
6324 break;
6326 case N_Identifier:
6327 case N_Expanded_Name:
6328 /* This represents either a subtype range, an enumeration
6329 literal, or a constant Ekind says which. If an enumeration
6330 literal or constant, fall through to the next case. */
6331 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6332 && Ekind (Entity (choice)) != E_Constant)
6334 tree type = gnat_to_gnu_type (Entity (choice));
6336 low = TYPE_MIN_VALUE (type);
6337 high = TYPE_MAX_VALUE (type);
6339 this_test
6340 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6341 build_binary_op (GE_EXPR, boolean_type_node,
6342 operand, low),
6343 build_binary_op (LE_EXPR, boolean_type_node,
6344 operand, high));
6345 break;
6348 /* ... fall through ... */
6350 case N_Character_Literal:
6351 case N_Integer_Literal:
6352 single = gnat_to_gnu (choice);
6353 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6354 single);
6355 break;
6357 case N_Others_Choice:
6358 this_test = boolean_true_node;
6359 break;
6361 default:
6362 gcc_unreachable ();
6365 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6366 this_test);
6369 return result;
6372 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6373 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6375 static int
6376 adjust_packed (tree field_type, tree record_type, int packed)
6378 /* If the field contains an item of variable size, we cannot pack it
6379 because we cannot create temporaries of non-fixed size in case
6380 we need to take the address of the field. See addressable_p and
6381 the notes on the addressability issues for further details. */
6382 if (type_has_variable_size (field_type))
6383 return 0;
6385 /* If the alignment of the record is specified and the field type
6386 is over-aligned, request Storage_Unit alignment for the field. */
6387 if (packed == -2)
6389 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6390 return -1;
6391 else
6392 return 0;
6395 return packed;
6398 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6399 placed in GNU_RECORD_TYPE.
6401 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6402 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6403 record has a specified alignment.
6405 DEFINITION is true if this field is for a record being defined.
6407 DEBUG_INFO_P is true if we need to write debug information for types
6408 that we may create in the process. */
6410 static tree
6411 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6412 bool definition, bool debug_info_p)
6414 const Entity_Id gnat_field_type = Etype (gnat_field);
6415 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6416 tree gnu_field_id = get_entity_name (gnat_field);
6417 tree gnu_field, gnu_size, gnu_pos;
6418 bool is_aliased
6419 = Is_Aliased (gnat_field);
6420 bool is_atomic
6421 = (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type));
6422 bool is_volatile
6423 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6424 bool needs_strict_alignment
6425 = (is_aliased || is_volatile || Strict_Alignment (gnat_field_type));
6427 /* If this field requires strict alignment, we cannot pack it because
6428 it would very likely be under-aligned in the record. */
6429 if (needs_strict_alignment)
6430 packed = 0;
6431 else
6432 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6434 /* If a size is specified, use it. Otherwise, if the record type is packed,
6435 use the official RM size. See "Handling of Type'Size Values" in Einfo
6436 for further details. */
6437 if (Known_Esize (gnat_field))
6438 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6439 gnat_field, FIELD_DECL, false, true);
6440 else if (packed == 1)
6441 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6442 gnat_field, FIELD_DECL, false, true);
6443 else
6444 gnu_size = NULL_TREE;
6446 /* If we have a specified size that is smaller than that of the field's type,
6447 or a position is specified, and the field's type is a record that doesn't
6448 require strict alignment, see if we can get either an integral mode form
6449 of the type or a smaller form. If we can, show a size was specified for
6450 the field if there wasn't one already, so we know to make this a bitfield
6451 and avoid making things wider.
6453 Changing to an integral mode form is useful when the record is packed as
6454 we can then place the field at a non-byte-aligned position and so achieve
6455 tighter packing. This is in addition required if the field shares a byte
6456 with another field and the front-end lets the back-end handle the access
6457 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6459 Changing to a smaller form is required if the specified size is smaller
6460 than that of the field's type and the type contains sub-fields that are
6461 padded, in order to avoid generating accesses to these sub-fields that
6462 are wider than the field.
6464 We avoid the transformation if it is not required or potentially useful,
6465 as it might entail an increase of the field's alignment and have ripple
6466 effects on the outer record type. A typical case is a field known to be
6467 byte-aligned and not to share a byte with another field. */
6468 if (!needs_strict_alignment
6469 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6470 && !TYPE_FAT_POINTER_P (gnu_field_type)
6471 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6472 && (packed == 1
6473 || (gnu_size
6474 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6475 || (Present (Component_Clause (gnat_field))
6476 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6477 % BITS_PER_UNIT == 0
6478 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6480 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6481 if (gnu_packable_type != gnu_field_type)
6483 gnu_field_type = gnu_packable_type;
6484 if (!gnu_size)
6485 gnu_size = rm_size (gnu_field_type);
6489 if (Is_Atomic (gnat_field))
6490 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6492 if (Present (Component_Clause (gnat_field)))
6494 Node_Id gnat_clause = Component_Clause (gnat_field);
6495 Entity_Id gnat_parent
6496 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6498 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6499 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6500 gnat_field, FIELD_DECL, false, true);
6502 /* Ensure the position does not overlap with the parent subtype, if there
6503 is one. This test is omitted if the parent of the tagged type has a
6504 full rep clause since, in this case, component clauses are allowed to
6505 overlay the space allocated for the parent type and the front-end has
6506 checked that there are no overlapping components. */
6507 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6509 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6511 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6512 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6513 post_error_ne_tree
6514 ("offset of& must be beyond parent{, minimum allowed is ^}",
6515 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6518 /* If this field needs strict alignment, make sure that the record is
6519 sufficiently aligned and that the position and size are consistent
6520 with the type. But don't do it if we are just annotating types and
6521 the field's type is tagged, since tagged types aren't fully laid out
6522 in this mode. Also, note that atomic implies volatile so the inner
6523 test sequences ordering is significant here. */
6524 if (needs_strict_alignment
6525 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6527 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6529 if (TYPE_ALIGN (gnu_record_type) < type_align)
6530 TYPE_ALIGN (gnu_record_type) = type_align;
6532 /* If the position is not a multiple of the alignment of the type,
6533 then error out and reset the position. */
6534 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6535 bitsize_int (type_align))))
6537 const char *s;
6539 if (is_atomic)
6540 s = "position of atomic field& must be multiple of ^ bits";
6541 else if (is_aliased)
6542 s = "position of aliased field& must be multiple of ^ bits";
6543 else if (is_volatile)
6544 s = "position of volatile field& must be multiple of ^ bits";
6545 else if (Strict_Alignment (gnat_field_type))
6546 s = "position of & with aliased or tagged part must be"
6547 " multiple of ^ bits";
6548 else
6549 gcc_unreachable ();
6551 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6552 type_align);
6553 gnu_pos = NULL_TREE;
6556 if (gnu_size)
6558 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6559 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6561 /* If the size is lower than that of the type, or greater for
6562 atomic and aliased, then error out and reset the size. */
6563 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6565 const char *s;
6567 if (is_atomic)
6568 s = "size of atomic field& must be ^ bits";
6569 else if (is_aliased)
6570 s = "size of aliased field& must be ^ bits";
6571 else if (is_volatile)
6572 s = "size of volatile field& must be at least ^ bits";
6573 else if (Strict_Alignment (gnat_field_type))
6574 s = "size of & with aliased or tagged part must be"
6575 " at least ^ bits";
6576 else
6577 gcc_unreachable ();
6579 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6580 gnu_type_size);
6581 gnu_size = NULL_TREE;
6584 /* Likewise if the size is not a multiple of a byte, */
6585 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6586 bitsize_unit_node)))
6588 const char *s;
6590 if (is_volatile)
6591 s = "size of volatile field& must be multiple of"
6592 " Storage_Unit";
6593 else if (Strict_Alignment (gnat_field_type))
6594 s = "size of & with aliased or tagged part must be"
6595 " multiple of Storage_Unit";
6596 else
6597 gcc_unreachable ();
6599 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6600 gnu_size = NULL_TREE;
6606 /* If the record has rep clauses and this is the tag field, make a rep
6607 clause for it as well. */
6608 else if (Has_Specified_Layout (Scope (gnat_field))
6609 && Chars (gnat_field) == Name_uTag)
6611 gnu_pos = bitsize_zero_node;
6612 gnu_size = TYPE_SIZE (gnu_field_type);
6615 else
6617 gnu_pos = NULL_TREE;
6619 /* If we are packing the record and the field is BLKmode, round the
6620 size up to a byte boundary. */
6621 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6622 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6625 /* We need to make the size the maximum for the type if it is
6626 self-referential and an unconstrained type. In that case, we can't
6627 pack the field since we can't make a copy to align it. */
6628 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6629 && !gnu_size
6630 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6631 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6633 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6634 packed = 0;
6637 /* If a size is specified, adjust the field's type to it. */
6638 if (gnu_size)
6640 tree orig_field_type;
6642 /* If the field's type is justified modular, we would need to remove
6643 the wrapper to (better) meet the layout requirements. However we
6644 can do so only if the field is not aliased to preserve the unique
6645 layout and if the prescribed size is not greater than that of the
6646 packed array to preserve the justification. */
6647 if (!needs_strict_alignment
6648 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6649 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6650 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6651 <= 0)
6652 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6654 /* Similarly if the field's type is a misaligned integral type, but
6655 there is no restriction on the size as there is no justification. */
6656 if (!needs_strict_alignment
6657 && TYPE_IS_PADDING_P (gnu_field_type)
6658 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6659 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6661 gnu_field_type
6662 = make_type_from_size (gnu_field_type, gnu_size,
6663 Has_Biased_Representation (gnat_field));
6665 orig_field_type = gnu_field_type;
6666 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6667 false, false, definition, true);
6669 /* If a padding record was made, declare it now since it will never be
6670 declared otherwise. This is necessary to ensure that its subtrees
6671 are properly marked. */
6672 if (gnu_field_type != orig_field_type
6673 && !DECL_P (TYPE_NAME (gnu_field_type)))
6674 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6675 debug_info_p, gnat_field);
6678 /* Otherwise (or if there was an error), don't specify a position. */
6679 else
6680 gnu_pos = NULL_TREE;
6682 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6683 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6685 /* Now create the decl for the field. */
6686 gnu_field
6687 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6688 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6689 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6690 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6691 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6693 if (Ekind (gnat_field) == E_Discriminant)
6694 DECL_DISCRIMINANT_NUMBER (gnu_field)
6695 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6697 return gnu_field;
6700 /* Return true if at least one member of COMPONENT_LIST needs strict
6701 alignment. */
6703 static bool
6704 components_need_strict_alignment (Node_Id component_list)
6706 Node_Id component_decl;
6708 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6709 Present (component_decl);
6710 component_decl = Next_Non_Pragma (component_decl))
6712 Entity_Id gnat_field = Defining_Entity (component_decl);
6714 if (Is_Aliased (gnat_field))
6715 return true;
6717 if (Strict_Alignment (Etype (gnat_field)))
6718 return true;
6721 return false;
6724 /* Return true if TYPE is a type with variable size or a padding type with a
6725 field of variable size or a record that has a field with such a type. */
6727 static bool
6728 type_has_variable_size (tree type)
6730 tree field;
6732 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6733 return true;
6735 if (TYPE_IS_PADDING_P (type)
6736 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6737 return true;
6739 if (!RECORD_OR_UNION_TYPE_P (type))
6740 return false;
6742 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6743 if (type_has_variable_size (TREE_TYPE (field)))
6744 return true;
6746 return false;
6749 /* Return true if FIELD is an artificial field. */
6751 static bool
6752 field_is_artificial (tree field)
6754 /* These fields are generated by the front-end proper. */
6755 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6756 return true;
6758 /* These fields are generated by gigi. */
6759 if (DECL_INTERNAL_P (field))
6760 return true;
6762 return false;
6765 /* Return true if FIELD is a non-artificial aliased field. */
6767 static bool
6768 field_is_aliased (tree field)
6770 if (field_is_artificial (field))
6771 return false;
6773 return DECL_ALIASED_P (field);
6776 /* Return true if FIELD is a non-artificial field with self-referential
6777 size. */
6779 static bool
6780 field_has_self_size (tree field)
6782 if (field_is_artificial (field))
6783 return false;
6785 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6786 return false;
6788 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6791 /* Return true if FIELD is a non-artificial field with variable size. */
6793 static bool
6794 field_has_variable_size (tree field)
6796 if (field_is_artificial (field))
6797 return false;
6799 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6800 return false;
6802 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6805 /* qsort comparer for the bit positions of two record components. */
6807 static int
6808 compare_field_bitpos (const PTR rt1, const PTR rt2)
6810 const_tree const field1 = * (const_tree const *) rt1;
6811 const_tree const field2 = * (const_tree const *) rt2;
6812 const int ret
6813 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6815 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6818 /* Structure holding information for a given variant. */
6819 typedef struct vinfo
6821 /* The record type of the variant. */
6822 tree type;
6824 /* The name of the variant. */
6825 tree name;
6827 /* The qualifier of the variant. */
6828 tree qual;
6830 /* Whether the variant has a rep clause. */
6831 bool has_rep;
6833 /* Whether the variant is packed. */
6834 bool packed;
6836 } vinfo_t;
6838 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6839 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6840 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6841 When called from gnat_to_gnu_entity during the processing of a record type
6842 definition, the GCC node for the parent, if any, will be the single field
6843 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6844 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6845 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6847 PACKED is 1 if this is for a packed record, -1 if this is for a record
6848 with Component_Alignment of Storage_Unit, -2 if this is for a record
6849 with a specified alignment.
6851 DEFINITION is true if we are defining this record type.
6853 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6854 out the record. This means the alignment only serves to force fields to
6855 be bitfields, but not to require the record to be that aligned. This is
6856 used for variants.
6858 ALL_REP is true if a rep clause is present for all the fields.
6860 UNCHECKED_UNION is true if we are building this type for a record with a
6861 Pragma Unchecked_Union.
6863 ARTIFICIAL is true if this is a type that was generated by the compiler.
6865 DEBUG_INFO is true if we need to write debug information about the type.
6867 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6868 mean that its contents may be unused as well, only the container itself.
6870 REORDER is true if we are permitted to reorder components of this type.
6872 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6873 the outer record type down to this variant level. It is nonzero only if
6874 all the fields down to this level have a rep clause and ALL_REP is false.
6876 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6877 with a rep clause is to be added; in this case, that is all that should
6878 be done with such fields and the return value will be false. */
6880 static bool
6881 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6882 tree gnu_field_list, int packed, bool definition,
6883 bool cancel_alignment, bool all_rep,
6884 bool unchecked_union, bool artificial,
6885 bool debug_info, bool maybe_unused, bool reorder,
6886 tree first_free_pos, tree *p_gnu_rep_list)
6888 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6889 bool variants_have_rep = all_rep;
6890 bool layout_with_rep = false;
6891 bool has_self_field = false;
6892 bool has_aliased_after_self_field = false;
6893 Node_Id component_decl, variant_part;
6894 tree gnu_field, gnu_next, gnu_last;
6895 tree gnu_variant_part = NULL_TREE;
6896 tree gnu_rep_list = NULL_TREE;
6897 tree gnu_var_list = NULL_TREE;
6898 tree gnu_self_list = NULL_TREE;
6899 tree gnu_zero_list = NULL_TREE;
6901 /* For each component referenced in a component declaration create a GCC
6902 field and add it to the list, skipping pragmas in the GNAT list. */
6903 gnu_last = tree_last (gnu_field_list);
6904 if (Present (Component_Items (gnat_component_list)))
6905 for (component_decl
6906 = First_Non_Pragma (Component_Items (gnat_component_list));
6907 Present (component_decl);
6908 component_decl = Next_Non_Pragma (component_decl))
6910 Entity_Id gnat_field = Defining_Entity (component_decl);
6911 Name_Id gnat_name = Chars (gnat_field);
6913 /* If present, the _Parent field must have been created as the single
6914 field of the record type. Put it before any other fields. */
6915 if (gnat_name == Name_uParent)
6917 gnu_field = TYPE_FIELDS (gnu_record_type);
6918 gnu_field_list = chainon (gnu_field_list, gnu_field);
6920 else
6922 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6923 definition, debug_info);
6925 /* If this is the _Tag field, put it before any other fields. */
6926 if (gnat_name == Name_uTag)
6927 gnu_field_list = chainon (gnu_field_list, gnu_field);
6929 /* If this is the _Controller field, put it before the other
6930 fields except for the _Tag or _Parent field. */
6931 else if (gnat_name == Name_uController && gnu_last)
6933 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6934 DECL_CHAIN (gnu_last) = gnu_field;
6937 /* If this is a regular field, put it after the other fields. */
6938 else
6940 DECL_CHAIN (gnu_field) = gnu_field_list;
6941 gnu_field_list = gnu_field;
6942 if (!gnu_last)
6943 gnu_last = gnu_field;
6945 /* And record information for the final layout. */
6946 if (field_has_self_size (gnu_field))
6947 has_self_field = true;
6948 else if (has_self_field && field_is_aliased (gnu_field))
6949 has_aliased_after_self_field = true;
6953 save_gnu_tree (gnat_field, gnu_field, false);
6956 /* At the end of the component list there may be a variant part. */
6957 variant_part = Variant_Part (gnat_component_list);
6959 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6960 mutually exclusive and should go in the same memory. To do this we need
6961 to treat each variant as a record whose elements are created from the
6962 component list for the variant. So here we create the records from the
6963 lists for the variants and put them all into the QUAL_UNION_TYPE.
6964 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6965 use GNU_RECORD_TYPE if there are no fields so far. */
6966 if (Present (variant_part))
6968 Node_Id gnat_discr = Name (variant_part), variant;
6969 tree gnu_discr = gnat_to_gnu (gnat_discr);
6970 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
6971 tree gnu_var_name
6972 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6973 "XVN");
6974 tree gnu_union_type, gnu_union_name;
6975 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6976 bool union_field_needs_strict_alignment = false;
6977 auto_vec <vinfo_t, 16> variant_types;
6978 vinfo_t *gnu_variant;
6979 unsigned int variants_align = 0;
6980 unsigned int i;
6982 gnu_union_name
6983 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6985 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6986 are all in the variant part, to match the layout of C unions. There
6987 is an associated check below. */
6988 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6989 gnu_union_type = gnu_record_type;
6990 else
6992 gnu_union_type
6993 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6995 TYPE_NAME (gnu_union_type) = gnu_union_name;
6996 TYPE_ALIGN (gnu_union_type) = 0;
6997 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7000 /* If all the fields down to this level have a rep clause, find out
7001 whether all the fields at this level also have one. If so, then
7002 compute the new first free position to be passed downward. */
7003 this_first_free_pos = first_free_pos;
7004 if (this_first_free_pos)
7006 for (gnu_field = gnu_field_list;
7007 gnu_field;
7008 gnu_field = DECL_CHAIN (gnu_field))
7009 if (DECL_FIELD_OFFSET (gnu_field))
7011 tree pos = bit_position (gnu_field);
7012 if (!tree_int_cst_lt (pos, this_first_free_pos))
7013 this_first_free_pos
7014 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7016 else
7018 this_first_free_pos = NULL_TREE;
7019 break;
7023 /* We build the variants in two passes. The bulk of the work is done in
7024 the first pass, that is to say translating the GNAT nodes, building
7025 the container types and computing the associated properties. However
7026 we cannot finish up the container types during this pass because we
7027 don't know where the variant part will be placed until the end. */
7028 for (variant = First_Non_Pragma (Variants (variant_part));
7029 Present (variant);
7030 variant = Next_Non_Pragma (variant))
7032 tree gnu_variant_type = make_node (RECORD_TYPE);
7033 tree gnu_inner_name, gnu_qual;
7034 bool has_rep;
7035 int field_packed;
7036 vinfo_t vinfo;
7038 Get_Variant_Encoding (variant);
7039 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7040 TYPE_NAME (gnu_variant_type)
7041 = concat_name (gnu_union_name,
7042 IDENTIFIER_POINTER (gnu_inner_name));
7044 /* Set the alignment of the inner type in case we need to make
7045 inner objects into bitfields, but then clear it out so the
7046 record actually gets only the alignment required. */
7047 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7048 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7050 /* Similarly, if the outer record has a size specified and all
7051 the fields have a rep clause, we can propagate the size. */
7052 if (all_rep_and_size)
7054 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7055 TYPE_SIZE_UNIT (gnu_variant_type)
7056 = TYPE_SIZE_UNIT (gnu_record_type);
7059 /* Add the fields into the record type for the variant. Note that
7060 we aren't sure to really use it at this point, see below. */
7061 has_rep
7062 = components_to_record (gnu_variant_type, Component_List (variant),
7063 NULL_TREE, packed, definition,
7064 !all_rep_and_size, all_rep,
7065 unchecked_union,
7066 true, debug_info, true, reorder,
7067 this_first_free_pos,
7068 all_rep || this_first_free_pos
7069 ? NULL : &gnu_rep_list);
7071 /* Translate the qualifier and annotate the GNAT node. */
7072 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7073 Set_Present_Expr (variant, annotate_value (gnu_qual));
7075 /* Deal with packedness like in gnat_to_gnu_field. */
7076 if (components_need_strict_alignment (Component_List (variant)))
7078 field_packed = 0;
7079 union_field_needs_strict_alignment = true;
7081 else
7082 field_packed
7083 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7085 /* Push this variant onto the stack for the second pass. */
7086 vinfo.type = gnu_variant_type;
7087 vinfo.name = gnu_inner_name;
7088 vinfo.qual = gnu_qual;
7089 vinfo.has_rep = has_rep;
7090 vinfo.packed = field_packed;
7091 variant_types.safe_push (vinfo);
7093 /* Compute the global properties that will determine the placement of
7094 the variant part. */
7095 variants_have_rep |= has_rep;
7096 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7097 variants_align = TYPE_ALIGN (gnu_variant_type);
7100 /* Round up the first free position to the alignment of the variant part
7101 for the variants without rep clause. This will guarantee a consistent
7102 layout independently of the placement of the variant part. */
7103 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7104 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7106 /* In the second pass, the container types are adjusted if necessary and
7107 finished up, then the corresponding fields of the variant part are
7108 built with their qualifier, unless this is an unchecked union. */
7109 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7111 tree gnu_variant_type = gnu_variant->type;
7112 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7114 /* If this is an Unchecked_Union whose fields are all in the variant
7115 part and we have a single field with no representation clause or
7116 placed at offset zero, use the field directly to match the layout
7117 of C unions. */
7118 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7119 && gnu_field_list
7120 && !DECL_CHAIN (gnu_field_list)
7121 && (!DECL_FIELD_OFFSET (gnu_field_list)
7122 || integer_zerop (bit_position (gnu_field_list))))
7124 gnu_field = gnu_field_list;
7125 DECL_CONTEXT (gnu_field) = gnu_record_type;
7127 else
7129 /* Finalize the variant type now. We used to throw away empty
7130 record types but we no longer do that because we need them to
7131 generate complete debug info for the variant; otherwise, the
7132 union type definition will be lacking the fields associated
7133 with these empty variants. */
7134 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7136 /* The variant part will be at offset 0 so we need to ensure
7137 that the fields are laid out starting from the first free
7138 position at this level. */
7139 tree gnu_rep_type = make_node (RECORD_TYPE);
7140 tree gnu_rep_part;
7141 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7142 gnu_rep_part
7143 = create_rep_part (gnu_rep_type, gnu_variant_type,
7144 this_first_free_pos);
7145 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7146 gnu_field_list = gnu_rep_part;
7147 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7148 false);
7151 if (debug_info)
7152 rest_of_record_type_compilation (gnu_variant_type);
7153 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7154 true, debug_info, gnat_component_list);
7156 gnu_field
7157 = create_field_decl (gnu_variant->name, gnu_variant_type,
7158 gnu_union_type,
7159 all_rep_and_size
7160 ? TYPE_SIZE (gnu_variant_type) : 0,
7161 variants_have_rep ? bitsize_zero_node : 0,
7162 gnu_variant->packed, 0);
7164 DECL_INTERNAL_P (gnu_field) = 1;
7166 if (!unchecked_union)
7167 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7170 DECL_CHAIN (gnu_field) = gnu_variant_list;
7171 gnu_variant_list = gnu_field;
7174 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7175 if (gnu_variant_list)
7177 int union_field_packed;
7179 if (all_rep_and_size)
7181 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7182 TYPE_SIZE_UNIT (gnu_union_type)
7183 = TYPE_SIZE_UNIT (gnu_record_type);
7186 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7187 all_rep_and_size ? 1 : 0, debug_info);
7189 /* If GNU_UNION_TYPE is our record type, it means we must have an
7190 Unchecked_Union with no fields. Verify that and, if so, just
7191 return. */
7192 if (gnu_union_type == gnu_record_type)
7194 gcc_assert (unchecked_union
7195 && !gnu_field_list
7196 && !gnu_rep_list);
7197 return variants_have_rep;
7200 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7201 debug_info, gnat_component_list);
7203 /* Deal with packedness like in gnat_to_gnu_field. */
7204 if (union_field_needs_strict_alignment)
7205 union_field_packed = 0;
7206 else
7207 union_field_packed
7208 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7210 gnu_variant_part
7211 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7212 all_rep_and_size
7213 ? TYPE_SIZE (gnu_union_type) : 0,
7214 variants_have_rep ? bitsize_zero_node : 0,
7215 union_field_packed, 0);
7217 DECL_INTERNAL_P (gnu_variant_part) = 1;
7221 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7222 permitted to reorder components, self-referential sizes or variable sizes.
7223 If they do, pull them out and put them onto the appropriate list. We have
7224 to do this in a separate pass since we want to handle the discriminants
7225 but can't play with them until we've used them in debugging data above.
7227 Similarly, pull out the fields with zero size and no rep clause, as they
7228 would otherwise modify the layout and thus very likely run afoul of the
7229 Ada semantics, which are different from those of C here.
7231 ??? If we reorder them, debugging information will be wrong but there is
7232 nothing that can be done about this at the moment. */
7233 gnu_last = NULL_TREE;
7235 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7236 do { \
7237 if (gnu_last) \
7238 DECL_CHAIN (gnu_last) = gnu_next; \
7239 else \
7240 gnu_field_list = gnu_next; \
7242 DECL_CHAIN (gnu_field) = (LIST); \
7243 (LIST) = gnu_field; \
7244 } while (0)
7246 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7248 gnu_next = DECL_CHAIN (gnu_field);
7250 if (DECL_FIELD_OFFSET (gnu_field))
7252 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7253 continue;
7256 if ((reorder || has_aliased_after_self_field)
7257 && field_has_self_size (gnu_field))
7259 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7260 continue;
7263 if (reorder && field_has_variable_size (gnu_field))
7265 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7266 continue;
7269 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7271 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7272 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7273 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7274 if (field_is_aliased (gnu_field))
7275 TYPE_ALIGN (gnu_record_type)
7276 = MAX (TYPE_ALIGN (gnu_record_type),
7277 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7278 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7279 continue;
7282 gnu_last = gnu_field;
7285 #undef MOVE_FROM_FIELD_LIST_TO
7287 gnu_field_list = nreverse (gnu_field_list);
7289 /* If permitted, we reorder the fields as follows:
7291 1) all fixed length fields,
7292 2) all fields whose length doesn't depend on discriminants,
7293 3) all fields whose length depends on discriminants,
7294 4) the variant part,
7296 within the record and within each variant recursively. */
7297 if (reorder)
7298 gnu_field_list
7299 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7301 /* Otherwise, if there is an aliased field placed after a field whose length
7302 depends on discriminants, we put all the fields of the latter sort, last.
7303 We need to do this in case an object of this record type is mutable. */
7304 else if (has_aliased_after_self_field)
7305 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7307 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7308 in our REP list to the previous level because this level needs them in
7309 order to do a correct layout, i.e. avoid having overlapping fields. */
7310 if (p_gnu_rep_list && gnu_rep_list)
7311 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7313 /* Otherwise, sort the fields by bit position and put them into their own
7314 record, before the others, if we also have fields without rep clause. */
7315 else if (gnu_rep_list)
7317 tree gnu_rep_type, gnu_rep_part;
7318 int i, len = list_length (gnu_rep_list);
7319 tree *gnu_arr = XALLOCAVEC (tree, len);
7321 /* If all the fields have a rep clause, we can do a flat layout. */
7322 layout_with_rep = !gnu_field_list
7323 && (!gnu_variant_part || variants_have_rep);
7324 gnu_rep_type
7325 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7327 for (gnu_field = gnu_rep_list, i = 0;
7328 gnu_field;
7329 gnu_field = DECL_CHAIN (gnu_field), i++)
7330 gnu_arr[i] = gnu_field;
7332 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7334 /* Put the fields in the list in order of increasing position, which
7335 means we start from the end. */
7336 gnu_rep_list = NULL_TREE;
7337 for (i = len - 1; i >= 0; i--)
7339 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7340 gnu_rep_list = gnu_arr[i];
7341 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7344 if (layout_with_rep)
7345 gnu_field_list = gnu_rep_list;
7346 else
7348 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7350 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7351 without rep clause are laid out starting from this position.
7352 Therefore, we force it as a minimal size on the REP part. */
7353 gnu_rep_part
7354 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7356 /* Chain the REP part at the beginning of the field list. */
7357 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7358 gnu_field_list = gnu_rep_part;
7362 /* Chain the variant part at the end of the field list. */
7363 if (gnu_variant_part)
7364 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7366 if (cancel_alignment)
7367 TYPE_ALIGN (gnu_record_type) = 0;
7369 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7371 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7372 debug_info && !maybe_unused);
7374 /* Chain the fields with zero size at the beginning of the field list. */
7375 if (gnu_zero_list)
7376 TYPE_FIELDS (gnu_record_type)
7377 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7379 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7382 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7383 placed into an Esize, Component_Bit_Offset, or Component_Size value
7384 in the GNAT tree. */
7386 static Uint
7387 annotate_value (tree gnu_size)
7389 TCode tcode;
7390 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7391 struct tree_int_map in;
7392 int i;
7394 /* See if we've already saved the value for this node. */
7395 if (EXPR_P (gnu_size))
7397 struct tree_int_map *e;
7399 in.base.from = gnu_size;
7400 e = annotate_value_cache->find (&in);
7402 if (e)
7403 return (Node_Ref_Or_Val) e->to;
7405 else
7406 in.base.from = NULL_TREE;
7408 /* If we do not return inside this switch, TCODE will be set to the
7409 code to use for a Create_Node operand and LEN (set above) will be
7410 the number of recursive calls for us to make. */
7412 switch (TREE_CODE (gnu_size))
7414 case INTEGER_CST:
7415 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7417 case COMPONENT_REF:
7418 /* The only case we handle here is a simple discriminant reference. */
7419 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7421 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7423 /* Climb up the chain of successive extensions, if any. */
7424 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7425 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7426 == parent_name_id)
7427 gnu_size = TREE_OPERAND (gnu_size, 0);
7429 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7430 return
7431 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7434 return No_Uint;
7436 CASE_CONVERT: case NON_LVALUE_EXPR:
7437 return annotate_value (TREE_OPERAND (gnu_size, 0));
7439 /* Now just list the operations we handle. */
7440 case COND_EXPR: tcode = Cond_Expr; break;
7441 case PLUS_EXPR: tcode = Plus_Expr; break;
7442 case MINUS_EXPR: tcode = Minus_Expr; break;
7443 case MULT_EXPR: tcode = Mult_Expr; break;
7444 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7445 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7446 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7447 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7448 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7449 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7450 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7451 case NEGATE_EXPR: tcode = Negate_Expr; break;
7452 case MIN_EXPR: tcode = Min_Expr; break;
7453 case MAX_EXPR: tcode = Max_Expr; break;
7454 case ABS_EXPR: tcode = Abs_Expr; break;
7455 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7456 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7457 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7458 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7459 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7460 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7461 case LT_EXPR: tcode = Lt_Expr; break;
7462 case LE_EXPR: tcode = Le_Expr; break;
7463 case GT_EXPR: tcode = Gt_Expr; break;
7464 case GE_EXPR: tcode = Ge_Expr; break;
7465 case EQ_EXPR: tcode = Eq_Expr; break;
7466 case NE_EXPR: tcode = Ne_Expr; break;
7468 case BIT_AND_EXPR:
7469 tcode = Bit_And_Expr;
7470 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7471 Such values appear in expressions with aligning patterns. Note that,
7472 since sizetype is unsigned, we have to jump through some hoops. */
7473 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7475 tree op1 = TREE_OPERAND (gnu_size, 1);
7476 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7477 if (wi::neg_p (signed_op1))
7479 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7480 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7483 break;
7485 case CALL_EXPR:
7486 /* In regular mode, inline back only if symbolic annotation is requested
7487 in order to avoid memory explosion on big discriminated record types.
7488 But not in ASIS mode, as symbolic annotation is required for DDA. */
7489 if (List_Representation_Info == 3 || type_annotate_only)
7491 tree t = maybe_inline_call_in_expr (gnu_size);
7492 if (t)
7493 return annotate_value (t);
7495 else
7496 return Uint_Minus_1;
7498 /* Fall through... */
7500 default:
7501 return No_Uint;
7504 /* Now get each of the operands that's relevant for this code. If any
7505 cannot be expressed as a repinfo node, say we can't. */
7506 for (i = 0; i < 3; i++)
7507 ops[i] = No_Uint;
7509 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7511 if (i == 1 && pre_op1 != No_Uint)
7512 ops[i] = pre_op1;
7513 else
7514 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7515 if (ops[i] == No_Uint)
7516 return No_Uint;
7519 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7521 /* Save the result in the cache. */
7522 if (in.base.from)
7524 struct tree_int_map **h;
7525 /* We can't assume the hash table data hasn't moved since the initial
7526 look up, so we have to search again. Allocating and inserting an
7527 entry at that point would be an alternative, but then we'd better
7528 discard the entry if we decided not to cache it. */
7529 h = annotate_value_cache->find_slot (&in, INSERT);
7530 gcc_assert (!*h);
7531 *h = ggc_alloc<tree_int_map> ();
7532 (*h)->base.from = gnu_size;
7533 (*h)->to = ret;
7536 return ret;
7539 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7540 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7541 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7542 BY_REF is true if the object is used by reference. */
7544 void
7545 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7547 if (by_ref)
7549 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7550 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7551 else
7552 gnu_type = TREE_TYPE (gnu_type);
7555 if (Unknown_Esize (gnat_entity))
7557 if (TREE_CODE (gnu_type) == RECORD_TYPE
7558 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7559 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7560 else if (!size)
7561 size = TYPE_SIZE (gnu_type);
7563 if (size)
7564 Set_Esize (gnat_entity, annotate_value (size));
7567 if (Unknown_Alignment (gnat_entity))
7568 Set_Alignment (gnat_entity,
7569 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7572 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7573 Return NULL_TREE if there is no such element in the list. */
7575 static tree
7576 purpose_member_field (const_tree elem, tree list)
7578 while (list)
7580 tree field = TREE_PURPOSE (list);
7581 if (SAME_FIELD_P (field, elem))
7582 return list;
7583 list = TREE_CHAIN (list);
7585 return NULL_TREE;
7588 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7589 set Component_Bit_Offset and Esize of the components to the position and
7590 size used by Gigi. */
7592 static void
7593 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7595 Entity_Id gnat_field;
7596 tree gnu_list;
7598 /* We operate by first making a list of all fields and their position (we
7599 can get the size easily) and then update all the sizes in the tree. */
7600 gnu_list
7601 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7602 BIGGEST_ALIGNMENT, NULL_TREE);
7604 for (gnat_field = First_Entity (gnat_entity);
7605 Present (gnat_field);
7606 gnat_field = Next_Entity (gnat_field))
7607 if (Ekind (gnat_field) == E_Component
7608 || (Ekind (gnat_field) == E_Discriminant
7609 && !Is_Unchecked_Union (Scope (gnat_field))))
7611 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7612 gnu_list);
7613 if (t)
7615 tree parent_offset;
7617 /* If we are just annotating types and the type is tagged, the tag
7618 and the parent components are not generated by the front-end so
7619 we need to add the appropriate offset to each component without
7620 representation clause. */
7621 if (type_annotate_only
7622 && Is_Tagged_Type (gnat_entity)
7623 && No (Component_Clause (gnat_field)))
7625 /* For a component appearing in the current extension, the
7626 offset is the size of the parent. */
7627 if (Is_Derived_Type (gnat_entity)
7628 && Original_Record_Component (gnat_field) == gnat_field)
7629 parent_offset
7630 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7631 bitsizetype);
7632 else
7633 parent_offset = bitsize_int (POINTER_SIZE);
7635 if (TYPE_FIELDS (gnu_type))
7636 parent_offset
7637 = round_up (parent_offset,
7638 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7640 else
7641 parent_offset = bitsize_zero_node;
7643 Set_Component_Bit_Offset
7644 (gnat_field,
7645 annotate_value
7646 (size_binop (PLUS_EXPR,
7647 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7648 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7649 parent_offset)));
7651 Set_Esize (gnat_field,
7652 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7654 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7656 /* If there is no entry, this is an inherited component whose
7657 position is the same as in the parent type. */
7658 Set_Component_Bit_Offset
7659 (gnat_field,
7660 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7662 Set_Esize (gnat_field,
7663 Esize (Original_Record_Component (gnat_field)));
7668 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7669 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7670 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7671 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7672 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7673 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7674 pre-existing list to be chained to the newly created entries. */
7676 static tree
7677 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7678 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7680 tree gnu_field;
7682 for (gnu_field = TYPE_FIELDS (gnu_type);
7683 gnu_field;
7684 gnu_field = DECL_CHAIN (gnu_field))
7686 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7687 DECL_FIELD_BIT_OFFSET (gnu_field));
7688 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7689 DECL_FIELD_OFFSET (gnu_field));
7690 unsigned int our_offset_align
7691 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7692 tree v = make_tree_vec (3);
7694 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7695 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7696 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7697 gnu_list = tree_cons (gnu_field, v, gnu_list);
7699 /* Recurse on internal fields, flattening the nested fields except for
7700 those in the variant part, if requested. */
7701 if (DECL_INTERNAL_P (gnu_field))
7703 tree gnu_field_type = TREE_TYPE (gnu_field);
7704 if (do_not_flatten_variant
7705 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7706 gnu_list
7707 = build_position_list (gnu_field_type, do_not_flatten_variant,
7708 size_zero_node, bitsize_zero_node,
7709 BIGGEST_ALIGNMENT, gnu_list);
7710 else
7711 gnu_list
7712 = build_position_list (gnu_field_type, do_not_flatten_variant,
7713 gnu_our_offset, gnu_our_bitpos,
7714 our_offset_align, gnu_list);
7718 return gnu_list;
7721 /* Return a list describing the substitutions needed to reflect the
7722 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7723 be in any order. The values in an element of the list are in the form
7724 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7725 a definition of GNAT_SUBTYPE. */
7727 static vec<subst_pair>
7728 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7730 vec<subst_pair> gnu_list = vNULL;
7731 Entity_Id gnat_discrim;
7732 Node_Id gnat_constr;
7734 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7735 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7736 Present (gnat_discrim);
7737 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7738 gnat_constr = Next_Elmt (gnat_constr))
7739 /* Ignore access discriminants. */
7740 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7742 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7743 tree replacement = convert (TREE_TYPE (gnu_field),
7744 elaborate_expression
7745 (Node (gnat_constr), gnat_subtype,
7746 get_entity_name (gnat_discrim),
7747 definition, true, false));
7748 subst_pair s = {gnu_field, replacement};
7749 gnu_list.safe_push (s);
7752 return gnu_list;
7755 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7756 variants of QUAL_UNION_TYPE that are still relevant after applying
7757 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7758 list to be prepended to the newly created entries. */
7760 static vec<variant_desc>
7761 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7762 vec<variant_desc> gnu_list)
7764 tree gnu_field;
7766 for (gnu_field = TYPE_FIELDS (qual_union_type);
7767 gnu_field;
7768 gnu_field = DECL_CHAIN (gnu_field))
7770 tree qual = DECL_QUALIFIER (gnu_field);
7771 unsigned int i;
7772 subst_pair *s;
7774 FOR_EACH_VEC_ELT (subst_list, i, s)
7775 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7777 /* If the new qualifier is not unconditionally false, its variant may
7778 still be accessed. */
7779 if (!integer_zerop (qual))
7781 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7782 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7784 gnu_list.safe_push (v);
7786 /* Recurse on the variant subpart of the variant, if any. */
7787 variant_subpart = get_variant_part (variant_type);
7788 if (variant_subpart)
7789 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7790 subst_list, gnu_list);
7792 /* If the new qualifier is unconditionally true, the subsequent
7793 variants cannot be accessed. */
7794 if (integer_onep (qual))
7795 break;
7799 return gnu_list;
7802 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7803 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7804 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7805 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7806 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7807 true if we are being called to process the Component_Size of GNAT_OBJECT;
7808 this is used only for error messages. ZERO_OK is true if a size of zero
7809 is permitted; if ZERO_OK is false, it means that a size of zero should be
7810 treated as an unspecified size. */
7812 static tree
7813 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7814 enum tree_code kind, bool component_p, bool zero_ok)
7816 Node_Id gnat_error_node;
7817 tree type_size, size;
7819 /* Return 0 if no size was specified. */
7820 if (uint_size == No_Uint)
7821 return NULL_TREE;
7823 /* Ignore a negative size since that corresponds to our back-annotation. */
7824 if (UI_Lt (uint_size, Uint_0))
7825 return NULL_TREE;
7827 /* Find the node to use for error messages. */
7828 if ((Ekind (gnat_object) == E_Component
7829 || Ekind (gnat_object) == E_Discriminant)
7830 && Present (Component_Clause (gnat_object)))
7831 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7832 else if (Present (Size_Clause (gnat_object)))
7833 gnat_error_node = Expression (Size_Clause (gnat_object));
7834 else
7835 gnat_error_node = gnat_object;
7837 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7838 but cannot be represented in bitsizetype. */
7839 size = UI_To_gnu (uint_size, bitsizetype);
7840 if (TREE_OVERFLOW (size))
7842 if (component_p)
7843 post_error_ne ("component size for& is too large", gnat_error_node,
7844 gnat_object);
7845 else
7846 post_error_ne ("size for& is too large", gnat_error_node,
7847 gnat_object);
7848 return NULL_TREE;
7851 /* Ignore a zero size if it is not permitted. */
7852 if (!zero_ok && integer_zerop (size))
7853 return NULL_TREE;
7855 /* The size of objects is always a multiple of a byte. */
7856 if (kind == VAR_DECL
7857 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7859 if (component_p)
7860 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7861 gnat_error_node, gnat_object);
7862 else
7863 post_error_ne ("size for& is not a multiple of Storage_Unit",
7864 gnat_error_node, gnat_object);
7865 return NULL_TREE;
7868 /* If this is an integral type or a packed array type, the front-end has
7869 already verified the size, so we need not do it here (which would mean
7870 checking against the bounds). However, if this is an aliased object,
7871 it may not be smaller than the type of the object. */
7872 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7873 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7874 return size;
7876 /* If the object is a record that contains a template, add the size of the
7877 template to the specified size. */
7878 if (TREE_CODE (gnu_type) == RECORD_TYPE
7879 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7880 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7882 if (kind == VAR_DECL
7883 /* If a type needs strict alignment, a component of this type in
7884 a packed record cannot be packed and thus uses the type size. */
7885 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7886 type_size = TYPE_SIZE (gnu_type);
7887 else
7888 type_size = rm_size (gnu_type);
7890 /* Modify the size of a discriminated type to be the maximum size. */
7891 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7892 type_size = max_size (type_size, true);
7894 /* If this is an access type or a fat pointer, the minimum size is that given
7895 by the smallest integral mode that's valid for pointers. */
7896 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7898 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7899 while (!targetm.valid_pointer_mode (p_mode))
7900 p_mode = GET_MODE_WIDER_MODE (p_mode);
7901 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7904 /* Issue an error either if the default size of the object isn't a constant
7905 or if the new size is smaller than it. */
7906 if (TREE_CODE (type_size) != INTEGER_CST
7907 || TREE_OVERFLOW (type_size)
7908 || tree_int_cst_lt (size, type_size))
7910 if (component_p)
7911 post_error_ne_tree
7912 ("component size for& too small{, minimum allowed is ^}",
7913 gnat_error_node, gnat_object, type_size);
7914 else
7915 post_error_ne_tree
7916 ("size for& too small{, minimum allowed is ^}",
7917 gnat_error_node, gnat_object, type_size);
7918 return NULL_TREE;
7921 return size;
7924 /* Similarly, but both validate and process a value of RM size. This routine
7925 is only called for types. */
7927 static void
7928 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7930 Node_Id gnat_attr_node;
7931 tree old_size, size;
7933 /* Do nothing if no size was specified. */
7934 if (uint_size == No_Uint)
7935 return;
7937 /* Ignore a negative size since that corresponds to our back-annotation. */
7938 if (UI_Lt (uint_size, Uint_0))
7939 return;
7941 /* Only issue an error if a Value_Size clause was explicitly given.
7942 Otherwise, we'd be duplicating an error on the Size clause. */
7943 gnat_attr_node
7944 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7946 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7947 but cannot be represented in bitsizetype. */
7948 size = UI_To_gnu (uint_size, bitsizetype);
7949 if (TREE_OVERFLOW (size))
7951 if (Present (gnat_attr_node))
7952 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7953 gnat_entity);
7954 return;
7957 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7958 exists, or this is an integer type, in which case the front-end will
7959 have always set it. */
7960 if (No (gnat_attr_node)
7961 && integer_zerop (size)
7962 && !Has_Size_Clause (gnat_entity)
7963 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7964 return;
7966 old_size = rm_size (gnu_type);
7968 /* If the old size is self-referential, get the maximum size. */
7969 if (CONTAINS_PLACEHOLDER_P (old_size))
7970 old_size = max_size (old_size, true);
7972 /* Issue an error either if the old size of the object isn't a constant or
7973 if the new size is smaller than it. The front-end has already verified
7974 this for scalar and packed array types. */
7975 if (TREE_CODE (old_size) != INTEGER_CST
7976 || TREE_OVERFLOW (old_size)
7977 || (AGGREGATE_TYPE_P (gnu_type)
7978 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7979 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7980 && !(TYPE_IS_PADDING_P (gnu_type)
7981 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7982 && TYPE_PACKED_ARRAY_TYPE_P
7983 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7984 && tree_int_cst_lt (size, old_size)))
7986 if (Present (gnat_attr_node))
7987 post_error_ne_tree
7988 ("Value_Size for& too small{, minimum allowed is ^}",
7989 gnat_attr_node, gnat_entity, old_size);
7990 return;
7993 /* Otherwise, set the RM size proper for integral types... */
7994 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7995 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7996 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7997 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7998 SET_TYPE_RM_SIZE (gnu_type, size);
8000 /* ...or the Ada size for record and union types. */
8001 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8002 && !TYPE_FAT_POINTER_P (gnu_type))
8003 SET_TYPE_ADA_SIZE (gnu_type, size);
8006 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8007 a type or object whose present alignment is ALIGN. If this alignment is
8008 valid, return it. Otherwise, give an error and return ALIGN. */
8010 static unsigned int
8011 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8013 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8014 unsigned int new_align;
8015 Node_Id gnat_error_node;
8017 /* Don't worry about checking alignment if alignment was not specified
8018 by the source program and we already posted an error for this entity. */
8019 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8020 return align;
8022 /* Post the error on the alignment clause if any. Note, for the implicit
8023 base type of an array type, the alignment clause is on the first
8024 subtype. */
8025 if (Present (Alignment_Clause (gnat_entity)))
8026 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8028 else if (Is_Itype (gnat_entity)
8029 && Is_Array_Type (gnat_entity)
8030 && Etype (gnat_entity) == gnat_entity
8031 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8032 gnat_error_node =
8033 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8035 else
8036 gnat_error_node = gnat_entity;
8038 /* Within GCC, an alignment is an integer, so we must make sure a value is
8039 specified that fits in that range. Also, there is an upper bound to
8040 alignments we can support/allow. */
8041 if (!UI_Is_In_Int_Range (alignment)
8042 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8043 post_error_ne_num ("largest supported alignment for& is ^",
8044 gnat_error_node, gnat_entity, max_allowed_alignment);
8045 else if (!(Present (Alignment_Clause (gnat_entity))
8046 && From_At_Mod (Alignment_Clause (gnat_entity)))
8047 && new_align * BITS_PER_UNIT < align)
8049 unsigned int double_align;
8050 bool is_capped_double, align_clause;
8052 /* If the default alignment of "double" or larger scalar types is
8053 specifically capped and the new alignment is above the cap, do
8054 not post an error and change the alignment only if there is an
8055 alignment clause; this makes it possible to have the associated
8056 GCC type overaligned by default for performance reasons. */
8057 if ((double_align = double_float_alignment) > 0)
8059 Entity_Id gnat_type
8060 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8061 is_capped_double
8062 = is_double_float_or_array (gnat_type, &align_clause);
8064 else if ((double_align = double_scalar_alignment) > 0)
8066 Entity_Id gnat_type
8067 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8068 is_capped_double
8069 = is_double_scalar_or_array (gnat_type, &align_clause);
8071 else
8072 is_capped_double = align_clause = false;
8074 if (is_capped_double && new_align >= double_align)
8076 if (align_clause)
8077 align = new_align * BITS_PER_UNIT;
8079 else
8081 if (is_capped_double)
8082 align = double_align * BITS_PER_UNIT;
8084 post_error_ne_num ("alignment for& must be at least ^",
8085 gnat_error_node, gnat_entity,
8086 align / BITS_PER_UNIT);
8089 else
8091 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8092 if (new_align > align)
8093 align = new_align;
8096 return align;
8099 /* Verify that TYPE is something we can implement atomically. If not, issue
8100 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8101 process a component type. */
8103 static void
8104 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8106 Node_Id gnat_error_point = gnat_entity;
8107 Node_Id gnat_node;
8108 machine_mode mode;
8109 enum mode_class mclass;
8110 unsigned int align;
8111 tree size;
8113 /* If this is an anonymous base type, nothing to check, the error will be
8114 reported on the source type if need be. */
8115 if (!Comes_From_Source (gnat_entity))
8116 return;
8118 mode = TYPE_MODE (type);
8119 mclass = GET_MODE_CLASS (mode);
8120 align = TYPE_ALIGN (type);
8121 size = TYPE_SIZE (type);
8123 /* Consider all aligned floating-point types atomic and any aligned types
8124 that are represented by integers no wider than a machine word. */
8125 if ((mclass == MODE_FLOAT
8126 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8127 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8128 && align >= GET_MODE_ALIGNMENT (mode))
8129 return;
8131 /* For the moment, also allow anything that has an alignment equal to its
8132 size and which is smaller than a word. */
8133 if (size
8134 && TREE_CODE (size) == INTEGER_CST
8135 && compare_tree_int (size, align) == 0
8136 && align <= BITS_PER_WORD)
8137 return;
8139 for (gnat_node = First_Rep_Item (gnat_entity);
8140 Present (gnat_node);
8141 gnat_node = Next_Rep_Item (gnat_node))
8142 if (Nkind (gnat_node) == N_Pragma)
8144 unsigned char pragma_id
8145 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8147 if ((pragma_id == Pragma_Atomic && !component_p)
8148 || (pragma_id == Pragma_Atomic_Components && component_p))
8150 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8151 break;
8155 if (component_p)
8156 post_error_ne ("atomic access to component of & cannot be guaranteed",
8157 gnat_error_point, gnat_entity);
8158 else
8159 post_error_ne ("atomic access to & cannot be guaranteed",
8160 gnat_error_point, gnat_entity);
8164 /* Helper for the intrin compatibility checks family. Evaluate whether
8165 two types are definitely incompatible. */
8167 static bool
8168 intrin_types_incompatible_p (tree t1, tree t2)
8170 enum tree_code code;
8172 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8173 return false;
8175 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8176 return true;
8178 if (TREE_CODE (t1) != TREE_CODE (t2))
8179 return true;
8181 code = TREE_CODE (t1);
8183 switch (code)
8185 case INTEGER_TYPE:
8186 case REAL_TYPE:
8187 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8189 case POINTER_TYPE:
8190 case REFERENCE_TYPE:
8191 /* Assume designated types are ok. We'd need to account for char * and
8192 void * variants to do better, which could rapidly get messy and isn't
8193 clearly worth the effort. */
8194 return false;
8196 default:
8197 break;
8200 return false;
8203 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8204 on the Ada/builtin argument lists for the INB binding. */
8206 static bool
8207 intrin_arglists_compatible_p (intrin_binding_t * inb)
8209 function_args_iterator ada_iter, btin_iter;
8211 function_args_iter_init (&ada_iter, inb->ada_fntype);
8212 function_args_iter_init (&btin_iter, inb->btin_fntype);
8214 /* Sequence position of the last argument we checked. */
8215 int argpos = 0;
8217 while (1)
8219 tree ada_type = function_args_iter_cond (&ada_iter);
8220 tree btin_type = function_args_iter_cond (&btin_iter);
8222 /* If we've exhausted both lists simultaneously, we're done. */
8223 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8224 break;
8226 /* If one list is shorter than the other, they fail to match. */
8227 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8228 return false;
8230 /* If we're done with the Ada args and not with the internal builtin
8231 args, or the other way around, complain. */
8232 if (ada_type == void_type_node
8233 && btin_type != void_type_node)
8235 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8236 return false;
8239 if (btin_type == void_type_node
8240 && ada_type != void_type_node)
8242 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8243 inb->gnat_entity, inb->gnat_entity, argpos);
8244 return false;
8247 /* Otherwise, check that types match for the current argument. */
8248 argpos ++;
8249 if (intrin_types_incompatible_p (ada_type, btin_type))
8251 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8252 inb->gnat_entity, inb->gnat_entity, argpos);
8253 return false;
8257 function_args_iter_next (&ada_iter);
8258 function_args_iter_next (&btin_iter);
8261 return true;
8264 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8265 on the Ada/builtin return values for the INB binding. */
8267 static bool
8268 intrin_return_compatible_p (intrin_binding_t * inb)
8270 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8271 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8273 /* Accept function imported as procedure, common and convenient. */
8274 if (VOID_TYPE_P (ada_return_type)
8275 && !VOID_TYPE_P (btin_return_type))
8276 return true;
8278 /* If return type is Address (integer type), map it to void *. */
8279 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8280 ada_return_type = ptr_void_type_node;
8282 /* Check return types compatibility otherwise. Note that this
8283 handles void/void as well. */
8284 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8286 post_error ("?intrinsic binding type mismatch on return value!",
8287 inb->gnat_entity);
8288 return false;
8291 return true;
8294 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8295 compatible. Issue relevant warnings when they are not.
8297 This is intended as a light check to diagnose the most obvious cases, not
8298 as a full fledged type compatibility predicate. It is the programmer's
8299 responsibility to ensure correctness of the Ada declarations in Imports,
8300 especially when binding straight to a compiler internal. */
8302 static bool
8303 intrin_profiles_compatible_p (intrin_binding_t * inb)
8305 /* Check compatibility on return values and argument lists, each responsible
8306 for posting warnings as appropriate. Ensure use of the proper sloc for
8307 this purpose. */
8309 bool arglists_compatible_p, return_compatible_p;
8310 location_t saved_location = input_location;
8312 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8314 return_compatible_p = intrin_return_compatible_p (inb);
8315 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8317 input_location = saved_location;
8319 return return_compatible_p && arglists_compatible_p;
8322 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8323 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8324 specified size for this field. POS_LIST is a position list describing
8325 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8326 to this layout. */
8328 static tree
8329 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8330 tree size, tree pos_list,
8331 vec<subst_pair> subst_list)
8333 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8334 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8335 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8336 tree new_pos, new_field;
8337 unsigned int i;
8338 subst_pair *s;
8340 if (CONTAINS_PLACEHOLDER_P (pos))
8341 FOR_EACH_VEC_ELT (subst_list, i, s)
8342 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8344 /* If the position is now a constant, we can set it as the position of the
8345 field when we make it. Otherwise, we need to deal with it specially. */
8346 if (TREE_CONSTANT (pos))
8347 new_pos = bit_from_pos (pos, bitpos);
8348 else
8349 new_pos = NULL_TREE;
8351 new_field
8352 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8353 size, new_pos, DECL_PACKED (old_field),
8354 !DECL_NONADDRESSABLE_P (old_field));
8356 if (!new_pos)
8358 normalize_offset (&pos, &bitpos, offset_align);
8359 /* Finalize the position. */
8360 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8361 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8362 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8363 DECL_SIZE (new_field) = size;
8364 DECL_SIZE_UNIT (new_field)
8365 = convert (sizetype,
8366 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8367 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8370 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8371 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8372 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8373 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8375 return new_field;
8378 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8379 it is the minimal size the REP_PART must have. */
8381 static tree
8382 create_rep_part (tree rep_type, tree record_type, tree min_size)
8384 tree field;
8386 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8387 min_size = NULL_TREE;
8389 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8390 min_size, NULL_TREE, 0, 1);
8391 DECL_INTERNAL_P (field) = 1;
8393 return field;
8396 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8398 static tree
8399 get_rep_part (tree record_type)
8401 tree field = TYPE_FIELDS (record_type);
8403 /* The REP part is the first field, internal, another record, and its name
8404 starts with an 'R'. */
8405 if (field
8406 && DECL_INTERNAL_P (field)
8407 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8408 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8409 return field;
8411 return NULL_TREE;
8414 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8416 tree
8417 get_variant_part (tree record_type)
8419 tree field;
8421 /* The variant part is the only internal field that is a qualified union. */
8422 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8423 if (DECL_INTERNAL_P (field)
8424 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8425 return field;
8427 return NULL_TREE;
8430 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8431 the list of variants to be used and RECORD_TYPE is the type of the parent.
8432 POS_LIST is a position list describing the layout of fields present in
8433 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8434 layout. */
8436 static tree
8437 create_variant_part_from (tree old_variant_part,
8438 vec<variant_desc> variant_list,
8439 tree record_type, tree pos_list,
8440 vec<subst_pair> subst_list)
8442 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8443 tree old_union_type = TREE_TYPE (old_variant_part);
8444 tree new_union_type, new_variant_part;
8445 tree union_field_list = NULL_TREE;
8446 variant_desc *v;
8447 unsigned int i;
8449 /* First create the type of the variant part from that of the old one. */
8450 new_union_type = make_node (QUAL_UNION_TYPE);
8451 TYPE_NAME (new_union_type)
8452 = concat_name (TYPE_NAME (record_type),
8453 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8455 /* If the position of the variant part is constant, subtract it from the
8456 size of the type of the parent to get the new size. This manual CSE
8457 reduces the code size when not optimizing. */
8458 if (TREE_CODE (offset) == INTEGER_CST)
8460 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8461 tree first_bit = bit_from_pos (offset, bitpos);
8462 TYPE_SIZE (new_union_type)
8463 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8464 TYPE_SIZE_UNIT (new_union_type)
8465 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8466 byte_from_pos (offset, bitpos));
8467 SET_TYPE_ADA_SIZE (new_union_type,
8468 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8469 first_bit));
8470 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8471 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8473 else
8474 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8476 /* Now finish up the new variants and populate the union type. */
8477 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8479 tree old_field = v->field, new_field;
8480 tree old_variant, old_variant_subpart, new_variant, field_list;
8482 /* Skip variants that don't belong to this nesting level. */
8483 if (DECL_CONTEXT (old_field) != old_union_type)
8484 continue;
8486 /* Retrieve the list of fields already added to the new variant. */
8487 new_variant = v->new_type;
8488 field_list = TYPE_FIELDS (new_variant);
8490 /* If the old variant had a variant subpart, we need to create a new
8491 variant subpart and add it to the field list. */
8492 old_variant = v->type;
8493 old_variant_subpart = get_variant_part (old_variant);
8494 if (old_variant_subpart)
8496 tree new_variant_subpart
8497 = create_variant_part_from (old_variant_subpart, variant_list,
8498 new_variant, pos_list, subst_list);
8499 DECL_CHAIN (new_variant_subpart) = field_list;
8500 field_list = new_variant_subpart;
8503 /* Finish up the new variant and create the field. No need for debug
8504 info thanks to the XVS type. */
8505 finish_record_type (new_variant, nreverse (field_list), 2, false);
8506 compute_record_mode (new_variant);
8507 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8508 Empty);
8510 new_field
8511 = create_field_decl_from (old_field, new_variant, new_union_type,
8512 TYPE_SIZE (new_variant),
8513 pos_list, subst_list);
8514 DECL_QUALIFIER (new_field) = v->qual;
8515 DECL_INTERNAL_P (new_field) = 1;
8516 DECL_CHAIN (new_field) = union_field_list;
8517 union_field_list = new_field;
8520 /* Finish up the union type and create the variant part. No need for debug
8521 info thanks to the XVS type. Note that we don't reverse the field list
8522 because VARIANT_LIST has been traversed in reverse order. */
8523 finish_record_type (new_union_type, union_field_list, 2, false);
8524 compute_record_mode (new_union_type);
8525 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8526 Empty);
8528 new_variant_part
8529 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8530 TYPE_SIZE (new_union_type),
8531 pos_list, subst_list);
8532 DECL_INTERNAL_P (new_variant_part) = 1;
8534 /* With multiple discriminants it is possible for an inner variant to be
8535 statically selected while outer ones are not; in this case, the list
8536 of fields of the inner variant is not flattened and we end up with a
8537 qualified union with a single member. Drop the useless container. */
8538 if (!DECL_CHAIN (union_field_list))
8540 DECL_CONTEXT (union_field_list) = record_type;
8541 DECL_FIELD_OFFSET (union_field_list)
8542 = DECL_FIELD_OFFSET (new_variant_part);
8543 DECL_FIELD_BIT_OFFSET (union_field_list)
8544 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8545 SET_DECL_OFFSET_ALIGN (union_field_list,
8546 DECL_OFFSET_ALIGN (new_variant_part));
8547 new_variant_part = union_field_list;
8550 return new_variant_part;
8553 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8554 which are both RECORD_TYPE, after applying the substitutions described
8555 in SUBST_LIST. */
8557 static void
8558 copy_and_substitute_in_size (tree new_type, tree old_type,
8559 vec<subst_pair> subst_list)
8561 unsigned int i;
8562 subst_pair *s;
8564 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8565 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8566 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8567 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8568 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8570 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8571 FOR_EACH_VEC_ELT (subst_list, i, s)
8572 TYPE_SIZE (new_type)
8573 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8574 s->discriminant, s->replacement);
8576 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8577 FOR_EACH_VEC_ELT (subst_list, i, s)
8578 TYPE_SIZE_UNIT (new_type)
8579 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8580 s->discriminant, s->replacement);
8582 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8583 FOR_EACH_VEC_ELT (subst_list, i, s)
8584 SET_TYPE_ADA_SIZE
8585 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8586 s->discriminant, s->replacement));
8588 /* Finalize the size. */
8589 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8590 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8593 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8594 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8595 The parallel type is the original array type if it has been translated. */
8597 static void
8598 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8600 Entity_Id gnat_original_array_type
8601 = Underlying_Type (Original_Array_Type (gnat_entity));
8602 tree gnu_original_array_type;
8604 if (!present_gnu_tree (gnat_original_array_type))
8605 return;
8607 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8609 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8610 return;
8612 add_parallel_type (gnu_type, gnu_original_array_type);
8615 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8616 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8617 updated by replacing F with R.
8619 The function doesn't update the layout of the type, i.e. it assumes
8620 that the substitution is purely formal. That's why the replacement
8621 value R must itself contain a PLACEHOLDER_EXPR. */
8623 tree
8624 substitute_in_type (tree t, tree f, tree r)
8626 tree nt;
8628 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8630 switch (TREE_CODE (t))
8632 case INTEGER_TYPE:
8633 case ENUMERAL_TYPE:
8634 case BOOLEAN_TYPE:
8635 case REAL_TYPE:
8637 /* First the domain types of arrays. */
8638 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8639 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8641 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8642 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8644 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8645 return t;
8647 nt = copy_type (t);
8648 TYPE_GCC_MIN_VALUE (nt) = low;
8649 TYPE_GCC_MAX_VALUE (nt) = high;
8651 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8652 SET_TYPE_INDEX_TYPE
8653 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8655 return nt;
8658 /* Then the subtypes. */
8659 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8660 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8662 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8663 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8665 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8666 return t;
8668 nt = copy_type (t);
8669 SET_TYPE_RM_MIN_VALUE (nt, low);
8670 SET_TYPE_RM_MAX_VALUE (nt, high);
8672 return nt;
8675 return t;
8677 case COMPLEX_TYPE:
8678 nt = substitute_in_type (TREE_TYPE (t), f, r);
8679 if (nt == TREE_TYPE (t))
8680 return t;
8682 return build_complex_type (nt);
8684 case FUNCTION_TYPE:
8685 /* These should never show up here. */
8686 gcc_unreachable ();
8688 case ARRAY_TYPE:
8690 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8691 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8693 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8694 return t;
8696 nt = build_nonshared_array_type (component, domain);
8697 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8698 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8699 SET_TYPE_MODE (nt, TYPE_MODE (t));
8700 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8701 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8702 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8703 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8704 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8705 return nt;
8708 case RECORD_TYPE:
8709 case UNION_TYPE:
8710 case QUAL_UNION_TYPE:
8712 bool changed_field = false;
8713 tree field;
8715 /* Start out with no fields, make new fields, and chain them
8716 in. If we haven't actually changed the type of any field,
8717 discard everything we've done and return the old type. */
8718 nt = copy_type (t);
8719 TYPE_FIELDS (nt) = NULL_TREE;
8721 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8723 tree new_field = copy_node (field), new_n;
8725 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8726 if (new_n != TREE_TYPE (field))
8728 TREE_TYPE (new_field) = new_n;
8729 changed_field = true;
8732 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8733 if (new_n != DECL_FIELD_OFFSET (field))
8735 DECL_FIELD_OFFSET (new_field) = new_n;
8736 changed_field = true;
8739 /* Do the substitution inside the qualifier, if any. */
8740 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8742 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8743 if (new_n != DECL_QUALIFIER (field))
8745 DECL_QUALIFIER (new_field) = new_n;
8746 changed_field = true;
8750 DECL_CONTEXT (new_field) = nt;
8751 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8753 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8754 TYPE_FIELDS (nt) = new_field;
8757 if (!changed_field)
8758 return t;
8760 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8761 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8762 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8763 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8764 return nt;
8767 default:
8768 return t;
8772 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8773 needed to represent the object. */
8775 tree
8776 rm_size (tree gnu_type)
8778 /* For integral types, we store the RM size explicitly. */
8779 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8780 return TYPE_RM_SIZE (gnu_type);
8782 /* Return the RM size of the actual data plus the size of the template. */
8783 if (TREE_CODE (gnu_type) == RECORD_TYPE
8784 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8785 return
8786 size_binop (PLUS_EXPR,
8787 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8788 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8790 /* For record or union types, we store the size explicitly. */
8791 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8792 && !TYPE_FAT_POINTER_P (gnu_type)
8793 && TYPE_ADA_SIZE (gnu_type))
8794 return TYPE_ADA_SIZE (gnu_type);
8796 /* For other types, this is just the size. */
8797 return TYPE_SIZE (gnu_type);
8800 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8801 fully-qualified name, possibly with type information encoding.
8802 Otherwise, return the name. */
8804 tree
8805 get_entity_name (Entity_Id gnat_entity)
8807 Get_Encoded_Name (gnat_entity);
8808 return get_identifier_with_length (Name_Buffer, Name_Len);
8811 /* Return an identifier representing the external name to be used for
8812 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8813 and the specified suffix. */
8815 tree
8816 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8818 const Entity_Kind kind = Ekind (gnat_entity);
8819 const bool has_suffix = (suffix != NULL);
8820 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8821 String_Pointer sp = {suffix, &temp};
8823 Get_External_Name (gnat_entity, has_suffix, sp);
8825 /* A variable using the Stdcall convention lives in a DLL. We adjust
8826 its name to use the jump table, the _imp__NAME contains the address
8827 for the NAME variable. */
8828 if ((kind == E_Variable || kind == E_Constant)
8829 && Has_Stdcall_Convention (gnat_entity))
8831 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8832 char *new_name = (char *) alloca (len + 1);
8833 strcpy (new_name, STDCALL_PREFIX);
8834 strcat (new_name, Name_Buffer);
8835 return get_identifier_with_length (new_name, len);
8838 return get_identifier_with_length (Name_Buffer, Name_Len);
8841 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8842 string, return a new IDENTIFIER_NODE that is the concatenation of
8843 the name followed by "___" and the specified suffix. */
8845 tree
8846 concat_name (tree gnu_name, const char *suffix)
8848 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8849 char *new_name = (char *) alloca (len + 1);
8850 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8851 strcat (new_name, "___");
8852 strcat (new_name, suffix);
8853 return get_identifier_with_length (new_name, len);
8856 /* Initialize data structures of the decl.c module. */
8858 void
8859 init_gnat_decl (void)
8861 /* Initialize the cache of annotated values. */
8862 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8865 /* Destroy data structures of the decl.c module. */
8867 void
8868 destroy_gnat_decl (void)
8870 /* Destroy the cache of annotated values. */
8871 annotate_value_cache->empty ();
8872 annotate_value_cache = NULL;
8875 #include "gt-ada-decl.h"