* gcc-interface/decl.c (gnat_to_gnu_field): Post the error message
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobfda3f0cc63e4a3647c0b4e9cd2831ef379c87e4f
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 const_flag = true;
1421 gnu_type
1422 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1425 if (const_flag)
1426 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1428 /* Convert the expression to the type of the object except in the
1429 case where the object's type is unconstrained or the object's type
1430 is a padded record whose field is of self-referential size. In
1431 the former case, converting will generate unnecessary evaluations
1432 of the CONSTRUCTOR to compute the size and in the latter case, we
1433 want to only copy the actual data. Also don't convert to a record
1434 type with a variant part from a record type without one, to keep
1435 the object simpler. */
1436 if (gnu_expr
1437 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1438 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1439 && !(TYPE_IS_PADDING_P (gnu_type)
1440 && CONTAINS_PLACEHOLDER_P
1441 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1442 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1443 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1444 && get_variant_part (gnu_type) != NULL_TREE
1445 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1446 gnu_expr = convert (gnu_type, gnu_expr);
1448 /* If this name is external or a name was specified, use it, but don't
1449 use the Interface_Name with an address clause (see cd30005). */
1450 if ((Present (Interface_Name (gnat_entity))
1451 && No (Address_Clause (gnat_entity)))
1452 || (Is_Public (gnat_entity)
1453 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1454 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1456 /* If this is an aggregate constant initialized to a constant, force it
1457 to be statically allocated. This saves an initialization copy. */
1458 if (!static_p
1459 && const_flag
1460 && gnu_expr && TREE_CONSTANT (gnu_expr)
1461 && AGGREGATE_TYPE_P (gnu_type)
1462 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1463 && !(TYPE_IS_PADDING_P (gnu_type)
1464 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1465 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1466 static_p = true;
1468 /* Deal with a pragma Linker_Section on a constant or variable. */
1469 if ((kind == E_Constant || kind == E_Variable)
1470 && Present (Linker_Section_Pragma (gnat_entity)))
1471 prepend_one_attribute_pragma (&attr_list,
1472 Linker_Section_Pragma (gnat_entity));
1474 /* Now create the variable or the constant and set various flags. */
1475 gnu_decl
1476 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1477 gnu_expr, const_flag, Is_Public (gnat_entity),
1478 imported_p || !definition, static_p,
1479 !renamed_obj, attr_list, gnat_entity);
1480 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1481 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1482 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1484 /* If we are defining an Out parameter and optimization isn't enabled,
1485 create a fake PARM_DECL for debugging purposes and make it point to
1486 the VAR_DECL. Suppress debug info for the latter but make sure it
1487 will live in memory so that it can be accessed from within the
1488 debugger through the PARM_DECL. */
1489 if (kind == E_Out_Parameter
1490 && definition
1491 && debug_info_p
1492 && !optimize
1493 && !flag_generate_lto)
1495 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1496 gnat_pushdecl (param, gnat_entity);
1497 SET_DECL_VALUE_EXPR (param, gnu_decl);
1498 DECL_HAS_VALUE_EXPR_P (param) = 1;
1499 DECL_IGNORED_P (gnu_decl) = 1;
1500 TREE_ADDRESSABLE (gnu_decl) = 1;
1503 /* If this is a loop parameter, set the corresponding flag. */
1504 else if (kind == E_Loop_Parameter)
1505 DECL_LOOP_PARM_P (gnu_decl) = 1;
1507 /* If this is a renaming pointer, attach the renamed object to it and
1508 register it if we are at the global level. Note that an external
1509 constant is at the global level. */
1510 if (renamed_obj)
1512 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1513 if ((!definition && kind == E_Constant) || global_bindings_p ())
1515 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1516 record_global_renaming_pointer (gnu_decl);
1520 /* If this is a constant and we are defining it or it generates a real
1521 symbol at the object level and we are referencing it, we may want
1522 or need to have a true variable to represent it:
1523 - if optimization isn't enabled, for debugging purposes,
1524 - if the constant is public and not overlaid on something else,
1525 - if its address is taken,
1526 - if either itself or its type is aliased. */
1527 if (TREE_CODE (gnu_decl) == CONST_DECL
1528 && (definition || Sloc (gnat_entity) > Standard_Location)
1529 && ((!optimize && debug_info_p)
1530 || (Is_Public (gnat_entity)
1531 && No (Address_Clause (gnat_entity)))
1532 || Address_Taken (gnat_entity)
1533 || Is_Aliased (gnat_entity)
1534 || Is_Aliased (Etype (gnat_entity))))
1536 tree gnu_corr_var
1537 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1538 gnu_expr, true, Is_Public (gnat_entity),
1539 !definition, static_p, attr_list,
1540 gnat_entity);
1542 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1544 /* As debugging information will be generated for the variable,
1545 do not generate debugging information for the constant. */
1546 if (debug_info_p)
1547 DECL_IGNORED_P (gnu_decl) = 1;
1548 else
1549 DECL_IGNORED_P (gnu_corr_var) = 1;
1552 /* If this is a constant, even if we don't need a true variable, we
1553 may need to avoid returning the initializer in every case. That
1554 can happen for the address of a (constant) constructor because,
1555 upon dereferencing it, the constructor will be reinjected in the
1556 tree, which may not be valid in every case; see lvalue_required_p
1557 for more details. */
1558 if (TREE_CODE (gnu_decl) == CONST_DECL)
1559 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1561 /* If this object is declared in a block that contains a block with an
1562 exception handler, and we aren't using the GCC exception mechanism,
1563 we must force this variable in memory in order to avoid an invalid
1564 optimization. */
1565 if (Exception_Mechanism != Back_End_Exceptions
1566 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1567 TREE_ADDRESSABLE (gnu_decl) = 1;
1569 /* If this is a local variable with non-BLKmode and aggregate type,
1570 and optimization isn't enabled, then force it in memory so that
1571 a register won't be allocated to it with possible subparts left
1572 uninitialized and reaching the register allocator. */
1573 else if (TREE_CODE (gnu_decl) == VAR_DECL
1574 && !DECL_EXTERNAL (gnu_decl)
1575 && !TREE_STATIC (gnu_decl)
1576 && DECL_MODE (gnu_decl) != BLKmode
1577 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1578 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1579 && !optimize)
1580 TREE_ADDRESSABLE (gnu_decl) = 1;
1582 /* If we are defining an object with variable size or an object with
1583 fixed size that will be dynamically allocated, and we are using the
1584 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1585 if (definition
1586 && Exception_Mechanism == Setjmp_Longjmp
1587 && get_block_jmpbuf_decl ()
1588 && DECL_SIZE_UNIT (gnu_decl)
1589 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1590 || (flag_stack_check == GENERIC_STACK_CHECK
1591 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1592 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1593 add_stmt_with_node (build_call_n_expr
1594 (update_setjmp_buf_decl, 1,
1595 build_unary_op (ADDR_EXPR, NULL_TREE,
1596 get_block_jmpbuf_decl ())),
1597 gnat_entity);
1599 /* Back-annotate Esize and Alignment of the object if not already
1600 known. Note that we pick the values of the type, not those of
1601 the object, to shield ourselves from low-level platform-dependent
1602 adjustments like alignment promotion. This is both consistent with
1603 all the treatment above, where alignment and size are set on the
1604 type of the object and not on the object directly, and makes it
1605 possible to support all confirming representation clauses. */
1606 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1607 used_by_ref);
1609 break;
1611 case E_Void:
1612 /* Return a TYPE_DECL for "void" that we previously made. */
1613 gnu_decl = TYPE_NAME (void_type_node);
1614 break;
1616 case E_Enumeration_Type:
1617 /* A special case: for the types Character and Wide_Character in
1618 Standard, we do not list all the literals. So if the literals
1619 are not specified, make this an unsigned integer type. */
1620 if (No (First_Literal (gnat_entity)))
1622 gnu_type = make_unsigned_type (esize);
1623 TYPE_NAME (gnu_type) = gnu_entity_name;
1625 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1626 This is needed by the DWARF-2 back-end to distinguish between
1627 unsigned integer types and character types. */
1628 TYPE_STRING_FLAG (gnu_type) = 1;
1630 else
1632 /* We have a list of enumeral constants in First_Literal. We make a
1633 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1634 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1635 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1636 value of the literal. But when we have a regular boolean type, we
1637 simplify this a little by using a BOOLEAN_TYPE. */
1638 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1639 && !Has_Non_Standard_Rep (gnat_entity);
1640 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1641 tree gnu_list = NULL_TREE;
1642 Entity_Id gnat_literal;
1644 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1645 TYPE_PRECISION (gnu_type) = esize;
1646 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1647 set_min_and_max_values_for_integral_type (gnu_type, esize,
1648 TYPE_SIGN (gnu_type));
1649 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1650 layout_type (gnu_type);
1652 for (gnat_literal = First_Literal (gnat_entity);
1653 Present (gnat_literal);
1654 gnat_literal = Next_Literal (gnat_literal))
1656 tree gnu_value
1657 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1658 tree gnu_literal
1659 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1660 gnu_type, gnu_value, true, false, false,
1661 false, NULL, gnat_literal);
1662 /* Do not generate debug info for individual enumerators. */
1663 DECL_IGNORED_P (gnu_literal) = 1;
1664 save_gnu_tree (gnat_literal, gnu_literal, false);
1665 gnu_list
1666 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1669 if (!is_boolean)
1670 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1672 /* Note that the bounds are updated at the end of this function
1673 to avoid an infinite recursion since they refer to the type. */
1674 goto discrete_type;
1676 break;
1678 case E_Signed_Integer_Type:
1679 case E_Ordinary_Fixed_Point_Type:
1680 case E_Decimal_Fixed_Point_Type:
1681 /* For integer types, just make a signed type the appropriate number
1682 of bits. */
1683 gnu_type = make_signed_type (esize);
1684 goto discrete_type;
1686 case E_Modular_Integer_Type:
1688 /* For modular types, make the unsigned type of the proper number
1689 of bits and then set up the modulus, if required. */
1690 tree gnu_modulus, gnu_high = NULL_TREE;
1692 /* Packed Array Impl. Types are supposed to be subtypes only. */
1693 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1695 gnu_type = make_unsigned_type (esize);
1697 /* Get the modulus in this type. If it overflows, assume it is because
1698 it is equal to 2**Esize. Note that there is no overflow checking
1699 done on unsigned type, so we detect the overflow by looking for
1700 a modulus of zero, which is otherwise invalid. */
1701 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1703 if (!integer_zerop (gnu_modulus))
1705 TYPE_MODULAR_P (gnu_type) = 1;
1706 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1707 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1708 convert (gnu_type, integer_one_node));
1711 /* If the upper bound is not maximal, make an extra subtype. */
1712 if (gnu_high
1713 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1715 tree gnu_subtype = make_unsigned_type (esize);
1716 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1717 TREE_TYPE (gnu_subtype) = gnu_type;
1718 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1719 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1720 gnu_type = gnu_subtype;
1723 goto discrete_type;
1725 case E_Signed_Integer_Subtype:
1726 case E_Enumeration_Subtype:
1727 case E_Modular_Integer_Subtype:
1728 case E_Ordinary_Fixed_Point_Subtype:
1729 case E_Decimal_Fixed_Point_Subtype:
1731 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1732 not want to call create_range_type since we would like each subtype
1733 node to be distinct. ??? Historically this was in preparation for
1734 when memory aliasing is implemented, but that's obsolete now given
1735 the call to relate_alias_sets below.
1737 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1738 this fact is used by the arithmetic conversion functions.
1740 We elaborate the Ancestor_Subtype if it is not in the current unit
1741 and one of our bounds is non-static. We do this to ensure consistent
1742 naming in the case where several subtypes share the same bounds, by
1743 elaborating the first such subtype first, thus using its name. */
1745 if (!definition
1746 && Present (Ancestor_Subtype (gnat_entity))
1747 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1748 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1749 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1750 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1752 /* Set the precision to the Esize except for bit-packed arrays. */
1753 if (Is_Packed_Array_Impl_Type (gnat_entity)
1754 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1755 esize = UI_To_Int (RM_Size (gnat_entity));
1757 /* This should be an unsigned type if the base type is unsigned or
1758 if the lower bound is constant and non-negative or if the type
1759 is biased. */
1760 if (Is_Unsigned_Type (Etype (gnat_entity))
1761 || Is_Unsigned_Type (gnat_entity)
1762 || Has_Biased_Representation (gnat_entity))
1763 gnu_type = make_unsigned_type (esize);
1764 else
1765 gnu_type = make_signed_type (esize);
1766 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1768 SET_TYPE_RM_MIN_VALUE
1769 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1770 gnat_entity, get_identifier ("L"),
1771 definition, true,
1772 Needs_Debug_Info (gnat_entity)));
1774 SET_TYPE_RM_MAX_VALUE
1775 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1776 gnat_entity, get_identifier ("U"),
1777 definition, true,
1778 Needs_Debug_Info (gnat_entity)));
1780 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1781 = Has_Biased_Representation (gnat_entity);
1783 /* Inherit our alias set from what we're a subtype of. Subtypes
1784 are not different types and a pointer can designate any instance
1785 within a subtype hierarchy. */
1786 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1788 /* One of the above calls might have caused us to be elaborated,
1789 so don't blow up if so. */
1790 if (present_gnu_tree (gnat_entity))
1792 maybe_present = true;
1793 break;
1796 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1797 TYPE_STUB_DECL (gnu_type)
1798 = create_type_stub_decl (gnu_entity_name, gnu_type);
1800 /* For a packed array, make the original array type a parallel type. */
1801 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1802 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1804 discrete_type:
1806 /* We have to handle clauses that under-align the type specially. */
1807 if ((Present (Alignment_Clause (gnat_entity))
1808 || (Is_Packed_Array_Impl_Type (gnat_entity)
1809 && Present
1810 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1811 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1813 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1814 if (align >= TYPE_ALIGN (gnu_type))
1815 align = 0;
1818 /* If the type we are dealing with represents a bit-packed array,
1819 we need to have the bits left justified on big-endian targets
1820 and right justified on little-endian targets. We also need to
1821 ensure that when the value is read (e.g. for comparison of two
1822 such values), we only get the good bits, since the unused bits
1823 are uninitialized. Both goals are accomplished by wrapping up
1824 the modular type in an enclosing record type. */
1825 if (Is_Packed_Array_Impl_Type (gnat_entity)
1826 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1828 tree gnu_field_type, gnu_field;
1830 /* Set the RM size before wrapping up the original type. */
1831 SET_TYPE_RM_SIZE (gnu_type,
1832 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1833 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1835 /* Create a stripped-down declaration, mainly for debugging. */
1836 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1837 gnat_entity);
1839 /* Now save it and build the enclosing record type. */
1840 gnu_field_type = gnu_type;
1842 gnu_type = make_node (RECORD_TYPE);
1843 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1844 TYPE_PACKED (gnu_type) = 1;
1845 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1846 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1847 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1849 /* Propagate the alignment of the modular type to the record type,
1850 unless there is an alignment clause that under-aligns the type.
1851 This means that bit-packed arrays are given "ceil" alignment for
1852 their size by default, which may seem counter-intuitive but makes
1853 it possible to overlay them on modular types easily. */
1854 TYPE_ALIGN (gnu_type)
1855 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1857 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1859 /* Don't declare the field as addressable since we won't be taking
1860 its address and this would prevent create_field_decl from making
1861 a bitfield. */
1862 gnu_field
1863 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1864 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1866 /* Do not emit debug info until after the parallel type is added. */
1867 finish_record_type (gnu_type, gnu_field, 2, false);
1868 compute_record_mode (gnu_type);
1869 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1871 if (debug_info_p)
1873 /* Make the original array type a parallel type. */
1874 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1876 rest_of_record_type_compilation (gnu_type);
1880 /* If the type we are dealing with has got a smaller alignment than the
1881 natural one, we need to wrap it up in a record type and misalign the
1882 latter; we reuse the padding machinery for this purpose. Note that,
1883 even if the record type is marked as packed because of misalignment,
1884 we don't pack the field so as to give it the size of the type. */
1885 else if (align > 0)
1887 tree gnu_field_type, gnu_field;
1889 /* Set the RM size before wrapping up the type. */
1890 SET_TYPE_RM_SIZE (gnu_type,
1891 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1893 /* Create a stripped-down declaration, mainly for debugging. */
1894 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1895 gnat_entity);
1897 /* Now save it and build the enclosing record type. */
1898 gnu_field_type = gnu_type;
1900 gnu_type = make_node (RECORD_TYPE);
1901 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1902 TYPE_PACKED (gnu_type) = 1;
1903 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1904 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1905 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1906 TYPE_ALIGN (gnu_type) = align;
1907 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1909 /* Don't declare the field as addressable since we won't be taking
1910 its address and this would prevent create_field_decl from making
1911 a bitfield. */
1912 gnu_field
1913 = create_field_decl (get_identifier ("F"), gnu_field_type,
1914 gnu_type, TYPE_SIZE (gnu_field_type),
1915 bitsize_zero_node, 0, 0);
1917 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1918 compute_record_mode (gnu_type);
1919 TYPE_PADDING_P (gnu_type) = 1;
1922 break;
1924 case E_Floating_Point_Type:
1925 /* The type of the Low and High bounds can be our type if this is
1926 a type from Standard, so set them at the end of the function. */
1927 gnu_type = make_node (REAL_TYPE);
1928 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1929 layout_type (gnu_type);
1930 break;
1932 case E_Floating_Point_Subtype:
1933 /* See the E_Signed_Integer_Subtype case for the rationale. */
1934 if (!definition
1935 && Present (Ancestor_Subtype (gnat_entity))
1936 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1937 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1938 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1939 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1941 gnu_type = make_node (REAL_TYPE);
1942 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1943 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1944 TYPE_GCC_MIN_VALUE (gnu_type)
1945 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1946 TYPE_GCC_MAX_VALUE (gnu_type)
1947 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1948 layout_type (gnu_type);
1950 SET_TYPE_RM_MIN_VALUE
1951 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1952 gnat_entity, get_identifier ("L"),
1953 definition, true,
1954 Needs_Debug_Info (gnat_entity)));
1956 SET_TYPE_RM_MAX_VALUE
1957 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1958 gnat_entity, get_identifier ("U"),
1959 definition, true,
1960 Needs_Debug_Info (gnat_entity)));
1962 /* Inherit our alias set from what we're a subtype of, as for
1963 integer subtypes. */
1964 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1966 /* One of the above calls might have caused us to be elaborated,
1967 so don't blow up if so. */
1968 maybe_present = true;
1969 break;
1971 /* Array Types and Subtypes
1973 Unconstrained array types are represented by E_Array_Type and
1974 constrained array types are represented by E_Array_Subtype. There
1975 are no actual objects of an unconstrained array type; all we have
1976 are pointers to that type.
1978 The following fields are defined on array types and subtypes:
1980 Component_Type Component type of the array.
1981 Number_Dimensions Number of dimensions (an int).
1982 First_Index Type of first index. */
1984 case E_Array_Type:
1986 const bool convention_fortran_p
1987 = (Convention (gnat_entity) == Convention_Fortran);
1988 const int ndim = Number_Dimensions (gnat_entity);
1989 tree gnu_template_type;
1990 tree gnu_ptr_template;
1991 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1992 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1993 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1994 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1995 Entity_Id gnat_index, gnat_name;
1996 int index;
1997 tree comp_type;
1999 /* Create the type for the component now, as it simplifies breaking
2000 type reference loops. */
2001 comp_type
2002 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2003 if (present_gnu_tree (gnat_entity))
2005 /* As a side effect, the type may have been translated. */
2006 maybe_present = true;
2007 break;
2010 /* We complete an existing dummy fat pointer type in place. This both
2011 avoids further complex adjustments in update_pointer_to and yields
2012 better debugging information in DWARF by leveraging the support for
2013 incomplete declarations of "tagged" types in the DWARF back-end. */
2014 gnu_type = get_dummy_type (gnat_entity);
2015 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2017 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2018 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2019 /* Save the contents of the dummy type for update_pointer_to. */
2020 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2021 gnu_ptr_template =
2022 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2023 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2025 else
2027 gnu_fat_type = make_node (RECORD_TYPE);
2028 gnu_template_type = make_node (RECORD_TYPE);
2029 gnu_ptr_template = build_pointer_type (gnu_template_type);
2032 /* Make a node for the array. If we are not defining the array
2033 suppress expanding incomplete types. */
2034 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2036 if (!definition)
2038 defer_incomplete_level++;
2039 this_deferred = true;
2042 /* Build the fat pointer type. Use a "void *" object instead of
2043 a pointer to the array type since we don't have the array type
2044 yet (it will reference the fat pointer via the bounds). */
2046 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2047 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2048 DECL_CHAIN (tem)
2049 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2050 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2052 if (COMPLETE_TYPE_P (gnu_fat_type))
2054 /* We are going to lay it out again so reset the alias set. */
2055 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2056 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2057 finish_fat_pointer_type (gnu_fat_type, tem);
2058 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2059 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2061 TYPE_FIELDS (t) = tem;
2062 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2065 else
2067 finish_fat_pointer_type (gnu_fat_type, tem);
2068 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2071 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2072 is the fat pointer. This will be used to access the individual
2073 fields once we build them. */
2074 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2075 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2076 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2077 gnu_template_reference
2078 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2079 TREE_READONLY (gnu_template_reference) = 1;
2080 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2082 /* Now create the GCC type for each index and add the fields for that
2083 index to the template. */
2084 for (index = (convention_fortran_p ? ndim - 1 : 0),
2085 gnat_index = First_Index (gnat_entity);
2086 0 <= index && index < ndim;
2087 index += (convention_fortran_p ? - 1 : 1),
2088 gnat_index = Next_Index (gnat_index))
2090 char field_name[16];
2091 tree gnu_index_base_type
2092 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2093 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2094 tree gnu_min, gnu_max, gnu_high;
2096 /* Make the FIELD_DECLs for the low and high bounds of this
2097 type and then make extractions of these fields from the
2098 template. */
2099 sprintf (field_name, "LB%d", index);
2100 gnu_lb_field = create_field_decl (get_identifier (field_name),
2101 gnu_index_base_type,
2102 gnu_template_type, NULL_TREE,
2103 NULL_TREE, 0, 0);
2104 Sloc_to_locus (Sloc (gnat_entity),
2105 &DECL_SOURCE_LOCATION (gnu_lb_field));
2107 field_name[0] = 'U';
2108 gnu_hb_field = create_field_decl (get_identifier (field_name),
2109 gnu_index_base_type,
2110 gnu_template_type, NULL_TREE,
2111 NULL_TREE, 0, 0);
2112 Sloc_to_locus (Sloc (gnat_entity),
2113 &DECL_SOURCE_LOCATION (gnu_hb_field));
2115 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2117 /* We can't use build_component_ref here since the template type
2118 isn't complete yet. */
2119 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2120 gnu_template_reference, gnu_lb_field,
2121 NULL_TREE);
2122 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2123 gnu_template_reference, gnu_hb_field,
2124 NULL_TREE);
2125 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2127 gnu_min = convert (sizetype, gnu_orig_min);
2128 gnu_max = convert (sizetype, gnu_orig_max);
2130 /* Compute the size of this dimension. See the E_Array_Subtype
2131 case below for the rationale. */
2132 gnu_high
2133 = build3 (COND_EXPR, sizetype,
2134 build2 (GE_EXPR, boolean_type_node,
2135 gnu_orig_max, gnu_orig_min),
2136 gnu_max,
2137 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2139 /* Make a range type with the new range in the Ada base type.
2140 Then make an index type with the size range in sizetype. */
2141 gnu_index_types[index]
2142 = create_index_type (gnu_min, gnu_high,
2143 create_range_type (gnu_index_base_type,
2144 gnu_orig_min,
2145 gnu_orig_max),
2146 gnat_entity);
2148 /* Update the maximum size of the array in elements. */
2149 if (gnu_max_size)
2151 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2152 tree gnu_min
2153 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2154 tree gnu_max
2155 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2156 tree gnu_this_max
2157 = size_binop (PLUS_EXPR, size_one_node,
2158 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2160 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2161 && TREE_OVERFLOW (gnu_this_max))
2162 gnu_max_size = NULL_TREE;
2163 else
2164 gnu_max_size
2165 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2168 TYPE_NAME (gnu_index_types[index])
2169 = create_concat_name (gnat_entity, field_name);
2172 /* Install all the fields into the template. */
2173 TYPE_NAME (gnu_template_type)
2174 = create_concat_name (gnat_entity, "XUB");
2175 gnu_template_fields = NULL_TREE;
2176 for (index = 0; index < ndim; index++)
2177 gnu_template_fields
2178 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2179 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2180 debug_info_p);
2181 TYPE_READONLY (gnu_template_type) = 1;
2183 /* If Component_Size is not already specified, annotate it with the
2184 size of the component. */
2185 if (Unknown_Component_Size (gnat_entity))
2186 Set_Component_Size (gnat_entity,
2187 annotate_value (TYPE_SIZE (comp_type)));
2189 /* Compute the maximum size of the array in units and bits. */
2190 if (gnu_max_size)
2192 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2193 TYPE_SIZE_UNIT (comp_type));
2194 gnu_max_size = size_binop (MULT_EXPR,
2195 convert (bitsizetype, gnu_max_size),
2196 TYPE_SIZE (comp_type));
2198 else
2199 gnu_max_size_unit = NULL_TREE;
2201 /* Now build the array type. */
2202 tem = comp_type;
2203 for (index = ndim - 1; index >= 0; index--)
2205 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2206 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2207 sorry ("non-default Scalar_Storage_Order");
2208 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2209 if (array_type_has_nonaliased_component (tem, gnat_entity))
2210 TYPE_NONALIASED_COMPONENT (tem) = 1;
2212 /* If it is passed by reference, force BLKmode to ensure that
2213 objects of this type will always be put in memory. */
2214 if (TYPE_MODE (tem) != BLKmode
2215 && Is_By_Reference_Type (gnat_entity))
2216 SET_TYPE_MODE (tem, BLKmode);
2219 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2221 /* If an alignment is specified, use it if valid. But ignore it
2222 for the original type of packed array types. If the alignment
2223 was requested with an explicit alignment clause, state so. */
2224 if (No (Packed_Array_Impl_Type (gnat_entity))
2225 && Known_Alignment (gnat_entity))
2227 TYPE_ALIGN (tem)
2228 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2229 TYPE_ALIGN (tem));
2230 if (Present (Alignment_Clause (gnat_entity)))
2231 TYPE_USER_ALIGN (tem) = 1;
2234 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2236 /* Adjust the type of the pointer-to-array field of the fat pointer
2237 and record the aliasing relationships if necessary. */
2238 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2239 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2240 record_component_aliases (gnu_fat_type);
2242 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2243 corresponding fat pointer. */
2244 TREE_TYPE (gnu_type) = gnu_fat_type;
2245 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2246 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2247 SET_TYPE_MODE (gnu_type, BLKmode);
2248 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2250 /* If the maximum size doesn't overflow, use it. */
2251 if (gnu_max_size
2252 && TREE_CODE (gnu_max_size) == INTEGER_CST
2253 && !TREE_OVERFLOW (gnu_max_size)
2254 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2255 && !TREE_OVERFLOW (gnu_max_size_unit))
2257 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2258 TYPE_SIZE (tem));
2259 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2260 TYPE_SIZE_UNIT (tem));
2263 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2264 !Comes_From_Source (gnat_entity), debug_info_p,
2265 gnat_entity);
2267 /* Give the fat pointer type a name. If this is a packed array, tell
2268 the debugger how to interpret the underlying bits. */
2269 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2270 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2271 else
2272 gnat_name = gnat_entity;
2273 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2274 !Comes_From_Source (gnat_entity), debug_info_p,
2275 gnat_entity);
2277 /* Create the type to be designated by thin pointers: a record type for
2278 the array and its template. We used to shift the fields to have the
2279 template at a negative offset, but this was somewhat of a kludge; we
2280 now shift thin pointer values explicitly but only those which have a
2281 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2282 tem = build_unc_object_type (gnu_template_type, tem,
2283 create_concat_name (gnat_name, "XUT"),
2284 debug_info_p);
2286 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2287 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2289 break;
2291 case E_Array_Subtype:
2293 /* This is the actual data type for array variables. Multidimensional
2294 arrays are implemented as arrays of arrays. Note that arrays which
2295 have sparse enumeration subtypes as index components create sparse
2296 arrays, which is obviously space inefficient but so much easier to
2297 code for now.
2299 Also note that the subtype never refers to the unconstrained array
2300 type, which is somewhat at variance with Ada semantics.
2302 First check to see if this is simply a renaming of the array type.
2303 If so, the result is the array type. */
2305 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2306 if (!Is_Constrained (gnat_entity))
2308 else
2310 Entity_Id gnat_index, gnat_base_index;
2311 const bool convention_fortran_p
2312 = (Convention (gnat_entity) == Convention_Fortran);
2313 const int ndim = Number_Dimensions (gnat_entity);
2314 tree gnu_base_type = gnu_type;
2315 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2316 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2317 bool need_index_type_struct = false;
2318 int index;
2320 /* First create the GCC type for each index and find out whether
2321 special types are needed for debugging information. */
2322 for (index = (convention_fortran_p ? ndim - 1 : 0),
2323 gnat_index = First_Index (gnat_entity),
2324 gnat_base_index
2325 = First_Index (Implementation_Base_Type (gnat_entity));
2326 0 <= index && index < ndim;
2327 index += (convention_fortran_p ? - 1 : 1),
2328 gnat_index = Next_Index (gnat_index),
2329 gnat_base_index = Next_Index (gnat_base_index))
2331 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2332 tree gnu_index_base_type = get_base_type (gnu_index_type);
2333 tree gnu_orig_min
2334 = convert (gnu_index_base_type,
2335 TYPE_MIN_VALUE (gnu_index_type));
2336 tree gnu_orig_max
2337 = convert (gnu_index_base_type,
2338 TYPE_MAX_VALUE (gnu_index_type));
2339 tree gnu_min = convert (sizetype, gnu_orig_min);
2340 tree gnu_max = convert (sizetype, gnu_orig_max);
2341 tree gnu_base_index_type
2342 = get_unpadded_type (Etype (gnat_base_index));
2343 tree gnu_base_index_base_type
2344 = get_base_type (gnu_base_index_type);
2345 tree gnu_base_orig_min
2346 = convert (gnu_base_index_base_type,
2347 TYPE_MIN_VALUE (gnu_base_index_type));
2348 tree gnu_base_orig_max
2349 = convert (gnu_base_index_base_type,
2350 TYPE_MAX_VALUE (gnu_base_index_type));
2351 tree gnu_high;
2353 /* See if the base array type is already flat. If it is, we
2354 are probably compiling an ACATS test but it will cause the
2355 code below to malfunction if we don't handle it specially. */
2356 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2357 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2358 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2360 gnu_min = size_one_node;
2361 gnu_max = size_zero_node;
2362 gnu_high = gnu_max;
2365 /* Similarly, if one of the values overflows in sizetype and the
2366 range is null, use 1..0 for the sizetype bounds. */
2367 else if (TREE_CODE (gnu_min) == INTEGER_CST
2368 && TREE_CODE (gnu_max) == INTEGER_CST
2369 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2370 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2372 gnu_min = size_one_node;
2373 gnu_max = size_zero_node;
2374 gnu_high = gnu_max;
2377 /* If the minimum and maximum values both overflow in sizetype,
2378 but the difference in the original type does not overflow in
2379 sizetype, ignore the overflow indication. */
2380 else if (TREE_CODE (gnu_min) == INTEGER_CST
2381 && TREE_CODE (gnu_max) == INTEGER_CST
2382 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2383 && !TREE_OVERFLOW
2384 (convert (sizetype,
2385 fold_build2 (MINUS_EXPR, gnu_index_type,
2386 gnu_orig_max,
2387 gnu_orig_min))))
2389 TREE_OVERFLOW (gnu_min) = 0;
2390 TREE_OVERFLOW (gnu_max) = 0;
2391 gnu_high = gnu_max;
2394 /* Compute the size of this dimension in the general case. We
2395 need to provide GCC with an upper bound to use but have to
2396 deal with the "superflat" case. There are three ways to do
2397 this. If we can prove that the array can never be superflat,
2398 we can just use the high bound of the index type. */
2399 else if ((Nkind (gnat_index) == N_Range
2400 && cannot_be_superflat_p (gnat_index))
2401 /* Bit-Packed Array Impl. Types are never superflat. */
2402 || (Is_Packed_Array_Impl_Type (gnat_entity)
2403 && Is_Bit_Packed_Array
2404 (Original_Array_Type (gnat_entity))))
2405 gnu_high = gnu_max;
2407 /* Otherwise, if the high bound is constant but the low bound is
2408 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2409 lower bound. Note that the comparison must be done in the
2410 original type to avoid any overflow during the conversion. */
2411 else if (TREE_CODE (gnu_max) == INTEGER_CST
2412 && TREE_CODE (gnu_min) != INTEGER_CST)
2414 gnu_high = gnu_max;
2415 gnu_min
2416 = build_cond_expr (sizetype,
2417 build_binary_op (GE_EXPR,
2418 boolean_type_node,
2419 gnu_orig_max,
2420 gnu_orig_min),
2421 gnu_min,
2422 int_const_binop (PLUS_EXPR, gnu_max,
2423 size_one_node));
2426 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2427 in all the other cases. Note that, here as well as above,
2428 the condition used in the comparison must be equivalent to
2429 the condition (length != 0). This is relied upon in order
2430 to optimize array comparisons in compare_arrays. Moreover
2431 we use int_const_binop for the shift by 1 if the bound is
2432 constant to avoid any unwanted overflow. */
2433 else
2434 gnu_high
2435 = build_cond_expr (sizetype,
2436 build_binary_op (GE_EXPR,
2437 boolean_type_node,
2438 gnu_orig_max,
2439 gnu_orig_min),
2440 gnu_max,
2441 TREE_CODE (gnu_min) == INTEGER_CST
2442 ? int_const_binop (MINUS_EXPR, gnu_min,
2443 size_one_node)
2444 : size_binop (MINUS_EXPR, gnu_min,
2445 size_one_node));
2447 /* Reuse the index type for the range type. Then make an index
2448 type with the size range in sizetype. */
2449 gnu_index_types[index]
2450 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2451 gnat_entity);
2453 /* Update the maximum size of the array in elements. Here we
2454 see if any constraint on the index type of the base type
2455 can be used in the case of self-referential bound on the
2456 index type of the subtype. We look for a non-"infinite"
2457 and non-self-referential bound from any type involved and
2458 handle each bound separately. */
2459 if (gnu_max_size)
2461 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2462 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2463 tree gnu_base_index_base_type
2464 = get_base_type (gnu_base_index_type);
2465 tree gnu_base_base_min
2466 = convert (sizetype,
2467 TYPE_MIN_VALUE (gnu_base_index_base_type));
2468 tree gnu_base_base_max
2469 = convert (sizetype,
2470 TYPE_MAX_VALUE (gnu_base_index_base_type));
2472 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2473 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2474 && !TREE_OVERFLOW (gnu_base_min)))
2475 gnu_base_min = gnu_min;
2477 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2478 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2479 && !TREE_OVERFLOW (gnu_base_max)))
2480 gnu_base_max = gnu_max;
2482 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2483 && TREE_OVERFLOW (gnu_base_min))
2484 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2485 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2486 && TREE_OVERFLOW (gnu_base_max))
2487 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2488 gnu_max_size = NULL_TREE;
2489 else
2491 tree gnu_this_max;
2493 /* Use int_const_binop if the bounds are constant to
2494 avoid any unwanted overflow. */
2495 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2496 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2497 gnu_this_max
2498 = int_const_binop (PLUS_EXPR, size_one_node,
2499 int_const_binop (MINUS_EXPR,
2500 gnu_base_max,
2501 gnu_base_min));
2502 else
2503 gnu_this_max
2504 = size_binop (PLUS_EXPR, size_one_node,
2505 size_binop (MINUS_EXPR,
2506 gnu_base_max,
2507 gnu_base_min));
2509 gnu_max_size
2510 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2514 /* We need special types for debugging information to point to
2515 the index types if they have variable bounds, are not integer
2516 types or are biased. */
2517 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2518 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2519 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2520 || (TREE_TYPE (gnu_index_type)
2521 && TREE_CODE (TREE_TYPE (gnu_index_type))
2522 != INTEGER_TYPE)
2523 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2524 need_index_type_struct = true;
2527 /* Then flatten: create the array of arrays. For an array type
2528 used to implement a packed array, get the component type from
2529 the original array type since the representation clauses that
2530 can affect it are on the latter. */
2531 if (Is_Packed_Array_Impl_Type (gnat_entity)
2532 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2534 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2535 for (index = ndim - 1; index >= 0; index--)
2536 gnu_type = TREE_TYPE (gnu_type);
2538 /* One of the above calls might have caused us to be elaborated,
2539 so don't blow up if so. */
2540 if (present_gnu_tree (gnat_entity))
2542 maybe_present = true;
2543 break;
2546 else
2548 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2549 debug_info_p);
2551 /* One of the above calls might have caused us to be elaborated,
2552 so don't blow up if so. */
2553 if (present_gnu_tree (gnat_entity))
2555 maybe_present = true;
2556 break;
2560 /* Compute the maximum size of the array in units and bits. */
2561 if (gnu_max_size)
2563 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2564 TYPE_SIZE_UNIT (gnu_type));
2565 gnu_max_size = size_binop (MULT_EXPR,
2566 convert (bitsizetype, gnu_max_size),
2567 TYPE_SIZE (gnu_type));
2569 else
2570 gnu_max_size_unit = NULL_TREE;
2572 /* Now build the array type. */
2573 for (index = ndim - 1; index >= 0; index --)
2575 gnu_type = build_nonshared_array_type (gnu_type,
2576 gnu_index_types[index]);
2577 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2578 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2579 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2581 /* See the E_Array_Type case for the rationale. */
2582 if (TYPE_MODE (gnu_type) != BLKmode
2583 && Is_By_Reference_Type (gnat_entity))
2584 SET_TYPE_MODE (gnu_type, BLKmode);
2587 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2589 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2590 TYPE_STUB_DECL (gnu_type)
2591 = create_type_stub_decl (gnu_entity_name, gnu_type);
2593 /* If we are at file level and this is a multi-dimensional array,
2594 we need to make a variable corresponding to the stride of the
2595 inner dimensions. */
2596 if (global_bindings_p () && ndim > 1)
2598 tree gnu_st_name = get_identifier ("ST");
2599 tree gnu_arr_type;
2601 for (gnu_arr_type = TREE_TYPE (gnu_type);
2602 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2603 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2604 gnu_st_name = concat_name (gnu_st_name, "ST"))
2606 tree eltype = TREE_TYPE (gnu_arr_type);
2608 TYPE_SIZE (gnu_arr_type)
2609 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2610 gnat_entity, gnu_st_name,
2611 definition, false);
2613 /* ??? For now, store the size as a multiple of the
2614 alignment of the element type in bytes so that we
2615 can see the alignment from the tree. */
2616 TYPE_SIZE_UNIT (gnu_arr_type)
2617 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2618 gnat_entity,
2619 concat_name (gnu_st_name, "A_U"),
2620 definition, false,
2621 TYPE_ALIGN (eltype));
2623 /* ??? create_type_decl is not invoked on the inner types so
2624 the MULT_EXPR node built above will never be marked. */
2625 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2629 /* If we need to write out a record type giving the names of the
2630 bounds for debugging purposes, do it now and make the record
2631 type a parallel type. This is not needed for a packed array
2632 since the bounds are conveyed by the original array type. */
2633 if (need_index_type_struct
2634 && debug_info_p
2635 && !Is_Packed_Array_Impl_Type (gnat_entity))
2637 tree gnu_bound_rec = make_node (RECORD_TYPE);
2638 tree gnu_field_list = NULL_TREE;
2639 tree gnu_field;
2641 TYPE_NAME (gnu_bound_rec)
2642 = create_concat_name (gnat_entity, "XA");
2644 for (index = ndim - 1; index >= 0; index--)
2646 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2647 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2649 /* Make sure to reference the types themselves, and not just
2650 their names, as the debugger may fall back on them. */
2651 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2652 gnu_bound_rec, NULL_TREE,
2653 NULL_TREE, 0, 0);
2654 DECL_CHAIN (gnu_field) = gnu_field_list;
2655 gnu_field_list = gnu_field;
2658 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2659 add_parallel_type (gnu_type, gnu_bound_rec);
2662 /* If this is a packed array type, make the original array type a
2663 parallel type. Otherwise, do it for the base array type if it
2664 isn't artificial to make sure it is kept in the debug info. */
2665 if (debug_info_p)
2667 if (Is_Packed_Array_Impl_Type (gnat_entity))
2668 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2669 else
2671 tree gnu_base_decl
2672 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2673 if (!DECL_ARTIFICIAL (gnu_base_decl))
2674 add_parallel_type (gnu_type,
2675 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2679 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2680 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2681 = (Is_Packed_Array_Impl_Type (gnat_entity)
2682 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2684 /* If the size is self-referential and the maximum size doesn't
2685 overflow, use it. */
2686 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2687 && gnu_max_size
2688 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2689 && TREE_OVERFLOW (gnu_max_size))
2690 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2691 && TREE_OVERFLOW (gnu_max_size_unit)))
2693 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2694 TYPE_SIZE (gnu_type));
2695 TYPE_SIZE_UNIT (gnu_type)
2696 = size_binop (MIN_EXPR, gnu_max_size_unit,
2697 TYPE_SIZE_UNIT (gnu_type));
2700 /* Set our alias set to that of our base type. This gives all
2701 array subtypes the same alias set. */
2702 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2704 /* If this is a packed type, make this type the same as the packed
2705 array type, but do some adjusting in the type first. */
2706 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2708 Entity_Id gnat_index;
2709 tree gnu_inner;
2711 /* First finish the type we had been making so that we output
2712 debugging information for it. */
2713 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2714 if (Treat_As_Volatile (gnat_entity))
2715 gnu_type
2716 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2717 /* Make it artificial only if the base type was artificial too.
2718 That's sort of "morally" true and will make it possible for
2719 the debugger to look it up by name in DWARF, which is needed
2720 in order to decode the packed array type. */
2721 gnu_decl
2722 = create_type_decl (gnu_entity_name, gnu_type,
2723 !Comes_From_Source (Etype (gnat_entity))
2724 && !Comes_From_Source (gnat_entity),
2725 debug_info_p, gnat_entity);
2727 /* Save it as our equivalent in case the call below elaborates
2728 this type again. */
2729 save_gnu_tree (gnat_entity, gnu_decl, false);
2731 gnu_decl
2732 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2733 NULL_TREE, 0);
2734 this_made_decl = true;
2735 gnu_type = TREE_TYPE (gnu_decl);
2736 save_gnu_tree (gnat_entity, NULL_TREE, false);
2738 gnu_inner = gnu_type;
2739 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2740 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2741 || TYPE_PADDING_P (gnu_inner)))
2742 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2744 /* We need to attach the index type to the type we just made so
2745 that the actual bounds can later be put into a template. */
2746 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2747 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2748 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2749 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2751 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2753 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2754 TYPE_MODULUS for modular types so we make an extra
2755 subtype if necessary. */
2756 if (TYPE_MODULAR_P (gnu_inner))
2758 tree gnu_subtype
2759 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2760 TREE_TYPE (gnu_subtype) = gnu_inner;
2761 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2762 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2763 TYPE_MIN_VALUE (gnu_inner));
2764 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2765 TYPE_MAX_VALUE (gnu_inner));
2766 gnu_inner = gnu_subtype;
2769 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2771 #ifdef ENABLE_CHECKING
2772 /* Check for other cases of overloading. */
2773 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2774 #endif
2777 for (gnat_index = First_Index (gnat_entity);
2778 Present (gnat_index);
2779 gnat_index = Next_Index (gnat_index))
2780 SET_TYPE_ACTUAL_BOUNDS
2781 (gnu_inner,
2782 tree_cons (NULL_TREE,
2783 get_unpadded_type (Etype (gnat_index)),
2784 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2786 if (Convention (gnat_entity) != Convention_Fortran)
2787 SET_TYPE_ACTUAL_BOUNDS
2788 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2790 if (TREE_CODE (gnu_type) == RECORD_TYPE
2791 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2792 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2796 else
2797 /* Abort if packed array with no Packed_Array_Impl_Type. */
2798 gcc_assert (!Is_Packed (gnat_entity));
2800 break;
2802 case E_String_Literal_Subtype:
2803 /* Create the type for a string literal. */
2805 Entity_Id gnat_full_type
2806 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2807 && Present (Full_View (Etype (gnat_entity)))
2808 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2809 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2810 tree gnu_string_array_type
2811 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2812 tree gnu_string_index_type
2813 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2814 (TYPE_DOMAIN (gnu_string_array_type))));
2815 tree gnu_lower_bound
2816 = convert (gnu_string_index_type,
2817 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2818 tree gnu_length
2819 = UI_To_gnu (String_Literal_Length (gnat_entity),
2820 gnu_string_index_type);
2821 tree gnu_upper_bound
2822 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2823 gnu_lower_bound,
2824 int_const_binop (MINUS_EXPR, gnu_length,
2825 convert (gnu_string_index_type,
2826 integer_one_node)));
2827 tree gnu_index_type
2828 = create_index_type (convert (sizetype, gnu_lower_bound),
2829 convert (sizetype, gnu_upper_bound),
2830 create_range_type (gnu_string_index_type,
2831 gnu_lower_bound,
2832 gnu_upper_bound),
2833 gnat_entity);
2835 gnu_type
2836 = build_nonshared_array_type (gnat_to_gnu_type
2837 (Component_Type (gnat_entity)),
2838 gnu_index_type);
2839 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2840 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2841 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2843 break;
2845 /* Record Types and Subtypes
2847 The following fields are defined on record types:
2849 Has_Discriminants True if the record has discriminants
2850 First_Discriminant Points to head of list of discriminants
2851 First_Entity Points to head of list of fields
2852 Is_Tagged_Type True if the record is tagged
2854 Implementation of Ada records and discriminated records:
2856 A record type definition is transformed into the equivalent of a C
2857 struct definition. The fields that are the discriminants which are
2858 found in the Full_Type_Declaration node and the elements of the
2859 Component_List found in the Record_Type_Definition node. The
2860 Component_List can be a recursive structure since each Variant of
2861 the Variant_Part of the Component_List has a Component_List.
2863 Processing of a record type definition comprises starting the list of
2864 field declarations here from the discriminants and the calling the
2865 function components_to_record to add the rest of the fields from the
2866 component list and return the gnu type node. The function
2867 components_to_record will call itself recursively as it traverses
2868 the tree. */
2870 case E_Record_Type:
2871 if (Has_Complex_Representation (gnat_entity))
2873 gnu_type
2874 = build_complex_type
2875 (get_unpadded_type
2876 (Etype (Defining_Entity
2877 (First (Component_Items
2878 (Component_List
2879 (Type_Definition
2880 (Declaration_Node (gnat_entity)))))))));
2882 break;
2886 Node_Id full_definition = Declaration_Node (gnat_entity);
2887 Node_Id record_definition = Type_Definition (full_definition);
2888 Node_Id gnat_constr;
2889 Entity_Id gnat_field;
2890 tree gnu_field, gnu_field_list = NULL_TREE;
2891 tree gnu_get_parent;
2892 /* Set PACKED in keeping with gnat_to_gnu_field. */
2893 const int packed
2894 = Is_Packed (gnat_entity)
2896 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2897 ? -1
2898 : (Known_Alignment (gnat_entity)
2899 || (Strict_Alignment (gnat_entity)
2900 && Known_RM_Size (gnat_entity)))
2901 ? -2
2902 : 0;
2903 const bool has_discr = Has_Discriminants (gnat_entity);
2904 const bool has_rep = Has_Specified_Layout (gnat_entity);
2905 const bool is_extension
2906 = (Is_Tagged_Type (gnat_entity)
2907 && Nkind (record_definition) == N_Derived_Type_Definition);
2908 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2909 bool all_rep = has_rep;
2911 /* See if all fields have a rep clause. Stop when we find one
2912 that doesn't. */
2913 if (all_rep)
2914 for (gnat_field = First_Entity (gnat_entity);
2915 Present (gnat_field);
2916 gnat_field = Next_Entity (gnat_field))
2917 if ((Ekind (gnat_field) == E_Component
2918 || Ekind (gnat_field) == E_Discriminant)
2919 && No (Component_Clause (gnat_field)))
2921 all_rep = false;
2922 break;
2925 /* If this is a record extension, go a level further to find the
2926 record definition. Also, verify we have a Parent_Subtype. */
2927 if (is_extension)
2929 if (!type_annotate_only
2930 || Present (Record_Extension_Part (record_definition)))
2931 record_definition = Record_Extension_Part (record_definition);
2933 gcc_assert (type_annotate_only
2934 || Present (Parent_Subtype (gnat_entity)));
2937 /* Make a node for the record. If we are not defining the record,
2938 suppress expanding incomplete types. */
2939 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2940 TYPE_NAME (gnu_type) = gnu_entity_name;
2941 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2942 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2943 sorry ("non-default Scalar_Storage_Order");
2944 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2946 if (!definition)
2948 defer_incomplete_level++;
2949 this_deferred = true;
2952 /* If both a size and rep clause was specified, put the size in
2953 the record type now so that it can get the proper mode. */
2954 if (has_rep && Known_RM_Size (gnat_entity))
2955 TYPE_SIZE (gnu_type)
2956 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2958 /* Always set the alignment here so that it can be used to
2959 set the mode, if it is making the alignment stricter. If
2960 it is invalid, it will be checked again below. If this is to
2961 be Atomic, choose a default alignment of a word unless we know
2962 the size and it's smaller. */
2963 if (Known_Alignment (gnat_entity))
2964 TYPE_ALIGN (gnu_type)
2965 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2966 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2968 unsigned int size = UI_To_Int (Esize (gnat_entity));
2969 TYPE_ALIGN (gnu_type)
2970 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2972 /* If a type needs strict alignment, the minimum size will be the
2973 type size instead of the RM size (see validate_size). Cap the
2974 alignment, lest it causes this type size to become too large. */
2975 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2977 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2978 unsigned int raw_align = raw_size & -raw_size;
2979 if (raw_align < BIGGEST_ALIGNMENT)
2980 TYPE_ALIGN (gnu_type) = raw_align;
2982 else
2983 TYPE_ALIGN (gnu_type) = 0;
2985 /* If we have a Parent_Subtype, make a field for the parent. If
2986 this record has rep clauses, force the position to zero. */
2987 if (Present (Parent_Subtype (gnat_entity)))
2989 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2990 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2991 tree gnu_parent;
2993 /* A major complexity here is that the parent subtype will
2994 reference our discriminants in its Stored_Constraint list.
2995 But those must reference the parent component of this record
2996 which is precisely of the parent subtype we have not built yet!
2997 To break the circle we first build a dummy COMPONENT_REF which
2998 represents the "get to the parent" operation and initialize
2999 each of those discriminants to a COMPONENT_REF of the above
3000 dummy parent referencing the corresponding discriminant of the
3001 base type of the parent subtype. */
3002 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3003 build0 (PLACEHOLDER_EXPR, gnu_type),
3004 build_decl (input_location,
3005 FIELD_DECL, NULL_TREE,
3006 gnu_dummy_parent_type),
3007 NULL_TREE);
3009 if (has_discr)
3010 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3011 Present (gnat_field);
3012 gnat_field = Next_Stored_Discriminant (gnat_field))
3013 if (Present (Corresponding_Discriminant (gnat_field)))
3015 tree gnu_field
3016 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3017 (gnat_field));
3018 save_gnu_tree
3019 (gnat_field,
3020 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3021 gnu_get_parent, gnu_field, NULL_TREE),
3022 true);
3025 /* Then we build the parent subtype. If it has discriminants but
3026 the type itself has unknown discriminants, this means that it
3027 doesn't contain information about how the discriminants are
3028 derived from those of the ancestor type, so it cannot be used
3029 directly. Instead it is built by cloning the parent subtype
3030 of the underlying record view of the type, for which the above
3031 derivation of discriminants has been made explicit. */
3032 if (Has_Discriminants (gnat_parent)
3033 && Has_Unknown_Discriminants (gnat_entity))
3035 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3037 /* If we are defining the type, the underlying record
3038 view must already have been elaborated at this point.
3039 Otherwise do it now as its parent subtype cannot be
3040 technically elaborated on its own. */
3041 if (definition)
3042 gcc_assert (present_gnu_tree (gnat_uview));
3043 else
3044 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3046 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3048 /* Substitute the "get to the parent" of the type for that
3049 of its underlying record view in the cloned type. */
3050 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3051 Present (gnat_field);
3052 gnat_field = Next_Stored_Discriminant (gnat_field))
3053 if (Present (Corresponding_Discriminant (gnat_field)))
3055 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3056 tree gnu_ref
3057 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3058 gnu_get_parent, gnu_field, NULL_TREE);
3059 gnu_parent
3060 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3063 else
3064 gnu_parent = gnat_to_gnu_type (gnat_parent);
3066 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3067 initially built. The discriminants must reference the fields
3068 of the parent subtype and not those of its base type for the
3069 placeholder machinery to properly work. */
3070 if (has_discr)
3072 /* The actual parent subtype is the full view. */
3073 if (IN (Ekind (gnat_parent), Private_Kind))
3075 if (Present (Full_View (gnat_parent)))
3076 gnat_parent = Full_View (gnat_parent);
3077 else
3078 gnat_parent = Underlying_Full_View (gnat_parent);
3081 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3082 Present (gnat_field);
3083 gnat_field = Next_Stored_Discriminant (gnat_field))
3084 if (Present (Corresponding_Discriminant (gnat_field)))
3086 Entity_Id field;
3087 for (field = First_Stored_Discriminant (gnat_parent);
3088 Present (field);
3089 field = Next_Stored_Discriminant (field))
3090 if (same_discriminant_p (gnat_field, field))
3091 break;
3092 gcc_assert (Present (field));
3093 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3094 = gnat_to_gnu_field_decl (field);
3098 /* The "get to the parent" COMPONENT_REF must be given its
3099 proper type... */
3100 TREE_TYPE (gnu_get_parent) = gnu_parent;
3102 /* ...and reference the _Parent field of this record. */
3103 gnu_field
3104 = create_field_decl (parent_name_id,
3105 gnu_parent, gnu_type,
3106 has_rep
3107 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3108 has_rep
3109 ? bitsize_zero_node : NULL_TREE,
3110 0, 1);
3111 DECL_INTERNAL_P (gnu_field) = 1;
3112 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3113 TYPE_FIELDS (gnu_type) = gnu_field;
3116 /* Make the fields for the discriminants and put them into the record
3117 unless it's an Unchecked_Union. */
3118 if (has_discr)
3119 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3120 Present (gnat_field);
3121 gnat_field = Next_Stored_Discriminant (gnat_field))
3123 /* If this is a record extension and this discriminant is the
3124 renaming of another discriminant, we've handled it above. */
3125 if (Present (Parent_Subtype (gnat_entity))
3126 && Present (Corresponding_Discriminant (gnat_field)))
3127 continue;
3129 gnu_field
3130 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3131 debug_info_p);
3133 /* Make an expression using a PLACEHOLDER_EXPR from the
3134 FIELD_DECL node just created and link that with the
3135 corresponding GNAT defining identifier. */
3136 save_gnu_tree (gnat_field,
3137 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3138 build0 (PLACEHOLDER_EXPR, gnu_type),
3139 gnu_field, NULL_TREE),
3140 true);
3142 if (!is_unchecked_union)
3144 DECL_CHAIN (gnu_field) = gnu_field_list;
3145 gnu_field_list = gnu_field;
3149 /* If we have a derived untagged type that renames discriminants in
3150 the root type, the (stored) discriminants are a just copy of the
3151 discriminants of the root type. This means that any constraints
3152 added by the renaming in the derivation are disregarded as far
3153 as the layout of the derived type is concerned. To rescue them,
3154 we change the type of the (stored) discriminants to a subtype
3155 with the bounds of the type of the visible discriminants. */
3156 if (has_discr
3157 && !is_extension
3158 && Stored_Constraint (gnat_entity) != No_Elist)
3159 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3160 gnat_constr != No_Elmt;
3161 gnat_constr = Next_Elmt (gnat_constr))
3162 if (Nkind (Node (gnat_constr)) == N_Identifier
3163 /* Ignore access discriminants. */
3164 && !Is_Access_Type (Etype (Node (gnat_constr)))
3165 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3167 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3168 tree gnu_discr_type, gnu_ref;
3170 /* If the scope of the discriminant is not the record type,
3171 this means that we're processing the implicit full view
3172 of a type derived from a private discriminated type: in
3173 this case, the Stored_Constraint list is simply copied
3174 from the partial view, see Build_Derived_Private_Type.
3175 So we need to retrieve the corresponding discriminant
3176 of the implicit full view, otherwise we will abort. */
3177 if (Scope (gnat_discr) != gnat_entity)
3179 Entity_Id field;
3180 for (field = First_Entity (gnat_entity);
3181 Present (field);
3182 field = Next_Entity (field))
3183 if (Ekind (field) == E_Discriminant
3184 && same_discriminant_p (gnat_discr, field))
3185 break;
3186 gcc_assert (Present (field));
3187 gnat_discr = field;
3190 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3191 gnu_ref
3192 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3193 NULL_TREE, 0);
3195 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3196 just above for one of the stored discriminants. */
3197 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3199 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3201 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3202 tree gnu_subtype
3203 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3204 ? make_unsigned_type (prec) : make_signed_type (prec);
3205 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3206 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3207 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3208 TYPE_MIN_VALUE (gnu_discr_type));
3209 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3210 TYPE_MAX_VALUE (gnu_discr_type));
3211 TREE_TYPE (gnu_ref)
3212 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3216 /* Add the fields into the record type and finish it up. */
3217 components_to_record (gnu_type, Component_List (record_definition),
3218 gnu_field_list, packed, definition, false,
3219 all_rep, is_unchecked_union,
3220 !Comes_From_Source (gnat_entity), debug_info_p,
3221 false, OK_To_Reorder_Components (gnat_entity),
3222 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3224 /* If it is passed by reference, force BLKmode to ensure that objects
3225 of this type will always be put in memory. */
3226 if (TYPE_MODE (gnu_type) != BLKmode
3227 && Is_By_Reference_Type (gnat_entity))
3228 SET_TYPE_MODE (gnu_type, BLKmode);
3230 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3232 /* Fill in locations of fields. */
3233 annotate_rep (gnat_entity, gnu_type);
3235 /* If there are any entities in the chain corresponding to components
3236 that we did not elaborate, ensure we elaborate their types if they
3237 are Itypes. */
3238 for (gnat_temp = First_Entity (gnat_entity);
3239 Present (gnat_temp);
3240 gnat_temp = Next_Entity (gnat_temp))
3241 if ((Ekind (gnat_temp) == E_Component
3242 || Ekind (gnat_temp) == E_Discriminant)
3243 && Is_Itype (Etype (gnat_temp))
3244 && !present_gnu_tree (gnat_temp))
3245 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3247 /* If this is a record type associated with an exception definition,
3248 equate its fields to those of the standard exception type. This
3249 will make it possible to convert between them. */
3250 if (gnu_entity_name == exception_data_name_id)
3252 tree gnu_std_field;
3253 for (gnu_field = TYPE_FIELDS (gnu_type),
3254 gnu_std_field = TYPE_FIELDS (except_type_node);
3255 gnu_field;
3256 gnu_field = DECL_CHAIN (gnu_field),
3257 gnu_std_field = DECL_CHAIN (gnu_std_field))
3258 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3259 gcc_assert (!gnu_std_field);
3262 break;
3264 case E_Class_Wide_Subtype:
3265 /* If an equivalent type is present, that is what we should use.
3266 Otherwise, fall through to handle this like a record subtype
3267 since it may have constraints. */
3268 if (gnat_equiv_type != gnat_entity)
3270 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3271 maybe_present = true;
3272 break;
3275 /* ... fall through ... */
3277 case E_Record_Subtype:
3278 /* If Cloned_Subtype is Present it means this record subtype has
3279 identical layout to that type or subtype and we should use
3280 that GCC type for this one. The front end guarantees that
3281 the component list is shared. */
3282 if (Present (Cloned_Subtype (gnat_entity)))
3284 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3285 NULL_TREE, 0);
3286 maybe_present = true;
3287 break;
3290 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3291 changing the type, make a new type with each field having the type of
3292 the field in the new subtype but the position computed by transforming
3293 every discriminant reference according to the constraints. We don't
3294 see any difference between private and non-private type here since
3295 derivations from types should have been deferred until the completion
3296 of the private type. */
3297 else
3299 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3300 tree gnu_base_type;
3302 if (!definition)
3304 defer_incomplete_level++;
3305 this_deferred = true;
3308 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3310 if (present_gnu_tree (gnat_entity))
3312 maybe_present = true;
3313 break;
3316 /* If this is a record subtype associated with a dispatch table,
3317 strip the suffix. This is necessary to make sure 2 different
3318 subtypes associated with the imported and exported views of a
3319 dispatch table are properly merged in LTO mode. */
3320 if (Is_Dispatch_Table_Entity (gnat_entity))
3322 char *p;
3323 Get_Encoded_Name (gnat_entity);
3324 p = strchr (Name_Buffer, '_');
3325 gcc_assert (p);
3326 strcpy (p+2, "dtS");
3327 gnu_entity_name = get_identifier (Name_Buffer);
3330 /* When the subtype has discriminants and these discriminants affect
3331 the initial shape it has inherited, factor them in. But for an
3332 Unchecked_Union (it must be an Itype), just return the type.
3333 We can't just test Is_Constrained because private subtypes without
3334 discriminants of types with discriminants with default expressions
3335 are Is_Constrained but aren't constrained! */
3336 if (IN (Ekind (gnat_base_type), Record_Kind)
3337 && !Is_Unchecked_Union (gnat_base_type)
3338 && !Is_For_Access_Subtype (gnat_entity)
3339 && Has_Discriminants (gnat_entity)
3340 && Is_Constrained (gnat_entity)
3341 && Stored_Constraint (gnat_entity) != No_Elist)
3343 vec<subst_pair> gnu_subst_list
3344 = build_subst_list (gnat_entity, gnat_base_type, definition);
3345 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3346 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3347 bool selected_variant = false, all_constant_pos = true;
3348 Entity_Id gnat_field;
3349 vec<variant_desc> gnu_variant_list;
3351 gnu_type = make_node (RECORD_TYPE);
3352 TYPE_NAME (gnu_type) = gnu_entity_name;
3353 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3354 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3356 /* Set the size, alignment and alias set of the new type to
3357 match that of the old one, doing required substitutions. */
3358 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3359 gnu_subst_list);
3361 if (TYPE_IS_PADDING_P (gnu_base_type))
3362 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3363 else
3364 gnu_unpad_base_type = gnu_base_type;
3366 /* Look for REP and variant parts in the base type. */
3367 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3368 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3370 /* If there is a variant part, we must compute whether the
3371 constraints statically select a particular variant. If
3372 so, we simply drop the qualified union and flatten the
3373 list of fields. Otherwise we'll build a new qualified
3374 union for the variants that are still relevant. */
3375 if (gnu_variant_part)
3377 variant_desc *v;
3378 unsigned int i;
3380 gnu_variant_list
3381 = build_variant_list (TREE_TYPE (gnu_variant_part),
3382 gnu_subst_list,
3383 vNULL);
3385 /* If all the qualifiers are unconditionally true, the
3386 innermost variant is statically selected. */
3387 selected_variant = true;
3388 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3389 if (!integer_onep (v->qual))
3391 selected_variant = false;
3392 break;
3395 /* Otherwise, create the new variants. */
3396 if (!selected_variant)
3397 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3399 tree old_variant = v->type;
3400 tree new_variant = make_node (RECORD_TYPE);
3401 tree suffix
3402 = concat_name (DECL_NAME (gnu_variant_part),
3403 IDENTIFIER_POINTER
3404 (DECL_NAME (v->field)));
3405 TYPE_NAME (new_variant)
3406 = concat_name (TYPE_NAME (gnu_type),
3407 IDENTIFIER_POINTER (suffix));
3408 copy_and_substitute_in_size (new_variant, old_variant,
3409 gnu_subst_list);
3410 v->new_type = new_variant;
3413 else
3415 gnu_variant_list.create (0);
3416 selected_variant = false;
3419 /* Make a list of fields and their position in the base type. */
3420 gnu_pos_list
3421 = build_position_list (gnu_unpad_base_type,
3422 gnu_variant_list.exists ()
3423 && !selected_variant,
3424 size_zero_node, bitsize_zero_node,
3425 BIGGEST_ALIGNMENT, NULL_TREE);
3427 /* Now go down every component in the subtype and compute its
3428 size and position from those of the component in the base
3429 type and from the constraints of the subtype. */
3430 for (gnat_field = First_Entity (gnat_entity);
3431 Present (gnat_field);
3432 gnat_field = Next_Entity (gnat_field))
3433 if ((Ekind (gnat_field) == E_Component
3434 || Ekind (gnat_field) == E_Discriminant)
3435 && !(Present (Corresponding_Discriminant (gnat_field))
3436 && Is_Tagged_Type (gnat_base_type))
3437 && Underlying_Type
3438 (Scope (Original_Record_Component (gnat_field)))
3439 == gnat_base_type)
3441 Name_Id gnat_name = Chars (gnat_field);
3442 Entity_Id gnat_old_field
3443 = Original_Record_Component (gnat_field);
3444 tree gnu_old_field
3445 = gnat_to_gnu_field_decl (gnat_old_field);
3446 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3447 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3448 tree gnu_cont_type, gnu_last = NULL_TREE;
3450 /* If the type is the same, retrieve the GCC type from the
3451 old field to take into account possible adjustments. */
3452 if (Etype (gnat_field) == Etype (gnat_old_field))
3453 gnu_field_type = TREE_TYPE (gnu_old_field);
3454 else
3455 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3457 /* If there was a component clause, the field types must be
3458 the same for the type and subtype, so copy the data from
3459 the old field to avoid recomputation here. Also if the
3460 field is justified modular and the optimization in
3461 gnat_to_gnu_field was applied. */
3462 if (Present (Component_Clause (gnat_old_field))
3463 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3464 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3465 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3466 == TREE_TYPE (gnu_old_field)))
3468 gnu_size = DECL_SIZE (gnu_old_field);
3469 gnu_field_type = TREE_TYPE (gnu_old_field);
3472 /* If the old field was packed and of constant size, we
3473 have to get the old size here, as it might differ from
3474 what the Etype conveys and the latter might overlap
3475 onto the following field. Try to arrange the type for
3476 possible better packing along the way. */
3477 else if (DECL_PACKED (gnu_old_field)
3478 && TREE_CODE (DECL_SIZE (gnu_old_field))
3479 == INTEGER_CST)
3481 gnu_size = DECL_SIZE (gnu_old_field);
3482 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3483 && !TYPE_FAT_POINTER_P (gnu_field_type)
3484 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3485 gnu_field_type
3486 = make_packable_type (gnu_field_type, true);
3489 else
3490 gnu_size = TYPE_SIZE (gnu_field_type);
3492 /* If the context of the old field is the base type or its
3493 REP part (if any), put the field directly in the new
3494 type; otherwise look up the context in the variant list
3495 and put the field either in the new type if there is a
3496 selected variant or in one of the new variants. */
3497 if (gnu_context == gnu_unpad_base_type
3498 || (gnu_rep_part
3499 && gnu_context == TREE_TYPE (gnu_rep_part)))
3500 gnu_cont_type = gnu_type;
3501 else
3503 variant_desc *v;
3504 unsigned int i;
3505 tree rep_part;
3507 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3508 if (gnu_context == v->type
3509 || ((rep_part = get_rep_part (v->type))
3510 && gnu_context == TREE_TYPE (rep_part)))
3511 break;
3512 if (v)
3514 if (selected_variant)
3515 gnu_cont_type = gnu_type;
3516 else
3517 gnu_cont_type = v->new_type;
3519 else
3520 /* The front-end may pass us "ghost" components if
3521 it fails to recognize that a constrained subtype
3522 is statically constrained. Discard them. */
3523 continue;
3526 /* Now create the new field modeled on the old one. */
3527 gnu_field
3528 = create_field_decl_from (gnu_old_field, gnu_field_type,
3529 gnu_cont_type, gnu_size,
3530 gnu_pos_list, gnu_subst_list);
3531 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3533 /* Put it in one of the new variants directly. */
3534 if (gnu_cont_type != gnu_type)
3536 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3537 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3540 /* To match the layout crafted in components_to_record,
3541 if this is the _Tag or _Parent field, put it before
3542 any other fields. */
3543 else if (gnat_name == Name_uTag
3544 || gnat_name == Name_uParent)
3545 gnu_field_list = chainon (gnu_field_list, gnu_field);
3547 /* Similarly, if this is the _Controller field, put
3548 it before the other fields except for the _Tag or
3549 _Parent field. */
3550 else if (gnat_name == Name_uController && gnu_last)
3552 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3553 DECL_CHAIN (gnu_last) = gnu_field;
3556 /* Otherwise, if this is a regular field, put it after
3557 the other fields. */
3558 else
3560 DECL_CHAIN (gnu_field) = gnu_field_list;
3561 gnu_field_list = gnu_field;
3562 if (!gnu_last)
3563 gnu_last = gnu_field;
3564 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3565 all_constant_pos = false;
3568 save_gnu_tree (gnat_field, gnu_field, false);
3571 /* If there is a variant list, a selected variant and the fields
3572 all have a constant position, put them in order of increasing
3573 position to match that of constant CONSTRUCTORs. Likewise if
3574 there is no variant list but a REP part, since the latter has
3575 been flattened in the process. */
3576 if (((gnu_variant_list.exists () && selected_variant)
3577 || (!gnu_variant_list.exists () && gnu_rep_part))
3578 && all_constant_pos)
3580 const int len = list_length (gnu_field_list);
3581 tree *field_arr = XALLOCAVEC (tree, len), t;
3582 int i;
3584 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3585 field_arr[i] = t;
3587 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3589 gnu_field_list = NULL_TREE;
3590 for (i = 0; i < len; i++)
3592 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3593 gnu_field_list = field_arr[i];
3597 /* If there is a variant list and no selected variant, we need
3598 to create the nest of variant parts from the old nest. */
3599 else if (gnu_variant_list.exists () && !selected_variant)
3601 tree new_variant_part
3602 = create_variant_part_from (gnu_variant_part,
3603 gnu_variant_list, gnu_type,
3604 gnu_pos_list, gnu_subst_list);
3605 DECL_CHAIN (new_variant_part) = gnu_field_list;
3606 gnu_field_list = new_variant_part;
3609 /* Now go through the entities again looking for Itypes that
3610 we have not elaborated but should (e.g., Etypes of fields
3611 that have Original_Components). */
3612 for (gnat_field = First_Entity (gnat_entity);
3613 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3614 if ((Ekind (gnat_field) == E_Discriminant
3615 || Ekind (gnat_field) == E_Component)
3616 && !present_gnu_tree (Etype (gnat_field)))
3617 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3619 /* Do not emit debug info for the type yet since we're going to
3620 modify it below. */
3621 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3622 false);
3623 compute_record_mode (gnu_type);
3625 /* See the E_Record_Type case for the rationale. */
3626 if (TYPE_MODE (gnu_type) != BLKmode
3627 && Is_By_Reference_Type (gnat_entity))
3628 SET_TYPE_MODE (gnu_type, BLKmode);
3630 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3632 /* Fill in locations of fields. */
3633 annotate_rep (gnat_entity, gnu_type);
3635 /* If debugging information is being written for the type, write
3636 a record that shows what we are a subtype of and also make a
3637 variable that indicates our size, if still variable. */
3638 if (debug_info_p)
3640 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3641 tree gnu_unpad_base_name
3642 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3643 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3645 TYPE_NAME (gnu_subtype_marker)
3646 = create_concat_name (gnat_entity, "XVS");
3647 finish_record_type (gnu_subtype_marker,
3648 create_field_decl (gnu_unpad_base_name,
3649 build_reference_type
3650 (gnu_unpad_base_type),
3651 gnu_subtype_marker,
3652 NULL_TREE, NULL_TREE,
3653 0, 0),
3654 0, true);
3656 add_parallel_type (gnu_type, gnu_subtype_marker);
3658 if (definition
3659 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3660 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3661 TYPE_SIZE_UNIT (gnu_subtype_marker)
3662 = create_var_decl (create_concat_name (gnat_entity,
3663 "XVZ"),
3664 NULL_TREE, sizetype, gnu_size_unit,
3665 false, false, false, false, NULL,
3666 gnat_entity);
3669 gnu_variant_list.release ();
3670 gnu_subst_list.release ();
3672 /* Now we can finalize it. */
3673 rest_of_record_type_compilation (gnu_type);
3676 /* Otherwise, go down all the components in the new type and make
3677 them equivalent to those in the base type. */
3678 else
3680 gnu_type = gnu_base_type;
3682 for (gnat_temp = First_Entity (gnat_entity);
3683 Present (gnat_temp);
3684 gnat_temp = Next_Entity (gnat_temp))
3685 if ((Ekind (gnat_temp) == E_Discriminant
3686 && !Is_Unchecked_Union (gnat_base_type))
3687 || Ekind (gnat_temp) == E_Component)
3688 save_gnu_tree (gnat_temp,
3689 gnat_to_gnu_field_decl
3690 (Original_Record_Component (gnat_temp)),
3691 false);
3694 break;
3696 case E_Access_Subprogram_Type:
3697 /* Use the special descriptor type for dispatch tables if needed,
3698 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3699 Note that we are only required to do so for static tables in
3700 order to be compatible with the C++ ABI, but Ada 2005 allows
3701 to extend library level tagged types at the local level so
3702 we do it in the non-static case as well. */
3703 if (TARGET_VTABLE_USES_DESCRIPTORS
3704 && Is_Dispatch_Table_Entity (gnat_entity))
3706 gnu_type = fdesc_type_node;
3707 gnu_size = TYPE_SIZE (gnu_type);
3708 break;
3711 /* ... fall through ... */
3713 case E_Anonymous_Access_Subprogram_Type:
3714 /* If we are not defining this entity, and we have incomplete
3715 entities being processed above us, make a dummy type and
3716 fill it in later. */
3717 if (!definition && defer_incomplete_level != 0)
3719 struct incomplete *p = XNEW (struct incomplete);
3721 gnu_type
3722 = build_pointer_type
3723 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3724 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3725 !Comes_From_Source (gnat_entity),
3726 debug_info_p, gnat_entity);
3727 this_made_decl = true;
3728 gnu_type = TREE_TYPE (gnu_decl);
3729 save_gnu_tree (gnat_entity, gnu_decl, false);
3730 saved = true;
3732 p->old_type = TREE_TYPE (gnu_type);
3733 p->full_type = Directly_Designated_Type (gnat_entity);
3734 p->next = defer_incomplete_list;
3735 defer_incomplete_list = p;
3736 break;
3739 /* ... fall through ... */
3741 case E_Allocator_Type:
3742 case E_Access_Type:
3743 case E_Access_Attribute_Type:
3744 case E_Anonymous_Access_Type:
3745 case E_General_Access_Type:
3747 /* The designated type and its equivalent type for gigi. */
3748 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3749 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3750 /* Whether it comes from a limited with. */
3751 bool is_from_limited_with
3752 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3753 && From_Limited_With (gnat_desig_equiv));
3754 /* The "full view" of the designated type. If this is an incomplete
3755 entity from a limited with, treat its non-limited view as the full
3756 view. Otherwise, if this is an incomplete or private type, use the
3757 full view. In the former case, we might point to a private type,
3758 in which case, we need its full view. Also, we want to look at the
3759 actual type used for the representation, so this takes a total of
3760 three steps. */
3761 Entity_Id gnat_desig_full_direct_first
3762 = (is_from_limited_with
3763 ? Non_Limited_View (gnat_desig_equiv)
3764 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3765 ? Full_View (gnat_desig_equiv) : Empty));
3766 Entity_Id gnat_desig_full_direct
3767 = ((is_from_limited_with
3768 && Present (gnat_desig_full_direct_first)
3769 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3770 ? Full_View (gnat_desig_full_direct_first)
3771 : gnat_desig_full_direct_first);
3772 Entity_Id gnat_desig_full
3773 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3774 /* The type actually used to represent the designated type, either
3775 gnat_desig_full or gnat_desig_equiv. */
3776 Entity_Id gnat_desig_rep;
3777 /* True if this is a pointer to an unconstrained array. */
3778 bool is_unconstrained_array;
3779 /* We want to know if we'll be seeing the freeze node for any
3780 incomplete type we may be pointing to. */
3781 bool in_main_unit
3782 = (Present (gnat_desig_full)
3783 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3784 : In_Extended_Main_Code_Unit (gnat_desig_type));
3785 /* True if we make a dummy type here. */
3786 bool made_dummy = false;
3787 /* The mode to be used for the pointer type. */
3788 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3789 /* The GCC type used for the designated type. */
3790 tree gnu_desig_type = NULL_TREE;
3792 if (!targetm.valid_pointer_mode (p_mode))
3793 p_mode = ptr_mode;
3795 /* If either the designated type or its full view is an unconstrained
3796 array subtype, replace it with the type it's a subtype of. This
3797 avoids problems with multiple copies of unconstrained array types.
3798 Likewise, if the designated type is a subtype of an incomplete
3799 record type, use the parent type to avoid order of elaboration
3800 issues. This can lose some code efficiency, but there is no
3801 alternative. */
3802 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3803 && !Is_Constrained (gnat_desig_equiv))
3804 gnat_desig_equiv = Etype (gnat_desig_equiv);
3805 if (Present (gnat_desig_full)
3806 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3807 && !Is_Constrained (gnat_desig_full))
3808 || (Ekind (gnat_desig_full) == E_Record_Subtype
3809 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3810 gnat_desig_full = Etype (gnat_desig_full);
3812 /* Set the type that's actually the representation of the designated
3813 type and also flag whether we have a unconstrained array. */
3814 gnat_desig_rep
3815 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3816 is_unconstrained_array
3817 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3819 /* If we are pointing to an incomplete type whose completion is an
3820 unconstrained array, make dummy fat and thin pointer types to it.
3821 Likewise if the type itself is dummy or an unconstrained array. */
3822 if (is_unconstrained_array
3823 && (Present (gnat_desig_full)
3824 || (present_gnu_tree (gnat_desig_equiv)
3825 && TYPE_IS_DUMMY_P
3826 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3827 || (!in_main_unit
3828 && defer_incomplete_level != 0
3829 && !present_gnu_tree (gnat_desig_equiv))
3830 || (in_main_unit
3831 && is_from_limited_with
3832 && Present (Freeze_Node (gnat_desig_equiv)))))
3834 if (present_gnu_tree (gnat_desig_rep))
3835 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3836 else
3838 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3839 made_dummy = true;
3842 /* If the call above got something that has a pointer, the pointer
3843 is our type. This could have happened either because the type
3844 was elaborated or because somebody else executed the code. */
3845 if (!TYPE_POINTER_TO (gnu_desig_type))
3846 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3847 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3850 /* If we already know what the full type is, use it. */
3851 else if (Present (gnat_desig_full)
3852 && present_gnu_tree (gnat_desig_full))
3853 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3855 /* Get the type of the thing we are to point to and build a pointer to
3856 it. If it is a reference to an incomplete or private type with a
3857 full view that is a record, make a dummy type node and get the
3858 actual type later when we have verified it is safe. */
3859 else if ((!in_main_unit
3860 && !present_gnu_tree (gnat_desig_equiv)
3861 && Present (gnat_desig_full)
3862 && !present_gnu_tree (gnat_desig_full)
3863 && Is_Record_Type (gnat_desig_full))
3864 /* Likewise if we are pointing to a record or array and we are
3865 to defer elaborating incomplete types. We do this as this
3866 access type may be the full view of a private type. Note
3867 that the unconstrained array case is handled above. */
3868 || ((!in_main_unit || imported_p)
3869 && defer_incomplete_level != 0
3870 && !present_gnu_tree (gnat_desig_equiv)
3871 && (Is_Record_Type (gnat_desig_rep)
3872 || Is_Array_Type (gnat_desig_rep)))
3873 /* If this is a reference from a limited_with type back to our
3874 main unit and there's a freeze node for it, either we have
3875 already processed the declaration and made the dummy type,
3876 in which case we just reuse the latter, or we have not yet,
3877 in which case we make the dummy type and it will be reused
3878 when the declaration is finally processed. In both cases,
3879 the pointer eventually created below will be automatically
3880 adjusted when the freeze node is processed. Note that the
3881 unconstrained array case is handled above. */
3882 || (in_main_unit
3883 && is_from_limited_with
3884 && Present (Freeze_Node (gnat_desig_rep))))
3886 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3887 made_dummy = true;
3890 /* Otherwise handle the case of a pointer to itself. */
3891 else if (gnat_desig_equiv == gnat_entity)
3893 gnu_type
3894 = build_pointer_type_for_mode (void_type_node, p_mode,
3895 No_Strict_Aliasing (gnat_entity));
3896 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3899 /* If expansion is disabled, the equivalent type of a concurrent type
3900 is absent, so build a dummy pointer type. */
3901 else if (type_annotate_only && No (gnat_desig_equiv))
3902 gnu_type = ptr_void_type_node;
3904 /* Finally, handle the default case where we can just elaborate our
3905 designated type. */
3906 else
3907 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3909 /* It is possible that a call to gnat_to_gnu_type above resolved our
3910 type. If so, just return it. */
3911 if (present_gnu_tree (gnat_entity))
3913 maybe_present = true;
3914 break;
3917 /* If we haven't done it yet, build the pointer type the usual way. */
3918 if (!gnu_type)
3920 /* Modify the designated type if we are pointing only to constant
3921 objects, but don't do it for unconstrained arrays. */
3922 if (Is_Access_Constant (gnat_entity)
3923 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3925 gnu_desig_type
3926 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3928 /* Some extra processing is required if we are building a
3929 pointer to an incomplete type (in the GCC sense). We might
3930 have such a type if we just made a dummy, or directly out
3931 of the call to gnat_to_gnu_type above if we are processing
3932 an access type for a record component designating the
3933 record type itself. */
3934 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3936 /* We must ensure that the pointer to variant we make will
3937 be processed by update_pointer_to when the initial type
3938 is completed. Pretend we made a dummy and let further
3939 processing act as usual. */
3940 made_dummy = true;
3942 /* We must ensure that update_pointer_to will not retrieve
3943 the dummy variant when building a properly qualified
3944 version of the complete type. We take advantage of the
3945 fact that get_qualified_type is requiring TYPE_NAMEs to
3946 match to influence build_qualified_type and then also
3947 update_pointer_to here. */
3948 TYPE_NAME (gnu_desig_type)
3949 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3953 gnu_type
3954 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3955 No_Strict_Aliasing (gnat_entity));
3958 /* If we are not defining this object and we have made a dummy pointer,
3959 save our current definition, evaluate the actual type, and replace
3960 the tentative type we made with the actual one. If we are to defer
3961 actually looking up the actual type, make an entry in the deferred
3962 list. If this is from a limited with, we may have to defer to the
3963 end of the current unit. */
3964 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3966 tree gnu_old_desig_type;
3968 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3970 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3971 if (esize == POINTER_SIZE)
3972 gnu_type = build_pointer_type
3973 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3975 else
3976 gnu_old_desig_type = TREE_TYPE (gnu_type);
3978 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3979 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3980 !Comes_From_Source (gnat_entity),
3981 debug_info_p, gnat_entity);
3982 this_made_decl = true;
3983 gnu_type = TREE_TYPE (gnu_decl);
3984 save_gnu_tree (gnat_entity, gnu_decl, false);
3985 saved = true;
3987 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3988 update gnu_old_desig_type directly, in which case it will not be
3989 a dummy type any more when we get into update_pointer_to.
3991 This can happen e.g. when the designated type is a record type,
3992 because their elaboration starts with an initial node from
3993 make_dummy_type, which may be the same node as the one we got.
3995 Besides, variants of this non-dummy type might have been created
3996 along the way. update_pointer_to is expected to properly take
3997 care of those situations. */
3998 if (defer_incomplete_level == 0 && !is_from_limited_with)
4000 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4001 gnat_to_gnu_type (gnat_desig_equiv));
4003 else
4005 struct incomplete *p = XNEW (struct incomplete);
4006 struct incomplete **head
4007 = (is_from_limited_with
4008 ? &defer_limited_with : &defer_incomplete_list);
4009 p->old_type = gnu_old_desig_type;
4010 p->full_type = gnat_desig_equiv;
4011 p->next = *head;
4012 *head = p;
4016 break;
4018 case E_Access_Protected_Subprogram_Type:
4019 case E_Anonymous_Access_Protected_Subprogram_Type:
4020 if (type_annotate_only && No (gnat_equiv_type))
4021 gnu_type = ptr_void_type_node;
4022 else
4024 /* The run-time representation is the equivalent type. */
4025 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4026 maybe_present = true;
4029 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4030 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4031 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4032 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4033 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4034 NULL_TREE, 0);
4036 break;
4038 case E_Access_Subtype:
4040 /* We treat this as identical to its base type; any constraint is
4041 meaningful only to the front-end.
4043 The designated type must be elaborated as well, if it does
4044 not have its own freeze node. Designated (sub)types created
4045 for constrained components of records with discriminants are
4046 not frozen by the front-end and thus not elaborated by gigi,
4047 because their use may appear before the base type is frozen,
4048 and because it is not clear that they are needed anywhere in
4049 gigi. With the current model, there is no correct place where
4050 they could be elaborated. */
4052 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4053 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4054 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4055 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4056 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4058 /* If we are not defining this entity, and we have incomplete
4059 entities being processed above us, make a dummy type and
4060 elaborate it later. */
4061 if (!definition && defer_incomplete_level != 0)
4063 struct incomplete *p = XNEW (struct incomplete);
4065 p->old_type
4066 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4067 p->full_type = Directly_Designated_Type (gnat_entity);
4068 p->next = defer_incomplete_list;
4069 defer_incomplete_list = p;
4071 else if (!IN (Ekind (Base_Type
4072 (Directly_Designated_Type (gnat_entity))),
4073 Incomplete_Or_Private_Kind))
4074 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4075 NULL_TREE, 0);
4078 maybe_present = true;
4079 break;
4081 /* Subprogram Entities
4083 The following access functions are defined for subprograms:
4085 Etype Return type or Standard_Void_Type.
4086 First_Formal The first formal parameter.
4087 Is_Imported Indicates that the subprogram has appeared in
4088 an INTERFACE or IMPORT pragma. For now we
4089 assume that the external language is C.
4090 Is_Exported Likewise but for an EXPORT pragma.
4091 Is_Inlined True if the subprogram is to be inlined.
4093 Each parameter is first checked by calling must_pass_by_ref on its
4094 type to determine if it is passed by reference. For parameters which
4095 are copied in, if they are Ada In Out or Out parameters, their return
4096 value becomes part of a record which becomes the return type of the
4097 function (C function - note that this applies only to Ada procedures
4098 so there is no Ada return type). Additional code to store back the
4099 parameters will be generated on the caller side. This transformation
4100 is done here, not in the front-end.
4102 The intended result of the transformation can be seen from the
4103 equivalent source rewritings that follow:
4105 struct temp {int a,b};
4106 procedure P (A,B: In Out ...) is temp P (int A,B)
4107 begin {
4108 .. ..
4109 end P; return {A,B};
4112 temp t;
4113 P(X,Y); t = P(X,Y);
4114 X = t.a , Y = t.b;
4116 For subprogram types we need to perform mainly the same conversions to
4117 GCC form that are needed for procedures and function declarations. The
4118 only difference is that at the end, we make a type declaration instead
4119 of a function declaration. */
4121 case E_Subprogram_Type:
4122 case E_Function:
4123 case E_Procedure:
4125 /* The type returned by a function or else Standard_Void_Type for a
4126 procedure. */
4127 Entity_Id gnat_return_type = Etype (gnat_entity);
4128 tree gnu_return_type;
4129 /* The first GCC parameter declaration (a PARM_DECL node). The
4130 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4131 actually is the head of this parameter list. */
4132 tree gnu_param_list = NULL_TREE;
4133 /* Non-null for subprograms containing parameters passed by copy-in
4134 copy-out (Ada In Out or Out parameters not passed by reference),
4135 in which case it is the list of nodes used to specify the values
4136 of the In Out/Out parameters that are returned as a record upon
4137 procedure return. The TREE_PURPOSE of an element of this list is
4138 a field of the record and the TREE_VALUE is the PARM_DECL
4139 corresponding to that field. This list will be saved in the
4140 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4141 tree gnu_cico_list = NULL_TREE;
4142 /* List of fields in return type of procedure with copy-in copy-out
4143 parameters. */
4144 tree gnu_field_list = NULL_TREE;
4145 /* If an import pragma asks to map this subprogram to a GCC builtin,
4146 this is the builtin DECL node. */
4147 tree gnu_builtin_decl = NULL_TREE;
4148 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4149 Entity_Id gnat_param;
4150 enum inline_status_t inline_status
4151 = Has_Pragma_No_Inline (gnat_entity)
4152 ? is_suppressed
4153 : Has_Pragma_Inline_Always (gnat_entity)
4154 ? is_required
4155 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4156 bool public_flag = Is_Public (gnat_entity) || imported_p;
4157 bool extern_flag
4158 = (Is_Public (gnat_entity) && !definition) || imported_p;
4159 bool artificial_flag = !Comes_From_Source (gnat_entity);
4160 /* The semantics of "pure" in Ada essentially matches that of "const"
4161 in the back-end. In particular, both properties are orthogonal to
4162 the "nothrow" property if the EH circuitry is explicit in the
4163 internal representation of the back-end. If we are to completely
4164 hide the EH circuitry from it, we need to declare that calls to pure
4165 Ada subprograms that can throw have side effects since they can
4166 trigger an "abnormal" transfer of control flow; thus they can be
4167 neither "const" nor "pure" in the back-end sense. */
4168 bool const_flag
4169 = (Exception_Mechanism == Back_End_Exceptions
4170 && Is_Pure (gnat_entity));
4171 bool volatile_flag = No_Return (gnat_entity);
4172 bool return_by_direct_ref_p = false;
4173 bool return_by_invisi_ref_p = false;
4174 bool return_unconstrained_p = false;
4175 int parmnum;
4177 /* A parameter may refer to this type, so defer completion of any
4178 incomplete types. */
4179 if (kind == E_Subprogram_Type && !definition)
4181 defer_incomplete_level++;
4182 this_deferred = true;
4185 /* If the subprogram has an alias, it is probably inherited, so
4186 we can use the original one. If the original "subprogram"
4187 is actually an enumeration literal, it may be the first use
4188 of its type, so we must elaborate that type now. */
4189 if (Present (Alias (gnat_entity)))
4191 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4192 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4194 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4196 /* Elaborate any Itypes in the parameters of this entity. */
4197 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4198 Present (gnat_temp);
4199 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4200 if (Is_Itype (Etype (gnat_temp)))
4201 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4203 break;
4206 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4207 corresponding DECL node. Proper generation of calls later on need
4208 proper parameter associations so we don't "break;" here. */
4209 if (Convention (gnat_entity) == Convention_Intrinsic
4210 && Present (Interface_Name (gnat_entity)))
4212 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4214 /* Inability to find the builtin decl most often indicates a
4215 genuine mistake, but imports of unregistered intrinsics are
4216 sometimes issued on purpose to allow hooking in alternate
4217 bodies. We post a warning conditioned on Wshadow in this case,
4218 to let developers be notified on demand without risking false
4219 positives with common default sets of options. */
4221 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4222 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4225 /* ??? What if we don't find the builtin node above ? warn ? err ?
4226 In the current state we neither warn nor err, and calls will just
4227 be handled as for regular subprograms. */
4229 /* Look into the return type and get its associated GCC tree. If it
4230 is not void, compute various flags for the subprogram type. */
4231 if (Ekind (gnat_return_type) == E_Void)
4232 gnu_return_type = void_type_node;
4233 else
4235 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4236 context may now appear in parameter and result profiles. If
4237 we are only annotating types, break circularities here. */
4238 if (type_annotate_only
4239 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4240 && From_Limited_With (gnat_return_type)
4241 && In_Extended_Main_Code_Unit
4242 (Non_Limited_View (gnat_return_type))
4243 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4244 gnu_return_type = ptr_void_type_node;
4245 else
4246 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4248 /* If this function returns by reference, make the actual return
4249 type the pointer type and make a note of that. */
4250 if (Returns_By_Ref (gnat_entity))
4252 gnu_return_type = build_pointer_type (gnu_return_type);
4253 return_by_direct_ref_p = true;
4256 /* If we are supposed to return an unconstrained array type, make
4257 the actual return type the fat pointer type. */
4258 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4260 gnu_return_type = TREE_TYPE (gnu_return_type);
4261 return_unconstrained_p = true;
4264 /* Likewise, if the return type requires a transient scope, the
4265 return value will be allocated on the secondary stack so the
4266 actual return type is the pointer type. */
4267 else if (Requires_Transient_Scope (gnat_return_type))
4269 gnu_return_type = build_pointer_type (gnu_return_type);
4270 return_unconstrained_p = true;
4273 /* If the Mechanism is By_Reference, ensure this function uses the
4274 target's by-invisible-reference mechanism, which may not be the
4275 same as above (e.g. it might be passing an extra parameter). */
4276 else if (kind == E_Function
4277 && Mechanism (gnat_entity) == By_Reference)
4278 return_by_invisi_ref_p = true;
4280 /* Likewise, if the return type is itself By_Reference. */
4281 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4282 return_by_invisi_ref_p = true;
4284 /* If the type is a padded type and the underlying type would not
4285 be passed by reference or the function has a foreign convention,
4286 return the underlying type. */
4287 else if (TYPE_IS_PADDING_P (gnu_return_type)
4288 && (!default_pass_by_ref
4289 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4290 || Has_Foreign_Convention (gnat_entity)))
4291 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4293 /* If the return type is unconstrained, that means it must have a
4294 maximum size. Use the padded type as the effective return type.
4295 And ensure the function uses the target's by-invisible-reference
4296 mechanism to avoid copying too much data when it returns. */
4297 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4299 tree orig_type = gnu_return_type;
4301 gnu_return_type
4302 = maybe_pad_type (gnu_return_type,
4303 max_size (TYPE_SIZE (gnu_return_type),
4304 true),
4305 0, gnat_entity, false, false, false, true);
4307 /* Declare it now since it will never be declared otherwise.
4308 This is necessary to ensure that its subtrees are properly
4309 marked. */
4310 if (gnu_return_type != orig_type
4311 && !DECL_P (TYPE_NAME (gnu_return_type)))
4312 create_type_decl (TYPE_NAME (gnu_return_type),
4313 gnu_return_type, true, debug_info_p,
4314 gnat_entity);
4316 return_by_invisi_ref_p = true;
4319 /* If the return type has a size that overflows, we cannot have
4320 a function that returns that type. This usage doesn't make
4321 sense anyway, so give an error here. */
4322 if (TYPE_SIZE_UNIT (gnu_return_type)
4323 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4324 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4326 post_error ("cannot return type whose size overflows",
4327 gnat_entity);
4328 gnu_return_type = copy_node (gnu_return_type);
4329 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4330 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4331 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4332 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4336 /* Loop over the parameters and get their associated GCC tree. While
4337 doing this, build a copy-in copy-out structure if we need one. */
4338 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4339 Present (gnat_param);
4340 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4342 Entity_Id gnat_param_type = Etype (gnat_param);
4343 tree gnu_param_name = get_entity_name (gnat_param);
4344 tree gnu_param_type, gnu_param, gnu_field;
4345 Mechanism_Type mech = Mechanism (gnat_param);
4346 bool copy_in_copy_out = false, fake_param_type;
4348 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4349 context may now appear in parameter and result profiles. If
4350 we are only annotating types, break circularities here. */
4351 if (type_annotate_only
4352 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4353 && From_Limited_With (Etype (gnat_param_type))
4354 && In_Extended_Main_Code_Unit
4355 (Non_Limited_View (gnat_param_type))
4356 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4358 gnu_param_type = ptr_void_type_node;
4359 fake_param_type = true;
4361 else
4363 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4364 fake_param_type = false;
4367 /* Builtins are expanded inline and there is no real call sequence
4368 involved. So the type expected by the underlying expander is
4369 always the type of each argument "as is". */
4370 if (gnu_builtin_decl)
4371 mech = By_Copy;
4372 /* Handle the first parameter of a valued procedure specially. */
4373 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4374 mech = By_Copy_Return;
4375 /* Otherwise, see if a Mechanism was supplied that forced this
4376 parameter to be passed one way or another. */
4377 else if (mech == Default
4378 || mech == By_Copy
4379 || mech == By_Reference)
4381 else if (mech > 0)
4383 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4384 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4385 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4386 mech))
4387 mech = By_Reference;
4388 else
4389 mech = By_Copy;
4391 else
4393 post_error ("unsupported mechanism for&", gnat_param);
4394 mech = Default;
4397 /* Do not call gnat_to_gnu_param for a fake parameter type since
4398 it will try to use the real type again. */
4399 if (fake_param_type)
4401 if (Ekind (gnat_param) == E_Out_Parameter)
4402 gnu_param = NULL_TREE;
4403 else
4405 gnu_param
4406 = create_param_decl (gnu_param_name, gnu_param_type,
4407 false);
4408 Set_Mechanism (gnat_param,
4409 mech == Default ? By_Copy : mech);
4410 if (Ekind (gnat_param) == E_In_Out_Parameter)
4411 copy_in_copy_out = true;
4414 else
4415 gnu_param
4416 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4417 Has_Foreign_Convention (gnat_entity),
4418 &copy_in_copy_out);
4420 /* We are returned either a PARM_DECL or a type if no parameter
4421 needs to be passed; in either case, adjust the type. */
4422 if (DECL_P (gnu_param))
4423 gnu_param_type = TREE_TYPE (gnu_param);
4424 else
4426 gnu_param_type = gnu_param;
4427 gnu_param = NULL_TREE;
4430 /* The failure of this assertion will very likely come from an
4431 order of elaboration issue for the type of the parameter. */
4432 gcc_assert (kind == E_Subprogram_Type
4433 || !TYPE_IS_DUMMY_P (gnu_param_type)
4434 || type_annotate_only);
4436 if (gnu_param)
4438 gnu_param_list = chainon (gnu_param, gnu_param_list);
4439 Sloc_to_locus (Sloc (gnat_param),
4440 &DECL_SOURCE_LOCATION (gnu_param));
4441 save_gnu_tree (gnat_param, gnu_param, false);
4443 /* If a parameter is a pointer, this function may modify
4444 memory through it and thus shouldn't be considered
4445 a const function. Also, the memory may be modified
4446 between two calls, so they can't be CSE'ed. The latter
4447 case also handles by-ref parameters. */
4448 if (POINTER_TYPE_P (gnu_param_type)
4449 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4450 const_flag = false;
4453 if (copy_in_copy_out)
4455 if (!gnu_cico_list)
4457 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4459 /* If this is a function, we also need a field for the
4460 return value to be placed. */
4461 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4463 gnu_field
4464 = create_field_decl (get_identifier ("RETVAL"),
4465 gnu_return_type,
4466 gnu_new_ret_type, NULL_TREE,
4467 NULL_TREE, 0, 0);
4468 Sloc_to_locus (Sloc (gnat_entity),
4469 &DECL_SOURCE_LOCATION (gnu_field));
4470 gnu_field_list = gnu_field;
4471 gnu_cico_list
4472 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4475 gnu_return_type = gnu_new_ret_type;
4476 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4477 /* Set a default alignment to speed up accesses. But we
4478 shouldn't increase the size of the structure too much,
4479 lest it doesn't fit in return registers anymore. */
4480 TYPE_ALIGN (gnu_return_type)
4481 = get_mode_alignment (ptr_mode);
4484 gnu_field
4485 = create_field_decl (gnu_param_name, gnu_param_type,
4486 gnu_return_type, NULL_TREE, NULL_TREE,
4487 0, 0);
4488 Sloc_to_locus (Sloc (gnat_param),
4489 &DECL_SOURCE_LOCATION (gnu_field));
4490 DECL_CHAIN (gnu_field) = gnu_field_list;
4491 gnu_field_list = gnu_field;
4492 gnu_cico_list
4493 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4497 if (gnu_cico_list)
4499 /* If we have a CICO list but it has only one entry, we convert
4500 this function into a function that returns this object. */
4501 if (list_length (gnu_cico_list) == 1)
4502 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4504 /* Do not finalize the return type if the subprogram is stubbed
4505 since structures are incomplete for the back-end. */
4506 else if (Convention (gnat_entity) != Convention_Stubbed)
4508 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4509 0, false);
4511 /* Try to promote the mode of the return type if it is passed
4512 in registers, again to speed up accesses. */
4513 if (TYPE_MODE (gnu_return_type) == BLKmode
4514 && !targetm.calls.return_in_memory (gnu_return_type,
4515 NULL_TREE))
4517 unsigned int size
4518 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4519 unsigned int i = BITS_PER_UNIT;
4520 machine_mode mode;
4522 while (i < size)
4523 i <<= 1;
4524 mode = mode_for_size (i, MODE_INT, 0);
4525 if (mode != BLKmode)
4527 SET_TYPE_MODE (gnu_return_type, mode);
4528 TYPE_ALIGN (gnu_return_type)
4529 = GET_MODE_ALIGNMENT (mode);
4530 TYPE_SIZE (gnu_return_type)
4531 = bitsize_int (GET_MODE_BITSIZE (mode));
4532 TYPE_SIZE_UNIT (gnu_return_type)
4533 = size_int (GET_MODE_SIZE (mode));
4537 if (debug_info_p)
4538 rest_of_record_type_compilation (gnu_return_type);
4542 /* Deal with platform-specific calling conventions. */
4543 if (Has_Stdcall_Convention (gnat_entity))
4544 prepend_one_attribute
4545 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4546 get_identifier ("stdcall"), NULL_TREE,
4547 gnat_entity);
4548 else if (Has_Thiscall_Convention (gnat_entity))
4549 prepend_one_attribute
4550 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4551 get_identifier ("thiscall"), NULL_TREE,
4552 gnat_entity);
4554 /* If we should request stack realignment for a foreign convention
4555 subprogram, do so. Note that this applies to task entry points
4556 in particular. */
4557 if (FOREIGN_FORCE_REALIGN_STACK
4558 && Has_Foreign_Convention (gnat_entity))
4559 prepend_one_attribute
4560 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4561 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4562 gnat_entity);
4564 /* Deal with a pragma Linker_Section on a subprogram. */
4565 if ((kind == E_Function || kind == E_Procedure)
4566 && Present (Linker_Section_Pragma (gnat_entity)))
4567 prepend_one_attribute_pragma (&attr_list,
4568 Linker_Section_Pragma (gnat_entity));
4570 /* The lists have been built in reverse. */
4571 gnu_param_list = nreverse (gnu_param_list);
4572 gnu_cico_list = nreverse (gnu_cico_list);
4574 if (kind == E_Function)
4575 Set_Mechanism (gnat_entity, return_unconstrained_p
4576 || return_by_direct_ref_p
4577 || return_by_invisi_ref_p
4578 ? By_Reference : By_Copy);
4579 gnu_type
4580 = create_subprog_type (gnu_return_type, gnu_param_list,
4581 gnu_cico_list, return_unconstrained_p,
4582 return_by_direct_ref_p,
4583 return_by_invisi_ref_p);
4585 /* A subprogram (something that doesn't return anything) shouldn't
4586 be considered const since there would be no reason for such a
4587 subprogram. Note that procedures with Out (or In Out) parameters
4588 have already been converted into a function with a return type. */
4589 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4590 const_flag = false;
4592 if (const_flag || volatile_flag)
4594 const int quals
4595 = (const_flag ? TYPE_QUAL_CONST : 0)
4596 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4598 gnu_type = change_qualified_type (gnu_type, quals);
4601 /* If we have a builtin decl for that function, use it. Check if the
4602 profiles are compatible and warn if they are not. The checker is
4603 expected to post extra diagnostics in this case. */
4604 if (gnu_builtin_decl)
4606 intrin_binding_t inb;
4608 inb.gnat_entity = gnat_entity;
4609 inb.ada_fntype = gnu_type;
4610 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4612 if (!intrin_profiles_compatible_p (&inb))
4613 post_error
4614 ("?profile of& doesn''t match the builtin it binds!",
4615 gnat_entity);
4617 gnu_decl = gnu_builtin_decl;
4618 gnu_type = TREE_TYPE (gnu_builtin_decl);
4619 break;
4622 /* If there was no specified Interface_Name and the external and
4623 internal names of the subprogram are the same, only use the
4624 internal name to allow disambiguation of nested subprograms. */
4625 if (No (Interface_Name (gnat_entity))
4626 && gnu_ext_name == gnu_entity_name)
4627 gnu_ext_name = NULL_TREE;
4629 /* If we are defining the subprogram and it has an Address clause
4630 we must get the address expression from the saved GCC tree for the
4631 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4632 the address expression here since the front-end has guaranteed
4633 in that case that the elaboration has no effects. If there is
4634 an Address clause and we are not defining the object, just
4635 make it a constant. */
4636 if (Present (Address_Clause (gnat_entity)))
4638 tree gnu_address = NULL_TREE;
4640 if (definition)
4641 gnu_address
4642 = (present_gnu_tree (gnat_entity)
4643 ? get_gnu_tree (gnat_entity)
4644 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4646 save_gnu_tree (gnat_entity, NULL_TREE, false);
4648 /* Convert the type of the object to a reference type that can
4649 alias everything as per 13.3(19). */
4650 gnu_type
4651 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4652 if (gnu_address)
4653 gnu_address = convert (gnu_type, gnu_address);
4655 gnu_decl
4656 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4657 gnu_address, false, Is_Public (gnat_entity),
4658 extern_flag, false, NULL, gnat_entity);
4659 DECL_BY_REF_P (gnu_decl) = 1;
4662 else if (kind == E_Subprogram_Type)
4664 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4665 gnu_decl
4666 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4667 debug_info_p, gnat_entity);
4669 else
4671 gnu_decl
4672 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4673 gnu_param_list, inline_status,
4674 public_flag, extern_flag, artificial_flag,
4675 attr_list, gnat_entity);
4676 /* This is unrelated to the stub built right above. */
4677 DECL_STUBBED_P (gnu_decl)
4678 = Convention (gnat_entity) == Convention_Stubbed;
4681 break;
4683 case E_Incomplete_Type:
4684 case E_Incomplete_Subtype:
4685 case E_Private_Type:
4686 case E_Private_Subtype:
4687 case E_Limited_Private_Type:
4688 case E_Limited_Private_Subtype:
4689 case E_Record_Type_With_Private:
4690 case E_Record_Subtype_With_Private:
4692 /* Get the "full view" of this entity. If this is an incomplete
4693 entity from a limited with, treat its non-limited view as the
4694 full view. Otherwise, use either the full view or the underlying
4695 full view, whichever is present. This is used in all the tests
4696 below. */
4697 Entity_Id full_view
4698 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4699 ? Non_Limited_View (gnat_entity)
4700 : Present (Full_View (gnat_entity))
4701 ? Full_View (gnat_entity)
4702 : IN (kind, Private_Kind)
4703 ? Underlying_Full_View (gnat_entity)
4704 : Empty;
4706 /* If this is an incomplete type with no full view, it must be a Taft
4707 Amendment type, in which case we return a dummy type. Otherwise,
4708 just get the type from its Etype. */
4709 if (No (full_view))
4711 if (kind == E_Incomplete_Type)
4713 gnu_type = make_dummy_type (gnat_entity);
4714 gnu_decl = TYPE_STUB_DECL (gnu_type);
4716 else
4718 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4719 NULL_TREE, 0);
4720 maybe_present = true;
4722 break;
4725 /* If we already made a type for the full view, reuse it. */
4726 else if (present_gnu_tree (full_view))
4728 gnu_decl = get_gnu_tree (full_view);
4729 break;
4732 /* Otherwise, if we are not defining the type now, get the type
4733 from the full view. But always get the type from the full view
4734 for define on use types, since otherwise we won't see them! */
4735 else if (!definition
4736 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4737 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4739 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4740 maybe_present = true;
4741 break;
4744 /* For incomplete types, make a dummy type entry which will be
4745 replaced later. Save it as the full declaration's type so
4746 we can do any needed updates when we see it. */
4747 gnu_type = make_dummy_type (gnat_entity);
4748 gnu_decl = TYPE_STUB_DECL (gnu_type);
4749 if (Has_Completion_In_Body (gnat_entity))
4750 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4751 save_gnu_tree (full_view, gnu_decl, 0);
4752 break;
4755 case E_Class_Wide_Type:
4756 /* Class-wide types are always transformed into their root type. */
4757 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4758 maybe_present = true;
4759 break;
4761 case E_Task_Type:
4762 case E_Task_Subtype:
4763 case E_Protected_Type:
4764 case E_Protected_Subtype:
4765 /* Concurrent types are always transformed into their record type. */
4766 if (type_annotate_only && No (gnat_equiv_type))
4767 gnu_type = void_type_node;
4768 else
4769 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4770 maybe_present = true;
4771 break;
4773 case E_Label:
4774 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4775 break;
4777 case E_Block:
4778 case E_Loop:
4779 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4780 we've already saved it, so we don't try to. */
4781 gnu_decl = error_mark_node;
4782 saved = true;
4783 break;
4785 case E_Abstract_State:
4786 /* This is a SPARK annotation that only reaches here when compiling in
4787 ASIS mode and has no characteristics to annotate. */
4788 gcc_assert (type_annotate_only);
4789 return error_mark_node;
4791 default:
4792 gcc_unreachable ();
4795 /* If we had a case where we evaluated another type and it might have
4796 defined this one, handle it here. */
4797 if (maybe_present && present_gnu_tree (gnat_entity))
4799 gnu_decl = get_gnu_tree (gnat_entity);
4800 saved = true;
4803 /* If we are processing a type and there is either no decl for it or
4804 we just made one, do some common processing for the type, such as
4805 handling alignment and possible padding. */
4806 if (is_type && (!gnu_decl || this_made_decl))
4808 /* Process the attributes, if not already done. Note that the type is
4809 already defined so we cannot pass true for IN_PLACE here. */
4810 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4812 /* Tell the middle-end that objects of tagged types are guaranteed to
4813 be properly aligned. This is necessary because conversions to the
4814 class-wide type are translated into conversions to the root type,
4815 which can be less aligned than some of its derived types. */
4816 if (Is_Tagged_Type (gnat_entity)
4817 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4818 TYPE_ALIGN_OK (gnu_type) = 1;
4820 /* Record whether the type is passed by reference. */
4821 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4822 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4824 /* ??? Don't set the size for a String_Literal since it is either
4825 confirming or we don't handle it properly (if the low bound is
4826 non-constant). */
4827 if (!gnu_size && kind != E_String_Literal_Subtype)
4829 Uint gnat_size = Known_Esize (gnat_entity)
4830 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4831 gnu_size
4832 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4833 false, Has_Size_Clause (gnat_entity));
4836 /* If a size was specified, see if we can make a new type of that size
4837 by rearranging the type, for example from a fat to a thin pointer. */
4838 if (gnu_size)
4840 gnu_type
4841 = make_type_from_size (gnu_type, gnu_size,
4842 Has_Biased_Representation (gnat_entity));
4844 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4845 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4846 gnu_size = NULL_TREE;
4849 /* If the alignment has not already been processed and this is not
4850 an unconstrained array type, see if an alignment is specified.
4851 If not, we pick a default alignment for atomic objects. */
4852 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4854 else if (Known_Alignment (gnat_entity))
4856 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4857 TYPE_ALIGN (gnu_type));
4859 /* Warn on suspiciously large alignments. This should catch
4860 errors about the (alignment,byte)/(size,bit) discrepancy. */
4861 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4863 tree size;
4865 /* If a size was specified, take it into account. Otherwise
4866 use the RM size for records or unions as the type size has
4867 already been adjusted to the alignment. */
4868 if (gnu_size)
4869 size = gnu_size;
4870 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4871 && !TYPE_FAT_POINTER_P (gnu_type))
4872 size = rm_size (gnu_type);
4873 else
4874 size = TYPE_SIZE (gnu_type);
4876 /* Consider an alignment as suspicious if the alignment/size
4877 ratio is greater or equal to the byte/bit ratio. */
4878 if (tree_fits_uhwi_p (size)
4879 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4880 post_error_ne ("?suspiciously large alignment specified for&",
4881 Expression (Alignment_Clause (gnat_entity)),
4882 gnat_entity);
4885 else if (Is_Atomic (gnat_entity) && !gnu_size
4886 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4887 && integer_pow2p (TYPE_SIZE (gnu_type)))
4888 align = MIN (BIGGEST_ALIGNMENT,
4889 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4890 else if (Is_Atomic (gnat_entity) && gnu_size
4891 && tree_fits_uhwi_p (gnu_size)
4892 && integer_pow2p (gnu_size))
4893 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4895 /* See if we need to pad the type. If we did, and made a record,
4896 the name of the new type may be changed. So get it back for
4897 us when we make the new TYPE_DECL below. */
4898 if (gnu_size || align > 0)
4899 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4900 false, !gnu_decl, definition, false);
4902 if (TYPE_IS_PADDING_P (gnu_type))
4903 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4905 /* Now set the RM size of the type. We cannot do it before padding
4906 because we need to accept arbitrary RM sizes on integral types. */
4907 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4909 /* If we are at global level, GCC will have applied variable_size to
4910 the type, but that won't have done anything. So, if it's not
4911 a constant or self-referential, call elaborate_expression_1 to
4912 make a variable for the size rather than calculating it each time.
4913 Handle both the RM size and the actual size. */
4914 if (global_bindings_p ()
4915 && TYPE_SIZE (gnu_type)
4916 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4917 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4919 tree size = TYPE_SIZE (gnu_type);
4921 TYPE_SIZE (gnu_type)
4922 = elaborate_expression_1 (size, gnat_entity,
4923 get_identifier ("SIZE"),
4924 definition, false);
4926 /* ??? For now, store the size as a multiple of the alignment in
4927 bytes so that we can see the alignment from the tree. */
4928 TYPE_SIZE_UNIT (gnu_type)
4929 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4930 get_identifier ("SIZE_A_UNIT"),
4931 definition, false,
4932 TYPE_ALIGN (gnu_type));
4934 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4935 may not be marked by the call to create_type_decl below. */
4936 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4938 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4940 tree variant_part = get_variant_part (gnu_type);
4941 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4943 if (variant_part)
4945 tree union_type = TREE_TYPE (variant_part);
4946 tree offset = DECL_FIELD_OFFSET (variant_part);
4948 /* If the position of the variant part is constant, subtract
4949 it from the size of the type of the parent to get the new
4950 size. This manual CSE reduces the data size. */
4951 if (TREE_CODE (offset) == INTEGER_CST)
4953 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4954 TYPE_SIZE (union_type)
4955 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4956 bit_from_pos (offset, bitpos));
4957 TYPE_SIZE_UNIT (union_type)
4958 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4959 byte_from_pos (offset, bitpos));
4961 else
4963 TYPE_SIZE (union_type)
4964 = elaborate_expression_1 (TYPE_SIZE (union_type),
4965 gnat_entity,
4966 get_identifier ("VSIZE"),
4967 definition, false);
4969 /* ??? For now, store the size as a multiple of the
4970 alignment in bytes so that we can see the alignment
4971 from the tree. */
4972 TYPE_SIZE_UNIT (union_type)
4973 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4974 gnat_entity,
4975 get_identifier
4976 ("VSIZE_A_UNIT"),
4977 definition, false,
4978 TYPE_ALIGN (union_type));
4980 /* ??? For now, store the offset as a multiple of the
4981 alignment in bytes so that we can see the alignment
4982 from the tree. */
4983 DECL_FIELD_OFFSET (variant_part)
4984 = elaborate_expression_2 (offset,
4985 gnat_entity,
4986 get_identifier ("VOFFSET"),
4987 definition, false,
4988 DECL_OFFSET_ALIGN
4989 (variant_part));
4992 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4993 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4996 if (operand_equal_p (ada_size, size, 0))
4997 ada_size = TYPE_SIZE (gnu_type);
4998 else
4999 ada_size
5000 = elaborate_expression_1 (ada_size, gnat_entity,
5001 get_identifier ("RM_SIZE"),
5002 definition, false);
5003 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5007 /* If this is a record type or subtype, call elaborate_expression_2 on
5008 any field position. Do this for both global and local types.
5009 Skip any fields that we haven't made trees for to avoid problems with
5010 class wide types. */
5011 if (IN (kind, Record_Kind))
5012 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5013 gnat_temp = Next_Entity (gnat_temp))
5014 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5016 tree gnu_field = get_gnu_tree (gnat_temp);
5018 /* ??? For now, store the offset as a multiple of the alignment
5019 in bytes so that we can see the alignment from the tree. */
5020 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5022 DECL_FIELD_OFFSET (gnu_field)
5023 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5024 gnat_temp,
5025 get_identifier ("OFFSET"),
5026 definition, false,
5027 DECL_OFFSET_ALIGN (gnu_field));
5029 /* ??? The context of gnu_field is not necessarily gnu_type
5030 so the MULT_EXPR node built above may not be marked by
5031 the call to create_type_decl below. */
5032 if (global_bindings_p ())
5033 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5037 if (Is_Atomic (gnat_entity))
5038 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5040 /* If this is not an unconstrained array type, set some flags. */
5041 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5043 if (Treat_As_Volatile (gnat_entity))
5044 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5046 if (Present (Alignment_Clause (gnat_entity)))
5047 TYPE_USER_ALIGN (gnu_type) = 1;
5049 if (Universal_Aliasing (gnat_entity))
5050 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5053 if (!gnu_decl)
5054 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5055 !Comes_From_Source (gnat_entity),
5056 debug_info_p, gnat_entity);
5057 else
5059 TREE_TYPE (gnu_decl) = gnu_type;
5060 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5064 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5066 gnu_type = TREE_TYPE (gnu_decl);
5068 /* If this is a derived type, relate its alias set to that of its parent
5069 to avoid troubles when a call to an inherited primitive is inlined in
5070 a context where a derived object is accessed. The inlined code works
5071 on the parent view so the resulting code may access the same object
5072 using both the parent and the derived alias sets, which thus have to
5073 conflict. As the same issue arises with component references, the
5074 parent alias set also has to conflict with composite types enclosing
5075 derived components. For instance, if we have:
5077 type D is new T;
5078 type R is record
5079 Component : D;
5080 end record;
5082 we want T to conflict with both D and R, in addition to R being a
5083 superset of D by record/component construction.
5085 One way to achieve this is to perform an alias set copy from the
5086 parent to the derived type. This is not quite appropriate, though,
5087 as we don't want separate derived types to conflict with each other:
5089 type I1 is new Integer;
5090 type I2 is new Integer;
5092 We want I1 and I2 to both conflict with Integer but we do not want
5093 I1 to conflict with I2, and an alias set copy on derivation would
5094 have that effect.
5096 The option chosen is to make the alias set of the derived type a
5097 superset of that of its parent type. It trivially fulfills the
5098 simple requirement for the Integer derivation example above, and
5099 the component case as well by superset transitivity:
5101 superset superset
5102 R ----------> D ----------> T
5104 However, for composite types, conversions between derived types are
5105 translated into VIEW_CONVERT_EXPRs so a sequence like:
5107 type Comp1 is new Comp;
5108 type Comp2 is new Comp;
5109 procedure Proc (C : Comp1);
5111 C : Comp2;
5112 Proc (Comp1 (C));
5114 is translated into:
5116 C : Comp2;
5117 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5119 and gimplified into:
5121 C : Comp2;
5122 Comp1 *C.0;
5123 C.0 = (Comp1 *) &C;
5124 Proc (C.0);
5126 i.e. generates code involving type punning. Therefore, Comp1 needs
5127 to conflict with Comp2 and an alias set copy is required.
5129 The language rules ensure the parent type is already frozen here. */
5130 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5132 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5133 /* For constrained packed array subtypes, the implementation type is
5134 used instead of the nominal type. */
5135 if (kind == E_Array_Subtype
5136 && Is_Constrained (gnat_entity)
5137 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5138 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5139 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5140 Is_Composite_Type (gnat_entity)
5141 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5144 /* Back-annotate the Alignment of the type if not already in the
5145 tree. Likewise for sizes. */
5146 if (Unknown_Alignment (gnat_entity))
5148 unsigned int double_align, align;
5149 bool is_capped_double, align_clause;
5151 /* If the default alignment of "double" or larger scalar types is
5152 specifically capped and this is not an array with an alignment
5153 clause on the component type, return the cap. */
5154 if ((double_align = double_float_alignment) > 0)
5155 is_capped_double
5156 = is_double_float_or_array (gnat_entity, &align_clause);
5157 else if ((double_align = double_scalar_alignment) > 0)
5158 is_capped_double
5159 = is_double_scalar_or_array (gnat_entity, &align_clause);
5160 else
5161 is_capped_double = align_clause = false;
5163 if (is_capped_double && !align_clause)
5164 align = double_align;
5165 else
5166 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5168 Set_Alignment (gnat_entity, UI_From_Int (align));
5171 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5173 tree gnu_size = TYPE_SIZE (gnu_type);
5175 /* If the size is self-referential, annotate the maximum value. */
5176 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5177 gnu_size = max_size (gnu_size, true);
5179 /* If we are just annotating types and the type is tagged, the tag
5180 and the parent components are not generated by the front-end so
5181 sizes must be adjusted if there is no representation clause. */
5182 if (type_annotate_only
5183 && Is_Tagged_Type (gnat_entity)
5184 && !VOID_TYPE_P (gnu_type)
5185 && (!TYPE_FIELDS (gnu_type)
5186 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5188 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5189 Uint uint_size;
5191 if (Is_Derived_Type (gnat_entity))
5193 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5194 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5195 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5197 else
5198 offset = pointer_size;
5200 if (TYPE_FIELDS (gnu_type))
5201 offset
5202 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5204 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5205 gnu_size = round_up (gnu_size, POINTER_SIZE);
5206 uint_size = annotate_value (gnu_size);
5207 Set_Esize (gnat_entity, uint_size);
5208 Set_RM_Size (gnat_entity, uint_size);
5210 else
5211 Set_Esize (gnat_entity, annotate_value (gnu_size));
5214 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5215 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5218 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5219 cannot do so if we are reusing the ..._DECL node made for an equivalent
5220 type or an alias or a renamed object as the predicates don't apply to it
5221 but to GNAT_ENTITY. */
5222 if (DECL_P (gnu_decl)
5223 && !(is_type && gnat_equiv_type != gnat_entity)
5224 && !Present (Alias (gnat_entity))
5225 && !(Present (Renamed_Object (gnat_entity)) && saved))
5227 if (!Comes_From_Source (gnat_entity))
5228 DECL_ARTIFICIAL (gnu_decl) = 1;
5230 if (!debug_info_p)
5231 DECL_IGNORED_P (gnu_decl) = 1;
5234 /* If we haven't already, associate the ..._DECL node that we just made with
5235 the input GNAT entity node. */
5236 if (!saved)
5237 save_gnu_tree (gnat_entity, gnu_decl, false);
5239 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5240 eliminate as many deferred computations as possible. */
5241 process_deferred_decl_context (false);
5243 /* If this is an enumeration or floating-point type, we were not able to set
5244 the bounds since they refer to the type. These are always static. */
5245 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5246 || (kind == E_Floating_Point_Type))
5248 tree gnu_scalar_type = gnu_type;
5249 tree gnu_low_bound, gnu_high_bound;
5251 /* If this is a padded type, we need to use the underlying type. */
5252 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5253 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5255 /* If this is a floating point type and we haven't set a floating
5256 point type yet, use this in the evaluation of the bounds. */
5257 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5258 longest_float_type_node = gnu_scalar_type;
5260 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5261 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5263 if (kind == E_Enumeration_Type)
5265 /* Enumeration types have specific RM bounds. */
5266 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5267 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5269 else
5271 /* Floating-point types don't have specific RM bounds. */
5272 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5273 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5277 /* If we deferred processing of incomplete types, re-enable it. If there
5278 were no other disables and we have deferred types to process, do so. */
5279 if (this_deferred
5280 && --defer_incomplete_level == 0
5281 && defer_incomplete_list)
5283 struct incomplete *p, *next;
5285 /* We are back to level 0 for the deferring of incomplete types.
5286 But processing these incomplete types below may itself require
5287 deferring, so preserve what we have and restart from scratch. */
5288 p = defer_incomplete_list;
5289 defer_incomplete_list = NULL;
5291 for (; p; p = next)
5293 next = p->next;
5295 if (p->old_type)
5296 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5297 gnat_to_gnu_type (p->full_type));
5298 free (p);
5302 /* If we are not defining this type, see if it's on one of the lists of
5303 incomplete types. If so, handle the list entry now. */
5304 if (is_type && !definition)
5306 struct incomplete *p;
5308 for (p = defer_incomplete_list; p; p = p->next)
5309 if (p->old_type && p->full_type == gnat_entity)
5311 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5312 TREE_TYPE (gnu_decl));
5313 p->old_type = NULL_TREE;
5316 for (p = defer_limited_with; p; p = p->next)
5317 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5319 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5320 TREE_TYPE (gnu_decl));
5321 p->old_type = NULL_TREE;
5325 if (this_global)
5326 force_global--;
5328 /* If this is a packed array type whose original array type is itself
5329 an Itype without freeze node, make sure the latter is processed. */
5330 if (Is_Packed_Array_Impl_Type (gnat_entity)
5331 && Is_Itype (Original_Array_Type (gnat_entity))
5332 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5333 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5334 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5336 return gnu_decl;
5339 /* Similar, but if the returned value is a COMPONENT_REF, return the
5340 FIELD_DECL. */
5342 tree
5343 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5345 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5347 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5348 gnu_field = TREE_OPERAND (gnu_field, 1);
5350 return gnu_field;
5353 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5354 the GCC type corresponding to that entity. */
5356 tree
5357 gnat_to_gnu_type (Entity_Id gnat_entity)
5359 tree gnu_decl;
5361 /* The back end never attempts to annotate generic types. */
5362 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5363 return void_type_node;
5365 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5366 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5368 return TREE_TYPE (gnu_decl);
5371 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5372 the unpadded version of the GCC type corresponding to that entity. */
5374 tree
5375 get_unpadded_type (Entity_Id gnat_entity)
5377 tree type = gnat_to_gnu_type (gnat_entity);
5379 if (TYPE_IS_PADDING_P (type))
5380 type = TREE_TYPE (TYPE_FIELDS (type));
5382 return type;
5385 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5386 type has been changed to that of the parameterless procedure, except if an
5387 alias is already present, in which case it is returned instead. */
5389 tree
5390 get_minimal_subprog_decl (Entity_Id gnat_entity)
5392 tree gnu_entity_name, gnu_ext_name;
5393 struct attrib *attr_list = NULL;
5395 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5396 of the handling applied here. */
5398 while (Present (Alias (gnat_entity)))
5400 gnat_entity = Alias (gnat_entity);
5401 if (present_gnu_tree (gnat_entity))
5402 return get_gnu_tree (gnat_entity);
5405 gnu_entity_name = get_entity_name (gnat_entity);
5406 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5408 if (Has_Stdcall_Convention (gnat_entity))
5409 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5410 get_identifier ("stdcall"), NULL_TREE,
5411 gnat_entity);
5412 else if (Has_Thiscall_Convention (gnat_entity))
5413 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5414 get_identifier ("thiscall"), NULL_TREE,
5415 gnat_entity);
5417 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5418 gnu_ext_name = NULL_TREE;
5420 return
5421 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5422 is_disabled, true, true, true, attr_list, gnat_entity);
5425 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5426 a C++ imported method or equivalent.
5428 We use the predicate on 32-bit x86/Windows to find out whether we need to
5429 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5430 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5432 bool
5433 is_cplusplus_method (Entity_Id gnat_entity)
5435 if (Convention (gnat_entity) != Convention_CPP)
5436 return false;
5438 /* This is the main case: C++ method imported as a primitive operation. */
5439 if (Is_Dispatching_Operation (gnat_entity))
5440 return true;
5442 /* A thunk needs to be handled like its associated primitive operation. */
5443 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5444 return true;
5446 /* C++ classes with no virtual functions can be imported as limited
5447 record types, but we need to return true for the constructors. */
5448 if (Is_Constructor (gnat_entity))
5449 return true;
5451 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5452 if (Is_Dispatch_Table_Entity (gnat_entity))
5453 return true;
5455 return false;
5458 /* Finalize the processing of From_Limited_With incomplete types. */
5460 void
5461 finalize_from_limited_with (void)
5463 struct incomplete *p, *next;
5465 p = defer_limited_with;
5466 defer_limited_with = NULL;
5468 for (; p; p = next)
5470 next = p->next;
5472 if (p->old_type)
5473 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5474 gnat_to_gnu_type (p->full_type));
5475 free (p);
5479 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5480 kind of type (such E_Task_Type) that has a different type which Gigi
5481 uses for its representation. If the type does not have a special type
5482 for its representation, return GNAT_ENTITY. If a type is supposed to
5483 exist, but does not, abort unless annotating types, in which case
5484 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5486 Entity_Id
5487 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5489 Entity_Id gnat_equiv = gnat_entity;
5491 if (No (gnat_entity))
5492 return gnat_entity;
5494 switch (Ekind (gnat_entity))
5496 case E_Class_Wide_Subtype:
5497 if (Present (Equivalent_Type (gnat_entity)))
5498 gnat_equiv = Equivalent_Type (gnat_entity);
5499 break;
5501 case E_Access_Protected_Subprogram_Type:
5502 case E_Anonymous_Access_Protected_Subprogram_Type:
5503 gnat_equiv = Equivalent_Type (gnat_entity);
5504 break;
5506 case E_Class_Wide_Type:
5507 gnat_equiv = Root_Type (gnat_entity);
5508 break;
5510 case E_Task_Type:
5511 case E_Task_Subtype:
5512 case E_Protected_Type:
5513 case E_Protected_Subtype:
5514 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5515 break;
5517 default:
5518 break;
5521 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5523 return gnat_equiv;
5526 /* Return a GCC tree for a type corresponding to the component type of the
5527 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5528 is for an array being defined. DEBUG_INFO_P is true if we need to write
5529 debug information for other types that we may create in the process. */
5531 static tree
5532 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5533 bool debug_info_p)
5535 const Entity_Id gnat_type = Component_Type (gnat_array);
5536 tree gnu_type = gnat_to_gnu_type (gnat_type);
5537 tree gnu_comp_size;
5539 /* Try to get a smaller form of the component if needed. */
5540 if ((Is_Packed (gnat_array)
5541 || Has_Component_Size_Clause (gnat_array))
5542 && !Is_Bit_Packed_Array (gnat_array)
5543 && !Has_Aliased_Components (gnat_array)
5544 && !Strict_Alignment (gnat_type)
5545 && RECORD_OR_UNION_TYPE_P (gnu_type)
5546 && !TYPE_FAT_POINTER_P (gnu_type)
5547 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5548 gnu_type = make_packable_type (gnu_type, false);
5550 if (Has_Atomic_Components (gnat_array))
5551 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5553 /* Get and validate any specified Component_Size. */
5554 gnu_comp_size
5555 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5556 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5557 true, Has_Component_Size_Clause (gnat_array));
5559 /* If the array has aliased components and the component size can be zero,
5560 force at least unit size to ensure that the components have distinct
5561 addresses. */
5562 if (!gnu_comp_size
5563 && Has_Aliased_Components (gnat_array)
5564 && (integer_zerop (TYPE_SIZE (gnu_type))
5565 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5566 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5567 gnu_comp_size
5568 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5570 /* If the component type is a RECORD_TYPE that has a self-referential size,
5571 then use the maximum size for the component size. */
5572 if (!gnu_comp_size
5573 && TREE_CODE (gnu_type) == RECORD_TYPE
5574 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5575 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5577 /* Honor the component size. This is not needed for bit-packed arrays. */
5578 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5580 tree orig_type = gnu_type;
5581 unsigned int max_align;
5583 /* If an alignment is specified, use it as a cap on the component type
5584 so that it can be honored for the whole type. But ignore it for the
5585 original type of packed array types. */
5586 if (No (Packed_Array_Impl_Type (gnat_array))
5587 && Known_Alignment (gnat_array))
5588 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5589 else
5590 max_align = 0;
5592 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5593 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5594 gnu_type = orig_type;
5595 else
5596 orig_type = gnu_type;
5598 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5599 true, false, definition, true);
5601 /* If a padding record was made, declare it now since it will never be
5602 declared otherwise. This is necessary to ensure that its subtrees
5603 are properly marked. */
5604 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5605 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5606 gnat_array);
5609 if (Has_Volatile_Components (gnat_array))
5610 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5612 return gnu_type;
5615 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5616 using MECH as its passing mechanism, to be placed in the parameter
5617 list built for GNAT_SUBPROG. Assume a foreign convention for the
5618 latter if FOREIGN is true. Also set CICO to true if the parameter
5619 must use the copy-in copy-out implementation mechanism.
5621 The returned tree is a PARM_DECL, except for those cases where no
5622 parameter needs to be actually passed to the subprogram; the type
5623 of this "shadow" parameter is then returned instead. */
5625 static tree
5626 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5627 Entity_Id gnat_subprog, bool foreign, bool *cico)
5629 tree gnu_param_name = get_entity_name (gnat_param);
5630 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5631 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5632 /* The parameter can be indirectly modified if its address is taken. */
5633 bool ro_param = in_param && !Address_Taken (gnat_param);
5634 bool by_return = false, by_component_ptr = false;
5635 bool by_ref = false;
5636 tree gnu_param;
5638 /* Copy-return is used only for the first parameter of a valued procedure.
5639 It's a copy mechanism for which a parameter is never allocated. */
5640 if (mech == By_Copy_Return)
5642 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5643 mech = By_Copy;
5644 by_return = true;
5647 /* If this is either a foreign function or if the underlying type won't
5648 be passed by reference, strip off possible padding type. */
5649 if (TYPE_IS_PADDING_P (gnu_param_type))
5651 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5653 if (mech == By_Reference
5654 || foreign
5655 || (!must_pass_by_ref (unpadded_type)
5656 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5657 gnu_param_type = unpadded_type;
5660 /* If this is a read-only parameter, make a variant of the type that is
5661 read-only. ??? However, if this is an unconstrained array, that type
5662 can be very complex, so skip it for now. Likewise for any other
5663 self-referential type. */
5664 if (ro_param
5665 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5666 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5667 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5669 /* For foreign conventions, pass arrays as pointers to the element type.
5670 First check for unconstrained array and get the underlying array. */
5671 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5672 gnu_param_type
5673 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5675 /* For GCC builtins, pass Address integer types as (void *) */
5676 if (Convention (gnat_subprog) == Convention_Intrinsic
5677 && Present (Interface_Name (gnat_subprog))
5678 && Is_Descendent_Of_Address (Etype (gnat_param)))
5679 gnu_param_type = ptr_void_type_node;
5681 /* Arrays are passed as pointers to element type for foreign conventions. */
5682 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5684 /* Strip off any multi-dimensional entries, then strip
5685 off the last array to get the component type. */
5686 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5687 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5688 gnu_param_type = TREE_TYPE (gnu_param_type);
5690 by_component_ptr = true;
5691 gnu_param_type = TREE_TYPE (gnu_param_type);
5693 if (ro_param)
5694 gnu_param_type
5695 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5697 gnu_param_type = build_pointer_type (gnu_param_type);
5700 /* Fat pointers are passed as thin pointers for foreign conventions. */
5701 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5702 gnu_param_type
5703 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5705 /* If we must pass or were requested to pass by reference, do so.
5706 If we were requested to pass by copy, do so.
5707 Otherwise, for foreign conventions, pass In Out or Out parameters
5708 or aggregates by reference. For COBOL and Fortran, pass all
5709 integer and FP types that way too. For Convention Ada, use
5710 the standard Ada default. */
5711 else if (must_pass_by_ref (gnu_param_type)
5712 || mech == By_Reference
5713 || (mech != By_Copy
5714 && ((foreign
5715 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5716 || (foreign
5717 && (Convention (gnat_subprog) == Convention_Fortran
5718 || Convention (gnat_subprog) == Convention_COBOL)
5719 && (INTEGRAL_TYPE_P (gnu_param_type)
5720 || FLOAT_TYPE_P (gnu_param_type)))
5721 || (!foreign
5722 && default_pass_by_ref (gnu_param_type)))))
5724 /* We take advantage of 6.2(12) by considering that references built for
5725 parameters whose type isn't by-ref and for which the mechanism hasn't
5726 been forced to by-ref are restrict-qualified in the C sense. */
5727 bool restrict_p
5728 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5729 gnu_param_type = build_reference_type (gnu_param_type);
5730 if (restrict_p)
5731 gnu_param_type
5732 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5733 by_ref = true;
5736 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5737 else if (!in_param)
5738 *cico = true;
5740 if (mech == By_Copy && (by_ref || by_component_ptr))
5741 post_error ("?cannot pass & by copy", gnat_param);
5743 /* If this is an Out parameter that isn't passed by reference and isn't
5744 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5745 it will be a VAR_DECL created when we process the procedure, so just
5746 return its type. For the special parameter of a valued procedure,
5747 never pass it in.
5749 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5750 Out parameters with discriminants or implicit initial values to be
5751 handled like In Out parameters. These type are normally built as
5752 aggregates, hence passed by reference, except for some packed arrays
5753 which end up encoded in special integer types. Note that scalars can
5754 be given implicit initial values using the Default_Value aspect.
5756 The exception we need to make is then for packed arrays of records
5757 with discriminants or implicit initial values. We have no light/easy
5758 way to check for the latter case, so we merely check for packed arrays
5759 of records. This may lead to useless copy-in operations, but in very
5760 rare cases only, as these would be exceptions in a set of already
5761 exceptional situations. */
5762 if (Ekind (gnat_param) == E_Out_Parameter
5763 && !by_ref
5764 && (by_return
5765 || (!POINTER_TYPE_P (gnu_param_type)
5766 && !AGGREGATE_TYPE_P (gnu_param_type)
5767 && !Has_Default_Aspect (Etype (gnat_param))))
5768 && !(Is_Array_Type (Etype (gnat_param))
5769 && Is_Packed (Etype (gnat_param))
5770 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5771 return gnu_param_type;
5773 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5774 ro_param || by_ref || by_component_ptr);
5775 DECL_BY_REF_P (gnu_param) = by_ref;
5776 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5777 DECL_POINTS_TO_READONLY_P (gnu_param)
5778 = (ro_param && (by_ref || by_component_ptr));
5779 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5781 /* If no Mechanism was specified, indicate what we're using, then
5782 back-annotate it. */
5783 if (mech == Default)
5784 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5786 Set_Mechanism (gnat_param, mech);
5787 return gnu_param;
5790 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5791 qualifiers on TYPE. */
5793 static tree
5794 change_qualified_type (tree type, int type_quals)
5796 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5799 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5801 static bool
5802 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5804 while (Present (Corresponding_Discriminant (discr1)))
5805 discr1 = Corresponding_Discriminant (discr1);
5807 while (Present (Corresponding_Discriminant (discr2)))
5808 discr2 = Corresponding_Discriminant (discr2);
5810 return
5811 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5814 /* Return true if the array type GNU_TYPE, which represents a dimension of
5815 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5817 static bool
5818 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5820 /* If the array type is not the innermost dimension of the GNAT type,
5821 then it has a non-aliased component. */
5822 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5823 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5824 return true;
5826 /* If the array type has an aliased component in the front-end sense,
5827 then it also has an aliased component in the back-end sense. */
5828 if (Has_Aliased_Components (gnat_type))
5829 return false;
5831 /* If this is a derived type, then it has a non-aliased component if
5832 and only if its parent type also has one. */
5833 if (Is_Derived_Type (gnat_type))
5835 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5836 int index;
5837 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5838 gnu_parent_type
5839 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5840 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5841 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5842 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5845 /* Otherwise, rely exclusively on properties of the element type. */
5846 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5849 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5851 static bool
5852 compile_time_known_address_p (Node_Id gnat_address)
5854 /* Catch System'To_Address. */
5855 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5856 gnat_address = Expression (gnat_address);
5858 return Compile_Time_Known_Value (gnat_address);
5861 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5862 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5864 static bool
5865 cannot_be_superflat_p (Node_Id gnat_range)
5867 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5868 Node_Id scalar_range;
5869 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5871 /* If the low bound is not constant, try to find an upper bound. */
5872 while (Nkind (gnat_lb) != N_Integer_Literal
5873 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5874 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5875 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5876 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5877 || Nkind (scalar_range) == N_Range))
5878 gnat_lb = High_Bound (scalar_range);
5880 /* If the high bound is not constant, try to find a lower bound. */
5881 while (Nkind (gnat_hb) != N_Integer_Literal
5882 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5883 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5884 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5885 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5886 || Nkind (scalar_range) == N_Range))
5887 gnat_hb = Low_Bound (scalar_range);
5889 /* If we have failed to find constant bounds, punt. */
5890 if (Nkind (gnat_lb) != N_Integer_Literal
5891 || Nkind (gnat_hb) != N_Integer_Literal)
5892 return false;
5894 /* We need at least a signed 64-bit type to catch most cases. */
5895 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5896 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5897 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5898 return false;
5900 /* If the low bound is the smallest integer, nothing can be smaller. */
5901 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5902 if (TREE_OVERFLOW (gnu_lb_minus_one))
5903 return true;
5905 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5908 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5910 static bool
5911 constructor_address_p (tree gnu_expr)
5913 while (TREE_CODE (gnu_expr) == NOP_EXPR
5914 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5915 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5916 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5918 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5919 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5922 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5923 be elaborated at the point of its definition, but do nothing else. */
5925 void
5926 elaborate_entity (Entity_Id gnat_entity)
5928 switch (Ekind (gnat_entity))
5930 case E_Signed_Integer_Subtype:
5931 case E_Modular_Integer_Subtype:
5932 case E_Enumeration_Subtype:
5933 case E_Ordinary_Fixed_Point_Subtype:
5934 case E_Decimal_Fixed_Point_Subtype:
5935 case E_Floating_Point_Subtype:
5937 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5938 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5940 /* ??? Tests to avoid Constraint_Error in static expressions
5941 are needed until after the front stops generating bogus
5942 conversions on bounds of real types. */
5943 if (!Raises_Constraint_Error (gnat_lb))
5944 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5945 true, false, Needs_Debug_Info (gnat_entity));
5946 if (!Raises_Constraint_Error (gnat_hb))
5947 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5948 true, false, Needs_Debug_Info (gnat_entity));
5949 break;
5952 case E_Record_Subtype:
5953 case E_Private_Subtype:
5954 case E_Limited_Private_Subtype:
5955 case E_Record_Subtype_With_Private:
5956 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5958 Node_Id gnat_discriminant_expr;
5959 Entity_Id gnat_field;
5961 for (gnat_field
5962 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5963 gnat_discriminant_expr
5964 = First_Elmt (Discriminant_Constraint (gnat_entity));
5965 Present (gnat_field);
5966 gnat_field = Next_Discriminant (gnat_field),
5967 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5968 /* Ignore access discriminants. */
5969 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5970 elaborate_expression (Node (gnat_discriminant_expr),
5971 gnat_entity, get_entity_name (gnat_field),
5972 true, false, false);
5974 break;
5979 /* Return true if the size in units represented by GNU_SIZE can be handled by
5980 an allocation. If STATIC_P is true, consider only what can be done with a
5981 static allocation. */
5983 static bool
5984 allocatable_size_p (tree gnu_size, bool static_p)
5986 /* We can allocate a fixed size if it is a valid for the middle-end. */
5987 if (TREE_CODE (gnu_size) == INTEGER_CST)
5988 return valid_constant_size_p (gnu_size);
5990 /* We can allocate a variable size if this isn't a static allocation. */
5991 else
5992 return !static_p;
5995 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5996 NAME, ARGS and ERROR_POINT. */
5998 static void
5999 prepend_one_attribute (struct attrib **attr_list,
6000 enum attr_type attr_type,
6001 tree attr_name,
6002 tree attr_args,
6003 Node_Id attr_error_point)
6005 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6007 attr->type = attr_type;
6008 attr->name = attr_name;
6009 attr->args = attr_args;
6010 attr->error_point = attr_error_point;
6012 attr->next = *attr_list;
6013 *attr_list = attr;
6016 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6018 static void
6019 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6021 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6022 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6023 enum attr_type etype;
6025 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6026 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6028 case Pragma_Machine_Attribute:
6029 etype = ATTR_MACHINE_ATTRIBUTE;
6030 break;
6032 case Pragma_Linker_Alias:
6033 etype = ATTR_LINK_ALIAS;
6034 break;
6036 case Pragma_Linker_Section:
6037 etype = ATTR_LINK_SECTION;
6038 break;
6040 case Pragma_Linker_Constructor:
6041 etype = ATTR_LINK_CONSTRUCTOR;
6042 break;
6044 case Pragma_Linker_Destructor:
6045 etype = ATTR_LINK_DESTRUCTOR;
6046 break;
6048 case Pragma_Weak_External:
6049 etype = ATTR_WEAK_EXTERNAL;
6050 break;
6052 case Pragma_Thread_Local_Storage:
6053 etype = ATTR_THREAD_LOCAL_STORAGE;
6054 break;
6056 default:
6057 return;
6060 /* See what arguments we have and turn them into GCC trees for attribute
6061 handlers. These expect identifier for strings. We handle at most two
6062 arguments and static expressions only. */
6063 if (Present (gnat_arg) && Present (First (gnat_arg)))
6065 Node_Id gnat_arg0 = Next (First (gnat_arg));
6066 Node_Id gnat_arg1 = Empty;
6068 if (Present (gnat_arg0)
6069 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6071 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6073 if (TREE_CODE (gnu_arg0) == STRING_CST)
6075 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6076 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6077 return;
6080 gnat_arg1 = Next (gnat_arg0);
6083 if (Present (gnat_arg1)
6084 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6086 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6088 if (TREE_CODE (gnu_arg1) == STRING_CST)
6089 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6093 /* Prepend to the list. Make a list of the argument we might have, as GCC
6094 expects it. */
6095 prepend_one_attribute (attr_list, etype, gnu_arg0,
6096 gnu_arg1
6097 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6098 Present (Next (First (gnat_arg)))
6099 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6102 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6104 static void
6105 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6107 Node_Id gnat_temp;
6109 /* Attributes are stored as Representation Item pragmas. */
6110 for (gnat_temp = First_Rep_Item (gnat_entity);
6111 Present (gnat_temp);
6112 gnat_temp = Next_Rep_Item (gnat_temp))
6113 if (Nkind (gnat_temp) == N_Pragma)
6114 prepend_one_attribute_pragma (attr_list, gnat_temp);
6117 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6118 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6119 return the GCC tree to use for that expression. GNU_NAME is the suffix
6120 to use if a variable needs to be created and DEFINITION is true if this
6121 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6122 otherwise, we are just elaborating the expression for side-effects. If
6123 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6124 isn't needed for code generation. */
6126 static tree
6127 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6128 bool definition, bool need_value, bool need_debug)
6130 tree gnu_expr;
6132 /* If we already elaborated this expression (e.g. it was involved
6133 in the definition of a private type), use the old value. */
6134 if (present_gnu_tree (gnat_expr))
6135 return get_gnu_tree (gnat_expr);
6137 /* If we don't need a value and this is static or a discriminant,
6138 we don't need to do anything. */
6139 if (!need_value
6140 && (Is_OK_Static_Expression (gnat_expr)
6141 || (Nkind (gnat_expr) == N_Identifier
6142 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6143 return NULL_TREE;
6145 /* If it's a static expression, we don't need a variable for debugging. */
6146 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6147 need_debug = false;
6149 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6150 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6151 gnu_name, definition, need_debug);
6153 /* Save the expression in case we try to elaborate this entity again. Since
6154 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6155 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6156 save_gnu_tree (gnat_expr, gnu_expr, true);
6158 return need_value ? gnu_expr : error_mark_node;
6161 /* Similar, but take a GNU expression and always return a result. */
6163 static tree
6164 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6165 bool definition, bool need_debug)
6167 const bool expr_public_p = Is_Public (gnat_entity);
6168 const bool expr_global_p = expr_public_p || global_bindings_p ();
6169 bool expr_variable_p, use_variable;
6171 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6172 reference will have been replaced with a COMPONENT_REF when the type
6173 is being elaborated. However, there are some cases involving child
6174 types where we will. So convert it to a COMPONENT_REF. We hope it
6175 will be at the highest level of the expression in these cases. */
6176 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6177 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6178 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6179 gnu_expr, NULL_TREE);
6181 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6182 that an expression cannot contain both a discriminant and a variable. */
6183 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6184 return gnu_expr;
6186 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6187 a variable that is initialized to contain the expression when the package
6188 containing the definition is elaborated. If this entity is defined at top
6189 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6190 if this is necessary. */
6191 if (CONSTANT_CLASS_P (gnu_expr))
6192 expr_variable_p = false;
6193 else
6195 /* Skip any conversions and simple constant arithmetics to see if the
6196 expression is based on a read-only variable.
6197 ??? This really should remain read-only, but we have to think about
6198 the typing of the tree here. */
6199 tree inner = remove_conversions (gnu_expr, true);
6201 inner = skip_simple_constant_arithmetic (inner);
6203 if (handled_component_p (inner))
6205 HOST_WIDE_INT bitsize, bitpos;
6206 tree offset;
6207 machine_mode mode;
6208 int unsignedp, volatilep;
6210 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6211 &mode, &unsignedp, &volatilep, false);
6212 /* If the offset is variable, err on the side of caution. */
6213 if (offset)
6214 inner = NULL_TREE;
6217 expr_variable_p
6218 = !(inner
6219 && TREE_CODE (inner) == VAR_DECL
6220 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6223 /* We only need to use the variable if we are in a global context since GCC
6224 can do the right thing in the local case. However, when not optimizing,
6225 use it for bounds of loop iteration scheme to avoid code duplication. */
6226 use_variable = expr_variable_p
6227 && (expr_global_p
6228 || (!optimize
6229 && definition
6230 && Is_Itype (gnat_entity)
6231 && Nkind (Associated_Node_For_Itype (gnat_entity))
6232 == N_Loop_Parameter_Specification));
6234 /* Now create it, possibly only for debugging purposes. */
6235 if (use_variable || need_debug)
6237 /* The following variable creation can happen when processing the body of
6238 subprograms that are defined out of the extended main unit and
6239 inlined. In this case, we are not at the global scope, and thus the
6240 new variable must not be tagged "external", as we used to do here as
6241 long as definition == 0. */
6242 const bool external_flag = !definition && expr_global_p;
6243 tree gnu_decl
6244 = create_var_decl_1
6245 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6246 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6247 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6249 DECL_ARTIFICIAL (gnu_decl) = 1;
6251 /* Using this variable at debug time (if need_debug is true) requires a
6252 proper location. The back-end will compute a location for this
6253 variable only if the variable is used by the generated code.
6254 Returning the variable ensures the caller will use it in generated
6255 code. Note that there is no need for a location if the debug info
6256 contains an integer constant.
6257 FIXME: when the encoding-based debug scheme is dropped, move this
6258 condition to the top-level IF block: we will not need to create a
6259 variable anymore in such cases, then. */
6260 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6261 return gnu_decl;
6264 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6267 /* Similar, but take an alignment factor and make it explicit in the tree. */
6269 static tree
6270 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6271 bool definition, bool need_debug, unsigned int align)
6273 tree unit_align = size_int (align / BITS_PER_UNIT);
6274 return
6275 size_binop (MULT_EXPR,
6276 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6277 gnu_expr,
6278 unit_align),
6279 gnat_entity, gnu_name, definition,
6280 need_debug),
6281 unit_align);
6284 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6285 the value passed against the list of choices. */
6287 tree
6288 choices_to_gnu (tree operand, Node_Id choices)
6290 Node_Id choice;
6291 Node_Id gnat_temp;
6292 tree result = boolean_false_node;
6293 tree this_test, low = 0, high = 0, single = 0;
6295 for (choice = First (choices); Present (choice); choice = Next (choice))
6297 switch (Nkind (choice))
6299 case N_Range:
6300 low = gnat_to_gnu (Low_Bound (choice));
6301 high = gnat_to_gnu (High_Bound (choice));
6303 this_test
6304 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6305 build_binary_op (GE_EXPR, boolean_type_node,
6306 operand, low),
6307 build_binary_op (LE_EXPR, boolean_type_node,
6308 operand, high));
6310 break;
6312 case N_Subtype_Indication:
6313 gnat_temp = Range_Expression (Constraint (choice));
6314 low = gnat_to_gnu (Low_Bound (gnat_temp));
6315 high = gnat_to_gnu (High_Bound (gnat_temp));
6317 this_test
6318 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6319 build_binary_op (GE_EXPR, boolean_type_node,
6320 operand, low),
6321 build_binary_op (LE_EXPR, boolean_type_node,
6322 operand, high));
6323 break;
6325 case N_Identifier:
6326 case N_Expanded_Name:
6327 /* This represents either a subtype range, an enumeration
6328 literal, or a constant Ekind says which. If an enumeration
6329 literal or constant, fall through to the next case. */
6330 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6331 && Ekind (Entity (choice)) != E_Constant)
6333 tree type = gnat_to_gnu_type (Entity (choice));
6335 low = TYPE_MIN_VALUE (type);
6336 high = TYPE_MAX_VALUE (type);
6338 this_test
6339 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6340 build_binary_op (GE_EXPR, boolean_type_node,
6341 operand, low),
6342 build_binary_op (LE_EXPR, boolean_type_node,
6343 operand, high));
6344 break;
6347 /* ... fall through ... */
6349 case N_Character_Literal:
6350 case N_Integer_Literal:
6351 single = gnat_to_gnu (choice);
6352 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6353 single);
6354 break;
6356 case N_Others_Choice:
6357 this_test = boolean_true_node;
6358 break;
6360 default:
6361 gcc_unreachable ();
6364 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6365 this_test);
6368 return result;
6371 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6372 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6374 static int
6375 adjust_packed (tree field_type, tree record_type, int packed)
6377 /* If the field contains an item of variable size, we cannot pack it
6378 because we cannot create temporaries of non-fixed size in case
6379 we need to take the address of the field. See addressable_p and
6380 the notes on the addressability issues for further details. */
6381 if (type_has_variable_size (field_type))
6382 return 0;
6384 /* If the alignment of the record is specified and the field type
6385 is over-aligned, request Storage_Unit alignment for the field. */
6386 if (packed == -2)
6388 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6389 return -1;
6390 else
6391 return 0;
6394 return packed;
6397 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6398 placed in GNU_RECORD_TYPE.
6400 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6401 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6402 record has a specified alignment.
6404 DEFINITION is true if this field is for a record being defined.
6406 DEBUG_INFO_P is true if we need to write debug information for types
6407 that we may create in the process. */
6409 static tree
6410 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6411 bool definition, bool debug_info_p)
6413 const Entity_Id gnat_field_type = Etype (gnat_field);
6414 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6415 tree gnu_field_id = get_entity_name (gnat_field);
6416 tree gnu_field, gnu_size, gnu_pos;
6417 bool is_aliased
6418 = Is_Aliased (gnat_field);
6419 bool is_atomic
6420 = (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type));
6421 bool is_volatile
6422 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6423 bool needs_strict_alignment
6424 = (is_aliased || is_volatile || Strict_Alignment (gnat_field_type));
6426 /* If this field requires strict alignment, we cannot pack it because
6427 it would very likely be under-aligned in the record. */
6428 if (needs_strict_alignment)
6429 packed = 0;
6430 else
6431 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6433 /* If a size is specified, use it. Otherwise, if the record type is packed,
6434 use the official RM size. See "Handling of Type'Size Values" in Einfo
6435 for further details. */
6436 if (Known_Esize (gnat_field))
6437 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6438 gnat_field, FIELD_DECL, false, true);
6439 else if (packed == 1)
6440 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6441 gnat_field, FIELD_DECL, false, true);
6442 else
6443 gnu_size = NULL_TREE;
6445 /* If we have a specified size that is smaller than that of the field's type,
6446 or a position is specified, and the field's type is a record that doesn't
6447 require strict alignment, see if we can get either an integral mode form
6448 of the type or a smaller form. If we can, show a size was specified for
6449 the field if there wasn't one already, so we know to make this a bitfield
6450 and avoid making things wider.
6452 Changing to an integral mode form is useful when the record is packed as
6453 we can then place the field at a non-byte-aligned position and so achieve
6454 tighter packing. This is in addition required if the field shares a byte
6455 with another field and the front-end lets the back-end handle the access
6456 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6458 Changing to a smaller form is required if the specified size is smaller
6459 than that of the field's type and the type contains sub-fields that are
6460 padded, in order to avoid generating accesses to these sub-fields that
6461 are wider than the field.
6463 We avoid the transformation if it is not required or potentially useful,
6464 as it might entail an increase of the field's alignment and have ripple
6465 effects on the outer record type. A typical case is a field known to be
6466 byte-aligned and not to share a byte with another field. */
6467 if (!needs_strict_alignment
6468 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6469 && !TYPE_FAT_POINTER_P (gnu_field_type)
6470 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6471 && (packed == 1
6472 || (gnu_size
6473 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6474 || (Present (Component_Clause (gnat_field))
6475 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6476 % BITS_PER_UNIT == 0
6477 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6479 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6480 if (gnu_packable_type != gnu_field_type)
6482 gnu_field_type = gnu_packable_type;
6483 if (!gnu_size)
6484 gnu_size = rm_size (gnu_field_type);
6488 if (Is_Atomic (gnat_field))
6489 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6491 if (Present (Component_Clause (gnat_field)))
6493 Node_Id gnat_clause = Component_Clause (gnat_field);
6494 Entity_Id gnat_parent
6495 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6497 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6498 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6499 gnat_field, FIELD_DECL, false, true);
6501 /* Ensure the position does not overlap with the parent subtype, if there
6502 is one. This test is omitted if the parent of the tagged type has a
6503 full rep clause since, in this case, component clauses are allowed to
6504 overlay the space allocated for the parent type and the front-end has
6505 checked that there are no overlapping components. */
6506 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6508 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6510 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6511 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6512 post_error_ne_tree
6513 ("offset of& must be beyond parent{, minimum allowed is ^}",
6514 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6517 /* If this field needs strict alignment, make sure that the record is
6518 sufficiently aligned and that the position and size are consistent
6519 with the type. But don't do it if we are just annotating types and
6520 the field's type is tagged, since tagged types aren't fully laid out
6521 in this mode. Also, note that atomic implies volatile so the inner
6522 test sequences ordering is significant here. */
6523 if (needs_strict_alignment
6524 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6526 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6528 if (TYPE_ALIGN (gnu_record_type) < type_align)
6529 TYPE_ALIGN (gnu_record_type) = type_align;
6531 /* If the position is not a multiple of the alignment of the type,
6532 then error out and reset the position. */
6533 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6534 bitsize_int (type_align))))
6536 const char *s;
6538 if (is_atomic)
6539 s = "position of atomic field& must be multiple of ^ bits";
6540 else if (is_aliased)
6541 s = "position of aliased field& must be multiple of ^ bits";
6542 else if (is_volatile)
6543 s = "position of volatile field& must be multiple of ^ bits";
6544 else if (Strict_Alignment (gnat_field_type))
6545 s = "position of & with aliased or tagged part must be"
6546 " multiple of ^ bits";
6547 else
6548 gcc_unreachable ();
6550 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6551 type_align);
6552 gnu_pos = NULL_TREE;
6555 if (gnu_size)
6557 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6558 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6560 /* If the size is lower than that of the type, or greater for
6561 atomic and aliased, then error out and reset the size. */
6562 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6564 const char *s;
6566 if (is_atomic)
6567 s = "size of atomic field& must be ^ bits";
6568 else if (is_aliased)
6569 s = "size of aliased field& must be ^ bits";
6570 else if (is_volatile)
6571 s = "size of volatile field& must be at least ^ bits";
6572 else if (Strict_Alignment (gnat_field_type))
6573 s = "size of & with aliased or tagged part must be"
6574 " at least ^ bits";
6575 else
6576 gcc_unreachable ();
6578 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6579 gnu_type_size);
6580 gnu_size = NULL_TREE;
6583 /* Likewise if the size is not a multiple of a byte, */
6584 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6585 bitsize_unit_node)))
6587 const char *s;
6589 if (is_volatile)
6590 s = "size of volatile field& must be multiple of"
6591 " Storage_Unit";
6592 else if (Strict_Alignment (gnat_field_type))
6593 s = "size of & with aliased or tagged part must be"
6594 " multiple of Storage_Unit";
6595 else
6596 gcc_unreachable ();
6598 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6599 gnu_size = NULL_TREE;
6605 /* If the record has rep clauses and this is the tag field, make a rep
6606 clause for it as well. */
6607 else if (Has_Specified_Layout (Scope (gnat_field))
6608 && Chars (gnat_field) == Name_uTag)
6610 gnu_pos = bitsize_zero_node;
6611 gnu_size = TYPE_SIZE (gnu_field_type);
6614 else
6616 gnu_pos = NULL_TREE;
6618 /* If we are packing the record and the field is BLKmode, round the
6619 size up to a byte boundary. */
6620 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6621 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6624 /* We need to make the size the maximum for the type if it is
6625 self-referential and an unconstrained type. In that case, we can't
6626 pack the field since we can't make a copy to align it. */
6627 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6628 && !gnu_size
6629 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6630 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6632 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6633 packed = 0;
6636 /* If a size is specified, adjust the field's type to it. */
6637 if (gnu_size)
6639 tree orig_field_type;
6641 /* If the field's type is justified modular, we would need to remove
6642 the wrapper to (better) meet the layout requirements. However we
6643 can do so only if the field is not aliased to preserve the unique
6644 layout and if the prescribed size is not greater than that of the
6645 packed array to preserve the justification. */
6646 if (!needs_strict_alignment
6647 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6648 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6649 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6650 <= 0)
6651 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6653 /* Similarly if the field's type is a misaligned integral type, but
6654 there is no restriction on the size as there is no justification. */
6655 if (!needs_strict_alignment
6656 && TYPE_IS_PADDING_P (gnu_field_type)
6657 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6658 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6660 gnu_field_type
6661 = make_type_from_size (gnu_field_type, gnu_size,
6662 Has_Biased_Representation (gnat_field));
6664 orig_field_type = gnu_field_type;
6665 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6666 false, false, definition, true);
6668 /* If a padding record was made, declare it now since it will never be
6669 declared otherwise. This is necessary to ensure that its subtrees
6670 are properly marked. */
6671 if (gnu_field_type != orig_field_type
6672 && !DECL_P (TYPE_NAME (gnu_field_type)))
6673 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6674 debug_info_p, gnat_field);
6677 /* Otherwise (or if there was an error), don't specify a position. */
6678 else
6679 gnu_pos = NULL_TREE;
6681 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6682 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6684 /* Now create the decl for the field. */
6685 gnu_field
6686 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6687 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6688 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6689 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6690 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6692 if (Ekind (gnat_field) == E_Discriminant)
6693 DECL_DISCRIMINANT_NUMBER (gnu_field)
6694 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6696 return gnu_field;
6699 /* Return true if at least one member of COMPONENT_LIST needs strict
6700 alignment. */
6702 static bool
6703 components_need_strict_alignment (Node_Id component_list)
6705 Node_Id component_decl;
6707 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6708 Present (component_decl);
6709 component_decl = Next_Non_Pragma (component_decl))
6711 Entity_Id gnat_field = Defining_Entity (component_decl);
6713 if (Is_Aliased (gnat_field))
6714 return true;
6716 if (Strict_Alignment (Etype (gnat_field)))
6717 return true;
6720 return false;
6723 /* Return true if TYPE is a type with variable size or a padding type with a
6724 field of variable size or a record that has a field with such a type. */
6726 static bool
6727 type_has_variable_size (tree type)
6729 tree field;
6731 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6732 return true;
6734 if (TYPE_IS_PADDING_P (type)
6735 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6736 return true;
6738 if (!RECORD_OR_UNION_TYPE_P (type))
6739 return false;
6741 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6742 if (type_has_variable_size (TREE_TYPE (field)))
6743 return true;
6745 return false;
6748 /* Return true if FIELD is an artificial field. */
6750 static bool
6751 field_is_artificial (tree field)
6753 /* These fields are generated by the front-end proper. */
6754 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6755 return true;
6757 /* These fields are generated by gigi. */
6758 if (DECL_INTERNAL_P (field))
6759 return true;
6761 return false;
6764 /* Return true if FIELD is a non-artificial aliased field. */
6766 static bool
6767 field_is_aliased (tree field)
6769 if (field_is_artificial (field))
6770 return false;
6772 return DECL_ALIASED_P (field);
6775 /* Return true if FIELD is a non-artificial field with self-referential
6776 size. */
6778 static bool
6779 field_has_self_size (tree field)
6781 if (field_is_artificial (field))
6782 return false;
6784 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6785 return false;
6787 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6790 /* Return true if FIELD is a non-artificial field with variable size. */
6792 static bool
6793 field_has_variable_size (tree field)
6795 if (field_is_artificial (field))
6796 return false;
6798 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6799 return false;
6801 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6804 /* qsort comparer for the bit positions of two record components. */
6806 static int
6807 compare_field_bitpos (const PTR rt1, const PTR rt2)
6809 const_tree const field1 = * (const_tree const *) rt1;
6810 const_tree const field2 = * (const_tree const *) rt2;
6811 const int ret
6812 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6814 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6817 /* Structure holding information for a given variant. */
6818 typedef struct vinfo
6820 /* The record type of the variant. */
6821 tree type;
6823 /* The name of the variant. */
6824 tree name;
6826 /* The qualifier of the variant. */
6827 tree qual;
6829 /* Whether the variant has a rep clause. */
6830 bool has_rep;
6832 /* Whether the variant is packed. */
6833 bool packed;
6835 } vinfo_t;
6837 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6838 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6839 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6840 When called from gnat_to_gnu_entity during the processing of a record type
6841 definition, the GCC node for the parent, if any, will be the single field
6842 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6843 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6844 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6846 PACKED is 1 if this is for a packed record, -1 if this is for a record
6847 with Component_Alignment of Storage_Unit, -2 if this is for a record
6848 with a specified alignment.
6850 DEFINITION is true if we are defining this record type.
6852 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6853 out the record. This means the alignment only serves to force fields to
6854 be bitfields, but not to require the record to be that aligned. This is
6855 used for variants.
6857 ALL_REP is true if a rep clause is present for all the fields.
6859 UNCHECKED_UNION is true if we are building this type for a record with a
6860 Pragma Unchecked_Union.
6862 ARTIFICIAL is true if this is a type that was generated by the compiler.
6864 DEBUG_INFO is true if we need to write debug information about the type.
6866 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6867 mean that its contents may be unused as well, only the container itself.
6869 REORDER is true if we are permitted to reorder components of this type.
6871 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6872 the outer record type down to this variant level. It is nonzero only if
6873 all the fields down to this level have a rep clause and ALL_REP is false.
6875 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6876 with a rep clause is to be added; in this case, that is all that should
6877 be done with such fields and the return value will be false. */
6879 static bool
6880 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6881 tree gnu_field_list, int packed, bool definition,
6882 bool cancel_alignment, bool all_rep,
6883 bool unchecked_union, bool artificial,
6884 bool debug_info, bool maybe_unused, bool reorder,
6885 tree first_free_pos, tree *p_gnu_rep_list)
6887 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6888 bool variants_have_rep = all_rep;
6889 bool layout_with_rep = false;
6890 bool has_self_field = false;
6891 bool has_aliased_after_self_field = false;
6892 Node_Id component_decl, variant_part;
6893 tree gnu_field, gnu_next, gnu_last;
6894 tree gnu_variant_part = NULL_TREE;
6895 tree gnu_rep_list = NULL_TREE;
6896 tree gnu_var_list = NULL_TREE;
6897 tree gnu_self_list = NULL_TREE;
6898 tree gnu_zero_list = NULL_TREE;
6900 /* For each component referenced in a component declaration create a GCC
6901 field and add it to the list, skipping pragmas in the GNAT list. */
6902 gnu_last = tree_last (gnu_field_list);
6903 if (Present (Component_Items (gnat_component_list)))
6904 for (component_decl
6905 = First_Non_Pragma (Component_Items (gnat_component_list));
6906 Present (component_decl);
6907 component_decl = Next_Non_Pragma (component_decl))
6909 Entity_Id gnat_field = Defining_Entity (component_decl);
6910 Name_Id gnat_name = Chars (gnat_field);
6912 /* If present, the _Parent field must have been created as the single
6913 field of the record type. Put it before any other fields. */
6914 if (gnat_name == Name_uParent)
6916 gnu_field = TYPE_FIELDS (gnu_record_type);
6917 gnu_field_list = chainon (gnu_field_list, gnu_field);
6919 else
6921 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6922 definition, debug_info);
6924 /* If this is the _Tag field, put it before any other fields. */
6925 if (gnat_name == Name_uTag)
6926 gnu_field_list = chainon (gnu_field_list, gnu_field);
6928 /* If this is the _Controller field, put it before the other
6929 fields except for the _Tag or _Parent field. */
6930 else if (gnat_name == Name_uController && gnu_last)
6932 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6933 DECL_CHAIN (gnu_last) = gnu_field;
6936 /* If this is a regular field, put it after the other fields. */
6937 else
6939 DECL_CHAIN (gnu_field) = gnu_field_list;
6940 gnu_field_list = gnu_field;
6941 if (!gnu_last)
6942 gnu_last = gnu_field;
6944 /* And record information for the final layout. */
6945 if (field_has_self_size (gnu_field))
6946 has_self_field = true;
6947 else if (has_self_field && field_is_aliased (gnu_field))
6948 has_aliased_after_self_field = true;
6952 save_gnu_tree (gnat_field, gnu_field, false);
6955 /* At the end of the component list there may be a variant part. */
6956 variant_part = Variant_Part (gnat_component_list);
6958 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6959 mutually exclusive and should go in the same memory. To do this we need
6960 to treat each variant as a record whose elements are created from the
6961 component list for the variant. So here we create the records from the
6962 lists for the variants and put them all into the QUAL_UNION_TYPE.
6963 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6964 use GNU_RECORD_TYPE if there are no fields so far. */
6965 if (Present (variant_part))
6967 Node_Id gnat_discr = Name (variant_part), variant;
6968 tree gnu_discr = gnat_to_gnu (gnat_discr);
6969 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
6970 tree gnu_var_name
6971 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6972 "XVN");
6973 tree gnu_union_type, gnu_union_name;
6974 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6975 bool union_field_needs_strict_alignment = false;
6976 auto_vec <vinfo_t, 16> variant_types;
6977 vinfo_t *gnu_variant;
6978 unsigned int variants_align = 0;
6979 unsigned int i;
6981 gnu_union_name
6982 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6984 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6985 are all in the variant part, to match the layout of C unions. There
6986 is an associated check below. */
6987 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6988 gnu_union_type = gnu_record_type;
6989 else
6991 gnu_union_type
6992 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6994 TYPE_NAME (gnu_union_type) = gnu_union_name;
6995 TYPE_ALIGN (gnu_union_type) = 0;
6996 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6999 /* If all the fields down to this level have a rep clause, find out
7000 whether all the fields at this level also have one. If so, then
7001 compute the new first free position to be passed downward. */
7002 this_first_free_pos = first_free_pos;
7003 if (this_first_free_pos)
7005 for (gnu_field = gnu_field_list;
7006 gnu_field;
7007 gnu_field = DECL_CHAIN (gnu_field))
7008 if (DECL_FIELD_OFFSET (gnu_field))
7010 tree pos = bit_position (gnu_field);
7011 if (!tree_int_cst_lt (pos, this_first_free_pos))
7012 this_first_free_pos
7013 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7015 else
7017 this_first_free_pos = NULL_TREE;
7018 break;
7022 /* We build the variants in two passes. The bulk of the work is done in
7023 the first pass, that is to say translating the GNAT nodes, building
7024 the container types and computing the associated properties. However
7025 we cannot finish up the container types during this pass because we
7026 don't know where the variant part will be placed until the end. */
7027 for (variant = First_Non_Pragma (Variants (variant_part));
7028 Present (variant);
7029 variant = Next_Non_Pragma (variant))
7031 tree gnu_variant_type = make_node (RECORD_TYPE);
7032 tree gnu_inner_name, gnu_qual;
7033 bool has_rep;
7034 int field_packed;
7035 vinfo_t vinfo;
7037 Get_Variant_Encoding (variant);
7038 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7039 TYPE_NAME (gnu_variant_type)
7040 = concat_name (gnu_union_name,
7041 IDENTIFIER_POINTER (gnu_inner_name));
7043 /* Set the alignment of the inner type in case we need to make
7044 inner objects into bitfields, but then clear it out so the
7045 record actually gets only the alignment required. */
7046 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7047 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7049 /* Similarly, if the outer record has a size specified and all
7050 the fields have a rep clause, we can propagate the size. */
7051 if (all_rep_and_size)
7053 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7054 TYPE_SIZE_UNIT (gnu_variant_type)
7055 = TYPE_SIZE_UNIT (gnu_record_type);
7058 /* Add the fields into the record type for the variant. Note that
7059 we aren't sure to really use it at this point, see below. */
7060 has_rep
7061 = components_to_record (gnu_variant_type, Component_List (variant),
7062 NULL_TREE, packed, definition,
7063 !all_rep_and_size, all_rep,
7064 unchecked_union,
7065 true, debug_info, true, reorder,
7066 this_first_free_pos,
7067 all_rep || this_first_free_pos
7068 ? NULL : &gnu_rep_list);
7070 /* Translate the qualifier and annotate the GNAT node. */
7071 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7072 Set_Present_Expr (variant, annotate_value (gnu_qual));
7074 /* Deal with packedness like in gnat_to_gnu_field. */
7075 if (components_need_strict_alignment (Component_List (variant)))
7077 field_packed = 0;
7078 union_field_needs_strict_alignment = true;
7080 else
7081 field_packed
7082 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7084 /* Push this variant onto the stack for the second pass. */
7085 vinfo.type = gnu_variant_type;
7086 vinfo.name = gnu_inner_name;
7087 vinfo.qual = gnu_qual;
7088 vinfo.has_rep = has_rep;
7089 vinfo.packed = field_packed;
7090 variant_types.safe_push (vinfo);
7092 /* Compute the global properties that will determine the placement of
7093 the variant part. */
7094 variants_have_rep |= has_rep;
7095 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7096 variants_align = TYPE_ALIGN (gnu_variant_type);
7099 /* Round up the first free position to the alignment of the variant part
7100 for the variants without rep clause. This will guarantee a consistent
7101 layout independently of the placement of the variant part. */
7102 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7103 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7105 /* In the second pass, the container types are adjusted if necessary and
7106 finished up, then the corresponding fields of the variant part are
7107 built with their qualifier, unless this is an unchecked union. */
7108 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7110 tree gnu_variant_type = gnu_variant->type;
7111 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7113 /* If this is an Unchecked_Union whose fields are all in the variant
7114 part and we have a single field with no representation clause or
7115 placed at offset zero, use the field directly to match the layout
7116 of C unions. */
7117 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7118 && gnu_field_list
7119 && !DECL_CHAIN (gnu_field_list)
7120 && (!DECL_FIELD_OFFSET (gnu_field_list)
7121 || integer_zerop (bit_position (gnu_field_list))))
7123 gnu_field = gnu_field_list;
7124 DECL_CONTEXT (gnu_field) = gnu_record_type;
7126 else
7128 /* Finalize the variant type now. We used to throw away empty
7129 record types but we no longer do that because we need them to
7130 generate complete debug info for the variant; otherwise, the
7131 union type definition will be lacking the fields associated
7132 with these empty variants. */
7133 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7135 /* The variant part will be at offset 0 so we need to ensure
7136 that the fields are laid out starting from the first free
7137 position at this level. */
7138 tree gnu_rep_type = make_node (RECORD_TYPE);
7139 tree gnu_rep_part;
7140 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7141 gnu_rep_part
7142 = create_rep_part (gnu_rep_type, gnu_variant_type,
7143 this_first_free_pos);
7144 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7145 gnu_field_list = gnu_rep_part;
7146 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7147 false);
7150 if (debug_info)
7151 rest_of_record_type_compilation (gnu_variant_type);
7152 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7153 true, debug_info, gnat_component_list);
7155 gnu_field
7156 = create_field_decl (gnu_variant->name, gnu_variant_type,
7157 gnu_union_type,
7158 all_rep_and_size
7159 ? TYPE_SIZE (gnu_variant_type) : 0,
7160 variants_have_rep ? bitsize_zero_node : 0,
7161 gnu_variant->packed, 0);
7163 DECL_INTERNAL_P (gnu_field) = 1;
7165 if (!unchecked_union)
7166 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7169 DECL_CHAIN (gnu_field) = gnu_variant_list;
7170 gnu_variant_list = gnu_field;
7173 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7174 if (gnu_variant_list)
7176 int union_field_packed;
7178 if (all_rep_and_size)
7180 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7181 TYPE_SIZE_UNIT (gnu_union_type)
7182 = TYPE_SIZE_UNIT (gnu_record_type);
7185 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7186 all_rep_and_size ? 1 : 0, debug_info);
7188 /* If GNU_UNION_TYPE is our record type, it means we must have an
7189 Unchecked_Union with no fields. Verify that and, if so, just
7190 return. */
7191 if (gnu_union_type == gnu_record_type)
7193 gcc_assert (unchecked_union
7194 && !gnu_field_list
7195 && !gnu_rep_list);
7196 return variants_have_rep;
7199 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7200 debug_info, gnat_component_list);
7202 /* Deal with packedness like in gnat_to_gnu_field. */
7203 if (union_field_needs_strict_alignment)
7204 union_field_packed = 0;
7205 else
7206 union_field_packed
7207 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7209 gnu_variant_part
7210 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7211 all_rep_and_size
7212 ? TYPE_SIZE (gnu_union_type) : 0,
7213 variants_have_rep ? bitsize_zero_node : 0,
7214 union_field_packed, 0);
7216 DECL_INTERNAL_P (gnu_variant_part) = 1;
7220 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7221 permitted to reorder components, self-referential sizes or variable sizes.
7222 If they do, pull them out and put them onto the appropriate list. We have
7223 to do this in a separate pass since we want to handle the discriminants
7224 but can't play with them until we've used them in debugging data above.
7226 Similarly, pull out the fields with zero size and no rep clause, as they
7227 would otherwise modify the layout and thus very likely run afoul of the
7228 Ada semantics, which are different from those of C here.
7230 ??? If we reorder them, debugging information will be wrong but there is
7231 nothing that can be done about this at the moment. */
7232 gnu_last = NULL_TREE;
7234 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7235 do { \
7236 if (gnu_last) \
7237 DECL_CHAIN (gnu_last) = gnu_next; \
7238 else \
7239 gnu_field_list = gnu_next; \
7241 DECL_CHAIN (gnu_field) = (LIST); \
7242 (LIST) = gnu_field; \
7243 } while (0)
7245 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7247 gnu_next = DECL_CHAIN (gnu_field);
7249 if (DECL_FIELD_OFFSET (gnu_field))
7251 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7252 continue;
7255 if ((reorder || has_aliased_after_self_field)
7256 && field_has_self_size (gnu_field))
7258 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7259 continue;
7262 if (reorder && field_has_variable_size (gnu_field))
7264 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7265 continue;
7268 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7270 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7271 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7272 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7273 if (field_is_aliased (gnu_field))
7274 TYPE_ALIGN (gnu_record_type)
7275 = MAX (TYPE_ALIGN (gnu_record_type),
7276 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7277 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7278 continue;
7281 gnu_last = gnu_field;
7284 #undef MOVE_FROM_FIELD_LIST_TO
7286 gnu_field_list = nreverse (gnu_field_list);
7288 /* If permitted, we reorder the fields as follows:
7290 1) all fixed length fields,
7291 2) all fields whose length doesn't depend on discriminants,
7292 3) all fields whose length depends on discriminants,
7293 4) the variant part,
7295 within the record and within each variant recursively. */
7296 if (reorder)
7297 gnu_field_list
7298 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7300 /* Otherwise, if there is an aliased field placed after a field whose length
7301 depends on discriminants, we put all the fields of the latter sort, last.
7302 We need to do this in case an object of this record type is mutable. */
7303 else if (has_aliased_after_self_field)
7304 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7306 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7307 in our REP list to the previous level because this level needs them in
7308 order to do a correct layout, i.e. avoid having overlapping fields. */
7309 if (p_gnu_rep_list && gnu_rep_list)
7310 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7312 /* Otherwise, sort the fields by bit position and put them into their own
7313 record, before the others, if we also have fields without rep clause. */
7314 else if (gnu_rep_list)
7316 tree gnu_rep_type, gnu_rep_part;
7317 int i, len = list_length (gnu_rep_list);
7318 tree *gnu_arr = XALLOCAVEC (tree, len);
7320 /* If all the fields have a rep clause, we can do a flat layout. */
7321 layout_with_rep = !gnu_field_list
7322 && (!gnu_variant_part || variants_have_rep);
7323 gnu_rep_type
7324 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7326 for (gnu_field = gnu_rep_list, i = 0;
7327 gnu_field;
7328 gnu_field = DECL_CHAIN (gnu_field), i++)
7329 gnu_arr[i] = gnu_field;
7331 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7333 /* Put the fields in the list in order of increasing position, which
7334 means we start from the end. */
7335 gnu_rep_list = NULL_TREE;
7336 for (i = len - 1; i >= 0; i--)
7338 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7339 gnu_rep_list = gnu_arr[i];
7340 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7343 if (layout_with_rep)
7344 gnu_field_list = gnu_rep_list;
7345 else
7347 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7349 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7350 without rep clause are laid out starting from this position.
7351 Therefore, we force it as a minimal size on the REP part. */
7352 gnu_rep_part
7353 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7355 /* Chain the REP part at the beginning of the field list. */
7356 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7357 gnu_field_list = gnu_rep_part;
7361 /* Chain the variant part at the end of the field list. */
7362 if (gnu_variant_part)
7363 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7365 if (cancel_alignment)
7366 TYPE_ALIGN (gnu_record_type) = 0;
7368 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7370 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7371 debug_info && !maybe_unused);
7373 /* Chain the fields with zero size at the beginning of the field list. */
7374 if (gnu_zero_list)
7375 TYPE_FIELDS (gnu_record_type)
7376 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7378 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7381 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7382 placed into an Esize, Component_Bit_Offset, or Component_Size value
7383 in the GNAT tree. */
7385 static Uint
7386 annotate_value (tree gnu_size)
7388 TCode tcode;
7389 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7390 struct tree_int_map in;
7391 int i;
7393 /* See if we've already saved the value for this node. */
7394 if (EXPR_P (gnu_size))
7396 struct tree_int_map *e;
7398 in.base.from = gnu_size;
7399 e = annotate_value_cache->find (&in);
7401 if (e)
7402 return (Node_Ref_Or_Val) e->to;
7404 else
7405 in.base.from = NULL_TREE;
7407 /* If we do not return inside this switch, TCODE will be set to the
7408 code to use for a Create_Node operand and LEN (set above) will be
7409 the number of recursive calls for us to make. */
7411 switch (TREE_CODE (gnu_size))
7413 case INTEGER_CST:
7414 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7416 case COMPONENT_REF:
7417 /* The only case we handle here is a simple discriminant reference. */
7418 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7420 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7422 /* Climb up the chain of successive extensions, if any. */
7423 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7424 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7425 == parent_name_id)
7426 gnu_size = TREE_OPERAND (gnu_size, 0);
7428 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7429 return
7430 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7433 return No_Uint;
7435 CASE_CONVERT: case NON_LVALUE_EXPR:
7436 return annotate_value (TREE_OPERAND (gnu_size, 0));
7438 /* Now just list the operations we handle. */
7439 case COND_EXPR: tcode = Cond_Expr; break;
7440 case PLUS_EXPR: tcode = Plus_Expr; break;
7441 case MINUS_EXPR: tcode = Minus_Expr; break;
7442 case MULT_EXPR: tcode = Mult_Expr; break;
7443 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7444 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7445 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7446 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7447 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7448 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7449 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7450 case NEGATE_EXPR: tcode = Negate_Expr; break;
7451 case MIN_EXPR: tcode = Min_Expr; break;
7452 case MAX_EXPR: tcode = Max_Expr; break;
7453 case ABS_EXPR: tcode = Abs_Expr; break;
7454 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7455 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7456 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7457 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7458 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7459 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7460 case LT_EXPR: tcode = Lt_Expr; break;
7461 case LE_EXPR: tcode = Le_Expr; break;
7462 case GT_EXPR: tcode = Gt_Expr; break;
7463 case GE_EXPR: tcode = Ge_Expr; break;
7464 case EQ_EXPR: tcode = Eq_Expr; break;
7465 case NE_EXPR: tcode = Ne_Expr; break;
7467 case BIT_AND_EXPR:
7468 tcode = Bit_And_Expr;
7469 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7470 Such values appear in expressions with aligning patterns. Note that,
7471 since sizetype is unsigned, we have to jump through some hoops. */
7472 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7474 tree op1 = TREE_OPERAND (gnu_size, 1);
7475 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7476 if (wi::neg_p (signed_op1))
7478 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7479 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7482 break;
7484 case CALL_EXPR:
7485 /* In regular mode, inline back only if symbolic annotation is requested
7486 in order to avoid memory explosion on big discriminated record types.
7487 But not in ASIS mode, as symbolic annotation is required for DDA. */
7488 if (List_Representation_Info == 3 || type_annotate_only)
7490 tree t = maybe_inline_call_in_expr (gnu_size);
7491 if (t)
7492 return annotate_value (t);
7494 else
7495 return Uint_Minus_1;
7497 /* Fall through... */
7499 default:
7500 return No_Uint;
7503 /* Now get each of the operands that's relevant for this code. If any
7504 cannot be expressed as a repinfo node, say we can't. */
7505 for (i = 0; i < 3; i++)
7506 ops[i] = No_Uint;
7508 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7510 if (i == 1 && pre_op1 != No_Uint)
7511 ops[i] = pre_op1;
7512 else
7513 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7514 if (ops[i] == No_Uint)
7515 return No_Uint;
7518 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7520 /* Save the result in the cache. */
7521 if (in.base.from)
7523 struct tree_int_map **h;
7524 /* We can't assume the hash table data hasn't moved since the initial
7525 look up, so we have to search again. Allocating and inserting an
7526 entry at that point would be an alternative, but then we'd better
7527 discard the entry if we decided not to cache it. */
7528 h = annotate_value_cache->find_slot (&in, INSERT);
7529 gcc_assert (!*h);
7530 *h = ggc_alloc<tree_int_map> ();
7531 (*h)->base.from = gnu_size;
7532 (*h)->to = ret;
7535 return ret;
7538 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7539 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7540 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7541 BY_REF is true if the object is used by reference. */
7543 void
7544 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7546 if (by_ref)
7548 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7549 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7550 else
7551 gnu_type = TREE_TYPE (gnu_type);
7554 if (Unknown_Esize (gnat_entity))
7556 if (TREE_CODE (gnu_type) == RECORD_TYPE
7557 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7558 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7559 else if (!size)
7560 size = TYPE_SIZE (gnu_type);
7562 if (size)
7563 Set_Esize (gnat_entity, annotate_value (size));
7566 if (Unknown_Alignment (gnat_entity))
7567 Set_Alignment (gnat_entity,
7568 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7571 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7572 Return NULL_TREE if there is no such element in the list. */
7574 static tree
7575 purpose_member_field (const_tree elem, tree list)
7577 while (list)
7579 tree field = TREE_PURPOSE (list);
7580 if (SAME_FIELD_P (field, elem))
7581 return list;
7582 list = TREE_CHAIN (list);
7584 return NULL_TREE;
7587 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7588 set Component_Bit_Offset and Esize of the components to the position and
7589 size used by Gigi. */
7591 static void
7592 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7594 Entity_Id gnat_field;
7595 tree gnu_list;
7597 /* We operate by first making a list of all fields and their position (we
7598 can get the size easily) and then update all the sizes in the tree. */
7599 gnu_list
7600 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7601 BIGGEST_ALIGNMENT, NULL_TREE);
7603 for (gnat_field = First_Entity (gnat_entity);
7604 Present (gnat_field);
7605 gnat_field = Next_Entity (gnat_field))
7606 if (Ekind (gnat_field) == E_Component
7607 || (Ekind (gnat_field) == E_Discriminant
7608 && !Is_Unchecked_Union (Scope (gnat_field))))
7610 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7611 gnu_list);
7612 if (t)
7614 tree parent_offset;
7616 /* If we are just annotating types and the type is tagged, the tag
7617 and the parent components are not generated by the front-end so
7618 we need to add the appropriate offset to each component without
7619 representation clause. */
7620 if (type_annotate_only
7621 && Is_Tagged_Type (gnat_entity)
7622 && No (Component_Clause (gnat_field)))
7624 /* For a component appearing in the current extension, the
7625 offset is the size of the parent. */
7626 if (Is_Derived_Type (gnat_entity)
7627 && Original_Record_Component (gnat_field) == gnat_field)
7628 parent_offset
7629 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7630 bitsizetype);
7631 else
7632 parent_offset = bitsize_int (POINTER_SIZE);
7634 if (TYPE_FIELDS (gnu_type))
7635 parent_offset
7636 = round_up (parent_offset,
7637 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7639 else
7640 parent_offset = bitsize_zero_node;
7642 Set_Component_Bit_Offset
7643 (gnat_field,
7644 annotate_value
7645 (size_binop (PLUS_EXPR,
7646 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7647 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7648 parent_offset)));
7650 Set_Esize (gnat_field,
7651 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7653 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7655 /* If there is no entry, this is an inherited component whose
7656 position is the same as in the parent type. */
7657 Set_Component_Bit_Offset
7658 (gnat_field,
7659 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7661 Set_Esize (gnat_field,
7662 Esize (Original_Record_Component (gnat_field)));
7667 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7668 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7669 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7670 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7671 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7672 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7673 pre-existing list to be chained to the newly created entries. */
7675 static tree
7676 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7677 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7679 tree gnu_field;
7681 for (gnu_field = TYPE_FIELDS (gnu_type);
7682 gnu_field;
7683 gnu_field = DECL_CHAIN (gnu_field))
7685 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7686 DECL_FIELD_BIT_OFFSET (gnu_field));
7687 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7688 DECL_FIELD_OFFSET (gnu_field));
7689 unsigned int our_offset_align
7690 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7691 tree v = make_tree_vec (3);
7693 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7694 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7695 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7696 gnu_list = tree_cons (gnu_field, v, gnu_list);
7698 /* Recurse on internal fields, flattening the nested fields except for
7699 those in the variant part, if requested. */
7700 if (DECL_INTERNAL_P (gnu_field))
7702 tree gnu_field_type = TREE_TYPE (gnu_field);
7703 if (do_not_flatten_variant
7704 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7705 gnu_list
7706 = build_position_list (gnu_field_type, do_not_flatten_variant,
7707 size_zero_node, bitsize_zero_node,
7708 BIGGEST_ALIGNMENT, gnu_list);
7709 else
7710 gnu_list
7711 = build_position_list (gnu_field_type, do_not_flatten_variant,
7712 gnu_our_offset, gnu_our_bitpos,
7713 our_offset_align, gnu_list);
7717 return gnu_list;
7720 /* Return a list describing the substitutions needed to reflect the
7721 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7722 be in any order. The values in an element of the list are in the form
7723 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7724 a definition of GNAT_SUBTYPE. */
7726 static vec<subst_pair>
7727 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7729 vec<subst_pair> gnu_list = vNULL;
7730 Entity_Id gnat_discrim;
7731 Node_Id gnat_constr;
7733 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7734 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7735 Present (gnat_discrim);
7736 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7737 gnat_constr = Next_Elmt (gnat_constr))
7738 /* Ignore access discriminants. */
7739 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7741 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7742 tree replacement = convert (TREE_TYPE (gnu_field),
7743 elaborate_expression
7744 (Node (gnat_constr), gnat_subtype,
7745 get_entity_name (gnat_discrim),
7746 definition, true, false));
7747 subst_pair s = {gnu_field, replacement};
7748 gnu_list.safe_push (s);
7751 return gnu_list;
7754 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7755 variants of QUAL_UNION_TYPE that are still relevant after applying
7756 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7757 list to be prepended to the newly created entries. */
7759 static vec<variant_desc>
7760 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7761 vec<variant_desc> gnu_list)
7763 tree gnu_field;
7765 for (gnu_field = TYPE_FIELDS (qual_union_type);
7766 gnu_field;
7767 gnu_field = DECL_CHAIN (gnu_field))
7769 tree qual = DECL_QUALIFIER (gnu_field);
7770 unsigned int i;
7771 subst_pair *s;
7773 FOR_EACH_VEC_ELT (subst_list, i, s)
7774 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7776 /* If the new qualifier is not unconditionally false, its variant may
7777 still be accessed. */
7778 if (!integer_zerop (qual))
7780 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7781 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7783 gnu_list.safe_push (v);
7785 /* Recurse on the variant subpart of the variant, if any. */
7786 variant_subpart = get_variant_part (variant_type);
7787 if (variant_subpart)
7788 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7789 subst_list, gnu_list);
7791 /* If the new qualifier is unconditionally true, the subsequent
7792 variants cannot be accessed. */
7793 if (integer_onep (qual))
7794 break;
7798 return gnu_list;
7801 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7802 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7803 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7804 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7805 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7806 true if we are being called to process the Component_Size of GNAT_OBJECT;
7807 this is used only for error messages. ZERO_OK is true if a size of zero
7808 is permitted; if ZERO_OK is false, it means that a size of zero should be
7809 treated as an unspecified size. */
7811 static tree
7812 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7813 enum tree_code kind, bool component_p, bool zero_ok)
7815 Node_Id gnat_error_node;
7816 tree type_size, size;
7818 /* Return 0 if no size was specified. */
7819 if (uint_size == No_Uint)
7820 return NULL_TREE;
7822 /* Ignore a negative size since that corresponds to our back-annotation. */
7823 if (UI_Lt (uint_size, Uint_0))
7824 return NULL_TREE;
7826 /* Find the node to use for error messages. */
7827 if ((Ekind (gnat_object) == E_Component
7828 || Ekind (gnat_object) == E_Discriminant)
7829 && Present (Component_Clause (gnat_object)))
7830 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7831 else if (Present (Size_Clause (gnat_object)))
7832 gnat_error_node = Expression (Size_Clause (gnat_object));
7833 else
7834 gnat_error_node = gnat_object;
7836 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7837 but cannot be represented in bitsizetype. */
7838 size = UI_To_gnu (uint_size, bitsizetype);
7839 if (TREE_OVERFLOW (size))
7841 if (component_p)
7842 post_error_ne ("component size for& is too large", gnat_error_node,
7843 gnat_object);
7844 else
7845 post_error_ne ("size for& is too large", gnat_error_node,
7846 gnat_object);
7847 return NULL_TREE;
7850 /* Ignore a zero size if it is not permitted. */
7851 if (!zero_ok && integer_zerop (size))
7852 return NULL_TREE;
7854 /* The size of objects is always a multiple of a byte. */
7855 if (kind == VAR_DECL
7856 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7858 if (component_p)
7859 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7860 gnat_error_node, gnat_object);
7861 else
7862 post_error_ne ("size for& is not a multiple of Storage_Unit",
7863 gnat_error_node, gnat_object);
7864 return NULL_TREE;
7867 /* If this is an integral type or a packed array type, the front-end has
7868 already verified the size, so we need not do it here (which would mean
7869 checking against the bounds). However, if this is an aliased object,
7870 it may not be smaller than the type of the object. */
7871 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7872 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7873 return size;
7875 /* If the object is a record that contains a template, add the size of the
7876 template to the specified size. */
7877 if (TREE_CODE (gnu_type) == RECORD_TYPE
7878 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7879 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7881 if (kind == VAR_DECL
7882 /* If a type needs strict alignment, a component of this type in
7883 a packed record cannot be packed and thus uses the type size. */
7884 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7885 type_size = TYPE_SIZE (gnu_type);
7886 else
7887 type_size = rm_size (gnu_type);
7889 /* Modify the size of a discriminated type to be the maximum size. */
7890 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7891 type_size = max_size (type_size, true);
7893 /* If this is an access type or a fat pointer, the minimum size is that given
7894 by the smallest integral mode that's valid for pointers. */
7895 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7897 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7898 while (!targetm.valid_pointer_mode (p_mode))
7899 p_mode = GET_MODE_WIDER_MODE (p_mode);
7900 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7903 /* Issue an error either if the default size of the object isn't a constant
7904 or if the new size is smaller than it. */
7905 if (TREE_CODE (type_size) != INTEGER_CST
7906 || TREE_OVERFLOW (type_size)
7907 || tree_int_cst_lt (size, type_size))
7909 if (component_p)
7910 post_error_ne_tree
7911 ("component size for& too small{, minimum allowed is ^}",
7912 gnat_error_node, gnat_object, type_size);
7913 else
7914 post_error_ne_tree
7915 ("size for& too small{, minimum allowed is ^}",
7916 gnat_error_node, gnat_object, type_size);
7917 return NULL_TREE;
7920 return size;
7923 /* Similarly, but both validate and process a value of RM size. This routine
7924 is only called for types. */
7926 static void
7927 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7929 Node_Id gnat_attr_node;
7930 tree old_size, size;
7932 /* Do nothing if no size was specified. */
7933 if (uint_size == No_Uint)
7934 return;
7936 /* Ignore a negative size since that corresponds to our back-annotation. */
7937 if (UI_Lt (uint_size, Uint_0))
7938 return;
7940 /* Only issue an error if a Value_Size clause was explicitly given.
7941 Otherwise, we'd be duplicating an error on the Size clause. */
7942 gnat_attr_node
7943 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7945 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7946 but cannot be represented in bitsizetype. */
7947 size = UI_To_gnu (uint_size, bitsizetype);
7948 if (TREE_OVERFLOW (size))
7950 if (Present (gnat_attr_node))
7951 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7952 gnat_entity);
7953 return;
7956 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7957 exists, or this is an integer type, in which case the front-end will
7958 have always set it. */
7959 if (No (gnat_attr_node)
7960 && integer_zerop (size)
7961 && !Has_Size_Clause (gnat_entity)
7962 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7963 return;
7965 old_size = rm_size (gnu_type);
7967 /* If the old size is self-referential, get the maximum size. */
7968 if (CONTAINS_PLACEHOLDER_P (old_size))
7969 old_size = max_size (old_size, true);
7971 /* Issue an error either if the old size of the object isn't a constant or
7972 if the new size is smaller than it. The front-end has already verified
7973 this for scalar and packed array types. */
7974 if (TREE_CODE (old_size) != INTEGER_CST
7975 || TREE_OVERFLOW (old_size)
7976 || (AGGREGATE_TYPE_P (gnu_type)
7977 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7978 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7979 && !(TYPE_IS_PADDING_P (gnu_type)
7980 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7981 && TYPE_PACKED_ARRAY_TYPE_P
7982 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7983 && tree_int_cst_lt (size, old_size)))
7985 if (Present (gnat_attr_node))
7986 post_error_ne_tree
7987 ("Value_Size for& too small{, minimum allowed is ^}",
7988 gnat_attr_node, gnat_entity, old_size);
7989 return;
7992 /* Otherwise, set the RM size proper for integral types... */
7993 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7994 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7995 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7996 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7997 SET_TYPE_RM_SIZE (gnu_type, size);
7999 /* ...or the Ada size for record and union types. */
8000 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8001 && !TYPE_FAT_POINTER_P (gnu_type))
8002 SET_TYPE_ADA_SIZE (gnu_type, size);
8005 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8006 a type or object whose present alignment is ALIGN. If this alignment is
8007 valid, return it. Otherwise, give an error and return ALIGN. */
8009 static unsigned int
8010 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8012 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8013 unsigned int new_align;
8014 Node_Id gnat_error_node;
8016 /* Don't worry about checking alignment if alignment was not specified
8017 by the source program and we already posted an error for this entity. */
8018 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8019 return align;
8021 /* Post the error on the alignment clause if any. Note, for the implicit
8022 base type of an array type, the alignment clause is on the first
8023 subtype. */
8024 if (Present (Alignment_Clause (gnat_entity)))
8025 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8027 else if (Is_Itype (gnat_entity)
8028 && Is_Array_Type (gnat_entity)
8029 && Etype (gnat_entity) == gnat_entity
8030 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8031 gnat_error_node =
8032 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8034 else
8035 gnat_error_node = gnat_entity;
8037 /* Within GCC, an alignment is an integer, so we must make sure a value is
8038 specified that fits in that range. Also, there is an upper bound to
8039 alignments we can support/allow. */
8040 if (!UI_Is_In_Int_Range (alignment)
8041 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8042 post_error_ne_num ("largest supported alignment for& is ^",
8043 gnat_error_node, gnat_entity, max_allowed_alignment);
8044 else if (!(Present (Alignment_Clause (gnat_entity))
8045 && From_At_Mod (Alignment_Clause (gnat_entity)))
8046 && new_align * BITS_PER_UNIT < align)
8048 unsigned int double_align;
8049 bool is_capped_double, align_clause;
8051 /* If the default alignment of "double" or larger scalar types is
8052 specifically capped and the new alignment is above the cap, do
8053 not post an error and change the alignment only if there is an
8054 alignment clause; this makes it possible to have the associated
8055 GCC type overaligned by default for performance reasons. */
8056 if ((double_align = double_float_alignment) > 0)
8058 Entity_Id gnat_type
8059 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8060 is_capped_double
8061 = is_double_float_or_array (gnat_type, &align_clause);
8063 else if ((double_align = double_scalar_alignment) > 0)
8065 Entity_Id gnat_type
8066 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8067 is_capped_double
8068 = is_double_scalar_or_array (gnat_type, &align_clause);
8070 else
8071 is_capped_double = align_clause = false;
8073 if (is_capped_double && new_align >= double_align)
8075 if (align_clause)
8076 align = new_align * BITS_PER_UNIT;
8078 else
8080 if (is_capped_double)
8081 align = double_align * BITS_PER_UNIT;
8083 post_error_ne_num ("alignment for& must be at least ^",
8084 gnat_error_node, gnat_entity,
8085 align / BITS_PER_UNIT);
8088 else
8090 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8091 if (new_align > align)
8092 align = new_align;
8095 return align;
8098 /* Verify that TYPE is something we can implement atomically. If not, issue
8099 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8100 process a component type. */
8102 static void
8103 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8105 Node_Id gnat_error_point = gnat_entity;
8106 Node_Id gnat_node;
8107 machine_mode mode;
8108 enum mode_class mclass;
8109 unsigned int align;
8110 tree size;
8112 /* If this is an anonymous base type, nothing to check, the error will be
8113 reported on the source type if need be. */
8114 if (!Comes_From_Source (gnat_entity))
8115 return;
8117 mode = TYPE_MODE (type);
8118 mclass = GET_MODE_CLASS (mode);
8119 align = TYPE_ALIGN (type);
8120 size = TYPE_SIZE (type);
8122 /* Consider all aligned floating-point types atomic and any aligned types
8123 that are represented by integers no wider than a machine word. */
8124 if ((mclass == MODE_FLOAT
8125 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8126 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8127 && align >= GET_MODE_ALIGNMENT (mode))
8128 return;
8130 /* For the moment, also allow anything that has an alignment equal to its
8131 size and which is smaller than a word. */
8132 if (size
8133 && TREE_CODE (size) == INTEGER_CST
8134 && compare_tree_int (size, align) == 0
8135 && align <= BITS_PER_WORD)
8136 return;
8138 for (gnat_node = First_Rep_Item (gnat_entity);
8139 Present (gnat_node);
8140 gnat_node = Next_Rep_Item (gnat_node))
8141 if (Nkind (gnat_node) == N_Pragma)
8143 unsigned char pragma_id
8144 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8146 if ((pragma_id == Pragma_Atomic && !component_p)
8147 || (pragma_id == Pragma_Atomic_Components && component_p))
8149 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8150 break;
8154 if (component_p)
8155 post_error_ne ("atomic access to component of & cannot be guaranteed",
8156 gnat_error_point, gnat_entity);
8157 else
8158 post_error_ne ("atomic access to & cannot be guaranteed",
8159 gnat_error_point, gnat_entity);
8163 /* Helper for the intrin compatibility checks family. Evaluate whether
8164 two types are definitely incompatible. */
8166 static bool
8167 intrin_types_incompatible_p (tree t1, tree t2)
8169 enum tree_code code;
8171 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8172 return false;
8174 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8175 return true;
8177 if (TREE_CODE (t1) != TREE_CODE (t2))
8178 return true;
8180 code = TREE_CODE (t1);
8182 switch (code)
8184 case INTEGER_TYPE:
8185 case REAL_TYPE:
8186 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8188 case POINTER_TYPE:
8189 case REFERENCE_TYPE:
8190 /* Assume designated types are ok. We'd need to account for char * and
8191 void * variants to do better, which could rapidly get messy and isn't
8192 clearly worth the effort. */
8193 return false;
8195 default:
8196 break;
8199 return false;
8202 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8203 on the Ada/builtin argument lists for the INB binding. */
8205 static bool
8206 intrin_arglists_compatible_p (intrin_binding_t * inb)
8208 function_args_iterator ada_iter, btin_iter;
8210 function_args_iter_init (&ada_iter, inb->ada_fntype);
8211 function_args_iter_init (&btin_iter, inb->btin_fntype);
8213 /* Sequence position of the last argument we checked. */
8214 int argpos = 0;
8216 while (1)
8218 tree ada_type = function_args_iter_cond (&ada_iter);
8219 tree btin_type = function_args_iter_cond (&btin_iter);
8221 /* If we've exhausted both lists simultaneously, we're done. */
8222 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8223 break;
8225 /* If one list is shorter than the other, they fail to match. */
8226 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8227 return false;
8229 /* If we're done with the Ada args and not with the internal builtin
8230 args, or the other way around, complain. */
8231 if (ada_type == void_type_node
8232 && btin_type != void_type_node)
8234 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8235 return false;
8238 if (btin_type == void_type_node
8239 && ada_type != void_type_node)
8241 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8242 inb->gnat_entity, inb->gnat_entity, argpos);
8243 return false;
8246 /* Otherwise, check that types match for the current argument. */
8247 argpos ++;
8248 if (intrin_types_incompatible_p (ada_type, btin_type))
8250 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8251 inb->gnat_entity, inb->gnat_entity, argpos);
8252 return false;
8256 function_args_iter_next (&ada_iter);
8257 function_args_iter_next (&btin_iter);
8260 return true;
8263 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8264 on the Ada/builtin return values for the INB binding. */
8266 static bool
8267 intrin_return_compatible_p (intrin_binding_t * inb)
8269 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8270 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8272 /* Accept function imported as procedure, common and convenient. */
8273 if (VOID_TYPE_P (ada_return_type)
8274 && !VOID_TYPE_P (btin_return_type))
8275 return true;
8277 /* If return type is Address (integer type), map it to void *. */
8278 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8279 ada_return_type = ptr_void_type_node;
8281 /* Check return types compatibility otherwise. Note that this
8282 handles void/void as well. */
8283 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8285 post_error ("?intrinsic binding type mismatch on return value!",
8286 inb->gnat_entity);
8287 return false;
8290 return true;
8293 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8294 compatible. Issue relevant warnings when they are not.
8296 This is intended as a light check to diagnose the most obvious cases, not
8297 as a full fledged type compatibility predicate. It is the programmer's
8298 responsibility to ensure correctness of the Ada declarations in Imports,
8299 especially when binding straight to a compiler internal. */
8301 static bool
8302 intrin_profiles_compatible_p (intrin_binding_t * inb)
8304 /* Check compatibility on return values and argument lists, each responsible
8305 for posting warnings as appropriate. Ensure use of the proper sloc for
8306 this purpose. */
8308 bool arglists_compatible_p, return_compatible_p;
8309 location_t saved_location = input_location;
8311 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8313 return_compatible_p = intrin_return_compatible_p (inb);
8314 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8316 input_location = saved_location;
8318 return return_compatible_p && arglists_compatible_p;
8321 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8322 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8323 specified size for this field. POS_LIST is a position list describing
8324 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8325 to this layout. */
8327 static tree
8328 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8329 tree size, tree pos_list,
8330 vec<subst_pair> subst_list)
8332 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8333 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8334 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8335 tree new_pos, new_field;
8336 unsigned int i;
8337 subst_pair *s;
8339 if (CONTAINS_PLACEHOLDER_P (pos))
8340 FOR_EACH_VEC_ELT (subst_list, i, s)
8341 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8343 /* If the position is now a constant, we can set it as the position of the
8344 field when we make it. Otherwise, we need to deal with it specially. */
8345 if (TREE_CONSTANT (pos))
8346 new_pos = bit_from_pos (pos, bitpos);
8347 else
8348 new_pos = NULL_TREE;
8350 new_field
8351 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8352 size, new_pos, DECL_PACKED (old_field),
8353 !DECL_NONADDRESSABLE_P (old_field));
8355 if (!new_pos)
8357 normalize_offset (&pos, &bitpos, offset_align);
8358 /* Finalize the position. */
8359 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8360 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8361 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8362 DECL_SIZE (new_field) = size;
8363 DECL_SIZE_UNIT (new_field)
8364 = convert (sizetype,
8365 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8366 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8369 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8370 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8371 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8372 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8374 return new_field;
8377 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8378 it is the minimal size the REP_PART must have. */
8380 static tree
8381 create_rep_part (tree rep_type, tree record_type, tree min_size)
8383 tree field;
8385 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8386 min_size = NULL_TREE;
8388 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8389 min_size, NULL_TREE, 0, 1);
8390 DECL_INTERNAL_P (field) = 1;
8392 return field;
8395 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8397 static tree
8398 get_rep_part (tree record_type)
8400 tree field = TYPE_FIELDS (record_type);
8402 /* The REP part is the first field, internal, another record, and its name
8403 starts with an 'R'. */
8404 if (field
8405 && DECL_INTERNAL_P (field)
8406 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8407 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8408 return field;
8410 return NULL_TREE;
8413 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8415 tree
8416 get_variant_part (tree record_type)
8418 tree field;
8420 /* The variant part is the only internal field that is a qualified union. */
8421 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8422 if (DECL_INTERNAL_P (field)
8423 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8424 return field;
8426 return NULL_TREE;
8429 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8430 the list of variants to be used and RECORD_TYPE is the type of the parent.
8431 POS_LIST is a position list describing the layout of fields present in
8432 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8433 layout. */
8435 static tree
8436 create_variant_part_from (tree old_variant_part,
8437 vec<variant_desc> variant_list,
8438 tree record_type, tree pos_list,
8439 vec<subst_pair> subst_list)
8441 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8442 tree old_union_type = TREE_TYPE (old_variant_part);
8443 tree new_union_type, new_variant_part;
8444 tree union_field_list = NULL_TREE;
8445 variant_desc *v;
8446 unsigned int i;
8448 /* First create the type of the variant part from that of the old one. */
8449 new_union_type = make_node (QUAL_UNION_TYPE);
8450 TYPE_NAME (new_union_type)
8451 = concat_name (TYPE_NAME (record_type),
8452 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8454 /* If the position of the variant part is constant, subtract it from the
8455 size of the type of the parent to get the new size. This manual CSE
8456 reduces the code size when not optimizing. */
8457 if (TREE_CODE (offset) == INTEGER_CST)
8459 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8460 tree first_bit = bit_from_pos (offset, bitpos);
8461 TYPE_SIZE (new_union_type)
8462 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8463 TYPE_SIZE_UNIT (new_union_type)
8464 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8465 byte_from_pos (offset, bitpos));
8466 SET_TYPE_ADA_SIZE (new_union_type,
8467 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8468 first_bit));
8469 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8470 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8472 else
8473 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8475 /* Now finish up the new variants and populate the union type. */
8476 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8478 tree old_field = v->field, new_field;
8479 tree old_variant, old_variant_subpart, new_variant, field_list;
8481 /* Skip variants that don't belong to this nesting level. */
8482 if (DECL_CONTEXT (old_field) != old_union_type)
8483 continue;
8485 /* Retrieve the list of fields already added to the new variant. */
8486 new_variant = v->new_type;
8487 field_list = TYPE_FIELDS (new_variant);
8489 /* If the old variant had a variant subpart, we need to create a new
8490 variant subpart and add it to the field list. */
8491 old_variant = v->type;
8492 old_variant_subpart = get_variant_part (old_variant);
8493 if (old_variant_subpart)
8495 tree new_variant_subpart
8496 = create_variant_part_from (old_variant_subpart, variant_list,
8497 new_variant, pos_list, subst_list);
8498 DECL_CHAIN (new_variant_subpart) = field_list;
8499 field_list = new_variant_subpart;
8502 /* Finish up the new variant and create the field. No need for debug
8503 info thanks to the XVS type. */
8504 finish_record_type (new_variant, nreverse (field_list), 2, false);
8505 compute_record_mode (new_variant);
8506 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8507 Empty);
8509 new_field
8510 = create_field_decl_from (old_field, new_variant, new_union_type,
8511 TYPE_SIZE (new_variant),
8512 pos_list, subst_list);
8513 DECL_QUALIFIER (new_field) = v->qual;
8514 DECL_INTERNAL_P (new_field) = 1;
8515 DECL_CHAIN (new_field) = union_field_list;
8516 union_field_list = new_field;
8519 /* Finish up the union type and create the variant part. No need for debug
8520 info thanks to the XVS type. Note that we don't reverse the field list
8521 because VARIANT_LIST has been traversed in reverse order. */
8522 finish_record_type (new_union_type, union_field_list, 2, false);
8523 compute_record_mode (new_union_type);
8524 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8525 Empty);
8527 new_variant_part
8528 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8529 TYPE_SIZE (new_union_type),
8530 pos_list, subst_list);
8531 DECL_INTERNAL_P (new_variant_part) = 1;
8533 /* With multiple discriminants it is possible for an inner variant to be
8534 statically selected while outer ones are not; in this case, the list
8535 of fields of the inner variant is not flattened and we end up with a
8536 qualified union with a single member. Drop the useless container. */
8537 if (!DECL_CHAIN (union_field_list))
8539 DECL_CONTEXT (union_field_list) = record_type;
8540 DECL_FIELD_OFFSET (union_field_list)
8541 = DECL_FIELD_OFFSET (new_variant_part);
8542 DECL_FIELD_BIT_OFFSET (union_field_list)
8543 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8544 SET_DECL_OFFSET_ALIGN (union_field_list,
8545 DECL_OFFSET_ALIGN (new_variant_part));
8546 new_variant_part = union_field_list;
8549 return new_variant_part;
8552 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8553 which are both RECORD_TYPE, after applying the substitutions described
8554 in SUBST_LIST. */
8556 static void
8557 copy_and_substitute_in_size (tree new_type, tree old_type,
8558 vec<subst_pair> subst_list)
8560 unsigned int i;
8561 subst_pair *s;
8563 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8564 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8565 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8566 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8567 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8569 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8570 FOR_EACH_VEC_ELT (subst_list, i, s)
8571 TYPE_SIZE (new_type)
8572 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8573 s->discriminant, s->replacement);
8575 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8576 FOR_EACH_VEC_ELT (subst_list, i, s)
8577 TYPE_SIZE_UNIT (new_type)
8578 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8579 s->discriminant, s->replacement);
8581 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8582 FOR_EACH_VEC_ELT (subst_list, i, s)
8583 SET_TYPE_ADA_SIZE
8584 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8585 s->discriminant, s->replacement));
8587 /* Finalize the size. */
8588 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8589 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8592 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8593 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8594 The parallel type is the original array type if it has been translated. */
8596 static void
8597 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8599 Entity_Id gnat_original_array_type
8600 = Underlying_Type (Original_Array_Type (gnat_entity));
8601 tree gnu_original_array_type;
8603 if (!present_gnu_tree (gnat_original_array_type))
8604 return;
8606 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8608 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8609 return;
8611 add_parallel_type (gnu_type, gnu_original_array_type);
8614 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8615 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8616 updated by replacing F with R.
8618 The function doesn't update the layout of the type, i.e. it assumes
8619 that the substitution is purely formal. That's why the replacement
8620 value R must itself contain a PLACEHOLDER_EXPR. */
8622 tree
8623 substitute_in_type (tree t, tree f, tree r)
8625 tree nt;
8627 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8629 switch (TREE_CODE (t))
8631 case INTEGER_TYPE:
8632 case ENUMERAL_TYPE:
8633 case BOOLEAN_TYPE:
8634 case REAL_TYPE:
8636 /* First the domain types of arrays. */
8637 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8638 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8640 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8641 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8643 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8644 return t;
8646 nt = copy_type (t);
8647 TYPE_GCC_MIN_VALUE (nt) = low;
8648 TYPE_GCC_MAX_VALUE (nt) = high;
8650 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8651 SET_TYPE_INDEX_TYPE
8652 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8654 return nt;
8657 /* Then the subtypes. */
8658 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8659 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8661 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8662 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8664 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8665 return t;
8667 nt = copy_type (t);
8668 SET_TYPE_RM_MIN_VALUE (nt, low);
8669 SET_TYPE_RM_MAX_VALUE (nt, high);
8671 return nt;
8674 return t;
8676 case COMPLEX_TYPE:
8677 nt = substitute_in_type (TREE_TYPE (t), f, r);
8678 if (nt == TREE_TYPE (t))
8679 return t;
8681 return build_complex_type (nt);
8683 case FUNCTION_TYPE:
8684 /* These should never show up here. */
8685 gcc_unreachable ();
8687 case ARRAY_TYPE:
8689 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8690 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8692 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8693 return t;
8695 nt = build_nonshared_array_type (component, domain);
8696 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8697 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8698 SET_TYPE_MODE (nt, TYPE_MODE (t));
8699 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8700 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8701 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8702 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8703 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8704 return nt;
8707 case RECORD_TYPE:
8708 case UNION_TYPE:
8709 case QUAL_UNION_TYPE:
8711 bool changed_field = false;
8712 tree field;
8714 /* Start out with no fields, make new fields, and chain them
8715 in. If we haven't actually changed the type of any field,
8716 discard everything we've done and return the old type. */
8717 nt = copy_type (t);
8718 TYPE_FIELDS (nt) = NULL_TREE;
8720 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8722 tree new_field = copy_node (field), new_n;
8724 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8725 if (new_n != TREE_TYPE (field))
8727 TREE_TYPE (new_field) = new_n;
8728 changed_field = true;
8731 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8732 if (new_n != DECL_FIELD_OFFSET (field))
8734 DECL_FIELD_OFFSET (new_field) = new_n;
8735 changed_field = true;
8738 /* Do the substitution inside the qualifier, if any. */
8739 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8741 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8742 if (new_n != DECL_QUALIFIER (field))
8744 DECL_QUALIFIER (new_field) = new_n;
8745 changed_field = true;
8749 DECL_CONTEXT (new_field) = nt;
8750 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8752 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8753 TYPE_FIELDS (nt) = new_field;
8756 if (!changed_field)
8757 return t;
8759 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8760 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8761 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8762 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8763 return nt;
8766 default:
8767 return t;
8771 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8772 needed to represent the object. */
8774 tree
8775 rm_size (tree gnu_type)
8777 /* For integral types, we store the RM size explicitly. */
8778 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8779 return TYPE_RM_SIZE (gnu_type);
8781 /* Return the RM size of the actual data plus the size of the template. */
8782 if (TREE_CODE (gnu_type) == RECORD_TYPE
8783 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8784 return
8785 size_binop (PLUS_EXPR,
8786 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8787 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8789 /* For record or union types, we store the size explicitly. */
8790 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8791 && !TYPE_FAT_POINTER_P (gnu_type)
8792 && TYPE_ADA_SIZE (gnu_type))
8793 return TYPE_ADA_SIZE (gnu_type);
8795 /* For other types, this is just the size. */
8796 return TYPE_SIZE (gnu_type);
8799 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8800 fully-qualified name, possibly with type information encoding.
8801 Otherwise, return the name. */
8803 tree
8804 get_entity_name (Entity_Id gnat_entity)
8806 Get_Encoded_Name (gnat_entity);
8807 return get_identifier_with_length (Name_Buffer, Name_Len);
8810 /* Return an identifier representing the external name to be used for
8811 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8812 and the specified suffix. */
8814 tree
8815 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8817 const Entity_Kind kind = Ekind (gnat_entity);
8818 const bool has_suffix = (suffix != NULL);
8819 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8820 String_Pointer sp = {suffix, &temp};
8822 Get_External_Name (gnat_entity, has_suffix, sp);
8824 /* A variable using the Stdcall convention lives in a DLL. We adjust
8825 its name to use the jump table, the _imp__NAME contains the address
8826 for the NAME variable. */
8827 if ((kind == E_Variable || kind == E_Constant)
8828 && Has_Stdcall_Convention (gnat_entity))
8830 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8831 char *new_name = (char *) alloca (len + 1);
8832 strcpy (new_name, STDCALL_PREFIX);
8833 strcat (new_name, Name_Buffer);
8834 return get_identifier_with_length (new_name, len);
8837 return get_identifier_with_length (Name_Buffer, Name_Len);
8840 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8841 string, return a new IDENTIFIER_NODE that is the concatenation of
8842 the name followed by "___" and the specified suffix. */
8844 tree
8845 concat_name (tree gnu_name, const char *suffix)
8847 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8848 char *new_name = (char *) alloca (len + 1);
8849 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8850 strcat (new_name, "___");
8851 strcat (new_name, suffix);
8852 return get_identifier_with_length (new_name, len);
8855 /* Initialize data structures of the decl.c module. */
8857 void
8858 init_gnat_decl (void)
8860 /* Initialize the cache of annotated values. */
8861 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8864 /* Destroy data structures of the decl.c module. */
8866 void
8867 destroy_gnat_decl (void)
8869 /* Destroy the cache of annotated values. */
8870 annotate_value_cache->empty ();
8871 annotate_value_cache = NULL;
8874 #include "gt-ada-decl.h"