PR ada/66167
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobc1ef4b8c61028fa09ab5ffbb6c8467a2a4589aee
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2015, 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 "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "stringpool.h"
42 #include "stor-layout.h"
43 #include "flags.h"
44 #include "toplev.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "tree-inline.h"
48 #include "diagnostic-core.h"
50 #include "ada.h"
51 #include "types.h"
52 #include "atree.h"
53 #include "elists.h"
54 #include "namet.h"
55 #include "nlists.h"
56 #include "repinfo.h"
57 #include "snames.h"
58 #include "stringt.h"
59 #include "uintp.h"
60 #include "fe.h"
61 #include "sinfo.h"
62 #include "einfo.h"
63 #include "ada-tree.h"
64 #include "gigi.h"
66 /* "stdcall" and "thiscall" conventions should be processed in a specific way
67 on 32-bit x86/Windows only. The macros below are helpers to avoid having
68 to check for a Windows specific attribute throughout this unit. */
70 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
71 #ifdef TARGET_64BIT
72 #define Has_Stdcall_Convention(E) \
73 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
74 #define Has_Thiscall_Convention(E) \
75 (!TARGET_64BIT && is_cplusplus_method (E))
76 #else
77 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
78 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
79 #endif
80 #else
81 #define Has_Stdcall_Convention(E) 0
82 #define Has_Thiscall_Convention(E) 0
83 #endif
85 #define STDCALL_PREFIX "_imp__"
87 /* Stack realignment is necessary for functions with foreign conventions when
88 the ABI doesn't mandate as much as what the compiler assumes - that is, up
89 to PREFERRED_STACK_BOUNDARY.
91 Such realignment can be requested with a dedicated function type attribute
92 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
93 characterize the situations where the attribute should be set. We rely on
94 compiler configuration settings for 'main' to decide. */
96 #ifdef MAIN_STACK_BOUNDARY
97 #define FOREIGN_FORCE_REALIGN_STACK \
98 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
99 #else
100 #define FOREIGN_FORCE_REALIGN_STACK 0
101 #endif
103 struct incomplete
105 struct incomplete *next;
106 tree old_type;
107 Entity_Id full_type;
110 /* These variables are used to defer recursively expanding incomplete types
111 while we are processing an array, a record or a subprogram type. */
112 static int defer_incomplete_level = 0;
113 static struct incomplete *defer_incomplete_list;
115 /* This variable is used to delay expanding From_Limited_With types until the
116 end of the spec. */
117 static struct incomplete *defer_limited_with;
119 typedef struct subst_pair_d {
120 tree discriminant;
121 tree replacement;
122 } subst_pair;
125 typedef struct variant_desc_d {
126 /* The type of the variant. */
127 tree type;
129 /* The associated field. */
130 tree field;
132 /* The value of the qualifier. */
133 tree qual;
135 /* The type of the variant after transformation. */
136 tree new_type;
137 } variant_desc;
140 /* A hash table used to cache the result of annotate_value. */
142 struct value_annotation_hasher : ggc_cache_hasher<tree_int_map *>
144 static inline hashval_t
145 hash (tree_int_map *m)
147 return htab_hash_pointer (m->base.from);
150 static inline bool
151 equal (tree_int_map *a, tree_int_map *b)
153 return a->base.from == b->base.from;
156 static void
157 handle_cache_entry (tree_int_map *&m)
159 extern void gt_ggc_mx (tree_int_map *&);
160 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
161 return;
162 else if (ggc_marked_p (m->base.from))
163 gt_ggc_mx (m);
164 else
165 m = static_cast<tree_int_map *> (HTAB_DELETED_ENTRY);
169 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
171 static bool allocatable_size_p (tree, bool);
172 static void prepend_one_attribute (struct attrib **,
173 enum attr_type, tree, tree, Node_Id);
174 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
175 static void prepend_attributes (struct attrib **, Entity_Id);
176 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
177 static bool type_has_variable_size (tree);
178 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
179 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
180 unsigned int);
181 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
182 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
183 bool *);
184 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
185 static bool is_from_limited_with_of_main (Entity_Id);
186 static tree change_qualified_type (tree, int);
187 static bool same_discriminant_p (Entity_Id, Entity_Id);
188 static bool array_type_has_nonaliased_component (tree, Entity_Id);
189 static bool compile_time_known_address_p (Node_Id);
190 static bool cannot_be_superflat_p (Node_Id);
191 static bool constructor_address_p (tree);
192 static int compare_field_bitpos (const PTR, const PTR);
193 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
194 bool, bool, bool, bool, bool, tree, tree *);
195 static Uint annotate_value (tree);
196 static void annotate_rep (Entity_Id, tree);
197 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
198 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
199 static vec<variant_desc> build_variant_list (tree,
200 vec<subst_pair> ,
201 vec<variant_desc> );
202 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
203 static void set_rm_size (Uint, tree, Entity_Id);
204 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
205 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
206 static tree create_field_decl_from (tree, tree, tree, tree, tree,
207 vec<subst_pair> );
208 static tree create_rep_part (tree, tree, tree);
209 static tree get_rep_part (tree);
210 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
211 tree, vec<subst_pair> );
212 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
213 static void add_parallel_type_for_packed_array (tree, Entity_Id);
215 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
216 to pass around calls performing profile compatibility checks. */
218 typedef struct {
219 Entity_Id gnat_entity; /* The Ada subprogram entity. */
220 tree ada_fntype; /* The corresponding GCC type node. */
221 tree btin_fntype; /* The GCC builtin function type node. */
222 } intrin_binding_t;
224 static bool intrin_profiles_compatible_p (intrin_binding_t *);
226 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
227 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
228 and associate the ..._DECL node with the input GNAT defining identifier.
230 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
231 initial value (in GCC tree form). This is optional for a variable. For
232 a renamed entity, GNU_EXPR gives the object being renamed.
234 DEFINITION is nonzero if this call is intended for a definition. This is
235 used for separate compilation where it is necessary to know whether an
236 external declaration or a definition must be created if the GCC equivalent
237 was not created previously. The value of 1 is normally used for a nonzero
238 DEFINITION, but a value of 2 is used in special circumstances, defined in
239 the code. */
241 tree
242 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
244 /* Contains the kind of the input GNAT node. */
245 const Entity_Kind kind = Ekind (gnat_entity);
246 /* True if this is a type. */
247 const bool is_type = IN (kind, Type_Kind);
248 /* True if debug info is requested for this entity. */
249 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
250 /* True if this entity is to be considered as imported. */
251 const bool imported_p
252 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
253 /* For a type, contains the equivalent GNAT node to be used in gigi. */
254 Entity_Id gnat_equiv_type = Empty;
255 /* Temporary used to walk the GNAT tree. */
256 Entity_Id gnat_temp;
257 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
258 This node will be associated with the GNAT node by calling at the end
259 of the `switch' statement. */
260 tree gnu_decl = NULL_TREE;
261 /* Contains the GCC type to be used for the GCC node. */
262 tree gnu_type = NULL_TREE;
263 /* Contains the GCC size tree to be used for the GCC node. */
264 tree gnu_size = NULL_TREE;
265 /* Contains the GCC name to be used for the GCC node. */
266 tree gnu_entity_name;
267 /* True if we have already saved gnu_decl as a GNAT association. */
268 bool saved = false;
269 /* True if we incremented defer_incomplete_level. */
270 bool this_deferred = false;
271 /* True if we incremented force_global. */
272 bool this_global = false;
273 /* True if we should check to see if elaborated during processing. */
274 bool maybe_present = false;
275 /* True if we made GNU_DECL and its type here. */
276 bool this_made_decl = false;
277 /* Size and alignment of the GCC node, if meaningful. */
278 unsigned int esize = 0, align = 0;
279 /* Contains the list of attributes directly attached to the entity. */
280 struct attrib *attr_list = NULL;
282 /* Since a use of an Itype is a definition, process it as such if it
283 is not in a with'ed unit. */
284 if (!definition
285 && is_type
286 && Is_Itype (gnat_entity)
287 && !present_gnu_tree (gnat_entity)
288 && In_Extended_Main_Code_Unit (gnat_entity))
290 /* Ensure that we are in a subprogram mentioned in the Scope chain of
291 this entity, our current scope is global, or we encountered a task
292 or entry (where we can't currently accurately check scoping). */
293 if (!current_function_decl
294 || DECL_ELABORATION_PROC_P (current_function_decl))
296 process_type (gnat_entity);
297 return get_gnu_tree (gnat_entity);
300 for (gnat_temp = Scope (gnat_entity);
301 Present (gnat_temp);
302 gnat_temp = Scope (gnat_temp))
304 if (Is_Type (gnat_temp))
305 gnat_temp = Underlying_Type (gnat_temp);
307 if (Ekind (gnat_temp) == E_Subprogram_Body)
308 gnat_temp
309 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
311 if (IN (Ekind (gnat_temp), Subprogram_Kind)
312 && Present (Protected_Body_Subprogram (gnat_temp)))
313 gnat_temp = Protected_Body_Subprogram (gnat_temp);
315 if (Ekind (gnat_temp) == E_Entry
316 || Ekind (gnat_temp) == E_Entry_Family
317 || Ekind (gnat_temp) == E_Task_Type
318 || (IN (Ekind (gnat_temp), Subprogram_Kind)
319 && present_gnu_tree (gnat_temp)
320 && (current_function_decl
321 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
323 process_type (gnat_entity);
324 return get_gnu_tree (gnat_entity);
328 /* This abort means the Itype has an incorrect scope, i.e. that its
329 scope does not correspond to the subprogram it is declared in. */
330 gcc_unreachable ();
333 /* If we've already processed this entity, return what we got last time.
334 If we are defining the node, we should not have already processed it.
335 In that case, we will abort below when we try to save a new GCC tree
336 for this object. We also need to handle the case of getting a dummy
337 type when a Full_View exists but be careful so as not to trigger its
338 premature elaboration. */
339 if ((!definition || (is_type && imported_p))
340 && present_gnu_tree (gnat_entity))
342 gnu_decl = get_gnu_tree (gnat_entity);
344 if (TREE_CODE (gnu_decl) == TYPE_DECL
345 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
346 && IN (kind, Incomplete_Or_Private_Kind)
347 && Present (Full_View (gnat_entity))
348 && (present_gnu_tree (Full_View (gnat_entity))
349 || No (Freeze_Node (Full_View (gnat_entity)))))
351 gnu_decl
352 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
353 save_gnu_tree (gnat_entity, NULL_TREE, false);
354 save_gnu_tree (gnat_entity, gnu_decl, false);
357 return gnu_decl;
360 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
361 must be specified unless it was specified by the programmer. Exceptions
362 are for access-to-protected-subprogram types and all access subtypes, as
363 another GNAT type is used to lay out the GCC type for them. */
364 gcc_assert (!Unknown_Esize (gnat_entity)
365 || Has_Size_Clause (gnat_entity)
366 || (!IN (kind, Numeric_Kind)
367 && !IN (kind, Enumeration_Kind)
368 && (!IN (kind, Access_Kind)
369 || kind == E_Access_Protected_Subprogram_Type
370 || kind == E_Anonymous_Access_Protected_Subprogram_Type
371 || kind == E_Access_Subtype
372 || type_annotate_only)));
374 /* The RM size must be specified for all discrete and fixed-point types. */
375 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
376 && Unknown_RM_Size (gnat_entity)));
378 /* If we get here, it means we have not yet done anything with this entity.
379 If we are not defining it, it must be a type or an entity that is defined
380 elsewhere or externally, otherwise we should have defined it already. */
381 gcc_assert (definition
382 || type_annotate_only
383 || is_type
384 || kind == E_Discriminant
385 || kind == E_Component
386 || kind == E_Label
387 || (kind == E_Constant && Present (Full_View (gnat_entity)))
388 || Is_Public (gnat_entity));
390 /* Get the name of the entity and set up the line number and filename of
391 the original definition for use in any decl we make. Make sure we do not
392 inherit another source location. */
393 gnu_entity_name = get_entity_name (gnat_entity);
394 if (Sloc (gnat_entity) != No_Location
395 && !renaming_from_generic_instantiation_p (gnat_entity))
396 Sloc_to_locus (Sloc (gnat_entity), &input_location);
398 /* For cases when we are not defining (i.e., we are referencing from
399 another compilation unit) public entities, show we are at global level
400 for the purpose of computing scopes. Don't do this for components or
401 discriminants since the relevant test is whether or not the record is
402 being defined. */
403 if (!definition
404 && kind != E_Component
405 && kind != E_Discriminant
406 && Is_Public (gnat_entity)
407 && !Is_Statically_Allocated (gnat_entity))
408 force_global++, this_global = true;
410 /* Handle any attributes directly attached to the entity. */
411 if (Has_Gigi_Rep_Item (gnat_entity))
412 prepend_attributes (&attr_list, gnat_entity);
414 /* Do some common processing for types. */
415 if (is_type)
417 /* Compute the equivalent type to be used in gigi. */
418 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
420 /* Machine_Attributes on types are expected to be propagated to
421 subtypes. The corresponding Gigi_Rep_Items are only attached
422 to the first subtype though, so we handle the propagation here. */
423 if (Base_Type (gnat_entity) != gnat_entity
424 && !Is_First_Subtype (gnat_entity)
425 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
426 prepend_attributes (&attr_list,
427 First_Subtype (Base_Type (gnat_entity)));
429 /* Compute a default value for the size of an elementary type. */
430 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
432 unsigned int max_esize;
434 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
435 esize = UI_To_Int (Esize (gnat_entity));
437 if (IN (kind, Float_Kind))
438 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
439 else if (IN (kind, Access_Kind))
440 max_esize = POINTER_SIZE * 2;
441 else
442 max_esize = LONG_LONG_TYPE_SIZE;
444 if (esize > max_esize)
445 esize = max_esize;
449 switch (kind)
451 case E_Constant:
452 /* If this is a use of a deferred constant without address clause,
453 get its full definition. */
454 if (!definition
455 && No (Address_Clause (gnat_entity))
456 && Present (Full_View (gnat_entity)))
458 gnu_decl
459 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
460 saved = true;
461 break;
464 /* If we have an external constant that we are not defining, get the
465 expression that is was defined to represent. We may throw it away
466 later if it is not a constant. But do not retrieve the expression
467 if it is an allocator because the designated type might be dummy
468 at this point. */
469 if (!definition
470 && !No_Initialization (Declaration_Node (gnat_entity))
471 && Present (Expression (Declaration_Node (gnat_entity)))
472 && Nkind (Expression (Declaration_Node (gnat_entity)))
473 != N_Allocator)
475 bool went_into_elab_proc = false;
476 int save_force_global = force_global;
478 /* The expression may contain N_Expression_With_Actions nodes and
479 thus object declarations from other units. In this case, even
480 though the expression will eventually be discarded since not a
481 constant, the declarations would be stuck either in the global
482 varpool or in the current scope. Therefore we force the local
483 context and create a fake scope that we'll zap at the end. */
484 if (!current_function_decl)
486 current_function_decl = get_elaboration_procedure ();
487 went_into_elab_proc = true;
489 force_global = 0;
490 gnat_pushlevel ();
492 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
494 gnat_zaplevel ();
495 force_global = save_force_global;
496 if (went_into_elab_proc)
497 current_function_decl = NULL_TREE;
500 /* Ignore deferred constant definitions without address clause since
501 they are processed fully in the front-end. If No_Initialization
502 is set, this is not a deferred constant but a constant whose value
503 is built manually. And constants that are renamings are handled
504 like variables. */
505 if (definition
506 && !gnu_expr
507 && No (Address_Clause (gnat_entity))
508 && !No_Initialization (Declaration_Node (gnat_entity))
509 && No (Renamed_Object (gnat_entity)))
511 gnu_decl = error_mark_node;
512 saved = true;
513 break;
516 /* Ignore constant definitions already marked with the error node. See
517 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
518 if (definition
519 && gnu_expr
520 && present_gnu_tree (gnat_entity)
521 && get_gnu_tree (gnat_entity) == error_mark_node)
523 maybe_present = true;
524 break;
527 goto object;
529 case E_Exception:
530 goto object;
532 case E_Component:
533 case E_Discriminant:
535 /* The GNAT record where the component was defined. */
536 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
538 /* If the entity is a discriminant of an extended tagged type used to
539 rename a discriminant of the parent type, return the latter. */
540 if (Is_Tagged_Type (gnat_record)
541 && Present (Corresponding_Discriminant (gnat_entity)))
543 gnu_decl
544 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
545 gnu_expr, definition);
546 saved = true;
547 break;
550 /* If the entity is an inherited component (in the case of extended
551 tagged record types), just return the original entity, which must
552 be a FIELD_DECL. Likewise for discriminants. If the entity is a
553 non-girder discriminant (in the case of derived untagged record
554 types), return the stored discriminant it renames. */
555 else if (Present (Original_Record_Component (gnat_entity))
556 && Original_Record_Component (gnat_entity) != gnat_entity)
558 gnu_decl
559 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
560 gnu_expr, definition);
561 saved = true;
562 break;
565 /* Otherwise, if we are not defining this and we have no GCC type
566 for the containing record, make one for it. Then we should
567 have made our own equivalent. */
568 else if (!definition && !present_gnu_tree (gnat_record))
570 /* ??? If this is in a record whose scope is a protected
571 type and we have an Original_Record_Component, use it.
572 This is a workaround for major problems in protected type
573 handling. */
574 Entity_Id Scop = Scope (Scope (gnat_entity));
575 if (Is_Protected_Type (Underlying_Type (Scop))
576 && Present (Original_Record_Component (gnat_entity)))
578 gnu_decl
579 = gnat_to_gnu_entity (Original_Record_Component
580 (gnat_entity),
581 gnu_expr, 0);
582 saved = true;
583 break;
586 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
587 gnu_decl = get_gnu_tree (gnat_entity);
588 saved = true;
589 break;
592 else
593 /* Here we have no GCC type and this is a reference rather than a
594 definition. This should never happen. Most likely the cause is
595 reference before declaration in the GNAT tree for gnat_entity. */
596 gcc_unreachable ();
599 case E_Loop_Parameter:
600 case E_Out_Parameter:
601 case E_Variable:
603 /* Simple variables, loop variables, Out parameters and exceptions. */
604 object:
606 /* Always create a variable for volatile objects and variables seen
607 constant but with a Linker_Section pragma. */
608 bool const_flag
609 = ((kind == E_Constant || kind == E_Variable)
610 && Is_True_Constant (gnat_entity)
611 && !(kind == E_Variable
612 && Present (Linker_Section_Pragma (gnat_entity)))
613 && !Treat_As_Volatile (gnat_entity)
614 && (((Nkind (Declaration_Node (gnat_entity))
615 == N_Object_Declaration)
616 && Present (Expression (Declaration_Node (gnat_entity))))
617 || Present (Renamed_Object (gnat_entity))
618 || imported_p));
619 bool inner_const_flag = const_flag;
620 bool static_p = Is_Statically_Allocated (gnat_entity);
621 bool mutable_p = false;
622 bool used_by_ref = false;
623 tree gnu_ext_name = NULL_TREE;
624 tree renamed_obj = NULL_TREE;
625 tree gnu_object_size;
627 if (Present (Renamed_Object (gnat_entity)) && !definition)
629 if (kind == E_Exception)
630 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
631 NULL_TREE, 0);
632 else
633 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
636 /* Get the type after elaborating the renamed object. */
637 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
639 /* If this is a standard exception definition, then use the standard
640 exception type. This is necessary to make sure that imported and
641 exported views of exceptions are properly merged in LTO mode. */
642 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
643 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
644 gnu_type = except_type_node;
646 /* For a debug renaming declaration, build a debug-only entity. */
647 if (Present (Debug_Renaming_Link (gnat_entity)))
649 /* Force a non-null value to make sure the symbol is retained. */
650 tree value = build1 (INDIRECT_REF, gnu_type,
651 build1 (NOP_EXPR,
652 build_pointer_type (gnu_type),
653 integer_minus_one_node));
654 gnu_decl = build_decl (input_location,
655 VAR_DECL, gnu_entity_name, gnu_type);
656 SET_DECL_VALUE_EXPR (gnu_decl, value);
657 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
658 gnat_pushdecl (gnu_decl, gnat_entity);
659 break;
662 /* If this is a loop variable, its type should be the base type.
663 This is because the code for processing a loop determines whether
664 a normal loop end test can be done by comparing the bounds of the
665 loop against those of the base type, which is presumed to be the
666 size used for computation. But this is not correct when the size
667 of the subtype is smaller than the type. */
668 if (kind == E_Loop_Parameter)
669 gnu_type = get_base_type (gnu_type);
671 /* Reject non-renamed objects whose type is an unconstrained array or
672 any object whose type is a dummy type or void. */
673 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
674 && No (Renamed_Object (gnat_entity)))
675 || TYPE_IS_DUMMY_P (gnu_type)
676 || TREE_CODE (gnu_type) == VOID_TYPE)
678 gcc_assert (type_annotate_only);
679 if (this_global)
680 force_global--;
681 return error_mark_node;
684 /* If an alignment is specified, use it if valid. Note that exceptions
685 are objects but don't have an alignment. We must do this before we
686 validate the size, since the alignment can affect the size. */
687 if (kind != E_Exception && Known_Alignment (gnat_entity))
689 gcc_assert (Present (Alignment (gnat_entity)));
691 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
692 TYPE_ALIGN (gnu_type));
694 /* No point in changing the type if there is an address clause
695 as the final type of the object will be a reference type. */
696 if (Present (Address_Clause (gnat_entity)))
697 align = 0;
698 else
700 tree orig_type = gnu_type;
702 gnu_type
703 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
704 false, false, definition, true);
706 /* If a padding record was made, declare it now since it will
707 never be declared otherwise. This is necessary to ensure
708 that its subtrees are properly marked. */
709 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
710 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
711 debug_info_p, gnat_entity);
715 /* If we are defining the object, see if it has a Size and validate it
716 if so. If we are not defining the object and a Size clause applies,
717 simply retrieve the value. We don't want to ignore the clause and
718 it is expected to have been validated already. Then get the new
719 type, if any. */
720 if (definition)
721 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
722 gnat_entity, VAR_DECL, false,
723 Has_Size_Clause (gnat_entity));
724 else if (Has_Size_Clause (gnat_entity))
725 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
727 if (gnu_size)
729 gnu_type
730 = make_type_from_size (gnu_type, gnu_size,
731 Has_Biased_Representation (gnat_entity));
733 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
734 gnu_size = NULL_TREE;
737 /* If this object has self-referential size, it must be a record with
738 a default discriminant. We are supposed to allocate an object of
739 the maximum size in this case, unless it is a constant with an
740 initializing expression, in which case we can get the size from
741 that. Note that the resulting size may still be a variable, so
742 this may end up with an indirect allocation. */
743 if (No (Renamed_Object (gnat_entity))
744 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
746 if (gnu_expr && kind == E_Constant)
748 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
749 if (CONTAINS_PLACEHOLDER_P (size))
751 /* If the initializing expression is itself a constant,
752 despite having a nominal type with self-referential
753 size, we can get the size directly from it. */
754 if (TREE_CODE (gnu_expr) == COMPONENT_REF
755 && TYPE_IS_PADDING_P
756 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
757 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
758 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
759 || DECL_READONLY_ONCE_ELAB
760 (TREE_OPERAND (gnu_expr, 0))))
761 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
762 else
763 gnu_size
764 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
766 else
767 gnu_size = size;
769 /* We may have no GNU_EXPR because No_Initialization is
770 set even though there's an Expression. */
771 else if (kind == E_Constant
772 && (Nkind (Declaration_Node (gnat_entity))
773 == N_Object_Declaration)
774 && Present (Expression (Declaration_Node (gnat_entity))))
775 gnu_size
776 = TYPE_SIZE (gnat_to_gnu_type
777 (Etype
778 (Expression (Declaration_Node (gnat_entity)))));
779 else
781 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
782 mutable_p = true;
785 /* If we are at global level and the size isn't constant, call
786 elaborate_expression_1 to make a variable for it rather than
787 calculating it each time. */
788 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
789 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
790 get_identifier ("SIZE"),
791 definition, false);
794 /* If the size is zero byte, make it one byte since some linkers have
795 troubles with zero-sized objects. If the object will have a
796 template, that will make it nonzero so don't bother. Also avoid
797 doing that for an object renaming or an object with an address
798 clause, as we would lose useful information on the view size
799 (e.g. for null array slices) and we are not allocating the object
800 here anyway. */
801 if (((gnu_size
802 && integer_zerop (gnu_size)
803 && !TREE_OVERFLOW (gnu_size))
804 || (TYPE_SIZE (gnu_type)
805 && integer_zerop (TYPE_SIZE (gnu_type))
806 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
807 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
808 && No (Renamed_Object (gnat_entity))
809 && No (Address_Clause (gnat_entity)))
810 gnu_size = bitsize_unit_node;
812 /* If this is an object with no specified size and alignment, and
813 if either it is atomic or we are not optimizing alignment for
814 space and it is composite and not an exception, an Out parameter
815 or a reference to another object, and the size of its type is a
816 constant, set the alignment to the smallest one which is not
817 smaller than the size, with an appropriate cap. */
818 if (!gnu_size && align == 0
819 && (Is_Atomic (gnat_entity)
820 || (!Optimize_Alignment_Space (gnat_entity)
821 && kind != E_Exception
822 && kind != E_Out_Parameter
823 && Is_Composite_Type (Etype (gnat_entity))
824 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
825 && !Is_Exported (gnat_entity)
826 && !imported_p
827 && No (Renamed_Object (gnat_entity))
828 && No (Address_Clause (gnat_entity))))
829 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
831 unsigned int size_cap, align_cap;
833 /* No point in promoting the alignment if this doesn't prevent
834 BLKmode access to the object, in particular block copy, as
835 this will for example disable the NRV optimization for it.
836 No point in jumping through all the hoops needed in order
837 to support BIGGEST_ALIGNMENT if we don't really have to.
838 So we cap to the smallest alignment that corresponds to
839 a known efficient memory access pattern of the target. */
840 if (Is_Atomic (gnat_entity))
842 size_cap = UINT_MAX;
843 align_cap = BIGGEST_ALIGNMENT;
845 else
847 size_cap = MAX_FIXED_MODE_SIZE;
848 align_cap = get_mode_alignment (ptr_mode);
851 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
852 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
853 align = 0;
854 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
855 align = align_cap;
856 else
857 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
859 /* But make sure not to under-align the object. */
860 if (align <= TYPE_ALIGN (gnu_type))
861 align = 0;
863 /* And honor the minimum valid atomic alignment, if any. */
864 #ifdef MINIMUM_ATOMIC_ALIGNMENT
865 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
866 align = MINIMUM_ATOMIC_ALIGNMENT;
867 #endif
870 /* If the object is set to have atomic components, find the component
871 type and validate it.
873 ??? Note that we ignore Has_Volatile_Components on objects; it's
874 not at all clear what to do in that case. */
875 if (Has_Atomic_Components (gnat_entity))
877 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
878 ? TREE_TYPE (gnu_type) : gnu_type);
880 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
881 && TYPE_MULTI_ARRAY_P (gnu_inner))
882 gnu_inner = TREE_TYPE (gnu_inner);
884 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
887 /* Now check if the type of the object allows atomic access. Note
888 that we must test the type, even if this object has size and
889 alignment to allow such access, because we will be going inside
890 the padded record to assign to the object. We could fix this by
891 always copying via an intermediate value, but it's not clear it's
892 worth the effort. */
893 if (Is_Atomic (gnat_entity))
894 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
896 /* If this is an aliased object with an unconstrained nominal subtype,
897 make a type that includes the template. */
898 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
899 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
900 && !type_annotate_only)
902 tree gnu_array
903 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
904 gnu_type
905 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
906 gnu_type,
907 concat_name (gnu_entity_name,
908 "UNC"),
909 debug_info_p);
912 /* ??? If this is an object of CW type initialized to a value, try to
913 ensure that the object is sufficient aligned for this value, but
914 without pessimizing the allocation. This is a kludge necessary
915 because we don't support dynamic alignment. */
916 if (align == 0
917 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
918 && No (Renamed_Object (gnat_entity))
919 && No (Address_Clause (gnat_entity)))
920 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
922 #ifdef MINIMUM_ATOMIC_ALIGNMENT
923 /* If the size is a constant and no alignment is specified, force
924 the alignment to be the minimum valid atomic alignment. The
925 restriction on constant size avoids problems with variable-size
926 temporaries; if the size is variable, there's no issue with
927 atomic access. Also don't do this for a constant, since it isn't
928 necessary and can interfere with constant replacement. Finally,
929 do not do it for Out parameters since that creates an
930 size inconsistency with In parameters. */
931 if (align == 0
932 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
933 && !FLOAT_TYPE_P (gnu_type)
934 && !const_flag && No (Renamed_Object (gnat_entity))
935 && !imported_p && No (Address_Clause (gnat_entity))
936 && kind != E_Out_Parameter
937 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
938 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
939 align = MINIMUM_ATOMIC_ALIGNMENT;
940 #endif
942 /* Make a new type with the desired size and alignment, if needed.
943 But do not take into account alignment promotions to compute the
944 size of the object. */
945 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
946 if (gnu_size || align > 0)
948 tree orig_type = gnu_type;
950 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
951 false, false, definition, true);
953 /* If a padding record was made, declare it now since it will
954 never be declared otherwise. This is necessary to ensure
955 that its subtrees are properly marked. */
956 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
957 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
958 debug_info_p, gnat_entity);
961 /* If this is a renaming, avoid as much as possible to create a new
962 object. However, in several cases, creating it is required.
963 This processing needs to be applied to the raw expression so
964 as to make it more likely to rename the underlying object. */
965 if (Present (Renamed_Object (gnat_entity)))
967 bool create_normal_object = false;
969 /* If the renamed object had padding, strip off the reference
970 to the inner object and reset our type. */
971 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
972 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
973 /* Strip useless conversions around the object. */
974 || gnat_useless_type_conversion (gnu_expr))
976 gnu_expr = TREE_OPERAND (gnu_expr, 0);
977 gnu_type = TREE_TYPE (gnu_expr);
980 /* Or else, if the renamed object has an unconstrained type with
981 default discriminant, use the padded type. */
982 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
983 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
984 == gnu_type
985 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
986 gnu_type = TREE_TYPE (gnu_expr);
988 /* Case 1: If this is a constant renaming stemming from a function
989 call, treat it as a normal object whose initial value is what is
990 being renamed. RM 3.3 says that the result of evaluating a
991 function call is a constant object. Treat constant literals
992 the same way. As a consequence, it can be the inner object of
993 a constant renaming. In this case, the renaming must be fully
994 instantiated, i.e. it cannot be a mere reference to (part of) an
995 existing object. */
996 if (const_flag)
998 tree inner_object = gnu_expr;
999 while (handled_component_p (inner_object))
1000 inner_object = TREE_OPERAND (inner_object, 0);
1001 if (TREE_CODE (inner_object) == CALL_EXPR
1002 || CONSTANT_CLASS_P (inner_object))
1003 create_normal_object = true;
1006 /* Otherwise, see if we can proceed with a stabilized version of
1007 the renamed entity or if we need to make a new object. */
1008 if (!create_normal_object)
1010 tree maybe_stable_expr = NULL_TREE;
1011 bool stable = false;
1013 /* Case 2: If the renaming entity need not be materialized and
1014 the renamed expression is something we can stabilize, use
1015 that for the renaming. At the global level, we can only do
1016 this if we know no SAVE_EXPRs need be made, because the
1017 expression we return might be used in arbitrary conditional
1018 branches so we must force the evaluation of the SAVE_EXPRs
1019 immediately and this requires a proper function context.
1020 Note that an external constant is at the global level. */
1021 if (!Materialize_Entity (gnat_entity)
1022 && (!((!definition && kind == E_Constant)
1023 || global_bindings_p ())
1024 || (staticp (gnu_expr)
1025 && !TREE_SIDE_EFFECTS (gnu_expr))))
1027 maybe_stable_expr
1028 = gnat_stabilize_reference (gnu_expr, true, &stable);
1030 if (stable)
1032 /* ??? No DECL_EXPR is created so we need to mark
1033 the expression manually lest it is shared. */
1034 if ((!definition && kind == E_Constant)
1035 || global_bindings_p ())
1036 MARK_VISITED (maybe_stable_expr);
1037 gnu_decl = maybe_stable_expr;
1038 save_gnu_tree (gnat_entity, gnu_decl, true);
1039 saved = true;
1040 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1041 false);
1042 /* This assertion will fail if the renamed object
1043 isn't aligned enough as to make it possible to
1044 honor the alignment set on the renaming. */
1045 if (align)
1047 unsigned int renamed_align
1048 = DECL_P (gnu_decl)
1049 ? DECL_ALIGN (gnu_decl)
1050 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1051 gcc_assert (renamed_align >= align);
1053 break;
1056 /* The stabilization failed. Keep maybe_stable_expr
1057 untouched here to let the pointer case below know
1058 about that failure. */
1061 /* Case 3: Make this into a constant pointer to the object we
1062 are to rename and attach the object to the pointer if it is
1063 something we can stabilize.
1065 From the proper scope, attached objects will be referenced
1066 directly instead of indirectly via the pointer to avoid
1067 subtle aliasing problems with non-addressable entities.
1068 They have to be stable because we must not evaluate the
1069 variables in the expression every time the renaming is used.
1070 The pointer is called a "renaming" pointer in this case.
1072 In the rare cases where we cannot stabilize the renamed
1073 object, we just make a "bare" pointer and the renamed
1074 object will always be accessed indirectly through it.
1076 Note that we need to preserve the volatility of the renamed
1077 object through the indirection. */
1078 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1079 gnu_type
1080 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1081 gnu_type = build_reference_type (gnu_type);
1082 inner_const_flag = TREE_READONLY (gnu_expr);
1083 const_flag = true;
1085 /* If the previous attempt at stabilizing failed, there is
1086 no point in trying again and we reuse the result without
1087 attaching it to the pointer. In this case it will only
1088 be used as the initializing expression of the pointer and
1089 thus needs no special treatment with regard to multiple
1090 evaluations.
1092 Otherwise, try to stabilize and attach the expression to
1093 the pointer if the stabilization succeeds.
1095 Note that this might introduce SAVE_EXPRs and we don't
1096 check whether we are at the global level or not. This
1097 is fine since we are building a pointer initializer and
1098 neither the pointer nor the initializing expression can
1099 be accessed before the pointer elaboration has taken
1100 place in a correct program.
1102 These SAVE_EXPRs will be evaluated at the right place
1103 by either the evaluation of the initializer for the
1104 non-global case or the elaboration code for the global
1105 case, and will be attached to the elaboration procedure
1106 in the latter case. */
1107 if (!maybe_stable_expr)
1109 maybe_stable_expr
1110 = gnat_stabilize_reference (gnu_expr, true, &stable);
1112 if (stable)
1113 renamed_obj = maybe_stable_expr;
1116 if (type_annotate_only
1117 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1118 gnu_expr = NULL_TREE;
1119 else
1120 gnu_expr
1121 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1123 gnu_size = NULL_TREE;
1124 used_by_ref = true;
1128 /* Make a volatile version of this object's type if we are to make
1129 the object volatile. We also interpret 13.3(19) conservatively
1130 and disallow any optimizations for such a non-constant object. */
1131 if ((Treat_As_Volatile (gnat_entity)
1132 || (!const_flag
1133 && gnu_type != except_type_node
1134 && (Is_Exported (gnat_entity)
1135 || imported_p
1136 || Present (Address_Clause (gnat_entity)))))
1137 && !TYPE_VOLATILE (gnu_type))
1138 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1140 /* If we are defining an aliased object whose nominal subtype is
1141 unconstrained, the object is a record that contains both the
1142 template and the object. If there is an initializer, it will
1143 have already been converted to the right type, but we need to
1144 create the template if there is no initializer. */
1145 if (definition
1146 && !gnu_expr
1147 && TREE_CODE (gnu_type) == RECORD_TYPE
1148 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1149 /* Beware that padding might have been introduced above. */
1150 || (TYPE_PADDING_P (gnu_type)
1151 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1152 == RECORD_TYPE
1153 && TYPE_CONTAINS_TEMPLATE_P
1154 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1156 tree template_field
1157 = TYPE_PADDING_P (gnu_type)
1158 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1159 : TYPE_FIELDS (gnu_type);
1160 vec<constructor_elt, va_gc> *v;
1161 vec_alloc (v, 1);
1162 tree t = build_template (TREE_TYPE (template_field),
1163 TREE_TYPE (DECL_CHAIN (template_field)),
1164 NULL_TREE);
1165 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1166 gnu_expr = gnat_build_constructor (gnu_type, v);
1169 /* Convert the expression to the type of the object except in the
1170 case where the object's type is unconstrained or the object's type
1171 is a padded record whose field is of self-referential size. In
1172 the former case, converting will generate unnecessary evaluations
1173 of the CONSTRUCTOR to compute the size and in the latter case, we
1174 want to only copy the actual data. Also don't convert to a record
1175 type with a variant part from a record type without one, to keep
1176 the object simpler. */
1177 if (gnu_expr
1178 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1179 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1180 && !(TYPE_IS_PADDING_P (gnu_type)
1181 && CONTAINS_PLACEHOLDER_P
1182 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1183 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1184 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1185 && get_variant_part (gnu_type) != NULL_TREE
1186 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1187 gnu_expr = convert (gnu_type, gnu_expr);
1189 /* If this is a pointer that doesn't have an initializing expression,
1190 initialize it to NULL, unless the object is imported. */
1191 if (definition
1192 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1193 && !gnu_expr
1194 && !Is_Imported (gnat_entity))
1195 gnu_expr = integer_zero_node;
1197 /* If we are defining the object and it has an Address clause, we must
1198 either get the address expression from the saved GCC tree for the
1199 object if it has a Freeze node, or elaborate the address expression
1200 here since the front-end has guaranteed that the elaboration has no
1201 effects in this case. */
1202 if (definition && Present (Address_Clause (gnat_entity)))
1204 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1205 tree gnu_address
1206 = present_gnu_tree (gnat_entity)
1207 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1209 save_gnu_tree (gnat_entity, NULL_TREE, false);
1211 /* Ignore the size. It's either meaningless or was handled
1212 above. */
1213 gnu_size = NULL_TREE;
1214 /* Convert the type of the object to a reference type that can
1215 alias everything as per 13.3(19). */
1216 gnu_type
1217 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1218 gnu_address = convert (gnu_type, gnu_address);
1219 used_by_ref = true;
1220 const_flag
1221 = !Is_Public (gnat_entity)
1222 || compile_time_known_address_p (gnat_expr);
1224 /* If this is a deferred constant, the initializer is attached to
1225 the full view. */
1226 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1227 gnu_expr
1228 = gnat_to_gnu
1229 (Expression (Declaration_Node (Full_View (gnat_entity))));
1231 /* If we don't have an initializing expression for the underlying
1232 variable, the initializing expression for the pointer is the
1233 specified address. Otherwise, we have to make a COMPOUND_EXPR
1234 to assign both the address and the initial value. */
1235 if (!gnu_expr)
1236 gnu_expr = gnu_address;
1237 else
1238 gnu_expr
1239 = build2 (COMPOUND_EXPR, gnu_type,
1240 build_binary_op
1241 (MODIFY_EXPR, NULL_TREE,
1242 build_unary_op (INDIRECT_REF, NULL_TREE,
1243 gnu_address),
1244 gnu_expr),
1245 gnu_address);
1248 /* If it has an address clause and we are not defining it, mark it
1249 as an indirect object. Likewise for Stdcall objects that are
1250 imported. */
1251 if ((!definition && Present (Address_Clause (gnat_entity)))
1252 || (Is_Imported (gnat_entity)
1253 && Has_Stdcall_Convention (gnat_entity)))
1255 /* Convert the type of the object to a reference type that can
1256 alias everything as per 13.3(19). */
1257 gnu_type
1258 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1259 gnu_size = NULL_TREE;
1261 /* No point in taking the address of an initializing expression
1262 that isn't going to be used. */
1263 gnu_expr = NULL_TREE;
1265 /* If it has an address clause whose value is known at compile
1266 time, make the object a CONST_DECL. This will avoid a
1267 useless dereference. */
1268 if (Present (Address_Clause (gnat_entity)))
1270 Node_Id gnat_address
1271 = Expression (Address_Clause (gnat_entity));
1273 if (compile_time_known_address_p (gnat_address))
1275 gnu_expr = gnat_to_gnu (gnat_address);
1276 const_flag = true;
1280 used_by_ref = true;
1283 /* If we are at top level and this object is of variable size,
1284 make the actual type a hidden pointer to the real type and
1285 make the initializer be a memory allocation and initialization.
1286 Likewise for objects we aren't defining (presumed to be
1287 external references from other packages), but there we do
1288 not set up an initialization.
1290 If the object's size overflows, make an allocator too, so that
1291 Storage_Error gets raised. Note that we will never free
1292 such memory, so we presume it never will get allocated. */
1293 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1294 global_bindings_p ()
1295 || !definition
1296 || static_p)
1297 || (gnu_size
1298 && !allocatable_size_p (convert (sizetype,
1299 size_binop
1300 (CEIL_DIV_EXPR, gnu_size,
1301 bitsize_unit_node)),
1302 global_bindings_p ()
1303 || !definition
1304 || static_p)))
1306 gnu_type = build_reference_type (gnu_type);
1307 gnu_size = NULL_TREE;
1308 used_by_ref = true;
1310 /* In case this was a aliased object whose nominal subtype is
1311 unconstrained, the pointer above will be a thin pointer and
1312 build_allocator will automatically make the template.
1314 If we have a template initializer only (that we made above),
1315 pretend there is none and rely on what build_allocator creates
1316 again anyway. Otherwise (if we have a full initializer), get
1317 the data part and feed that to build_allocator.
1319 If we are elaborating a mutable object, tell build_allocator to
1320 ignore a possibly simpler size from the initializer, if any, as
1321 we must allocate the maximum possible size in this case. */
1322 if (definition && !imported_p)
1324 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1326 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1327 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1329 gnu_alloc_type
1330 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1332 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1333 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1334 gnu_expr = 0;
1335 else
1336 gnu_expr
1337 = build_component_ref
1338 (gnu_expr, NULL_TREE,
1339 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1340 false);
1343 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1344 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1345 post_error ("?`Storage_Error` will be raised at run time!",
1346 gnat_entity);
1348 gnu_expr
1349 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1350 Empty, Empty, gnat_entity, mutable_p);
1351 const_flag = true;
1353 else
1355 gnu_expr = NULL_TREE;
1356 const_flag = false;
1360 /* If this object would go into the stack and has an alignment larger
1361 than the largest stack alignment the back-end can honor, resort to
1362 a variable of "aligning type". */
1363 if (!global_bindings_p () && !static_p && definition
1364 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1366 /* Create the new variable. No need for extra room before the
1367 aligned field as this is in automatic storage. */
1368 tree gnu_new_type
1369 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1370 TYPE_SIZE_UNIT (gnu_type),
1371 BIGGEST_ALIGNMENT, 0, gnat_entity);
1372 tree gnu_new_var
1373 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1374 NULL_TREE, gnu_new_type, NULL_TREE, false,
1375 false, false, false, NULL, gnat_entity);
1377 /* Initialize the aligned field if we have an initializer. */
1378 if (gnu_expr)
1379 add_stmt_with_node
1380 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1381 build_component_ref
1382 (gnu_new_var, NULL_TREE,
1383 TYPE_FIELDS (gnu_new_type), false),
1384 gnu_expr),
1385 gnat_entity);
1387 /* And setup this entity as a reference to the aligned field. */
1388 gnu_type = build_reference_type (gnu_type);
1389 gnu_expr
1390 = build_unary_op
1391 (ADDR_EXPR, gnu_type,
1392 build_component_ref (gnu_new_var, NULL_TREE,
1393 TYPE_FIELDS (gnu_new_type), false));
1395 gnu_size = NULL_TREE;
1396 used_by_ref = true;
1397 const_flag = true;
1400 /* If this is an aliased object with an unconstrained nominal subtype,
1401 we make its type a thin reference, i.e. the reference counterpart
1402 of a thin pointer, so that it points to the array part. This is
1403 aimed at making it easier for the debugger to decode the object.
1404 Note that we have to do that this late because of the couple of
1405 allocation adjustments that might be made just above. */
1406 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1407 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1408 && !type_annotate_only)
1410 tree gnu_array
1411 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1413 /* In case the object with the template has already been allocated
1414 just above, we have nothing to do here. */
1415 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1417 tree gnu_unc_var
1418 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1419 NULL_TREE, gnu_type, gnu_expr,
1420 const_flag, Is_Public (gnat_entity),
1421 imported_p || !definition, static_p,
1422 NULL, gnat_entity);
1423 gnu_expr
1424 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1425 TREE_CONSTANT (gnu_expr) = 1;
1427 gnu_size = NULL_TREE;
1428 used_by_ref = true;
1429 inner_const_flag = TREE_READONLY (gnu_unc_var);
1430 const_flag = true;
1433 gnu_type
1434 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1437 if (const_flag)
1438 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1440 /* Convert the expression to the type of the object except in the
1441 case where the object's type is unconstrained or the object's type
1442 is a padded record whose field is of self-referential size. In
1443 the former case, converting will generate unnecessary evaluations
1444 of the CONSTRUCTOR to compute the size and in the latter case, we
1445 want to only copy the actual data. Also don't convert to a record
1446 type with a variant part from a record type without one, to keep
1447 the object simpler. */
1448 if (gnu_expr
1449 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1450 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1451 && !(TYPE_IS_PADDING_P (gnu_type)
1452 && CONTAINS_PLACEHOLDER_P
1453 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1454 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1455 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1456 && get_variant_part (gnu_type) != NULL_TREE
1457 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1458 gnu_expr = convert (gnu_type, gnu_expr);
1460 /* If this name is external or a name was specified, use it, but don't
1461 use the Interface_Name with an address clause (see cd30005). */
1462 if ((Present (Interface_Name (gnat_entity))
1463 && No (Address_Clause (gnat_entity)))
1464 || (Is_Public (gnat_entity)
1465 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1466 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1468 /* If this is an aggregate constant initialized to a constant, force it
1469 to be statically allocated. This saves an initialization copy. */
1470 if (!static_p
1471 && const_flag
1472 && gnu_expr && TREE_CONSTANT (gnu_expr)
1473 && AGGREGATE_TYPE_P (gnu_type)
1474 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1475 && !(TYPE_IS_PADDING_P (gnu_type)
1476 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1477 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1478 static_p = true;
1480 /* Deal with a pragma Linker_Section on a constant or variable. */
1481 if ((kind == E_Constant || kind == E_Variable)
1482 && Present (Linker_Section_Pragma (gnat_entity)))
1483 prepend_one_attribute_pragma (&attr_list,
1484 Linker_Section_Pragma (gnat_entity));
1486 /* Now create the variable or the constant and set various flags. */
1487 gnu_decl
1488 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1489 gnu_expr, const_flag, Is_Public (gnat_entity),
1490 imported_p || !definition, static_p,
1491 !renamed_obj, attr_list, gnat_entity);
1492 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1493 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1494 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1496 /* If we are defining an Out parameter and optimization isn't enabled,
1497 create a fake PARM_DECL for debugging purposes and make it point to
1498 the VAR_DECL. Suppress debug info for the latter but make sure it
1499 will live in memory so that it can be accessed from within the
1500 debugger through the PARM_DECL. */
1501 if (kind == E_Out_Parameter
1502 && definition
1503 && debug_info_p
1504 && !optimize
1505 && !flag_generate_lto)
1507 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1508 gnat_pushdecl (param, gnat_entity);
1509 SET_DECL_VALUE_EXPR (param, gnu_decl);
1510 DECL_HAS_VALUE_EXPR_P (param) = 1;
1511 DECL_IGNORED_P (gnu_decl) = 1;
1512 TREE_ADDRESSABLE (gnu_decl) = 1;
1515 /* If this is a loop parameter, set the corresponding flag. */
1516 else if (kind == E_Loop_Parameter)
1517 DECL_LOOP_PARM_P (gnu_decl) = 1;
1519 /* If this is a renaming pointer, attach the renamed object to it and
1520 register it if we are at the global level. Note that an external
1521 constant is at the global level. */
1522 if (renamed_obj)
1524 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1525 if ((!definition && kind == E_Constant) || global_bindings_p ())
1527 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1528 record_global_renaming_pointer (gnu_decl);
1532 /* If this is a constant and we are defining it or it generates a real
1533 symbol at the object level and we are referencing it, we may want
1534 or need to have a true variable to represent it:
1535 - if optimization isn't enabled, for debugging purposes,
1536 - if the constant is public and not overlaid on something else,
1537 - if its address is taken,
1538 - if either itself or its type is aliased. */
1539 if (TREE_CODE (gnu_decl) == CONST_DECL
1540 && (definition || Sloc (gnat_entity) > Standard_Location)
1541 && ((!optimize && debug_info_p)
1542 || (Is_Public (gnat_entity)
1543 && No (Address_Clause (gnat_entity)))
1544 || Address_Taken (gnat_entity)
1545 || Is_Aliased (gnat_entity)
1546 || Is_Aliased (Etype (gnat_entity))))
1548 tree gnu_corr_var
1549 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1550 gnu_expr, true, Is_Public (gnat_entity),
1551 !definition, static_p, attr_list,
1552 gnat_entity);
1554 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1556 /* As debugging information will be generated for the variable,
1557 do not generate debugging information for the constant. */
1558 if (debug_info_p)
1559 DECL_IGNORED_P (gnu_decl) = 1;
1560 else
1561 DECL_IGNORED_P (gnu_corr_var) = 1;
1564 /* If this is a constant, even if we don't need a true variable, we
1565 may need to avoid returning the initializer in every case. That
1566 can happen for the address of a (constant) constructor because,
1567 upon dereferencing it, the constructor will be reinjected in the
1568 tree, which may not be valid in every case; see lvalue_required_p
1569 for more details. */
1570 if (TREE_CODE (gnu_decl) == CONST_DECL)
1571 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1573 /* If this object is declared in a block that contains a block with an
1574 exception handler, and we aren't using the GCC exception mechanism,
1575 we must force this variable in memory in order to avoid an invalid
1576 optimization. */
1577 if (Exception_Mechanism != Back_End_Exceptions
1578 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1579 TREE_ADDRESSABLE (gnu_decl) = 1;
1581 /* If this is a local variable with non-BLKmode and aggregate type,
1582 and optimization isn't enabled, then force it in memory so that
1583 a register won't be allocated to it with possible subparts left
1584 uninitialized and reaching the register allocator. */
1585 else if (TREE_CODE (gnu_decl) == VAR_DECL
1586 && !DECL_EXTERNAL (gnu_decl)
1587 && !TREE_STATIC (gnu_decl)
1588 && DECL_MODE (gnu_decl) != BLKmode
1589 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1590 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1591 && !optimize)
1592 TREE_ADDRESSABLE (gnu_decl) = 1;
1594 /* If we are defining an object with variable size or an object with
1595 fixed size that will be dynamically allocated, and we are using the
1596 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1597 if (definition
1598 && Exception_Mechanism == Setjmp_Longjmp
1599 && get_block_jmpbuf_decl ()
1600 && DECL_SIZE_UNIT (gnu_decl)
1601 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1602 || (flag_stack_check == GENERIC_STACK_CHECK
1603 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1604 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1605 add_stmt_with_node (build_call_n_expr
1606 (update_setjmp_buf_decl, 1,
1607 build_unary_op (ADDR_EXPR, NULL_TREE,
1608 get_block_jmpbuf_decl ())),
1609 gnat_entity);
1611 /* Back-annotate Esize and Alignment of the object if not already
1612 known. Note that we pick the values of the type, not those of
1613 the object, to shield ourselves from low-level platform-dependent
1614 adjustments like alignment promotion. This is both consistent with
1615 all the treatment above, where alignment and size are set on the
1616 type of the object and not on the object directly, and makes it
1617 possible to support all confirming representation clauses. */
1618 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1619 used_by_ref);
1621 break;
1623 case E_Void:
1624 /* Return a TYPE_DECL for "void" that we previously made. */
1625 gnu_decl = TYPE_NAME (void_type_node);
1626 break;
1628 case E_Enumeration_Type:
1629 /* A special case: for the types Character and Wide_Character in
1630 Standard, we do not list all the literals. So if the literals
1631 are not specified, make this an unsigned integer type. */
1632 if (No (First_Literal (gnat_entity)))
1634 gnu_type = make_unsigned_type (esize);
1635 TYPE_NAME (gnu_type) = gnu_entity_name;
1637 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1638 This is needed by the DWARF-2 back-end to distinguish between
1639 unsigned integer types and character types. */
1640 TYPE_STRING_FLAG (gnu_type) = 1;
1642 else
1644 /* We have a list of enumeral constants in First_Literal. We make a
1645 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1646 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1647 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1648 value of the literal. But when we have a regular boolean type, we
1649 simplify this a little by using a BOOLEAN_TYPE. */
1650 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1651 && !Has_Non_Standard_Rep (gnat_entity);
1652 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1653 tree gnu_list = NULL_TREE;
1654 Entity_Id gnat_literal;
1656 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1657 TYPE_PRECISION (gnu_type) = esize;
1658 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1659 set_min_and_max_values_for_integral_type (gnu_type, esize,
1660 TYPE_SIGN (gnu_type));
1661 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1662 layout_type (gnu_type);
1664 for (gnat_literal = First_Literal (gnat_entity);
1665 Present (gnat_literal);
1666 gnat_literal = Next_Literal (gnat_literal))
1668 tree gnu_value
1669 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1670 tree gnu_literal
1671 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1672 gnu_type, gnu_value, true, false, false,
1673 false, NULL, gnat_literal);
1674 /* Do not generate debug info for individual enumerators. */
1675 DECL_IGNORED_P (gnu_literal) = 1;
1676 save_gnu_tree (gnat_literal, gnu_literal, false);
1677 gnu_list
1678 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1681 if (!is_boolean)
1682 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1684 /* Note that the bounds are updated at the end of this function
1685 to avoid an infinite recursion since they refer to the type. */
1686 goto discrete_type;
1688 break;
1690 case E_Signed_Integer_Type:
1691 case E_Ordinary_Fixed_Point_Type:
1692 case E_Decimal_Fixed_Point_Type:
1693 /* For integer types, just make a signed type the appropriate number
1694 of bits. */
1695 gnu_type = make_signed_type (esize);
1696 goto discrete_type;
1698 case E_Modular_Integer_Type:
1700 /* For modular types, make the unsigned type of the proper number
1701 of bits and then set up the modulus, if required. */
1702 tree gnu_modulus, gnu_high = NULL_TREE;
1704 /* Packed Array Impl. Types are supposed to be subtypes only. */
1705 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1707 gnu_type = make_unsigned_type (esize);
1709 /* Get the modulus in this type. If it overflows, assume it is because
1710 it is equal to 2**Esize. Note that there is no overflow checking
1711 done on unsigned type, so we detect the overflow by looking for
1712 a modulus of zero, which is otherwise invalid. */
1713 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1715 if (!integer_zerop (gnu_modulus))
1717 TYPE_MODULAR_P (gnu_type) = 1;
1718 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1719 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1720 convert (gnu_type, integer_one_node));
1723 /* If the upper bound is not maximal, make an extra subtype. */
1724 if (gnu_high
1725 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1727 tree gnu_subtype = make_unsigned_type (esize);
1728 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1729 TREE_TYPE (gnu_subtype) = gnu_type;
1730 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1731 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1732 gnu_type = gnu_subtype;
1735 goto discrete_type;
1737 case E_Signed_Integer_Subtype:
1738 case E_Enumeration_Subtype:
1739 case E_Modular_Integer_Subtype:
1740 case E_Ordinary_Fixed_Point_Subtype:
1741 case E_Decimal_Fixed_Point_Subtype:
1743 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1744 not want to call create_range_type since we would like each subtype
1745 node to be distinct. ??? Historically this was in preparation for
1746 when memory aliasing is implemented, but that's obsolete now given
1747 the call to relate_alias_sets below.
1749 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1750 this fact is used by the arithmetic conversion functions.
1752 We elaborate the Ancestor_Subtype if it is not in the current unit
1753 and one of our bounds is non-static. We do this to ensure consistent
1754 naming in the case where several subtypes share the same bounds, by
1755 elaborating the first such subtype first, thus using its name. */
1757 if (!definition
1758 && Present (Ancestor_Subtype (gnat_entity))
1759 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1760 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1761 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1762 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1764 /* Set the precision to the Esize except for bit-packed arrays. */
1765 if (Is_Packed_Array_Impl_Type (gnat_entity)
1766 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1767 esize = UI_To_Int (RM_Size (gnat_entity));
1769 /* This should be an unsigned type if the base type is unsigned or
1770 if the lower bound is constant and non-negative or if the type
1771 is biased. */
1772 if (Is_Unsigned_Type (Etype (gnat_entity))
1773 || Is_Unsigned_Type (gnat_entity)
1774 || Has_Biased_Representation (gnat_entity))
1775 gnu_type = make_unsigned_type (esize);
1776 else
1777 gnu_type = make_signed_type (esize);
1778 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1780 SET_TYPE_RM_MIN_VALUE
1781 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1782 gnat_entity, get_identifier ("L"),
1783 definition, true,
1784 Needs_Debug_Info (gnat_entity)));
1786 SET_TYPE_RM_MAX_VALUE
1787 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1788 gnat_entity, get_identifier ("U"),
1789 definition, true,
1790 Needs_Debug_Info (gnat_entity)));
1792 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1793 = Has_Biased_Representation (gnat_entity);
1795 /* Inherit our alias set from what we're a subtype of. Subtypes
1796 are not different types and a pointer can designate any instance
1797 within a subtype hierarchy. */
1798 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1800 /* One of the above calls might have caused us to be elaborated,
1801 so don't blow up if so. */
1802 if (present_gnu_tree (gnat_entity))
1804 maybe_present = true;
1805 break;
1808 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1809 TYPE_STUB_DECL (gnu_type)
1810 = create_type_stub_decl (gnu_entity_name, gnu_type);
1812 /* For a packed array, make the original array type a parallel type. */
1813 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1814 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1816 discrete_type:
1818 /* We have to handle clauses that under-align the type specially. */
1819 if ((Present (Alignment_Clause (gnat_entity))
1820 || (Is_Packed_Array_Impl_Type (gnat_entity)
1821 && Present
1822 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1823 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1825 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1826 if (align >= TYPE_ALIGN (gnu_type))
1827 align = 0;
1830 /* If the type we are dealing with represents a bit-packed array,
1831 we need to have the bits left justified on big-endian targets
1832 and right justified on little-endian targets. We also need to
1833 ensure that when the value is read (e.g. for comparison of two
1834 such values), we only get the good bits, since the unused bits
1835 are uninitialized. Both goals are accomplished by wrapping up
1836 the modular type in an enclosing record type. */
1837 if (Is_Packed_Array_Impl_Type (gnat_entity)
1838 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1840 tree gnu_field_type, gnu_field;
1842 /* Set the RM size before wrapping up the original type. */
1843 SET_TYPE_RM_SIZE (gnu_type,
1844 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1845 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1847 /* Create a stripped-down declaration, mainly for debugging. */
1848 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1849 gnat_entity);
1851 /* Now save it and build the enclosing record type. */
1852 gnu_field_type = gnu_type;
1854 gnu_type = make_node (RECORD_TYPE);
1855 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1856 TYPE_PACKED (gnu_type) = 1;
1857 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1858 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1859 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1861 /* Propagate the alignment of the modular type to the record type,
1862 unless there is an alignment clause that under-aligns the type.
1863 This means that bit-packed arrays are given "ceil" alignment for
1864 their size by default, which may seem counter-intuitive but makes
1865 it possible to overlay them on modular types easily. */
1866 TYPE_ALIGN (gnu_type)
1867 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1869 /* Propagate the reverse storage order flag to the record type so
1870 that the required byte swapping is performed when retrieving the
1871 enclosed modular value. */
1872 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1873 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1875 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1877 /* Don't declare the field as addressable since we won't be taking
1878 its address and this would prevent create_field_decl from making
1879 a bitfield. */
1880 gnu_field
1881 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1882 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1884 /* Do not emit debug info until after the parallel type is added. */
1885 finish_record_type (gnu_type, gnu_field, 2, false);
1886 compute_record_mode (gnu_type);
1887 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1889 if (debug_info_p)
1891 /* Make the original array type a parallel type. */
1892 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1894 rest_of_record_type_compilation (gnu_type);
1898 /* If the type we are dealing with has got a smaller alignment than the
1899 natural one, we need to wrap it up in a record type and misalign the
1900 latter; we reuse the padding machinery for this purpose. Note that,
1901 even if the record type is marked as packed because of misalignment,
1902 we don't pack the field so as to give it the size of the type. */
1903 else if (align > 0)
1905 tree gnu_field_type, gnu_field;
1907 /* Set the RM size before wrapping up the type. */
1908 SET_TYPE_RM_SIZE (gnu_type,
1909 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1911 /* Create a stripped-down declaration, mainly for debugging. */
1912 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1913 gnat_entity);
1915 /* Now save it and build the enclosing record type. */
1916 gnu_field_type = gnu_type;
1918 gnu_type = make_node (RECORD_TYPE);
1919 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1920 TYPE_PACKED (gnu_type) = 1;
1921 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1922 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1923 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1924 TYPE_ALIGN (gnu_type) = align;
1925 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1927 /* Don't declare the field as addressable since we won't be taking
1928 its address and this would prevent create_field_decl from making
1929 a bitfield. */
1930 gnu_field
1931 = create_field_decl (get_identifier ("F"), gnu_field_type,
1932 gnu_type, TYPE_SIZE (gnu_field_type),
1933 bitsize_zero_node, 0, 0);
1935 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1936 compute_record_mode (gnu_type);
1937 TYPE_PADDING_P (gnu_type) = 1;
1940 break;
1942 case E_Floating_Point_Type:
1943 /* The type of the Low and High bounds can be our type if this is
1944 a type from Standard, so set them at the end of the function. */
1945 gnu_type = make_node (REAL_TYPE);
1946 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1947 layout_type (gnu_type);
1948 break;
1950 case E_Floating_Point_Subtype:
1951 /* See the E_Signed_Integer_Subtype case for the rationale. */
1952 if (!definition
1953 && Present (Ancestor_Subtype (gnat_entity))
1954 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1955 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1956 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1957 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1959 gnu_type = make_node (REAL_TYPE);
1960 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1961 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1962 TYPE_GCC_MIN_VALUE (gnu_type)
1963 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1964 TYPE_GCC_MAX_VALUE (gnu_type)
1965 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1966 layout_type (gnu_type);
1968 SET_TYPE_RM_MIN_VALUE
1969 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1970 gnat_entity, get_identifier ("L"),
1971 definition, true,
1972 Needs_Debug_Info (gnat_entity)));
1974 SET_TYPE_RM_MAX_VALUE
1975 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1976 gnat_entity, get_identifier ("U"),
1977 definition, true,
1978 Needs_Debug_Info (gnat_entity)));
1980 /* Inherit our alias set from what we're a subtype of, as for
1981 integer subtypes. */
1982 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1984 /* One of the above calls might have caused us to be elaborated,
1985 so don't blow up if so. */
1986 maybe_present = true;
1987 break;
1989 /* Array Types and Subtypes
1991 Unconstrained array types are represented by E_Array_Type and
1992 constrained array types are represented by E_Array_Subtype. There
1993 are no actual objects of an unconstrained array type; all we have
1994 are pointers to that type.
1996 The following fields are defined on array types and subtypes:
1998 Component_Type Component type of the array.
1999 Number_Dimensions Number of dimensions (an int).
2000 First_Index Type of first index. */
2002 case E_Array_Type:
2004 const bool convention_fortran_p
2005 = (Convention (gnat_entity) == Convention_Fortran);
2006 const int ndim = Number_Dimensions (gnat_entity);
2007 tree gnu_template_type;
2008 tree gnu_ptr_template;
2009 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2010 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2011 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2012 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2013 Entity_Id gnat_index, gnat_name;
2014 int index;
2015 tree comp_type;
2017 /* Create the type for the component now, as it simplifies breaking
2018 type reference loops. */
2019 comp_type
2020 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2021 if (present_gnu_tree (gnat_entity))
2023 /* As a side effect, the type may have been translated. */
2024 maybe_present = true;
2025 break;
2028 /* We complete an existing dummy fat pointer type in place. This both
2029 avoids further complex adjustments in update_pointer_to and yields
2030 better debugging information in DWARF by leveraging the support for
2031 incomplete declarations of "tagged" types in the DWARF back-end. */
2032 gnu_type = get_dummy_type (gnat_entity);
2033 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2035 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2036 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2037 /* Save the contents of the dummy type for update_pointer_to. */
2038 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2039 gnu_ptr_template =
2040 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2041 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2043 else
2045 gnu_fat_type = make_node (RECORD_TYPE);
2046 gnu_template_type = make_node (RECORD_TYPE);
2047 gnu_ptr_template = build_pointer_type (gnu_template_type);
2050 /* Make a node for the array. If we are not defining the array
2051 suppress expanding incomplete types. */
2052 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2054 if (!definition)
2056 defer_incomplete_level++;
2057 this_deferred = true;
2060 /* Build the fat pointer type. Use a "void *" object instead of
2061 a pointer to the array type since we don't have the array type
2062 yet (it will reference the fat pointer via the bounds). */
2064 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2065 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2066 DECL_CHAIN (tem)
2067 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2068 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2070 if (COMPLETE_TYPE_P (gnu_fat_type))
2072 /* We are going to lay it out again so reset the alias set. */
2073 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2074 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2075 finish_fat_pointer_type (gnu_fat_type, tem);
2076 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2077 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2079 TYPE_FIELDS (t) = tem;
2080 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2083 else
2085 finish_fat_pointer_type (gnu_fat_type, tem);
2086 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2089 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2090 is the fat pointer. This will be used to access the individual
2091 fields once we build them. */
2092 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2093 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2094 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2095 gnu_template_reference
2096 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2097 TREE_READONLY (gnu_template_reference) = 1;
2098 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2100 /* Now create the GCC type for each index and add the fields for that
2101 index to the template. */
2102 for (index = (convention_fortran_p ? ndim - 1 : 0),
2103 gnat_index = First_Index (gnat_entity);
2104 0 <= index && index < ndim;
2105 index += (convention_fortran_p ? - 1 : 1),
2106 gnat_index = Next_Index (gnat_index))
2108 char field_name[16];
2109 tree gnu_index_base_type
2110 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2111 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2112 tree gnu_min, gnu_max, gnu_high;
2114 /* Make the FIELD_DECLs for the low and high bounds of this
2115 type and then make extractions of these fields from the
2116 template. */
2117 sprintf (field_name, "LB%d", index);
2118 gnu_lb_field = create_field_decl (get_identifier (field_name),
2119 gnu_index_base_type,
2120 gnu_template_type, NULL_TREE,
2121 NULL_TREE, 0, 0);
2122 Sloc_to_locus (Sloc (gnat_entity),
2123 &DECL_SOURCE_LOCATION (gnu_lb_field));
2125 field_name[0] = 'U';
2126 gnu_hb_field = create_field_decl (get_identifier (field_name),
2127 gnu_index_base_type,
2128 gnu_template_type, NULL_TREE,
2129 NULL_TREE, 0, 0);
2130 Sloc_to_locus (Sloc (gnat_entity),
2131 &DECL_SOURCE_LOCATION (gnu_hb_field));
2133 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2135 /* We can't use build_component_ref here since the template type
2136 isn't complete yet. */
2137 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2138 gnu_template_reference, gnu_lb_field,
2139 NULL_TREE);
2140 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2141 gnu_template_reference, gnu_hb_field,
2142 NULL_TREE);
2143 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2145 gnu_min = convert (sizetype, gnu_orig_min);
2146 gnu_max = convert (sizetype, gnu_orig_max);
2148 /* Compute the size of this dimension. See the E_Array_Subtype
2149 case below for the rationale. */
2150 gnu_high
2151 = build3 (COND_EXPR, sizetype,
2152 build2 (GE_EXPR, boolean_type_node,
2153 gnu_orig_max, gnu_orig_min),
2154 gnu_max,
2155 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2157 /* Make a range type with the new range in the Ada base type.
2158 Then make an index type with the size range in sizetype. */
2159 gnu_index_types[index]
2160 = create_index_type (gnu_min, gnu_high,
2161 create_range_type (gnu_index_base_type,
2162 gnu_orig_min,
2163 gnu_orig_max),
2164 gnat_entity);
2166 /* Update the maximum size of the array in elements. */
2167 if (gnu_max_size)
2169 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2170 tree gnu_min
2171 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2172 tree gnu_max
2173 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2174 tree gnu_this_max
2175 = size_binop (PLUS_EXPR, size_one_node,
2176 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2178 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2179 && TREE_OVERFLOW (gnu_this_max))
2180 gnu_max_size = NULL_TREE;
2181 else
2182 gnu_max_size
2183 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2186 TYPE_NAME (gnu_index_types[index])
2187 = create_concat_name (gnat_entity, field_name);
2190 /* Install all the fields into the template. */
2191 TYPE_NAME (gnu_template_type)
2192 = create_concat_name (gnat_entity, "XUB");
2193 gnu_template_fields = NULL_TREE;
2194 for (index = 0; index < ndim; index++)
2195 gnu_template_fields
2196 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2197 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2198 debug_info_p);
2199 TYPE_READONLY (gnu_template_type) = 1;
2201 /* If Component_Size is not already specified, annotate it with the
2202 size of the component. */
2203 if (Unknown_Component_Size (gnat_entity))
2204 Set_Component_Size (gnat_entity,
2205 annotate_value (TYPE_SIZE (comp_type)));
2207 /* Compute the maximum size of the array in units and bits. */
2208 if (gnu_max_size)
2210 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2211 TYPE_SIZE_UNIT (comp_type));
2212 gnu_max_size = size_binop (MULT_EXPR,
2213 convert (bitsizetype, gnu_max_size),
2214 TYPE_SIZE (comp_type));
2216 else
2217 gnu_max_size_unit = NULL_TREE;
2219 /* Now build the array type. */
2220 tem = comp_type;
2221 for (index = ndim - 1; index >= 0; index--)
2223 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2224 if (index == ndim - 1)
2225 TYPE_REVERSE_STORAGE_ORDER (tem)
2226 = Reverse_Storage_Order (gnat_entity);
2227 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2228 if (array_type_has_nonaliased_component (tem, gnat_entity))
2229 TYPE_NONALIASED_COMPONENT (tem) = 1;
2231 /* If it is passed by reference, force BLKmode to ensure that
2232 objects of this type will always be put in memory. */
2233 if (TYPE_MODE (tem) != BLKmode
2234 && Is_By_Reference_Type (gnat_entity))
2235 SET_TYPE_MODE (tem, BLKmode);
2238 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2240 /* If an alignment is specified, use it if valid. But ignore it
2241 for the original type of packed array types. If the alignment
2242 was requested with an explicit alignment clause, state so. */
2243 if (No (Packed_Array_Impl_Type (gnat_entity))
2244 && Known_Alignment (gnat_entity))
2246 TYPE_ALIGN (tem)
2247 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2248 TYPE_ALIGN (tem));
2249 if (Present (Alignment_Clause (gnat_entity)))
2250 TYPE_USER_ALIGN (tem) = 1;
2253 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2255 /* Adjust the type of the pointer-to-array field of the fat pointer
2256 and record the aliasing relationships if necessary. */
2257 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2258 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2259 record_component_aliases (gnu_fat_type);
2261 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2262 corresponding fat pointer. */
2263 TREE_TYPE (gnu_type) = gnu_fat_type;
2264 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2265 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2266 SET_TYPE_MODE (gnu_type, BLKmode);
2267 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2269 /* If the maximum size doesn't overflow, use it. */
2270 if (gnu_max_size
2271 && TREE_CODE (gnu_max_size) == INTEGER_CST
2272 && !TREE_OVERFLOW (gnu_max_size)
2273 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2274 && !TREE_OVERFLOW (gnu_max_size_unit))
2276 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2277 TYPE_SIZE (tem));
2278 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2279 TYPE_SIZE_UNIT (tem));
2282 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2283 !Comes_From_Source (gnat_entity), debug_info_p,
2284 gnat_entity);
2286 /* Give the fat pointer type a name. If this is a packed array, tell
2287 the debugger how to interpret the underlying bits. */
2288 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2289 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2290 else
2291 gnat_name = gnat_entity;
2292 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2293 !Comes_From_Source (gnat_entity), debug_info_p,
2294 gnat_entity);
2296 /* Create the type to be designated by thin pointers: a record type for
2297 the array and its template. We used to shift the fields to have the
2298 template at a negative offset, but this was somewhat of a kludge; we
2299 now shift thin pointer values explicitly but only those which have a
2300 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2301 tem = build_unc_object_type (gnu_template_type, tem,
2302 create_concat_name (gnat_name, "XUT"),
2303 debug_info_p);
2305 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2306 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2308 break;
2310 case E_Array_Subtype:
2312 /* This is the actual data type for array variables. Multidimensional
2313 arrays are implemented as arrays of arrays. Note that arrays which
2314 have sparse enumeration subtypes as index components create sparse
2315 arrays, which is obviously space inefficient but so much easier to
2316 code for now.
2318 Also note that the subtype never refers to the unconstrained array
2319 type, which is somewhat at variance with Ada semantics.
2321 First check to see if this is simply a renaming of the array type.
2322 If so, the result is the array type. */
2324 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2325 if (!Is_Constrained (gnat_entity))
2327 else
2329 Entity_Id gnat_index, gnat_base_index;
2330 const bool convention_fortran_p
2331 = (Convention (gnat_entity) == Convention_Fortran);
2332 const int ndim = Number_Dimensions (gnat_entity);
2333 tree gnu_base_type = gnu_type;
2334 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2335 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2336 bool need_index_type_struct = false;
2337 int index;
2339 /* First create the GCC type for each index and find out whether
2340 special types are needed for debugging information. */
2341 for (index = (convention_fortran_p ? ndim - 1 : 0),
2342 gnat_index = First_Index (gnat_entity),
2343 gnat_base_index
2344 = First_Index (Implementation_Base_Type (gnat_entity));
2345 0 <= index && index < ndim;
2346 index += (convention_fortran_p ? - 1 : 1),
2347 gnat_index = Next_Index (gnat_index),
2348 gnat_base_index = Next_Index (gnat_base_index))
2350 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2351 tree gnu_index_base_type = get_base_type (gnu_index_type);
2352 tree gnu_orig_min
2353 = convert (gnu_index_base_type,
2354 TYPE_MIN_VALUE (gnu_index_type));
2355 tree gnu_orig_max
2356 = convert (gnu_index_base_type,
2357 TYPE_MAX_VALUE (gnu_index_type));
2358 tree gnu_min = convert (sizetype, gnu_orig_min);
2359 tree gnu_max = convert (sizetype, gnu_orig_max);
2360 tree gnu_base_index_type
2361 = get_unpadded_type (Etype (gnat_base_index));
2362 tree gnu_base_index_base_type
2363 = get_base_type (gnu_base_index_type);
2364 tree gnu_base_orig_min
2365 = convert (gnu_base_index_base_type,
2366 TYPE_MIN_VALUE (gnu_base_index_type));
2367 tree gnu_base_orig_max
2368 = convert (gnu_base_index_base_type,
2369 TYPE_MAX_VALUE (gnu_base_index_type));
2370 tree gnu_high;
2372 /* See if the base array type is already flat. If it is, we
2373 are probably compiling an ACATS test but it will cause the
2374 code below to malfunction if we don't handle it specially. */
2375 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2376 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2377 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2379 gnu_min = size_one_node;
2380 gnu_max = size_zero_node;
2381 gnu_high = gnu_max;
2384 /* Similarly, if one of the values overflows in sizetype and the
2385 range is null, use 1..0 for the sizetype bounds. */
2386 else if (TREE_CODE (gnu_min) == INTEGER_CST
2387 && TREE_CODE (gnu_max) == INTEGER_CST
2388 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2389 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2391 gnu_min = size_one_node;
2392 gnu_max = size_zero_node;
2393 gnu_high = gnu_max;
2396 /* If the minimum and maximum values both overflow in sizetype,
2397 but the difference in the original type does not overflow in
2398 sizetype, ignore the overflow indication. */
2399 else if (TREE_CODE (gnu_min) == INTEGER_CST
2400 && TREE_CODE (gnu_max) == INTEGER_CST
2401 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2402 && !TREE_OVERFLOW
2403 (convert (sizetype,
2404 fold_build2 (MINUS_EXPR, gnu_index_type,
2405 gnu_orig_max,
2406 gnu_orig_min))))
2408 TREE_OVERFLOW (gnu_min) = 0;
2409 TREE_OVERFLOW (gnu_max) = 0;
2410 gnu_high = gnu_max;
2413 /* Compute the size of this dimension in the general case. We
2414 need to provide GCC with an upper bound to use but have to
2415 deal with the "superflat" case. There are three ways to do
2416 this. If we can prove that the array can never be superflat,
2417 we can just use the high bound of the index type. */
2418 else if ((Nkind (gnat_index) == N_Range
2419 && cannot_be_superflat_p (gnat_index))
2420 /* Bit-Packed Array Impl. Types are never superflat. */
2421 || (Is_Packed_Array_Impl_Type (gnat_entity)
2422 && Is_Bit_Packed_Array
2423 (Original_Array_Type (gnat_entity))))
2424 gnu_high = gnu_max;
2426 /* Otherwise, if the high bound is constant but the low bound is
2427 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2428 lower bound. Note that the comparison must be done in the
2429 original type to avoid any overflow during the conversion. */
2430 else if (TREE_CODE (gnu_max) == INTEGER_CST
2431 && TREE_CODE (gnu_min) != INTEGER_CST)
2433 gnu_high = gnu_max;
2434 gnu_min
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_min,
2441 int_const_binop (PLUS_EXPR, gnu_max,
2442 size_one_node));
2445 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2446 in all the other cases. Note that, here as well as above,
2447 the condition used in the comparison must be equivalent to
2448 the condition (length != 0). This is relied upon in order
2449 to optimize array comparisons in compare_arrays. Moreover
2450 we use int_const_binop for the shift by 1 if the bound is
2451 constant to avoid any unwanted overflow. */
2452 else
2453 gnu_high
2454 = build_cond_expr (sizetype,
2455 build_binary_op (GE_EXPR,
2456 boolean_type_node,
2457 gnu_orig_max,
2458 gnu_orig_min),
2459 gnu_max,
2460 TREE_CODE (gnu_min) == INTEGER_CST
2461 ? int_const_binop (MINUS_EXPR, gnu_min,
2462 size_one_node)
2463 : size_binop (MINUS_EXPR, gnu_min,
2464 size_one_node));
2466 /* Reuse the index type for the range type. Then make an index
2467 type with the size range in sizetype. */
2468 gnu_index_types[index]
2469 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2470 gnat_entity);
2472 /* Update the maximum size of the array in elements. Here we
2473 see if any constraint on the index type of the base type
2474 can be used in the case of self-referential bound on the
2475 index type of the subtype. We look for a non-"infinite"
2476 and non-self-referential bound from any type involved and
2477 handle each bound separately. */
2478 if (gnu_max_size)
2480 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2481 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2482 tree gnu_base_index_base_type
2483 = get_base_type (gnu_base_index_type);
2484 tree gnu_base_base_min
2485 = convert (sizetype,
2486 TYPE_MIN_VALUE (gnu_base_index_base_type));
2487 tree gnu_base_base_max
2488 = convert (sizetype,
2489 TYPE_MAX_VALUE (gnu_base_index_base_type));
2491 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2492 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2493 && !TREE_OVERFLOW (gnu_base_min)))
2494 gnu_base_min = gnu_min;
2496 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2497 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2498 && !TREE_OVERFLOW (gnu_base_max)))
2499 gnu_base_max = gnu_max;
2501 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2502 && TREE_OVERFLOW (gnu_base_min))
2503 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2504 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2505 && TREE_OVERFLOW (gnu_base_max))
2506 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2507 gnu_max_size = NULL_TREE;
2508 else
2510 tree gnu_this_max;
2512 /* Use int_const_binop if the bounds are constant to
2513 avoid any unwanted overflow. */
2514 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2515 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2516 gnu_this_max
2517 = int_const_binop (PLUS_EXPR, size_one_node,
2518 int_const_binop (MINUS_EXPR,
2519 gnu_base_max,
2520 gnu_base_min));
2521 else
2522 gnu_this_max
2523 = size_binop (PLUS_EXPR, size_one_node,
2524 size_binop (MINUS_EXPR,
2525 gnu_base_max,
2526 gnu_base_min));
2528 gnu_max_size
2529 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2533 /* We need special types for debugging information to point to
2534 the index types if they have variable bounds, are not integer
2535 types or are biased. */
2536 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2537 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2538 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2539 || (TREE_TYPE (gnu_index_type)
2540 && TREE_CODE (TREE_TYPE (gnu_index_type))
2541 != INTEGER_TYPE)
2542 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2543 need_index_type_struct = true;
2546 /* Then flatten: create the array of arrays. For an array type
2547 used to implement a packed array, get the component type from
2548 the original array type since the representation clauses that
2549 can affect it are on the latter. */
2550 if (Is_Packed_Array_Impl_Type (gnat_entity)
2551 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2553 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2554 for (index = ndim - 1; index >= 0; index--)
2555 gnu_type = TREE_TYPE (gnu_type);
2557 /* One of the above calls might have caused us to be elaborated,
2558 so don't blow up if so. */
2559 if (present_gnu_tree (gnat_entity))
2561 maybe_present = true;
2562 break;
2565 else
2567 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2568 debug_info_p);
2570 /* One of the above calls might have caused us to be elaborated,
2571 so don't blow up if so. */
2572 if (present_gnu_tree (gnat_entity))
2574 maybe_present = true;
2575 break;
2579 /* Compute the maximum size of the array in units and bits. */
2580 if (gnu_max_size)
2582 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2583 TYPE_SIZE_UNIT (gnu_type));
2584 gnu_max_size = size_binop (MULT_EXPR,
2585 convert (bitsizetype, gnu_max_size),
2586 TYPE_SIZE (gnu_type));
2588 else
2589 gnu_max_size_unit = NULL_TREE;
2591 /* Now build the array type. */
2592 for (index = ndim - 1; index >= 0; index --)
2594 gnu_type = build_nonshared_array_type (gnu_type,
2595 gnu_index_types[index]);
2596 if (index == ndim - 1)
2597 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2598 = Reverse_Storage_Order (gnat_entity);
2599 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2600 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2601 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2603 /* See the E_Array_Type case for the rationale. */
2604 if (TYPE_MODE (gnu_type) != BLKmode
2605 && Is_By_Reference_Type (gnat_entity))
2606 SET_TYPE_MODE (gnu_type, BLKmode);
2609 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2611 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2612 TYPE_STUB_DECL (gnu_type)
2613 = create_type_stub_decl (gnu_entity_name, gnu_type);
2615 /* If we are at file level and this is a multi-dimensional array,
2616 we need to make a variable corresponding to the stride of the
2617 inner dimensions. */
2618 if (global_bindings_p () && ndim > 1)
2620 tree gnu_st_name = get_identifier ("ST");
2621 tree gnu_arr_type;
2623 for (gnu_arr_type = TREE_TYPE (gnu_type);
2624 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2625 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2626 gnu_st_name = concat_name (gnu_st_name, "ST"))
2628 tree eltype = TREE_TYPE (gnu_arr_type);
2630 TYPE_SIZE (gnu_arr_type)
2631 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2632 gnat_entity, gnu_st_name,
2633 definition, false);
2635 /* ??? For now, store the size as a multiple of the
2636 alignment of the element type in bytes so that we
2637 can see the alignment from the tree. */
2638 TYPE_SIZE_UNIT (gnu_arr_type)
2639 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2640 gnat_entity,
2641 concat_name (gnu_st_name, "A_U"),
2642 definition, false,
2643 TYPE_ALIGN (eltype));
2645 /* ??? create_type_decl is not invoked on the inner types so
2646 the MULT_EXPR node built above will never be marked. */
2647 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2651 /* If we need to write out a record type giving the names of the
2652 bounds for debugging purposes, do it now and make the record
2653 type a parallel type. This is not needed for a packed array
2654 since the bounds are conveyed by the original array type. */
2655 if (need_index_type_struct
2656 && debug_info_p
2657 && !Is_Packed_Array_Impl_Type (gnat_entity))
2659 tree gnu_bound_rec = make_node (RECORD_TYPE);
2660 tree gnu_field_list = NULL_TREE;
2661 tree gnu_field;
2663 TYPE_NAME (gnu_bound_rec)
2664 = create_concat_name (gnat_entity, "XA");
2666 for (index = ndim - 1; index >= 0; index--)
2668 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2669 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2671 /* Make sure to reference the types themselves, and not just
2672 their names, as the debugger may fall back on them. */
2673 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2674 gnu_bound_rec, NULL_TREE,
2675 NULL_TREE, 0, 0);
2676 DECL_CHAIN (gnu_field) = gnu_field_list;
2677 gnu_field_list = gnu_field;
2680 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2681 add_parallel_type (gnu_type, gnu_bound_rec);
2684 /* If this is a packed array type, make the original array type a
2685 parallel type. Otherwise, do it for the base array type if it
2686 isn't artificial to make sure it is kept in the debug info. */
2687 if (debug_info_p)
2689 if (Is_Packed_Array_Impl_Type (gnat_entity))
2690 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2691 else
2693 tree gnu_base_decl
2694 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2695 if (!DECL_ARTIFICIAL (gnu_base_decl))
2696 add_parallel_type (gnu_type,
2697 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2701 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2702 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2703 = (Is_Packed_Array_Impl_Type (gnat_entity)
2704 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2706 /* If the size is self-referential and the maximum size doesn't
2707 overflow, use it. */
2708 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2709 && gnu_max_size
2710 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2711 && TREE_OVERFLOW (gnu_max_size))
2712 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2713 && TREE_OVERFLOW (gnu_max_size_unit)))
2715 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2716 TYPE_SIZE (gnu_type));
2717 TYPE_SIZE_UNIT (gnu_type)
2718 = size_binop (MIN_EXPR, gnu_max_size_unit,
2719 TYPE_SIZE_UNIT (gnu_type));
2722 /* Set our alias set to that of our base type. This gives all
2723 array subtypes the same alias set. */
2724 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2726 /* If this is a packed type, make this type the same as the packed
2727 array type, but do some adjusting in the type first. */
2728 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2730 Entity_Id gnat_index;
2731 tree gnu_inner;
2733 /* First finish the type we had been making so that we output
2734 debugging information for it. */
2735 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2736 if (Treat_As_Volatile (gnat_entity))
2737 gnu_type
2738 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2739 /* Make it artificial only if the base type was artificial too.
2740 That's sort of "morally" true and will make it possible for
2741 the debugger to look it up by name in DWARF, which is needed
2742 in order to decode the packed array type. */
2743 gnu_decl
2744 = create_type_decl (gnu_entity_name, gnu_type,
2745 !Comes_From_Source (Etype (gnat_entity))
2746 && !Comes_From_Source (gnat_entity),
2747 debug_info_p, gnat_entity);
2749 /* Save it as our equivalent in case the call below elaborates
2750 this type again. */
2751 save_gnu_tree (gnat_entity, gnu_decl, false);
2753 gnu_decl
2754 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2755 NULL_TREE, 0);
2756 this_made_decl = true;
2757 gnu_type = TREE_TYPE (gnu_decl);
2758 save_gnu_tree (gnat_entity, NULL_TREE, false);
2760 gnu_inner = gnu_type;
2761 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2762 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2763 || TYPE_PADDING_P (gnu_inner)))
2764 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2766 /* We need to attach the index type to the type we just made so
2767 that the actual bounds can later be put into a template. */
2768 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2769 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2770 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2771 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2773 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2775 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2776 TYPE_MODULUS for modular types so we make an extra
2777 subtype if necessary. */
2778 if (TYPE_MODULAR_P (gnu_inner))
2780 tree gnu_subtype
2781 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2782 TREE_TYPE (gnu_subtype) = gnu_inner;
2783 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2784 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2785 TYPE_MIN_VALUE (gnu_inner));
2786 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2787 TYPE_MAX_VALUE (gnu_inner));
2788 gnu_inner = gnu_subtype;
2791 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2793 #ifdef ENABLE_CHECKING
2794 /* Check for other cases of overloading. */
2795 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2796 #endif
2799 for (gnat_index = First_Index (gnat_entity);
2800 Present (gnat_index);
2801 gnat_index = Next_Index (gnat_index))
2802 SET_TYPE_ACTUAL_BOUNDS
2803 (gnu_inner,
2804 tree_cons (NULL_TREE,
2805 get_unpadded_type (Etype (gnat_index)),
2806 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2808 if (Convention (gnat_entity) != Convention_Fortran)
2809 SET_TYPE_ACTUAL_BOUNDS
2810 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2812 if (TREE_CODE (gnu_type) == RECORD_TYPE
2813 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2814 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2818 else
2819 /* Abort if packed array with no Packed_Array_Impl_Type. */
2820 gcc_assert (!Is_Packed (gnat_entity));
2822 break;
2824 case E_String_Literal_Subtype:
2825 /* Create the type for a string literal. */
2827 Entity_Id gnat_full_type
2828 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2829 && Present (Full_View (Etype (gnat_entity)))
2830 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2831 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2832 tree gnu_string_array_type
2833 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2834 tree gnu_string_index_type
2835 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2836 (TYPE_DOMAIN (gnu_string_array_type))));
2837 tree gnu_lower_bound
2838 = convert (gnu_string_index_type,
2839 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2840 tree gnu_length
2841 = UI_To_gnu (String_Literal_Length (gnat_entity),
2842 gnu_string_index_type);
2843 tree gnu_upper_bound
2844 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2845 gnu_lower_bound,
2846 int_const_binop (MINUS_EXPR, gnu_length,
2847 convert (gnu_string_index_type,
2848 integer_one_node)));
2849 tree gnu_index_type
2850 = create_index_type (convert (sizetype, gnu_lower_bound),
2851 convert (sizetype, gnu_upper_bound),
2852 create_range_type (gnu_string_index_type,
2853 gnu_lower_bound,
2854 gnu_upper_bound),
2855 gnat_entity);
2857 gnu_type
2858 = build_nonshared_array_type (gnat_to_gnu_type
2859 (Component_Type (gnat_entity)),
2860 gnu_index_type);
2861 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2862 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2863 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2865 break;
2867 /* Record Types and Subtypes
2869 The following fields are defined on record types:
2871 Has_Discriminants True if the record has discriminants
2872 First_Discriminant Points to head of list of discriminants
2873 First_Entity Points to head of list of fields
2874 Is_Tagged_Type True if the record is tagged
2876 Implementation of Ada records and discriminated records:
2878 A record type definition is transformed into the equivalent of a C
2879 struct definition. The fields that are the discriminants which are
2880 found in the Full_Type_Declaration node and the elements of the
2881 Component_List found in the Record_Type_Definition node. The
2882 Component_List can be a recursive structure since each Variant of
2883 the Variant_Part of the Component_List has a Component_List.
2885 Processing of a record type definition comprises starting the list of
2886 field declarations here from the discriminants and the calling the
2887 function components_to_record to add the rest of the fields from the
2888 component list and return the gnu type node. The function
2889 components_to_record will call itself recursively as it traverses
2890 the tree. */
2892 case E_Record_Type:
2893 if (Has_Complex_Representation (gnat_entity))
2895 gnu_type
2896 = build_complex_type
2897 (get_unpadded_type
2898 (Etype (Defining_Entity
2899 (First (Component_Items
2900 (Component_List
2901 (Type_Definition
2902 (Declaration_Node (gnat_entity)))))))));
2904 break;
2908 Node_Id full_definition = Declaration_Node (gnat_entity);
2909 Node_Id record_definition = Type_Definition (full_definition);
2910 Node_Id gnat_constr;
2911 Entity_Id gnat_field;
2912 tree gnu_field, gnu_field_list = NULL_TREE;
2913 tree gnu_get_parent;
2914 /* Set PACKED in keeping with gnat_to_gnu_field. */
2915 const int packed
2916 = Is_Packed (gnat_entity)
2918 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2919 ? -1
2920 : (Known_Alignment (gnat_entity)
2921 || (Strict_Alignment (gnat_entity)
2922 && Known_RM_Size (gnat_entity)))
2923 ? -2
2924 : 0;
2925 const bool has_discr = Has_Discriminants (gnat_entity);
2926 const bool has_rep = Has_Specified_Layout (gnat_entity);
2927 const bool is_extension
2928 = (Is_Tagged_Type (gnat_entity)
2929 && Nkind (record_definition) == N_Derived_Type_Definition);
2930 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2931 bool all_rep = has_rep;
2933 /* See if all fields have a rep clause. Stop when we find one
2934 that doesn't. */
2935 if (all_rep)
2936 for (gnat_field = First_Entity (gnat_entity);
2937 Present (gnat_field);
2938 gnat_field = Next_Entity (gnat_field))
2939 if ((Ekind (gnat_field) == E_Component
2940 || Ekind (gnat_field) == E_Discriminant)
2941 && No (Component_Clause (gnat_field)))
2943 all_rep = false;
2944 break;
2947 /* If this is a record extension, go a level further to find the
2948 record definition. Also, verify we have a Parent_Subtype. */
2949 if (is_extension)
2951 if (!type_annotate_only
2952 || Present (Record_Extension_Part (record_definition)))
2953 record_definition = Record_Extension_Part (record_definition);
2955 gcc_assert (type_annotate_only
2956 || Present (Parent_Subtype (gnat_entity)));
2959 /* Make a node for the record. If we are not defining the record,
2960 suppress expanding incomplete types. */
2961 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2962 TYPE_NAME (gnu_type) = gnu_entity_name;
2963 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2964 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2965 = Reverse_Storage_Order (gnat_entity);
2966 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2968 if (!definition)
2970 defer_incomplete_level++;
2971 this_deferred = true;
2974 /* If both a size and rep clause was specified, put the size in
2975 the record type now so that it can get the proper mode. */
2976 if (has_rep && Known_RM_Size (gnat_entity))
2977 TYPE_SIZE (gnu_type)
2978 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2980 /* Always set the alignment here so that it can be used to
2981 set the mode, if it is making the alignment stricter. If
2982 it is invalid, it will be checked again below. If this is to
2983 be Atomic, choose a default alignment of a word unless we know
2984 the size and it's smaller. */
2985 if (Known_Alignment (gnat_entity))
2986 TYPE_ALIGN (gnu_type)
2987 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2988 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2990 unsigned int size = UI_To_Int (Esize (gnat_entity));
2991 TYPE_ALIGN (gnu_type)
2992 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2994 /* If a type needs strict alignment, the minimum size will be the
2995 type size instead of the RM size (see validate_size). Cap the
2996 alignment, lest it causes this type size to become too large. */
2997 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2999 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
3000 unsigned int raw_align = raw_size & -raw_size;
3001 if (raw_align < BIGGEST_ALIGNMENT)
3002 TYPE_ALIGN (gnu_type) = raw_align;
3004 else
3005 TYPE_ALIGN (gnu_type) = 0;
3007 /* If we have a Parent_Subtype, make a field for the parent. If
3008 this record has rep clauses, force the position to zero. */
3009 if (Present (Parent_Subtype (gnat_entity)))
3011 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3012 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3013 tree gnu_parent;
3015 /* A major complexity here is that the parent subtype will
3016 reference our discriminants in its Stored_Constraint list.
3017 But those must reference the parent component of this record
3018 which is precisely of the parent subtype we have not built yet!
3019 To break the circle we first build a dummy COMPONENT_REF which
3020 represents the "get to the parent" operation and initialize
3021 each of those discriminants to a COMPONENT_REF of the above
3022 dummy parent referencing the corresponding discriminant of the
3023 base type of the parent subtype. */
3024 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3025 build0 (PLACEHOLDER_EXPR, gnu_type),
3026 build_decl (input_location,
3027 FIELD_DECL, NULL_TREE,
3028 gnu_dummy_parent_type),
3029 NULL_TREE);
3031 if (has_discr)
3032 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3033 Present (gnat_field);
3034 gnat_field = Next_Stored_Discriminant (gnat_field))
3035 if (Present (Corresponding_Discriminant (gnat_field)))
3037 tree gnu_field
3038 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3039 (gnat_field));
3040 save_gnu_tree
3041 (gnat_field,
3042 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3043 gnu_get_parent, gnu_field, NULL_TREE),
3044 true);
3047 /* Then we build the parent subtype. If it has discriminants but
3048 the type itself has unknown discriminants, this means that it
3049 doesn't contain information about how the discriminants are
3050 derived from those of the ancestor type, so it cannot be used
3051 directly. Instead it is built by cloning the parent subtype
3052 of the underlying record view of the type, for which the above
3053 derivation of discriminants has been made explicit. */
3054 if (Has_Discriminants (gnat_parent)
3055 && Has_Unknown_Discriminants (gnat_entity))
3057 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3059 /* If we are defining the type, the underlying record
3060 view must already have been elaborated at this point.
3061 Otherwise do it now as its parent subtype cannot be
3062 technically elaborated on its own. */
3063 if (definition)
3064 gcc_assert (present_gnu_tree (gnat_uview));
3065 else
3066 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3068 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3070 /* Substitute the "get to the parent" of the type for that
3071 of its underlying record view in the cloned type. */
3072 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3073 Present (gnat_field);
3074 gnat_field = Next_Stored_Discriminant (gnat_field))
3075 if (Present (Corresponding_Discriminant (gnat_field)))
3077 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3078 tree gnu_ref
3079 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3080 gnu_get_parent, gnu_field, NULL_TREE);
3081 gnu_parent
3082 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3085 else
3086 gnu_parent = gnat_to_gnu_type (gnat_parent);
3088 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3089 initially built. The discriminants must reference the fields
3090 of the parent subtype and not those of its base type for the
3091 placeholder machinery to properly work. */
3092 if (has_discr)
3094 /* The actual parent subtype is the full view. */
3095 if (IN (Ekind (gnat_parent), Private_Kind))
3097 if (Present (Full_View (gnat_parent)))
3098 gnat_parent = Full_View (gnat_parent);
3099 else
3100 gnat_parent = Underlying_Full_View (gnat_parent);
3103 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3104 Present (gnat_field);
3105 gnat_field = Next_Stored_Discriminant (gnat_field))
3106 if (Present (Corresponding_Discriminant (gnat_field)))
3108 Entity_Id field;
3109 for (field = First_Stored_Discriminant (gnat_parent);
3110 Present (field);
3111 field = Next_Stored_Discriminant (field))
3112 if (same_discriminant_p (gnat_field, field))
3113 break;
3114 gcc_assert (Present (field));
3115 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3116 = gnat_to_gnu_field_decl (field);
3120 /* The "get to the parent" COMPONENT_REF must be given its
3121 proper type... */
3122 TREE_TYPE (gnu_get_parent) = gnu_parent;
3124 /* ...and reference the _Parent field of this record. */
3125 gnu_field
3126 = create_field_decl (parent_name_id,
3127 gnu_parent, gnu_type,
3128 has_rep
3129 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3130 has_rep
3131 ? bitsize_zero_node : NULL_TREE,
3132 0, 1);
3133 DECL_INTERNAL_P (gnu_field) = 1;
3134 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3135 TYPE_FIELDS (gnu_type) = gnu_field;
3138 /* Make the fields for the discriminants and put them into the record
3139 unless it's an Unchecked_Union. */
3140 if (has_discr)
3141 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3142 Present (gnat_field);
3143 gnat_field = Next_Stored_Discriminant (gnat_field))
3145 /* If this is a record extension and this discriminant is the
3146 renaming of another discriminant, we've handled it above. */
3147 if (Present (Parent_Subtype (gnat_entity))
3148 && Present (Corresponding_Discriminant (gnat_field)))
3149 continue;
3151 gnu_field
3152 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3153 debug_info_p);
3155 /* Make an expression using a PLACEHOLDER_EXPR from the
3156 FIELD_DECL node just created and link that with the
3157 corresponding GNAT defining identifier. */
3158 save_gnu_tree (gnat_field,
3159 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3160 build0 (PLACEHOLDER_EXPR, gnu_type),
3161 gnu_field, NULL_TREE),
3162 true);
3164 if (!is_unchecked_union)
3166 DECL_CHAIN (gnu_field) = gnu_field_list;
3167 gnu_field_list = gnu_field;
3171 /* If we have a derived untagged type that renames discriminants in
3172 the root type, the (stored) discriminants are a just copy of the
3173 discriminants of the root type. This means that any constraints
3174 added by the renaming in the derivation are disregarded as far
3175 as the layout of the derived type is concerned. To rescue them,
3176 we change the type of the (stored) discriminants to a subtype
3177 with the bounds of the type of the visible discriminants. */
3178 if (has_discr
3179 && !is_extension
3180 && Stored_Constraint (gnat_entity) != No_Elist)
3181 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3182 gnat_constr != No_Elmt;
3183 gnat_constr = Next_Elmt (gnat_constr))
3184 if (Nkind (Node (gnat_constr)) == N_Identifier
3185 /* Ignore access discriminants. */
3186 && !Is_Access_Type (Etype (Node (gnat_constr)))
3187 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3189 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3190 tree gnu_discr_type, gnu_ref;
3192 /* If the scope of the discriminant is not the record type,
3193 this means that we're processing the implicit full view
3194 of a type derived from a private discriminated type: in
3195 this case, the Stored_Constraint list is simply copied
3196 from the partial view, see Build_Derived_Private_Type.
3197 So we need to retrieve the corresponding discriminant
3198 of the implicit full view, otherwise we will abort. */
3199 if (Scope (gnat_discr) != gnat_entity)
3201 Entity_Id field;
3202 for (field = First_Entity (gnat_entity);
3203 Present (field);
3204 field = Next_Entity (field))
3205 if (Ekind (field) == E_Discriminant
3206 && same_discriminant_p (gnat_discr, field))
3207 break;
3208 gcc_assert (Present (field));
3209 gnat_discr = field;
3212 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3213 gnu_ref
3214 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3215 NULL_TREE, 0);
3217 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3218 just above for one of the stored discriminants. */
3219 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3221 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3223 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3224 tree gnu_subtype
3225 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3226 ? make_unsigned_type (prec) : make_signed_type (prec);
3227 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3228 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3229 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3230 TYPE_MIN_VALUE (gnu_discr_type));
3231 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3232 TYPE_MAX_VALUE (gnu_discr_type));
3233 TREE_TYPE (gnu_ref)
3234 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3238 /* Add the fields into the record type and finish it up. */
3239 components_to_record (gnu_type, Component_List (record_definition),
3240 gnu_field_list, packed, definition, false,
3241 all_rep, is_unchecked_union,
3242 !Comes_From_Source (gnat_entity), debug_info_p,
3243 false, OK_To_Reorder_Components (gnat_entity),
3244 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3246 /* If it is passed by reference, force BLKmode to ensure that objects
3247 of this type will always be put in memory. */
3248 if (TYPE_MODE (gnu_type) != BLKmode
3249 && Is_By_Reference_Type (gnat_entity))
3250 SET_TYPE_MODE (gnu_type, BLKmode);
3252 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3254 /* Fill in locations of fields. */
3255 annotate_rep (gnat_entity, gnu_type);
3257 /* If there are any entities in the chain corresponding to components
3258 that we did not elaborate, ensure we elaborate their types if they
3259 are Itypes. */
3260 for (gnat_temp = First_Entity (gnat_entity);
3261 Present (gnat_temp);
3262 gnat_temp = Next_Entity (gnat_temp))
3263 if ((Ekind (gnat_temp) == E_Component
3264 || Ekind (gnat_temp) == E_Discriminant)
3265 && Is_Itype (Etype (gnat_temp))
3266 && !present_gnu_tree (gnat_temp))
3267 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3269 /* If this is a record type associated with an exception definition,
3270 equate its fields to those of the standard exception type. This
3271 will make it possible to convert between them. */
3272 if (gnu_entity_name == exception_data_name_id)
3274 tree gnu_std_field;
3275 for (gnu_field = TYPE_FIELDS (gnu_type),
3276 gnu_std_field = TYPE_FIELDS (except_type_node);
3277 gnu_field;
3278 gnu_field = DECL_CHAIN (gnu_field),
3279 gnu_std_field = DECL_CHAIN (gnu_std_field))
3280 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3281 gcc_assert (!gnu_std_field);
3284 break;
3286 case E_Class_Wide_Subtype:
3287 /* If an equivalent type is present, that is what we should use.
3288 Otherwise, fall through to handle this like a record subtype
3289 since it may have constraints. */
3290 if (gnat_equiv_type != gnat_entity)
3292 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3293 maybe_present = true;
3294 break;
3297 /* ... fall through ... */
3299 case E_Record_Subtype:
3300 /* If Cloned_Subtype is Present it means this record subtype has
3301 identical layout to that type or subtype and we should use
3302 that GCC type for this one. The front end guarantees that
3303 the component list is shared. */
3304 if (Present (Cloned_Subtype (gnat_entity)))
3306 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3307 NULL_TREE, 0);
3308 maybe_present = true;
3309 break;
3312 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3313 changing the type, make a new type with each field having the type of
3314 the field in the new subtype but the position computed by transforming
3315 every discriminant reference according to the constraints. We don't
3316 see any difference between private and non-private type here since
3317 derivations from types should have been deferred until the completion
3318 of the private type. */
3319 else
3321 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3322 tree gnu_base_type;
3324 if (!definition)
3326 defer_incomplete_level++;
3327 this_deferred = true;
3330 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3332 if (present_gnu_tree (gnat_entity))
3334 maybe_present = true;
3335 break;
3338 /* If this is a record subtype associated with a dispatch table,
3339 strip the suffix. This is necessary to make sure 2 different
3340 subtypes associated with the imported and exported views of a
3341 dispatch table are properly merged in LTO mode. */
3342 if (Is_Dispatch_Table_Entity (gnat_entity))
3344 char *p;
3345 Get_Encoded_Name (gnat_entity);
3346 p = strchr (Name_Buffer, '_');
3347 gcc_assert (p);
3348 strcpy (p+2, "dtS");
3349 gnu_entity_name = get_identifier (Name_Buffer);
3352 /* When the subtype has discriminants and these discriminants affect
3353 the initial shape it has inherited, factor them in. But for an
3354 Unchecked_Union (it must be an Itype), just return the type.
3355 We can't just test Is_Constrained because private subtypes without
3356 discriminants of types with discriminants with default expressions
3357 are Is_Constrained but aren't constrained! */
3358 if (IN (Ekind (gnat_base_type), Record_Kind)
3359 && !Is_Unchecked_Union (gnat_base_type)
3360 && !Is_For_Access_Subtype (gnat_entity)
3361 && Has_Discriminants (gnat_entity)
3362 && Is_Constrained (gnat_entity)
3363 && Stored_Constraint (gnat_entity) != No_Elist)
3365 vec<subst_pair> gnu_subst_list
3366 = build_subst_list (gnat_entity, gnat_base_type, definition);
3367 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3368 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3369 bool selected_variant = false, all_constant_pos = true;
3370 Entity_Id gnat_field;
3371 vec<variant_desc> gnu_variant_list;
3373 gnu_type = make_node (RECORD_TYPE);
3374 TYPE_NAME (gnu_type) = gnu_entity_name;
3375 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3376 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3377 = Reverse_Storage_Order (gnat_entity);
3378 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3380 /* Set the size, alignment and alias set of the new type to
3381 match that of the old one, doing required substitutions. */
3382 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3383 gnu_subst_list);
3385 if (TYPE_IS_PADDING_P (gnu_base_type))
3386 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3387 else
3388 gnu_unpad_base_type = gnu_base_type;
3390 /* Look for REP and variant parts in the base type. */
3391 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3392 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3394 /* If there is a variant part, we must compute whether the
3395 constraints statically select a particular variant. If
3396 so, we simply drop the qualified union and flatten the
3397 list of fields. Otherwise we'll build a new qualified
3398 union for the variants that are still relevant. */
3399 if (gnu_variant_part)
3401 variant_desc *v;
3402 unsigned int i;
3404 gnu_variant_list
3405 = build_variant_list (TREE_TYPE (gnu_variant_part),
3406 gnu_subst_list,
3407 vNULL);
3409 /* If all the qualifiers are unconditionally true, the
3410 innermost variant is statically selected. */
3411 selected_variant = true;
3412 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3413 if (!integer_onep (v->qual))
3415 selected_variant = false;
3416 break;
3419 /* Otherwise, create the new variants. */
3420 if (!selected_variant)
3421 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3423 tree old_variant = v->type;
3424 tree new_variant = make_node (RECORD_TYPE);
3425 tree suffix
3426 = concat_name (DECL_NAME (gnu_variant_part),
3427 IDENTIFIER_POINTER
3428 (DECL_NAME (v->field)));
3429 TYPE_NAME (new_variant)
3430 = concat_name (TYPE_NAME (gnu_type),
3431 IDENTIFIER_POINTER (suffix));
3432 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3433 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3434 copy_and_substitute_in_size (new_variant, old_variant,
3435 gnu_subst_list);
3436 v->new_type = new_variant;
3439 else
3441 gnu_variant_list.create (0);
3442 selected_variant = false;
3445 /* Make a list of fields and their position in the base type. */
3446 gnu_pos_list
3447 = build_position_list (gnu_unpad_base_type,
3448 gnu_variant_list.exists ()
3449 && !selected_variant,
3450 size_zero_node, bitsize_zero_node,
3451 BIGGEST_ALIGNMENT, NULL_TREE);
3453 /* Now go down every component in the subtype and compute its
3454 size and position from those of the component in the base
3455 type and from the constraints of the subtype. */
3456 for (gnat_field = First_Entity (gnat_entity);
3457 Present (gnat_field);
3458 gnat_field = Next_Entity (gnat_field))
3459 if ((Ekind (gnat_field) == E_Component
3460 || Ekind (gnat_field) == E_Discriminant)
3461 && !(Present (Corresponding_Discriminant (gnat_field))
3462 && Is_Tagged_Type (gnat_base_type))
3463 && Underlying_Type
3464 (Scope (Original_Record_Component (gnat_field)))
3465 == gnat_base_type)
3467 Name_Id gnat_name = Chars (gnat_field);
3468 Entity_Id gnat_old_field
3469 = Original_Record_Component (gnat_field);
3470 tree gnu_old_field
3471 = gnat_to_gnu_field_decl (gnat_old_field);
3472 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3473 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3474 tree gnu_cont_type, gnu_last = NULL_TREE;
3476 /* If the type is the same, retrieve the GCC type from the
3477 old field to take into account possible adjustments. */
3478 if (Etype (gnat_field) == Etype (gnat_old_field))
3479 gnu_field_type = TREE_TYPE (gnu_old_field);
3480 else
3481 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3483 /* If there was a component clause, the field types must be
3484 the same for the type and subtype, so copy the data from
3485 the old field to avoid recomputation here. Also if the
3486 field is justified modular and the optimization in
3487 gnat_to_gnu_field was applied. */
3488 if (Present (Component_Clause (gnat_old_field))
3489 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3490 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3491 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3492 == TREE_TYPE (gnu_old_field)))
3494 gnu_size = DECL_SIZE (gnu_old_field);
3495 gnu_field_type = TREE_TYPE (gnu_old_field);
3498 /* If the old field was packed and of constant size, we
3499 have to get the old size here, as it might differ from
3500 what the Etype conveys and the latter might overlap
3501 onto the following field. Try to arrange the type for
3502 possible better packing along the way. */
3503 else if (DECL_PACKED (gnu_old_field)
3504 && TREE_CODE (DECL_SIZE (gnu_old_field))
3505 == INTEGER_CST)
3507 gnu_size = DECL_SIZE (gnu_old_field);
3508 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3509 && !TYPE_FAT_POINTER_P (gnu_field_type)
3510 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3511 gnu_field_type
3512 = make_packable_type (gnu_field_type, true);
3515 else
3516 gnu_size = TYPE_SIZE (gnu_field_type);
3518 /* If the context of the old field is the base type or its
3519 REP part (if any), put the field directly in the new
3520 type; otherwise look up the context in the variant list
3521 and put the field either in the new type if there is a
3522 selected variant or in one of the new variants. */
3523 if (gnu_context == gnu_unpad_base_type
3524 || (gnu_rep_part
3525 && gnu_context == TREE_TYPE (gnu_rep_part)))
3526 gnu_cont_type = gnu_type;
3527 else
3529 variant_desc *v;
3530 unsigned int i;
3531 tree rep_part;
3533 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3534 if (gnu_context == v->type
3535 || ((rep_part = get_rep_part (v->type))
3536 && gnu_context == TREE_TYPE (rep_part)))
3537 break;
3538 if (v)
3540 if (selected_variant)
3541 gnu_cont_type = gnu_type;
3542 else
3543 gnu_cont_type = v->new_type;
3545 else
3546 /* The front-end may pass us "ghost" components if
3547 it fails to recognize that a constrained subtype
3548 is statically constrained. Discard them. */
3549 continue;
3552 /* Now create the new field modeled on the old one. */
3553 gnu_field
3554 = create_field_decl_from (gnu_old_field, gnu_field_type,
3555 gnu_cont_type, gnu_size,
3556 gnu_pos_list, gnu_subst_list);
3557 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3559 /* Put it in one of the new variants directly. */
3560 if (gnu_cont_type != gnu_type)
3562 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3563 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3566 /* To match the layout crafted in components_to_record,
3567 if this is the _Tag or _Parent field, put it before
3568 any other fields. */
3569 else if (gnat_name == Name_uTag
3570 || gnat_name == Name_uParent)
3571 gnu_field_list = chainon (gnu_field_list, gnu_field);
3573 /* Similarly, if this is the _Controller field, put
3574 it before the other fields except for the _Tag or
3575 _Parent field. */
3576 else if (gnat_name == Name_uController && gnu_last)
3578 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3579 DECL_CHAIN (gnu_last) = gnu_field;
3582 /* Otherwise, if this is a regular field, put it after
3583 the other fields. */
3584 else
3586 DECL_CHAIN (gnu_field) = gnu_field_list;
3587 gnu_field_list = gnu_field;
3588 if (!gnu_last)
3589 gnu_last = gnu_field;
3590 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3591 all_constant_pos = false;
3594 save_gnu_tree (gnat_field, gnu_field, false);
3597 /* If there is a variant list, a selected variant and the fields
3598 all have a constant position, put them in order of increasing
3599 position to match that of constant CONSTRUCTORs. Likewise if
3600 there is no variant list but a REP part, since the latter has
3601 been flattened in the process. */
3602 if (((gnu_variant_list.exists () && selected_variant)
3603 || (!gnu_variant_list.exists () && gnu_rep_part))
3604 && all_constant_pos)
3606 const int len = list_length (gnu_field_list);
3607 tree *field_arr = XALLOCAVEC (tree, len), t;
3608 int i;
3610 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3611 field_arr[i] = t;
3613 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3615 gnu_field_list = NULL_TREE;
3616 for (i = 0; i < len; i++)
3618 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3619 gnu_field_list = field_arr[i];
3623 /* If there is a variant list and no selected variant, we need
3624 to create the nest of variant parts from the old nest. */
3625 else if (gnu_variant_list.exists () && !selected_variant)
3627 tree new_variant_part
3628 = create_variant_part_from (gnu_variant_part,
3629 gnu_variant_list, gnu_type,
3630 gnu_pos_list, gnu_subst_list);
3631 DECL_CHAIN (new_variant_part) = gnu_field_list;
3632 gnu_field_list = new_variant_part;
3635 /* Now go through the entities again looking for Itypes that
3636 we have not elaborated but should (e.g., Etypes of fields
3637 that have Original_Components). */
3638 for (gnat_field = First_Entity (gnat_entity);
3639 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3640 if ((Ekind (gnat_field) == E_Discriminant
3641 || Ekind (gnat_field) == E_Component)
3642 && !present_gnu_tree (Etype (gnat_field)))
3643 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3645 /* Do not emit debug info for the type yet since we're going to
3646 modify it below. */
3647 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3648 false);
3649 compute_record_mode (gnu_type);
3651 /* See the E_Record_Type case for the rationale. */
3652 if (TYPE_MODE (gnu_type) != BLKmode
3653 && Is_By_Reference_Type (gnat_entity))
3654 SET_TYPE_MODE (gnu_type, BLKmode);
3656 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3658 /* Fill in locations of fields. */
3659 annotate_rep (gnat_entity, gnu_type);
3661 /* If debugging information is being written for the type, write
3662 a record that shows what we are a subtype of and also make a
3663 variable that indicates our size, if still variable. */
3664 if (debug_info_p)
3666 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3667 tree gnu_unpad_base_name
3668 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3669 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3671 TYPE_NAME (gnu_subtype_marker)
3672 = create_concat_name (gnat_entity, "XVS");
3673 finish_record_type (gnu_subtype_marker,
3674 create_field_decl (gnu_unpad_base_name,
3675 build_reference_type
3676 (gnu_unpad_base_type),
3677 gnu_subtype_marker,
3678 NULL_TREE, NULL_TREE,
3679 0, 0),
3680 0, true);
3682 add_parallel_type (gnu_type, gnu_subtype_marker);
3684 if (definition
3685 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3686 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3687 TYPE_SIZE_UNIT (gnu_subtype_marker)
3688 = create_var_decl (create_concat_name (gnat_entity,
3689 "XVZ"),
3690 NULL_TREE, sizetype, gnu_size_unit,
3691 false, false, false, false, NULL,
3692 gnat_entity);
3695 gnu_variant_list.release ();
3696 gnu_subst_list.release ();
3698 /* Now we can finalize it. */
3699 rest_of_record_type_compilation (gnu_type);
3702 /* Otherwise, go down all the components in the new type and make
3703 them equivalent to those in the base type. */
3704 else
3706 gnu_type = gnu_base_type;
3708 for (gnat_temp = First_Entity (gnat_entity);
3709 Present (gnat_temp);
3710 gnat_temp = Next_Entity (gnat_temp))
3711 if ((Ekind (gnat_temp) == E_Discriminant
3712 && !Is_Unchecked_Union (gnat_base_type))
3713 || Ekind (gnat_temp) == E_Component)
3714 save_gnu_tree (gnat_temp,
3715 gnat_to_gnu_field_decl
3716 (Original_Record_Component (gnat_temp)),
3717 false);
3720 break;
3722 case E_Access_Subprogram_Type:
3723 /* Use the special descriptor type for dispatch tables if needed,
3724 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3725 Note that we are only required to do so for static tables in
3726 order to be compatible with the C++ ABI, but Ada 2005 allows
3727 to extend library level tagged types at the local level so
3728 we do it in the non-static case as well. */
3729 if (TARGET_VTABLE_USES_DESCRIPTORS
3730 && Is_Dispatch_Table_Entity (gnat_entity))
3732 gnu_type = fdesc_type_node;
3733 gnu_size = TYPE_SIZE (gnu_type);
3734 break;
3737 /* ... fall through ... */
3739 case E_Anonymous_Access_Subprogram_Type:
3740 /* If we are not defining this entity, and we have incomplete
3741 entities being processed above us, make a dummy type and
3742 fill it in later. */
3743 if (!definition && defer_incomplete_level != 0)
3745 struct incomplete *p = XNEW (struct incomplete);
3747 gnu_type
3748 = build_pointer_type
3749 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3750 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3751 !Comes_From_Source (gnat_entity),
3752 debug_info_p, gnat_entity);
3753 this_made_decl = true;
3754 gnu_type = TREE_TYPE (gnu_decl);
3755 save_gnu_tree (gnat_entity, gnu_decl, false);
3756 saved = true;
3758 p->old_type = TREE_TYPE (gnu_type);
3759 p->full_type = Directly_Designated_Type (gnat_entity);
3760 p->next = defer_incomplete_list;
3761 defer_incomplete_list = p;
3762 break;
3765 /* ... fall through ... */
3767 case E_Allocator_Type:
3768 case E_Access_Type:
3769 case E_Access_Attribute_Type:
3770 case E_Anonymous_Access_Type:
3771 case E_General_Access_Type:
3773 /* The designated type and its equivalent type for gigi. */
3774 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3775 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3776 /* Whether it comes from a limited with. */
3777 bool is_from_limited_with
3778 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3779 && From_Limited_With (gnat_desig_equiv));
3780 /* The "full view" of the designated type. If this is an incomplete
3781 entity from a limited with, treat its non-limited view as the full
3782 view. Otherwise, if this is an incomplete or private type, use the
3783 full view. In the former case, we might point to a private type,
3784 in which case, we need its full view. Also, we want to look at the
3785 actual type used for the representation, so this takes a total of
3786 three steps. */
3787 Entity_Id gnat_desig_full_direct_first
3788 = (is_from_limited_with
3789 ? Non_Limited_View (gnat_desig_equiv)
3790 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3791 ? Full_View (gnat_desig_equiv) : Empty));
3792 Entity_Id gnat_desig_full_direct
3793 = ((is_from_limited_with
3794 && Present (gnat_desig_full_direct_first)
3795 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3796 ? Full_View (gnat_desig_full_direct_first)
3797 : gnat_desig_full_direct_first);
3798 Entity_Id gnat_desig_full
3799 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3800 /* The type actually used to represent the designated type, either
3801 gnat_desig_full or gnat_desig_equiv. */
3802 Entity_Id gnat_desig_rep;
3803 /* True if this is a pointer to an unconstrained array. */
3804 bool is_unconstrained_array;
3805 /* We want to know if we'll be seeing the freeze node for any
3806 incomplete type we may be pointing to. */
3807 bool in_main_unit
3808 = (Present (gnat_desig_full)
3809 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3810 : In_Extended_Main_Code_Unit (gnat_desig_type));
3811 /* True if we make a dummy type here. */
3812 bool made_dummy = false;
3813 /* The mode to be used for the pointer type. */
3814 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3815 /* The GCC type used for the designated type. */
3816 tree gnu_desig_type = NULL_TREE;
3818 if (!targetm.valid_pointer_mode (p_mode))
3819 p_mode = ptr_mode;
3821 /* If either the designated type or its full view is an unconstrained
3822 array subtype, replace it with the type it's a subtype of. This
3823 avoids problems with multiple copies of unconstrained array types.
3824 Likewise, if the designated type is a subtype of an incomplete
3825 record type, use the parent type to avoid order of elaboration
3826 issues. This can lose some code efficiency, but there is no
3827 alternative. */
3828 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3829 && !Is_Constrained (gnat_desig_equiv))
3830 gnat_desig_equiv = Etype (gnat_desig_equiv);
3831 if (Present (gnat_desig_full)
3832 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3833 && !Is_Constrained (gnat_desig_full))
3834 || (Ekind (gnat_desig_full) == E_Record_Subtype
3835 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3836 gnat_desig_full = Etype (gnat_desig_full);
3838 /* Set the type that's actually the representation of the designated
3839 type and also flag whether we have a unconstrained array. */
3840 gnat_desig_rep
3841 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3842 is_unconstrained_array
3843 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3845 /* If we are pointing to an incomplete type whose completion is an
3846 unconstrained array, make dummy fat and thin pointer types to it.
3847 Likewise if the type itself is dummy or an unconstrained array. */
3848 if (is_unconstrained_array
3849 && (Present (gnat_desig_full)
3850 || (present_gnu_tree (gnat_desig_equiv)
3851 && TYPE_IS_DUMMY_P
3852 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3853 || (!in_main_unit
3854 && defer_incomplete_level != 0
3855 && !present_gnu_tree (gnat_desig_equiv))
3856 || (in_main_unit
3857 && is_from_limited_with
3858 && Present (Freeze_Node (gnat_desig_equiv)))))
3860 if (present_gnu_tree (gnat_desig_rep))
3861 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3862 else
3864 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3865 made_dummy = true;
3868 /* If the call above got something that has a pointer, the pointer
3869 is our type. This could have happened either because the type
3870 was elaborated or because somebody else executed the code. */
3871 if (!TYPE_POINTER_TO (gnu_desig_type))
3872 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3873 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3876 /* If we already know what the full type is, use it. */
3877 else if (Present (gnat_desig_full)
3878 && present_gnu_tree (gnat_desig_full))
3879 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3881 /* Get the type of the thing we are to point to and build a pointer to
3882 it. If it is a reference to an incomplete or private type with a
3883 full view that is a record, make a dummy type node and get the
3884 actual type later when we have verified it is safe. */
3885 else if ((!in_main_unit
3886 && !present_gnu_tree (gnat_desig_equiv)
3887 && Present (gnat_desig_full)
3888 && !present_gnu_tree (gnat_desig_full)
3889 && Is_Record_Type (gnat_desig_full))
3890 /* Likewise if we are pointing to a record or array and we are
3891 to defer elaborating incomplete types. We do this as this
3892 access type may be the full view of a private type. Note
3893 that the unconstrained array case is handled above. */
3894 || ((!in_main_unit || imported_p)
3895 && defer_incomplete_level != 0
3896 && !present_gnu_tree (gnat_desig_equiv)
3897 && (Is_Record_Type (gnat_desig_rep)
3898 || Is_Array_Type (gnat_desig_rep)))
3899 /* If this is a reference from a limited_with type back to our
3900 main unit and there's a freeze node for it, either we have
3901 already processed the declaration and made the dummy type,
3902 in which case we just reuse the latter, or we have not yet,
3903 in which case we make the dummy type and it will be reused
3904 when the declaration is finally processed. In both cases,
3905 the pointer eventually created below will be automatically
3906 adjusted when the freeze node is processed. Note that the
3907 unconstrained array case is handled above. */
3908 || (in_main_unit
3909 && is_from_limited_with
3910 && Present (Freeze_Node (gnat_desig_rep))))
3912 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3913 made_dummy = true;
3916 /* Otherwise handle the case of a pointer to itself. */
3917 else if (gnat_desig_equiv == gnat_entity)
3919 gnu_type
3920 = build_pointer_type_for_mode (void_type_node, p_mode,
3921 No_Strict_Aliasing (gnat_entity));
3922 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3925 /* If expansion is disabled, the equivalent type of a concurrent type
3926 is absent, so build a dummy pointer type. */
3927 else if (type_annotate_only && No (gnat_desig_equiv))
3928 gnu_type = ptr_void_type_node;
3930 /* Finally, handle the default case where we can just elaborate our
3931 designated type. */
3932 else
3933 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3935 /* It is possible that a call to gnat_to_gnu_type above resolved our
3936 type. If so, just return it. */
3937 if (present_gnu_tree (gnat_entity))
3939 maybe_present = true;
3940 break;
3943 /* If we haven't done it yet, build the pointer type the usual way. */
3944 if (!gnu_type)
3946 /* Modify the designated type if we are pointing only to constant
3947 objects, but don't do it for unconstrained arrays. */
3948 if (Is_Access_Constant (gnat_entity)
3949 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3951 gnu_desig_type
3952 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3954 /* Some extra processing is required if we are building a
3955 pointer to an incomplete type (in the GCC sense). We might
3956 have such a type if we just made a dummy, or directly out
3957 of the call to gnat_to_gnu_type above if we are processing
3958 an access type for a record component designating the
3959 record type itself. */
3960 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3962 /* We must ensure that the pointer to variant we make will
3963 be processed by update_pointer_to when the initial type
3964 is completed. Pretend we made a dummy and let further
3965 processing act as usual. */
3966 made_dummy = true;
3968 /* We must ensure that update_pointer_to will not retrieve
3969 the dummy variant when building a properly qualified
3970 version of the complete type. We take advantage of the
3971 fact that get_qualified_type is requiring TYPE_NAMEs to
3972 match to influence build_qualified_type and then also
3973 update_pointer_to here. */
3974 TYPE_NAME (gnu_desig_type)
3975 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3979 gnu_type
3980 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3981 No_Strict_Aliasing (gnat_entity));
3984 /* If we are not defining this object and we have made a dummy pointer,
3985 save our current definition, evaluate the actual type, and replace
3986 the tentative type we made with the actual one. If we are to defer
3987 actually looking up the actual type, make an entry in the deferred
3988 list. If this is from a limited with, we may have to defer to the
3989 end of the current unit. */
3990 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3992 tree gnu_old_desig_type;
3994 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3996 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3997 if (esize == POINTER_SIZE)
3998 gnu_type = build_pointer_type
3999 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
4001 else
4002 gnu_old_desig_type = TREE_TYPE (gnu_type);
4004 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4005 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4006 !Comes_From_Source (gnat_entity),
4007 debug_info_p, gnat_entity);
4008 this_made_decl = true;
4009 gnu_type = TREE_TYPE (gnu_decl);
4010 save_gnu_tree (gnat_entity, gnu_decl, false);
4011 saved = true;
4013 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4014 update gnu_old_desig_type directly, in which case it will not be
4015 a dummy type any more when we get into update_pointer_to.
4017 This can happen e.g. when the designated type is a record type,
4018 because their elaboration starts with an initial node from
4019 make_dummy_type, which may be the same node as the one we got.
4021 Besides, variants of this non-dummy type might have been created
4022 along the way. update_pointer_to is expected to properly take
4023 care of those situations. */
4024 if (defer_incomplete_level == 0 && !is_from_limited_with)
4026 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4027 gnat_to_gnu_type (gnat_desig_equiv));
4029 else
4031 struct incomplete *p = XNEW (struct incomplete);
4032 struct incomplete **head
4033 = (is_from_limited_with
4034 ? &defer_limited_with : &defer_incomplete_list);
4035 p->old_type = gnu_old_desig_type;
4036 p->full_type = gnat_desig_equiv;
4037 p->next = *head;
4038 *head = p;
4042 break;
4044 case E_Access_Protected_Subprogram_Type:
4045 case E_Anonymous_Access_Protected_Subprogram_Type:
4046 if (type_annotate_only && No (gnat_equiv_type))
4047 gnu_type = ptr_void_type_node;
4048 else
4050 /* The run-time representation is the equivalent type. */
4051 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4052 maybe_present = true;
4055 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4056 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4057 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4058 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4059 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4060 NULL_TREE, 0);
4062 break;
4064 case E_Access_Subtype:
4066 /* We treat this as identical to its base type; any constraint is
4067 meaningful only to the front-end.
4069 The designated type must be elaborated as well, if it does
4070 not have its own freeze node. Designated (sub)types created
4071 for constrained components of records with discriminants are
4072 not frozen by the front-end and thus not elaborated by gigi,
4073 because their use may appear before the base type is frozen,
4074 and because it is not clear that they are needed anywhere in
4075 gigi. With the current model, there is no correct place where
4076 they could be elaborated. */
4078 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4079 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4080 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4081 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4082 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4084 /* If we are not defining this entity, and we have incomplete
4085 entities being processed above us, make a dummy type and
4086 elaborate it later. */
4087 if (!definition && defer_incomplete_level != 0)
4089 struct incomplete *p = XNEW (struct incomplete);
4091 p->old_type
4092 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4093 p->full_type = Directly_Designated_Type (gnat_entity);
4094 p->next = defer_incomplete_list;
4095 defer_incomplete_list = p;
4097 else if (!IN (Ekind (Base_Type
4098 (Directly_Designated_Type (gnat_entity))),
4099 Incomplete_Or_Private_Kind))
4100 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4101 NULL_TREE, 0);
4104 maybe_present = true;
4105 break;
4107 /* Subprogram Entities
4109 The following access functions are defined for subprograms:
4111 Etype Return type or Standard_Void_Type.
4112 First_Formal The first formal parameter.
4113 Is_Imported Indicates that the subprogram has appeared in
4114 an INTERFACE or IMPORT pragma. For now we
4115 assume that the external language is C.
4116 Is_Exported Likewise but for an EXPORT pragma.
4117 Is_Inlined True if the subprogram is to be inlined.
4119 Each parameter is first checked by calling must_pass_by_ref on its
4120 type to determine if it is passed by reference. For parameters which
4121 are copied in, if they are Ada In Out or Out parameters, their return
4122 value becomes part of a record which becomes the return type of the
4123 function (C function - note that this applies only to Ada procedures
4124 so there is no Ada return type). Additional code to store back the
4125 parameters will be generated on the caller side. This transformation
4126 is done here, not in the front-end.
4128 The intended result of the transformation can be seen from the
4129 equivalent source rewritings that follow:
4131 struct temp {int a,b};
4132 procedure P (A,B: In Out ...) is temp P (int A,B)
4133 begin {
4134 .. ..
4135 end P; return {A,B};
4138 temp t;
4139 P(X,Y); t = P(X,Y);
4140 X = t.a , Y = t.b;
4142 For subprogram types we need to perform mainly the same conversions to
4143 GCC form that are needed for procedures and function declarations. The
4144 only difference is that at the end, we make a type declaration instead
4145 of a function declaration. */
4147 case E_Subprogram_Type:
4148 case E_Function:
4149 case E_Procedure:
4151 /* The type returned by a function or else Standard_Void_Type for a
4152 procedure. */
4153 Entity_Id gnat_return_type = Etype (gnat_entity);
4154 tree gnu_return_type;
4155 /* The first GCC parameter declaration (a PARM_DECL node). The
4156 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4157 actually is the head of this parameter list. */
4158 tree gnu_param_list = NULL_TREE;
4159 /* Non-null for subprograms containing parameters passed by copy-in
4160 copy-out (Ada In Out or Out parameters not passed by reference),
4161 in which case it is the list of nodes used to specify the values
4162 of the In Out/Out parameters that are returned as a record upon
4163 procedure return. The TREE_PURPOSE of an element of this list is
4164 a field of the record and the TREE_VALUE is the PARM_DECL
4165 corresponding to that field. This list will be saved in the
4166 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4167 tree gnu_cico_list = NULL_TREE;
4168 /* List of fields in return type of procedure with copy-in copy-out
4169 parameters. */
4170 tree gnu_field_list = NULL_TREE;
4171 /* If an import pragma asks to map this subprogram to a GCC builtin,
4172 this is the builtin DECL node. */
4173 tree gnu_builtin_decl = NULL_TREE;
4174 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4175 Entity_Id gnat_param;
4176 enum inline_status_t inline_status
4177 = Has_Pragma_No_Inline (gnat_entity)
4178 ? is_suppressed
4179 : Has_Pragma_Inline_Always (gnat_entity)
4180 ? is_required
4181 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4182 bool public_flag = Is_Public (gnat_entity) || imported_p;
4183 /* Subprograms marked both Intrinsic and Always_Inline need not
4184 have a body of their own. */
4185 bool extern_flag
4186 = ((Is_Public (gnat_entity) && !definition)
4187 || imported_p
4188 || (Convention (gnat_entity) == Convention_Intrinsic
4189 && Has_Pragma_Inline_Always (gnat_entity)));
4190 bool artificial_flag = !Comes_From_Source (gnat_entity);
4191 /* The semantics of "pure" in Ada essentially matches that of "const"
4192 in the back-end. In particular, both properties are orthogonal to
4193 the "nothrow" property if the EH circuitry is explicit in the
4194 internal representation of the back-end. If we are to completely
4195 hide the EH circuitry from it, we need to declare that calls to pure
4196 Ada subprograms that can throw have side effects since they can
4197 trigger an "abnormal" transfer of control flow; thus they can be
4198 neither "const" nor "pure" in the back-end sense. */
4199 bool const_flag
4200 = (Exception_Mechanism == Back_End_Exceptions
4201 && Is_Pure (gnat_entity));
4202 bool volatile_flag = No_Return (gnat_entity);
4203 bool return_by_direct_ref_p = false;
4204 bool return_by_invisi_ref_p = false;
4205 bool return_unconstrained_p = false;
4206 int parmnum;
4208 /* A parameter may refer to this type, so defer completion of any
4209 incomplete types. */
4210 if (kind == E_Subprogram_Type && !definition)
4212 defer_incomplete_level++;
4213 this_deferred = true;
4216 /* If the subprogram has an alias, it is probably inherited, so
4217 we can use the original one. If the original "subprogram"
4218 is actually an enumeration literal, it may be the first use
4219 of its type, so we must elaborate that type now. */
4220 if (Present (Alias (gnat_entity)))
4222 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4223 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4225 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4227 /* Elaborate any Itypes in the parameters of this entity. */
4228 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4229 Present (gnat_temp);
4230 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4231 if (Is_Itype (Etype (gnat_temp)))
4232 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4234 break;
4237 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4238 corresponding DECL node. Proper generation of calls later on need
4239 proper parameter associations so we don't "break;" here. */
4240 if (Convention (gnat_entity) == Convention_Intrinsic
4241 && Present (Interface_Name (gnat_entity)))
4243 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4245 /* Inability to find the builtin decl most often indicates a
4246 genuine mistake, but imports of unregistered intrinsics are
4247 sometimes issued on purpose to allow hooking in alternate
4248 bodies. We post a warning conditioned on Wshadow in this case,
4249 to let developers be notified on demand without risking false
4250 positives with common default sets of options. */
4252 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4253 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4256 /* ??? What if we don't find the builtin node above ? warn ? err ?
4257 In the current state we neither warn nor err, and calls will just
4258 be handled as for regular subprograms. */
4260 /* Look into the return type and get its associated GCC tree. If it
4261 is not void, compute various flags for the subprogram type. */
4262 if (Ekind (gnat_return_type) == E_Void)
4263 gnu_return_type = void_type_node;
4264 else
4266 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4267 context may now appear in parameter and result profiles. If
4268 we are only annotating types, break circularities here. */
4269 if (type_annotate_only
4270 && is_from_limited_with_of_main (gnat_return_type))
4271 gnu_return_type = ptr_void_type_node;
4272 else
4273 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4275 /* If this function returns by reference, make the actual return
4276 type the pointer type and make a note of that. */
4277 if (Returns_By_Ref (gnat_entity))
4279 gnu_return_type = build_pointer_type (gnu_return_type);
4280 return_by_direct_ref_p = true;
4283 /* If the return type is an unconstrained array type, the return
4284 value will be allocated on the secondary stack so the actual
4285 return type is the fat pointer type. */
4286 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4288 gnu_return_type = TREE_TYPE (gnu_return_type);
4289 return_unconstrained_p = true;
4292 /* Likewise, if the return type requires a transient scope, the
4293 return value will also be allocated on the secondary stack so
4294 the actual return type is the pointer type. */
4295 else if (Requires_Transient_Scope (gnat_return_type))
4297 gnu_return_type = build_pointer_type (gnu_return_type);
4298 return_unconstrained_p = true;
4301 /* If the Mechanism is By_Reference, ensure this function uses the
4302 target's by-invisible-reference mechanism, which may not be the
4303 same as above (e.g. it might be passing an extra parameter). */
4304 else if (kind == E_Function
4305 && Mechanism (gnat_entity) == By_Reference)
4306 return_by_invisi_ref_p = true;
4308 /* Likewise, if the return type is itself By_Reference. */
4309 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4310 return_by_invisi_ref_p = true;
4312 /* If the type is a padded type and the underlying type would not
4313 be passed by reference or the function has a foreign convention,
4314 return the underlying type. */
4315 else if (TYPE_IS_PADDING_P (gnu_return_type)
4316 && (!default_pass_by_ref
4317 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4318 || Has_Foreign_Convention (gnat_entity)))
4319 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4321 /* If the return type is unconstrained, that means it must have a
4322 maximum size. Use the padded type as the effective return type.
4323 And ensure the function uses the target's by-invisible-reference
4324 mechanism to avoid copying too much data when it returns. */
4325 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4327 tree orig_type = gnu_return_type;
4329 gnu_return_type
4330 = maybe_pad_type (gnu_return_type,
4331 max_size (TYPE_SIZE (gnu_return_type),
4332 true),
4333 0, gnat_entity, false, false, false, true);
4335 /* Declare it now since it will never be declared otherwise.
4336 This is necessary to ensure that its subtrees are properly
4337 marked. */
4338 if (gnu_return_type != orig_type
4339 && !DECL_P (TYPE_NAME (gnu_return_type)))
4340 create_type_decl (TYPE_NAME (gnu_return_type),
4341 gnu_return_type, true, debug_info_p,
4342 gnat_entity);
4344 return_by_invisi_ref_p = true;
4347 /* If the return type has a size that overflows, we cannot have
4348 a function that returns that type. This usage doesn't make
4349 sense anyway, so give an error here. */
4350 if (TYPE_SIZE_UNIT (gnu_return_type)
4351 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4352 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4354 post_error ("cannot return type whose size overflows",
4355 gnat_entity);
4356 gnu_return_type = copy_node (gnu_return_type);
4357 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4358 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4359 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4360 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4364 /* Loop over the parameters and get their associated GCC tree. While
4365 doing this, build a copy-in copy-out structure if we need one. */
4366 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4367 Present (gnat_param);
4368 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4370 Entity_Id gnat_param_type = Etype (gnat_param);
4371 tree gnu_param_name = get_entity_name (gnat_param);
4372 tree gnu_param_type, gnu_param, gnu_field;
4373 Mechanism_Type mech = Mechanism (gnat_param);
4374 bool copy_in_copy_out = false, fake_param_type;
4376 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4377 context may now appear in parameter and result profiles. If
4378 we are only annotating types, break circularities here. */
4379 if (type_annotate_only
4380 && is_from_limited_with_of_main (gnat_param_type))
4382 gnu_param_type = ptr_void_type_node;
4383 fake_param_type = true;
4385 else
4387 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4388 fake_param_type = false;
4391 /* Builtins are expanded inline and there is no real call sequence
4392 involved. So the type expected by the underlying expander is
4393 always the type of each argument "as is". */
4394 if (gnu_builtin_decl)
4395 mech = By_Copy;
4396 /* Handle the first parameter of a valued procedure specially. */
4397 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4398 mech = By_Copy_Return;
4399 /* Otherwise, see if a Mechanism was supplied that forced this
4400 parameter to be passed one way or another. */
4401 else if (mech == Default
4402 || mech == By_Copy
4403 || mech == By_Reference)
4405 else if (mech > 0)
4407 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4408 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4409 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4410 mech))
4411 mech = By_Reference;
4412 else
4413 mech = By_Copy;
4415 else
4417 post_error ("unsupported mechanism for&", gnat_param);
4418 mech = Default;
4421 /* Do not call gnat_to_gnu_param for a fake parameter type since
4422 it will try to use the real type again. */
4423 if (fake_param_type)
4425 if (Ekind (gnat_param) == E_Out_Parameter)
4426 gnu_param = NULL_TREE;
4427 else
4429 gnu_param
4430 = create_param_decl (gnu_param_name, gnu_param_type,
4431 false);
4432 Set_Mechanism (gnat_param,
4433 mech == Default ? By_Copy : mech);
4434 if (Ekind (gnat_param) == E_In_Out_Parameter)
4435 copy_in_copy_out = true;
4438 else
4439 gnu_param
4440 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4441 Has_Foreign_Convention (gnat_entity),
4442 &copy_in_copy_out);
4444 /* We are returned either a PARM_DECL or a type if no parameter
4445 needs to be passed; in either case, adjust the type. */
4446 if (DECL_P (gnu_param))
4447 gnu_param_type = TREE_TYPE (gnu_param);
4448 else
4450 gnu_param_type = gnu_param;
4451 gnu_param = NULL_TREE;
4454 /* The failure of this assertion will very likely come from an
4455 order of elaboration issue for the type of the parameter. */
4456 gcc_assert (kind == E_Subprogram_Type
4457 || !TYPE_IS_DUMMY_P (gnu_param_type)
4458 || type_annotate_only);
4460 if (gnu_param)
4462 gnu_param_list = chainon (gnu_param, gnu_param_list);
4463 Sloc_to_locus (Sloc (gnat_param),
4464 &DECL_SOURCE_LOCATION (gnu_param));
4465 save_gnu_tree (gnat_param, gnu_param, false);
4467 /* If a parameter is a pointer, this function may modify
4468 memory through it and thus shouldn't be considered
4469 a const function. Also, the memory may be modified
4470 between two calls, so they can't be CSE'ed. The latter
4471 case also handles by-ref parameters. */
4472 if (POINTER_TYPE_P (gnu_param_type)
4473 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4474 const_flag = false;
4477 if (copy_in_copy_out)
4479 if (!gnu_cico_list)
4481 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4483 /* If this is a function, we also need a field for the
4484 return value to be placed. */
4485 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4487 gnu_field
4488 = create_field_decl (get_identifier ("RETVAL"),
4489 gnu_return_type,
4490 gnu_new_ret_type, NULL_TREE,
4491 NULL_TREE, 0, 0);
4492 Sloc_to_locus (Sloc (gnat_entity),
4493 &DECL_SOURCE_LOCATION (gnu_field));
4494 gnu_field_list = gnu_field;
4495 gnu_cico_list
4496 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4499 gnu_return_type = gnu_new_ret_type;
4500 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4501 /* Set a default alignment to speed up accesses. But we
4502 shouldn't increase the size of the structure too much,
4503 lest it doesn't fit in return registers anymore. */
4504 TYPE_ALIGN (gnu_return_type)
4505 = get_mode_alignment (ptr_mode);
4508 gnu_field
4509 = create_field_decl (gnu_param_name, gnu_param_type,
4510 gnu_return_type, NULL_TREE, NULL_TREE,
4511 0, 0);
4512 Sloc_to_locus (Sloc (gnat_param),
4513 &DECL_SOURCE_LOCATION (gnu_field));
4514 DECL_CHAIN (gnu_field) = gnu_field_list;
4515 gnu_field_list = gnu_field;
4516 gnu_cico_list
4517 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4521 if (gnu_cico_list)
4523 /* If we have a CICO list but it has only one entry, we convert
4524 this function into a function that returns this object. */
4525 if (list_length (gnu_cico_list) == 1)
4526 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4528 /* Do not finalize the return type if the subprogram is stubbed
4529 since structures are incomplete for the back-end. */
4530 else if (Convention (gnat_entity) != Convention_Stubbed)
4532 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4533 0, false);
4535 /* Try to promote the mode of the return type if it is passed
4536 in registers, again to speed up accesses. */
4537 if (TYPE_MODE (gnu_return_type) == BLKmode
4538 && !targetm.calls.return_in_memory (gnu_return_type,
4539 NULL_TREE))
4541 unsigned int size
4542 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4543 unsigned int i = BITS_PER_UNIT;
4544 machine_mode mode;
4546 while (i < size)
4547 i <<= 1;
4548 mode = mode_for_size (i, MODE_INT, 0);
4549 if (mode != BLKmode)
4551 SET_TYPE_MODE (gnu_return_type, mode);
4552 TYPE_ALIGN (gnu_return_type)
4553 = GET_MODE_ALIGNMENT (mode);
4554 TYPE_SIZE (gnu_return_type)
4555 = bitsize_int (GET_MODE_BITSIZE (mode));
4556 TYPE_SIZE_UNIT (gnu_return_type)
4557 = size_int (GET_MODE_SIZE (mode));
4561 if (debug_info_p)
4562 rest_of_record_type_compilation (gnu_return_type);
4566 /* Deal with platform-specific calling conventions. */
4567 if (Has_Stdcall_Convention (gnat_entity))
4568 prepend_one_attribute
4569 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4570 get_identifier ("stdcall"), NULL_TREE,
4571 gnat_entity);
4572 else if (Has_Thiscall_Convention (gnat_entity))
4573 prepend_one_attribute
4574 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4575 get_identifier ("thiscall"), NULL_TREE,
4576 gnat_entity);
4578 /* If we should request stack realignment for a foreign convention
4579 subprogram, do so. Note that this applies to task entry points
4580 in particular. */
4581 if (FOREIGN_FORCE_REALIGN_STACK
4582 && Has_Foreign_Convention (gnat_entity))
4583 prepend_one_attribute
4584 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4585 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4586 gnat_entity);
4588 /* Deal with a pragma Linker_Section on a subprogram. */
4589 if ((kind == E_Function || kind == E_Procedure)
4590 && Present (Linker_Section_Pragma (gnat_entity)))
4591 prepend_one_attribute_pragma (&attr_list,
4592 Linker_Section_Pragma (gnat_entity));
4594 /* The lists have been built in reverse. */
4595 gnu_param_list = nreverse (gnu_param_list);
4596 gnu_cico_list = nreverse (gnu_cico_list);
4598 if (kind == E_Function)
4599 Set_Mechanism (gnat_entity, return_unconstrained_p
4600 || return_by_direct_ref_p
4601 || return_by_invisi_ref_p
4602 ? By_Reference : By_Copy);
4603 gnu_type
4604 = create_subprog_type (gnu_return_type, gnu_param_list,
4605 gnu_cico_list, return_unconstrained_p,
4606 return_by_direct_ref_p,
4607 return_by_invisi_ref_p);
4609 /* A procedure (something that doesn't return anything) shouldn't be
4610 considered const since there would be no reason for calling such a
4611 subprogram. Note that procedures with Out (or In Out) parameters
4612 have already been converted into a function with a return type.
4613 Similarly, if the function returns an unconstrained type, then the
4614 function will allocate the return value on the secondary stack and
4615 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4616 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4617 const_flag = false;
4619 if (const_flag || volatile_flag)
4621 const int quals
4622 = (const_flag ? TYPE_QUAL_CONST : 0)
4623 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4625 gnu_type = change_qualified_type (gnu_type, quals);
4628 /* If we have a builtin decl for that function, use it. Check if the
4629 profiles are compatible and warn if they are not. The checker is
4630 expected to post extra diagnostics in this case. */
4631 if (gnu_builtin_decl)
4633 intrin_binding_t inb;
4635 inb.gnat_entity = gnat_entity;
4636 inb.ada_fntype = gnu_type;
4637 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4639 if (!intrin_profiles_compatible_p (&inb))
4640 post_error
4641 ("?profile of& doesn''t match the builtin it binds!",
4642 gnat_entity);
4644 gnu_decl = gnu_builtin_decl;
4645 gnu_type = TREE_TYPE (gnu_builtin_decl);
4646 break;
4649 /* If there was no specified Interface_Name and the external and
4650 internal names of the subprogram are the same, only use the
4651 internal name to allow disambiguation of nested subprograms. */
4652 if (No (Interface_Name (gnat_entity))
4653 && gnu_ext_name == gnu_entity_name)
4654 gnu_ext_name = NULL_TREE;
4656 /* If we are defining the subprogram and it has an Address clause
4657 we must get the address expression from the saved GCC tree for the
4658 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4659 the address expression here since the front-end has guaranteed
4660 in that case that the elaboration has no effects. If there is
4661 an Address clause and we are not defining the object, just
4662 make it a constant. */
4663 if (Present (Address_Clause (gnat_entity)))
4665 tree gnu_address = NULL_TREE;
4667 if (definition)
4668 gnu_address
4669 = (present_gnu_tree (gnat_entity)
4670 ? get_gnu_tree (gnat_entity)
4671 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4673 save_gnu_tree (gnat_entity, NULL_TREE, false);
4675 /* Convert the type of the object to a reference type that can
4676 alias everything as per 13.3(19). */
4677 gnu_type
4678 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4679 if (gnu_address)
4680 gnu_address = convert (gnu_type, gnu_address);
4682 gnu_decl
4683 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4684 gnu_address, false, Is_Public (gnat_entity),
4685 extern_flag, false, NULL, gnat_entity);
4686 DECL_BY_REF_P (gnu_decl) = 1;
4689 else if (kind == E_Subprogram_Type)
4691 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4692 gnu_decl
4693 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4694 debug_info_p, gnat_entity);
4696 else
4698 gnu_decl
4699 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4700 gnu_param_list, inline_status,
4701 public_flag, extern_flag, artificial_flag,
4702 attr_list, gnat_entity);
4703 /* This is unrelated to the stub built right above. */
4704 DECL_STUBBED_P (gnu_decl)
4705 = Convention (gnat_entity) == Convention_Stubbed;
4708 break;
4710 case E_Incomplete_Type:
4711 case E_Incomplete_Subtype:
4712 case E_Private_Type:
4713 case E_Private_Subtype:
4714 case E_Limited_Private_Type:
4715 case E_Limited_Private_Subtype:
4716 case E_Record_Type_With_Private:
4717 case E_Record_Subtype_With_Private:
4719 /* Get the "full view" of this entity. If this is an incomplete
4720 entity from a limited with, treat its non-limited view as the
4721 full view. Otherwise, use either the full view or the underlying
4722 full view, whichever is present. This is used in all the tests
4723 below. */
4724 Entity_Id full_view
4725 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4726 ? Non_Limited_View (gnat_entity)
4727 : Present (Full_View (gnat_entity))
4728 ? Full_View (gnat_entity)
4729 : IN (kind, Private_Kind)
4730 ? Underlying_Full_View (gnat_entity)
4731 : Empty;
4733 /* If this is an incomplete type with no full view, it must be a Taft
4734 Amendment type, in which case we return a dummy type. Otherwise,
4735 just get the type from its Etype. */
4736 if (No (full_view))
4738 if (kind == E_Incomplete_Type)
4740 gnu_type = make_dummy_type (gnat_entity);
4741 gnu_decl = TYPE_STUB_DECL (gnu_type);
4743 else
4745 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4746 NULL_TREE, 0);
4747 maybe_present = true;
4749 break;
4752 /* If we already made a type for the full view, reuse it. */
4753 else if (present_gnu_tree (full_view))
4755 gnu_decl = get_gnu_tree (full_view);
4756 break;
4759 /* Otherwise, if we are not defining the type now, get the type
4760 from the full view. But always get the type from the full view
4761 for define on use types, since otherwise we won't see them! */
4762 else if (!definition
4763 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4764 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4766 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4767 maybe_present = true;
4768 break;
4771 /* For incomplete types, make a dummy type entry which will be
4772 replaced later. Save it as the full declaration's type so
4773 we can do any needed updates when we see it. */
4774 gnu_type = make_dummy_type (gnat_entity);
4775 gnu_decl = TYPE_STUB_DECL (gnu_type);
4776 if (Has_Completion_In_Body (gnat_entity))
4777 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4778 save_gnu_tree (full_view, gnu_decl, 0);
4779 break;
4782 case E_Class_Wide_Type:
4783 /* Class-wide types are always transformed into their root type. */
4784 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4785 maybe_present = true;
4786 break;
4788 case E_Task_Type:
4789 case E_Task_Subtype:
4790 case E_Protected_Type:
4791 case E_Protected_Subtype:
4792 /* Concurrent types are always transformed into their record type. */
4793 if (type_annotate_only && No (gnat_equiv_type))
4794 gnu_type = void_type_node;
4795 else
4796 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4797 maybe_present = true;
4798 break;
4800 case E_Label:
4801 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4802 break;
4804 case E_Block:
4805 case E_Loop:
4806 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4807 we've already saved it, so we don't try to. */
4808 gnu_decl = error_mark_node;
4809 saved = true;
4810 break;
4812 case E_Abstract_State:
4813 /* This is a SPARK annotation that only reaches here when compiling in
4814 ASIS mode. */
4815 gcc_assert (type_annotate_only);
4816 gnu_decl = error_mark_node;
4817 saved = true;
4818 break;
4820 default:
4821 gcc_unreachable ();
4824 /* If we had a case where we evaluated another type and it might have
4825 defined this one, handle it here. */
4826 if (maybe_present && present_gnu_tree (gnat_entity))
4828 gnu_decl = get_gnu_tree (gnat_entity);
4829 saved = true;
4832 /* If we are processing a type and there is either no decl for it or
4833 we just made one, do some common processing for the type, such as
4834 handling alignment and possible padding. */
4835 if (is_type && (!gnu_decl || this_made_decl))
4837 /* Process the attributes, if not already done. Note that the type is
4838 already defined so we cannot pass true for IN_PLACE here. */
4839 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4841 /* Tell the middle-end that objects of tagged types are guaranteed to
4842 be properly aligned. This is necessary because conversions to the
4843 class-wide type are translated into conversions to the root type,
4844 which can be less aligned than some of its derived types. */
4845 if (Is_Tagged_Type (gnat_entity)
4846 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4847 TYPE_ALIGN_OK (gnu_type) = 1;
4849 /* Record whether the type is passed by reference. */
4850 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4851 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4853 /* ??? Don't set the size for a String_Literal since it is either
4854 confirming or we don't handle it properly (if the low bound is
4855 non-constant). */
4856 if (!gnu_size && kind != E_String_Literal_Subtype)
4858 Uint gnat_size = Known_Esize (gnat_entity)
4859 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4860 gnu_size
4861 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4862 false, Has_Size_Clause (gnat_entity));
4865 /* If a size was specified, see if we can make a new type of that size
4866 by rearranging the type, for example from a fat to a thin pointer. */
4867 if (gnu_size)
4869 gnu_type
4870 = make_type_from_size (gnu_type, gnu_size,
4871 Has_Biased_Representation (gnat_entity));
4873 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4874 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4875 gnu_size = NULL_TREE;
4878 /* If the alignment has not already been processed and this is not
4879 an unconstrained array type, see if an alignment is specified.
4880 If not, we pick a default alignment for atomic objects. */
4881 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4883 else if (Known_Alignment (gnat_entity))
4885 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4886 TYPE_ALIGN (gnu_type));
4888 /* Warn on suspiciously large alignments. This should catch
4889 errors about the (alignment,byte)/(size,bit) discrepancy. */
4890 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4892 tree size;
4894 /* If a size was specified, take it into account. Otherwise
4895 use the RM size for records or unions as the type size has
4896 already been adjusted to the alignment. */
4897 if (gnu_size)
4898 size = gnu_size;
4899 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4900 && !TYPE_FAT_POINTER_P (gnu_type))
4901 size = rm_size (gnu_type);
4902 else
4903 size = TYPE_SIZE (gnu_type);
4905 /* Consider an alignment as suspicious if the alignment/size
4906 ratio is greater or equal to the byte/bit ratio. */
4907 if (tree_fits_uhwi_p (size)
4908 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4909 post_error_ne ("?suspiciously large alignment specified for&",
4910 Expression (Alignment_Clause (gnat_entity)),
4911 gnat_entity);
4914 else if (Is_Atomic (gnat_entity) && !gnu_size
4915 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4916 && integer_pow2p (TYPE_SIZE (gnu_type)))
4917 align = MIN (BIGGEST_ALIGNMENT,
4918 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4919 else if (Is_Atomic (gnat_entity) && gnu_size
4920 && tree_fits_uhwi_p (gnu_size)
4921 && integer_pow2p (gnu_size))
4922 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4924 /* See if we need to pad the type. If we did, and made a record,
4925 the name of the new type may be changed. So get it back for
4926 us when we make the new TYPE_DECL below. */
4927 if (gnu_size || align > 0)
4928 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4929 false, !gnu_decl, definition, false);
4931 if (TYPE_IS_PADDING_P (gnu_type))
4932 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4934 /* Now set the RM size of the type. We cannot do it before padding
4935 because we need to accept arbitrary RM sizes on integral types. */
4936 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4938 /* If we are at global level, GCC will have applied variable_size to
4939 the type, but that won't have done anything. So, if it's not
4940 a constant or self-referential, call elaborate_expression_1 to
4941 make a variable for the size rather than calculating it each time.
4942 Handle both the RM size and the actual size. */
4943 if (global_bindings_p ()
4944 && TYPE_SIZE (gnu_type)
4945 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4946 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4948 tree size = TYPE_SIZE (gnu_type);
4950 TYPE_SIZE (gnu_type)
4951 = elaborate_expression_1 (size, gnat_entity,
4952 get_identifier ("SIZE"),
4953 definition, false);
4955 /* ??? For now, store the size as a multiple of the alignment in
4956 bytes so that we can see the alignment from the tree. */
4957 TYPE_SIZE_UNIT (gnu_type)
4958 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4959 get_identifier ("SIZE_A_UNIT"),
4960 definition, false,
4961 TYPE_ALIGN (gnu_type));
4963 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4964 may not be marked by the call to create_type_decl below. */
4965 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4967 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4969 tree variant_part = get_variant_part (gnu_type);
4970 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4972 if (variant_part)
4974 tree union_type = TREE_TYPE (variant_part);
4975 tree offset = DECL_FIELD_OFFSET (variant_part);
4977 /* If the position of the variant part is constant, subtract
4978 it from the size of the type of the parent to get the new
4979 size. This manual CSE reduces the data size. */
4980 if (TREE_CODE (offset) == INTEGER_CST)
4982 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4983 TYPE_SIZE (union_type)
4984 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4985 bit_from_pos (offset, bitpos));
4986 TYPE_SIZE_UNIT (union_type)
4987 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4988 byte_from_pos (offset, bitpos));
4990 else
4992 TYPE_SIZE (union_type)
4993 = elaborate_expression_1 (TYPE_SIZE (union_type),
4994 gnat_entity,
4995 get_identifier ("VSIZE"),
4996 definition, false);
4998 /* ??? For now, store the size as a multiple of the
4999 alignment in bytes so that we can see the alignment
5000 from the tree. */
5001 TYPE_SIZE_UNIT (union_type)
5002 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5003 gnat_entity,
5004 get_identifier
5005 ("VSIZE_A_UNIT"),
5006 definition, false,
5007 TYPE_ALIGN (union_type));
5009 /* ??? For now, store the offset as a multiple of the
5010 alignment in bytes so that we can see the alignment
5011 from the tree. */
5012 DECL_FIELD_OFFSET (variant_part)
5013 = elaborate_expression_2 (offset,
5014 gnat_entity,
5015 get_identifier ("VOFFSET"),
5016 definition, false,
5017 DECL_OFFSET_ALIGN
5018 (variant_part));
5021 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5022 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5025 if (operand_equal_p (ada_size, size, 0))
5026 ada_size = TYPE_SIZE (gnu_type);
5027 else
5028 ada_size
5029 = elaborate_expression_1 (ada_size, gnat_entity,
5030 get_identifier ("RM_SIZE"),
5031 definition, false);
5032 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5036 /* If this is a record type or subtype, call elaborate_expression_2 on
5037 any field position. Do this for both global and local types.
5038 Skip any fields that we haven't made trees for to avoid problems with
5039 class wide types. */
5040 if (IN (kind, Record_Kind))
5041 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5042 gnat_temp = Next_Entity (gnat_temp))
5043 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5045 tree gnu_field = get_gnu_tree (gnat_temp);
5047 /* ??? For now, store the offset as a multiple of the alignment
5048 in bytes so that we can see the alignment from the tree. */
5049 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5051 DECL_FIELD_OFFSET (gnu_field)
5052 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5053 gnat_temp,
5054 get_identifier ("OFFSET"),
5055 definition, false,
5056 DECL_OFFSET_ALIGN (gnu_field));
5058 /* ??? The context of gnu_field is not necessarily gnu_type
5059 so the MULT_EXPR node built above may not be marked by
5060 the call to create_type_decl below. */
5061 if (global_bindings_p ())
5062 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5066 if (Is_Atomic (gnat_entity))
5067 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5069 /* If this is not an unconstrained array type, set some flags. */
5070 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5072 if (Treat_As_Volatile (gnat_entity))
5073 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5075 if (Present (Alignment_Clause (gnat_entity)))
5076 TYPE_USER_ALIGN (gnu_type) = 1;
5078 if (Universal_Aliasing (gnat_entity))
5079 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5082 if (!gnu_decl)
5083 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5084 !Comes_From_Source (gnat_entity),
5085 debug_info_p, gnat_entity);
5086 else
5088 TREE_TYPE (gnu_decl) = gnu_type;
5089 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5093 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5095 gnu_type = TREE_TYPE (gnu_decl);
5097 /* If this is a derived type, relate its alias set to that of its parent
5098 to avoid troubles when a call to an inherited primitive is inlined in
5099 a context where a derived object is accessed. The inlined code works
5100 on the parent view so the resulting code may access the same object
5101 using both the parent and the derived alias sets, which thus have to
5102 conflict. As the same issue arises with component references, the
5103 parent alias set also has to conflict with composite types enclosing
5104 derived components. For instance, if we have:
5106 type D is new T;
5107 type R is record
5108 Component : D;
5109 end record;
5111 we want T to conflict with both D and R, in addition to R being a
5112 superset of D by record/component construction.
5114 One way to achieve this is to perform an alias set copy from the
5115 parent to the derived type. This is not quite appropriate, though,
5116 as we don't want separate derived types to conflict with each other:
5118 type I1 is new Integer;
5119 type I2 is new Integer;
5121 We want I1 and I2 to both conflict with Integer but we do not want
5122 I1 to conflict with I2, and an alias set copy on derivation would
5123 have that effect.
5125 The option chosen is to make the alias set of the derived type a
5126 superset of that of its parent type. It trivially fulfills the
5127 simple requirement for the Integer derivation example above, and
5128 the component case as well by superset transitivity:
5130 superset superset
5131 R ----------> D ----------> T
5133 However, for composite types, conversions between derived types are
5134 translated into VIEW_CONVERT_EXPRs so a sequence like:
5136 type Comp1 is new Comp;
5137 type Comp2 is new Comp;
5138 procedure Proc (C : Comp1);
5140 C : Comp2;
5141 Proc (Comp1 (C));
5143 is translated into:
5145 C : Comp2;
5146 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5148 and gimplified into:
5150 C : Comp2;
5151 Comp1 *C.0;
5152 C.0 = (Comp1 *) &C;
5153 Proc (C.0);
5155 i.e. generates code involving type punning. Therefore, Comp1 needs
5156 to conflict with Comp2 and an alias set copy is required.
5158 The language rules ensure the parent type is already frozen here. */
5159 if (kind != E_Subprogram_Type
5160 && Is_Derived_Type (gnat_entity)
5161 && !type_annotate_only)
5163 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5164 /* For constrained packed array subtypes, the implementation type is
5165 used instead of the nominal type. */
5166 if (kind == E_Array_Subtype
5167 && Is_Constrained (gnat_entity)
5168 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5169 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5170 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5171 Is_Composite_Type (gnat_entity)
5172 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5175 /* Back-annotate the Alignment of the type if not already in the
5176 tree. Likewise for sizes. */
5177 if (Unknown_Alignment (gnat_entity))
5179 unsigned int double_align, align;
5180 bool is_capped_double, align_clause;
5182 /* If the default alignment of "double" or larger scalar types is
5183 specifically capped and this is not an array with an alignment
5184 clause on the component type, return the cap. */
5185 if ((double_align = double_float_alignment) > 0)
5186 is_capped_double
5187 = is_double_float_or_array (gnat_entity, &align_clause);
5188 else if ((double_align = double_scalar_alignment) > 0)
5189 is_capped_double
5190 = is_double_scalar_or_array (gnat_entity, &align_clause);
5191 else
5192 is_capped_double = align_clause = false;
5194 if (is_capped_double && !align_clause)
5195 align = double_align;
5196 else
5197 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5199 Set_Alignment (gnat_entity, UI_From_Int (align));
5202 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5204 tree gnu_size = TYPE_SIZE (gnu_type);
5206 /* If the size is self-referential, annotate the maximum value. */
5207 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5208 gnu_size = max_size (gnu_size, true);
5210 /* If we are just annotating types and the type is tagged, the tag
5211 and the parent components are not generated by the front-end so
5212 sizes must be adjusted if there is no representation clause. */
5213 if (type_annotate_only
5214 && Is_Tagged_Type (gnat_entity)
5215 && !VOID_TYPE_P (gnu_type)
5216 && (!TYPE_FIELDS (gnu_type)
5217 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5219 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5220 Uint uint_size;
5222 if (Is_Derived_Type (gnat_entity))
5224 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5225 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5226 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5228 else
5229 offset = pointer_size;
5231 if (TYPE_FIELDS (gnu_type))
5232 offset
5233 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5235 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5236 gnu_size = round_up (gnu_size, POINTER_SIZE);
5237 uint_size = annotate_value (gnu_size);
5238 Set_Esize (gnat_entity, uint_size);
5239 Set_RM_Size (gnat_entity, uint_size);
5241 else
5242 Set_Esize (gnat_entity, annotate_value (gnu_size));
5245 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5246 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5249 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5250 cannot do so if we are reusing the ..._DECL node made for an equivalent
5251 type or an alias or a renamed object as the predicates don't apply to it
5252 but to GNAT_ENTITY. */
5253 if (DECL_P (gnu_decl)
5254 && !(is_type && gnat_equiv_type != gnat_entity)
5255 && !Present (Alias (gnat_entity))
5256 && !(Present (Renamed_Object (gnat_entity)) && saved))
5258 if (!Comes_From_Source (gnat_entity))
5259 DECL_ARTIFICIAL (gnu_decl) = 1;
5261 if (!debug_info_p)
5262 DECL_IGNORED_P (gnu_decl) = 1;
5265 /* If we haven't already, associate the ..._DECL node that we just made with
5266 the input GNAT entity node. */
5267 if (!saved)
5268 save_gnu_tree (gnat_entity, gnu_decl, false);
5270 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5271 eliminate as many deferred computations as possible. */
5272 process_deferred_decl_context (false);
5274 /* If this is an enumeration or floating-point type, we were not able to set
5275 the bounds since they refer to the type. These are always static. */
5276 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5277 || (kind == E_Floating_Point_Type))
5279 tree gnu_scalar_type = gnu_type;
5280 tree gnu_low_bound, gnu_high_bound;
5282 /* If this is a padded type, we need to use the underlying type. */
5283 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5284 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5286 /* If this is a floating point type and we haven't set a floating
5287 point type yet, use this in the evaluation of the bounds. */
5288 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5289 longest_float_type_node = gnu_scalar_type;
5291 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5292 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5294 if (kind == E_Enumeration_Type)
5296 /* Enumeration types have specific RM bounds. */
5297 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5298 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5300 else
5302 /* Floating-point types don't have specific RM bounds. */
5303 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5304 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5308 /* If we deferred processing of incomplete types, re-enable it. If there
5309 were no other disables and we have deferred types to process, do so. */
5310 if (this_deferred
5311 && --defer_incomplete_level == 0
5312 && defer_incomplete_list)
5314 struct incomplete *p, *next;
5316 /* We are back to level 0 for the deferring of incomplete types.
5317 But processing these incomplete types below may itself require
5318 deferring, so preserve what we have and restart from scratch. */
5319 p = defer_incomplete_list;
5320 defer_incomplete_list = NULL;
5322 for (; p; p = next)
5324 next = p->next;
5326 if (p->old_type)
5327 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5328 gnat_to_gnu_type (p->full_type));
5329 free (p);
5333 /* If we are not defining this type, see if it's on one of the lists of
5334 incomplete types. If so, handle the list entry now. */
5335 if (is_type && !definition)
5337 struct incomplete *p;
5339 for (p = defer_incomplete_list; p; p = p->next)
5340 if (p->old_type && p->full_type == gnat_entity)
5342 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5343 TREE_TYPE (gnu_decl));
5344 p->old_type = NULL_TREE;
5347 for (p = defer_limited_with; p; p = p->next)
5348 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5350 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5351 TREE_TYPE (gnu_decl));
5352 p->old_type = NULL_TREE;
5356 if (this_global)
5357 force_global--;
5359 /* If this is a packed array type whose original array type is itself
5360 an Itype without freeze node, make sure the latter is processed. */
5361 if (Is_Packed_Array_Impl_Type (gnat_entity)
5362 && Is_Itype (Original_Array_Type (gnat_entity))
5363 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5364 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5365 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5367 return gnu_decl;
5370 /* Similar, but if the returned value is a COMPONENT_REF, return the
5371 FIELD_DECL. */
5373 tree
5374 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5376 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5378 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5379 gnu_field = TREE_OPERAND (gnu_field, 1);
5381 return gnu_field;
5384 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5385 the GCC type corresponding to that entity. */
5387 tree
5388 gnat_to_gnu_type (Entity_Id gnat_entity)
5390 tree gnu_decl;
5392 /* The back end never attempts to annotate generic types. */
5393 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5394 return void_type_node;
5396 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5397 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5399 return TREE_TYPE (gnu_decl);
5402 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5403 the unpadded version of the GCC type corresponding to that entity. */
5405 tree
5406 get_unpadded_type (Entity_Id gnat_entity)
5408 tree type = gnat_to_gnu_type (gnat_entity);
5410 if (TYPE_IS_PADDING_P (type))
5411 type = TREE_TYPE (TYPE_FIELDS (type));
5413 return type;
5416 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5417 type has been changed to that of the parameterless procedure, except if an
5418 alias is already present, in which case it is returned instead. */
5420 tree
5421 get_minimal_subprog_decl (Entity_Id gnat_entity)
5423 tree gnu_entity_name, gnu_ext_name;
5424 struct attrib *attr_list = NULL;
5426 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5427 of the handling applied here. */
5429 while (Present (Alias (gnat_entity)))
5431 gnat_entity = Alias (gnat_entity);
5432 if (present_gnu_tree (gnat_entity))
5433 return get_gnu_tree (gnat_entity);
5436 gnu_entity_name = get_entity_name (gnat_entity);
5437 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5439 if (Has_Stdcall_Convention (gnat_entity))
5440 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5441 get_identifier ("stdcall"), NULL_TREE,
5442 gnat_entity);
5443 else if (Has_Thiscall_Convention (gnat_entity))
5444 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5445 get_identifier ("thiscall"), NULL_TREE,
5446 gnat_entity);
5448 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5449 gnu_ext_name = NULL_TREE;
5451 return
5452 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5453 is_disabled, true, true, true, attr_list, gnat_entity);
5456 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5457 a C++ imported method or equivalent.
5459 We use the predicate on 32-bit x86/Windows to find out whether we need to
5460 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5461 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5463 bool
5464 is_cplusplus_method (Entity_Id gnat_entity)
5466 if (Convention (gnat_entity) != Convention_CPP)
5467 return false;
5469 /* This is the main case: C++ method imported as a primitive operation.
5470 Note that a C++ class with no virtual functions can be imported as a
5471 limited record type so the operation is not necessarily dispatching. */
5472 if (Is_Primitive (gnat_entity))
5473 return true;
5475 /* A thunk needs to be handled like its associated primitive operation. */
5476 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5477 return true;
5479 /* A constructor is a method on the C++ side. */
5480 if (Is_Constructor (gnat_entity))
5481 return true;
5483 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5484 if (Is_Dispatch_Table_Entity (gnat_entity))
5485 return true;
5487 return false;
5490 /* Finalize the processing of From_Limited_With incomplete types. */
5492 void
5493 finalize_from_limited_with (void)
5495 struct incomplete *p, *next;
5497 p = defer_limited_with;
5498 defer_limited_with = NULL;
5500 for (; p; p = next)
5502 next = p->next;
5504 if (p->old_type)
5505 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5506 gnat_to_gnu_type (p->full_type));
5507 free (p);
5511 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5512 kind of type (such E_Task_Type) that has a different type which Gigi
5513 uses for its representation. If the type does not have a special type
5514 for its representation, return GNAT_ENTITY. If a type is supposed to
5515 exist, but does not, abort unless annotating types, in which case
5516 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5518 Entity_Id
5519 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5521 Entity_Id gnat_equiv = gnat_entity;
5523 if (No (gnat_entity))
5524 return gnat_entity;
5526 switch (Ekind (gnat_entity))
5528 case E_Class_Wide_Subtype:
5529 if (Present (Equivalent_Type (gnat_entity)))
5530 gnat_equiv = Equivalent_Type (gnat_entity);
5531 break;
5533 case E_Access_Protected_Subprogram_Type:
5534 case E_Anonymous_Access_Protected_Subprogram_Type:
5535 gnat_equiv = Equivalent_Type (gnat_entity);
5536 break;
5538 case E_Class_Wide_Type:
5539 gnat_equiv = Root_Type (gnat_entity);
5540 break;
5542 case E_Task_Type:
5543 case E_Task_Subtype:
5544 case E_Protected_Type:
5545 case E_Protected_Subtype:
5546 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5547 break;
5549 default:
5550 break;
5553 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5555 return gnat_equiv;
5558 /* Return a GCC tree for a type corresponding to the component type of the
5559 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5560 is for an array being defined. DEBUG_INFO_P is true if we need to write
5561 debug information for other types that we may create in the process. */
5563 static tree
5564 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5565 bool debug_info_p)
5567 const Entity_Id gnat_type = Component_Type (gnat_array);
5568 tree gnu_type = gnat_to_gnu_type (gnat_type);
5569 tree gnu_comp_size;
5571 /* Try to get a smaller form of the component if needed. */
5572 if ((Is_Packed (gnat_array)
5573 || Has_Component_Size_Clause (gnat_array))
5574 && !Is_Bit_Packed_Array (gnat_array)
5575 && !Has_Aliased_Components (gnat_array)
5576 && !Strict_Alignment (gnat_type)
5577 && RECORD_OR_UNION_TYPE_P (gnu_type)
5578 && !TYPE_FAT_POINTER_P (gnu_type)
5579 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5580 gnu_type = make_packable_type (gnu_type, false);
5582 if (Has_Atomic_Components (gnat_array))
5583 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5585 /* Get and validate any specified Component_Size. */
5586 gnu_comp_size
5587 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5588 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5589 true, Has_Component_Size_Clause (gnat_array));
5591 /* If the array has aliased components and the component size can be zero,
5592 force at least unit size to ensure that the components have distinct
5593 addresses. */
5594 if (!gnu_comp_size
5595 && Has_Aliased_Components (gnat_array)
5596 && (integer_zerop (TYPE_SIZE (gnu_type))
5597 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5598 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5599 gnu_comp_size
5600 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5602 /* If the component type is a RECORD_TYPE that has a self-referential size,
5603 then use the maximum size for the component size. */
5604 if (!gnu_comp_size
5605 && TREE_CODE (gnu_type) == RECORD_TYPE
5606 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5607 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5609 /* Honor the component size. This is not needed for bit-packed arrays. */
5610 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5612 tree orig_type = gnu_type;
5613 unsigned int max_align;
5615 /* If an alignment is specified, use it as a cap on the component type
5616 so that it can be honored for the whole type. But ignore it for the
5617 original type of packed array types. */
5618 if (No (Packed_Array_Impl_Type (gnat_array))
5619 && Known_Alignment (gnat_array))
5620 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5621 else
5622 max_align = 0;
5624 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5625 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5626 gnu_type = orig_type;
5627 else
5628 orig_type = gnu_type;
5630 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5631 true, false, definition, true);
5633 /* If a padding record was made, declare it now since it will never be
5634 declared otherwise. This is necessary to ensure that its subtrees
5635 are properly marked. */
5636 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5637 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5638 gnat_array);
5641 /* If the component type is a padded type made for a non-bit-packed array
5642 of scalars with reverse storage order, we need to propagate the reverse
5643 storage order to the padding type since it is the innermost enclosing
5644 aggregate type around the scalar. */
5645 if (TYPE_IS_PADDING_P (gnu_type)
5646 && Reverse_Storage_Order (gnat_array)
5647 && !Is_Bit_Packed_Array (gnat_array)
5648 && Is_Scalar_Type (gnat_type))
5649 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5651 if (Has_Volatile_Components (gnat_array))
5652 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5654 return gnu_type;
5657 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5658 using MECH as its passing mechanism, to be placed in the parameter
5659 list built for GNAT_SUBPROG. Assume a foreign convention for the
5660 latter if FOREIGN is true. Also set CICO to true if the parameter
5661 must use the copy-in copy-out implementation mechanism.
5663 The returned tree is a PARM_DECL, except for those cases where no
5664 parameter needs to be actually passed to the subprogram; the type
5665 of this "shadow" parameter is then returned instead. */
5667 static tree
5668 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5669 Entity_Id gnat_subprog, bool foreign, bool *cico)
5671 tree gnu_param_name = get_entity_name (gnat_param);
5672 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5673 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5674 /* The parameter can be indirectly modified if its address is taken. */
5675 bool ro_param = in_param && !Address_Taken (gnat_param);
5676 bool by_return = false, by_component_ptr = false;
5677 bool by_ref = false;
5678 tree gnu_param;
5680 /* Copy-return is used only for the first parameter of a valued procedure.
5681 It's a copy mechanism for which a parameter is never allocated. */
5682 if (mech == By_Copy_Return)
5684 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5685 mech = By_Copy;
5686 by_return = true;
5689 /* If this is either a foreign function or if the underlying type won't
5690 be passed by reference and is as aligned as the original type, strip
5691 off possible padding type. */
5692 if (TYPE_IS_PADDING_P (gnu_param_type))
5694 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5696 if (foreign
5697 || (!must_pass_by_ref (unpadded_type)
5698 && mech != By_Reference
5699 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5700 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5701 gnu_param_type = unpadded_type;
5704 /* If this is a read-only parameter, make a variant of the type that is
5705 read-only. ??? However, if this is an unconstrained array, that type
5706 can be very complex, so skip it for now. Likewise for any other
5707 self-referential type. */
5708 if (ro_param
5709 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5710 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5711 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5713 /* For foreign conventions, pass arrays as pointers to the element type.
5714 First check for unconstrained array and get the underlying array. */
5715 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5716 gnu_param_type
5717 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5719 /* For GCC builtins, pass Address integer types as (void *) */
5720 if (Convention (gnat_subprog) == Convention_Intrinsic
5721 && Present (Interface_Name (gnat_subprog))
5722 && Is_Descendent_Of_Address (Etype (gnat_param)))
5723 gnu_param_type = ptr_void_type_node;
5725 /* Arrays are passed as pointers to element type for foreign conventions. */
5726 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5728 /* Strip off any multi-dimensional entries, then strip
5729 off the last array to get the component type. */
5730 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5731 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5732 gnu_param_type = TREE_TYPE (gnu_param_type);
5734 by_component_ptr = true;
5735 gnu_param_type = TREE_TYPE (gnu_param_type);
5737 if (ro_param)
5738 gnu_param_type
5739 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5741 gnu_param_type = build_pointer_type (gnu_param_type);
5744 /* Fat pointers are passed as thin pointers for foreign conventions. */
5745 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5746 gnu_param_type
5747 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5749 /* If we must pass or were requested to pass by reference, do so.
5750 If we were requested to pass by copy, do so.
5751 Otherwise, for foreign conventions, pass In Out or Out parameters
5752 or aggregates by reference. For COBOL and Fortran, pass all
5753 integer and FP types that way too. For Convention Ada, use
5754 the standard Ada default. */
5755 else if (must_pass_by_ref (gnu_param_type)
5756 || mech == By_Reference
5757 || (mech != By_Copy
5758 && ((foreign
5759 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5760 || (foreign
5761 && (Convention (gnat_subprog) == Convention_Fortran
5762 || Convention (gnat_subprog) == Convention_COBOL)
5763 && (INTEGRAL_TYPE_P (gnu_param_type)
5764 || FLOAT_TYPE_P (gnu_param_type)))
5765 || (!foreign
5766 && default_pass_by_ref (gnu_param_type)))))
5768 /* We take advantage of 6.2(12) by considering that references built for
5769 parameters whose type isn't by-ref and for which the mechanism hasn't
5770 been forced to by-ref are restrict-qualified in the C sense. */
5771 bool restrict_p
5772 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5773 gnu_param_type = build_reference_type (gnu_param_type);
5774 if (restrict_p)
5775 gnu_param_type
5776 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5777 by_ref = true;
5780 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5781 else if (!in_param)
5782 *cico = true;
5784 if (mech == By_Copy && (by_ref || by_component_ptr))
5785 post_error ("?cannot pass & by copy", gnat_param);
5787 /* If this is an Out parameter that isn't passed by reference and isn't
5788 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5789 it will be a VAR_DECL created when we process the procedure, so just
5790 return its type. For the special parameter of a valued procedure,
5791 never pass it in.
5793 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5794 Out parameters with discriminants or implicit initial values to be
5795 handled like In Out parameters. These type are normally built as
5796 aggregates, hence passed by reference, except for some packed arrays
5797 which end up encoded in special integer types. Note that scalars can
5798 be given implicit initial values using the Default_Value aspect.
5800 The exception we need to make is then for packed arrays of records
5801 with discriminants or implicit initial values. We have no light/easy
5802 way to check for the latter case, so we merely check for packed arrays
5803 of records. This may lead to useless copy-in operations, but in very
5804 rare cases only, as these would be exceptions in a set of already
5805 exceptional situations. */
5806 if (Ekind (gnat_param) == E_Out_Parameter
5807 && !by_ref
5808 && (by_return
5809 || (!POINTER_TYPE_P (gnu_param_type)
5810 && !AGGREGATE_TYPE_P (gnu_param_type)
5811 && !Has_Default_Aspect (Etype (gnat_param))))
5812 && !(Is_Array_Type (Etype (gnat_param))
5813 && Is_Packed (Etype (gnat_param))
5814 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5815 return gnu_param_type;
5817 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5818 ro_param || by_ref || by_component_ptr);
5819 DECL_BY_REF_P (gnu_param) = by_ref;
5820 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5821 DECL_POINTS_TO_READONLY_P (gnu_param)
5822 = (ro_param && (by_ref || by_component_ptr));
5823 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5825 /* If no Mechanism was specified, indicate what we're using, then
5826 back-annotate it. */
5827 if (mech == Default)
5828 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5830 Set_Mechanism (gnat_param, mech);
5831 return gnu_param;
5834 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5835 with of the main unit and whose full view has not been elaborated yet. */
5837 static bool
5838 is_from_limited_with_of_main (Entity_Id gnat_entity)
5840 /* Class-wide types are always transformed into their root type. */
5841 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5842 gnat_entity = Root_Type (gnat_entity);
5844 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5845 && From_Limited_With (gnat_entity))
5847 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5849 if (present_gnu_tree (gnat_full_view))
5850 return false;
5852 return In_Extended_Main_Code_Unit (gnat_full_view);
5855 return false;
5858 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5859 qualifiers on TYPE. */
5861 static tree
5862 change_qualified_type (tree type, int type_quals)
5864 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5867 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5869 static bool
5870 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5872 while (Present (Corresponding_Discriminant (discr1)))
5873 discr1 = Corresponding_Discriminant (discr1);
5875 while (Present (Corresponding_Discriminant (discr2)))
5876 discr2 = Corresponding_Discriminant (discr2);
5878 return
5879 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5882 /* Return true if the array type GNU_TYPE, which represents a dimension of
5883 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5885 static bool
5886 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5888 /* If the array type is not the innermost dimension of the GNAT type,
5889 then it has a non-aliased component. */
5890 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5891 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5892 return true;
5894 /* If the array type has an aliased component in the front-end sense,
5895 then it also has an aliased component in the back-end sense. */
5896 if (Has_Aliased_Components (gnat_type))
5897 return false;
5899 /* If this is a derived type, then it has a non-aliased component if
5900 and only if its parent type also has one. */
5901 if (Is_Derived_Type (gnat_type))
5903 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5904 int index;
5905 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5906 gnu_parent_type
5907 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5908 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5909 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5910 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5913 /* Otherwise, rely exclusively on properties of the element type. */
5914 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5917 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5919 static bool
5920 compile_time_known_address_p (Node_Id gnat_address)
5922 /* Catch System'To_Address. */
5923 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5924 gnat_address = Expression (gnat_address);
5926 return Compile_Time_Known_Value (gnat_address);
5929 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5930 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5932 static bool
5933 cannot_be_superflat_p (Node_Id gnat_range)
5935 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5936 Node_Id scalar_range;
5937 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5939 /* If the low bound is not constant, try to find an upper bound. */
5940 while (Nkind (gnat_lb) != N_Integer_Literal
5941 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5942 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5943 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5944 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5945 || Nkind (scalar_range) == N_Range))
5946 gnat_lb = High_Bound (scalar_range);
5948 /* If the high bound is not constant, try to find a lower bound. */
5949 while (Nkind (gnat_hb) != N_Integer_Literal
5950 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5951 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5952 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5953 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5954 || Nkind (scalar_range) == N_Range))
5955 gnat_hb = Low_Bound (scalar_range);
5957 /* If we have failed to find constant bounds, punt. */
5958 if (Nkind (gnat_lb) != N_Integer_Literal
5959 || Nkind (gnat_hb) != N_Integer_Literal)
5960 return false;
5962 /* We need at least a signed 64-bit type to catch most cases. */
5963 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5964 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5965 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5966 return false;
5968 /* If the low bound is the smallest integer, nothing can be smaller. */
5969 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5970 if (TREE_OVERFLOW (gnu_lb_minus_one))
5971 return true;
5973 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5976 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5978 static bool
5979 constructor_address_p (tree gnu_expr)
5981 while (TREE_CODE (gnu_expr) == NOP_EXPR
5982 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5983 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5984 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5986 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5987 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5990 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5991 be elaborated at the point of its definition, but do nothing else. */
5993 void
5994 elaborate_entity (Entity_Id gnat_entity)
5996 switch (Ekind (gnat_entity))
5998 case E_Signed_Integer_Subtype:
5999 case E_Modular_Integer_Subtype:
6000 case E_Enumeration_Subtype:
6001 case E_Ordinary_Fixed_Point_Subtype:
6002 case E_Decimal_Fixed_Point_Subtype:
6003 case E_Floating_Point_Subtype:
6005 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6006 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6008 /* ??? Tests to avoid Constraint_Error in static expressions
6009 are needed until after the front stops generating bogus
6010 conversions on bounds of real types. */
6011 if (!Raises_Constraint_Error (gnat_lb))
6012 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
6013 true, false, Needs_Debug_Info (gnat_entity));
6014 if (!Raises_Constraint_Error (gnat_hb))
6015 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
6016 true, false, Needs_Debug_Info (gnat_entity));
6017 break;
6020 case E_Record_Subtype:
6021 case E_Private_Subtype:
6022 case E_Limited_Private_Subtype:
6023 case E_Record_Subtype_With_Private:
6024 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6026 Node_Id gnat_discriminant_expr;
6027 Entity_Id gnat_field;
6029 for (gnat_field
6030 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6031 gnat_discriminant_expr
6032 = First_Elmt (Discriminant_Constraint (gnat_entity));
6033 Present (gnat_field);
6034 gnat_field = Next_Discriminant (gnat_field),
6035 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6036 /* Ignore access discriminants. */
6037 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6038 elaborate_expression (Node (gnat_discriminant_expr),
6039 gnat_entity, get_entity_name (gnat_field),
6040 true, false, false);
6042 break;
6047 /* Return true if the size in units represented by GNU_SIZE can be handled by
6048 an allocation. If STATIC_P is true, consider only what can be done with a
6049 static allocation. */
6051 static bool
6052 allocatable_size_p (tree gnu_size, bool static_p)
6054 /* We can allocate a fixed size if it is a valid for the middle-end. */
6055 if (TREE_CODE (gnu_size) == INTEGER_CST)
6056 return valid_constant_size_p (gnu_size);
6058 /* We can allocate a variable size if this isn't a static allocation. */
6059 else
6060 return !static_p;
6063 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6064 NAME, ARGS and ERROR_POINT. */
6066 static void
6067 prepend_one_attribute (struct attrib **attr_list,
6068 enum attr_type attr_type,
6069 tree attr_name,
6070 tree attr_args,
6071 Node_Id attr_error_point)
6073 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6075 attr->type = attr_type;
6076 attr->name = attr_name;
6077 attr->args = attr_args;
6078 attr->error_point = attr_error_point;
6080 attr->next = *attr_list;
6081 *attr_list = attr;
6084 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6086 static void
6087 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6089 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6090 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6091 enum attr_type etype;
6093 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6094 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6096 case Pragma_Machine_Attribute:
6097 etype = ATTR_MACHINE_ATTRIBUTE;
6098 break;
6100 case Pragma_Linker_Alias:
6101 etype = ATTR_LINK_ALIAS;
6102 break;
6104 case Pragma_Linker_Section:
6105 etype = ATTR_LINK_SECTION;
6106 break;
6108 case Pragma_Linker_Constructor:
6109 etype = ATTR_LINK_CONSTRUCTOR;
6110 break;
6112 case Pragma_Linker_Destructor:
6113 etype = ATTR_LINK_DESTRUCTOR;
6114 break;
6116 case Pragma_Weak_External:
6117 etype = ATTR_WEAK_EXTERNAL;
6118 break;
6120 case Pragma_Thread_Local_Storage:
6121 etype = ATTR_THREAD_LOCAL_STORAGE;
6122 break;
6124 default:
6125 return;
6128 /* See what arguments we have and turn them into GCC trees for attribute
6129 handlers. These expect identifier for strings. We handle at most two
6130 arguments and static expressions only. */
6131 if (Present (gnat_arg) && Present (First (gnat_arg)))
6133 Node_Id gnat_arg0 = Next (First (gnat_arg));
6134 Node_Id gnat_arg1 = Empty;
6136 if (Present (gnat_arg0)
6137 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6139 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6141 if (TREE_CODE (gnu_arg0) == STRING_CST)
6143 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6144 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6145 return;
6148 gnat_arg1 = Next (gnat_arg0);
6151 if (Present (gnat_arg1)
6152 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6154 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6156 if (TREE_CODE (gnu_arg1) == STRING_CST)
6157 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6161 /* Prepend to the list. Make a list of the argument we might have, as GCC
6162 expects it. */
6163 prepend_one_attribute (attr_list, etype, gnu_arg0,
6164 gnu_arg1
6165 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6166 Present (Next (First (gnat_arg)))
6167 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6170 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6172 static void
6173 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6175 Node_Id gnat_temp;
6177 /* Attributes are stored as Representation Item pragmas. */
6178 for (gnat_temp = First_Rep_Item (gnat_entity);
6179 Present (gnat_temp);
6180 gnat_temp = Next_Rep_Item (gnat_temp))
6181 if (Nkind (gnat_temp) == N_Pragma)
6182 prepend_one_attribute_pragma (attr_list, gnat_temp);
6185 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6186 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6187 return the GCC tree to use for that expression. GNU_NAME is the suffix
6188 to use if a variable needs to be created and DEFINITION is true if this
6189 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6190 otherwise, we are just elaborating the expression for side-effects. If
6191 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6192 isn't needed for code generation. */
6194 static tree
6195 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6196 bool definition, bool need_value, bool need_debug)
6198 tree gnu_expr;
6200 /* If we already elaborated this expression (e.g. it was involved
6201 in the definition of a private type), use the old value. */
6202 if (present_gnu_tree (gnat_expr))
6203 return get_gnu_tree (gnat_expr);
6205 /* If we don't need a value and this is static or a discriminant,
6206 we don't need to do anything. */
6207 if (!need_value
6208 && (Is_OK_Static_Expression (gnat_expr)
6209 || (Nkind (gnat_expr) == N_Identifier
6210 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6211 return NULL_TREE;
6213 /* If it's a static expression, we don't need a variable for debugging. */
6214 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6215 need_debug = false;
6217 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6218 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6219 gnu_name, definition, need_debug);
6221 /* Save the expression in case we try to elaborate this entity again. Since
6222 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6223 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6224 save_gnu_tree (gnat_expr, gnu_expr, true);
6226 return need_value ? gnu_expr : error_mark_node;
6229 /* Similar, but take a GNU expression and always return a result. */
6231 static tree
6232 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6233 bool definition, bool need_debug)
6235 const bool expr_public_p = Is_Public (gnat_entity);
6236 const bool expr_global_p = expr_public_p || global_bindings_p ();
6237 bool expr_variable_p, use_variable;
6239 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6240 reference will have been replaced with a COMPONENT_REF when the type
6241 is being elaborated. However, there are some cases involving child
6242 types where we will. So convert it to a COMPONENT_REF. We hope it
6243 will be at the highest level of the expression in these cases. */
6244 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6245 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6246 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6247 gnu_expr, NULL_TREE);
6249 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6250 that an expression cannot contain both a discriminant and a variable. */
6251 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6252 return gnu_expr;
6254 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6255 a variable that is initialized to contain the expression when the package
6256 containing the definition is elaborated. If this entity is defined at top
6257 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6258 if this is necessary. */
6259 if (CONSTANT_CLASS_P (gnu_expr))
6260 expr_variable_p = false;
6261 else
6263 /* Skip any conversions and simple constant arithmetics to see if the
6264 expression is based on a read-only variable.
6265 ??? This really should remain read-only, but we have to think about
6266 the typing of the tree here. */
6267 tree inner = remove_conversions (gnu_expr, true);
6269 inner = skip_simple_constant_arithmetic (inner);
6271 if (handled_component_p (inner))
6273 HOST_WIDE_INT bitsize, bitpos;
6274 tree offset;
6275 machine_mode mode;
6276 int unsignedp, reversep, volatilep;
6278 inner
6279 = get_inner_reference (inner, &bitsize, &bitpos, &offset, &mode,
6280 &unsignedp, &reversep, &volatilep, false);
6281 /* If the offset is variable, err on the side of caution. */
6282 if (offset)
6283 inner = NULL_TREE;
6286 expr_variable_p
6287 = !(inner
6288 && TREE_CODE (inner) == VAR_DECL
6289 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6292 /* We only need to use the variable if we are in a global context since GCC
6293 can do the right thing in the local case. However, when not optimizing,
6294 use it for bounds of loop iteration scheme to avoid code duplication. */
6295 use_variable = expr_variable_p
6296 && (expr_global_p
6297 || (!optimize
6298 && definition
6299 && Is_Itype (gnat_entity)
6300 && Nkind (Associated_Node_For_Itype (gnat_entity))
6301 == N_Loop_Parameter_Specification));
6303 /* Now create it, possibly only for debugging purposes. */
6304 if (use_variable || need_debug)
6306 /* The following variable creation can happen when processing the body of
6307 subprograms that are defined out of the extended main unit and
6308 inlined. In this case, we are not at the global scope, and thus the
6309 new variable must not be tagged "external", as we used to do here as
6310 long as definition == 0. */
6311 const bool external_flag = !definition && expr_global_p;
6312 tree gnu_decl
6313 = create_var_decl_1
6314 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6315 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6316 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6318 DECL_ARTIFICIAL (gnu_decl) = 1;
6320 /* Using this variable at debug time (if need_debug is true) requires a
6321 proper location. The back-end will compute a location for this
6322 variable only if the variable is used by the generated code.
6323 Returning the variable ensures the caller will use it in generated
6324 code. Note that there is no need for a location if the debug info
6325 contains an integer constant.
6326 FIXME: when the encoding-based debug scheme is dropped, move this
6327 condition to the top-level IF block: we will not need to create a
6328 variable anymore in such cases, then. */
6329 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6330 return gnu_decl;
6333 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6336 /* Similar, but take an alignment factor and make it explicit in the tree. */
6338 static tree
6339 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6340 bool definition, bool need_debug, unsigned int align)
6342 tree unit_align = size_int (align / BITS_PER_UNIT);
6343 return
6344 size_binop (MULT_EXPR,
6345 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6346 gnu_expr,
6347 unit_align),
6348 gnat_entity, gnu_name, definition,
6349 need_debug),
6350 unit_align);
6353 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6354 the value passed against the list of choices. */
6356 tree
6357 choices_to_gnu (tree operand, Node_Id choices)
6359 Node_Id choice;
6360 Node_Id gnat_temp;
6361 tree result = boolean_false_node;
6362 tree this_test, low = 0, high = 0, single = 0;
6364 for (choice = First (choices); Present (choice); choice = Next (choice))
6366 switch (Nkind (choice))
6368 case N_Range:
6369 low = gnat_to_gnu (Low_Bound (choice));
6370 high = gnat_to_gnu (High_Bound (choice));
6372 this_test
6373 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6374 build_binary_op (GE_EXPR, boolean_type_node,
6375 operand, low),
6376 build_binary_op (LE_EXPR, boolean_type_node,
6377 operand, high));
6379 break;
6381 case N_Subtype_Indication:
6382 gnat_temp = Range_Expression (Constraint (choice));
6383 low = gnat_to_gnu (Low_Bound (gnat_temp));
6384 high = gnat_to_gnu (High_Bound (gnat_temp));
6386 this_test
6387 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6388 build_binary_op (GE_EXPR, boolean_type_node,
6389 operand, low),
6390 build_binary_op (LE_EXPR, boolean_type_node,
6391 operand, high));
6392 break;
6394 case N_Identifier:
6395 case N_Expanded_Name:
6396 /* This represents either a subtype range, an enumeration
6397 literal, or a constant Ekind says which. If an enumeration
6398 literal or constant, fall through to the next case. */
6399 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6400 && Ekind (Entity (choice)) != E_Constant)
6402 tree type = gnat_to_gnu_type (Entity (choice));
6404 low = TYPE_MIN_VALUE (type);
6405 high = TYPE_MAX_VALUE (type);
6407 this_test
6408 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6409 build_binary_op (GE_EXPR, boolean_type_node,
6410 operand, low),
6411 build_binary_op (LE_EXPR, boolean_type_node,
6412 operand, high));
6413 break;
6416 /* ... fall through ... */
6418 case N_Character_Literal:
6419 case N_Integer_Literal:
6420 single = gnat_to_gnu (choice);
6421 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6422 single);
6423 break;
6425 case N_Others_Choice:
6426 this_test = boolean_true_node;
6427 break;
6429 default:
6430 gcc_unreachable ();
6433 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6434 this_test);
6437 return result;
6440 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6441 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6443 static int
6444 adjust_packed (tree field_type, tree record_type, int packed)
6446 /* If the field contains an item of variable size, we cannot pack it
6447 because we cannot create temporaries of non-fixed size in case
6448 we need to take the address of the field. See addressable_p and
6449 the notes on the addressability issues for further details. */
6450 if (type_has_variable_size (field_type))
6451 return 0;
6453 /* If the alignment of the record is specified and the field type
6454 is over-aligned, request Storage_Unit alignment for the field. */
6455 if (packed == -2)
6457 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6458 return -1;
6459 else
6460 return 0;
6463 return packed;
6466 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6467 placed in GNU_RECORD_TYPE.
6469 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6470 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6471 record has a specified alignment.
6473 DEFINITION is true if this field is for a record being defined.
6475 DEBUG_INFO_P is true if we need to write debug information for types
6476 that we may create in the process. */
6478 static tree
6479 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6480 bool definition, bool debug_info_p)
6482 const Entity_Id gnat_field_type = Etype (gnat_field);
6483 const bool is_aliased
6484 = Is_Aliased (gnat_field);
6485 const bool is_atomic
6486 = (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type));
6487 const bool is_independent
6488 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6489 const bool is_volatile
6490 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6491 const bool needs_strict_alignment
6492 = (is_aliased
6493 || is_independent
6494 || is_volatile
6495 || Strict_Alignment (gnat_field_type));
6496 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6497 tree gnu_field_id = get_entity_name (gnat_field);
6498 tree gnu_field, gnu_size, gnu_pos;
6500 /* If this field requires strict alignment, we cannot pack it because
6501 it would very likely be under-aligned in the record. */
6502 if (needs_strict_alignment)
6503 packed = 0;
6504 else
6505 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6507 /* If a size is specified, use it. Otherwise, if the record type is packed,
6508 use the official RM size. See "Handling of Type'Size Values" in Einfo
6509 for further details. */
6510 if (Known_Esize (gnat_field))
6511 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6512 gnat_field, FIELD_DECL, false, true);
6513 else if (packed == 1)
6514 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6515 gnat_field, FIELD_DECL, false, true);
6516 else
6517 gnu_size = NULL_TREE;
6519 /* If we have a specified size that is smaller than that of the field's type,
6520 or a position is specified, and the field's type is a record that doesn't
6521 require strict alignment, see if we can get either an integral mode form
6522 of the type or a smaller form. If we can, show a size was specified for
6523 the field if there wasn't one already, so we know to make this a bitfield
6524 and avoid making things wider.
6526 Changing to an integral mode form is useful when the record is packed as
6527 we can then place the field at a non-byte-aligned position and so achieve
6528 tighter packing. This is in addition required if the field shares a byte
6529 with another field and the front-end lets the back-end handle the access
6530 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6532 Changing to a smaller form is required if the specified size is smaller
6533 than that of the field's type and the type contains sub-fields that are
6534 padded, in order to avoid generating accesses to these sub-fields that
6535 are wider than the field.
6537 We avoid the transformation if it is not required or potentially useful,
6538 as it might entail an increase of the field's alignment and have ripple
6539 effects on the outer record type. A typical case is a field known to be
6540 byte-aligned and not to share a byte with another field. */
6541 if (!needs_strict_alignment
6542 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6543 && !TYPE_FAT_POINTER_P (gnu_field_type)
6544 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6545 && (packed == 1
6546 || (gnu_size
6547 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6548 || (Present (Component_Clause (gnat_field))
6549 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6550 % BITS_PER_UNIT == 0
6551 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6553 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6554 if (gnu_packable_type != gnu_field_type)
6556 gnu_field_type = gnu_packable_type;
6557 if (!gnu_size)
6558 gnu_size = rm_size (gnu_field_type);
6562 if (Is_Atomic (gnat_field))
6563 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6565 if (Present (Component_Clause (gnat_field)))
6567 Node_Id gnat_clause = Component_Clause (gnat_field);
6568 Entity_Id gnat_parent
6569 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6571 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6572 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6573 gnat_field, FIELD_DECL, false, true);
6575 /* Ensure the position does not overlap with the parent subtype, if there
6576 is one. This test is omitted if the parent of the tagged type has a
6577 full rep clause since, in this case, component clauses are allowed to
6578 overlay the space allocated for the parent type and the front-end has
6579 checked that there are no overlapping components. */
6580 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6582 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6584 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6585 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6586 post_error_ne_tree
6587 ("offset of& must be beyond parent{, minimum allowed is ^}",
6588 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6591 /* If this field needs strict alignment, make sure that the record is
6592 sufficiently aligned and that the position and size are consistent
6593 with the type. But don't do it if we are just annotating types and
6594 the field's type is tagged, since tagged types aren't fully laid out
6595 in this mode. Also, note that atomic implies volatile so the inner
6596 test sequences ordering is significant here. */
6597 if (needs_strict_alignment
6598 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6600 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6602 if (TYPE_ALIGN (gnu_record_type) < type_align)
6603 TYPE_ALIGN (gnu_record_type) = type_align;
6605 /* If the position is not a multiple of the alignment of the type,
6606 then error out and reset the position. */
6607 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6608 bitsize_int (type_align))))
6610 const char *s;
6612 if (is_atomic)
6613 s = "position of atomic field& must be multiple of ^ bits";
6614 else if (is_aliased)
6615 s = "position of aliased field& must be multiple of ^ bits";
6616 else if (is_independent)
6617 s = "position of independent field& must be multiple of ^ bits";
6618 else if (is_volatile)
6619 s = "position of volatile field& must be multiple of ^ bits";
6620 else if (Strict_Alignment (gnat_field_type))
6621 s = "position of & with aliased or tagged part must be"
6622 " multiple of ^ bits";
6623 else
6624 gcc_unreachable ();
6626 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6627 type_align);
6628 gnu_pos = NULL_TREE;
6631 if (gnu_size)
6633 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6634 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6636 /* If the size is lower than that of the type, or greater for
6637 atomic and aliased, then error out and reset the size. */
6638 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6640 const char *s;
6642 if (is_atomic)
6643 s = "size of atomic field& must be ^ bits";
6644 else if (is_aliased)
6645 s = "size of aliased field& must be ^ bits";
6646 else if (is_independent)
6647 s = "size of independent field& must be at least ^ bits";
6648 else if (is_volatile)
6649 s = "size of volatile field& must be at least ^ bits";
6650 else if (Strict_Alignment (gnat_field_type))
6651 s = "size of & with aliased or tagged part must be"
6652 " at least ^ bits";
6653 else
6654 gcc_unreachable ();
6656 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6657 gnu_type_size);
6658 gnu_size = NULL_TREE;
6661 /* Likewise if the size is not a multiple of a byte, */
6662 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6663 bitsize_unit_node)))
6665 const char *s;
6667 if (is_independent)
6668 s = "size of independent field& must be multiple of"
6669 " Storage_Unit";
6670 else if (is_volatile)
6671 s = "size of volatile field& must be multiple of"
6672 " Storage_Unit";
6673 else if (Strict_Alignment (gnat_field_type))
6674 s = "size of & with aliased or tagged part must be"
6675 " multiple of Storage_Unit";
6676 else
6677 gcc_unreachable ();
6679 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6680 gnu_size = NULL_TREE;
6686 /* If the record has rep clauses and this is the tag field, make a rep
6687 clause for it as well. */
6688 else if (Has_Specified_Layout (Scope (gnat_field))
6689 && Chars (gnat_field) == Name_uTag)
6691 gnu_pos = bitsize_zero_node;
6692 gnu_size = TYPE_SIZE (gnu_field_type);
6695 else
6697 gnu_pos = NULL_TREE;
6699 /* If we are packing the record and the field is BLKmode, round the
6700 size up to a byte boundary. */
6701 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6702 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6705 /* We need to make the size the maximum for the type if it is
6706 self-referential and an unconstrained type. In that case, we can't
6707 pack the field since we can't make a copy to align it. */
6708 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6709 && !gnu_size
6710 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6711 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6713 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6714 packed = 0;
6717 /* If a size is specified, adjust the field's type to it. */
6718 if (gnu_size)
6720 tree orig_field_type;
6722 /* If the field's type is justified modular, we would need to remove
6723 the wrapper to (better) meet the layout requirements. However we
6724 can do so only if the field is not aliased to preserve the unique
6725 layout and if the prescribed size is not greater than that of the
6726 packed array to preserve the justification. */
6727 if (!needs_strict_alignment
6728 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6729 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6730 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6731 <= 0)
6732 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6734 /* Similarly if the field's type is a misaligned integral type, but
6735 there is no restriction on the size as there is no justification. */
6736 if (!needs_strict_alignment
6737 && TYPE_IS_PADDING_P (gnu_field_type)
6738 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6739 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6741 gnu_field_type
6742 = make_type_from_size (gnu_field_type, gnu_size,
6743 Has_Biased_Representation (gnat_field));
6745 orig_field_type = gnu_field_type;
6746 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6747 false, false, definition, true);
6749 /* If a padding record was made, declare it now since it will never be
6750 declared otherwise. This is necessary to ensure that its subtrees
6751 are properly marked. */
6752 if (gnu_field_type != orig_field_type
6753 && !DECL_P (TYPE_NAME (gnu_field_type)))
6754 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6755 debug_info_p, gnat_field);
6758 /* Otherwise (or if there was an error), don't specify a position. */
6759 else
6760 gnu_pos = NULL_TREE;
6762 /* If the field's type is a padded type made for a scalar field of a record
6763 type with reverse storage order, we need to propagate the reverse storage
6764 order to the padding type since it is the innermost enclosing aggregate
6765 type around the scalar. */
6766 if (TYPE_IS_PADDING_P (gnu_field_type)
6767 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
6768 && Is_Scalar_Type (gnat_field_type))
6769 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
6771 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6772 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6774 /* Now create the decl for the field. */
6775 gnu_field
6776 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6777 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6778 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6779 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6780 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6782 if (Ekind (gnat_field) == E_Discriminant)
6783 DECL_DISCRIMINANT_NUMBER (gnu_field)
6784 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6786 return gnu_field;
6789 /* Return true if at least one member of COMPONENT_LIST needs strict
6790 alignment. */
6792 static bool
6793 components_need_strict_alignment (Node_Id component_list)
6795 Node_Id component_decl;
6797 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6798 Present (component_decl);
6799 component_decl = Next_Non_Pragma (component_decl))
6801 Entity_Id gnat_field = Defining_Entity (component_decl);
6803 if (Is_Aliased (gnat_field))
6804 return true;
6806 if (Strict_Alignment (Etype (gnat_field)))
6807 return true;
6810 return false;
6813 /* Return true if TYPE is a type with variable size or a padding type with a
6814 field of variable size or a record that has a field with such a type. */
6816 static bool
6817 type_has_variable_size (tree type)
6819 tree field;
6821 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6822 return true;
6824 if (TYPE_IS_PADDING_P (type)
6825 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6826 return true;
6828 if (!RECORD_OR_UNION_TYPE_P (type))
6829 return false;
6831 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6832 if (type_has_variable_size (TREE_TYPE (field)))
6833 return true;
6835 return false;
6838 /* Return true if FIELD is an artificial field. */
6840 static bool
6841 field_is_artificial (tree field)
6843 /* These fields are generated by the front-end proper. */
6844 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6845 return true;
6847 /* These fields are generated by gigi. */
6848 if (DECL_INTERNAL_P (field))
6849 return true;
6851 return false;
6854 /* Return true if FIELD is a non-artificial aliased field. */
6856 static bool
6857 field_is_aliased (tree field)
6859 if (field_is_artificial (field))
6860 return false;
6862 return DECL_ALIASED_P (field);
6865 /* Return true if FIELD is a non-artificial field with self-referential
6866 size. */
6868 static bool
6869 field_has_self_size (tree field)
6871 if (field_is_artificial (field))
6872 return false;
6874 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6875 return false;
6877 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6880 /* Return true if FIELD is a non-artificial field with variable size. */
6882 static bool
6883 field_has_variable_size (tree field)
6885 if (field_is_artificial (field))
6886 return false;
6888 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6889 return false;
6891 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6894 /* qsort comparer for the bit positions of two record components. */
6896 static int
6897 compare_field_bitpos (const PTR rt1, const PTR rt2)
6899 const_tree const field1 = * (const_tree const *) rt1;
6900 const_tree const field2 = * (const_tree const *) rt2;
6901 const int ret
6902 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6904 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6907 /* Structure holding information for a given variant. */
6908 typedef struct vinfo
6910 /* The record type of the variant. */
6911 tree type;
6913 /* The name of the variant. */
6914 tree name;
6916 /* The qualifier of the variant. */
6917 tree qual;
6919 /* Whether the variant has a rep clause. */
6920 bool has_rep;
6922 /* Whether the variant is packed. */
6923 bool packed;
6925 } vinfo_t;
6927 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6928 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6929 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6930 When called from gnat_to_gnu_entity during the processing of a record type
6931 definition, the GCC node for the parent, if any, will be the single field
6932 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6933 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6934 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6936 PACKED is 1 if this is for a packed record, -1 if this is for a record
6937 with Component_Alignment of Storage_Unit, -2 if this is for a record
6938 with a specified alignment.
6940 DEFINITION is true if we are defining this record type.
6942 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6943 out the record. This means the alignment only serves to force fields to
6944 be bitfields, but not to require the record to be that aligned. This is
6945 used for variants.
6947 ALL_REP is true if a rep clause is present for all the fields.
6949 UNCHECKED_UNION is true if we are building this type for a record with a
6950 Pragma Unchecked_Union.
6952 ARTIFICIAL is true if this is a type that was generated by the compiler.
6954 DEBUG_INFO is true if we need to write debug information about the type.
6956 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6957 mean that its contents may be unused as well, only the container itself.
6959 REORDER is true if we are permitted to reorder components of this type.
6961 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6962 the outer record type down to this variant level. It is nonzero only if
6963 all the fields down to this level have a rep clause and ALL_REP is false.
6965 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6966 with a rep clause is to be added; in this case, that is all that should
6967 be done with such fields and the return value will be false. */
6969 static bool
6970 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6971 tree gnu_field_list, int packed, bool definition,
6972 bool cancel_alignment, bool all_rep,
6973 bool unchecked_union, bool artificial,
6974 bool debug_info, bool maybe_unused, bool reorder,
6975 tree first_free_pos, tree *p_gnu_rep_list)
6977 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6978 bool variants_have_rep = all_rep;
6979 bool layout_with_rep = false;
6980 bool has_self_field = false;
6981 bool has_aliased_after_self_field = false;
6982 Node_Id component_decl, variant_part;
6983 tree gnu_field, gnu_next, gnu_last;
6984 tree gnu_variant_part = NULL_TREE;
6985 tree gnu_rep_list = NULL_TREE;
6986 tree gnu_var_list = NULL_TREE;
6987 tree gnu_self_list = NULL_TREE;
6988 tree gnu_zero_list = NULL_TREE;
6990 /* For each component referenced in a component declaration create a GCC
6991 field and add it to the list, skipping pragmas in the GNAT list. */
6992 gnu_last = tree_last (gnu_field_list);
6993 if (Present (Component_Items (gnat_component_list)))
6994 for (component_decl
6995 = First_Non_Pragma (Component_Items (gnat_component_list));
6996 Present (component_decl);
6997 component_decl = Next_Non_Pragma (component_decl))
6999 Entity_Id gnat_field = Defining_Entity (component_decl);
7000 Name_Id gnat_name = Chars (gnat_field);
7002 /* If present, the _Parent field must have been created as the single
7003 field of the record type. Put it before any other fields. */
7004 if (gnat_name == Name_uParent)
7006 gnu_field = TYPE_FIELDS (gnu_record_type);
7007 gnu_field_list = chainon (gnu_field_list, gnu_field);
7009 else
7011 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7012 definition, debug_info);
7014 /* If this is the _Tag field, put it before any other fields. */
7015 if (gnat_name == Name_uTag)
7016 gnu_field_list = chainon (gnu_field_list, gnu_field);
7018 /* If this is the _Controller field, put it before the other
7019 fields except for the _Tag or _Parent field. */
7020 else if (gnat_name == Name_uController && gnu_last)
7022 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7023 DECL_CHAIN (gnu_last) = gnu_field;
7026 /* If this is a regular field, put it after the other fields. */
7027 else
7029 DECL_CHAIN (gnu_field) = gnu_field_list;
7030 gnu_field_list = gnu_field;
7031 if (!gnu_last)
7032 gnu_last = gnu_field;
7034 /* And record information for the final layout. */
7035 if (field_has_self_size (gnu_field))
7036 has_self_field = true;
7037 else if (has_self_field && field_is_aliased (gnu_field))
7038 has_aliased_after_self_field = true;
7042 save_gnu_tree (gnat_field, gnu_field, false);
7045 /* At the end of the component list there may be a variant part. */
7046 variant_part = Variant_Part (gnat_component_list);
7048 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7049 mutually exclusive and should go in the same memory. To do this we need
7050 to treat each variant as a record whose elements are created from the
7051 component list for the variant. So here we create the records from the
7052 lists for the variants and put them all into the QUAL_UNION_TYPE.
7053 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7054 use GNU_RECORD_TYPE if there are no fields so far. */
7055 if (Present (variant_part))
7057 Node_Id gnat_discr = Name (variant_part), variant;
7058 tree gnu_discr = gnat_to_gnu (gnat_discr);
7059 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7060 tree gnu_var_name
7061 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7062 "XVN");
7063 tree gnu_union_type, gnu_union_name;
7064 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7065 bool union_field_needs_strict_alignment = false;
7066 auto_vec <vinfo_t, 16> variant_types;
7067 vinfo_t *gnu_variant;
7068 unsigned int variants_align = 0;
7069 unsigned int i;
7071 gnu_union_name
7072 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7074 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7075 are all in the variant part, to match the layout of C unions. There
7076 is an associated check below. */
7077 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7078 gnu_union_type = gnu_record_type;
7079 else
7081 gnu_union_type
7082 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7084 TYPE_NAME (gnu_union_type) = gnu_union_name;
7085 TYPE_ALIGN (gnu_union_type) = 0;
7086 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7087 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7088 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7091 /* If all the fields down to this level have a rep clause, find out
7092 whether all the fields at this level also have one. If so, then
7093 compute the new first free position to be passed downward. */
7094 this_first_free_pos = first_free_pos;
7095 if (this_first_free_pos)
7097 for (gnu_field = gnu_field_list;
7098 gnu_field;
7099 gnu_field = DECL_CHAIN (gnu_field))
7100 if (DECL_FIELD_OFFSET (gnu_field))
7102 tree pos = bit_position (gnu_field);
7103 if (!tree_int_cst_lt (pos, this_first_free_pos))
7104 this_first_free_pos
7105 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7107 else
7109 this_first_free_pos = NULL_TREE;
7110 break;
7114 /* We build the variants in two passes. The bulk of the work is done in
7115 the first pass, that is to say translating the GNAT nodes, building
7116 the container types and computing the associated properties. However
7117 we cannot finish up the container types during this pass because we
7118 don't know where the variant part will be placed until the end. */
7119 for (variant = First_Non_Pragma (Variants (variant_part));
7120 Present (variant);
7121 variant = Next_Non_Pragma (variant))
7123 tree gnu_variant_type = make_node (RECORD_TYPE);
7124 tree gnu_inner_name, gnu_qual;
7125 bool has_rep;
7126 int field_packed;
7127 vinfo_t vinfo;
7129 Get_Variant_Encoding (variant);
7130 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7131 TYPE_NAME (gnu_variant_type)
7132 = concat_name (gnu_union_name,
7133 IDENTIFIER_POINTER (gnu_inner_name));
7135 /* Set the alignment of the inner type in case we need to make
7136 inner objects into bitfields, but then clear it out so the
7137 record actually gets only the alignment required. */
7138 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7139 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7140 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7141 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7143 /* Similarly, if the outer record has a size specified and all
7144 the fields have a rep clause, we can propagate the size. */
7145 if (all_rep_and_size)
7147 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7148 TYPE_SIZE_UNIT (gnu_variant_type)
7149 = TYPE_SIZE_UNIT (gnu_record_type);
7152 /* Add the fields into the record type for the variant. Note that
7153 we aren't sure to really use it at this point, see below. */
7154 has_rep
7155 = components_to_record (gnu_variant_type, Component_List (variant),
7156 NULL_TREE, packed, definition,
7157 !all_rep_and_size, all_rep,
7158 unchecked_union,
7159 true, debug_info, true, reorder,
7160 this_first_free_pos,
7161 all_rep || this_first_free_pos
7162 ? NULL : &gnu_rep_list);
7164 /* Translate the qualifier and annotate the GNAT node. */
7165 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7166 Set_Present_Expr (variant, annotate_value (gnu_qual));
7168 /* Deal with packedness like in gnat_to_gnu_field. */
7169 if (components_need_strict_alignment (Component_List (variant)))
7171 field_packed = 0;
7172 union_field_needs_strict_alignment = true;
7174 else
7175 field_packed
7176 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7178 /* Push this variant onto the stack for the second pass. */
7179 vinfo.type = gnu_variant_type;
7180 vinfo.name = gnu_inner_name;
7181 vinfo.qual = gnu_qual;
7182 vinfo.has_rep = has_rep;
7183 vinfo.packed = field_packed;
7184 variant_types.safe_push (vinfo);
7186 /* Compute the global properties that will determine the placement of
7187 the variant part. */
7188 variants_have_rep |= has_rep;
7189 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7190 variants_align = TYPE_ALIGN (gnu_variant_type);
7193 /* Round up the first free position to the alignment of the variant part
7194 for the variants without rep clause. This will guarantee a consistent
7195 layout independently of the placement of the variant part. */
7196 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7197 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7199 /* In the second pass, the container types are adjusted if necessary and
7200 finished up, then the corresponding fields of the variant part are
7201 built with their qualifier, unless this is an unchecked union. */
7202 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7204 tree gnu_variant_type = gnu_variant->type;
7205 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7207 /* If this is an Unchecked_Union whose fields are all in the variant
7208 part and we have a single field with no representation clause or
7209 placed at offset zero, use the field directly to match the layout
7210 of C unions. */
7211 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7212 && gnu_field_list
7213 && !DECL_CHAIN (gnu_field_list)
7214 && (!DECL_FIELD_OFFSET (gnu_field_list)
7215 || integer_zerop (bit_position (gnu_field_list))))
7217 gnu_field = gnu_field_list;
7218 DECL_CONTEXT (gnu_field) = gnu_record_type;
7220 else
7222 /* Finalize the variant type now. We used to throw away empty
7223 record types but we no longer do that because we need them to
7224 generate complete debug info for the variant; otherwise, the
7225 union type definition will be lacking the fields associated
7226 with these empty variants. */
7227 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7229 /* The variant part will be at offset 0 so we need to ensure
7230 that the fields are laid out starting from the first free
7231 position at this level. */
7232 tree gnu_rep_type = make_node (RECORD_TYPE);
7233 tree gnu_rep_part;
7234 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7235 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7236 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7237 gnu_rep_part
7238 = create_rep_part (gnu_rep_type, gnu_variant_type,
7239 this_first_free_pos);
7240 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7241 gnu_field_list = gnu_rep_part;
7242 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7243 false);
7246 if (debug_info)
7247 rest_of_record_type_compilation (gnu_variant_type);
7248 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7249 true, debug_info, gnat_component_list);
7251 gnu_field
7252 = create_field_decl (gnu_variant->name, gnu_variant_type,
7253 gnu_union_type,
7254 all_rep_and_size
7255 ? TYPE_SIZE (gnu_variant_type) : 0,
7256 variants_have_rep ? bitsize_zero_node : 0,
7257 gnu_variant->packed, 0);
7259 DECL_INTERNAL_P (gnu_field) = 1;
7261 if (!unchecked_union)
7262 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7265 DECL_CHAIN (gnu_field) = gnu_variant_list;
7266 gnu_variant_list = gnu_field;
7269 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7270 if (gnu_variant_list)
7272 int union_field_packed;
7274 if (all_rep_and_size)
7276 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7277 TYPE_SIZE_UNIT (gnu_union_type)
7278 = TYPE_SIZE_UNIT (gnu_record_type);
7281 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7282 all_rep_and_size ? 1 : 0, debug_info);
7284 /* If GNU_UNION_TYPE is our record type, it means we must have an
7285 Unchecked_Union with no fields. Verify that and, if so, just
7286 return. */
7287 if (gnu_union_type == gnu_record_type)
7289 gcc_assert (unchecked_union
7290 && !gnu_field_list
7291 && !gnu_rep_list);
7292 return variants_have_rep;
7295 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7296 debug_info, gnat_component_list);
7298 /* Deal with packedness like in gnat_to_gnu_field. */
7299 if (union_field_needs_strict_alignment)
7300 union_field_packed = 0;
7301 else
7302 union_field_packed
7303 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7305 gnu_variant_part
7306 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7307 all_rep_and_size
7308 ? TYPE_SIZE (gnu_union_type) : 0,
7309 variants_have_rep ? bitsize_zero_node : 0,
7310 union_field_packed, 0);
7312 DECL_INTERNAL_P (gnu_variant_part) = 1;
7316 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7317 permitted to reorder components, self-referential sizes or variable sizes.
7318 If they do, pull them out and put them onto the appropriate list. We have
7319 to do this in a separate pass since we want to handle the discriminants
7320 but can't play with them until we've used them in debugging data above.
7322 Similarly, pull out the fields with zero size and no rep clause, as they
7323 would otherwise modify the layout and thus very likely run afoul of the
7324 Ada semantics, which are different from those of C here.
7326 ??? If we reorder them, debugging information will be wrong but there is
7327 nothing that can be done about this at the moment. */
7328 gnu_last = NULL_TREE;
7330 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7331 do { \
7332 if (gnu_last) \
7333 DECL_CHAIN (gnu_last) = gnu_next; \
7334 else \
7335 gnu_field_list = gnu_next; \
7337 DECL_CHAIN (gnu_field) = (LIST); \
7338 (LIST) = gnu_field; \
7339 } while (0)
7341 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7343 gnu_next = DECL_CHAIN (gnu_field);
7345 if (DECL_FIELD_OFFSET (gnu_field))
7347 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7348 continue;
7351 if ((reorder || has_aliased_after_self_field)
7352 && field_has_self_size (gnu_field))
7354 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7355 continue;
7358 if (reorder && field_has_variable_size (gnu_field))
7360 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7361 continue;
7364 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7366 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7367 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7368 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7369 if (field_is_aliased (gnu_field))
7370 TYPE_ALIGN (gnu_record_type)
7371 = MAX (TYPE_ALIGN (gnu_record_type),
7372 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7373 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7374 continue;
7377 gnu_last = gnu_field;
7380 #undef MOVE_FROM_FIELD_LIST_TO
7382 gnu_field_list = nreverse (gnu_field_list);
7384 /* If permitted, we reorder the fields as follows:
7386 1) all fixed length fields,
7387 2) all fields whose length doesn't depend on discriminants,
7388 3) all fields whose length depends on discriminants,
7389 4) the variant part,
7391 within the record and within each variant recursively. */
7392 if (reorder)
7393 gnu_field_list
7394 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7396 /* Otherwise, if there is an aliased field placed after a field whose length
7397 depends on discriminants, we put all the fields of the latter sort, last.
7398 We need to do this in case an object of this record type is mutable. */
7399 else if (has_aliased_after_self_field)
7400 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7402 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7403 in our REP list to the previous level because this level needs them in
7404 order to do a correct layout, i.e. avoid having overlapping fields. */
7405 if (p_gnu_rep_list && gnu_rep_list)
7406 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7408 /* Otherwise, sort the fields by bit position and put them into their own
7409 record, before the others, if we also have fields without rep clause. */
7410 else if (gnu_rep_list)
7412 tree gnu_rep_type, gnu_rep_part;
7413 int i, len = list_length (gnu_rep_list);
7414 tree *gnu_arr = XALLOCAVEC (tree, len);
7416 /* If all the fields have a rep clause, we can do a flat layout. */
7417 layout_with_rep = !gnu_field_list
7418 && (!gnu_variant_part || variants_have_rep);
7419 gnu_rep_type
7420 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7422 for (gnu_field = gnu_rep_list, i = 0;
7423 gnu_field;
7424 gnu_field = DECL_CHAIN (gnu_field), i++)
7425 gnu_arr[i] = gnu_field;
7427 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7429 /* Put the fields in the list in order of increasing position, which
7430 means we start from the end. */
7431 gnu_rep_list = NULL_TREE;
7432 for (i = len - 1; i >= 0; i--)
7434 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7435 gnu_rep_list = gnu_arr[i];
7436 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7439 if (layout_with_rep)
7440 gnu_field_list = gnu_rep_list;
7441 else
7443 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7444 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7445 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7447 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7448 without rep clause are laid out starting from this position.
7449 Therefore, we force it as a minimal size on the REP part. */
7450 gnu_rep_part
7451 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7453 /* Chain the REP part at the beginning of the field list. */
7454 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7455 gnu_field_list = gnu_rep_part;
7459 /* Chain the variant part at the end of the field list. */
7460 if (gnu_variant_part)
7461 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7463 if (cancel_alignment)
7464 TYPE_ALIGN (gnu_record_type) = 0;
7466 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7468 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7469 debug_info && !maybe_unused);
7471 /* Chain the fields with zero size at the beginning of the field list. */
7472 if (gnu_zero_list)
7473 TYPE_FIELDS (gnu_record_type)
7474 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7476 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7479 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7480 placed into an Esize, Component_Bit_Offset, or Component_Size value
7481 in the GNAT tree. */
7483 static Uint
7484 annotate_value (tree gnu_size)
7486 TCode tcode;
7487 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7488 struct tree_int_map in;
7489 int i;
7491 /* See if we've already saved the value for this node. */
7492 if (EXPR_P (gnu_size))
7494 struct tree_int_map *e;
7496 in.base.from = gnu_size;
7497 e = annotate_value_cache->find (&in);
7499 if (e)
7500 return (Node_Ref_Or_Val) e->to;
7502 else
7503 in.base.from = NULL_TREE;
7505 /* If we do not return inside this switch, TCODE will be set to the
7506 code to use for a Create_Node operand and LEN (set above) will be
7507 the number of recursive calls for us to make. */
7509 switch (TREE_CODE (gnu_size))
7511 case INTEGER_CST:
7512 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7514 case COMPONENT_REF:
7515 /* The only case we handle here is a simple discriminant reference. */
7516 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7518 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7520 /* Climb up the chain of successive extensions, if any. */
7521 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7522 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7523 == parent_name_id)
7524 gnu_size = TREE_OPERAND (gnu_size, 0);
7526 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7527 return
7528 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7531 return No_Uint;
7533 CASE_CONVERT: case NON_LVALUE_EXPR:
7534 return annotate_value (TREE_OPERAND (gnu_size, 0));
7536 /* Now just list the operations we handle. */
7537 case COND_EXPR: tcode = Cond_Expr; break;
7538 case PLUS_EXPR: tcode = Plus_Expr; break;
7539 case MINUS_EXPR: tcode = Minus_Expr; break;
7540 case MULT_EXPR: tcode = Mult_Expr; break;
7541 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7542 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7543 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7544 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7545 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7546 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7547 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7548 case NEGATE_EXPR: tcode = Negate_Expr; break;
7549 case MIN_EXPR: tcode = Min_Expr; break;
7550 case MAX_EXPR: tcode = Max_Expr; break;
7551 case ABS_EXPR: tcode = Abs_Expr; break;
7552 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7553 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7554 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7555 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7556 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7557 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7558 case LT_EXPR: tcode = Lt_Expr; break;
7559 case LE_EXPR: tcode = Le_Expr; break;
7560 case GT_EXPR: tcode = Gt_Expr; break;
7561 case GE_EXPR: tcode = Ge_Expr; break;
7562 case EQ_EXPR: tcode = Eq_Expr; break;
7563 case NE_EXPR: tcode = Ne_Expr; break;
7565 case BIT_AND_EXPR:
7566 tcode = Bit_And_Expr;
7567 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7568 Such values appear in expressions with aligning patterns. Note that,
7569 since sizetype is unsigned, we have to jump through some hoops. */
7570 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7572 tree op1 = TREE_OPERAND (gnu_size, 1);
7573 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7574 if (wi::neg_p (signed_op1))
7576 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7577 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7580 break;
7582 case CALL_EXPR:
7583 /* In regular mode, inline back only if symbolic annotation is requested
7584 in order to avoid memory explosion on big discriminated record types.
7585 But not in ASIS mode, as symbolic annotation is required for DDA. */
7586 if (List_Representation_Info == 3 || type_annotate_only)
7588 tree t = maybe_inline_call_in_expr (gnu_size);
7589 if (t)
7590 return annotate_value (t);
7592 else
7593 return Uint_Minus_1;
7595 /* Fall through... */
7597 default:
7598 return No_Uint;
7601 /* Now get each of the operands that's relevant for this code. If any
7602 cannot be expressed as a repinfo node, say we can't. */
7603 for (i = 0; i < 3; i++)
7604 ops[i] = No_Uint;
7606 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7608 if (i == 1 && pre_op1 != No_Uint)
7609 ops[i] = pre_op1;
7610 else
7611 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7612 if (ops[i] == No_Uint)
7613 return No_Uint;
7616 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7618 /* Save the result in the cache. */
7619 if (in.base.from)
7621 struct tree_int_map **h;
7622 /* We can't assume the hash table data hasn't moved since the initial
7623 look up, so we have to search again. Allocating and inserting an
7624 entry at that point would be an alternative, but then we'd better
7625 discard the entry if we decided not to cache it. */
7626 h = annotate_value_cache->find_slot (&in, INSERT);
7627 gcc_assert (!*h);
7628 *h = ggc_alloc<tree_int_map> ();
7629 (*h)->base.from = gnu_size;
7630 (*h)->to = ret;
7633 return ret;
7636 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7637 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7638 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7639 BY_REF is true if the object is used by reference. */
7641 void
7642 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7644 if (by_ref)
7646 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7647 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7648 else
7649 gnu_type = TREE_TYPE (gnu_type);
7652 if (Unknown_Esize (gnat_entity))
7654 if (TREE_CODE (gnu_type) == RECORD_TYPE
7655 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7656 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7657 else if (!size)
7658 size = TYPE_SIZE (gnu_type);
7660 if (size)
7661 Set_Esize (gnat_entity, annotate_value (size));
7664 if (Unknown_Alignment (gnat_entity))
7665 Set_Alignment (gnat_entity,
7666 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7669 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7670 Return NULL_TREE if there is no such element in the list. */
7672 static tree
7673 purpose_member_field (const_tree elem, tree list)
7675 while (list)
7677 tree field = TREE_PURPOSE (list);
7678 if (SAME_FIELD_P (field, elem))
7679 return list;
7680 list = TREE_CHAIN (list);
7682 return NULL_TREE;
7685 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7686 set Component_Bit_Offset and Esize of the components to the position and
7687 size used by Gigi. */
7689 static void
7690 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7692 Entity_Id gnat_field;
7693 tree gnu_list;
7695 /* We operate by first making a list of all fields and their position (we
7696 can get the size easily) and then update all the sizes in the tree. */
7697 gnu_list
7698 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7699 BIGGEST_ALIGNMENT, NULL_TREE);
7701 for (gnat_field = First_Entity (gnat_entity);
7702 Present (gnat_field);
7703 gnat_field = Next_Entity (gnat_field))
7704 if (Ekind (gnat_field) == E_Component
7705 || (Ekind (gnat_field) == E_Discriminant
7706 && !Is_Unchecked_Union (Scope (gnat_field))))
7708 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7709 gnu_list);
7710 if (t)
7712 tree parent_offset;
7714 /* If we are just annotating types and the type is tagged, the tag
7715 and the parent components are not generated by the front-end so
7716 we need to add the appropriate offset to each component without
7717 representation clause. */
7718 if (type_annotate_only
7719 && Is_Tagged_Type (gnat_entity)
7720 && No (Component_Clause (gnat_field)))
7722 /* For a component appearing in the current extension, the
7723 offset is the size of the parent. */
7724 if (Is_Derived_Type (gnat_entity)
7725 && Original_Record_Component (gnat_field) == gnat_field)
7726 parent_offset
7727 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7728 bitsizetype);
7729 else
7730 parent_offset = bitsize_int (POINTER_SIZE);
7732 if (TYPE_FIELDS (gnu_type))
7733 parent_offset
7734 = round_up (parent_offset,
7735 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7737 else
7738 parent_offset = bitsize_zero_node;
7740 Set_Component_Bit_Offset
7741 (gnat_field,
7742 annotate_value
7743 (size_binop (PLUS_EXPR,
7744 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7745 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7746 parent_offset)));
7748 Set_Esize (gnat_field,
7749 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7751 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7753 /* If there is no entry, this is an inherited component whose
7754 position is the same as in the parent type. */
7755 Set_Component_Bit_Offset
7756 (gnat_field,
7757 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7759 Set_Esize (gnat_field,
7760 Esize (Original_Record_Component (gnat_field)));
7765 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7766 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7767 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7768 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7769 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7770 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7771 pre-existing list to be chained to the newly created entries. */
7773 static tree
7774 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7775 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7777 tree gnu_field;
7779 for (gnu_field = TYPE_FIELDS (gnu_type);
7780 gnu_field;
7781 gnu_field = DECL_CHAIN (gnu_field))
7783 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7784 DECL_FIELD_BIT_OFFSET (gnu_field));
7785 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7786 DECL_FIELD_OFFSET (gnu_field));
7787 unsigned int our_offset_align
7788 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7789 tree v = make_tree_vec (3);
7791 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7792 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7793 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7794 gnu_list = tree_cons (gnu_field, v, gnu_list);
7796 /* Recurse on internal fields, flattening the nested fields except for
7797 those in the variant part, if requested. */
7798 if (DECL_INTERNAL_P (gnu_field))
7800 tree gnu_field_type = TREE_TYPE (gnu_field);
7801 if (do_not_flatten_variant
7802 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7803 gnu_list
7804 = build_position_list (gnu_field_type, do_not_flatten_variant,
7805 size_zero_node, bitsize_zero_node,
7806 BIGGEST_ALIGNMENT, gnu_list);
7807 else
7808 gnu_list
7809 = build_position_list (gnu_field_type, do_not_flatten_variant,
7810 gnu_our_offset, gnu_our_bitpos,
7811 our_offset_align, gnu_list);
7815 return gnu_list;
7818 /* Return a list describing the substitutions needed to reflect the
7819 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7820 be in any order. The values in an element of the list are in the form
7821 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7822 a definition of GNAT_SUBTYPE. */
7824 static vec<subst_pair>
7825 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7827 vec<subst_pair> gnu_list = vNULL;
7828 Entity_Id gnat_discrim;
7829 Node_Id gnat_constr;
7831 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7832 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7833 Present (gnat_discrim);
7834 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7835 gnat_constr = Next_Elmt (gnat_constr))
7836 /* Ignore access discriminants. */
7837 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7839 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7840 tree replacement = convert (TREE_TYPE (gnu_field),
7841 elaborate_expression
7842 (Node (gnat_constr), gnat_subtype,
7843 get_entity_name (gnat_discrim),
7844 definition, true, false));
7845 subst_pair s = {gnu_field, replacement};
7846 gnu_list.safe_push (s);
7849 return gnu_list;
7852 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7853 variants of QUAL_UNION_TYPE that are still relevant after applying
7854 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7855 list to be prepended to the newly created entries. */
7857 static vec<variant_desc>
7858 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7859 vec<variant_desc> gnu_list)
7861 tree gnu_field;
7863 for (gnu_field = TYPE_FIELDS (qual_union_type);
7864 gnu_field;
7865 gnu_field = DECL_CHAIN (gnu_field))
7867 tree qual = DECL_QUALIFIER (gnu_field);
7868 unsigned int i;
7869 subst_pair *s;
7871 FOR_EACH_VEC_ELT (subst_list, i, s)
7872 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7874 /* If the new qualifier is not unconditionally false, its variant may
7875 still be accessed. */
7876 if (!integer_zerop (qual))
7878 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7879 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7881 gnu_list.safe_push (v);
7883 /* Recurse on the variant subpart of the variant, if any. */
7884 variant_subpart = get_variant_part (variant_type);
7885 if (variant_subpart)
7886 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7887 subst_list, gnu_list);
7889 /* If the new qualifier is unconditionally true, the subsequent
7890 variants cannot be accessed. */
7891 if (integer_onep (qual))
7892 break;
7896 return gnu_list;
7899 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7900 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7901 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7902 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7903 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7904 true if we are being called to process the Component_Size of GNAT_OBJECT;
7905 this is used only for error messages. ZERO_OK is true if a size of zero
7906 is permitted; if ZERO_OK is false, it means that a size of zero should be
7907 treated as an unspecified size. */
7909 static tree
7910 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7911 enum tree_code kind, bool component_p, bool zero_ok)
7913 Node_Id gnat_error_node;
7914 tree type_size, size;
7916 /* Return 0 if no size was specified. */
7917 if (uint_size == No_Uint)
7918 return NULL_TREE;
7920 /* Ignore a negative size since that corresponds to our back-annotation. */
7921 if (UI_Lt (uint_size, Uint_0))
7922 return NULL_TREE;
7924 /* Find the node to use for error messages. */
7925 if ((Ekind (gnat_object) == E_Component
7926 || Ekind (gnat_object) == E_Discriminant)
7927 && Present (Component_Clause (gnat_object)))
7928 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7929 else if (Present (Size_Clause (gnat_object)))
7930 gnat_error_node = Expression (Size_Clause (gnat_object));
7931 else
7932 gnat_error_node = gnat_object;
7934 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7935 but cannot be represented in bitsizetype. */
7936 size = UI_To_gnu (uint_size, bitsizetype);
7937 if (TREE_OVERFLOW (size))
7939 if (component_p)
7940 post_error_ne ("component size for& is too large", gnat_error_node,
7941 gnat_object);
7942 else
7943 post_error_ne ("size for& is too large", gnat_error_node,
7944 gnat_object);
7945 return NULL_TREE;
7948 /* Ignore a zero size if it is not permitted. */
7949 if (!zero_ok && integer_zerop (size))
7950 return NULL_TREE;
7952 /* The size of objects is always a multiple of a byte. */
7953 if (kind == VAR_DECL
7954 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7956 if (component_p)
7957 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7958 gnat_error_node, gnat_object);
7959 else
7960 post_error_ne ("size for& is not a multiple of Storage_Unit",
7961 gnat_error_node, gnat_object);
7962 return NULL_TREE;
7965 /* If this is an integral type or a packed array type, the front-end has
7966 already verified the size, so we need not do it here (which would mean
7967 checking against the bounds). However, if this is an aliased object,
7968 it may not be smaller than the type of the object. */
7969 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7970 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7971 return size;
7973 /* If the object is a record that contains a template, add the size of the
7974 template to the specified size. */
7975 if (TREE_CODE (gnu_type) == RECORD_TYPE
7976 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7977 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7979 if (kind == VAR_DECL
7980 /* If a type needs strict alignment, a component of this type in
7981 a packed record cannot be packed and thus uses the type size. */
7982 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7983 type_size = TYPE_SIZE (gnu_type);
7984 else
7985 type_size = rm_size (gnu_type);
7987 /* Modify the size of a discriminated type to be the maximum size. */
7988 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7989 type_size = max_size (type_size, true);
7991 /* If this is an access type or a fat pointer, the minimum size is that given
7992 by the smallest integral mode that's valid for pointers. */
7993 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7995 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7996 while (!targetm.valid_pointer_mode (p_mode))
7997 p_mode = GET_MODE_WIDER_MODE (p_mode);
7998 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8001 /* Issue an error either if the default size of the object isn't a constant
8002 or if the new size is smaller than it. */
8003 if (TREE_CODE (type_size) != INTEGER_CST
8004 || TREE_OVERFLOW (type_size)
8005 || tree_int_cst_lt (size, type_size))
8007 if (component_p)
8008 post_error_ne_tree
8009 ("component size for& too small{, minimum allowed is ^}",
8010 gnat_error_node, gnat_object, type_size);
8011 else
8012 post_error_ne_tree
8013 ("size for& too small{, minimum allowed is ^}",
8014 gnat_error_node, gnat_object, type_size);
8015 return NULL_TREE;
8018 return size;
8021 /* Similarly, but both validate and process a value of RM size. This routine
8022 is only called for types. */
8024 static void
8025 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8027 Node_Id gnat_attr_node;
8028 tree old_size, size;
8030 /* Do nothing if no size was specified. */
8031 if (uint_size == No_Uint)
8032 return;
8034 /* Ignore a negative size since that corresponds to our back-annotation. */
8035 if (UI_Lt (uint_size, Uint_0))
8036 return;
8038 /* Only issue an error if a Value_Size clause was explicitly given.
8039 Otherwise, we'd be duplicating an error on the Size clause. */
8040 gnat_attr_node
8041 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8043 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8044 but cannot be represented in bitsizetype. */
8045 size = UI_To_gnu (uint_size, bitsizetype);
8046 if (TREE_OVERFLOW (size))
8048 if (Present (gnat_attr_node))
8049 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8050 gnat_entity);
8051 return;
8054 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8055 exists, or this is an integer type, in which case the front-end will
8056 have always set it. */
8057 if (No (gnat_attr_node)
8058 && integer_zerop (size)
8059 && !Has_Size_Clause (gnat_entity)
8060 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8061 return;
8063 old_size = rm_size (gnu_type);
8065 /* If the old size is self-referential, get the maximum size. */
8066 if (CONTAINS_PLACEHOLDER_P (old_size))
8067 old_size = max_size (old_size, true);
8069 /* Issue an error either if the old size of the object isn't a constant or
8070 if the new size is smaller than it. The front-end has already verified
8071 this for scalar and packed array types. */
8072 if (TREE_CODE (old_size) != INTEGER_CST
8073 || TREE_OVERFLOW (old_size)
8074 || (AGGREGATE_TYPE_P (gnu_type)
8075 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8076 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8077 && !(TYPE_IS_PADDING_P (gnu_type)
8078 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8079 && TYPE_PACKED_ARRAY_TYPE_P
8080 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8081 && tree_int_cst_lt (size, old_size)))
8083 if (Present (gnat_attr_node))
8084 post_error_ne_tree
8085 ("Value_Size for& too small{, minimum allowed is ^}",
8086 gnat_attr_node, gnat_entity, old_size);
8087 return;
8090 /* Otherwise, set the RM size proper for integral types... */
8091 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8092 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8093 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8094 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8095 SET_TYPE_RM_SIZE (gnu_type, size);
8097 /* ...or the Ada size for record and union types. */
8098 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8099 && !TYPE_FAT_POINTER_P (gnu_type))
8100 SET_TYPE_ADA_SIZE (gnu_type, size);
8103 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8104 a type or object whose present alignment is ALIGN. If this alignment is
8105 valid, return it. Otherwise, give an error and return ALIGN. */
8107 static unsigned int
8108 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8110 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8111 unsigned int new_align;
8112 Node_Id gnat_error_node;
8114 /* Don't worry about checking alignment if alignment was not specified
8115 by the source program and we already posted an error for this entity. */
8116 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8117 return align;
8119 /* Post the error on the alignment clause if any. Note, for the implicit
8120 base type of an array type, the alignment clause is on the first
8121 subtype. */
8122 if (Present (Alignment_Clause (gnat_entity)))
8123 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8125 else if (Is_Itype (gnat_entity)
8126 && Is_Array_Type (gnat_entity)
8127 && Etype (gnat_entity) == gnat_entity
8128 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8129 gnat_error_node =
8130 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8132 else
8133 gnat_error_node = gnat_entity;
8135 /* Within GCC, an alignment is an integer, so we must make sure a value is
8136 specified that fits in that range. Also, there is an upper bound to
8137 alignments we can support/allow. */
8138 if (!UI_Is_In_Int_Range (alignment)
8139 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8140 post_error_ne_num ("largest supported alignment for& is ^",
8141 gnat_error_node, gnat_entity, max_allowed_alignment);
8142 else if (!(Present (Alignment_Clause (gnat_entity))
8143 && From_At_Mod (Alignment_Clause (gnat_entity)))
8144 && new_align * BITS_PER_UNIT < align)
8146 unsigned int double_align;
8147 bool is_capped_double, align_clause;
8149 /* If the default alignment of "double" or larger scalar types is
8150 specifically capped and the new alignment is above the cap, do
8151 not post an error and change the alignment only if there is an
8152 alignment clause; this makes it possible to have the associated
8153 GCC type overaligned by default for performance reasons. */
8154 if ((double_align = double_float_alignment) > 0)
8156 Entity_Id gnat_type
8157 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8158 is_capped_double
8159 = is_double_float_or_array (gnat_type, &align_clause);
8161 else if ((double_align = double_scalar_alignment) > 0)
8163 Entity_Id gnat_type
8164 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8165 is_capped_double
8166 = is_double_scalar_or_array (gnat_type, &align_clause);
8168 else
8169 is_capped_double = align_clause = false;
8171 if (is_capped_double && new_align >= double_align)
8173 if (align_clause)
8174 align = new_align * BITS_PER_UNIT;
8176 else
8178 if (is_capped_double)
8179 align = double_align * BITS_PER_UNIT;
8181 post_error_ne_num ("alignment for& must be at least ^",
8182 gnat_error_node, gnat_entity,
8183 align / BITS_PER_UNIT);
8186 else
8188 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8189 if (new_align > align)
8190 align = new_align;
8193 return align;
8196 /* Verify that TYPE is something we can implement atomically. If not, issue
8197 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8198 process a component type. */
8200 static void
8201 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8203 Node_Id gnat_error_point = gnat_entity;
8204 Node_Id gnat_node;
8205 machine_mode mode;
8206 enum mode_class mclass;
8207 unsigned int align;
8208 tree size;
8210 /* If this is an anonymous base type, nothing to check, the error will be
8211 reported on the source type if need be. */
8212 if (!Comes_From_Source (gnat_entity))
8213 return;
8215 mode = TYPE_MODE (type);
8216 mclass = GET_MODE_CLASS (mode);
8217 align = TYPE_ALIGN (type);
8218 size = TYPE_SIZE (type);
8220 /* Consider all aligned floating-point types atomic and any aligned types
8221 that are represented by integers no wider than a machine word. */
8222 if ((mclass == MODE_FLOAT
8223 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8224 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8225 && align >= GET_MODE_ALIGNMENT (mode))
8226 return;
8228 /* For the moment, also allow anything that has an alignment equal to its
8229 size and which is smaller than a word. */
8230 if (size
8231 && TREE_CODE (size) == INTEGER_CST
8232 && compare_tree_int (size, align) == 0
8233 && align <= BITS_PER_WORD)
8234 return;
8236 for (gnat_node = First_Rep_Item (gnat_entity);
8237 Present (gnat_node);
8238 gnat_node = Next_Rep_Item (gnat_node))
8239 if (Nkind (gnat_node) == N_Pragma)
8241 unsigned char pragma_id
8242 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8244 if ((pragma_id == Pragma_Atomic && !component_p)
8245 || (pragma_id == Pragma_Atomic_Components && component_p))
8247 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8248 break;
8252 if (component_p)
8253 post_error_ne ("atomic access to component of & cannot be guaranteed",
8254 gnat_error_point, gnat_entity);
8255 else
8256 post_error_ne ("atomic access to & cannot be guaranteed",
8257 gnat_error_point, gnat_entity);
8261 /* Helper for the intrin compatibility checks family. Evaluate whether
8262 two types are definitely incompatible. */
8264 static bool
8265 intrin_types_incompatible_p (tree t1, tree t2)
8267 enum tree_code code;
8269 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8270 return false;
8272 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8273 return true;
8275 if (TREE_CODE (t1) != TREE_CODE (t2))
8276 return true;
8278 code = TREE_CODE (t1);
8280 switch (code)
8282 case INTEGER_TYPE:
8283 case REAL_TYPE:
8284 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8286 case POINTER_TYPE:
8287 case REFERENCE_TYPE:
8288 /* Assume designated types are ok. We'd need to account for char * and
8289 void * variants to do better, which could rapidly get messy and isn't
8290 clearly worth the effort. */
8291 return false;
8293 default:
8294 break;
8297 return false;
8300 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8301 on the Ada/builtin argument lists for the INB binding. */
8303 static bool
8304 intrin_arglists_compatible_p (intrin_binding_t * inb)
8306 function_args_iterator ada_iter, btin_iter;
8308 function_args_iter_init (&ada_iter, inb->ada_fntype);
8309 function_args_iter_init (&btin_iter, inb->btin_fntype);
8311 /* Sequence position of the last argument we checked. */
8312 int argpos = 0;
8314 while (1)
8316 tree ada_type = function_args_iter_cond (&ada_iter);
8317 tree btin_type = function_args_iter_cond (&btin_iter);
8319 /* If we've exhausted both lists simultaneously, we're done. */
8320 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8321 break;
8323 /* If one list is shorter than the other, they fail to match. */
8324 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8325 return false;
8327 /* If we're done with the Ada args and not with the internal builtin
8328 args, or the other way around, complain. */
8329 if (ada_type == void_type_node
8330 && btin_type != void_type_node)
8332 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8333 return false;
8336 if (btin_type == void_type_node
8337 && ada_type != void_type_node)
8339 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8340 inb->gnat_entity, inb->gnat_entity, argpos);
8341 return false;
8344 /* Otherwise, check that types match for the current argument. */
8345 argpos ++;
8346 if (intrin_types_incompatible_p (ada_type, btin_type))
8348 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8349 inb->gnat_entity, inb->gnat_entity, argpos);
8350 return false;
8354 function_args_iter_next (&ada_iter);
8355 function_args_iter_next (&btin_iter);
8358 return true;
8361 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8362 on the Ada/builtin return values for the INB binding. */
8364 static bool
8365 intrin_return_compatible_p (intrin_binding_t * inb)
8367 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8368 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8370 /* Accept function imported as procedure, common and convenient. */
8371 if (VOID_TYPE_P (ada_return_type)
8372 && !VOID_TYPE_P (btin_return_type))
8373 return true;
8375 /* If return type is Address (integer type), map it to void *. */
8376 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8377 ada_return_type = ptr_void_type_node;
8379 /* Check return types compatibility otherwise. Note that this
8380 handles void/void as well. */
8381 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8383 post_error ("?intrinsic binding type mismatch on return value!",
8384 inb->gnat_entity);
8385 return false;
8388 return true;
8391 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8392 compatible. Issue relevant warnings when they are not.
8394 This is intended as a light check to diagnose the most obvious cases, not
8395 as a full fledged type compatibility predicate. It is the programmer's
8396 responsibility to ensure correctness of the Ada declarations in Imports,
8397 especially when binding straight to a compiler internal. */
8399 static bool
8400 intrin_profiles_compatible_p (intrin_binding_t * inb)
8402 /* Check compatibility on return values and argument lists, each responsible
8403 for posting warnings as appropriate. Ensure use of the proper sloc for
8404 this purpose. */
8406 bool arglists_compatible_p, return_compatible_p;
8407 location_t saved_location = input_location;
8409 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8411 return_compatible_p = intrin_return_compatible_p (inb);
8412 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8414 input_location = saved_location;
8416 return return_compatible_p && arglists_compatible_p;
8419 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8420 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8421 specified size for this field. POS_LIST is a position list describing
8422 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8423 to this layout. */
8425 static tree
8426 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8427 tree size, tree pos_list,
8428 vec<subst_pair> subst_list)
8430 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8431 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8432 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8433 tree new_pos, new_field;
8434 unsigned int i;
8435 subst_pair *s;
8437 if (CONTAINS_PLACEHOLDER_P (pos))
8438 FOR_EACH_VEC_ELT (subst_list, i, s)
8439 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8441 /* If the position is now a constant, we can set it as the position of the
8442 field when we make it. Otherwise, we need to deal with it specially. */
8443 if (TREE_CONSTANT (pos))
8444 new_pos = bit_from_pos (pos, bitpos);
8445 else
8446 new_pos = NULL_TREE;
8448 new_field
8449 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8450 size, new_pos, DECL_PACKED (old_field),
8451 !DECL_NONADDRESSABLE_P (old_field));
8453 if (!new_pos)
8455 normalize_offset (&pos, &bitpos, offset_align);
8456 /* Finalize the position. */
8457 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8458 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8459 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8460 DECL_SIZE (new_field) = size;
8461 DECL_SIZE_UNIT (new_field)
8462 = convert (sizetype,
8463 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8464 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8467 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8468 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8469 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8470 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8472 return new_field;
8475 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8476 it is the minimal size the REP_PART must have. */
8478 static tree
8479 create_rep_part (tree rep_type, tree record_type, tree min_size)
8481 tree field;
8483 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8484 min_size = NULL_TREE;
8486 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8487 min_size, NULL_TREE, 0, 1);
8488 DECL_INTERNAL_P (field) = 1;
8490 return field;
8493 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8495 static tree
8496 get_rep_part (tree record_type)
8498 tree field = TYPE_FIELDS (record_type);
8500 /* The REP part is the first field, internal, another record, and its name
8501 starts with an 'R'. */
8502 if (field
8503 && DECL_INTERNAL_P (field)
8504 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8505 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8506 return field;
8508 return NULL_TREE;
8511 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8513 tree
8514 get_variant_part (tree record_type)
8516 tree field;
8518 /* The variant part is the only internal field that is a qualified union. */
8519 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8520 if (DECL_INTERNAL_P (field)
8521 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8522 return field;
8524 return NULL_TREE;
8527 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8528 the list of variants to be used and RECORD_TYPE is the type of the parent.
8529 POS_LIST is a position list describing the layout of fields present in
8530 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8531 layout. */
8533 static tree
8534 create_variant_part_from (tree old_variant_part,
8535 vec<variant_desc> variant_list,
8536 tree record_type, tree pos_list,
8537 vec<subst_pair> subst_list)
8539 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8540 tree old_union_type = TREE_TYPE (old_variant_part);
8541 tree new_union_type, new_variant_part;
8542 tree union_field_list = NULL_TREE;
8543 variant_desc *v;
8544 unsigned int i;
8546 /* First create the type of the variant part from that of the old one. */
8547 new_union_type = make_node (QUAL_UNION_TYPE);
8548 TYPE_NAME (new_union_type)
8549 = concat_name (TYPE_NAME (record_type),
8550 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8552 /* If the position of the variant part is constant, subtract it from the
8553 size of the type of the parent to get the new size. This manual CSE
8554 reduces the code size when not optimizing. */
8555 if (TREE_CODE (offset) == INTEGER_CST)
8557 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8558 tree first_bit = bit_from_pos (offset, bitpos);
8559 TYPE_SIZE (new_union_type)
8560 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8561 TYPE_SIZE_UNIT (new_union_type)
8562 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8563 byte_from_pos (offset, bitpos));
8564 SET_TYPE_ADA_SIZE (new_union_type,
8565 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8566 first_bit));
8567 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8568 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8570 else
8571 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8573 /* Now finish up the new variants and populate the union type. */
8574 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8576 tree old_field = v->field, new_field;
8577 tree old_variant, old_variant_subpart, new_variant, field_list;
8579 /* Skip variants that don't belong to this nesting level. */
8580 if (DECL_CONTEXT (old_field) != old_union_type)
8581 continue;
8583 /* Retrieve the list of fields already added to the new variant. */
8584 new_variant = v->new_type;
8585 field_list = TYPE_FIELDS (new_variant);
8587 /* If the old variant had a variant subpart, we need to create a new
8588 variant subpart and add it to the field list. */
8589 old_variant = v->type;
8590 old_variant_subpart = get_variant_part (old_variant);
8591 if (old_variant_subpart)
8593 tree new_variant_subpart
8594 = create_variant_part_from (old_variant_subpart, variant_list,
8595 new_variant, pos_list, subst_list);
8596 DECL_CHAIN (new_variant_subpart) = field_list;
8597 field_list = new_variant_subpart;
8600 /* Finish up the new variant and create the field. No need for debug
8601 info thanks to the XVS type. */
8602 finish_record_type (new_variant, nreverse (field_list), 2, false);
8603 compute_record_mode (new_variant);
8604 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8605 Empty);
8607 new_field
8608 = create_field_decl_from (old_field, new_variant, new_union_type,
8609 TYPE_SIZE (new_variant),
8610 pos_list, subst_list);
8611 DECL_QUALIFIER (new_field) = v->qual;
8612 DECL_INTERNAL_P (new_field) = 1;
8613 DECL_CHAIN (new_field) = union_field_list;
8614 union_field_list = new_field;
8617 /* Finish up the union type and create the variant part. No need for debug
8618 info thanks to the XVS type. Note that we don't reverse the field list
8619 because VARIANT_LIST has been traversed in reverse order. */
8620 finish_record_type (new_union_type, union_field_list, 2, false);
8621 compute_record_mode (new_union_type);
8622 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8623 Empty);
8625 new_variant_part
8626 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8627 TYPE_SIZE (new_union_type),
8628 pos_list, subst_list);
8629 DECL_INTERNAL_P (new_variant_part) = 1;
8631 /* With multiple discriminants it is possible for an inner variant to be
8632 statically selected while outer ones are not; in this case, the list
8633 of fields of the inner variant is not flattened and we end up with a
8634 qualified union with a single member. Drop the useless container. */
8635 if (!DECL_CHAIN (union_field_list))
8637 DECL_CONTEXT (union_field_list) = record_type;
8638 DECL_FIELD_OFFSET (union_field_list)
8639 = DECL_FIELD_OFFSET (new_variant_part);
8640 DECL_FIELD_BIT_OFFSET (union_field_list)
8641 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8642 SET_DECL_OFFSET_ALIGN (union_field_list,
8643 DECL_OFFSET_ALIGN (new_variant_part));
8644 new_variant_part = union_field_list;
8647 return new_variant_part;
8650 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8651 which are both RECORD_TYPE, after applying the substitutions described
8652 in SUBST_LIST. */
8654 static void
8655 copy_and_substitute_in_size (tree new_type, tree old_type,
8656 vec<subst_pair> subst_list)
8658 unsigned int i;
8659 subst_pair *s;
8661 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8662 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8663 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8664 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8665 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8667 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8668 FOR_EACH_VEC_ELT (subst_list, i, s)
8669 TYPE_SIZE (new_type)
8670 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8671 s->discriminant, s->replacement);
8673 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8674 FOR_EACH_VEC_ELT (subst_list, i, s)
8675 TYPE_SIZE_UNIT (new_type)
8676 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8677 s->discriminant, s->replacement);
8679 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8680 FOR_EACH_VEC_ELT (subst_list, i, s)
8681 SET_TYPE_ADA_SIZE
8682 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8683 s->discriminant, s->replacement));
8685 /* Finalize the size. */
8686 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8687 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8690 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8691 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8692 The parallel type is the original array type if it has been translated. */
8694 static void
8695 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8697 Entity_Id gnat_original_array_type
8698 = Underlying_Type (Original_Array_Type (gnat_entity));
8699 tree gnu_original_array_type;
8701 if (!present_gnu_tree (gnat_original_array_type))
8702 return;
8704 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8706 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8707 return;
8709 add_parallel_type (gnu_type, gnu_original_array_type);
8712 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8713 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8714 updated by replacing F with R.
8716 The function doesn't update the layout of the type, i.e. it assumes
8717 that the substitution is purely formal. That's why the replacement
8718 value R must itself contain a PLACEHOLDER_EXPR. */
8720 tree
8721 substitute_in_type (tree t, tree f, tree r)
8723 tree nt;
8725 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8727 switch (TREE_CODE (t))
8729 case INTEGER_TYPE:
8730 case ENUMERAL_TYPE:
8731 case BOOLEAN_TYPE:
8732 case REAL_TYPE:
8734 /* First the domain types of arrays. */
8735 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8736 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8738 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8739 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8741 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8742 return t;
8744 nt = copy_type (t);
8745 TYPE_GCC_MIN_VALUE (nt) = low;
8746 TYPE_GCC_MAX_VALUE (nt) = high;
8748 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8749 SET_TYPE_INDEX_TYPE
8750 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8752 return nt;
8755 /* Then the subtypes. */
8756 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8757 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8759 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8760 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8762 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8763 return t;
8765 nt = copy_type (t);
8766 SET_TYPE_RM_MIN_VALUE (nt, low);
8767 SET_TYPE_RM_MAX_VALUE (nt, high);
8769 return nt;
8772 return t;
8774 case COMPLEX_TYPE:
8775 nt = substitute_in_type (TREE_TYPE (t), f, r);
8776 if (nt == TREE_TYPE (t))
8777 return t;
8779 return build_complex_type (nt);
8781 case FUNCTION_TYPE:
8782 /* These should never show up here. */
8783 gcc_unreachable ();
8785 case ARRAY_TYPE:
8787 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8788 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8790 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8791 return t;
8793 nt = build_nonshared_array_type (component, domain);
8794 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8795 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8796 SET_TYPE_MODE (nt, TYPE_MODE (t));
8797 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8798 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8799 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8800 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8801 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8802 return nt;
8805 case RECORD_TYPE:
8806 case UNION_TYPE:
8807 case QUAL_UNION_TYPE:
8809 bool changed_field = false;
8810 tree field;
8812 /* Start out with no fields, make new fields, and chain them
8813 in. If we haven't actually changed the type of any field,
8814 discard everything we've done and return the old type. */
8815 nt = copy_type (t);
8816 TYPE_FIELDS (nt) = NULL_TREE;
8818 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8820 tree new_field = copy_node (field), new_n;
8822 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8823 if (new_n != TREE_TYPE (field))
8825 TREE_TYPE (new_field) = new_n;
8826 changed_field = true;
8829 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8830 if (new_n != DECL_FIELD_OFFSET (field))
8832 DECL_FIELD_OFFSET (new_field) = new_n;
8833 changed_field = true;
8836 /* Do the substitution inside the qualifier, if any. */
8837 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8839 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8840 if (new_n != DECL_QUALIFIER (field))
8842 DECL_QUALIFIER (new_field) = new_n;
8843 changed_field = true;
8847 DECL_CONTEXT (new_field) = nt;
8848 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8850 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8851 TYPE_FIELDS (nt) = new_field;
8854 if (!changed_field)
8855 return t;
8857 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8858 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8859 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8860 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8861 return nt;
8864 default:
8865 return t;
8869 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8870 needed to represent the object. */
8872 tree
8873 rm_size (tree gnu_type)
8875 /* For integral types, we store the RM size explicitly. */
8876 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8877 return TYPE_RM_SIZE (gnu_type);
8879 /* Return the RM size of the actual data plus the size of the template. */
8880 if (TREE_CODE (gnu_type) == RECORD_TYPE
8881 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8882 return
8883 size_binop (PLUS_EXPR,
8884 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8885 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8887 /* For record or union types, we store the size explicitly. */
8888 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8889 && !TYPE_FAT_POINTER_P (gnu_type)
8890 && TYPE_ADA_SIZE (gnu_type))
8891 return TYPE_ADA_SIZE (gnu_type);
8893 /* For other types, this is just the size. */
8894 return TYPE_SIZE (gnu_type);
8897 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8898 fully-qualified name, possibly with type information encoding.
8899 Otherwise, return the name. */
8901 tree
8902 get_entity_name (Entity_Id gnat_entity)
8904 Get_Encoded_Name (gnat_entity);
8905 return get_identifier_with_length (Name_Buffer, Name_Len);
8908 /* Return an identifier representing the external name to be used for
8909 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8910 and the specified suffix. */
8912 tree
8913 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8915 const Entity_Kind kind = Ekind (gnat_entity);
8916 const bool has_suffix = (suffix != NULL);
8917 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8918 String_Pointer sp = {suffix, &temp};
8920 Get_External_Name (gnat_entity, has_suffix, sp);
8922 /* A variable using the Stdcall convention lives in a DLL. We adjust
8923 its name to use the jump table, the _imp__NAME contains the address
8924 for the NAME variable. */
8925 if ((kind == E_Variable || kind == E_Constant)
8926 && Has_Stdcall_Convention (gnat_entity))
8928 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8929 char *new_name = (char *) alloca (len + 1);
8930 strcpy (new_name, STDCALL_PREFIX);
8931 strcat (new_name, Name_Buffer);
8932 return get_identifier_with_length (new_name, len);
8935 return get_identifier_with_length (Name_Buffer, Name_Len);
8938 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8939 string, return a new IDENTIFIER_NODE that is the concatenation of
8940 the name followed by "___" and the specified suffix. */
8942 tree
8943 concat_name (tree gnu_name, const char *suffix)
8945 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8946 char *new_name = (char *) alloca (len + 1);
8947 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8948 strcat (new_name, "___");
8949 strcat (new_name, suffix);
8950 return get_identifier_with_length (new_name, len);
8953 /* Initialize data structures of the decl.c module. */
8955 void
8956 init_gnat_decl (void)
8958 /* Initialize the cache of annotated values. */
8959 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8962 /* Destroy data structures of the decl.c module. */
8964 void
8965 destroy_gnat_decl (void)
8967 /* Destroy the cache of annotated values. */
8968 annotate_value_cache->empty ();
8969 annotate_value_cache = NULL;
8972 #include "gt-ada-decl.h"