Merge from trunk @222673.
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob3dbed7e51ac54d6ec8902eb9c3d60f27a565b346
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 == 0)
2225 TYPE_REVERSE_STORAGE_ORDER (tem)
2226 = Reverse_Storage_Order (gnat_entity);
2227 else
2228 TYPE_MULTI_ARRAY_P (tem) = 1;
2229 if (array_type_has_nonaliased_component (tem, gnat_entity))
2230 TYPE_NONALIASED_COMPONENT (tem) = 1;
2232 /* If it is passed by reference, force BLKmode to ensure that
2233 objects of this type will always be put in memory. */
2234 if (TYPE_MODE (tem) != BLKmode
2235 && Is_By_Reference_Type (gnat_entity))
2236 SET_TYPE_MODE (tem, BLKmode);
2239 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2241 /* If an alignment is specified, use it if valid. But ignore it
2242 for the original type of packed array types. If the alignment
2243 was requested with an explicit alignment clause, state so. */
2244 if (No (Packed_Array_Impl_Type (gnat_entity))
2245 && Known_Alignment (gnat_entity))
2247 TYPE_ALIGN (tem)
2248 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2249 TYPE_ALIGN (tem));
2250 if (Present (Alignment_Clause (gnat_entity)))
2251 TYPE_USER_ALIGN (tem) = 1;
2254 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2256 /* Adjust the type of the pointer-to-array field of the fat pointer
2257 and record the aliasing relationships if necessary. */
2258 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2259 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2260 record_component_aliases (gnu_fat_type);
2262 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2263 corresponding fat pointer. */
2264 TREE_TYPE (gnu_type) = gnu_fat_type;
2265 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2266 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2267 SET_TYPE_MODE (gnu_type, BLKmode);
2268 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2270 /* If the maximum size doesn't overflow, use it. */
2271 if (gnu_max_size
2272 && TREE_CODE (gnu_max_size) == INTEGER_CST
2273 && !TREE_OVERFLOW (gnu_max_size)
2274 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2275 && !TREE_OVERFLOW (gnu_max_size_unit))
2277 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2278 TYPE_SIZE (tem));
2279 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2280 TYPE_SIZE_UNIT (tem));
2283 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2284 !Comes_From_Source (gnat_entity), debug_info_p,
2285 gnat_entity);
2287 /* Give the fat pointer type a name. If this is a packed array, tell
2288 the debugger how to interpret the underlying bits. */
2289 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2290 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2291 else
2292 gnat_name = gnat_entity;
2293 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2294 !Comes_From_Source (gnat_entity), debug_info_p,
2295 gnat_entity);
2297 /* Create the type to be designated by thin pointers: a record type for
2298 the array and its template. We used to shift the fields to have the
2299 template at a negative offset, but this was somewhat of a kludge; we
2300 now shift thin pointer values explicitly but only those which have a
2301 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2302 tem = build_unc_object_type (gnu_template_type, tem,
2303 create_concat_name (gnat_name, "XUT"),
2304 debug_info_p);
2306 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2307 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2309 break;
2311 case E_Array_Subtype:
2313 /* This is the actual data type for array variables. Multidimensional
2314 arrays are implemented as arrays of arrays. Note that arrays which
2315 have sparse enumeration subtypes as index components create sparse
2316 arrays, which is obviously space inefficient but so much easier to
2317 code for now.
2319 Also note that the subtype never refers to the unconstrained array
2320 type, which is somewhat at variance with Ada semantics.
2322 First check to see if this is simply a renaming of the array type.
2323 If so, the result is the array type. */
2325 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2326 if (!Is_Constrained (gnat_entity))
2328 else
2330 Entity_Id gnat_index, gnat_base_index;
2331 const bool convention_fortran_p
2332 = (Convention (gnat_entity) == Convention_Fortran);
2333 const int ndim = Number_Dimensions (gnat_entity);
2334 tree gnu_base_type = gnu_type;
2335 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2336 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2337 bool need_index_type_struct = false;
2338 int index;
2340 /* First create the GCC type for each index and find out whether
2341 special types are needed for debugging information. */
2342 for (index = (convention_fortran_p ? ndim - 1 : 0),
2343 gnat_index = First_Index (gnat_entity),
2344 gnat_base_index
2345 = First_Index (Implementation_Base_Type (gnat_entity));
2346 0 <= index && index < ndim;
2347 index += (convention_fortran_p ? - 1 : 1),
2348 gnat_index = Next_Index (gnat_index),
2349 gnat_base_index = Next_Index (gnat_base_index))
2351 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2352 tree gnu_index_base_type = get_base_type (gnu_index_type);
2353 tree gnu_orig_min
2354 = convert (gnu_index_base_type,
2355 TYPE_MIN_VALUE (gnu_index_type));
2356 tree gnu_orig_max
2357 = convert (gnu_index_base_type,
2358 TYPE_MAX_VALUE (gnu_index_type));
2359 tree gnu_min = convert (sizetype, gnu_orig_min);
2360 tree gnu_max = convert (sizetype, gnu_orig_max);
2361 tree gnu_base_index_type
2362 = get_unpadded_type (Etype (gnat_base_index));
2363 tree gnu_base_index_base_type
2364 = get_base_type (gnu_base_index_type);
2365 tree gnu_base_orig_min
2366 = convert (gnu_base_index_base_type,
2367 TYPE_MIN_VALUE (gnu_base_index_type));
2368 tree gnu_base_orig_max
2369 = convert (gnu_base_index_base_type,
2370 TYPE_MAX_VALUE (gnu_base_index_type));
2371 tree gnu_high;
2373 /* See if the base array type is already flat. If it is, we
2374 are probably compiling an ACATS test but it will cause the
2375 code below to malfunction if we don't handle it specially. */
2376 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2377 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2378 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2380 gnu_min = size_one_node;
2381 gnu_max = size_zero_node;
2382 gnu_high = gnu_max;
2385 /* Similarly, if one of the values overflows in sizetype and the
2386 range is null, use 1..0 for the sizetype bounds. */
2387 else if (TREE_CODE (gnu_min) == INTEGER_CST
2388 && TREE_CODE (gnu_max) == INTEGER_CST
2389 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2390 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2392 gnu_min = size_one_node;
2393 gnu_max = size_zero_node;
2394 gnu_high = gnu_max;
2397 /* If the minimum and maximum values both overflow in sizetype,
2398 but the difference in the original type does not overflow in
2399 sizetype, ignore the overflow indication. */
2400 else if (TREE_CODE (gnu_min) == INTEGER_CST
2401 && TREE_CODE (gnu_max) == INTEGER_CST
2402 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2403 && !TREE_OVERFLOW
2404 (convert (sizetype,
2405 fold_build2 (MINUS_EXPR, gnu_index_type,
2406 gnu_orig_max,
2407 gnu_orig_min))))
2409 TREE_OVERFLOW (gnu_min) = 0;
2410 TREE_OVERFLOW (gnu_max) = 0;
2411 gnu_high = gnu_max;
2414 /* Compute the size of this dimension in the general case. We
2415 need to provide GCC with an upper bound to use but have to
2416 deal with the "superflat" case. There are three ways to do
2417 this. If we can prove that the array can never be superflat,
2418 we can just use the high bound of the index type. */
2419 else if ((Nkind (gnat_index) == N_Range
2420 && cannot_be_superflat_p (gnat_index))
2421 /* Bit-Packed Array Impl. Types are never superflat. */
2422 || (Is_Packed_Array_Impl_Type (gnat_entity)
2423 && Is_Bit_Packed_Array
2424 (Original_Array_Type (gnat_entity))))
2425 gnu_high = gnu_max;
2427 /* Otherwise, if the high bound is constant but the low bound is
2428 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2429 lower bound. Note that the comparison must be done in the
2430 original type to avoid any overflow during the conversion. */
2431 else if (TREE_CODE (gnu_max) == INTEGER_CST
2432 && TREE_CODE (gnu_min) != INTEGER_CST)
2434 gnu_high = gnu_max;
2435 gnu_min
2436 = build_cond_expr (sizetype,
2437 build_binary_op (GE_EXPR,
2438 boolean_type_node,
2439 gnu_orig_max,
2440 gnu_orig_min),
2441 gnu_min,
2442 int_const_binop (PLUS_EXPR, gnu_max,
2443 size_one_node));
2446 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2447 in all the other cases. Note that, here as well as above,
2448 the condition used in the comparison must be equivalent to
2449 the condition (length != 0). This is relied upon in order
2450 to optimize array comparisons in compare_arrays. Moreover
2451 we use int_const_binop for the shift by 1 if the bound is
2452 constant to avoid any unwanted overflow. */
2453 else
2454 gnu_high
2455 = build_cond_expr (sizetype,
2456 build_binary_op (GE_EXPR,
2457 boolean_type_node,
2458 gnu_orig_max,
2459 gnu_orig_min),
2460 gnu_max,
2461 TREE_CODE (gnu_min) == INTEGER_CST
2462 ? int_const_binop (MINUS_EXPR, gnu_min,
2463 size_one_node)
2464 : size_binop (MINUS_EXPR, gnu_min,
2465 size_one_node));
2467 /* Reuse the index type for the range type. Then make an index
2468 type with the size range in sizetype. */
2469 gnu_index_types[index]
2470 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2471 gnat_entity);
2473 /* Update the maximum size of the array in elements. Here we
2474 see if any constraint on the index type of the base type
2475 can be used in the case of self-referential bound on the
2476 index type of the subtype. We look for a non-"infinite"
2477 and non-self-referential bound from any type involved and
2478 handle each bound separately. */
2479 if (gnu_max_size)
2481 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2482 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2483 tree gnu_base_index_base_type
2484 = get_base_type (gnu_base_index_type);
2485 tree gnu_base_base_min
2486 = convert (sizetype,
2487 TYPE_MIN_VALUE (gnu_base_index_base_type));
2488 tree gnu_base_base_max
2489 = convert (sizetype,
2490 TYPE_MAX_VALUE (gnu_base_index_base_type));
2492 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2493 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2494 && !TREE_OVERFLOW (gnu_base_min)))
2495 gnu_base_min = gnu_min;
2497 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2498 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2499 && !TREE_OVERFLOW (gnu_base_max)))
2500 gnu_base_max = gnu_max;
2502 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2503 && TREE_OVERFLOW (gnu_base_min))
2504 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2505 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2506 && TREE_OVERFLOW (gnu_base_max))
2507 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2508 gnu_max_size = NULL_TREE;
2509 else
2511 tree gnu_this_max;
2513 /* Use int_const_binop if the bounds are constant to
2514 avoid any unwanted overflow. */
2515 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2516 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2517 gnu_this_max
2518 = int_const_binop (PLUS_EXPR, size_one_node,
2519 int_const_binop (MINUS_EXPR,
2520 gnu_base_max,
2521 gnu_base_min));
2522 else
2523 gnu_this_max
2524 = size_binop (PLUS_EXPR, size_one_node,
2525 size_binop (MINUS_EXPR,
2526 gnu_base_max,
2527 gnu_base_min));
2529 gnu_max_size
2530 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2534 /* We need special types for debugging information to point to
2535 the index types if they have variable bounds, are not integer
2536 types or are biased. */
2537 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2538 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2539 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2540 || (TREE_TYPE (gnu_index_type)
2541 && TREE_CODE (TREE_TYPE (gnu_index_type))
2542 != INTEGER_TYPE)
2543 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2544 need_index_type_struct = true;
2547 /* Then flatten: create the array of arrays. For an array type
2548 used to implement a packed array, get the component type from
2549 the original array type since the representation clauses that
2550 can affect it are on the latter. */
2551 if (Is_Packed_Array_Impl_Type (gnat_entity)
2552 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2554 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2555 for (index = ndim - 1; index >= 0; index--)
2556 gnu_type = TREE_TYPE (gnu_type);
2558 /* One of the above calls might have caused us to be elaborated,
2559 so don't blow up if so. */
2560 if (present_gnu_tree (gnat_entity))
2562 maybe_present = true;
2563 break;
2566 else
2568 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2569 debug_info_p);
2571 /* One of the above calls might have caused us to be elaborated,
2572 so don't blow up if so. */
2573 if (present_gnu_tree (gnat_entity))
2575 maybe_present = true;
2576 break;
2580 /* Compute the maximum size of the array in units and bits. */
2581 if (gnu_max_size)
2583 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2584 TYPE_SIZE_UNIT (gnu_type));
2585 gnu_max_size = size_binop (MULT_EXPR,
2586 convert (bitsizetype, gnu_max_size),
2587 TYPE_SIZE (gnu_type));
2589 else
2590 gnu_max_size_unit = NULL_TREE;
2592 /* Now build the array type. */
2593 for (index = ndim - 1; index >= 0; index --)
2595 gnu_type = build_nonshared_array_type (gnu_type,
2596 gnu_index_types[index]);
2597 if (index == 0)
2598 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2599 = Reverse_Storage_Order (gnat_entity);
2600 else
2601 TYPE_MULTI_ARRAY_P (gnu_type) = 1;
2602 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2603 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2605 /* See the E_Array_Type case for the rationale. */
2606 if (TYPE_MODE (gnu_type) != BLKmode
2607 && Is_By_Reference_Type (gnat_entity))
2608 SET_TYPE_MODE (gnu_type, BLKmode);
2611 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2613 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2614 TYPE_STUB_DECL (gnu_type)
2615 = create_type_stub_decl (gnu_entity_name, gnu_type);
2617 /* If we are at file level and this is a multi-dimensional array,
2618 we need to make a variable corresponding to the stride of the
2619 inner dimensions. */
2620 if (global_bindings_p () && ndim > 1)
2622 tree gnu_st_name = get_identifier ("ST");
2623 tree gnu_arr_type;
2625 for (gnu_arr_type = TREE_TYPE (gnu_type);
2626 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2627 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2628 gnu_st_name = concat_name (gnu_st_name, "ST"))
2630 tree eltype = TREE_TYPE (gnu_arr_type);
2632 TYPE_SIZE (gnu_arr_type)
2633 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2634 gnat_entity, gnu_st_name,
2635 definition, false);
2637 /* ??? For now, store the size as a multiple of the
2638 alignment of the element type in bytes so that we
2639 can see the alignment from the tree. */
2640 TYPE_SIZE_UNIT (gnu_arr_type)
2641 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2642 gnat_entity,
2643 concat_name (gnu_st_name, "A_U"),
2644 definition, false,
2645 TYPE_ALIGN (eltype));
2647 /* ??? create_type_decl is not invoked on the inner types so
2648 the MULT_EXPR node built above will never be marked. */
2649 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2653 /* If we need to write out a record type giving the names of the
2654 bounds for debugging purposes, do it now and make the record
2655 type a parallel type. This is not needed for a packed array
2656 since the bounds are conveyed by the original array type. */
2657 if (need_index_type_struct
2658 && debug_info_p
2659 && !Is_Packed_Array_Impl_Type (gnat_entity))
2661 tree gnu_bound_rec = make_node (RECORD_TYPE);
2662 tree gnu_field_list = NULL_TREE;
2663 tree gnu_field;
2665 TYPE_NAME (gnu_bound_rec)
2666 = create_concat_name (gnat_entity, "XA");
2668 for (index = ndim - 1; index >= 0; index--)
2670 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2671 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2673 /* Make sure to reference the types themselves, and not just
2674 their names, as the debugger may fall back on them. */
2675 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2676 gnu_bound_rec, NULL_TREE,
2677 NULL_TREE, 0, 0);
2678 DECL_CHAIN (gnu_field) = gnu_field_list;
2679 gnu_field_list = gnu_field;
2682 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2683 add_parallel_type (gnu_type, gnu_bound_rec);
2686 /* If this is a packed array type, make the original array type a
2687 parallel type. Otherwise, do it for the base array type if it
2688 isn't artificial to make sure it is kept in the debug info. */
2689 if (debug_info_p)
2691 if (Is_Packed_Array_Impl_Type (gnat_entity))
2692 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2693 else
2695 tree gnu_base_decl
2696 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2697 if (!DECL_ARTIFICIAL (gnu_base_decl))
2698 add_parallel_type (gnu_type,
2699 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2703 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2704 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2705 = (Is_Packed_Array_Impl_Type (gnat_entity)
2706 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2708 /* If the size is self-referential and the maximum size doesn't
2709 overflow, use it. */
2710 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2711 && gnu_max_size
2712 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2713 && TREE_OVERFLOW (gnu_max_size))
2714 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2715 && TREE_OVERFLOW (gnu_max_size_unit)))
2717 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2718 TYPE_SIZE (gnu_type));
2719 TYPE_SIZE_UNIT (gnu_type)
2720 = size_binop (MIN_EXPR, gnu_max_size_unit,
2721 TYPE_SIZE_UNIT (gnu_type));
2724 /* Set our alias set to that of our base type. This gives all
2725 array subtypes the same alias set. */
2726 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2728 /* If this is a packed type, make this type the same as the packed
2729 array type, but do some adjusting in the type first. */
2730 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2732 Entity_Id gnat_index;
2733 tree gnu_inner;
2735 /* First finish the type we had been making so that we output
2736 debugging information for it. */
2737 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2738 if (Treat_As_Volatile (gnat_entity))
2739 gnu_type
2740 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2741 /* Make it artificial only if the base type was artificial too.
2742 That's sort of "morally" true and will make it possible for
2743 the debugger to look it up by name in DWARF, which is needed
2744 in order to decode the packed array type. */
2745 gnu_decl
2746 = create_type_decl (gnu_entity_name, gnu_type,
2747 !Comes_From_Source (Etype (gnat_entity))
2748 && !Comes_From_Source (gnat_entity),
2749 debug_info_p, gnat_entity);
2751 /* Save it as our equivalent in case the call below elaborates
2752 this type again. */
2753 save_gnu_tree (gnat_entity, gnu_decl, false);
2755 gnu_decl
2756 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2757 NULL_TREE, 0);
2758 this_made_decl = true;
2759 gnu_type = TREE_TYPE (gnu_decl);
2760 save_gnu_tree (gnat_entity, NULL_TREE, false);
2762 gnu_inner = gnu_type;
2763 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2764 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2765 || TYPE_PADDING_P (gnu_inner)))
2766 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2768 /* We need to attach the index type to the type we just made so
2769 that the actual bounds can later be put into a template. */
2770 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2771 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2772 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2773 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2775 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2777 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2778 TYPE_MODULUS for modular types so we make an extra
2779 subtype if necessary. */
2780 if (TYPE_MODULAR_P (gnu_inner))
2782 tree gnu_subtype
2783 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2784 TREE_TYPE (gnu_subtype) = gnu_inner;
2785 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2786 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2787 TYPE_MIN_VALUE (gnu_inner));
2788 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2789 TYPE_MAX_VALUE (gnu_inner));
2790 gnu_inner = gnu_subtype;
2793 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2795 #ifdef ENABLE_CHECKING
2796 /* Check for other cases of overloading. */
2797 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2798 #endif
2801 for (gnat_index = First_Index (gnat_entity);
2802 Present (gnat_index);
2803 gnat_index = Next_Index (gnat_index))
2804 SET_TYPE_ACTUAL_BOUNDS
2805 (gnu_inner,
2806 tree_cons (NULL_TREE,
2807 get_unpadded_type (Etype (gnat_index)),
2808 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2810 if (Convention (gnat_entity) != Convention_Fortran)
2811 SET_TYPE_ACTUAL_BOUNDS
2812 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2814 if (TREE_CODE (gnu_type) == RECORD_TYPE
2815 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2816 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2820 else
2821 /* Abort if packed array with no Packed_Array_Impl_Type. */
2822 gcc_assert (!Is_Packed (gnat_entity));
2824 break;
2826 case E_String_Literal_Subtype:
2827 /* Create the type for a string literal. */
2829 Entity_Id gnat_full_type
2830 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2831 && Present (Full_View (Etype (gnat_entity)))
2832 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2833 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2834 tree gnu_string_array_type
2835 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2836 tree gnu_string_index_type
2837 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2838 (TYPE_DOMAIN (gnu_string_array_type))));
2839 tree gnu_lower_bound
2840 = convert (gnu_string_index_type,
2841 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2842 tree gnu_length
2843 = UI_To_gnu (String_Literal_Length (gnat_entity),
2844 gnu_string_index_type);
2845 tree gnu_upper_bound
2846 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2847 gnu_lower_bound,
2848 int_const_binop (MINUS_EXPR, gnu_length,
2849 convert (gnu_string_index_type,
2850 integer_one_node)));
2851 tree gnu_index_type
2852 = create_index_type (convert (sizetype, gnu_lower_bound),
2853 convert (sizetype, gnu_upper_bound),
2854 create_range_type (gnu_string_index_type,
2855 gnu_lower_bound,
2856 gnu_upper_bound),
2857 gnat_entity);
2859 gnu_type
2860 = build_nonshared_array_type (gnat_to_gnu_type
2861 (Component_Type (gnat_entity)),
2862 gnu_index_type);
2863 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2864 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2865 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2867 break;
2869 /* Record Types and Subtypes
2871 The following fields are defined on record types:
2873 Has_Discriminants True if the record has discriminants
2874 First_Discriminant Points to head of list of discriminants
2875 First_Entity Points to head of list of fields
2876 Is_Tagged_Type True if the record is tagged
2878 Implementation of Ada records and discriminated records:
2880 A record type definition is transformed into the equivalent of a C
2881 struct definition. The fields that are the discriminants which are
2882 found in the Full_Type_Declaration node and the elements of the
2883 Component_List found in the Record_Type_Definition node. The
2884 Component_List can be a recursive structure since each Variant of
2885 the Variant_Part of the Component_List has a Component_List.
2887 Processing of a record type definition comprises starting the list of
2888 field declarations here from the discriminants and the calling the
2889 function components_to_record to add the rest of the fields from the
2890 component list and return the gnu type node. The function
2891 components_to_record will call itself recursively as it traverses
2892 the tree. */
2894 case E_Record_Type:
2895 if (Has_Complex_Representation (gnat_entity))
2897 gnu_type
2898 = build_complex_type
2899 (get_unpadded_type
2900 (Etype (Defining_Entity
2901 (First (Component_Items
2902 (Component_List
2903 (Type_Definition
2904 (Declaration_Node (gnat_entity)))))))));
2906 break;
2910 Node_Id full_definition = Declaration_Node (gnat_entity);
2911 Node_Id record_definition = Type_Definition (full_definition);
2912 Node_Id gnat_constr;
2913 Entity_Id gnat_field;
2914 tree gnu_field, gnu_field_list = NULL_TREE;
2915 tree gnu_get_parent;
2916 /* Set PACKED in keeping with gnat_to_gnu_field. */
2917 const int packed
2918 = Is_Packed (gnat_entity)
2920 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2921 ? -1
2922 : (Known_Alignment (gnat_entity)
2923 || (Strict_Alignment (gnat_entity)
2924 && Known_RM_Size (gnat_entity)))
2925 ? -2
2926 : 0;
2927 const bool has_discr = Has_Discriminants (gnat_entity);
2928 const bool has_rep = Has_Specified_Layout (gnat_entity);
2929 const bool is_extension
2930 = (Is_Tagged_Type (gnat_entity)
2931 && Nkind (record_definition) == N_Derived_Type_Definition);
2932 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2933 bool all_rep = has_rep;
2935 /* See if all fields have a rep clause. Stop when we find one
2936 that doesn't. */
2937 if (all_rep)
2938 for (gnat_field = First_Entity (gnat_entity);
2939 Present (gnat_field);
2940 gnat_field = Next_Entity (gnat_field))
2941 if ((Ekind (gnat_field) == E_Component
2942 || Ekind (gnat_field) == E_Discriminant)
2943 && No (Component_Clause (gnat_field)))
2945 all_rep = false;
2946 break;
2949 /* If this is a record extension, go a level further to find the
2950 record definition. Also, verify we have a Parent_Subtype. */
2951 if (is_extension)
2953 if (!type_annotate_only
2954 || Present (Record_Extension_Part (record_definition)))
2955 record_definition = Record_Extension_Part (record_definition);
2957 gcc_assert (type_annotate_only
2958 || Present (Parent_Subtype (gnat_entity)));
2961 /* Make a node for the record. If we are not defining the record,
2962 suppress expanding incomplete types. */
2963 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2964 TYPE_NAME (gnu_type) = gnu_entity_name;
2965 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2966 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2967 = Reverse_Storage_Order (gnat_entity);
2968 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2970 if (!definition)
2972 defer_incomplete_level++;
2973 this_deferred = true;
2976 /* If both a size and rep clause was specified, put the size in
2977 the record type now so that it can get the proper mode. */
2978 if (has_rep && Known_RM_Size (gnat_entity))
2979 TYPE_SIZE (gnu_type)
2980 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2982 /* Always set the alignment here so that it can be used to
2983 set the mode, if it is making the alignment stricter. If
2984 it is invalid, it will be checked again below. If this is to
2985 be Atomic, choose a default alignment of a word unless we know
2986 the size and it's smaller. */
2987 if (Known_Alignment (gnat_entity))
2988 TYPE_ALIGN (gnu_type)
2989 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2990 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2992 unsigned int size = UI_To_Int (Esize (gnat_entity));
2993 TYPE_ALIGN (gnu_type)
2994 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2996 /* If a type needs strict alignment, the minimum size will be the
2997 type size instead of the RM size (see validate_size). Cap the
2998 alignment, lest it causes this type size to become too large. */
2999 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3001 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
3002 unsigned int raw_align = raw_size & -raw_size;
3003 if (raw_align < BIGGEST_ALIGNMENT)
3004 TYPE_ALIGN (gnu_type) = raw_align;
3006 else
3007 TYPE_ALIGN (gnu_type) = 0;
3009 /* If we have a Parent_Subtype, make a field for the parent. If
3010 this record has rep clauses, force the position to zero. */
3011 if (Present (Parent_Subtype (gnat_entity)))
3013 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3014 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3015 tree gnu_parent;
3017 /* A major complexity here is that the parent subtype will
3018 reference our discriminants in its Stored_Constraint list.
3019 But those must reference the parent component of this record
3020 which is precisely of the parent subtype we have not built yet!
3021 To break the circle we first build a dummy COMPONENT_REF which
3022 represents the "get to the parent" operation and initialize
3023 each of those discriminants to a COMPONENT_REF of the above
3024 dummy parent referencing the corresponding discriminant of the
3025 base type of the parent subtype. */
3026 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3027 build0 (PLACEHOLDER_EXPR, gnu_type),
3028 build_decl (input_location,
3029 FIELD_DECL, NULL_TREE,
3030 gnu_dummy_parent_type),
3031 NULL_TREE);
3033 if (has_discr)
3034 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3035 Present (gnat_field);
3036 gnat_field = Next_Stored_Discriminant (gnat_field))
3037 if (Present (Corresponding_Discriminant (gnat_field)))
3039 tree gnu_field
3040 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3041 (gnat_field));
3042 save_gnu_tree
3043 (gnat_field,
3044 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3045 gnu_get_parent, gnu_field, NULL_TREE),
3046 true);
3049 /* Then we build the parent subtype. If it has discriminants but
3050 the type itself has unknown discriminants, this means that it
3051 doesn't contain information about how the discriminants are
3052 derived from those of the ancestor type, so it cannot be used
3053 directly. Instead it is built by cloning the parent subtype
3054 of the underlying record view of the type, for which the above
3055 derivation of discriminants has been made explicit. */
3056 if (Has_Discriminants (gnat_parent)
3057 && Has_Unknown_Discriminants (gnat_entity))
3059 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3061 /* If we are defining the type, the underlying record
3062 view must already have been elaborated at this point.
3063 Otherwise do it now as its parent subtype cannot be
3064 technically elaborated on its own. */
3065 if (definition)
3066 gcc_assert (present_gnu_tree (gnat_uview));
3067 else
3068 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3070 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3072 /* Substitute the "get to the parent" of the type for that
3073 of its underlying record view in the cloned type. */
3074 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3075 Present (gnat_field);
3076 gnat_field = Next_Stored_Discriminant (gnat_field))
3077 if (Present (Corresponding_Discriminant (gnat_field)))
3079 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3080 tree gnu_ref
3081 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3082 gnu_get_parent, gnu_field, NULL_TREE);
3083 gnu_parent
3084 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3087 else
3088 gnu_parent = gnat_to_gnu_type (gnat_parent);
3090 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3091 initially built. The discriminants must reference the fields
3092 of the parent subtype and not those of its base type for the
3093 placeholder machinery to properly work. */
3094 if (has_discr)
3096 /* The actual parent subtype is the full view. */
3097 if (IN (Ekind (gnat_parent), Private_Kind))
3099 if (Present (Full_View (gnat_parent)))
3100 gnat_parent = Full_View (gnat_parent);
3101 else
3102 gnat_parent = Underlying_Full_View (gnat_parent);
3105 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3106 Present (gnat_field);
3107 gnat_field = Next_Stored_Discriminant (gnat_field))
3108 if (Present (Corresponding_Discriminant (gnat_field)))
3110 Entity_Id field;
3111 for (field = First_Stored_Discriminant (gnat_parent);
3112 Present (field);
3113 field = Next_Stored_Discriminant (field))
3114 if (same_discriminant_p (gnat_field, field))
3115 break;
3116 gcc_assert (Present (field));
3117 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3118 = gnat_to_gnu_field_decl (field);
3122 /* The "get to the parent" COMPONENT_REF must be given its
3123 proper type... */
3124 TREE_TYPE (gnu_get_parent) = gnu_parent;
3126 /* ...and reference the _Parent field of this record. */
3127 gnu_field
3128 = create_field_decl (parent_name_id,
3129 gnu_parent, gnu_type,
3130 has_rep
3131 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3132 has_rep
3133 ? bitsize_zero_node : NULL_TREE,
3134 0, 1);
3135 DECL_INTERNAL_P (gnu_field) = 1;
3136 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3137 TYPE_FIELDS (gnu_type) = gnu_field;
3140 /* Make the fields for the discriminants and put them into the record
3141 unless it's an Unchecked_Union. */
3142 if (has_discr)
3143 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3144 Present (gnat_field);
3145 gnat_field = Next_Stored_Discriminant (gnat_field))
3147 /* If this is a record extension and this discriminant is the
3148 renaming of another discriminant, we've handled it above. */
3149 if (Present (Parent_Subtype (gnat_entity))
3150 && Present (Corresponding_Discriminant (gnat_field)))
3151 continue;
3153 gnu_field
3154 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3155 debug_info_p);
3157 /* Make an expression using a PLACEHOLDER_EXPR from the
3158 FIELD_DECL node just created and link that with the
3159 corresponding GNAT defining identifier. */
3160 save_gnu_tree (gnat_field,
3161 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3162 build0 (PLACEHOLDER_EXPR, gnu_type),
3163 gnu_field, NULL_TREE),
3164 true);
3166 if (!is_unchecked_union)
3168 DECL_CHAIN (gnu_field) = gnu_field_list;
3169 gnu_field_list = gnu_field;
3173 /* If we have a derived untagged type that renames discriminants in
3174 the root type, the (stored) discriminants are a just copy of the
3175 discriminants of the root type. This means that any constraints
3176 added by the renaming in the derivation are disregarded as far
3177 as the layout of the derived type is concerned. To rescue them,
3178 we change the type of the (stored) discriminants to a subtype
3179 with the bounds of the type of the visible discriminants. */
3180 if (has_discr
3181 && !is_extension
3182 && Stored_Constraint (gnat_entity) != No_Elist)
3183 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3184 gnat_constr != No_Elmt;
3185 gnat_constr = Next_Elmt (gnat_constr))
3186 if (Nkind (Node (gnat_constr)) == N_Identifier
3187 /* Ignore access discriminants. */
3188 && !Is_Access_Type (Etype (Node (gnat_constr)))
3189 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3191 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3192 tree gnu_discr_type, gnu_ref;
3194 /* If the scope of the discriminant is not the record type,
3195 this means that we're processing the implicit full view
3196 of a type derived from a private discriminated type: in
3197 this case, the Stored_Constraint list is simply copied
3198 from the partial view, see Build_Derived_Private_Type.
3199 So we need to retrieve the corresponding discriminant
3200 of the implicit full view, otherwise we will abort. */
3201 if (Scope (gnat_discr) != gnat_entity)
3203 Entity_Id field;
3204 for (field = First_Entity (gnat_entity);
3205 Present (field);
3206 field = Next_Entity (field))
3207 if (Ekind (field) == E_Discriminant
3208 && same_discriminant_p (gnat_discr, field))
3209 break;
3210 gcc_assert (Present (field));
3211 gnat_discr = field;
3214 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3215 gnu_ref
3216 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3217 NULL_TREE, 0);
3219 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3220 just above for one of the stored discriminants. */
3221 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3223 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3225 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3226 tree gnu_subtype
3227 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3228 ? make_unsigned_type (prec) : make_signed_type (prec);
3229 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3230 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3231 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3232 TYPE_MIN_VALUE (gnu_discr_type));
3233 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3234 TYPE_MAX_VALUE (gnu_discr_type));
3235 TREE_TYPE (gnu_ref)
3236 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3240 /* Add the fields into the record type and finish it up. */
3241 components_to_record (gnu_type, Component_List (record_definition),
3242 gnu_field_list, packed, definition, false,
3243 all_rep, is_unchecked_union,
3244 !Comes_From_Source (gnat_entity), debug_info_p,
3245 false, OK_To_Reorder_Components (gnat_entity),
3246 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3248 /* If it is passed by reference, force BLKmode to ensure that objects
3249 of this type will always be put in memory. */
3250 if (TYPE_MODE (gnu_type) != BLKmode
3251 && Is_By_Reference_Type (gnat_entity))
3252 SET_TYPE_MODE (gnu_type, BLKmode);
3254 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3256 /* Fill in locations of fields. */
3257 annotate_rep (gnat_entity, gnu_type);
3259 /* If there are any entities in the chain corresponding to components
3260 that we did not elaborate, ensure we elaborate their types if they
3261 are Itypes. */
3262 for (gnat_temp = First_Entity (gnat_entity);
3263 Present (gnat_temp);
3264 gnat_temp = Next_Entity (gnat_temp))
3265 if ((Ekind (gnat_temp) == E_Component
3266 || Ekind (gnat_temp) == E_Discriminant)
3267 && Is_Itype (Etype (gnat_temp))
3268 && !present_gnu_tree (gnat_temp))
3269 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3271 /* If this is a record type associated with an exception definition,
3272 equate its fields to those of the standard exception type. This
3273 will make it possible to convert between them. */
3274 if (gnu_entity_name == exception_data_name_id)
3276 tree gnu_std_field;
3277 for (gnu_field = TYPE_FIELDS (gnu_type),
3278 gnu_std_field = TYPE_FIELDS (except_type_node);
3279 gnu_field;
3280 gnu_field = DECL_CHAIN (gnu_field),
3281 gnu_std_field = DECL_CHAIN (gnu_std_field))
3282 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3283 gcc_assert (!gnu_std_field);
3286 break;
3288 case E_Class_Wide_Subtype:
3289 /* If an equivalent type is present, that is what we should use.
3290 Otherwise, fall through to handle this like a record subtype
3291 since it may have constraints. */
3292 if (gnat_equiv_type != gnat_entity)
3294 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3295 maybe_present = true;
3296 break;
3299 /* ... fall through ... */
3301 case E_Record_Subtype:
3302 /* If Cloned_Subtype is Present it means this record subtype has
3303 identical layout to that type or subtype and we should use
3304 that GCC type for this one. The front end guarantees that
3305 the component list is shared. */
3306 if (Present (Cloned_Subtype (gnat_entity)))
3308 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3309 NULL_TREE, 0);
3310 maybe_present = true;
3311 break;
3314 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3315 changing the type, make a new type with each field having the type of
3316 the field in the new subtype but the position computed by transforming
3317 every discriminant reference according to the constraints. We don't
3318 see any difference between private and non-private type here since
3319 derivations from types should have been deferred until the completion
3320 of the private type. */
3321 else
3323 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3324 tree gnu_base_type;
3326 if (!definition)
3328 defer_incomplete_level++;
3329 this_deferred = true;
3332 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3334 if (present_gnu_tree (gnat_entity))
3336 maybe_present = true;
3337 break;
3340 /* If this is a record subtype associated with a dispatch table,
3341 strip the suffix. This is necessary to make sure 2 different
3342 subtypes associated with the imported and exported views of a
3343 dispatch table are properly merged in LTO mode. */
3344 if (Is_Dispatch_Table_Entity (gnat_entity))
3346 char *p;
3347 Get_Encoded_Name (gnat_entity);
3348 p = strchr (Name_Buffer, '_');
3349 gcc_assert (p);
3350 strcpy (p+2, "dtS");
3351 gnu_entity_name = get_identifier (Name_Buffer);
3354 /* When the subtype has discriminants and these discriminants affect
3355 the initial shape it has inherited, factor them in. But for an
3356 Unchecked_Union (it must be an Itype), just return the type.
3357 We can't just test Is_Constrained because private subtypes without
3358 discriminants of types with discriminants with default expressions
3359 are Is_Constrained but aren't constrained! */
3360 if (IN (Ekind (gnat_base_type), Record_Kind)
3361 && !Is_Unchecked_Union (gnat_base_type)
3362 && !Is_For_Access_Subtype (gnat_entity)
3363 && Has_Discriminants (gnat_entity)
3364 && Is_Constrained (gnat_entity)
3365 && Stored_Constraint (gnat_entity) != No_Elist)
3367 vec<subst_pair> gnu_subst_list
3368 = build_subst_list (gnat_entity, gnat_base_type, definition);
3369 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3370 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3371 bool selected_variant = false, all_constant_pos = true;
3372 Entity_Id gnat_field;
3373 vec<variant_desc> gnu_variant_list;
3375 gnu_type = make_node (RECORD_TYPE);
3376 TYPE_NAME (gnu_type) = gnu_entity_name;
3377 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3378 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3379 = Reverse_Storage_Order (gnat_entity);
3380 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3382 /* Set the size, alignment and alias set of the new type to
3383 match that of the old one, doing required substitutions. */
3384 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3385 gnu_subst_list);
3387 if (TYPE_IS_PADDING_P (gnu_base_type))
3388 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3389 else
3390 gnu_unpad_base_type = gnu_base_type;
3392 /* Look for REP and variant parts in the base type. */
3393 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3394 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3396 /* If there is a variant part, we must compute whether the
3397 constraints statically select a particular variant. If
3398 so, we simply drop the qualified union and flatten the
3399 list of fields. Otherwise we'll build a new qualified
3400 union for the variants that are still relevant. */
3401 if (gnu_variant_part)
3403 variant_desc *v;
3404 unsigned int i;
3406 gnu_variant_list
3407 = build_variant_list (TREE_TYPE (gnu_variant_part),
3408 gnu_subst_list,
3409 vNULL);
3411 /* If all the qualifiers are unconditionally true, the
3412 innermost variant is statically selected. */
3413 selected_variant = true;
3414 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3415 if (!integer_onep (v->qual))
3417 selected_variant = false;
3418 break;
3421 /* Otherwise, create the new variants. */
3422 if (!selected_variant)
3423 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3425 tree old_variant = v->type;
3426 tree new_variant = make_node (RECORD_TYPE);
3427 tree suffix
3428 = concat_name (DECL_NAME (gnu_variant_part),
3429 IDENTIFIER_POINTER
3430 (DECL_NAME (v->field)));
3431 TYPE_NAME (new_variant)
3432 = concat_name (TYPE_NAME (gnu_type),
3433 IDENTIFIER_POINTER (suffix));
3434 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3435 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3436 copy_and_substitute_in_size (new_variant, old_variant,
3437 gnu_subst_list);
3438 v->new_type = new_variant;
3441 else
3443 gnu_variant_list.create (0);
3444 selected_variant = false;
3447 /* Make a list of fields and their position in the base type. */
3448 gnu_pos_list
3449 = build_position_list (gnu_unpad_base_type,
3450 gnu_variant_list.exists ()
3451 && !selected_variant,
3452 size_zero_node, bitsize_zero_node,
3453 BIGGEST_ALIGNMENT, NULL_TREE);
3455 /* Now go down every component in the subtype and compute its
3456 size and position from those of the component in the base
3457 type and from the constraints of the subtype. */
3458 for (gnat_field = First_Entity (gnat_entity);
3459 Present (gnat_field);
3460 gnat_field = Next_Entity (gnat_field))
3461 if ((Ekind (gnat_field) == E_Component
3462 || Ekind (gnat_field) == E_Discriminant)
3463 && !(Present (Corresponding_Discriminant (gnat_field))
3464 && Is_Tagged_Type (gnat_base_type))
3465 && Underlying_Type
3466 (Scope (Original_Record_Component (gnat_field)))
3467 == gnat_base_type)
3469 Name_Id gnat_name = Chars (gnat_field);
3470 Entity_Id gnat_old_field
3471 = Original_Record_Component (gnat_field);
3472 tree gnu_old_field
3473 = gnat_to_gnu_field_decl (gnat_old_field);
3474 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3475 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3476 tree gnu_cont_type, gnu_last = NULL_TREE;
3478 /* If the type is the same, retrieve the GCC type from the
3479 old field to take into account possible adjustments. */
3480 if (Etype (gnat_field) == Etype (gnat_old_field))
3481 gnu_field_type = TREE_TYPE (gnu_old_field);
3482 else
3483 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3485 /* If there was a component clause, the field types must be
3486 the same for the type and subtype, so copy the data from
3487 the old field to avoid recomputation here. Also if the
3488 field is justified modular and the optimization in
3489 gnat_to_gnu_field was applied. */
3490 if (Present (Component_Clause (gnat_old_field))
3491 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3492 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3493 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3494 == TREE_TYPE (gnu_old_field)))
3496 gnu_size = DECL_SIZE (gnu_old_field);
3497 gnu_field_type = TREE_TYPE (gnu_old_field);
3500 /* If the old field was packed and of constant size, we
3501 have to get the old size here, as it might differ from
3502 what the Etype conveys and the latter might overlap
3503 onto the following field. Try to arrange the type for
3504 possible better packing along the way. */
3505 else if (DECL_PACKED (gnu_old_field)
3506 && TREE_CODE (DECL_SIZE (gnu_old_field))
3507 == INTEGER_CST)
3509 gnu_size = DECL_SIZE (gnu_old_field);
3510 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3511 && !TYPE_FAT_POINTER_P (gnu_field_type)
3512 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3513 gnu_field_type
3514 = make_packable_type (gnu_field_type, true);
3517 else
3518 gnu_size = TYPE_SIZE (gnu_field_type);
3520 /* If the context of the old field is the base type or its
3521 REP part (if any), put the field directly in the new
3522 type; otherwise look up the context in the variant list
3523 and put the field either in the new type if there is a
3524 selected variant or in one of the new variants. */
3525 if (gnu_context == gnu_unpad_base_type
3526 || (gnu_rep_part
3527 && gnu_context == TREE_TYPE (gnu_rep_part)))
3528 gnu_cont_type = gnu_type;
3529 else
3531 variant_desc *v;
3532 unsigned int i;
3533 tree rep_part;
3535 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3536 if (gnu_context == v->type
3537 || ((rep_part = get_rep_part (v->type))
3538 && gnu_context == TREE_TYPE (rep_part)))
3539 break;
3540 if (v)
3542 if (selected_variant)
3543 gnu_cont_type = gnu_type;
3544 else
3545 gnu_cont_type = v->new_type;
3547 else
3548 /* The front-end may pass us "ghost" components if
3549 it fails to recognize that a constrained subtype
3550 is statically constrained. Discard them. */
3551 continue;
3554 /* Now create the new field modeled on the old one. */
3555 gnu_field
3556 = create_field_decl_from (gnu_old_field, gnu_field_type,
3557 gnu_cont_type, gnu_size,
3558 gnu_pos_list, gnu_subst_list);
3559 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3561 /* Put it in one of the new variants directly. */
3562 if (gnu_cont_type != gnu_type)
3564 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3565 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3568 /* To match the layout crafted in components_to_record,
3569 if this is the _Tag or _Parent field, put it before
3570 any other fields. */
3571 else if (gnat_name == Name_uTag
3572 || gnat_name == Name_uParent)
3573 gnu_field_list = chainon (gnu_field_list, gnu_field);
3575 /* Similarly, if this is the _Controller field, put
3576 it before the other fields except for the _Tag or
3577 _Parent field. */
3578 else if (gnat_name == Name_uController && gnu_last)
3580 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3581 DECL_CHAIN (gnu_last) = gnu_field;
3584 /* Otherwise, if this is a regular field, put it after
3585 the other fields. */
3586 else
3588 DECL_CHAIN (gnu_field) = gnu_field_list;
3589 gnu_field_list = gnu_field;
3590 if (!gnu_last)
3591 gnu_last = gnu_field;
3592 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3593 all_constant_pos = false;
3596 save_gnu_tree (gnat_field, gnu_field, false);
3599 /* If there is a variant list, a selected variant and the fields
3600 all have a constant position, put them in order of increasing
3601 position to match that of constant CONSTRUCTORs. Likewise if
3602 there is no variant list but a REP part, since the latter has
3603 been flattened in the process. */
3604 if (((gnu_variant_list.exists () && selected_variant)
3605 || (!gnu_variant_list.exists () && gnu_rep_part))
3606 && all_constant_pos)
3608 const int len = list_length (gnu_field_list);
3609 tree *field_arr = XALLOCAVEC (tree, len), t;
3610 int i;
3612 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3613 field_arr[i] = t;
3615 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3617 gnu_field_list = NULL_TREE;
3618 for (i = 0; i < len; i++)
3620 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3621 gnu_field_list = field_arr[i];
3625 /* If there is a variant list and no selected variant, we need
3626 to create the nest of variant parts from the old nest. */
3627 else if (gnu_variant_list.exists () && !selected_variant)
3629 tree new_variant_part
3630 = create_variant_part_from (gnu_variant_part,
3631 gnu_variant_list, gnu_type,
3632 gnu_pos_list, gnu_subst_list);
3633 DECL_CHAIN (new_variant_part) = gnu_field_list;
3634 gnu_field_list = new_variant_part;
3637 /* Now go through the entities again looking for Itypes that
3638 we have not elaborated but should (e.g., Etypes of fields
3639 that have Original_Components). */
3640 for (gnat_field = First_Entity (gnat_entity);
3641 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3642 if ((Ekind (gnat_field) == E_Discriminant
3643 || Ekind (gnat_field) == E_Component)
3644 && !present_gnu_tree (Etype (gnat_field)))
3645 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3647 /* Do not emit debug info for the type yet since we're going to
3648 modify it below. */
3649 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3650 false);
3651 compute_record_mode (gnu_type);
3653 /* See the E_Record_Type case for the rationale. */
3654 if (TYPE_MODE (gnu_type) != BLKmode
3655 && Is_By_Reference_Type (gnat_entity))
3656 SET_TYPE_MODE (gnu_type, BLKmode);
3658 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3660 /* Fill in locations of fields. */
3661 annotate_rep (gnat_entity, gnu_type);
3663 /* If debugging information is being written for the type, write
3664 a record that shows what we are a subtype of and also make a
3665 variable that indicates our size, if still variable. */
3666 if (debug_info_p)
3668 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3669 tree gnu_unpad_base_name
3670 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3671 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3673 TYPE_NAME (gnu_subtype_marker)
3674 = create_concat_name (gnat_entity, "XVS");
3675 finish_record_type (gnu_subtype_marker,
3676 create_field_decl (gnu_unpad_base_name,
3677 build_reference_type
3678 (gnu_unpad_base_type),
3679 gnu_subtype_marker,
3680 NULL_TREE, NULL_TREE,
3681 0, 0),
3682 0, true);
3684 add_parallel_type (gnu_type, gnu_subtype_marker);
3686 if (definition
3687 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3688 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3689 TYPE_SIZE_UNIT (gnu_subtype_marker)
3690 = create_var_decl (create_concat_name (gnat_entity,
3691 "XVZ"),
3692 NULL_TREE, sizetype, gnu_size_unit,
3693 false, false, false, false, NULL,
3694 gnat_entity);
3697 gnu_variant_list.release ();
3698 gnu_subst_list.release ();
3700 /* Now we can finalize it. */
3701 rest_of_record_type_compilation (gnu_type);
3704 /* Otherwise, go down all the components in the new type and make
3705 them equivalent to those in the base type. */
3706 else
3708 gnu_type = gnu_base_type;
3710 for (gnat_temp = First_Entity (gnat_entity);
3711 Present (gnat_temp);
3712 gnat_temp = Next_Entity (gnat_temp))
3713 if ((Ekind (gnat_temp) == E_Discriminant
3714 && !Is_Unchecked_Union (gnat_base_type))
3715 || Ekind (gnat_temp) == E_Component)
3716 save_gnu_tree (gnat_temp,
3717 gnat_to_gnu_field_decl
3718 (Original_Record_Component (gnat_temp)),
3719 false);
3722 break;
3724 case E_Access_Subprogram_Type:
3725 /* Use the special descriptor type for dispatch tables if needed,
3726 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3727 Note that we are only required to do so for static tables in
3728 order to be compatible with the C++ ABI, but Ada 2005 allows
3729 to extend library level tagged types at the local level so
3730 we do it in the non-static case as well. */
3731 if (TARGET_VTABLE_USES_DESCRIPTORS
3732 && Is_Dispatch_Table_Entity (gnat_entity))
3734 gnu_type = fdesc_type_node;
3735 gnu_size = TYPE_SIZE (gnu_type);
3736 break;
3739 /* ... fall through ... */
3741 case E_Anonymous_Access_Subprogram_Type:
3742 /* If we are not defining this entity, and we have incomplete
3743 entities being processed above us, make a dummy type and
3744 fill it in later. */
3745 if (!definition && defer_incomplete_level != 0)
3747 struct incomplete *p = XNEW (struct incomplete);
3749 gnu_type
3750 = build_pointer_type
3751 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3752 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3753 !Comes_From_Source (gnat_entity),
3754 debug_info_p, gnat_entity);
3755 this_made_decl = true;
3756 gnu_type = TREE_TYPE (gnu_decl);
3757 save_gnu_tree (gnat_entity, gnu_decl, false);
3758 saved = true;
3760 p->old_type = TREE_TYPE (gnu_type);
3761 p->full_type = Directly_Designated_Type (gnat_entity);
3762 p->next = defer_incomplete_list;
3763 defer_incomplete_list = p;
3764 break;
3767 /* ... fall through ... */
3769 case E_Allocator_Type:
3770 case E_Access_Type:
3771 case E_Access_Attribute_Type:
3772 case E_Anonymous_Access_Type:
3773 case E_General_Access_Type:
3775 /* The designated type and its equivalent type for gigi. */
3776 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3777 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3778 /* Whether it comes from a limited with. */
3779 bool is_from_limited_with
3780 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3781 && From_Limited_With (gnat_desig_equiv));
3782 /* The "full view" of the designated type. If this is an incomplete
3783 entity from a limited with, treat its non-limited view as the full
3784 view. Otherwise, if this is an incomplete or private type, use the
3785 full view. In the former case, we might point to a private type,
3786 in which case, we need its full view. Also, we want to look at the
3787 actual type used for the representation, so this takes a total of
3788 three steps. */
3789 Entity_Id gnat_desig_full_direct_first
3790 = (is_from_limited_with
3791 ? Non_Limited_View (gnat_desig_equiv)
3792 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3793 ? Full_View (gnat_desig_equiv) : Empty));
3794 Entity_Id gnat_desig_full_direct
3795 = ((is_from_limited_with
3796 && Present (gnat_desig_full_direct_first)
3797 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3798 ? Full_View (gnat_desig_full_direct_first)
3799 : gnat_desig_full_direct_first);
3800 Entity_Id gnat_desig_full
3801 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3802 /* The type actually used to represent the designated type, either
3803 gnat_desig_full or gnat_desig_equiv. */
3804 Entity_Id gnat_desig_rep;
3805 /* True if this is a pointer to an unconstrained array. */
3806 bool is_unconstrained_array;
3807 /* We want to know if we'll be seeing the freeze node for any
3808 incomplete type we may be pointing to. */
3809 bool in_main_unit
3810 = (Present (gnat_desig_full)
3811 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3812 : In_Extended_Main_Code_Unit (gnat_desig_type));
3813 /* True if we make a dummy type here. */
3814 bool made_dummy = false;
3815 /* The mode to be used for the pointer type. */
3816 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3817 /* The GCC type used for the designated type. */
3818 tree gnu_desig_type = NULL_TREE;
3820 if (!targetm.valid_pointer_mode (p_mode))
3821 p_mode = ptr_mode;
3823 /* If either the designated type or its full view is an unconstrained
3824 array subtype, replace it with the type it's a subtype of. This
3825 avoids problems with multiple copies of unconstrained array types.
3826 Likewise, if the designated type is a subtype of an incomplete
3827 record type, use the parent type to avoid order of elaboration
3828 issues. This can lose some code efficiency, but there is no
3829 alternative. */
3830 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3831 && !Is_Constrained (gnat_desig_equiv))
3832 gnat_desig_equiv = Etype (gnat_desig_equiv);
3833 if (Present (gnat_desig_full)
3834 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3835 && !Is_Constrained (gnat_desig_full))
3836 || (Ekind (gnat_desig_full) == E_Record_Subtype
3837 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3838 gnat_desig_full = Etype (gnat_desig_full);
3840 /* Set the type that's actually the representation of the designated
3841 type and also flag whether we have a unconstrained array. */
3842 gnat_desig_rep
3843 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3844 is_unconstrained_array
3845 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3847 /* If we are pointing to an incomplete type whose completion is an
3848 unconstrained array, make dummy fat and thin pointer types to it.
3849 Likewise if the type itself is dummy or an unconstrained array. */
3850 if (is_unconstrained_array
3851 && (Present (gnat_desig_full)
3852 || (present_gnu_tree (gnat_desig_equiv)
3853 && TYPE_IS_DUMMY_P
3854 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3855 || (!in_main_unit
3856 && defer_incomplete_level != 0
3857 && !present_gnu_tree (gnat_desig_equiv))
3858 || (in_main_unit
3859 && is_from_limited_with
3860 && Present (Freeze_Node (gnat_desig_equiv)))))
3862 if (present_gnu_tree (gnat_desig_rep))
3863 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3864 else
3866 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3867 made_dummy = true;
3870 /* If the call above got something that has a pointer, the pointer
3871 is our type. This could have happened either because the type
3872 was elaborated or because somebody else executed the code. */
3873 if (!TYPE_POINTER_TO (gnu_desig_type))
3874 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3875 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3878 /* If we already know what the full type is, use it. */
3879 else if (Present (gnat_desig_full)
3880 && present_gnu_tree (gnat_desig_full))
3881 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3883 /* Get the type of the thing we are to point to and build a pointer to
3884 it. If it is a reference to an incomplete or private type with a
3885 full view that is a record, make a dummy type node and get the
3886 actual type later when we have verified it is safe. */
3887 else if ((!in_main_unit
3888 && !present_gnu_tree (gnat_desig_equiv)
3889 && Present (gnat_desig_full)
3890 && !present_gnu_tree (gnat_desig_full)
3891 && Is_Record_Type (gnat_desig_full))
3892 /* Likewise if we are pointing to a record or array and we are
3893 to defer elaborating incomplete types. We do this as this
3894 access type may be the full view of a private type. Note
3895 that the unconstrained array case is handled above. */
3896 || ((!in_main_unit || imported_p)
3897 && defer_incomplete_level != 0
3898 && !present_gnu_tree (gnat_desig_equiv)
3899 && (Is_Record_Type (gnat_desig_rep)
3900 || Is_Array_Type (gnat_desig_rep)))
3901 /* If this is a reference from a limited_with type back to our
3902 main unit and there's a freeze node for it, either we have
3903 already processed the declaration and made the dummy type,
3904 in which case we just reuse the latter, or we have not yet,
3905 in which case we make the dummy type and it will be reused
3906 when the declaration is finally processed. In both cases,
3907 the pointer eventually created below will be automatically
3908 adjusted when the freeze node is processed. Note that the
3909 unconstrained array case is handled above. */
3910 || (in_main_unit
3911 && is_from_limited_with
3912 && Present (Freeze_Node (gnat_desig_rep))))
3914 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3915 made_dummy = true;
3918 /* Otherwise handle the case of a pointer to itself. */
3919 else if (gnat_desig_equiv == gnat_entity)
3921 gnu_type
3922 = build_pointer_type_for_mode (void_type_node, p_mode,
3923 No_Strict_Aliasing (gnat_entity));
3924 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3927 /* If expansion is disabled, the equivalent type of a concurrent type
3928 is absent, so build a dummy pointer type. */
3929 else if (type_annotate_only && No (gnat_desig_equiv))
3930 gnu_type = ptr_void_type_node;
3932 /* Finally, handle the default case where we can just elaborate our
3933 designated type. */
3934 else
3935 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3937 /* It is possible that a call to gnat_to_gnu_type above resolved our
3938 type. If so, just return it. */
3939 if (present_gnu_tree (gnat_entity))
3941 maybe_present = true;
3942 break;
3945 /* If we haven't done it yet, build the pointer type the usual way. */
3946 if (!gnu_type)
3948 /* Modify the designated type if we are pointing only to constant
3949 objects, but don't do it for unconstrained arrays. */
3950 if (Is_Access_Constant (gnat_entity)
3951 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3953 gnu_desig_type
3954 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3956 /* Some extra processing is required if we are building a
3957 pointer to an incomplete type (in the GCC sense). We might
3958 have such a type if we just made a dummy, or directly out
3959 of the call to gnat_to_gnu_type above if we are processing
3960 an access type for a record component designating the
3961 record type itself. */
3962 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3964 /* We must ensure that the pointer to variant we make will
3965 be processed by update_pointer_to when the initial type
3966 is completed. Pretend we made a dummy and let further
3967 processing act as usual. */
3968 made_dummy = true;
3970 /* We must ensure that update_pointer_to will not retrieve
3971 the dummy variant when building a properly qualified
3972 version of the complete type. We take advantage of the
3973 fact that get_qualified_type is requiring TYPE_NAMEs to
3974 match to influence build_qualified_type and then also
3975 update_pointer_to here. */
3976 TYPE_NAME (gnu_desig_type)
3977 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3981 gnu_type
3982 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3983 No_Strict_Aliasing (gnat_entity));
3986 /* If we are not defining this object and we have made a dummy pointer,
3987 save our current definition, evaluate the actual type, and replace
3988 the tentative type we made with the actual one. If we are to defer
3989 actually looking up the actual type, make an entry in the deferred
3990 list. If this is from a limited with, we may have to defer to the
3991 end of the current unit. */
3992 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3994 tree gnu_old_desig_type;
3996 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3998 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3999 if (esize == POINTER_SIZE)
4000 gnu_type = build_pointer_type
4001 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
4003 else
4004 gnu_old_desig_type = TREE_TYPE (gnu_type);
4006 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4007 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4008 !Comes_From_Source (gnat_entity),
4009 debug_info_p, gnat_entity);
4010 this_made_decl = true;
4011 gnu_type = TREE_TYPE (gnu_decl);
4012 save_gnu_tree (gnat_entity, gnu_decl, false);
4013 saved = true;
4015 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4016 update gnu_old_desig_type directly, in which case it will not be
4017 a dummy type any more when we get into update_pointer_to.
4019 This can happen e.g. when the designated type is a record type,
4020 because their elaboration starts with an initial node from
4021 make_dummy_type, which may be the same node as the one we got.
4023 Besides, variants of this non-dummy type might have been created
4024 along the way. update_pointer_to is expected to properly take
4025 care of those situations. */
4026 if (defer_incomplete_level == 0 && !is_from_limited_with)
4028 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4029 gnat_to_gnu_type (gnat_desig_equiv));
4031 else
4033 struct incomplete *p = XNEW (struct incomplete);
4034 struct incomplete **head
4035 = (is_from_limited_with
4036 ? &defer_limited_with : &defer_incomplete_list);
4037 p->old_type = gnu_old_desig_type;
4038 p->full_type = gnat_desig_equiv;
4039 p->next = *head;
4040 *head = p;
4044 break;
4046 case E_Access_Protected_Subprogram_Type:
4047 case E_Anonymous_Access_Protected_Subprogram_Type:
4048 if (type_annotate_only && No (gnat_equiv_type))
4049 gnu_type = ptr_void_type_node;
4050 else
4052 /* The run-time representation is the equivalent type. */
4053 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4054 maybe_present = true;
4057 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4058 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4059 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4060 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4061 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4062 NULL_TREE, 0);
4064 break;
4066 case E_Access_Subtype:
4068 /* We treat this as identical to its base type; any constraint is
4069 meaningful only to the front-end.
4071 The designated type must be elaborated as well, if it does
4072 not have its own freeze node. Designated (sub)types created
4073 for constrained components of records with discriminants are
4074 not frozen by the front-end and thus not elaborated by gigi,
4075 because their use may appear before the base type is frozen,
4076 and because it is not clear that they are needed anywhere in
4077 gigi. With the current model, there is no correct place where
4078 they could be elaborated. */
4080 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4081 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4082 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4083 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4084 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4086 /* If we are not defining this entity, and we have incomplete
4087 entities being processed above us, make a dummy type and
4088 elaborate it later. */
4089 if (!definition && defer_incomplete_level != 0)
4091 struct incomplete *p = XNEW (struct incomplete);
4093 p->old_type
4094 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4095 p->full_type = Directly_Designated_Type (gnat_entity);
4096 p->next = defer_incomplete_list;
4097 defer_incomplete_list = p;
4099 else if (!IN (Ekind (Base_Type
4100 (Directly_Designated_Type (gnat_entity))),
4101 Incomplete_Or_Private_Kind))
4102 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4103 NULL_TREE, 0);
4106 maybe_present = true;
4107 break;
4109 /* Subprogram Entities
4111 The following access functions are defined for subprograms:
4113 Etype Return type or Standard_Void_Type.
4114 First_Formal The first formal parameter.
4115 Is_Imported Indicates that the subprogram has appeared in
4116 an INTERFACE or IMPORT pragma. For now we
4117 assume that the external language is C.
4118 Is_Exported Likewise but for an EXPORT pragma.
4119 Is_Inlined True if the subprogram is to be inlined.
4121 Each parameter is first checked by calling must_pass_by_ref on its
4122 type to determine if it is passed by reference. For parameters which
4123 are copied in, if they are Ada In Out or Out parameters, their return
4124 value becomes part of a record which becomes the return type of the
4125 function (C function - note that this applies only to Ada procedures
4126 so there is no Ada return type). Additional code to store back the
4127 parameters will be generated on the caller side. This transformation
4128 is done here, not in the front-end.
4130 The intended result of the transformation can be seen from the
4131 equivalent source rewritings that follow:
4133 struct temp {int a,b};
4134 procedure P (A,B: In Out ...) is temp P (int A,B)
4135 begin {
4136 .. ..
4137 end P; return {A,B};
4140 temp t;
4141 P(X,Y); t = P(X,Y);
4142 X = t.a , Y = t.b;
4144 For subprogram types we need to perform mainly the same conversions to
4145 GCC form that are needed for procedures and function declarations. The
4146 only difference is that at the end, we make a type declaration instead
4147 of a function declaration. */
4149 case E_Subprogram_Type:
4150 case E_Function:
4151 case E_Procedure:
4153 /* The type returned by a function or else Standard_Void_Type for a
4154 procedure. */
4155 Entity_Id gnat_return_type = Etype (gnat_entity);
4156 tree gnu_return_type;
4157 /* The first GCC parameter declaration (a PARM_DECL node). The
4158 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4159 actually is the head of this parameter list. */
4160 tree gnu_param_list = NULL_TREE;
4161 /* Non-null for subprograms containing parameters passed by copy-in
4162 copy-out (Ada In Out or Out parameters not passed by reference),
4163 in which case it is the list of nodes used to specify the values
4164 of the In Out/Out parameters that are returned as a record upon
4165 procedure return. The TREE_PURPOSE of an element of this list is
4166 a field of the record and the TREE_VALUE is the PARM_DECL
4167 corresponding to that field. This list will be saved in the
4168 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4169 tree gnu_cico_list = NULL_TREE;
4170 /* List of fields in return type of procedure with copy-in copy-out
4171 parameters. */
4172 tree gnu_field_list = NULL_TREE;
4173 /* If an import pragma asks to map this subprogram to a GCC builtin,
4174 this is the builtin DECL node. */
4175 tree gnu_builtin_decl = NULL_TREE;
4176 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4177 Entity_Id gnat_param;
4178 enum inline_status_t inline_status
4179 = Has_Pragma_No_Inline (gnat_entity)
4180 ? is_suppressed
4181 : Has_Pragma_Inline_Always (gnat_entity)
4182 ? is_required
4183 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4184 bool public_flag = Is_Public (gnat_entity) || imported_p;
4185 /* Subprograms marked both Intrinsic and Always_Inline need not
4186 have a body of their own. */
4187 bool extern_flag
4188 = ((Is_Public (gnat_entity) && !definition)
4189 || imported_p
4190 || (Convention (gnat_entity) == Convention_Intrinsic
4191 && Has_Pragma_Inline_Always (gnat_entity)));
4192 bool artificial_flag = !Comes_From_Source (gnat_entity);
4193 /* The semantics of "pure" in Ada essentially matches that of "const"
4194 in the back-end. In particular, both properties are orthogonal to
4195 the "nothrow" property if the EH circuitry is explicit in the
4196 internal representation of the back-end. If we are to completely
4197 hide the EH circuitry from it, we need to declare that calls to pure
4198 Ada subprograms that can throw have side effects since they can
4199 trigger an "abnormal" transfer of control flow; thus they can be
4200 neither "const" nor "pure" in the back-end sense. */
4201 bool const_flag
4202 = (Exception_Mechanism == Back_End_Exceptions
4203 && Is_Pure (gnat_entity));
4204 bool volatile_flag = No_Return (gnat_entity);
4205 bool return_by_direct_ref_p = false;
4206 bool return_by_invisi_ref_p = false;
4207 bool return_unconstrained_p = false;
4208 int parmnum;
4210 /* A parameter may refer to this type, so defer completion of any
4211 incomplete types. */
4212 if (kind == E_Subprogram_Type && !definition)
4214 defer_incomplete_level++;
4215 this_deferred = true;
4218 /* If the subprogram has an alias, it is probably inherited, so
4219 we can use the original one. If the original "subprogram"
4220 is actually an enumeration literal, it may be the first use
4221 of its type, so we must elaborate that type now. */
4222 if (Present (Alias (gnat_entity)))
4224 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4225 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4227 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4229 /* Elaborate any Itypes in the parameters of this entity. */
4230 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4231 Present (gnat_temp);
4232 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4233 if (Is_Itype (Etype (gnat_temp)))
4234 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4236 break;
4239 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4240 corresponding DECL node. Proper generation of calls later on need
4241 proper parameter associations so we don't "break;" here. */
4242 if (Convention (gnat_entity) == Convention_Intrinsic
4243 && Present (Interface_Name (gnat_entity)))
4245 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4247 /* Inability to find the builtin decl most often indicates a
4248 genuine mistake, but imports of unregistered intrinsics are
4249 sometimes issued on purpose to allow hooking in alternate
4250 bodies. We post a warning conditioned on Wshadow in this case,
4251 to let developers be notified on demand without risking false
4252 positives with common default sets of options. */
4254 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4255 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4258 /* ??? What if we don't find the builtin node above ? warn ? err ?
4259 In the current state we neither warn nor err, and calls will just
4260 be handled as for regular subprograms. */
4262 /* Look into the return type and get its associated GCC tree. If it
4263 is not void, compute various flags for the subprogram type. */
4264 if (Ekind (gnat_return_type) == E_Void)
4265 gnu_return_type = void_type_node;
4266 else
4268 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4269 context may now appear in parameter and result profiles. If
4270 we are only annotating types, break circularities here. */
4271 if (type_annotate_only
4272 && is_from_limited_with_of_main (gnat_return_type))
4273 gnu_return_type = ptr_void_type_node;
4274 else
4275 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4277 /* If this function returns by reference, make the actual return
4278 type the pointer type and make a note of that. */
4279 if (Returns_By_Ref (gnat_entity))
4281 gnu_return_type = build_pointer_type (gnu_return_type);
4282 return_by_direct_ref_p = true;
4285 /* If the return type is an unconstrained array type, the return
4286 value will be allocated on the secondary stack so the actual
4287 return type is the fat pointer type. */
4288 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4290 gnu_return_type = TREE_TYPE (gnu_return_type);
4291 return_unconstrained_p = true;
4294 /* Likewise, if the return type requires a transient scope, the
4295 return value will also be allocated on the secondary stack so
4296 the actual return type is the pointer type. */
4297 else if (Requires_Transient_Scope (gnat_return_type))
4299 gnu_return_type = build_pointer_type (gnu_return_type);
4300 return_unconstrained_p = true;
4303 /* If the Mechanism is By_Reference, ensure this function uses the
4304 target's by-invisible-reference mechanism, which may not be the
4305 same as above (e.g. it might be passing an extra parameter). */
4306 else if (kind == E_Function
4307 && Mechanism (gnat_entity) == By_Reference)
4308 return_by_invisi_ref_p = true;
4310 /* Likewise, if the return type is itself By_Reference. */
4311 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4312 return_by_invisi_ref_p = true;
4314 /* If the type is a padded type and the underlying type would not
4315 be passed by reference or the function has a foreign convention,
4316 return the underlying type. */
4317 else if (TYPE_IS_PADDING_P (gnu_return_type)
4318 && (!default_pass_by_ref
4319 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4320 || Has_Foreign_Convention (gnat_entity)))
4321 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4323 /* If the return type is unconstrained, that means it must have a
4324 maximum size. Use the padded type as the effective return type.
4325 And ensure the function uses the target's by-invisible-reference
4326 mechanism to avoid copying too much data when it returns. */
4327 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4329 tree orig_type = gnu_return_type;
4331 gnu_return_type
4332 = maybe_pad_type (gnu_return_type,
4333 max_size (TYPE_SIZE (gnu_return_type),
4334 true),
4335 0, gnat_entity, false, false, false, true);
4337 /* Declare it now since it will never be declared otherwise.
4338 This is necessary to ensure that its subtrees are properly
4339 marked. */
4340 if (gnu_return_type != orig_type
4341 && !DECL_P (TYPE_NAME (gnu_return_type)))
4342 create_type_decl (TYPE_NAME (gnu_return_type),
4343 gnu_return_type, true, debug_info_p,
4344 gnat_entity);
4346 return_by_invisi_ref_p = true;
4349 /* If the return type has a size that overflows, we cannot have
4350 a function that returns that type. This usage doesn't make
4351 sense anyway, so give an error here. */
4352 if (TYPE_SIZE_UNIT (gnu_return_type)
4353 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4354 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4356 post_error ("cannot return type whose size overflows",
4357 gnat_entity);
4358 gnu_return_type = copy_node (gnu_return_type);
4359 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4360 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4361 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4362 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4366 /* Loop over the parameters and get their associated GCC tree. While
4367 doing this, build a copy-in copy-out structure if we need one. */
4368 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4369 Present (gnat_param);
4370 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4372 Entity_Id gnat_param_type = Etype (gnat_param);
4373 tree gnu_param_name = get_entity_name (gnat_param);
4374 tree gnu_param_type, gnu_param, gnu_field;
4375 Mechanism_Type mech = Mechanism (gnat_param);
4376 bool copy_in_copy_out = false, fake_param_type;
4378 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4379 context may now appear in parameter and result profiles. If
4380 we are only annotating types, break circularities here. */
4381 if (type_annotate_only
4382 && is_from_limited_with_of_main (gnat_param_type))
4384 gnu_param_type = ptr_void_type_node;
4385 fake_param_type = true;
4387 else
4389 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4390 fake_param_type = false;
4393 /* Builtins are expanded inline and there is no real call sequence
4394 involved. So the type expected by the underlying expander is
4395 always the type of each argument "as is". */
4396 if (gnu_builtin_decl)
4397 mech = By_Copy;
4398 /* Handle the first parameter of a valued procedure specially. */
4399 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4400 mech = By_Copy_Return;
4401 /* Otherwise, see if a Mechanism was supplied that forced this
4402 parameter to be passed one way or another. */
4403 else if (mech == Default
4404 || mech == By_Copy
4405 || mech == By_Reference)
4407 else if (mech > 0)
4409 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4410 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4411 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4412 mech))
4413 mech = By_Reference;
4414 else
4415 mech = By_Copy;
4417 else
4419 post_error ("unsupported mechanism for&", gnat_param);
4420 mech = Default;
4423 /* Do not call gnat_to_gnu_param for a fake parameter type since
4424 it will try to use the real type again. */
4425 if (fake_param_type)
4427 if (Ekind (gnat_param) == E_Out_Parameter)
4428 gnu_param = NULL_TREE;
4429 else
4431 gnu_param
4432 = create_param_decl (gnu_param_name, gnu_param_type,
4433 false);
4434 Set_Mechanism (gnat_param,
4435 mech == Default ? By_Copy : mech);
4436 if (Ekind (gnat_param) == E_In_Out_Parameter)
4437 copy_in_copy_out = true;
4440 else
4441 gnu_param
4442 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4443 Has_Foreign_Convention (gnat_entity),
4444 &copy_in_copy_out);
4446 /* We are returned either a PARM_DECL or a type if no parameter
4447 needs to be passed; in either case, adjust the type. */
4448 if (DECL_P (gnu_param))
4449 gnu_param_type = TREE_TYPE (gnu_param);
4450 else
4452 gnu_param_type = gnu_param;
4453 gnu_param = NULL_TREE;
4456 /* The failure of this assertion will very likely come from an
4457 order of elaboration issue for the type of the parameter. */
4458 gcc_assert (kind == E_Subprogram_Type
4459 || !TYPE_IS_DUMMY_P (gnu_param_type)
4460 || type_annotate_only);
4462 if (gnu_param)
4464 gnu_param_list = chainon (gnu_param, gnu_param_list);
4465 Sloc_to_locus (Sloc (gnat_param),
4466 &DECL_SOURCE_LOCATION (gnu_param));
4467 save_gnu_tree (gnat_param, gnu_param, false);
4469 /* If a parameter is a pointer, this function may modify
4470 memory through it and thus shouldn't be considered
4471 a const function. Also, the memory may be modified
4472 between two calls, so they can't be CSE'ed. The latter
4473 case also handles by-ref parameters. */
4474 if (POINTER_TYPE_P (gnu_param_type)
4475 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4476 const_flag = false;
4479 if (copy_in_copy_out)
4481 if (!gnu_cico_list)
4483 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4485 /* If this is a function, we also need a field for the
4486 return value to be placed. */
4487 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4489 gnu_field
4490 = create_field_decl (get_identifier ("RETVAL"),
4491 gnu_return_type,
4492 gnu_new_ret_type, NULL_TREE,
4493 NULL_TREE, 0, 0);
4494 Sloc_to_locus (Sloc (gnat_entity),
4495 &DECL_SOURCE_LOCATION (gnu_field));
4496 gnu_field_list = gnu_field;
4497 gnu_cico_list
4498 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4501 gnu_return_type = gnu_new_ret_type;
4502 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4503 /* Set a default alignment to speed up accesses. But we
4504 shouldn't increase the size of the structure too much,
4505 lest it doesn't fit in return registers anymore. */
4506 TYPE_ALIGN (gnu_return_type)
4507 = get_mode_alignment (ptr_mode);
4510 gnu_field
4511 = create_field_decl (gnu_param_name, gnu_param_type,
4512 gnu_return_type, NULL_TREE, NULL_TREE,
4513 0, 0);
4514 Sloc_to_locus (Sloc (gnat_param),
4515 &DECL_SOURCE_LOCATION (gnu_field));
4516 DECL_CHAIN (gnu_field) = gnu_field_list;
4517 gnu_field_list = gnu_field;
4518 gnu_cico_list
4519 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4523 if (gnu_cico_list)
4525 /* If we have a CICO list but it has only one entry, we convert
4526 this function into a function that returns this object. */
4527 if (list_length (gnu_cico_list) == 1)
4528 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4530 /* Do not finalize the return type if the subprogram is stubbed
4531 since structures are incomplete for the back-end. */
4532 else if (Convention (gnat_entity) != Convention_Stubbed)
4534 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4535 0, false);
4537 /* Try to promote the mode of the return type if it is passed
4538 in registers, again to speed up accesses. */
4539 if (TYPE_MODE (gnu_return_type) == BLKmode
4540 && !targetm.calls.return_in_memory (gnu_return_type,
4541 NULL_TREE))
4543 unsigned int size
4544 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4545 unsigned int i = BITS_PER_UNIT;
4546 machine_mode mode;
4548 while (i < size)
4549 i <<= 1;
4550 mode = mode_for_size (i, MODE_INT, 0);
4551 if (mode != BLKmode)
4553 SET_TYPE_MODE (gnu_return_type, mode);
4554 TYPE_ALIGN (gnu_return_type)
4555 = GET_MODE_ALIGNMENT (mode);
4556 TYPE_SIZE (gnu_return_type)
4557 = bitsize_int (GET_MODE_BITSIZE (mode));
4558 TYPE_SIZE_UNIT (gnu_return_type)
4559 = size_int (GET_MODE_SIZE (mode));
4563 if (debug_info_p)
4564 rest_of_record_type_compilation (gnu_return_type);
4568 /* Deal with platform-specific calling conventions. */
4569 if (Has_Stdcall_Convention (gnat_entity))
4570 prepend_one_attribute
4571 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4572 get_identifier ("stdcall"), NULL_TREE,
4573 gnat_entity);
4574 else if (Has_Thiscall_Convention (gnat_entity))
4575 prepend_one_attribute
4576 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4577 get_identifier ("thiscall"), NULL_TREE,
4578 gnat_entity);
4580 /* If we should request stack realignment for a foreign convention
4581 subprogram, do so. Note that this applies to task entry points
4582 in particular. */
4583 if (FOREIGN_FORCE_REALIGN_STACK
4584 && Has_Foreign_Convention (gnat_entity))
4585 prepend_one_attribute
4586 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4587 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4588 gnat_entity);
4590 /* Deal with a pragma Linker_Section on a subprogram. */
4591 if ((kind == E_Function || kind == E_Procedure)
4592 && Present (Linker_Section_Pragma (gnat_entity)))
4593 prepend_one_attribute_pragma (&attr_list,
4594 Linker_Section_Pragma (gnat_entity));
4596 /* The lists have been built in reverse. */
4597 gnu_param_list = nreverse (gnu_param_list);
4598 gnu_cico_list = nreverse (gnu_cico_list);
4600 if (kind == E_Function)
4601 Set_Mechanism (gnat_entity, return_unconstrained_p
4602 || return_by_direct_ref_p
4603 || return_by_invisi_ref_p
4604 ? By_Reference : By_Copy);
4605 gnu_type
4606 = create_subprog_type (gnu_return_type, gnu_param_list,
4607 gnu_cico_list, return_unconstrained_p,
4608 return_by_direct_ref_p,
4609 return_by_invisi_ref_p);
4611 /* A procedure (something that doesn't return anything) shouldn't be
4612 considered const since there would be no reason for calling such a
4613 subprogram. Note that procedures with Out (or In Out) parameters
4614 have already been converted into a function with a return type.
4615 Similarly, if the function returns an unconstrained type, then the
4616 function will allocate the return value on the secondary stack and
4617 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4618 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4619 const_flag = false;
4621 if (const_flag || volatile_flag)
4623 const int quals
4624 = (const_flag ? TYPE_QUAL_CONST : 0)
4625 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4627 gnu_type = change_qualified_type (gnu_type, quals);
4630 /* If we have a builtin decl for that function, use it. Check if the
4631 profiles are compatible and warn if they are not. The checker is
4632 expected to post extra diagnostics in this case. */
4633 if (gnu_builtin_decl)
4635 intrin_binding_t inb;
4637 inb.gnat_entity = gnat_entity;
4638 inb.ada_fntype = gnu_type;
4639 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4641 if (!intrin_profiles_compatible_p (&inb))
4642 post_error
4643 ("?profile of& doesn''t match the builtin it binds!",
4644 gnat_entity);
4646 gnu_decl = gnu_builtin_decl;
4647 gnu_type = TREE_TYPE (gnu_builtin_decl);
4648 break;
4651 /* If there was no specified Interface_Name and the external and
4652 internal names of the subprogram are the same, only use the
4653 internal name to allow disambiguation of nested subprograms. */
4654 if (No (Interface_Name (gnat_entity))
4655 && gnu_ext_name == gnu_entity_name)
4656 gnu_ext_name = NULL_TREE;
4658 /* If we are defining the subprogram and it has an Address clause
4659 we must get the address expression from the saved GCC tree for the
4660 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4661 the address expression here since the front-end has guaranteed
4662 in that case that the elaboration has no effects. If there is
4663 an Address clause and we are not defining the object, just
4664 make it a constant. */
4665 if (Present (Address_Clause (gnat_entity)))
4667 tree gnu_address = NULL_TREE;
4669 if (definition)
4670 gnu_address
4671 = (present_gnu_tree (gnat_entity)
4672 ? get_gnu_tree (gnat_entity)
4673 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4675 save_gnu_tree (gnat_entity, NULL_TREE, false);
4677 /* Convert the type of the object to a reference type that can
4678 alias everything as per 13.3(19). */
4679 gnu_type
4680 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4681 if (gnu_address)
4682 gnu_address = convert (gnu_type, gnu_address);
4684 gnu_decl
4685 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4686 gnu_address, false, Is_Public (gnat_entity),
4687 extern_flag, false, NULL, gnat_entity);
4688 DECL_BY_REF_P (gnu_decl) = 1;
4691 else if (kind == E_Subprogram_Type)
4693 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4694 gnu_decl
4695 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4696 debug_info_p, gnat_entity);
4698 else
4700 gnu_decl
4701 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4702 gnu_param_list, inline_status,
4703 public_flag, extern_flag, artificial_flag,
4704 attr_list, gnat_entity);
4705 /* This is unrelated to the stub built right above. */
4706 DECL_STUBBED_P (gnu_decl)
4707 = Convention (gnat_entity) == Convention_Stubbed;
4710 break;
4712 case E_Incomplete_Type:
4713 case E_Incomplete_Subtype:
4714 case E_Private_Type:
4715 case E_Private_Subtype:
4716 case E_Limited_Private_Type:
4717 case E_Limited_Private_Subtype:
4718 case E_Record_Type_With_Private:
4719 case E_Record_Subtype_With_Private:
4721 /* Get the "full view" of this entity. If this is an incomplete
4722 entity from a limited with, treat its non-limited view as the
4723 full view. Otherwise, use either the full view or the underlying
4724 full view, whichever is present. This is used in all the tests
4725 below. */
4726 Entity_Id full_view
4727 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4728 ? Non_Limited_View (gnat_entity)
4729 : Present (Full_View (gnat_entity))
4730 ? Full_View (gnat_entity)
4731 : IN (kind, Private_Kind)
4732 ? Underlying_Full_View (gnat_entity)
4733 : Empty;
4735 /* If this is an incomplete type with no full view, it must be a Taft
4736 Amendment type, in which case we return a dummy type. Otherwise,
4737 just get the type from its Etype. */
4738 if (No (full_view))
4740 if (kind == E_Incomplete_Type)
4742 gnu_type = make_dummy_type (gnat_entity);
4743 gnu_decl = TYPE_STUB_DECL (gnu_type);
4745 else
4747 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4748 NULL_TREE, 0);
4749 maybe_present = true;
4751 break;
4754 /* If we already made a type for the full view, reuse it. */
4755 else if (present_gnu_tree (full_view))
4757 gnu_decl = get_gnu_tree (full_view);
4758 break;
4761 /* Otherwise, if we are not defining the type now, get the type
4762 from the full view. But always get the type from the full view
4763 for define on use types, since otherwise we won't see them! */
4764 else if (!definition
4765 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4766 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4768 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4769 maybe_present = true;
4770 break;
4773 /* For incomplete types, make a dummy type entry which will be
4774 replaced later. Save it as the full declaration's type so
4775 we can do any needed updates when we see it. */
4776 gnu_type = make_dummy_type (gnat_entity);
4777 gnu_decl = TYPE_STUB_DECL (gnu_type);
4778 if (Has_Completion_In_Body (gnat_entity))
4779 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4780 save_gnu_tree (full_view, gnu_decl, 0);
4781 break;
4784 case E_Class_Wide_Type:
4785 /* Class-wide types are always transformed into their root type. */
4786 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4787 maybe_present = true;
4788 break;
4790 case E_Task_Type:
4791 case E_Task_Subtype:
4792 case E_Protected_Type:
4793 case E_Protected_Subtype:
4794 /* Concurrent types are always transformed into their record type. */
4795 if (type_annotate_only && No (gnat_equiv_type))
4796 gnu_type = void_type_node;
4797 else
4798 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4799 maybe_present = true;
4800 break;
4802 case E_Label:
4803 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4804 break;
4806 case E_Block:
4807 case E_Loop:
4808 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4809 we've already saved it, so we don't try to. */
4810 gnu_decl = error_mark_node;
4811 saved = true;
4812 break;
4814 case E_Abstract_State:
4815 /* This is a SPARK annotation that only reaches here when compiling in
4816 ASIS mode. */
4817 gcc_assert (type_annotate_only);
4818 gnu_decl = error_mark_node;
4819 saved = true;
4820 break;
4822 default:
4823 gcc_unreachable ();
4826 /* If we had a case where we evaluated another type and it might have
4827 defined this one, handle it here. */
4828 if (maybe_present && present_gnu_tree (gnat_entity))
4830 gnu_decl = get_gnu_tree (gnat_entity);
4831 saved = true;
4834 /* If we are processing a type and there is either no decl for it or
4835 we just made one, do some common processing for the type, such as
4836 handling alignment and possible padding. */
4837 if (is_type && (!gnu_decl || this_made_decl))
4839 /* Process the attributes, if not already done. Note that the type is
4840 already defined so we cannot pass true for IN_PLACE here. */
4841 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4843 /* Tell the middle-end that objects of tagged types are guaranteed to
4844 be properly aligned. This is necessary because conversions to the
4845 class-wide type are translated into conversions to the root type,
4846 which can be less aligned than some of its derived types. */
4847 if (Is_Tagged_Type (gnat_entity)
4848 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4849 TYPE_ALIGN_OK (gnu_type) = 1;
4851 /* Record whether the type is passed by reference. */
4852 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4853 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4855 /* ??? Don't set the size for a String_Literal since it is either
4856 confirming or we don't handle it properly (if the low bound is
4857 non-constant). */
4858 if (!gnu_size && kind != E_String_Literal_Subtype)
4860 Uint gnat_size = Known_Esize (gnat_entity)
4861 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4862 gnu_size
4863 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4864 false, Has_Size_Clause (gnat_entity));
4867 /* If a size was specified, see if we can make a new type of that size
4868 by rearranging the type, for example from a fat to a thin pointer. */
4869 if (gnu_size)
4871 gnu_type
4872 = make_type_from_size (gnu_type, gnu_size,
4873 Has_Biased_Representation (gnat_entity));
4875 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4876 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4877 gnu_size = NULL_TREE;
4880 /* If the alignment has not already been processed and this is not
4881 an unconstrained array type, see if an alignment is specified.
4882 If not, we pick a default alignment for atomic objects. */
4883 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4885 else if (Known_Alignment (gnat_entity))
4887 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4888 TYPE_ALIGN (gnu_type));
4890 /* Warn on suspiciously large alignments. This should catch
4891 errors about the (alignment,byte)/(size,bit) discrepancy. */
4892 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4894 tree size;
4896 /* If a size was specified, take it into account. Otherwise
4897 use the RM size for records or unions as the type size has
4898 already been adjusted to the alignment. */
4899 if (gnu_size)
4900 size = gnu_size;
4901 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4902 && !TYPE_FAT_POINTER_P (gnu_type))
4903 size = rm_size (gnu_type);
4904 else
4905 size = TYPE_SIZE (gnu_type);
4907 /* Consider an alignment as suspicious if the alignment/size
4908 ratio is greater or equal to the byte/bit ratio. */
4909 if (tree_fits_uhwi_p (size)
4910 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4911 post_error_ne ("?suspiciously large alignment specified for&",
4912 Expression (Alignment_Clause (gnat_entity)),
4913 gnat_entity);
4916 else if (Is_Atomic (gnat_entity) && !gnu_size
4917 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4918 && integer_pow2p (TYPE_SIZE (gnu_type)))
4919 align = MIN (BIGGEST_ALIGNMENT,
4920 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4921 else if (Is_Atomic (gnat_entity) && gnu_size
4922 && tree_fits_uhwi_p (gnu_size)
4923 && integer_pow2p (gnu_size))
4924 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4926 /* See if we need to pad the type. If we did, and made a record,
4927 the name of the new type may be changed. So get it back for
4928 us when we make the new TYPE_DECL below. */
4929 if (gnu_size || align > 0)
4930 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4931 false, !gnu_decl, definition, false);
4933 if (TYPE_IS_PADDING_P (gnu_type))
4934 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4936 /* Now set the RM size of the type. We cannot do it before padding
4937 because we need to accept arbitrary RM sizes on integral types. */
4938 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4940 /* If we are at global level, GCC will have applied variable_size to
4941 the type, but that won't have done anything. So, if it's not
4942 a constant or self-referential, call elaborate_expression_1 to
4943 make a variable for the size rather than calculating it each time.
4944 Handle both the RM size and the actual size. */
4945 if (global_bindings_p ()
4946 && TYPE_SIZE (gnu_type)
4947 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4948 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4950 tree size = TYPE_SIZE (gnu_type);
4952 TYPE_SIZE (gnu_type)
4953 = elaborate_expression_1 (size, gnat_entity,
4954 get_identifier ("SIZE"),
4955 definition, false);
4957 /* ??? For now, store the size as a multiple of the alignment in
4958 bytes so that we can see the alignment from the tree. */
4959 TYPE_SIZE_UNIT (gnu_type)
4960 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4961 get_identifier ("SIZE_A_UNIT"),
4962 definition, false,
4963 TYPE_ALIGN (gnu_type));
4965 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4966 may not be marked by the call to create_type_decl below. */
4967 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4969 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4971 tree variant_part = get_variant_part (gnu_type);
4972 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4974 if (variant_part)
4976 tree union_type = TREE_TYPE (variant_part);
4977 tree offset = DECL_FIELD_OFFSET (variant_part);
4979 /* If the position of the variant part is constant, subtract
4980 it from the size of the type of the parent to get the new
4981 size. This manual CSE reduces the data size. */
4982 if (TREE_CODE (offset) == INTEGER_CST)
4984 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4985 TYPE_SIZE (union_type)
4986 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4987 bit_from_pos (offset, bitpos));
4988 TYPE_SIZE_UNIT (union_type)
4989 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4990 byte_from_pos (offset, bitpos));
4992 else
4994 TYPE_SIZE (union_type)
4995 = elaborate_expression_1 (TYPE_SIZE (union_type),
4996 gnat_entity,
4997 get_identifier ("VSIZE"),
4998 definition, false);
5000 /* ??? For now, store the size as a multiple of the
5001 alignment in bytes so that we can see the alignment
5002 from the tree. */
5003 TYPE_SIZE_UNIT (union_type)
5004 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5005 gnat_entity,
5006 get_identifier
5007 ("VSIZE_A_UNIT"),
5008 definition, false,
5009 TYPE_ALIGN (union_type));
5011 /* ??? For now, store the offset as a multiple of the
5012 alignment in bytes so that we can see the alignment
5013 from the tree. */
5014 DECL_FIELD_OFFSET (variant_part)
5015 = elaborate_expression_2 (offset,
5016 gnat_entity,
5017 get_identifier ("VOFFSET"),
5018 definition, false,
5019 DECL_OFFSET_ALIGN
5020 (variant_part));
5023 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5024 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5027 if (operand_equal_p (ada_size, size, 0))
5028 ada_size = TYPE_SIZE (gnu_type);
5029 else
5030 ada_size
5031 = elaborate_expression_1 (ada_size, gnat_entity,
5032 get_identifier ("RM_SIZE"),
5033 definition, false);
5034 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5038 /* If this is a record type or subtype, call elaborate_expression_2 on
5039 any field position. Do this for both global and local types.
5040 Skip any fields that we haven't made trees for to avoid problems with
5041 class wide types. */
5042 if (IN (kind, Record_Kind))
5043 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5044 gnat_temp = Next_Entity (gnat_temp))
5045 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5047 tree gnu_field = get_gnu_tree (gnat_temp);
5049 /* ??? For now, store the offset as a multiple of the alignment
5050 in bytes so that we can see the alignment from the tree. */
5051 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5053 DECL_FIELD_OFFSET (gnu_field)
5054 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5055 gnat_temp,
5056 get_identifier ("OFFSET"),
5057 definition, false,
5058 DECL_OFFSET_ALIGN (gnu_field));
5060 /* ??? The context of gnu_field is not necessarily gnu_type
5061 so the MULT_EXPR node built above may not be marked by
5062 the call to create_type_decl below. */
5063 if (global_bindings_p ())
5064 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5068 if (Is_Atomic (gnat_entity))
5069 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5071 /* If this is not an unconstrained array type, set some flags. */
5072 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5074 if (Treat_As_Volatile (gnat_entity))
5075 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5077 if (Present (Alignment_Clause (gnat_entity)))
5078 TYPE_USER_ALIGN (gnu_type) = 1;
5080 if (Universal_Aliasing (gnat_entity))
5081 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5084 if (!gnu_decl)
5085 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5086 !Comes_From_Source (gnat_entity),
5087 debug_info_p, gnat_entity);
5088 else
5090 TREE_TYPE (gnu_decl) = gnu_type;
5091 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5095 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5097 gnu_type = TREE_TYPE (gnu_decl);
5099 /* If this is a derived type, relate its alias set to that of its parent
5100 to avoid troubles when a call to an inherited primitive is inlined in
5101 a context where a derived object is accessed. The inlined code works
5102 on the parent view so the resulting code may access the same object
5103 using both the parent and the derived alias sets, which thus have to
5104 conflict. As the same issue arises with component references, the
5105 parent alias set also has to conflict with composite types enclosing
5106 derived components. For instance, if we have:
5108 type D is new T;
5109 type R is record
5110 Component : D;
5111 end record;
5113 we want T to conflict with both D and R, in addition to R being a
5114 superset of D by record/component construction.
5116 One way to achieve this is to perform an alias set copy from the
5117 parent to the derived type. This is not quite appropriate, though,
5118 as we don't want separate derived types to conflict with each other:
5120 type I1 is new Integer;
5121 type I2 is new Integer;
5123 We want I1 and I2 to both conflict with Integer but we do not want
5124 I1 to conflict with I2, and an alias set copy on derivation would
5125 have that effect.
5127 The option chosen is to make the alias set of the derived type a
5128 superset of that of its parent type. It trivially fulfills the
5129 simple requirement for the Integer derivation example above, and
5130 the component case as well by superset transitivity:
5132 superset superset
5133 R ----------> D ----------> T
5135 However, for composite types, conversions between derived types are
5136 translated into VIEW_CONVERT_EXPRs so a sequence like:
5138 type Comp1 is new Comp;
5139 type Comp2 is new Comp;
5140 procedure Proc (C : Comp1);
5142 C : Comp2;
5143 Proc (Comp1 (C));
5145 is translated into:
5147 C : Comp2;
5148 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5150 and gimplified into:
5152 C : Comp2;
5153 Comp1 *C.0;
5154 C.0 = (Comp1 *) &C;
5155 Proc (C.0);
5157 i.e. generates code involving type punning. Therefore, Comp1 needs
5158 to conflict with Comp2 and an alias set copy is required.
5160 The language rules ensure the parent type is already frozen here. */
5161 if (kind != E_Subprogram_Type
5162 && Is_Derived_Type (gnat_entity)
5163 && !type_annotate_only)
5165 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5166 /* For constrained packed array subtypes, the implementation type is
5167 used instead of the nominal type. */
5168 if (kind == E_Array_Subtype
5169 && Is_Constrained (gnat_entity)
5170 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5171 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5172 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5173 Is_Composite_Type (gnat_entity)
5174 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5177 /* Back-annotate the Alignment of the type if not already in the
5178 tree. Likewise for sizes. */
5179 if (Unknown_Alignment (gnat_entity))
5181 unsigned int double_align, align;
5182 bool is_capped_double, align_clause;
5184 /* If the default alignment of "double" or larger scalar types is
5185 specifically capped and this is not an array with an alignment
5186 clause on the component type, return the cap. */
5187 if ((double_align = double_float_alignment) > 0)
5188 is_capped_double
5189 = is_double_float_or_array (gnat_entity, &align_clause);
5190 else if ((double_align = double_scalar_alignment) > 0)
5191 is_capped_double
5192 = is_double_scalar_or_array (gnat_entity, &align_clause);
5193 else
5194 is_capped_double = align_clause = false;
5196 if (is_capped_double && !align_clause)
5197 align = double_align;
5198 else
5199 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5201 Set_Alignment (gnat_entity, UI_From_Int (align));
5204 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5206 tree gnu_size = TYPE_SIZE (gnu_type);
5208 /* If the size is self-referential, annotate the maximum value. */
5209 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5210 gnu_size = max_size (gnu_size, true);
5212 /* If we are just annotating types and the type is tagged, the tag
5213 and the parent components are not generated by the front-end so
5214 sizes must be adjusted if there is no representation clause. */
5215 if (type_annotate_only
5216 && Is_Tagged_Type (gnat_entity)
5217 && !VOID_TYPE_P (gnu_type)
5218 && (!TYPE_FIELDS (gnu_type)
5219 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5221 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5222 Uint uint_size;
5224 if (Is_Derived_Type (gnat_entity))
5226 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5227 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5228 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5230 else
5231 offset = pointer_size;
5233 if (TYPE_FIELDS (gnu_type))
5234 offset
5235 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5237 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5238 gnu_size = round_up (gnu_size, POINTER_SIZE);
5239 uint_size = annotate_value (gnu_size);
5240 Set_Esize (gnat_entity, uint_size);
5241 Set_RM_Size (gnat_entity, uint_size);
5243 else
5244 Set_Esize (gnat_entity, annotate_value (gnu_size));
5247 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5248 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5251 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5252 cannot do so if we are reusing the ..._DECL node made for an equivalent
5253 type or an alias or a renamed object as the predicates don't apply to it
5254 but to GNAT_ENTITY. */
5255 if (DECL_P (gnu_decl)
5256 && !(is_type && gnat_equiv_type != gnat_entity)
5257 && !Present (Alias (gnat_entity))
5258 && !(Present (Renamed_Object (gnat_entity)) && saved))
5260 if (!Comes_From_Source (gnat_entity))
5261 DECL_ARTIFICIAL (gnu_decl) = 1;
5263 if (!debug_info_p)
5264 DECL_IGNORED_P (gnu_decl) = 1;
5267 /* If we haven't already, associate the ..._DECL node that we just made with
5268 the input GNAT entity node. */
5269 if (!saved)
5270 save_gnu_tree (gnat_entity, gnu_decl, false);
5272 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5273 eliminate as many deferred computations as possible. */
5274 process_deferred_decl_context (false);
5276 /* If this is an enumeration or floating-point type, we were not able to set
5277 the bounds since they refer to the type. These are always static. */
5278 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5279 || (kind == E_Floating_Point_Type))
5281 tree gnu_scalar_type = gnu_type;
5282 tree gnu_low_bound, gnu_high_bound;
5284 /* If this is a padded type, we need to use the underlying type. */
5285 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5286 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5288 /* If this is a floating point type and we haven't set a floating
5289 point type yet, use this in the evaluation of the bounds. */
5290 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5291 longest_float_type_node = gnu_scalar_type;
5293 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5294 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5296 if (kind == E_Enumeration_Type)
5298 /* Enumeration types have specific RM bounds. */
5299 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5300 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5302 else
5304 /* Floating-point types don't have specific RM bounds. */
5305 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5306 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5310 /* If we deferred processing of incomplete types, re-enable it. If there
5311 were no other disables and we have deferred types to process, do so. */
5312 if (this_deferred
5313 && --defer_incomplete_level == 0
5314 && defer_incomplete_list)
5316 struct incomplete *p, *next;
5318 /* We are back to level 0 for the deferring of incomplete types.
5319 But processing these incomplete types below may itself require
5320 deferring, so preserve what we have and restart from scratch. */
5321 p = defer_incomplete_list;
5322 defer_incomplete_list = NULL;
5324 for (; p; p = next)
5326 next = p->next;
5328 if (p->old_type)
5329 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5330 gnat_to_gnu_type (p->full_type));
5331 free (p);
5335 /* If we are not defining this type, see if it's on one of the lists of
5336 incomplete types. If so, handle the list entry now. */
5337 if (is_type && !definition)
5339 struct incomplete *p;
5341 for (p = defer_incomplete_list; p; p = p->next)
5342 if (p->old_type && p->full_type == gnat_entity)
5344 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5345 TREE_TYPE (gnu_decl));
5346 p->old_type = NULL_TREE;
5349 for (p = defer_limited_with; p; p = p->next)
5350 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5352 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5353 TREE_TYPE (gnu_decl));
5354 p->old_type = NULL_TREE;
5358 if (this_global)
5359 force_global--;
5361 /* If this is a packed array type whose original array type is itself
5362 an Itype without freeze node, make sure the latter is processed. */
5363 if (Is_Packed_Array_Impl_Type (gnat_entity)
5364 && Is_Itype (Original_Array_Type (gnat_entity))
5365 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5366 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5367 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5369 return gnu_decl;
5372 /* Similar, but if the returned value is a COMPONENT_REF, return the
5373 FIELD_DECL. */
5375 tree
5376 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5378 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5380 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5381 gnu_field = TREE_OPERAND (gnu_field, 1);
5383 return gnu_field;
5386 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5387 the GCC type corresponding to that entity. */
5389 tree
5390 gnat_to_gnu_type (Entity_Id gnat_entity)
5392 tree gnu_decl;
5394 /* The back end never attempts to annotate generic types. */
5395 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5396 return void_type_node;
5398 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5399 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5401 return TREE_TYPE (gnu_decl);
5404 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5405 the unpadded version of the GCC type corresponding to that entity. */
5407 tree
5408 get_unpadded_type (Entity_Id gnat_entity)
5410 tree type = gnat_to_gnu_type (gnat_entity);
5412 if (TYPE_IS_PADDING_P (type))
5413 type = TREE_TYPE (TYPE_FIELDS (type));
5415 return type;
5418 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5419 type has been changed to that of the parameterless procedure, except if an
5420 alias is already present, in which case it is returned instead. */
5422 tree
5423 get_minimal_subprog_decl (Entity_Id gnat_entity)
5425 tree gnu_entity_name, gnu_ext_name;
5426 struct attrib *attr_list = NULL;
5428 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5429 of the handling applied here. */
5431 while (Present (Alias (gnat_entity)))
5433 gnat_entity = Alias (gnat_entity);
5434 if (present_gnu_tree (gnat_entity))
5435 return get_gnu_tree (gnat_entity);
5438 gnu_entity_name = get_entity_name (gnat_entity);
5439 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5441 if (Has_Stdcall_Convention (gnat_entity))
5442 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5443 get_identifier ("stdcall"), NULL_TREE,
5444 gnat_entity);
5445 else if (Has_Thiscall_Convention (gnat_entity))
5446 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5447 get_identifier ("thiscall"), NULL_TREE,
5448 gnat_entity);
5450 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5451 gnu_ext_name = NULL_TREE;
5453 return
5454 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5455 is_disabled, true, true, true, attr_list, gnat_entity);
5458 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5459 a C++ imported method or equivalent.
5461 We use the predicate on 32-bit x86/Windows to find out whether we need to
5462 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5463 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5465 bool
5466 is_cplusplus_method (Entity_Id gnat_entity)
5468 if (Convention (gnat_entity) != Convention_CPP)
5469 return false;
5471 /* This is the main case: C++ method imported as a primitive operation.
5472 Note that a C++ class with no virtual functions can be imported as a
5473 limited record type so the operation is not necessarily dispatching. */
5474 if (Is_Primitive (gnat_entity))
5475 return true;
5477 /* A thunk needs to be handled like its associated primitive operation. */
5478 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5479 return true;
5481 /* A constructor is a method on the C++ side. */
5482 if (Is_Constructor (gnat_entity))
5483 return true;
5485 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5486 if (Is_Dispatch_Table_Entity (gnat_entity))
5487 return true;
5489 return false;
5492 /* Finalize the processing of From_Limited_With incomplete types. */
5494 void
5495 finalize_from_limited_with (void)
5497 struct incomplete *p, *next;
5499 p = defer_limited_with;
5500 defer_limited_with = NULL;
5502 for (; p; p = next)
5504 next = p->next;
5506 if (p->old_type)
5507 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5508 gnat_to_gnu_type (p->full_type));
5509 free (p);
5513 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5514 kind of type (such E_Task_Type) that has a different type which Gigi
5515 uses for its representation. If the type does not have a special type
5516 for its representation, return GNAT_ENTITY. If a type is supposed to
5517 exist, but does not, abort unless annotating types, in which case
5518 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5520 Entity_Id
5521 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5523 Entity_Id gnat_equiv = gnat_entity;
5525 if (No (gnat_entity))
5526 return gnat_entity;
5528 switch (Ekind (gnat_entity))
5530 case E_Class_Wide_Subtype:
5531 if (Present (Equivalent_Type (gnat_entity)))
5532 gnat_equiv = Equivalent_Type (gnat_entity);
5533 break;
5535 case E_Access_Protected_Subprogram_Type:
5536 case E_Anonymous_Access_Protected_Subprogram_Type:
5537 gnat_equiv = Equivalent_Type (gnat_entity);
5538 break;
5540 case E_Class_Wide_Type:
5541 gnat_equiv = Root_Type (gnat_entity);
5542 break;
5544 case E_Task_Type:
5545 case E_Task_Subtype:
5546 case E_Protected_Type:
5547 case E_Protected_Subtype:
5548 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5549 break;
5551 default:
5552 break;
5555 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5557 return gnat_equiv;
5560 /* Return a GCC tree for a type corresponding to the component type of the
5561 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5562 is for an array being defined. DEBUG_INFO_P is true if we need to write
5563 debug information for other types that we may create in the process. */
5565 static tree
5566 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5567 bool debug_info_p)
5569 const Entity_Id gnat_type = Component_Type (gnat_array);
5570 tree gnu_type = gnat_to_gnu_type (gnat_type);
5571 tree gnu_comp_size;
5573 /* Try to get a smaller form of the component if needed. */
5574 if ((Is_Packed (gnat_array)
5575 || Has_Component_Size_Clause (gnat_array))
5576 && !Is_Bit_Packed_Array (gnat_array)
5577 && !Has_Aliased_Components (gnat_array)
5578 && !Strict_Alignment (gnat_type)
5579 && RECORD_OR_UNION_TYPE_P (gnu_type)
5580 && !TYPE_FAT_POINTER_P (gnu_type)
5581 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5582 gnu_type = make_packable_type (gnu_type, false);
5584 if (Has_Atomic_Components (gnat_array))
5585 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5587 /* Get and validate any specified Component_Size. */
5588 gnu_comp_size
5589 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5590 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5591 true, Has_Component_Size_Clause (gnat_array));
5593 /* If the array has aliased components and the component size can be zero,
5594 force at least unit size to ensure that the components have distinct
5595 addresses. */
5596 if (!gnu_comp_size
5597 && Has_Aliased_Components (gnat_array)
5598 && (integer_zerop (TYPE_SIZE (gnu_type))
5599 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5600 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5601 gnu_comp_size
5602 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5604 /* If the component type is a RECORD_TYPE that has a self-referential size,
5605 then use the maximum size for the component size. */
5606 if (!gnu_comp_size
5607 && TREE_CODE (gnu_type) == RECORD_TYPE
5608 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5609 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5611 /* Honor the component size. This is not needed for bit-packed arrays. */
5612 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5614 tree orig_type = gnu_type;
5615 unsigned int max_align;
5617 /* If an alignment is specified, use it as a cap on the component type
5618 so that it can be honored for the whole type. But ignore it for the
5619 original type of packed array types. */
5620 if (No (Packed_Array_Impl_Type (gnat_array))
5621 && Known_Alignment (gnat_array))
5622 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5623 else
5624 max_align = 0;
5626 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5627 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5628 gnu_type = orig_type;
5629 else
5630 orig_type = gnu_type;
5632 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5633 true, false, definition, true);
5635 /* If a padding record was made, declare it now since it will never be
5636 declared otherwise. This is necessary to ensure that its subtrees
5637 are properly marked. */
5638 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5639 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5640 gnat_array);
5643 /* If the component type is a padded type made for a non-bit-packed array
5644 of scalars with reverse storage order, we need to propagate the reverse
5645 storage order to the padding type since it is the innermost enclosing
5646 aggregate type around the scalar. */
5647 if (TYPE_IS_PADDING_P (gnu_type)
5648 && Reverse_Storage_Order (gnat_array)
5649 && !Is_Bit_Packed_Array (gnat_array)
5650 && Is_Scalar_Type (gnat_type))
5651 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5653 if (Has_Volatile_Components (gnat_array))
5654 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5656 return gnu_type;
5659 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5660 using MECH as its passing mechanism, to be placed in the parameter
5661 list built for GNAT_SUBPROG. Assume a foreign convention for the
5662 latter if FOREIGN is true. Also set CICO to true if the parameter
5663 must use the copy-in copy-out implementation mechanism.
5665 The returned tree is a PARM_DECL, except for those cases where no
5666 parameter needs to be actually passed to the subprogram; the type
5667 of this "shadow" parameter is then returned instead. */
5669 static tree
5670 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5671 Entity_Id gnat_subprog, bool foreign, bool *cico)
5673 tree gnu_param_name = get_entity_name (gnat_param);
5674 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5675 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5676 /* The parameter can be indirectly modified if its address is taken. */
5677 bool ro_param = in_param && !Address_Taken (gnat_param);
5678 bool by_return = false, by_component_ptr = false;
5679 bool by_ref = false;
5680 tree gnu_param;
5682 /* Copy-return is used only for the first parameter of a valued procedure.
5683 It's a copy mechanism for which a parameter is never allocated. */
5684 if (mech == By_Copy_Return)
5686 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5687 mech = By_Copy;
5688 by_return = true;
5691 /* If this is either a foreign function or if the underlying type won't
5692 be passed by reference and is as aligned as the original type, strip
5693 off possible padding type. */
5694 if (TYPE_IS_PADDING_P (gnu_param_type))
5696 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5698 if (foreign
5699 || (!must_pass_by_ref (unpadded_type)
5700 && mech != By_Reference
5701 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5702 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5703 gnu_param_type = unpadded_type;
5706 /* If this is a read-only parameter, make a variant of the type that is
5707 read-only. ??? However, if this is an unconstrained array, that type
5708 can be very complex, so skip it for now. Likewise for any other
5709 self-referential type. */
5710 if (ro_param
5711 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5712 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5713 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5715 /* For foreign conventions, pass arrays as pointers to the element type.
5716 First check for unconstrained array and get the underlying array. */
5717 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5718 gnu_param_type
5719 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5721 /* For GCC builtins, pass Address integer types as (void *) */
5722 if (Convention (gnat_subprog) == Convention_Intrinsic
5723 && Present (Interface_Name (gnat_subprog))
5724 && Is_Descendent_Of_Address (Etype (gnat_param)))
5725 gnu_param_type = ptr_void_type_node;
5727 /* Arrays are passed as pointers to element type for foreign conventions. */
5728 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5730 /* Strip off any multi-dimensional entries, then strip
5731 off the last array to get the component type. */
5732 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5733 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5734 gnu_param_type = TREE_TYPE (gnu_param_type);
5736 by_component_ptr = true;
5737 gnu_param_type = TREE_TYPE (gnu_param_type);
5739 if (ro_param)
5740 gnu_param_type
5741 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5743 gnu_param_type = build_pointer_type (gnu_param_type);
5746 /* Fat pointers are passed as thin pointers for foreign conventions. */
5747 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5748 gnu_param_type
5749 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5751 /* If we must pass or were requested to pass by reference, do so.
5752 If we were requested to pass by copy, do so.
5753 Otherwise, for foreign conventions, pass In Out or Out parameters
5754 or aggregates by reference. For COBOL and Fortran, pass all
5755 integer and FP types that way too. For Convention Ada, use
5756 the standard Ada default. */
5757 else if (must_pass_by_ref (gnu_param_type)
5758 || mech == By_Reference
5759 || (mech != By_Copy
5760 && ((foreign
5761 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5762 || (foreign
5763 && (Convention (gnat_subprog) == Convention_Fortran
5764 || Convention (gnat_subprog) == Convention_COBOL)
5765 && (INTEGRAL_TYPE_P (gnu_param_type)
5766 || FLOAT_TYPE_P (gnu_param_type)))
5767 || (!foreign
5768 && default_pass_by_ref (gnu_param_type)))))
5770 /* We take advantage of 6.2(12) by considering that references built for
5771 parameters whose type isn't by-ref and for which the mechanism hasn't
5772 been forced to by-ref are restrict-qualified in the C sense. */
5773 bool restrict_p
5774 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5775 gnu_param_type = build_reference_type (gnu_param_type);
5776 if (restrict_p)
5777 gnu_param_type
5778 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5779 by_ref = true;
5782 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5783 else if (!in_param)
5784 *cico = true;
5786 if (mech == By_Copy && (by_ref || by_component_ptr))
5787 post_error ("?cannot pass & by copy", gnat_param);
5789 /* If this is an Out parameter that isn't passed by reference and isn't
5790 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5791 it will be a VAR_DECL created when we process the procedure, so just
5792 return its type. For the special parameter of a valued procedure,
5793 never pass it in.
5795 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5796 Out parameters with discriminants or implicit initial values to be
5797 handled like In Out parameters. These type are normally built as
5798 aggregates, hence passed by reference, except for some packed arrays
5799 which end up encoded in special integer types. Note that scalars can
5800 be given implicit initial values using the Default_Value aspect.
5802 The exception we need to make is then for packed arrays of records
5803 with discriminants or implicit initial values. We have no light/easy
5804 way to check for the latter case, so we merely check for packed arrays
5805 of records. This may lead to useless copy-in operations, but in very
5806 rare cases only, as these would be exceptions in a set of already
5807 exceptional situations. */
5808 if (Ekind (gnat_param) == E_Out_Parameter
5809 && !by_ref
5810 && (by_return
5811 || (!POINTER_TYPE_P (gnu_param_type)
5812 && !AGGREGATE_TYPE_P (gnu_param_type)
5813 && !Has_Default_Aspect (Etype (gnat_param))))
5814 && !(Is_Array_Type (Etype (gnat_param))
5815 && Is_Packed (Etype (gnat_param))
5816 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5817 return gnu_param_type;
5819 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5820 ro_param || by_ref || by_component_ptr);
5821 DECL_BY_REF_P (gnu_param) = by_ref;
5822 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5823 DECL_POINTS_TO_READONLY_P (gnu_param)
5824 = (ro_param && (by_ref || by_component_ptr));
5825 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5827 /* If no Mechanism was specified, indicate what we're using, then
5828 back-annotate it. */
5829 if (mech == Default)
5830 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5832 Set_Mechanism (gnat_param, mech);
5833 return gnu_param;
5836 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5837 with of the main unit and whose full view has not been elaborated yet. */
5839 static bool
5840 is_from_limited_with_of_main (Entity_Id gnat_entity)
5842 /* Class-wide types are always transformed into their root type. */
5843 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5844 gnat_entity = Root_Type (gnat_entity);
5846 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5847 && From_Limited_With (gnat_entity))
5849 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5851 if (present_gnu_tree (gnat_full_view))
5852 return false;
5854 return In_Extended_Main_Code_Unit (gnat_full_view);
5857 return false;
5860 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5861 qualifiers on TYPE. */
5863 static tree
5864 change_qualified_type (tree type, int type_quals)
5866 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5869 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5871 static bool
5872 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5874 while (Present (Corresponding_Discriminant (discr1)))
5875 discr1 = Corresponding_Discriminant (discr1);
5877 while (Present (Corresponding_Discriminant (discr2)))
5878 discr2 = Corresponding_Discriminant (discr2);
5880 return
5881 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5884 /* Return true if the array type GNU_TYPE, which represents a dimension of
5885 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5887 static bool
5888 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5890 /* If the array type is not the innermost dimension of the GNAT type,
5891 then it has a non-aliased component. */
5892 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5893 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5894 return true;
5896 /* If the array type has an aliased component in the front-end sense,
5897 then it also has an aliased component in the back-end sense. */
5898 if (Has_Aliased_Components (gnat_type))
5899 return false;
5901 /* If this is a derived type, then it has a non-aliased component if
5902 and only if its parent type also has one. */
5903 if (Is_Derived_Type (gnat_type))
5905 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5906 int index;
5907 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5908 gnu_parent_type
5909 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5910 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5911 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5912 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5915 /* Otherwise, rely exclusively on properties of the element type. */
5916 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5919 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5921 static bool
5922 compile_time_known_address_p (Node_Id gnat_address)
5924 /* Catch System'To_Address. */
5925 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5926 gnat_address = Expression (gnat_address);
5928 return Compile_Time_Known_Value (gnat_address);
5931 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5932 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5934 static bool
5935 cannot_be_superflat_p (Node_Id gnat_range)
5937 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5938 Node_Id scalar_range;
5939 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5941 /* If the low bound is not constant, try to find an upper bound. */
5942 while (Nkind (gnat_lb) != N_Integer_Literal
5943 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5944 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5945 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5946 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5947 || Nkind (scalar_range) == N_Range))
5948 gnat_lb = High_Bound (scalar_range);
5950 /* If the high bound is not constant, try to find a lower bound. */
5951 while (Nkind (gnat_hb) != N_Integer_Literal
5952 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5953 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5954 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5955 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5956 || Nkind (scalar_range) == N_Range))
5957 gnat_hb = Low_Bound (scalar_range);
5959 /* If we have failed to find constant bounds, punt. */
5960 if (Nkind (gnat_lb) != N_Integer_Literal
5961 || Nkind (gnat_hb) != N_Integer_Literal)
5962 return false;
5964 /* We need at least a signed 64-bit type to catch most cases. */
5965 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5966 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5967 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5968 return false;
5970 /* If the low bound is the smallest integer, nothing can be smaller. */
5971 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5972 if (TREE_OVERFLOW (gnu_lb_minus_one))
5973 return true;
5975 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5978 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5980 static bool
5981 constructor_address_p (tree gnu_expr)
5983 while (TREE_CODE (gnu_expr) == NOP_EXPR
5984 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5985 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5986 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5988 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5989 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5992 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5993 be elaborated at the point of its definition, but do nothing else. */
5995 void
5996 elaborate_entity (Entity_Id gnat_entity)
5998 switch (Ekind (gnat_entity))
6000 case E_Signed_Integer_Subtype:
6001 case E_Modular_Integer_Subtype:
6002 case E_Enumeration_Subtype:
6003 case E_Ordinary_Fixed_Point_Subtype:
6004 case E_Decimal_Fixed_Point_Subtype:
6005 case E_Floating_Point_Subtype:
6007 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6008 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6010 /* ??? Tests to avoid Constraint_Error in static expressions
6011 are needed until after the front stops generating bogus
6012 conversions on bounds of real types. */
6013 if (!Raises_Constraint_Error (gnat_lb))
6014 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
6015 true, false, Needs_Debug_Info (gnat_entity));
6016 if (!Raises_Constraint_Error (gnat_hb))
6017 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
6018 true, false, Needs_Debug_Info (gnat_entity));
6019 break;
6022 case E_Record_Subtype:
6023 case E_Private_Subtype:
6024 case E_Limited_Private_Subtype:
6025 case E_Record_Subtype_With_Private:
6026 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6028 Node_Id gnat_discriminant_expr;
6029 Entity_Id gnat_field;
6031 for (gnat_field
6032 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6033 gnat_discriminant_expr
6034 = First_Elmt (Discriminant_Constraint (gnat_entity));
6035 Present (gnat_field);
6036 gnat_field = Next_Discriminant (gnat_field),
6037 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6038 /* Ignore access discriminants. */
6039 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6040 elaborate_expression (Node (gnat_discriminant_expr),
6041 gnat_entity, get_entity_name (gnat_field),
6042 true, false, false);
6044 break;
6049 /* Return true if the size in units represented by GNU_SIZE can be handled by
6050 an allocation. If STATIC_P is true, consider only what can be done with a
6051 static allocation. */
6053 static bool
6054 allocatable_size_p (tree gnu_size, bool static_p)
6056 /* We can allocate a fixed size if it is a valid for the middle-end. */
6057 if (TREE_CODE (gnu_size) == INTEGER_CST)
6058 return valid_constant_size_p (gnu_size);
6060 /* We can allocate a variable size if this isn't a static allocation. */
6061 else
6062 return !static_p;
6065 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6066 NAME, ARGS and ERROR_POINT. */
6068 static void
6069 prepend_one_attribute (struct attrib **attr_list,
6070 enum attr_type attr_type,
6071 tree attr_name,
6072 tree attr_args,
6073 Node_Id attr_error_point)
6075 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6077 attr->type = attr_type;
6078 attr->name = attr_name;
6079 attr->args = attr_args;
6080 attr->error_point = attr_error_point;
6082 attr->next = *attr_list;
6083 *attr_list = attr;
6086 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6088 static void
6089 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6091 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6092 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6093 enum attr_type etype;
6095 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6096 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6098 case Pragma_Machine_Attribute:
6099 etype = ATTR_MACHINE_ATTRIBUTE;
6100 break;
6102 case Pragma_Linker_Alias:
6103 etype = ATTR_LINK_ALIAS;
6104 break;
6106 case Pragma_Linker_Section:
6107 etype = ATTR_LINK_SECTION;
6108 break;
6110 case Pragma_Linker_Constructor:
6111 etype = ATTR_LINK_CONSTRUCTOR;
6112 break;
6114 case Pragma_Linker_Destructor:
6115 etype = ATTR_LINK_DESTRUCTOR;
6116 break;
6118 case Pragma_Weak_External:
6119 etype = ATTR_WEAK_EXTERNAL;
6120 break;
6122 case Pragma_Thread_Local_Storage:
6123 etype = ATTR_THREAD_LOCAL_STORAGE;
6124 break;
6126 default:
6127 return;
6130 /* See what arguments we have and turn them into GCC trees for attribute
6131 handlers. These expect identifier for strings. We handle at most two
6132 arguments and static expressions only. */
6133 if (Present (gnat_arg) && Present (First (gnat_arg)))
6135 Node_Id gnat_arg0 = Next (First (gnat_arg));
6136 Node_Id gnat_arg1 = Empty;
6138 if (Present (gnat_arg0)
6139 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6141 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6143 if (TREE_CODE (gnu_arg0) == STRING_CST)
6145 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6146 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6147 return;
6150 gnat_arg1 = Next (gnat_arg0);
6153 if (Present (gnat_arg1)
6154 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6156 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6158 if (TREE_CODE (gnu_arg1) == STRING_CST)
6159 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6163 /* Prepend to the list. Make a list of the argument we might have, as GCC
6164 expects it. */
6165 prepend_one_attribute (attr_list, etype, gnu_arg0,
6166 gnu_arg1
6167 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6168 Present (Next (First (gnat_arg)))
6169 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6172 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6174 static void
6175 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6177 Node_Id gnat_temp;
6179 /* Attributes are stored as Representation Item pragmas. */
6180 for (gnat_temp = First_Rep_Item (gnat_entity);
6181 Present (gnat_temp);
6182 gnat_temp = Next_Rep_Item (gnat_temp))
6183 if (Nkind (gnat_temp) == N_Pragma)
6184 prepend_one_attribute_pragma (attr_list, gnat_temp);
6187 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6188 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6189 return the GCC tree to use for that expression. GNU_NAME is the suffix
6190 to use if a variable needs to be created and DEFINITION is true if this
6191 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6192 otherwise, we are just elaborating the expression for side-effects. If
6193 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6194 isn't needed for code generation. */
6196 static tree
6197 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6198 bool definition, bool need_value, bool need_debug)
6200 tree gnu_expr;
6202 /* If we already elaborated this expression (e.g. it was involved
6203 in the definition of a private type), use the old value. */
6204 if (present_gnu_tree (gnat_expr))
6205 return get_gnu_tree (gnat_expr);
6207 /* If we don't need a value and this is static or a discriminant,
6208 we don't need to do anything. */
6209 if (!need_value
6210 && (Is_OK_Static_Expression (gnat_expr)
6211 || (Nkind (gnat_expr) == N_Identifier
6212 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6213 return NULL_TREE;
6215 /* If it's a static expression, we don't need a variable for debugging. */
6216 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6217 need_debug = false;
6219 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6220 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6221 gnu_name, definition, need_debug);
6223 /* Save the expression in case we try to elaborate this entity again. Since
6224 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6225 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6226 save_gnu_tree (gnat_expr, gnu_expr, true);
6228 return need_value ? gnu_expr : error_mark_node;
6231 /* Similar, but take a GNU expression and always return a result. */
6233 static tree
6234 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6235 bool definition, bool need_debug)
6237 const bool expr_public_p = Is_Public (gnat_entity);
6238 const bool expr_global_p = expr_public_p || global_bindings_p ();
6239 bool expr_variable_p, use_variable;
6241 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6242 reference will have been replaced with a COMPONENT_REF when the type
6243 is being elaborated. However, there are some cases involving child
6244 types where we will. So convert it to a COMPONENT_REF. We hope it
6245 will be at the highest level of the expression in these cases. */
6246 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6247 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6248 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6249 gnu_expr, NULL_TREE);
6251 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6252 that an expression cannot contain both a discriminant and a variable. */
6253 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6254 return gnu_expr;
6256 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6257 a variable that is initialized to contain the expression when the package
6258 containing the definition is elaborated. If this entity is defined at top
6259 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6260 if this is necessary. */
6261 if (CONSTANT_CLASS_P (gnu_expr))
6262 expr_variable_p = false;
6263 else
6265 /* Skip any conversions and simple constant arithmetics to see if the
6266 expression is based on a read-only variable.
6267 ??? This really should remain read-only, but we have to think about
6268 the typing of the tree here. */
6269 tree inner = remove_conversions (gnu_expr, true);
6271 inner = skip_simple_constant_arithmetic (inner);
6273 if (handled_component_p (inner))
6275 HOST_WIDE_INT bitsize, bitpos;
6276 tree offset;
6277 machine_mode mode;
6278 int unsignedp, reversep, volatilep;
6280 inner
6281 = get_inner_reference (inner, &bitsize, &bitpos, &offset, &mode,
6282 &unsignedp, &reversep, &volatilep, false);
6283 /* If the offset is variable, err on the side of caution. */
6284 if (offset)
6285 inner = NULL_TREE;
6288 expr_variable_p
6289 = !(inner
6290 && TREE_CODE (inner) == VAR_DECL
6291 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6294 /* We only need to use the variable if we are in a global context since GCC
6295 can do the right thing in the local case. However, when not optimizing,
6296 use it for bounds of loop iteration scheme to avoid code duplication. */
6297 use_variable = expr_variable_p
6298 && (expr_global_p
6299 || (!optimize
6300 && definition
6301 && Is_Itype (gnat_entity)
6302 && Nkind (Associated_Node_For_Itype (gnat_entity))
6303 == N_Loop_Parameter_Specification));
6305 /* Now create it, possibly only for debugging purposes. */
6306 if (use_variable || need_debug)
6308 /* The following variable creation can happen when processing the body of
6309 subprograms that are defined out of the extended main unit and
6310 inlined. In this case, we are not at the global scope, and thus the
6311 new variable must not be tagged "external", as we used to do here as
6312 long as definition == 0. */
6313 const bool external_flag = !definition && expr_global_p;
6314 tree gnu_decl
6315 = create_var_decl_1
6316 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6317 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6318 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6320 DECL_ARTIFICIAL (gnu_decl) = 1;
6322 /* Using this variable at debug time (if need_debug is true) requires a
6323 proper location. The back-end will compute a location for this
6324 variable only if the variable is used by the generated code.
6325 Returning the variable ensures the caller will use it in generated
6326 code. Note that there is no need for a location if the debug info
6327 contains an integer constant.
6328 FIXME: when the encoding-based debug scheme is dropped, move this
6329 condition to the top-level IF block: we will not need to create a
6330 variable anymore in such cases, then. */
6331 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6332 return gnu_decl;
6335 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6338 /* Similar, but take an alignment factor and make it explicit in the tree. */
6340 static tree
6341 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6342 bool definition, bool need_debug, unsigned int align)
6344 tree unit_align = size_int (align / BITS_PER_UNIT);
6345 return
6346 size_binop (MULT_EXPR,
6347 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6348 gnu_expr,
6349 unit_align),
6350 gnat_entity, gnu_name, definition,
6351 need_debug),
6352 unit_align);
6355 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6356 the value passed against the list of choices. */
6358 tree
6359 choices_to_gnu (tree operand, Node_Id choices)
6361 Node_Id choice;
6362 Node_Id gnat_temp;
6363 tree result = boolean_false_node;
6364 tree this_test, low = 0, high = 0, single = 0;
6366 for (choice = First (choices); Present (choice); choice = Next (choice))
6368 switch (Nkind (choice))
6370 case N_Range:
6371 low = gnat_to_gnu (Low_Bound (choice));
6372 high = gnat_to_gnu (High_Bound (choice));
6374 this_test
6375 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6376 build_binary_op (GE_EXPR, boolean_type_node,
6377 operand, low),
6378 build_binary_op (LE_EXPR, boolean_type_node,
6379 operand, high));
6381 break;
6383 case N_Subtype_Indication:
6384 gnat_temp = Range_Expression (Constraint (choice));
6385 low = gnat_to_gnu (Low_Bound (gnat_temp));
6386 high = gnat_to_gnu (High_Bound (gnat_temp));
6388 this_test
6389 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6390 build_binary_op (GE_EXPR, boolean_type_node,
6391 operand, low),
6392 build_binary_op (LE_EXPR, boolean_type_node,
6393 operand, high));
6394 break;
6396 case N_Identifier:
6397 case N_Expanded_Name:
6398 /* This represents either a subtype range, an enumeration
6399 literal, or a constant Ekind says which. If an enumeration
6400 literal or constant, fall through to the next case. */
6401 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6402 && Ekind (Entity (choice)) != E_Constant)
6404 tree type = gnat_to_gnu_type (Entity (choice));
6406 low = TYPE_MIN_VALUE (type);
6407 high = TYPE_MAX_VALUE (type);
6409 this_test
6410 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6411 build_binary_op (GE_EXPR, boolean_type_node,
6412 operand, low),
6413 build_binary_op (LE_EXPR, boolean_type_node,
6414 operand, high));
6415 break;
6418 /* ... fall through ... */
6420 case N_Character_Literal:
6421 case N_Integer_Literal:
6422 single = gnat_to_gnu (choice);
6423 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6424 single);
6425 break;
6427 case N_Others_Choice:
6428 this_test = boolean_true_node;
6429 break;
6431 default:
6432 gcc_unreachable ();
6435 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6436 this_test);
6439 return result;
6442 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6443 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6445 static int
6446 adjust_packed (tree field_type, tree record_type, int packed)
6448 /* If the field contains an item of variable size, we cannot pack it
6449 because we cannot create temporaries of non-fixed size in case
6450 we need to take the address of the field. See addressable_p and
6451 the notes on the addressability issues for further details. */
6452 if (type_has_variable_size (field_type))
6453 return 0;
6455 /* If the alignment of the record is specified and the field type
6456 is over-aligned, request Storage_Unit alignment for the field. */
6457 if (packed == -2)
6459 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6460 return -1;
6461 else
6462 return 0;
6465 return packed;
6468 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6469 placed in GNU_RECORD_TYPE.
6471 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6472 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6473 record has a specified alignment.
6475 DEFINITION is true if this field is for a record being defined.
6477 DEBUG_INFO_P is true if we need to write debug information for types
6478 that we may create in the process. */
6480 static tree
6481 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6482 bool definition, bool debug_info_p)
6484 const Entity_Id gnat_field_type = Etype (gnat_field);
6485 const bool is_aliased
6486 = Is_Aliased (gnat_field);
6487 const bool is_atomic
6488 = (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type));
6489 const bool is_independent
6490 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6491 const bool is_volatile
6492 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6493 const bool needs_strict_alignment
6494 = (is_aliased
6495 || is_independent
6496 || is_volatile
6497 || Strict_Alignment (gnat_field_type));
6498 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6499 tree gnu_field_id = get_entity_name (gnat_field);
6500 tree gnu_field, gnu_size, gnu_pos;
6502 /* If this field requires strict alignment, we cannot pack it because
6503 it would very likely be under-aligned in the record. */
6504 if (needs_strict_alignment)
6505 packed = 0;
6506 else
6507 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6509 /* If a size is specified, use it. Otherwise, if the record type is packed,
6510 use the official RM size. See "Handling of Type'Size Values" in Einfo
6511 for further details. */
6512 if (Known_Esize (gnat_field))
6513 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6514 gnat_field, FIELD_DECL, false, true);
6515 else if (packed == 1)
6516 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6517 gnat_field, FIELD_DECL, false, true);
6518 else
6519 gnu_size = NULL_TREE;
6521 /* If we have a specified size that is smaller than that of the field's type,
6522 or a position is specified, and the field's type is a record that doesn't
6523 require strict alignment, see if we can get either an integral mode form
6524 of the type or a smaller form. If we can, show a size was specified for
6525 the field if there wasn't one already, so we know to make this a bitfield
6526 and avoid making things wider.
6528 Changing to an integral mode form is useful when the record is packed as
6529 we can then place the field at a non-byte-aligned position and so achieve
6530 tighter packing. This is in addition required if the field shares a byte
6531 with another field and the front-end lets the back-end handle the access
6532 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6534 Changing to a smaller form is required if the specified size is smaller
6535 than that of the field's type and the type contains sub-fields that are
6536 padded, in order to avoid generating accesses to these sub-fields that
6537 are wider than the field.
6539 We avoid the transformation if it is not required or potentially useful,
6540 as it might entail an increase of the field's alignment and have ripple
6541 effects on the outer record type. A typical case is a field known to be
6542 byte-aligned and not to share a byte with another field. */
6543 if (!needs_strict_alignment
6544 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6545 && !TYPE_FAT_POINTER_P (gnu_field_type)
6546 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6547 && (packed == 1
6548 || (gnu_size
6549 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6550 || (Present (Component_Clause (gnat_field))
6551 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6552 % BITS_PER_UNIT == 0
6553 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6555 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6556 if (gnu_packable_type != gnu_field_type)
6558 gnu_field_type = gnu_packable_type;
6559 if (!gnu_size)
6560 gnu_size = rm_size (gnu_field_type);
6564 if (Is_Atomic (gnat_field))
6565 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6567 if (Present (Component_Clause (gnat_field)))
6569 Node_Id gnat_clause = Component_Clause (gnat_field);
6570 Entity_Id gnat_parent
6571 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6573 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6574 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6575 gnat_field, FIELD_DECL, false, true);
6577 /* Ensure the position does not overlap with the parent subtype, if there
6578 is one. This test is omitted if the parent of the tagged type has a
6579 full rep clause since, in this case, component clauses are allowed to
6580 overlay the space allocated for the parent type and the front-end has
6581 checked that there are no overlapping components. */
6582 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6584 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6586 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6587 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6588 post_error_ne_tree
6589 ("offset of& must be beyond parent{, minimum allowed is ^}",
6590 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6593 /* If this field needs strict alignment, make sure that the record is
6594 sufficiently aligned and that the position and size are consistent
6595 with the type. But don't do it if we are just annotating types and
6596 the field's type is tagged, since tagged types aren't fully laid out
6597 in this mode. Also, note that atomic implies volatile so the inner
6598 test sequences ordering is significant here. */
6599 if (needs_strict_alignment
6600 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6602 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6604 if (TYPE_ALIGN (gnu_record_type) < type_align)
6605 TYPE_ALIGN (gnu_record_type) = type_align;
6607 /* If the position is not a multiple of the alignment of the type,
6608 then error out and reset the position. */
6609 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6610 bitsize_int (type_align))))
6612 const char *s;
6614 if (is_atomic)
6615 s = "position of atomic field& must be multiple of ^ bits";
6616 else if (is_aliased)
6617 s = "position of aliased field& must be multiple of ^ bits";
6618 else if (is_independent)
6619 s = "position of independent field& must be multiple of ^ bits";
6620 else if (is_volatile)
6621 s = "position of volatile field& must be multiple of ^ bits";
6622 else if (Strict_Alignment (gnat_field_type))
6623 s = "position of & with aliased or tagged part must be"
6624 " multiple of ^ bits";
6625 else
6626 gcc_unreachable ();
6628 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6629 type_align);
6630 gnu_pos = NULL_TREE;
6633 if (gnu_size)
6635 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6636 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6638 /* If the size is lower than that of the type, or greater for
6639 atomic and aliased, then error out and reset the size. */
6640 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6642 const char *s;
6644 if (is_atomic)
6645 s = "size of atomic field& must be ^ bits";
6646 else if (is_aliased)
6647 s = "size of aliased field& must be ^ bits";
6648 else if (is_independent)
6649 s = "size of independent field& must be at least ^ bits";
6650 else if (is_volatile)
6651 s = "size of volatile field& must be at least ^ bits";
6652 else if (Strict_Alignment (gnat_field_type))
6653 s = "size of & with aliased or tagged part must be"
6654 " at least ^ bits";
6655 else
6656 gcc_unreachable ();
6658 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6659 gnu_type_size);
6660 gnu_size = NULL_TREE;
6663 /* Likewise if the size is not a multiple of a byte, */
6664 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6665 bitsize_unit_node)))
6667 const char *s;
6669 if (is_independent)
6670 s = "size of independent field& must be multiple of"
6671 " Storage_Unit";
6672 else if (is_volatile)
6673 s = "size of volatile field& must be multiple of"
6674 " Storage_Unit";
6675 else if (Strict_Alignment (gnat_field_type))
6676 s = "size of & with aliased or tagged part must be"
6677 " multiple of Storage_Unit";
6678 else
6679 gcc_unreachable ();
6681 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6682 gnu_size = NULL_TREE;
6688 /* If the record has rep clauses and this is the tag field, make a rep
6689 clause for it as well. */
6690 else if (Has_Specified_Layout (Scope (gnat_field))
6691 && Chars (gnat_field) == Name_uTag)
6693 gnu_pos = bitsize_zero_node;
6694 gnu_size = TYPE_SIZE (gnu_field_type);
6697 else
6699 gnu_pos = NULL_TREE;
6701 /* If we are packing the record and the field is BLKmode, round the
6702 size up to a byte boundary. */
6703 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6704 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6707 /* We need to make the size the maximum for the type if it is
6708 self-referential and an unconstrained type. In that case, we can't
6709 pack the field since we can't make a copy to align it. */
6710 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6711 && !gnu_size
6712 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6713 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6715 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6716 packed = 0;
6719 /* If a size is specified, adjust the field's type to it. */
6720 if (gnu_size)
6722 tree orig_field_type;
6724 /* If the field's type is justified modular, we would need to remove
6725 the wrapper to (better) meet the layout requirements. However we
6726 can do so only if the field is not aliased to preserve the unique
6727 layout and if the prescribed size is not greater than that of the
6728 packed array to preserve the justification. */
6729 if (!needs_strict_alignment
6730 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6731 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6732 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6733 <= 0)
6734 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6736 /* Similarly if the field's type is a misaligned integral type, but
6737 there is no restriction on the size as there is no justification. */
6738 if (!needs_strict_alignment
6739 && TYPE_IS_PADDING_P (gnu_field_type)
6740 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6741 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6743 gnu_field_type
6744 = make_type_from_size (gnu_field_type, gnu_size,
6745 Has_Biased_Representation (gnat_field));
6747 orig_field_type = gnu_field_type;
6748 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6749 false, false, definition, true);
6751 /* If a padding record was made, declare it now since it will never be
6752 declared otherwise. This is necessary to ensure that its subtrees
6753 are properly marked. */
6754 if (gnu_field_type != orig_field_type
6755 && !DECL_P (TYPE_NAME (gnu_field_type)))
6756 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6757 debug_info_p, gnat_field);
6760 /* Otherwise (or if there was an error), don't specify a position. */
6761 else
6762 gnu_pos = NULL_TREE;
6764 /* If the field's type is a padded type made for a scalar field of a record
6765 type with reverse storage order, we need to propagate the reverse storage
6766 order to the padding type since it is the innermost enclosing aggregate
6767 type around the scalar. */
6768 if (TYPE_IS_PADDING_P (gnu_field_type)
6769 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
6770 && Is_Scalar_Type (gnat_field_type))
6771 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
6773 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6774 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6776 /* Now create the decl for the field. */
6777 gnu_field
6778 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6779 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6780 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6781 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6782 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6784 if (Ekind (gnat_field) == E_Discriminant)
6785 DECL_DISCRIMINANT_NUMBER (gnu_field)
6786 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6788 return gnu_field;
6791 /* Return true if at least one member of COMPONENT_LIST needs strict
6792 alignment. */
6794 static bool
6795 components_need_strict_alignment (Node_Id component_list)
6797 Node_Id component_decl;
6799 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6800 Present (component_decl);
6801 component_decl = Next_Non_Pragma (component_decl))
6803 Entity_Id gnat_field = Defining_Entity (component_decl);
6805 if (Is_Aliased (gnat_field))
6806 return true;
6808 if (Strict_Alignment (Etype (gnat_field)))
6809 return true;
6812 return false;
6815 /* Return true if TYPE is a type with variable size or a padding type with a
6816 field of variable size or a record that has a field with such a type. */
6818 static bool
6819 type_has_variable_size (tree type)
6821 tree field;
6823 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6824 return true;
6826 if (TYPE_IS_PADDING_P (type)
6827 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6828 return true;
6830 if (!RECORD_OR_UNION_TYPE_P (type))
6831 return false;
6833 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6834 if (type_has_variable_size (TREE_TYPE (field)))
6835 return true;
6837 return false;
6840 /* Return true if FIELD is an artificial field. */
6842 static bool
6843 field_is_artificial (tree field)
6845 /* These fields are generated by the front-end proper. */
6846 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6847 return true;
6849 /* These fields are generated by gigi. */
6850 if (DECL_INTERNAL_P (field))
6851 return true;
6853 return false;
6856 /* Return true if FIELD is a non-artificial aliased field. */
6858 static bool
6859 field_is_aliased (tree field)
6861 if (field_is_artificial (field))
6862 return false;
6864 return DECL_ALIASED_P (field);
6867 /* Return true if FIELD is a non-artificial field with self-referential
6868 size. */
6870 static bool
6871 field_has_self_size (tree field)
6873 if (field_is_artificial (field))
6874 return false;
6876 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6877 return false;
6879 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6882 /* Return true if FIELD is a non-artificial field with variable size. */
6884 static bool
6885 field_has_variable_size (tree field)
6887 if (field_is_artificial (field))
6888 return false;
6890 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6891 return false;
6893 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6896 /* qsort comparer for the bit positions of two record components. */
6898 static int
6899 compare_field_bitpos (const PTR rt1, const PTR rt2)
6901 const_tree const field1 = * (const_tree const *) rt1;
6902 const_tree const field2 = * (const_tree const *) rt2;
6903 const int ret
6904 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6906 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6909 /* Structure holding information for a given variant. */
6910 typedef struct vinfo
6912 /* The record type of the variant. */
6913 tree type;
6915 /* The name of the variant. */
6916 tree name;
6918 /* The qualifier of the variant. */
6919 tree qual;
6921 /* Whether the variant has a rep clause. */
6922 bool has_rep;
6924 /* Whether the variant is packed. */
6925 bool packed;
6927 } vinfo_t;
6929 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6930 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6931 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6932 When called from gnat_to_gnu_entity during the processing of a record type
6933 definition, the GCC node for the parent, if any, will be the single field
6934 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6935 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6936 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6938 PACKED is 1 if this is for a packed record, -1 if this is for a record
6939 with Component_Alignment of Storage_Unit, -2 if this is for a record
6940 with a specified alignment.
6942 DEFINITION is true if we are defining this record type.
6944 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6945 out the record. This means the alignment only serves to force fields to
6946 be bitfields, but not to require the record to be that aligned. This is
6947 used for variants.
6949 ALL_REP is true if a rep clause is present for all the fields.
6951 UNCHECKED_UNION is true if we are building this type for a record with a
6952 Pragma Unchecked_Union.
6954 ARTIFICIAL is true if this is a type that was generated by the compiler.
6956 DEBUG_INFO is true if we need to write debug information about the type.
6958 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6959 mean that its contents may be unused as well, only the container itself.
6961 REORDER is true if we are permitted to reorder components of this type.
6963 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6964 the outer record type down to this variant level. It is nonzero only if
6965 all the fields down to this level have a rep clause and ALL_REP is false.
6967 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6968 with a rep clause is to be added; in this case, that is all that should
6969 be done with such fields and the return value will be false. */
6971 static bool
6972 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6973 tree gnu_field_list, int packed, bool definition,
6974 bool cancel_alignment, bool all_rep,
6975 bool unchecked_union, bool artificial,
6976 bool debug_info, bool maybe_unused, bool reorder,
6977 tree first_free_pos, tree *p_gnu_rep_list)
6979 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6980 bool variants_have_rep = all_rep;
6981 bool layout_with_rep = false;
6982 bool has_self_field = false;
6983 bool has_aliased_after_self_field = false;
6984 Node_Id component_decl, variant_part;
6985 tree gnu_field, gnu_next, gnu_last;
6986 tree gnu_variant_part = NULL_TREE;
6987 tree gnu_rep_list = NULL_TREE;
6988 tree gnu_var_list = NULL_TREE;
6989 tree gnu_self_list = NULL_TREE;
6990 tree gnu_zero_list = NULL_TREE;
6992 /* For each component referenced in a component declaration create a GCC
6993 field and add it to the list, skipping pragmas in the GNAT list. */
6994 gnu_last = tree_last (gnu_field_list);
6995 if (Present (Component_Items (gnat_component_list)))
6996 for (component_decl
6997 = First_Non_Pragma (Component_Items (gnat_component_list));
6998 Present (component_decl);
6999 component_decl = Next_Non_Pragma (component_decl))
7001 Entity_Id gnat_field = Defining_Entity (component_decl);
7002 Name_Id gnat_name = Chars (gnat_field);
7004 /* If present, the _Parent field must have been created as the single
7005 field of the record type. Put it before any other fields. */
7006 if (gnat_name == Name_uParent)
7008 gnu_field = TYPE_FIELDS (gnu_record_type);
7009 gnu_field_list = chainon (gnu_field_list, gnu_field);
7011 else
7013 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7014 definition, debug_info);
7016 /* If this is the _Tag field, put it before any other fields. */
7017 if (gnat_name == Name_uTag)
7018 gnu_field_list = chainon (gnu_field_list, gnu_field);
7020 /* If this is the _Controller field, put it before the other
7021 fields except for the _Tag or _Parent field. */
7022 else if (gnat_name == Name_uController && gnu_last)
7024 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7025 DECL_CHAIN (gnu_last) = gnu_field;
7028 /* If this is a regular field, put it after the other fields. */
7029 else
7031 DECL_CHAIN (gnu_field) = gnu_field_list;
7032 gnu_field_list = gnu_field;
7033 if (!gnu_last)
7034 gnu_last = gnu_field;
7036 /* And record information for the final layout. */
7037 if (field_has_self_size (gnu_field))
7038 has_self_field = true;
7039 else if (has_self_field && field_is_aliased (gnu_field))
7040 has_aliased_after_self_field = true;
7044 save_gnu_tree (gnat_field, gnu_field, false);
7047 /* At the end of the component list there may be a variant part. */
7048 variant_part = Variant_Part (gnat_component_list);
7050 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7051 mutually exclusive and should go in the same memory. To do this we need
7052 to treat each variant as a record whose elements are created from the
7053 component list for the variant. So here we create the records from the
7054 lists for the variants and put them all into the QUAL_UNION_TYPE.
7055 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7056 use GNU_RECORD_TYPE if there are no fields so far. */
7057 if (Present (variant_part))
7059 Node_Id gnat_discr = Name (variant_part), variant;
7060 tree gnu_discr = gnat_to_gnu (gnat_discr);
7061 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7062 tree gnu_var_name
7063 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7064 "XVN");
7065 tree gnu_union_type, gnu_union_name;
7066 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7067 bool union_field_needs_strict_alignment = false;
7068 auto_vec <vinfo_t, 16> variant_types;
7069 vinfo_t *gnu_variant;
7070 unsigned int variants_align = 0;
7071 unsigned int i;
7073 gnu_union_name
7074 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7076 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7077 are all in the variant part, to match the layout of C unions. There
7078 is an associated check below. */
7079 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7080 gnu_union_type = gnu_record_type;
7081 else
7083 gnu_union_type
7084 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7086 TYPE_NAME (gnu_union_type) = gnu_union_name;
7087 TYPE_ALIGN (gnu_union_type) = 0;
7088 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7089 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7090 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7093 /* If all the fields down to this level have a rep clause, find out
7094 whether all the fields at this level also have one. If so, then
7095 compute the new first free position to be passed downward. */
7096 this_first_free_pos = first_free_pos;
7097 if (this_first_free_pos)
7099 for (gnu_field = gnu_field_list;
7100 gnu_field;
7101 gnu_field = DECL_CHAIN (gnu_field))
7102 if (DECL_FIELD_OFFSET (gnu_field))
7104 tree pos = bit_position (gnu_field);
7105 if (!tree_int_cst_lt (pos, this_first_free_pos))
7106 this_first_free_pos
7107 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7109 else
7111 this_first_free_pos = NULL_TREE;
7112 break;
7116 /* We build the variants in two passes. The bulk of the work is done in
7117 the first pass, that is to say translating the GNAT nodes, building
7118 the container types and computing the associated properties. However
7119 we cannot finish up the container types during this pass because we
7120 don't know where the variant part will be placed until the end. */
7121 for (variant = First_Non_Pragma (Variants (variant_part));
7122 Present (variant);
7123 variant = Next_Non_Pragma (variant))
7125 tree gnu_variant_type = make_node (RECORD_TYPE);
7126 tree gnu_inner_name, gnu_qual;
7127 bool has_rep;
7128 int field_packed;
7129 vinfo_t vinfo;
7131 Get_Variant_Encoding (variant);
7132 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7133 TYPE_NAME (gnu_variant_type)
7134 = concat_name (gnu_union_name,
7135 IDENTIFIER_POINTER (gnu_inner_name));
7137 /* Set the alignment of the inner type in case we need to make
7138 inner objects into bitfields, but then clear it out so the
7139 record actually gets only the alignment required. */
7140 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7141 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7142 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7143 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7145 /* Similarly, if the outer record has a size specified and all
7146 the fields have a rep clause, we can propagate the size. */
7147 if (all_rep_and_size)
7149 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7150 TYPE_SIZE_UNIT (gnu_variant_type)
7151 = TYPE_SIZE_UNIT (gnu_record_type);
7154 /* Add the fields into the record type for the variant. Note that
7155 we aren't sure to really use it at this point, see below. */
7156 has_rep
7157 = components_to_record (gnu_variant_type, Component_List (variant),
7158 NULL_TREE, packed, definition,
7159 !all_rep_and_size, all_rep,
7160 unchecked_union,
7161 true, debug_info, true, reorder,
7162 this_first_free_pos,
7163 all_rep || this_first_free_pos
7164 ? NULL : &gnu_rep_list);
7166 /* Translate the qualifier and annotate the GNAT node. */
7167 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7168 Set_Present_Expr (variant, annotate_value (gnu_qual));
7170 /* Deal with packedness like in gnat_to_gnu_field. */
7171 if (components_need_strict_alignment (Component_List (variant)))
7173 field_packed = 0;
7174 union_field_needs_strict_alignment = true;
7176 else
7177 field_packed
7178 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7180 /* Push this variant onto the stack for the second pass. */
7181 vinfo.type = gnu_variant_type;
7182 vinfo.name = gnu_inner_name;
7183 vinfo.qual = gnu_qual;
7184 vinfo.has_rep = has_rep;
7185 vinfo.packed = field_packed;
7186 variant_types.safe_push (vinfo);
7188 /* Compute the global properties that will determine the placement of
7189 the variant part. */
7190 variants_have_rep |= has_rep;
7191 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7192 variants_align = TYPE_ALIGN (gnu_variant_type);
7195 /* Round up the first free position to the alignment of the variant part
7196 for the variants without rep clause. This will guarantee a consistent
7197 layout independently of the placement of the variant part. */
7198 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7199 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7201 /* In the second pass, the container types are adjusted if necessary and
7202 finished up, then the corresponding fields of the variant part are
7203 built with their qualifier, unless this is an unchecked union. */
7204 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7206 tree gnu_variant_type = gnu_variant->type;
7207 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7209 /* If this is an Unchecked_Union whose fields are all in the variant
7210 part and we have a single field with no representation clause or
7211 placed at offset zero, use the field directly to match the layout
7212 of C unions. */
7213 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7214 && gnu_field_list
7215 && !DECL_CHAIN (gnu_field_list)
7216 && (!DECL_FIELD_OFFSET (gnu_field_list)
7217 || integer_zerop (bit_position (gnu_field_list))))
7219 gnu_field = gnu_field_list;
7220 DECL_CONTEXT (gnu_field) = gnu_record_type;
7222 else
7224 /* Finalize the variant type now. We used to throw away empty
7225 record types but we no longer do that because we need them to
7226 generate complete debug info for the variant; otherwise, the
7227 union type definition will be lacking the fields associated
7228 with these empty variants. */
7229 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7231 /* The variant part will be at offset 0 so we need to ensure
7232 that the fields are laid out starting from the first free
7233 position at this level. */
7234 tree gnu_rep_type = make_node (RECORD_TYPE);
7235 tree gnu_rep_part;
7236 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7237 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7238 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7239 gnu_rep_part
7240 = create_rep_part (gnu_rep_type, gnu_variant_type,
7241 this_first_free_pos);
7242 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7243 gnu_field_list = gnu_rep_part;
7244 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7245 false);
7248 if (debug_info)
7249 rest_of_record_type_compilation (gnu_variant_type);
7250 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7251 true, debug_info, gnat_component_list);
7253 gnu_field
7254 = create_field_decl (gnu_variant->name, gnu_variant_type,
7255 gnu_union_type,
7256 all_rep_and_size
7257 ? TYPE_SIZE (gnu_variant_type) : 0,
7258 variants_have_rep ? bitsize_zero_node : 0,
7259 gnu_variant->packed, 0);
7261 DECL_INTERNAL_P (gnu_field) = 1;
7263 if (!unchecked_union)
7264 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7267 DECL_CHAIN (gnu_field) = gnu_variant_list;
7268 gnu_variant_list = gnu_field;
7271 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7272 if (gnu_variant_list)
7274 int union_field_packed;
7276 if (all_rep_and_size)
7278 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7279 TYPE_SIZE_UNIT (gnu_union_type)
7280 = TYPE_SIZE_UNIT (gnu_record_type);
7283 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7284 all_rep_and_size ? 1 : 0, debug_info);
7286 /* If GNU_UNION_TYPE is our record type, it means we must have an
7287 Unchecked_Union with no fields. Verify that and, if so, just
7288 return. */
7289 if (gnu_union_type == gnu_record_type)
7291 gcc_assert (unchecked_union
7292 && !gnu_field_list
7293 && !gnu_rep_list);
7294 return variants_have_rep;
7297 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7298 debug_info, gnat_component_list);
7300 /* Deal with packedness like in gnat_to_gnu_field. */
7301 if (union_field_needs_strict_alignment)
7302 union_field_packed = 0;
7303 else
7304 union_field_packed
7305 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7307 gnu_variant_part
7308 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7309 all_rep_and_size
7310 ? TYPE_SIZE (gnu_union_type) : 0,
7311 variants_have_rep ? bitsize_zero_node : 0,
7312 union_field_packed, 0);
7314 DECL_INTERNAL_P (gnu_variant_part) = 1;
7318 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7319 permitted to reorder components, self-referential sizes or variable sizes.
7320 If they do, pull them out and put them onto the appropriate list. We have
7321 to do this in a separate pass since we want to handle the discriminants
7322 but can't play with them until we've used them in debugging data above.
7324 Similarly, pull out the fields with zero size and no rep clause, as they
7325 would otherwise modify the layout and thus very likely run afoul of the
7326 Ada semantics, which are different from those of C here.
7328 ??? If we reorder them, debugging information will be wrong but there is
7329 nothing that can be done about this at the moment. */
7330 gnu_last = NULL_TREE;
7332 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7333 do { \
7334 if (gnu_last) \
7335 DECL_CHAIN (gnu_last) = gnu_next; \
7336 else \
7337 gnu_field_list = gnu_next; \
7339 DECL_CHAIN (gnu_field) = (LIST); \
7340 (LIST) = gnu_field; \
7341 } while (0)
7343 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7345 gnu_next = DECL_CHAIN (gnu_field);
7347 if (DECL_FIELD_OFFSET (gnu_field))
7349 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7350 continue;
7353 if ((reorder || has_aliased_after_self_field)
7354 && field_has_self_size (gnu_field))
7356 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7357 continue;
7360 if (reorder && field_has_variable_size (gnu_field))
7362 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7363 continue;
7366 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7368 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7369 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7370 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7371 if (field_is_aliased (gnu_field))
7372 TYPE_ALIGN (gnu_record_type)
7373 = MAX (TYPE_ALIGN (gnu_record_type),
7374 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7375 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7376 continue;
7379 gnu_last = gnu_field;
7382 #undef MOVE_FROM_FIELD_LIST_TO
7384 gnu_field_list = nreverse (gnu_field_list);
7386 /* If permitted, we reorder the fields as follows:
7388 1) all fixed length fields,
7389 2) all fields whose length doesn't depend on discriminants,
7390 3) all fields whose length depends on discriminants,
7391 4) the variant part,
7393 within the record and within each variant recursively. */
7394 if (reorder)
7395 gnu_field_list
7396 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7398 /* Otherwise, if there is an aliased field placed after a field whose length
7399 depends on discriminants, we put all the fields of the latter sort, last.
7400 We need to do this in case an object of this record type is mutable. */
7401 else if (has_aliased_after_self_field)
7402 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7404 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7405 in our REP list to the previous level because this level needs them in
7406 order to do a correct layout, i.e. avoid having overlapping fields. */
7407 if (p_gnu_rep_list && gnu_rep_list)
7408 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7410 /* Otherwise, sort the fields by bit position and put them into their own
7411 record, before the others, if we also have fields without rep clause. */
7412 else if (gnu_rep_list)
7414 tree gnu_rep_type, gnu_rep_part;
7415 int i, len = list_length (gnu_rep_list);
7416 tree *gnu_arr = XALLOCAVEC (tree, len);
7418 /* If all the fields have a rep clause, we can do a flat layout. */
7419 layout_with_rep = !gnu_field_list
7420 && (!gnu_variant_part || variants_have_rep);
7421 gnu_rep_type
7422 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7424 for (gnu_field = gnu_rep_list, i = 0;
7425 gnu_field;
7426 gnu_field = DECL_CHAIN (gnu_field), i++)
7427 gnu_arr[i] = gnu_field;
7429 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7431 /* Put the fields in the list in order of increasing position, which
7432 means we start from the end. */
7433 gnu_rep_list = NULL_TREE;
7434 for (i = len - 1; i >= 0; i--)
7436 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7437 gnu_rep_list = gnu_arr[i];
7438 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7441 if (layout_with_rep)
7442 gnu_field_list = gnu_rep_list;
7443 else
7445 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7446 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7447 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7449 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7450 without rep clause are laid out starting from this position.
7451 Therefore, we force it as a minimal size on the REP part. */
7452 gnu_rep_part
7453 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7455 /* Chain the REP part at the beginning of the field list. */
7456 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7457 gnu_field_list = gnu_rep_part;
7461 /* Chain the variant part at the end of the field list. */
7462 if (gnu_variant_part)
7463 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7465 if (cancel_alignment)
7466 TYPE_ALIGN (gnu_record_type) = 0;
7468 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7470 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7471 debug_info && !maybe_unused);
7473 /* Chain the fields with zero size at the beginning of the field list. */
7474 if (gnu_zero_list)
7475 TYPE_FIELDS (gnu_record_type)
7476 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7478 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7481 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7482 placed into an Esize, Component_Bit_Offset, or Component_Size value
7483 in the GNAT tree. */
7485 static Uint
7486 annotate_value (tree gnu_size)
7488 TCode tcode;
7489 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7490 struct tree_int_map in;
7491 int i;
7493 /* See if we've already saved the value for this node. */
7494 if (EXPR_P (gnu_size))
7496 struct tree_int_map *e;
7498 in.base.from = gnu_size;
7499 e = annotate_value_cache->find (&in);
7501 if (e)
7502 return (Node_Ref_Or_Val) e->to;
7504 else
7505 in.base.from = NULL_TREE;
7507 /* If we do not return inside this switch, TCODE will be set to the
7508 code to use for a Create_Node operand and LEN (set above) will be
7509 the number of recursive calls for us to make. */
7511 switch (TREE_CODE (gnu_size))
7513 case INTEGER_CST:
7514 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7516 case COMPONENT_REF:
7517 /* The only case we handle here is a simple discriminant reference. */
7518 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7520 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7522 /* Climb up the chain of successive extensions, if any. */
7523 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7524 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7525 == parent_name_id)
7526 gnu_size = TREE_OPERAND (gnu_size, 0);
7528 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7529 return
7530 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7533 return No_Uint;
7535 CASE_CONVERT: case NON_LVALUE_EXPR:
7536 return annotate_value (TREE_OPERAND (gnu_size, 0));
7538 /* Now just list the operations we handle. */
7539 case COND_EXPR: tcode = Cond_Expr; break;
7540 case PLUS_EXPR: tcode = Plus_Expr; break;
7541 case MINUS_EXPR: tcode = Minus_Expr; break;
7542 case MULT_EXPR: tcode = Mult_Expr; break;
7543 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7544 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7545 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7546 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7547 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7548 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7549 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7550 case NEGATE_EXPR: tcode = Negate_Expr; break;
7551 case MIN_EXPR: tcode = Min_Expr; break;
7552 case MAX_EXPR: tcode = Max_Expr; break;
7553 case ABS_EXPR: tcode = Abs_Expr; break;
7554 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7555 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7556 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7557 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7558 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7559 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7560 case LT_EXPR: tcode = Lt_Expr; break;
7561 case LE_EXPR: tcode = Le_Expr; break;
7562 case GT_EXPR: tcode = Gt_Expr; break;
7563 case GE_EXPR: tcode = Ge_Expr; break;
7564 case EQ_EXPR: tcode = Eq_Expr; break;
7565 case NE_EXPR: tcode = Ne_Expr; break;
7567 case BIT_AND_EXPR:
7568 tcode = Bit_And_Expr;
7569 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7570 Such values appear in expressions with aligning patterns. Note that,
7571 since sizetype is unsigned, we have to jump through some hoops. */
7572 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7574 tree op1 = TREE_OPERAND (gnu_size, 1);
7575 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7576 if (wi::neg_p (signed_op1))
7578 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7579 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7582 break;
7584 case CALL_EXPR:
7585 /* In regular mode, inline back only if symbolic annotation is requested
7586 in order to avoid memory explosion on big discriminated record types.
7587 But not in ASIS mode, as symbolic annotation is required for DDA. */
7588 if (List_Representation_Info == 3 || type_annotate_only)
7590 tree t = maybe_inline_call_in_expr (gnu_size);
7591 if (t)
7592 return annotate_value (t);
7594 else
7595 return Uint_Minus_1;
7597 /* Fall through... */
7599 default:
7600 return No_Uint;
7603 /* Now get each of the operands that's relevant for this code. If any
7604 cannot be expressed as a repinfo node, say we can't. */
7605 for (i = 0; i < 3; i++)
7606 ops[i] = No_Uint;
7608 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7610 if (i == 1 && pre_op1 != No_Uint)
7611 ops[i] = pre_op1;
7612 else
7613 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7614 if (ops[i] == No_Uint)
7615 return No_Uint;
7618 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7620 /* Save the result in the cache. */
7621 if (in.base.from)
7623 struct tree_int_map **h;
7624 /* We can't assume the hash table data hasn't moved since the initial
7625 look up, so we have to search again. Allocating and inserting an
7626 entry at that point would be an alternative, but then we'd better
7627 discard the entry if we decided not to cache it. */
7628 h = annotate_value_cache->find_slot (&in, INSERT);
7629 gcc_assert (!*h);
7630 *h = ggc_alloc<tree_int_map> ();
7631 (*h)->base.from = gnu_size;
7632 (*h)->to = ret;
7635 return ret;
7638 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7639 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7640 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7641 BY_REF is true if the object is used by reference. */
7643 void
7644 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7646 if (by_ref)
7648 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7649 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7650 else
7651 gnu_type = TREE_TYPE (gnu_type);
7654 if (Unknown_Esize (gnat_entity))
7656 if (TREE_CODE (gnu_type) == RECORD_TYPE
7657 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7658 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7659 else if (!size)
7660 size = TYPE_SIZE (gnu_type);
7662 if (size)
7663 Set_Esize (gnat_entity, annotate_value (size));
7666 if (Unknown_Alignment (gnat_entity))
7667 Set_Alignment (gnat_entity,
7668 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7671 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7672 Return NULL_TREE if there is no such element in the list. */
7674 static tree
7675 purpose_member_field (const_tree elem, tree list)
7677 while (list)
7679 tree field = TREE_PURPOSE (list);
7680 if (SAME_FIELD_P (field, elem))
7681 return list;
7682 list = TREE_CHAIN (list);
7684 return NULL_TREE;
7687 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7688 set Component_Bit_Offset and Esize of the components to the position and
7689 size used by Gigi. */
7691 static void
7692 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7694 Entity_Id gnat_field;
7695 tree gnu_list;
7697 /* We operate by first making a list of all fields and their position (we
7698 can get the size easily) and then update all the sizes in the tree. */
7699 gnu_list
7700 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7701 BIGGEST_ALIGNMENT, NULL_TREE);
7703 for (gnat_field = First_Entity (gnat_entity);
7704 Present (gnat_field);
7705 gnat_field = Next_Entity (gnat_field))
7706 if (Ekind (gnat_field) == E_Component
7707 || (Ekind (gnat_field) == E_Discriminant
7708 && !Is_Unchecked_Union (Scope (gnat_field))))
7710 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7711 gnu_list);
7712 if (t)
7714 tree parent_offset;
7716 /* If we are just annotating types and the type is tagged, the tag
7717 and the parent components are not generated by the front-end so
7718 we need to add the appropriate offset to each component without
7719 representation clause. */
7720 if (type_annotate_only
7721 && Is_Tagged_Type (gnat_entity)
7722 && No (Component_Clause (gnat_field)))
7724 /* For a component appearing in the current extension, the
7725 offset is the size of the parent. */
7726 if (Is_Derived_Type (gnat_entity)
7727 && Original_Record_Component (gnat_field) == gnat_field)
7728 parent_offset
7729 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7730 bitsizetype);
7731 else
7732 parent_offset = bitsize_int (POINTER_SIZE);
7734 if (TYPE_FIELDS (gnu_type))
7735 parent_offset
7736 = round_up (parent_offset,
7737 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7739 else
7740 parent_offset = bitsize_zero_node;
7742 Set_Component_Bit_Offset
7743 (gnat_field,
7744 annotate_value
7745 (size_binop (PLUS_EXPR,
7746 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7747 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7748 parent_offset)));
7750 Set_Esize (gnat_field,
7751 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7753 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7755 /* If there is no entry, this is an inherited component whose
7756 position is the same as in the parent type. */
7757 Set_Component_Bit_Offset
7758 (gnat_field,
7759 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7761 Set_Esize (gnat_field,
7762 Esize (Original_Record_Component (gnat_field)));
7767 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7768 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7769 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7770 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7771 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7772 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7773 pre-existing list to be chained to the newly created entries. */
7775 static tree
7776 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7777 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7779 tree gnu_field;
7781 for (gnu_field = TYPE_FIELDS (gnu_type);
7782 gnu_field;
7783 gnu_field = DECL_CHAIN (gnu_field))
7785 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7786 DECL_FIELD_BIT_OFFSET (gnu_field));
7787 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7788 DECL_FIELD_OFFSET (gnu_field));
7789 unsigned int our_offset_align
7790 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7791 tree v = make_tree_vec (3);
7793 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7794 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7795 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7796 gnu_list = tree_cons (gnu_field, v, gnu_list);
7798 /* Recurse on internal fields, flattening the nested fields except for
7799 those in the variant part, if requested. */
7800 if (DECL_INTERNAL_P (gnu_field))
7802 tree gnu_field_type = TREE_TYPE (gnu_field);
7803 if (do_not_flatten_variant
7804 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7805 gnu_list
7806 = build_position_list (gnu_field_type, do_not_flatten_variant,
7807 size_zero_node, bitsize_zero_node,
7808 BIGGEST_ALIGNMENT, gnu_list);
7809 else
7810 gnu_list
7811 = build_position_list (gnu_field_type, do_not_flatten_variant,
7812 gnu_our_offset, gnu_our_bitpos,
7813 our_offset_align, gnu_list);
7817 return gnu_list;
7820 /* Return a list describing the substitutions needed to reflect the
7821 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7822 be in any order. The values in an element of the list are in the form
7823 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7824 a definition of GNAT_SUBTYPE. */
7826 static vec<subst_pair>
7827 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7829 vec<subst_pair> gnu_list = vNULL;
7830 Entity_Id gnat_discrim;
7831 Node_Id gnat_constr;
7833 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7834 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7835 Present (gnat_discrim);
7836 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7837 gnat_constr = Next_Elmt (gnat_constr))
7838 /* Ignore access discriminants. */
7839 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7841 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7842 tree replacement = convert (TREE_TYPE (gnu_field),
7843 elaborate_expression
7844 (Node (gnat_constr), gnat_subtype,
7845 get_entity_name (gnat_discrim),
7846 definition, true, false));
7847 subst_pair s = {gnu_field, replacement};
7848 gnu_list.safe_push (s);
7851 return gnu_list;
7854 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7855 variants of QUAL_UNION_TYPE that are still relevant after applying
7856 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7857 list to be prepended to the newly created entries. */
7859 static vec<variant_desc>
7860 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7861 vec<variant_desc> gnu_list)
7863 tree gnu_field;
7865 for (gnu_field = TYPE_FIELDS (qual_union_type);
7866 gnu_field;
7867 gnu_field = DECL_CHAIN (gnu_field))
7869 tree qual = DECL_QUALIFIER (gnu_field);
7870 unsigned int i;
7871 subst_pair *s;
7873 FOR_EACH_VEC_ELT (subst_list, i, s)
7874 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7876 /* If the new qualifier is not unconditionally false, its variant may
7877 still be accessed. */
7878 if (!integer_zerop (qual))
7880 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7881 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7883 gnu_list.safe_push (v);
7885 /* Recurse on the variant subpart of the variant, if any. */
7886 variant_subpart = get_variant_part (variant_type);
7887 if (variant_subpart)
7888 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7889 subst_list, gnu_list);
7891 /* If the new qualifier is unconditionally true, the subsequent
7892 variants cannot be accessed. */
7893 if (integer_onep (qual))
7894 break;
7898 return gnu_list;
7901 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7902 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7903 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7904 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7905 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7906 true if we are being called to process the Component_Size of GNAT_OBJECT;
7907 this is used only for error messages. ZERO_OK is true if a size of zero
7908 is permitted; if ZERO_OK is false, it means that a size of zero should be
7909 treated as an unspecified size. */
7911 static tree
7912 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7913 enum tree_code kind, bool component_p, bool zero_ok)
7915 Node_Id gnat_error_node;
7916 tree type_size, size;
7918 /* Return 0 if no size was specified. */
7919 if (uint_size == No_Uint)
7920 return NULL_TREE;
7922 /* Ignore a negative size since that corresponds to our back-annotation. */
7923 if (UI_Lt (uint_size, Uint_0))
7924 return NULL_TREE;
7926 /* Find the node to use for error messages. */
7927 if ((Ekind (gnat_object) == E_Component
7928 || Ekind (gnat_object) == E_Discriminant)
7929 && Present (Component_Clause (gnat_object)))
7930 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7931 else if (Present (Size_Clause (gnat_object)))
7932 gnat_error_node = Expression (Size_Clause (gnat_object));
7933 else
7934 gnat_error_node = gnat_object;
7936 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7937 but cannot be represented in bitsizetype. */
7938 size = UI_To_gnu (uint_size, bitsizetype);
7939 if (TREE_OVERFLOW (size))
7941 if (component_p)
7942 post_error_ne ("component size for& is too large", gnat_error_node,
7943 gnat_object);
7944 else
7945 post_error_ne ("size for& is too large", gnat_error_node,
7946 gnat_object);
7947 return NULL_TREE;
7950 /* Ignore a zero size if it is not permitted. */
7951 if (!zero_ok && integer_zerop (size))
7952 return NULL_TREE;
7954 /* The size of objects is always a multiple of a byte. */
7955 if (kind == VAR_DECL
7956 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7958 if (component_p)
7959 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7960 gnat_error_node, gnat_object);
7961 else
7962 post_error_ne ("size for& is not a multiple of Storage_Unit",
7963 gnat_error_node, gnat_object);
7964 return NULL_TREE;
7967 /* If this is an integral type or a packed array type, the front-end has
7968 already verified the size, so we need not do it here (which would mean
7969 checking against the bounds). However, if this is an aliased object,
7970 it may not be smaller than the type of the object. */
7971 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7972 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7973 return size;
7975 /* If the object is a record that contains a template, add the size of the
7976 template to the specified size. */
7977 if (TREE_CODE (gnu_type) == RECORD_TYPE
7978 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7979 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7981 if (kind == VAR_DECL
7982 /* If a type needs strict alignment, a component of this type in
7983 a packed record cannot be packed and thus uses the type size. */
7984 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7985 type_size = TYPE_SIZE (gnu_type);
7986 else
7987 type_size = rm_size (gnu_type);
7989 /* Modify the size of a discriminated type to be the maximum size. */
7990 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7991 type_size = max_size (type_size, true);
7993 /* If this is an access type or a fat pointer, the minimum size is that given
7994 by the smallest integral mode that's valid for pointers. */
7995 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7997 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7998 while (!targetm.valid_pointer_mode (p_mode))
7999 p_mode = GET_MODE_WIDER_MODE (p_mode);
8000 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8003 /* Issue an error either if the default size of the object isn't a constant
8004 or if the new size is smaller than it. */
8005 if (TREE_CODE (type_size) != INTEGER_CST
8006 || TREE_OVERFLOW (type_size)
8007 || tree_int_cst_lt (size, type_size))
8009 if (component_p)
8010 post_error_ne_tree
8011 ("component size for& too small{, minimum allowed is ^}",
8012 gnat_error_node, gnat_object, type_size);
8013 else
8014 post_error_ne_tree
8015 ("size for& too small{, minimum allowed is ^}",
8016 gnat_error_node, gnat_object, type_size);
8017 return NULL_TREE;
8020 return size;
8023 /* Similarly, but both validate and process a value of RM size. This routine
8024 is only called for types. */
8026 static void
8027 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8029 Node_Id gnat_attr_node;
8030 tree old_size, size;
8032 /* Do nothing if no size was specified. */
8033 if (uint_size == No_Uint)
8034 return;
8036 /* Ignore a negative size since that corresponds to our back-annotation. */
8037 if (UI_Lt (uint_size, Uint_0))
8038 return;
8040 /* Only issue an error if a Value_Size clause was explicitly given.
8041 Otherwise, we'd be duplicating an error on the Size clause. */
8042 gnat_attr_node
8043 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8045 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8046 but cannot be represented in bitsizetype. */
8047 size = UI_To_gnu (uint_size, bitsizetype);
8048 if (TREE_OVERFLOW (size))
8050 if (Present (gnat_attr_node))
8051 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8052 gnat_entity);
8053 return;
8056 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8057 exists, or this is an integer type, in which case the front-end will
8058 have always set it. */
8059 if (No (gnat_attr_node)
8060 && integer_zerop (size)
8061 && !Has_Size_Clause (gnat_entity)
8062 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8063 return;
8065 old_size = rm_size (gnu_type);
8067 /* If the old size is self-referential, get the maximum size. */
8068 if (CONTAINS_PLACEHOLDER_P (old_size))
8069 old_size = max_size (old_size, true);
8071 /* Issue an error either if the old size of the object isn't a constant or
8072 if the new size is smaller than it. The front-end has already verified
8073 this for scalar and packed array types. */
8074 if (TREE_CODE (old_size) != INTEGER_CST
8075 || TREE_OVERFLOW (old_size)
8076 || (AGGREGATE_TYPE_P (gnu_type)
8077 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8078 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8079 && !(TYPE_IS_PADDING_P (gnu_type)
8080 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8081 && TYPE_PACKED_ARRAY_TYPE_P
8082 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8083 && tree_int_cst_lt (size, old_size)))
8085 if (Present (gnat_attr_node))
8086 post_error_ne_tree
8087 ("Value_Size for& too small{, minimum allowed is ^}",
8088 gnat_attr_node, gnat_entity, old_size);
8089 return;
8092 /* Otherwise, set the RM size proper for integral types... */
8093 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8094 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8095 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8096 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8097 SET_TYPE_RM_SIZE (gnu_type, size);
8099 /* ...or the Ada size for record and union types. */
8100 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8101 && !TYPE_FAT_POINTER_P (gnu_type))
8102 SET_TYPE_ADA_SIZE (gnu_type, size);
8105 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8106 a type or object whose present alignment is ALIGN. If this alignment is
8107 valid, return it. Otherwise, give an error and return ALIGN. */
8109 static unsigned int
8110 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8112 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8113 unsigned int new_align;
8114 Node_Id gnat_error_node;
8116 /* Don't worry about checking alignment if alignment was not specified
8117 by the source program and we already posted an error for this entity. */
8118 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8119 return align;
8121 /* Post the error on the alignment clause if any. Note, for the implicit
8122 base type of an array type, the alignment clause is on the first
8123 subtype. */
8124 if (Present (Alignment_Clause (gnat_entity)))
8125 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8127 else if (Is_Itype (gnat_entity)
8128 && Is_Array_Type (gnat_entity)
8129 && Etype (gnat_entity) == gnat_entity
8130 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8131 gnat_error_node =
8132 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8134 else
8135 gnat_error_node = gnat_entity;
8137 /* Within GCC, an alignment is an integer, so we must make sure a value is
8138 specified that fits in that range. Also, there is an upper bound to
8139 alignments we can support/allow. */
8140 if (!UI_Is_In_Int_Range (alignment)
8141 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8142 post_error_ne_num ("largest supported alignment for& is ^",
8143 gnat_error_node, gnat_entity, max_allowed_alignment);
8144 else if (!(Present (Alignment_Clause (gnat_entity))
8145 && From_At_Mod (Alignment_Clause (gnat_entity)))
8146 && new_align * BITS_PER_UNIT < align)
8148 unsigned int double_align;
8149 bool is_capped_double, align_clause;
8151 /* If the default alignment of "double" or larger scalar types is
8152 specifically capped and the new alignment is above the cap, do
8153 not post an error and change the alignment only if there is an
8154 alignment clause; this makes it possible to have the associated
8155 GCC type overaligned by default for performance reasons. */
8156 if ((double_align = double_float_alignment) > 0)
8158 Entity_Id gnat_type
8159 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8160 is_capped_double
8161 = is_double_float_or_array (gnat_type, &align_clause);
8163 else if ((double_align = double_scalar_alignment) > 0)
8165 Entity_Id gnat_type
8166 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8167 is_capped_double
8168 = is_double_scalar_or_array (gnat_type, &align_clause);
8170 else
8171 is_capped_double = align_clause = false;
8173 if (is_capped_double && new_align >= double_align)
8175 if (align_clause)
8176 align = new_align * BITS_PER_UNIT;
8178 else
8180 if (is_capped_double)
8181 align = double_align * BITS_PER_UNIT;
8183 post_error_ne_num ("alignment for& must be at least ^",
8184 gnat_error_node, gnat_entity,
8185 align / BITS_PER_UNIT);
8188 else
8190 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8191 if (new_align > align)
8192 align = new_align;
8195 return align;
8198 /* Verify that TYPE is something we can implement atomically. If not, issue
8199 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8200 process a component type. */
8202 static void
8203 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8205 Node_Id gnat_error_point = gnat_entity;
8206 Node_Id gnat_node;
8207 machine_mode mode;
8208 enum mode_class mclass;
8209 unsigned int align;
8210 tree size;
8212 /* If this is an anonymous base type, nothing to check, the error will be
8213 reported on the source type if need be. */
8214 if (!Comes_From_Source (gnat_entity))
8215 return;
8217 mode = TYPE_MODE (type);
8218 mclass = GET_MODE_CLASS (mode);
8219 align = TYPE_ALIGN (type);
8220 size = TYPE_SIZE (type);
8222 /* Consider all aligned floating-point types atomic and any aligned types
8223 that are represented by integers no wider than a machine word. */
8224 if ((mclass == MODE_FLOAT
8225 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8226 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8227 && align >= GET_MODE_ALIGNMENT (mode))
8228 return;
8230 /* For the moment, also allow anything that has an alignment equal to its
8231 size and which is smaller than a word. */
8232 if (size
8233 && TREE_CODE (size) == INTEGER_CST
8234 && compare_tree_int (size, align) == 0
8235 && align <= BITS_PER_WORD)
8236 return;
8238 for (gnat_node = First_Rep_Item (gnat_entity);
8239 Present (gnat_node);
8240 gnat_node = Next_Rep_Item (gnat_node))
8241 if (Nkind (gnat_node) == N_Pragma)
8243 unsigned char pragma_id
8244 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8246 if ((pragma_id == Pragma_Atomic && !component_p)
8247 || (pragma_id == Pragma_Atomic_Components && component_p))
8249 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8250 break;
8254 if (component_p)
8255 post_error_ne ("atomic access to component of & cannot be guaranteed",
8256 gnat_error_point, gnat_entity);
8257 else
8258 post_error_ne ("atomic access to & cannot be guaranteed",
8259 gnat_error_point, gnat_entity);
8263 /* Helper for the intrin compatibility checks family. Evaluate whether
8264 two types are definitely incompatible. */
8266 static bool
8267 intrin_types_incompatible_p (tree t1, tree t2)
8269 enum tree_code code;
8271 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8272 return false;
8274 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8275 return true;
8277 if (TREE_CODE (t1) != TREE_CODE (t2))
8278 return true;
8280 code = TREE_CODE (t1);
8282 switch (code)
8284 case INTEGER_TYPE:
8285 case REAL_TYPE:
8286 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8288 case POINTER_TYPE:
8289 case REFERENCE_TYPE:
8290 /* Assume designated types are ok. We'd need to account for char * and
8291 void * variants to do better, which could rapidly get messy and isn't
8292 clearly worth the effort. */
8293 return false;
8295 default:
8296 break;
8299 return false;
8302 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8303 on the Ada/builtin argument lists for the INB binding. */
8305 static bool
8306 intrin_arglists_compatible_p (intrin_binding_t * inb)
8308 function_args_iterator ada_iter, btin_iter;
8310 function_args_iter_init (&ada_iter, inb->ada_fntype);
8311 function_args_iter_init (&btin_iter, inb->btin_fntype);
8313 /* Sequence position of the last argument we checked. */
8314 int argpos = 0;
8316 while (1)
8318 tree ada_type = function_args_iter_cond (&ada_iter);
8319 tree btin_type = function_args_iter_cond (&btin_iter);
8321 /* If we've exhausted both lists simultaneously, we're done. */
8322 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8323 break;
8325 /* If one list is shorter than the other, they fail to match. */
8326 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8327 return false;
8329 /* If we're done with the Ada args and not with the internal builtin
8330 args, or the other way around, complain. */
8331 if (ada_type == void_type_node
8332 && btin_type != void_type_node)
8334 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8335 return false;
8338 if (btin_type == void_type_node
8339 && ada_type != void_type_node)
8341 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8342 inb->gnat_entity, inb->gnat_entity, argpos);
8343 return false;
8346 /* Otherwise, check that types match for the current argument. */
8347 argpos ++;
8348 if (intrin_types_incompatible_p (ada_type, btin_type))
8350 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8351 inb->gnat_entity, inb->gnat_entity, argpos);
8352 return false;
8356 function_args_iter_next (&ada_iter);
8357 function_args_iter_next (&btin_iter);
8360 return true;
8363 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8364 on the Ada/builtin return values for the INB binding. */
8366 static bool
8367 intrin_return_compatible_p (intrin_binding_t * inb)
8369 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8370 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8372 /* Accept function imported as procedure, common and convenient. */
8373 if (VOID_TYPE_P (ada_return_type)
8374 && !VOID_TYPE_P (btin_return_type))
8375 return true;
8377 /* If return type is Address (integer type), map it to void *. */
8378 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8379 ada_return_type = ptr_void_type_node;
8381 /* Check return types compatibility otherwise. Note that this
8382 handles void/void as well. */
8383 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8385 post_error ("?intrinsic binding type mismatch on return value!",
8386 inb->gnat_entity);
8387 return false;
8390 return true;
8393 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8394 compatible. Issue relevant warnings when they are not.
8396 This is intended as a light check to diagnose the most obvious cases, not
8397 as a full fledged type compatibility predicate. It is the programmer's
8398 responsibility to ensure correctness of the Ada declarations in Imports,
8399 especially when binding straight to a compiler internal. */
8401 static bool
8402 intrin_profiles_compatible_p (intrin_binding_t * inb)
8404 /* Check compatibility on return values and argument lists, each responsible
8405 for posting warnings as appropriate. Ensure use of the proper sloc for
8406 this purpose. */
8408 bool arglists_compatible_p, return_compatible_p;
8409 location_t saved_location = input_location;
8411 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8413 return_compatible_p = intrin_return_compatible_p (inb);
8414 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8416 input_location = saved_location;
8418 return return_compatible_p && arglists_compatible_p;
8421 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8422 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8423 specified size for this field. POS_LIST is a position list describing
8424 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8425 to this layout. */
8427 static tree
8428 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8429 tree size, tree pos_list,
8430 vec<subst_pair> subst_list)
8432 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8433 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8434 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8435 tree new_pos, new_field;
8436 unsigned int i;
8437 subst_pair *s;
8439 if (CONTAINS_PLACEHOLDER_P (pos))
8440 FOR_EACH_VEC_ELT (subst_list, i, s)
8441 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8443 /* If the position is now a constant, we can set it as the position of the
8444 field when we make it. Otherwise, we need to deal with it specially. */
8445 if (TREE_CONSTANT (pos))
8446 new_pos = bit_from_pos (pos, bitpos);
8447 else
8448 new_pos = NULL_TREE;
8450 new_field
8451 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8452 size, new_pos, DECL_PACKED (old_field),
8453 !DECL_NONADDRESSABLE_P (old_field));
8455 if (!new_pos)
8457 normalize_offset (&pos, &bitpos, offset_align);
8458 /* Finalize the position. */
8459 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8460 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8461 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8462 DECL_SIZE (new_field) = size;
8463 DECL_SIZE_UNIT (new_field)
8464 = convert (sizetype,
8465 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8466 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8469 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8470 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8471 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8472 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8474 return new_field;
8477 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8478 it is the minimal size the REP_PART must have. */
8480 static tree
8481 create_rep_part (tree rep_type, tree record_type, tree min_size)
8483 tree field;
8485 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8486 min_size = NULL_TREE;
8488 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8489 min_size, NULL_TREE, 0, 1);
8490 DECL_INTERNAL_P (field) = 1;
8492 return field;
8495 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8497 static tree
8498 get_rep_part (tree record_type)
8500 tree field = TYPE_FIELDS (record_type);
8502 /* The REP part is the first field, internal, another record, and its name
8503 starts with an 'R'. */
8504 if (field
8505 && DECL_INTERNAL_P (field)
8506 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8507 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8508 return field;
8510 return NULL_TREE;
8513 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8515 tree
8516 get_variant_part (tree record_type)
8518 tree field;
8520 /* The variant part is the only internal field that is a qualified union. */
8521 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8522 if (DECL_INTERNAL_P (field)
8523 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8524 return field;
8526 return NULL_TREE;
8529 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8530 the list of variants to be used and RECORD_TYPE is the type of the parent.
8531 POS_LIST is a position list describing the layout of fields present in
8532 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8533 layout. */
8535 static tree
8536 create_variant_part_from (tree old_variant_part,
8537 vec<variant_desc> variant_list,
8538 tree record_type, tree pos_list,
8539 vec<subst_pair> subst_list)
8541 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8542 tree old_union_type = TREE_TYPE (old_variant_part);
8543 tree new_union_type, new_variant_part;
8544 tree union_field_list = NULL_TREE;
8545 variant_desc *v;
8546 unsigned int i;
8548 /* First create the type of the variant part from that of the old one. */
8549 new_union_type = make_node (QUAL_UNION_TYPE);
8550 TYPE_NAME (new_union_type)
8551 = concat_name (TYPE_NAME (record_type),
8552 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8554 /* If the position of the variant part is constant, subtract it from the
8555 size of the type of the parent to get the new size. This manual CSE
8556 reduces the code size when not optimizing. */
8557 if (TREE_CODE (offset) == INTEGER_CST)
8559 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8560 tree first_bit = bit_from_pos (offset, bitpos);
8561 TYPE_SIZE (new_union_type)
8562 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8563 TYPE_SIZE_UNIT (new_union_type)
8564 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8565 byte_from_pos (offset, bitpos));
8566 SET_TYPE_ADA_SIZE (new_union_type,
8567 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8568 first_bit));
8569 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8570 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8572 else
8573 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8575 /* Now finish up the new variants and populate the union type. */
8576 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8578 tree old_field = v->field, new_field;
8579 tree old_variant, old_variant_subpart, new_variant, field_list;
8581 /* Skip variants that don't belong to this nesting level. */
8582 if (DECL_CONTEXT (old_field) != old_union_type)
8583 continue;
8585 /* Retrieve the list of fields already added to the new variant. */
8586 new_variant = v->new_type;
8587 field_list = TYPE_FIELDS (new_variant);
8589 /* If the old variant had a variant subpart, we need to create a new
8590 variant subpart and add it to the field list. */
8591 old_variant = v->type;
8592 old_variant_subpart = get_variant_part (old_variant);
8593 if (old_variant_subpart)
8595 tree new_variant_subpart
8596 = create_variant_part_from (old_variant_subpart, variant_list,
8597 new_variant, pos_list, subst_list);
8598 DECL_CHAIN (new_variant_subpart) = field_list;
8599 field_list = new_variant_subpart;
8602 /* Finish up the new variant and create the field. No need for debug
8603 info thanks to the XVS type. */
8604 finish_record_type (new_variant, nreverse (field_list), 2, false);
8605 compute_record_mode (new_variant);
8606 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8607 Empty);
8609 new_field
8610 = create_field_decl_from (old_field, new_variant, new_union_type,
8611 TYPE_SIZE (new_variant),
8612 pos_list, subst_list);
8613 DECL_QUALIFIER (new_field) = v->qual;
8614 DECL_INTERNAL_P (new_field) = 1;
8615 DECL_CHAIN (new_field) = union_field_list;
8616 union_field_list = new_field;
8619 /* Finish up the union type and create the variant part. No need for debug
8620 info thanks to the XVS type. Note that we don't reverse the field list
8621 because VARIANT_LIST has been traversed in reverse order. */
8622 finish_record_type (new_union_type, union_field_list, 2, false);
8623 compute_record_mode (new_union_type);
8624 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8625 Empty);
8627 new_variant_part
8628 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8629 TYPE_SIZE (new_union_type),
8630 pos_list, subst_list);
8631 DECL_INTERNAL_P (new_variant_part) = 1;
8633 /* With multiple discriminants it is possible for an inner variant to be
8634 statically selected while outer ones are not; in this case, the list
8635 of fields of the inner variant is not flattened and we end up with a
8636 qualified union with a single member. Drop the useless container. */
8637 if (!DECL_CHAIN (union_field_list))
8639 DECL_CONTEXT (union_field_list) = record_type;
8640 DECL_FIELD_OFFSET (union_field_list)
8641 = DECL_FIELD_OFFSET (new_variant_part);
8642 DECL_FIELD_BIT_OFFSET (union_field_list)
8643 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8644 SET_DECL_OFFSET_ALIGN (union_field_list,
8645 DECL_OFFSET_ALIGN (new_variant_part));
8646 new_variant_part = union_field_list;
8649 return new_variant_part;
8652 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8653 which are both RECORD_TYPE, after applying the substitutions described
8654 in SUBST_LIST. */
8656 static void
8657 copy_and_substitute_in_size (tree new_type, tree old_type,
8658 vec<subst_pair> subst_list)
8660 unsigned int i;
8661 subst_pair *s;
8663 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8664 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8665 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8666 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8667 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8669 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8670 FOR_EACH_VEC_ELT (subst_list, i, s)
8671 TYPE_SIZE (new_type)
8672 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8673 s->discriminant, s->replacement);
8675 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8676 FOR_EACH_VEC_ELT (subst_list, i, s)
8677 TYPE_SIZE_UNIT (new_type)
8678 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8679 s->discriminant, s->replacement);
8681 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8682 FOR_EACH_VEC_ELT (subst_list, i, s)
8683 SET_TYPE_ADA_SIZE
8684 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8685 s->discriminant, s->replacement));
8687 /* Finalize the size. */
8688 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8689 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8692 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8693 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8694 The parallel type is the original array type if it has been translated. */
8696 static void
8697 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8699 Entity_Id gnat_original_array_type
8700 = Underlying_Type (Original_Array_Type (gnat_entity));
8701 tree gnu_original_array_type;
8703 if (!present_gnu_tree (gnat_original_array_type))
8704 return;
8706 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8708 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8709 return;
8711 add_parallel_type (gnu_type, gnu_original_array_type);
8714 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8715 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8716 updated by replacing F with R.
8718 The function doesn't update the layout of the type, i.e. it assumes
8719 that the substitution is purely formal. That's why the replacement
8720 value R must itself contain a PLACEHOLDER_EXPR. */
8722 tree
8723 substitute_in_type (tree t, tree f, tree r)
8725 tree nt;
8727 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8729 switch (TREE_CODE (t))
8731 case INTEGER_TYPE:
8732 case ENUMERAL_TYPE:
8733 case BOOLEAN_TYPE:
8734 case REAL_TYPE:
8736 /* First the domain types of arrays. */
8737 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8738 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8740 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8741 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8743 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8744 return t;
8746 nt = copy_type (t);
8747 TYPE_GCC_MIN_VALUE (nt) = low;
8748 TYPE_GCC_MAX_VALUE (nt) = high;
8750 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8751 SET_TYPE_INDEX_TYPE
8752 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8754 return nt;
8757 /* Then the subtypes. */
8758 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8759 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8761 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8762 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8764 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8765 return t;
8767 nt = copy_type (t);
8768 SET_TYPE_RM_MIN_VALUE (nt, low);
8769 SET_TYPE_RM_MAX_VALUE (nt, high);
8771 return nt;
8774 return t;
8776 case COMPLEX_TYPE:
8777 nt = substitute_in_type (TREE_TYPE (t), f, r);
8778 if (nt == TREE_TYPE (t))
8779 return t;
8781 return build_complex_type (nt);
8783 case FUNCTION_TYPE:
8784 /* These should never show up here. */
8785 gcc_unreachable ();
8787 case ARRAY_TYPE:
8789 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8790 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8792 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8793 return t;
8795 nt = build_nonshared_array_type (component, domain);
8796 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8797 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8798 SET_TYPE_MODE (nt, TYPE_MODE (t));
8799 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8800 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8801 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8802 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8803 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8804 return nt;
8807 case RECORD_TYPE:
8808 case UNION_TYPE:
8809 case QUAL_UNION_TYPE:
8811 bool changed_field = false;
8812 tree field;
8814 /* Start out with no fields, make new fields, and chain them
8815 in. If we haven't actually changed the type of any field,
8816 discard everything we've done and return the old type. */
8817 nt = copy_type (t);
8818 TYPE_FIELDS (nt) = NULL_TREE;
8820 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8822 tree new_field = copy_node (field), new_n;
8824 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8825 if (new_n != TREE_TYPE (field))
8827 TREE_TYPE (new_field) = new_n;
8828 changed_field = true;
8831 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8832 if (new_n != DECL_FIELD_OFFSET (field))
8834 DECL_FIELD_OFFSET (new_field) = new_n;
8835 changed_field = true;
8838 /* Do the substitution inside the qualifier, if any. */
8839 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8841 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8842 if (new_n != DECL_QUALIFIER (field))
8844 DECL_QUALIFIER (new_field) = new_n;
8845 changed_field = true;
8849 DECL_CONTEXT (new_field) = nt;
8850 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8852 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8853 TYPE_FIELDS (nt) = new_field;
8856 if (!changed_field)
8857 return t;
8859 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8860 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8861 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8862 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8863 return nt;
8866 default:
8867 return t;
8871 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8872 needed to represent the object. */
8874 tree
8875 rm_size (tree gnu_type)
8877 /* For integral types, we store the RM size explicitly. */
8878 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8879 return TYPE_RM_SIZE (gnu_type);
8881 /* Return the RM size of the actual data plus the size of the template. */
8882 if (TREE_CODE (gnu_type) == RECORD_TYPE
8883 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8884 return
8885 size_binop (PLUS_EXPR,
8886 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8887 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8889 /* For record or union types, we store the size explicitly. */
8890 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8891 && !TYPE_FAT_POINTER_P (gnu_type)
8892 && TYPE_ADA_SIZE (gnu_type))
8893 return TYPE_ADA_SIZE (gnu_type);
8895 /* For other types, this is just the size. */
8896 return TYPE_SIZE (gnu_type);
8899 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8900 fully-qualified name, possibly with type information encoding.
8901 Otherwise, return the name. */
8903 tree
8904 get_entity_name (Entity_Id gnat_entity)
8906 Get_Encoded_Name (gnat_entity);
8907 return get_identifier_with_length (Name_Buffer, Name_Len);
8910 /* Return an identifier representing the external name to be used for
8911 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8912 and the specified suffix. */
8914 tree
8915 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8917 const Entity_Kind kind = Ekind (gnat_entity);
8918 const bool has_suffix = (suffix != NULL);
8919 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8920 String_Pointer sp = {suffix, &temp};
8922 Get_External_Name (gnat_entity, has_suffix, sp);
8924 /* A variable using the Stdcall convention lives in a DLL. We adjust
8925 its name to use the jump table, the _imp__NAME contains the address
8926 for the NAME variable. */
8927 if ((kind == E_Variable || kind == E_Constant)
8928 && Has_Stdcall_Convention (gnat_entity))
8930 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8931 char *new_name = (char *) alloca (len + 1);
8932 strcpy (new_name, STDCALL_PREFIX);
8933 strcat (new_name, Name_Buffer);
8934 return get_identifier_with_length (new_name, len);
8937 return get_identifier_with_length (Name_Buffer, Name_Len);
8940 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8941 string, return a new IDENTIFIER_NODE that is the concatenation of
8942 the name followed by "___" and the specified suffix. */
8944 tree
8945 concat_name (tree gnu_name, const char *suffix)
8947 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8948 char *new_name = (char *) alloca (len + 1);
8949 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8950 strcat (new_name, "___");
8951 strcat (new_name, suffix);
8952 return get_identifier_with_length (new_name, len);
8955 /* Initialize data structures of the decl.c module. */
8957 void
8958 init_gnat_decl (void)
8960 /* Initialize the cache of annotated values. */
8961 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8964 /* Destroy data structures of the decl.c module. */
8966 void
8967 destroy_gnat_decl (void)
8969 /* Destroy the cache of annotated values. */
8970 annotate_value_cache->empty ();
8971 annotate_value_cache = NULL;
8974 #include "gt-ada-decl.h"