* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Function>: Make sure
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobf487e9b5849ab848ae978e40d323daba13e5e1de
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 "vec.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
38 #include "flags.h"
39 #include "toplev.h"
40 #include "ggc.h"
41 #include "target.h"
42 #include "tree-inline.h"
43 #include "diagnostic-core.h"
45 #include "ada.h"
46 #include "types.h"
47 #include "atree.h"
48 #include "elists.h"
49 #include "namet.h"
50 #include "nlists.h"
51 #include "repinfo.h"
52 #include "snames.h"
53 #include "stringt.h"
54 #include "uintp.h"
55 #include "fe.h"
56 #include "sinfo.h"
57 #include "einfo.h"
58 #include "ada-tree.h"
59 #include "gigi.h"
61 /* "stdcall" and "thiscall" conventions should be processed in a specific way
62 on 32-bit x86/Windows only. The macros below are helpers to avoid having
63 to check for a Windows specific attribute throughout this unit. */
65 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
66 #ifdef TARGET_64BIT
67 #define Has_Stdcall_Convention(E) \
68 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
69 #define Has_Thiscall_Convention(E) \
70 (!TARGET_64BIT && is_cplusplus_method (E))
71 #else
72 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
73 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
74 #endif
75 #else
76 #define Has_Stdcall_Convention(E) 0
77 #define Has_Thiscall_Convention(E) 0
78 #endif
80 #define STDCALL_PREFIX "_imp__"
82 /* Stack realignment is necessary for functions with foreign conventions when
83 the ABI doesn't mandate as much as what the compiler assumes - that is, up
84 to PREFERRED_STACK_BOUNDARY.
86 Such realignment can be requested with a dedicated function type attribute
87 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
88 characterize the situations where the attribute should be set. We rely on
89 compiler configuration settings for 'main' to decide. */
91 #ifdef MAIN_STACK_BOUNDARY
92 #define FOREIGN_FORCE_REALIGN_STACK \
93 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
94 #else
95 #define FOREIGN_FORCE_REALIGN_STACK 0
96 #endif
98 struct incomplete
100 struct incomplete *next;
101 tree old_type;
102 Entity_Id full_type;
105 /* These variables are used to defer recursively expanding incomplete types
106 while we are processing an array, a record or a subprogram type. */
107 static int defer_incomplete_level = 0;
108 static struct incomplete *defer_incomplete_list;
110 /* This variable is used to delay expanding From_Limited_With types until the
111 end of the spec. */
112 static struct incomplete *defer_limited_with;
114 typedef struct subst_pair_d {
115 tree discriminant;
116 tree replacement;
117 } subst_pair;
120 typedef struct variant_desc_d {
121 /* The type of the variant. */
122 tree type;
124 /* The associated field. */
125 tree field;
127 /* The value of the qualifier. */
128 tree qual;
130 /* The type of the variant after transformation. */
131 tree new_type;
132 } variant_desc;
135 /* A hash table used to cache the result of annotate_value. */
137 struct value_annotation_hasher : ggc_cache_hasher<tree_int_map *>
139 static inline hashval_t
140 hash (tree_int_map *m)
142 return htab_hash_pointer (m->base.from);
145 static inline bool
146 equal (tree_int_map *a, tree_int_map *b)
148 return a->base.from == b->base.from;
151 static void
152 handle_cache_entry (tree_int_map *&m)
154 extern void gt_ggc_mx (tree_int_map *&);
155 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
156 return;
157 else if (ggc_marked_p (m->base.from))
158 gt_ggc_mx (m);
159 else
160 m = static_cast<tree_int_map *> (HTAB_DELETED_ENTRY);
164 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
166 static void prepend_one_attribute (struct attrib **,
167 enum attr_type, tree, tree, Node_Id);
168 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
169 static void prepend_attributes (struct attrib **, Entity_Id);
170 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
171 bool);
172 static bool type_has_variable_size (tree);
173 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
174 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
175 unsigned int);
176 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
177 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
178 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
179 bool *);
180 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
181 static bool is_from_limited_with_of_main (Entity_Id);
182 static tree change_qualified_type (tree, int);
183 static bool same_discriminant_p (Entity_Id, Entity_Id);
184 static bool array_type_has_nonaliased_component (tree, Entity_Id);
185 static bool compile_time_known_address_p (Node_Id);
186 static bool cannot_be_superflat (Node_Id);
187 static bool constructor_address_p (tree);
188 static bool allocatable_size_p (tree, bool);
189 static bool initial_value_needs_conversion (tree, tree);
190 static int compare_field_bitpos (const PTR, const PTR);
191 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
192 bool, bool, bool, bool, bool, tree, tree *);
193 static Uint annotate_value (tree);
194 static void annotate_rep (Entity_Id, tree);
195 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
196 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
197 static vec<variant_desc> build_variant_list (tree,
198 vec<subst_pair> ,
199 vec<variant_desc> );
200 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
201 static void set_rm_size (Uint, tree, Entity_Id);
202 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
203 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
204 static tree create_field_decl_from (tree, tree, tree, tree, tree,
205 vec<subst_pair> );
206 static tree create_rep_part (tree, tree, tree);
207 static tree get_rep_part (tree);
208 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
209 tree, vec<subst_pair> );
210 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
211 static void add_parallel_type_for_packed_array (tree, Entity_Id);
212 static const char *get_entity_char (Entity_Id);
214 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
215 to pass around calls performing profile compatibility checks. */
217 typedef struct {
218 Entity_Id gnat_entity; /* The Ada subprogram entity. */
219 tree ada_fntype; /* The corresponding GCC type node. */
220 tree btin_fntype; /* The GCC builtin function type node. */
221 } intrin_binding_t;
223 static bool intrin_profiles_compatible_p (intrin_binding_t *);
225 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
226 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
227 and associate the ..._DECL node with the input GNAT defining identifier.
229 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
230 initial value (in GCC tree form). This is optional for a variable. For
231 a renamed entity, GNU_EXPR gives the object being renamed.
233 DEFINITION is nonzero if this call is intended for a definition. This is
234 used for separate compilation where it is necessary to know whether an
235 external declaration or a definition must be created if the GCC equivalent
236 was not created previously. The value of 1 is normally used for a nonzero
237 DEFINITION, but a value of 2 is used in special circumstances, defined in
238 the code. */
240 tree
241 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
243 /* Contains the kind of the input GNAT node. */
244 const Entity_Kind kind = Ekind (gnat_entity);
245 /* True if this is a type. */
246 const bool is_type = IN (kind, Type_Kind);
247 /* True if debug info is requested for this entity. */
248 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
249 /* True if this entity is to be considered as imported. */
250 const bool imported_p
251 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
252 /* For a type, contains the equivalent GNAT node to be used in gigi. */
253 Entity_Id gnat_equiv_type = Empty;
254 /* Temporary used to walk the GNAT tree. */
255 Entity_Id gnat_temp;
256 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
257 This node will be associated with the GNAT node by calling at the end
258 of the `switch' statement. */
259 tree gnu_decl = NULL_TREE;
260 /* Contains the GCC type to be used for the GCC node. */
261 tree gnu_type = NULL_TREE;
262 /* Contains the GCC size tree to be used for the GCC node. */
263 tree gnu_size = NULL_TREE;
264 /* Contains the GCC name to be used for the GCC node. */
265 tree gnu_entity_name;
266 /* True if we have already saved gnu_decl as a GNAT association. */
267 bool saved = false;
268 /* True if we incremented defer_incomplete_level. */
269 bool this_deferred = false;
270 /* True if we incremented force_global. */
271 bool this_global = false;
272 /* True if we should check to see if elaborated during processing. */
273 bool maybe_present = false;
274 /* True if we made GNU_DECL and its type here. */
275 bool this_made_decl = false;
276 /* Size and alignment of the GCC node, if meaningful. */
277 unsigned int esize = 0, align = 0;
278 /* Contains the list of attributes directly attached to the entity. */
279 struct attrib *attr_list = NULL;
281 /* Since a use of an Itype is a definition, process it as such if it
282 is not in a with'ed unit. */
283 if (!definition
284 && is_type
285 && Is_Itype (gnat_entity)
286 && !present_gnu_tree (gnat_entity)
287 && In_Extended_Main_Code_Unit (gnat_entity))
289 /* Ensure that we are in a subprogram mentioned in the Scope chain of
290 this entity, our current scope is global, or we encountered a task
291 or entry (where we can't currently accurately check scoping). */
292 if (!current_function_decl
293 || DECL_ELABORATION_PROC_P (current_function_decl))
295 process_type (gnat_entity);
296 return get_gnu_tree (gnat_entity);
299 for (gnat_temp = Scope (gnat_entity);
300 Present (gnat_temp);
301 gnat_temp = Scope (gnat_temp))
303 if (Is_Type (gnat_temp))
304 gnat_temp = Underlying_Type (gnat_temp);
306 if (Ekind (gnat_temp) == E_Subprogram_Body)
307 gnat_temp
308 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
310 if (IN (Ekind (gnat_temp), Subprogram_Kind)
311 && Present (Protected_Body_Subprogram (gnat_temp)))
312 gnat_temp = Protected_Body_Subprogram (gnat_temp);
314 if (Ekind (gnat_temp) == E_Entry
315 || Ekind (gnat_temp) == E_Entry_Family
316 || Ekind (gnat_temp) == E_Task_Type
317 || (IN (Ekind (gnat_temp), Subprogram_Kind)
318 && present_gnu_tree (gnat_temp)
319 && (current_function_decl
320 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
322 process_type (gnat_entity);
323 return get_gnu_tree (gnat_entity);
327 /* This abort means the Itype has an incorrect scope, i.e. that its
328 scope does not correspond to the subprogram it is declared in. */
329 gcc_unreachable ();
332 /* If we've already processed this entity, return what we got last time.
333 If we are defining the node, we should not have already processed it.
334 In that case, we will abort below when we try to save a new GCC tree
335 for this object. We also need to handle the case of getting a dummy
336 type when a Full_View exists but be careful so as not to trigger its
337 premature elaboration. */
338 if ((!definition || (is_type && imported_p))
339 && present_gnu_tree (gnat_entity))
341 gnu_decl = get_gnu_tree (gnat_entity);
343 if (TREE_CODE (gnu_decl) == TYPE_DECL
344 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
345 && IN (kind, Incomplete_Or_Private_Kind)
346 && Present (Full_View (gnat_entity))
347 && (present_gnu_tree (Full_View (gnat_entity))
348 || No (Freeze_Node (Full_View (gnat_entity)))))
350 gnu_decl
351 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
352 save_gnu_tree (gnat_entity, NULL_TREE, false);
353 save_gnu_tree (gnat_entity, gnu_decl, false);
356 return gnu_decl;
359 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
360 must be specified unless it was specified by the programmer. Exceptions
361 are for access-to-protected-subprogram types and all access subtypes, as
362 another GNAT type is used to lay out the GCC type for them. */
363 gcc_assert (!Unknown_Esize (gnat_entity)
364 || Has_Size_Clause (gnat_entity)
365 || (!IN (kind, Numeric_Kind)
366 && !IN (kind, Enumeration_Kind)
367 && (!IN (kind, Access_Kind)
368 || kind == E_Access_Protected_Subprogram_Type
369 || kind == E_Anonymous_Access_Protected_Subprogram_Type
370 || kind == E_Access_Subtype
371 || type_annotate_only)));
373 /* The RM size must be specified for all discrete and fixed-point types. */
374 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
375 && Unknown_RM_Size (gnat_entity)));
377 /* If we get here, it means we have not yet done anything with this entity.
378 If we are not defining it, it must be a type or an entity that is defined
379 elsewhere or externally, otherwise we should have defined it already. */
380 gcc_assert (definition
381 || type_annotate_only
382 || is_type
383 || kind == E_Discriminant
384 || kind == E_Component
385 || kind == E_Label
386 || (kind == E_Constant && Present (Full_View (gnat_entity)))
387 || Is_Public (gnat_entity));
389 /* Get the name of the entity and set up the line number and filename of
390 the original definition for use in any decl we make. Make sure we do not
391 inherit another source location. */
392 gnu_entity_name = get_entity_name (gnat_entity);
393 if (Sloc (gnat_entity) != No_Location
394 && !renaming_from_generic_instantiation_p (gnat_entity))
395 Sloc_to_locus (Sloc (gnat_entity), &input_location);
397 /* For cases when we are not defining (i.e., we are referencing from
398 another compilation unit) public entities, show we are at global level
399 for the purpose of computing scopes. Don't do this for components or
400 discriminants since the relevant test is whether or not the record is
401 being defined. */
402 if (!definition
403 && kind != E_Component
404 && kind != E_Discriminant
405 && Is_Public (gnat_entity)
406 && !Is_Statically_Allocated (gnat_entity))
407 force_global++, this_global = true;
409 /* Handle any attributes directly attached to the entity. */
410 if (Has_Gigi_Rep_Item (gnat_entity))
411 prepend_attributes (&attr_list, gnat_entity);
413 /* Do some common processing for types. */
414 if (is_type)
416 /* Compute the equivalent type to be used in gigi. */
417 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
419 /* Machine_Attributes on types are expected to be propagated to
420 subtypes. The corresponding Gigi_Rep_Items are only attached
421 to the first subtype though, so we handle the propagation here. */
422 if (Base_Type (gnat_entity) != gnat_entity
423 && !Is_First_Subtype (gnat_entity)
424 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
425 prepend_attributes (&attr_list,
426 First_Subtype (Base_Type (gnat_entity)));
428 /* Compute a default value for the size of an elementary type. */
429 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
431 unsigned int max_esize;
433 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
434 esize = UI_To_Int (Esize (gnat_entity));
436 if (IN (kind, Float_Kind))
437 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
438 else if (IN (kind, Access_Kind))
439 max_esize = POINTER_SIZE * 2;
440 else
441 max_esize = LONG_LONG_TYPE_SIZE;
443 if (esize > max_esize)
444 esize = max_esize;
448 switch (kind)
450 case E_Component:
451 case E_Discriminant:
453 /* The GNAT record where the component was defined. */
454 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
456 /* If the entity is a discriminant of an extended tagged type used to
457 rename a discriminant of the parent type, return the latter. */
458 if (Is_Tagged_Type (gnat_record)
459 && Present (Corresponding_Discriminant (gnat_entity)))
461 gnu_decl
462 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
463 gnu_expr, definition);
464 saved = true;
465 break;
468 /* If the entity is an inherited component (in the case of extended
469 tagged record types), just return the original entity, which must
470 be a FIELD_DECL. Likewise for discriminants. If the entity is a
471 non-girder discriminant (in the case of derived untagged record
472 types), return the stored discriminant it renames. */
473 else if (Present (Original_Record_Component (gnat_entity))
474 && Original_Record_Component (gnat_entity) != gnat_entity)
476 gnu_decl
477 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
478 gnu_expr, definition);
479 saved = true;
480 break;
483 /* Otherwise, if we are not defining this and we have no GCC type
484 for the containing record, make one for it. Then we should
485 have made our own equivalent. */
486 else if (!definition && !present_gnu_tree (gnat_record))
488 /* ??? If this is in a record whose scope is a protected
489 type and we have an Original_Record_Component, use it.
490 This is a workaround for major problems in protected type
491 handling. */
492 Entity_Id Scop = Scope (Scope (gnat_entity));
493 if (Is_Protected_Type (Underlying_Type (Scop))
494 && Present (Original_Record_Component (gnat_entity)))
496 gnu_decl
497 = gnat_to_gnu_entity (Original_Record_Component
498 (gnat_entity),
499 gnu_expr, 0);
500 saved = true;
501 break;
504 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
505 gnu_decl = get_gnu_tree (gnat_entity);
506 saved = true;
507 break;
510 else
511 /* Here we have no GCC type and this is a reference rather than a
512 definition. This should never happen. Most likely the cause is
513 reference before declaration in the GNAT tree for gnat_entity. */
514 gcc_unreachable ();
517 case E_Constant:
518 /* Ignore constant definitions already marked with the error node. See
519 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
520 if (definition
521 && gnu_expr
522 && present_gnu_tree (gnat_entity)
523 && get_gnu_tree (gnat_entity) == error_mark_node)
525 maybe_present = true;
526 break;
529 /* Ignore deferred constant definitions without address clause since
530 they are processed fully in the front-end. If No_Initialization
531 is set, this is not a deferred constant but a constant whose value
532 is built manually. And constants that are renamings are handled
533 like variables. */
534 if (definition
535 && !gnu_expr
536 && No (Address_Clause (gnat_entity))
537 && !No_Initialization (Declaration_Node (gnat_entity))
538 && No (Renamed_Object (gnat_entity)))
540 gnu_decl = error_mark_node;
541 saved = true;
542 break;
545 /* If this is a use of a deferred constant without address clause,
546 get its full definition. */
547 if (!definition
548 && No (Address_Clause (gnat_entity))
549 && Present (Full_View (gnat_entity)))
551 gnu_decl
552 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
553 saved = true;
554 break;
557 /* If we have a constant that we are not defining, get the expression it
558 was defined to represent. This is necessary to avoid generating dumb
559 elaboration code in simple cases, but we may throw it away later if it
560 is not a constant. But do not retrieve it if it is an allocator since
561 the designated type might still be dummy at this point. */
562 if (!definition
563 && !No_Initialization (Declaration_Node (gnat_entity))
564 && Present (Expression (Declaration_Node (gnat_entity)))
565 && Nkind (Expression (Declaration_Node (gnat_entity)))
566 != N_Allocator)
568 bool went_into_elab_proc = false;
569 int save_force_global = force_global;
571 /* The expression may contain N_Expression_With_Actions nodes and
572 thus object declarations from other units. In this case, even
573 though the expression will eventually be discarded since not a
574 constant, the declarations would be stuck either in the global
575 varpool or in the current scope. Therefore we force the local
576 context and create a fake scope that we'll zap at the end. */
577 if (!current_function_decl)
579 current_function_decl = get_elaboration_procedure ();
580 went_into_elab_proc = true;
582 force_global = 0;
583 gnat_pushlevel ();
585 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
587 gnat_zaplevel ();
588 force_global = save_force_global;
589 if (went_into_elab_proc)
590 current_function_decl = NULL_TREE;
593 /* ... fall through ... */
595 case E_Exception:
596 case E_Loop_Parameter:
597 case E_Out_Parameter:
598 case E_Variable:
600 /* Always create a variable for volatile objects and variables seen
601 constant but with a Linker_Section pragma. */
602 bool const_flag
603 = ((kind == E_Constant || kind == E_Variable)
604 && Is_True_Constant (gnat_entity)
605 && !(kind == E_Variable
606 && Present (Linker_Section_Pragma (gnat_entity)))
607 && !Treat_As_Volatile (gnat_entity)
608 && (((Nkind (Declaration_Node (gnat_entity))
609 == N_Object_Declaration)
610 && Present (Expression (Declaration_Node (gnat_entity))))
611 || Present (Renamed_Object (gnat_entity))
612 || imported_p));
613 bool inner_const_flag = const_flag;
614 bool static_p = Is_Statically_Allocated (gnat_entity);
615 bool mutable_p = false;
616 bool used_by_ref = false;
617 tree gnu_ext_name = NULL_TREE;
618 tree renamed_obj = NULL_TREE;
619 tree gnu_object_size;
621 if (Present (Renamed_Object (gnat_entity)) && !definition)
623 if (kind == E_Exception)
624 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
625 NULL_TREE, 0);
626 else
627 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
630 /* Get the type after elaborating the renamed object. */
631 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
633 /* If this is a standard exception definition, then use the standard
634 exception type. This is necessary to make sure that imported and
635 exported views of exceptions are properly merged in LTO mode. */
636 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
637 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
638 gnu_type = except_type_node;
640 /* For a debug renaming declaration, build a debug-only entity. */
641 if (Present (Debug_Renaming_Link (gnat_entity)))
643 /* Force a non-null value to make sure the symbol is retained. */
644 tree value = build1 (INDIRECT_REF, gnu_type,
645 build1 (NOP_EXPR,
646 build_pointer_type (gnu_type),
647 integer_minus_one_node));
648 gnu_decl = build_decl (input_location,
649 VAR_DECL, gnu_entity_name, gnu_type);
650 SET_DECL_VALUE_EXPR (gnu_decl, value);
651 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
652 gnat_pushdecl (gnu_decl, gnat_entity);
653 break;
656 /* If this is a loop variable, its type should be the base type.
657 This is because the code for processing a loop determines whether
658 a normal loop end test can be done by comparing the bounds of the
659 loop against those of the base type, which is presumed to be the
660 size used for computation. But this is not correct when the size
661 of the subtype is smaller than the type. */
662 if (kind == E_Loop_Parameter)
663 gnu_type = get_base_type (gnu_type);
665 /* Reject non-renamed objects whose type is an unconstrained array or
666 any object whose type is a dummy type or void. */
667 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
668 && No (Renamed_Object (gnat_entity)))
669 || TYPE_IS_DUMMY_P (gnu_type)
670 || TREE_CODE (gnu_type) == VOID_TYPE)
672 gcc_assert (type_annotate_only);
673 if (this_global)
674 force_global--;
675 return error_mark_node;
678 /* If an alignment is specified, use it if valid. Note that exceptions
679 are objects but don't have an alignment. We must do this before we
680 validate the size, since the alignment can affect the size. */
681 if (kind != E_Exception && Known_Alignment (gnat_entity))
683 gcc_assert (Present (Alignment (gnat_entity)));
685 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
686 TYPE_ALIGN (gnu_type));
688 /* No point in changing the type if there is an address clause
689 as the final type of the object will be a reference type. */
690 if (Present (Address_Clause (gnat_entity)))
691 align = 0;
692 else
694 tree orig_type = gnu_type;
696 gnu_type
697 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
698 false, false, definition, true);
700 /* If a padding record was made, declare it now since it will
701 never be declared otherwise. This is necessary to ensure
702 that its subtrees are properly marked. */
703 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
704 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
705 debug_info_p, gnat_entity);
709 /* If we are defining the object, see if it has a Size and validate it
710 if so. If we are not defining the object and a Size clause applies,
711 simply retrieve the value. We don't want to ignore the clause and
712 it is expected to have been validated already. Then get the new
713 type, if any. */
714 if (definition)
715 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
716 gnat_entity, VAR_DECL, false,
717 Has_Size_Clause (gnat_entity));
718 else if (Has_Size_Clause (gnat_entity))
719 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
721 if (gnu_size)
723 gnu_type
724 = make_type_from_size (gnu_type, gnu_size,
725 Has_Biased_Representation (gnat_entity));
727 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
728 gnu_size = NULL_TREE;
731 /* If this object has self-referential size, it must be a record with
732 a default discriminant. We are supposed to allocate an object of
733 the maximum size in this case, unless it is a constant with an
734 initializing expression, in which case we can get the size from
735 that. Note that the resulting size may still be a variable, so
736 this may end up with an indirect allocation. */
737 if (No (Renamed_Object (gnat_entity))
738 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
740 if (gnu_expr && kind == E_Constant)
742 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
743 if (CONTAINS_PLACEHOLDER_P (size))
745 /* If the initializing expression is itself a constant,
746 despite having a nominal type with self-referential
747 size, we can get the size directly from it. */
748 if (TREE_CODE (gnu_expr) == COMPONENT_REF
749 && TYPE_IS_PADDING_P
750 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
751 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
752 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
753 || DECL_READONLY_ONCE_ELAB
754 (TREE_OPERAND (gnu_expr, 0))))
755 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
756 else
757 gnu_size
758 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
760 else
761 gnu_size = size;
763 /* We may have no GNU_EXPR because No_Initialization is
764 set even though there's an Expression. */
765 else if (kind == E_Constant
766 && (Nkind (Declaration_Node (gnat_entity))
767 == N_Object_Declaration)
768 && Present (Expression (Declaration_Node (gnat_entity))))
769 gnu_size
770 = TYPE_SIZE (gnat_to_gnu_type
771 (Etype
772 (Expression (Declaration_Node (gnat_entity)))));
773 else
775 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
776 mutable_p = true;
779 /* If we are at global level and the size isn't constant, call
780 elaborate_expression_1 to make a variable for it rather than
781 calculating it each time. */
782 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
783 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
784 "SIZE", definition, false);
787 /* If the size is zero byte, make it one byte since some linkers have
788 troubles with zero-sized objects. If the object will have a
789 template, that will make it nonzero so don't bother. Also avoid
790 doing that for an object renaming or an object with an address
791 clause, as we would lose useful information on the view size
792 (e.g. for null array slices) and we are not allocating the object
793 here anyway. */
794 if (((gnu_size
795 && integer_zerop (gnu_size)
796 && !TREE_OVERFLOW (gnu_size))
797 || (TYPE_SIZE (gnu_type)
798 && integer_zerop (TYPE_SIZE (gnu_type))
799 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
800 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
801 && No (Renamed_Object (gnat_entity))
802 && No (Address_Clause (gnat_entity)))
803 gnu_size = bitsize_unit_node;
805 /* If this is an object with no specified size and alignment, and
806 if either it is atomic or we are not optimizing alignment for
807 space and it is composite and not an exception, an Out parameter
808 or a reference to another object, and the size of its type is a
809 constant, set the alignment to the smallest one which is not
810 smaller than the size, with an appropriate cap. */
811 if (!gnu_size && align == 0
812 && (Is_Atomic_Or_VFA (gnat_entity)
813 || (!Optimize_Alignment_Space (gnat_entity)
814 && kind != E_Exception
815 && kind != E_Out_Parameter
816 && Is_Composite_Type (Etype (gnat_entity))
817 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
818 && !Is_Exported (gnat_entity)
819 && !imported_p
820 && No (Renamed_Object (gnat_entity))
821 && No (Address_Clause (gnat_entity))))
822 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
824 unsigned int size_cap, align_cap;
826 /* No point in promoting the alignment if this doesn't prevent
827 BLKmode access to the object, in particular block copy, as
828 this will for example disable the NRV optimization for it.
829 No point in jumping through all the hoops needed in order
830 to support BIGGEST_ALIGNMENT if we don't really have to.
831 So we cap to the smallest alignment that corresponds to
832 a known efficient memory access pattern of the target. */
833 if (Is_Atomic_Or_VFA (gnat_entity))
835 size_cap = UINT_MAX;
836 align_cap = BIGGEST_ALIGNMENT;
838 else
840 size_cap = MAX_FIXED_MODE_SIZE;
841 align_cap = get_mode_alignment (ptr_mode);
844 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
845 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
846 align = 0;
847 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
848 align = align_cap;
849 else
850 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
852 /* But make sure not to under-align the object. */
853 if (align <= TYPE_ALIGN (gnu_type))
854 align = 0;
856 /* And honor the minimum valid atomic alignment, if any. */
857 #ifdef MINIMUM_ATOMIC_ALIGNMENT
858 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
859 align = MINIMUM_ATOMIC_ALIGNMENT;
860 #endif
863 /* If the object is set to have atomic components, find the component
864 type and validate it.
866 ??? Note that we ignore Has_Volatile_Components on objects; it's
867 not at all clear what to do in that case. */
868 if (Has_Atomic_Components (gnat_entity))
870 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
871 ? TREE_TYPE (gnu_type) : gnu_type);
873 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
874 && TYPE_MULTI_ARRAY_P (gnu_inner))
875 gnu_inner = TREE_TYPE (gnu_inner);
877 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
880 /* If this is an aliased object with an unconstrained array nominal
881 subtype, make a type that includes the template. We will either
882 allocate or create a variable of that type, see below. */
883 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
884 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
885 && !type_annotate_only)
887 tree gnu_array
888 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
889 gnu_type
890 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
891 gnu_type,
892 concat_name (gnu_entity_name,
893 "UNC"),
894 debug_info_p);
897 /* ??? If this is an object of CW type initialized to a value, try to
898 ensure that the object is sufficient aligned for this value, but
899 without pessimizing the allocation. This is a kludge necessary
900 because we don't support dynamic alignment. */
901 if (align == 0
902 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
903 && No (Renamed_Object (gnat_entity))
904 && No (Address_Clause (gnat_entity)))
905 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
907 #ifdef MINIMUM_ATOMIC_ALIGNMENT
908 /* If the size is a constant and no alignment is specified, force
909 the alignment to be the minimum valid atomic alignment. The
910 restriction on constant size avoids problems with variable-size
911 temporaries; if the size is variable, there's no issue with
912 atomic access. Also don't do this for a constant, since it isn't
913 necessary and can interfere with constant replacement. Finally,
914 do not do it for Out parameters since that creates an
915 size inconsistency with In parameters. */
916 if (align == 0
917 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
918 && !FLOAT_TYPE_P (gnu_type)
919 && !const_flag && No (Renamed_Object (gnat_entity))
920 && !imported_p && No (Address_Clause (gnat_entity))
921 && kind != E_Out_Parameter
922 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
923 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
924 align = MINIMUM_ATOMIC_ALIGNMENT;
925 #endif
927 /* Make a new type with the desired size and alignment, if needed.
928 But do not take into account alignment promotions to compute the
929 size of the object. */
930 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
931 if (gnu_size || align > 0)
933 tree orig_type = gnu_type;
935 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
936 false, false, definition, true);
938 /* If a padding record was made, declare it now since it will
939 never be declared otherwise. This is necessary to ensure
940 that its subtrees are properly marked. */
941 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
942 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
943 debug_info_p, gnat_entity);
946 /* Now check if the type of the object allows atomic access. */
947 if (Is_Atomic_Or_VFA (gnat_entity))
948 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
950 /* If this is a renaming, avoid as much as possible to create a new
951 object. However, in some cases, creating it is required because
952 renaming can be applied to objects that are not names in Ada.
953 This processing needs to be applied to the raw expression so as
954 to make it more likely to rename the underlying object. */
955 if (Present (Renamed_Object (gnat_entity)))
957 /* If the renamed object had padding, strip off the reference to
958 the inner object and reset our type. */
959 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
960 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
961 /* Strip useless conversions around the object. */
962 || gnat_useless_type_conversion (gnu_expr))
964 gnu_expr = TREE_OPERAND (gnu_expr, 0);
965 gnu_type = TREE_TYPE (gnu_expr);
968 /* Or else, if the renamed object has an unconstrained type with
969 default discriminant, use the padded type. */
970 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
971 gnu_type = TREE_TYPE (gnu_expr);
973 /* Case 1: if this is a constant renaming stemming from a function
974 call, treat it as a normal object whose initial value is what
975 is being renamed. RM 3.3 says that the result of evaluating a
976 function call is a constant object. Therefore, it can be the
977 inner object of a constant renaming and the renaming must be
978 fully instantiated, i.e. it cannot be a reference to (part of)
979 an existing object. And treat null expressions, constructors
980 and literals the same way. */
981 tree inner = gnu_expr;
982 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
983 inner = TREE_OPERAND (inner, 0);
984 /* Expand_Dispatching_Call can prepend a comparison of the tags
985 before the call to "=". */
986 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR)
987 inner = TREE_OPERAND (inner, 1);
988 if ((TREE_CODE (inner) == CALL_EXPR
989 && !call_is_atomic_load (inner))
990 || TREE_CODE (inner) == NULL_EXPR
991 || TREE_CODE (inner) == CONSTRUCTOR
992 || CONSTANT_CLASS_P (inner))
995 /* Case 2: if the renaming entity need not be materialized, use
996 the elaborated renamed expression for the renaming. But this
997 means that the caller is responsible for evaluating the address
998 of the renaming in the correct place for the definition case to
999 instantiate the SAVE_EXPRs. */
1000 else if (TREE_CODE (inner) != COMPOUND_EXPR
1001 && !Materialize_Entity (gnat_entity))
1003 tree init = NULL_TREE;
1005 gnu_decl
1006 = elaborate_reference (gnu_expr, gnat_entity, definition,
1007 &init);
1009 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1010 correct place for this case, hence the above test. */
1011 gcc_assert (init == NULL_TREE);
1013 /* No DECL_EXPR will be created so the expression needs to be
1014 marked manually because it will likely be shared. */
1015 if (global_bindings_p ())
1016 MARK_VISITED (gnu_decl);
1018 /* This assertion will fail if the renamed object isn't aligned
1019 enough as to make it possible to honor the alignment set on
1020 the renaming. */
1021 if (align)
1023 unsigned int ralign = DECL_P (gnu_decl)
1024 ? DECL_ALIGN (gnu_decl)
1025 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1026 gcc_assert (ralign >= align);
1029 save_gnu_tree (gnat_entity, gnu_decl, true);
1030 saved = true;
1031 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1032 break;
1035 /* Case 3: otherwise, make a constant pointer to the object we
1036 are renaming and attach the object to the pointer after it is
1037 elaborated. The object will be referenced directly instead
1038 of indirectly via the pointer to avoid aliasing problems with
1039 non-addressable entities. The pointer is called a "renaming"
1040 pointer in this case. Note that we also need to preserve the
1041 volatility of the renamed object through the indirection. */
1042 else
1044 tree init = NULL_TREE;
1046 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1047 gnu_type
1048 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1050 gnu_type = build_reference_type (gnu_type);
1051 used_by_ref = true;
1052 const_flag = true;
1053 inner_const_flag = TREE_READONLY (gnu_expr);
1054 gnu_size = NULL_TREE;
1056 renamed_obj
1057 = elaborate_reference (gnu_expr, gnat_entity, definition,
1058 &init);
1060 /* If we are not defining the entity, the expression will not
1061 be attached through DECL_INITIAL so it needs to be marked
1062 manually because it will likely be shared. Likewise for a
1063 dereference as it will be folded by the ADDR_EXPR below. */
1064 if ((!definition || TREE_CODE (renamed_obj) == INDIRECT_REF)
1065 && global_bindings_p ())
1066 MARK_VISITED (renamed_obj);
1068 if (type_annotate_only
1069 && TREE_CODE (renamed_obj) == ERROR_MARK)
1070 gnu_expr = NULL_TREE;
1071 else
1073 gnu_expr
1074 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1075 if (init)
1076 gnu_expr
1077 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1078 gnu_expr);
1083 /* Make a volatile version of this object's type if we are to make
1084 the object volatile. We also interpret 13.3(19) conservatively
1085 and disallow any optimizations for such a non-constant object. */
1086 if ((Treat_As_Volatile (gnat_entity)
1087 || (!const_flag
1088 && gnu_type != except_type_node
1089 && (Is_Exported (gnat_entity)
1090 || imported_p
1091 || Present (Address_Clause (gnat_entity)))))
1092 && !TYPE_VOLATILE (gnu_type))
1094 const int quals
1095 = TYPE_QUAL_VOLATILE
1096 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
1097 gnu_type = change_qualified_type (gnu_type, quals);
1100 /* If we are defining an aliased object whose nominal subtype is
1101 unconstrained, the object is a record that contains both the
1102 template and the object. If there is an initializer, it will
1103 have already been converted to the right type, but we need to
1104 create the template if there is no initializer. */
1105 if (definition
1106 && !gnu_expr
1107 && TREE_CODE (gnu_type) == RECORD_TYPE
1108 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1109 /* Beware that padding might have been introduced above. */
1110 || (TYPE_PADDING_P (gnu_type)
1111 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1112 == RECORD_TYPE
1113 && TYPE_CONTAINS_TEMPLATE_P
1114 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1116 tree template_field
1117 = TYPE_PADDING_P (gnu_type)
1118 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1119 : TYPE_FIELDS (gnu_type);
1120 vec<constructor_elt, va_gc> *v;
1121 vec_alloc (v, 1);
1122 tree t = build_template (TREE_TYPE (template_field),
1123 TREE_TYPE (DECL_CHAIN (template_field)),
1124 NULL_TREE);
1125 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1126 gnu_expr = gnat_build_constructor (gnu_type, v);
1129 /* Convert the expression to the type of the object if need be. */
1130 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1131 gnu_expr = convert (gnu_type, gnu_expr);
1133 /* If this is a pointer that doesn't have an initializing expression,
1134 initialize it to NULL, unless the object is imported. */
1135 if (definition
1136 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1137 && !gnu_expr
1138 && !Is_Imported (gnat_entity))
1139 gnu_expr = integer_zero_node;
1141 /* If we are defining the object and it has an Address clause, we must
1142 either get the address expression from the saved GCC tree for the
1143 object if it has a Freeze node, or elaborate the address expression
1144 here since the front-end has guaranteed that the elaboration has no
1145 effects in this case. */
1146 if (definition && Present (Address_Clause (gnat_entity)))
1148 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1149 Node_Id gnat_expr = Expression (gnat_clause);
1150 tree gnu_address
1151 = present_gnu_tree (gnat_entity)
1152 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1154 save_gnu_tree (gnat_entity, NULL_TREE, false);
1156 /* Convert the type of the object to a reference type that can
1157 alias everything as per 13.3(19). */
1158 gnu_type
1159 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1160 gnu_address = convert (gnu_type, gnu_address);
1161 used_by_ref = true;
1162 const_flag
1163 = !Is_Public (gnat_entity)
1164 || compile_time_known_address_p (gnat_expr);
1165 gnu_size = NULL_TREE;
1167 /* If this is an aliased object with an unconstrained array nominal
1168 subtype, then it can overlay only another aliased object with an
1169 unconstrained array nominal subtype and compatible template. */
1170 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1171 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1172 && !type_annotate_only)
1174 tree rec_type = TREE_TYPE (gnu_type);
1175 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1177 /* This is the pattern built for a regular object. */
1178 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1179 && TREE_OPERAND (gnu_address, 1) == off)
1180 gnu_address = TREE_OPERAND (gnu_address, 0);
1181 /* This is the pattern built for an overaligned object. */
1182 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1183 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1184 == PLUS_EXPR
1185 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1186 == off)
1187 gnu_address
1188 = build2 (POINTER_PLUS_EXPR, gnu_type,
1189 TREE_OPERAND (gnu_address, 0),
1190 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1191 else
1193 post_error_ne ("aliased object& with unconstrained array "
1194 "nominal subtype", gnat_clause,
1195 gnat_entity);
1196 post_error ("\\can overlay only aliased object with "
1197 "compatible subtype", gnat_clause);
1201 /* If this is a deferred constant, the initializer is attached to
1202 the full view. */
1203 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1204 gnu_expr
1205 = gnat_to_gnu
1206 (Expression (Declaration_Node (Full_View (gnat_entity))));
1208 /* If we don't have an initializing expression for the underlying
1209 variable, the initializing expression for the pointer is the
1210 specified address. Otherwise, we have to make a COMPOUND_EXPR
1211 to assign both the address and the initial value. */
1212 if (!gnu_expr)
1213 gnu_expr = gnu_address;
1214 else
1215 gnu_expr
1216 = build2 (COMPOUND_EXPR, gnu_type,
1217 build_binary_op (INIT_EXPR, NULL_TREE,
1218 build_unary_op (INDIRECT_REF,
1219 NULL_TREE,
1220 gnu_address),
1221 gnu_expr),
1222 gnu_address);
1225 /* If it has an address clause and we are not defining it, mark it
1226 as an indirect object. Likewise for Stdcall objects that are
1227 imported. */
1228 if ((!definition && Present (Address_Clause (gnat_entity)))
1229 || (Is_Imported (gnat_entity)
1230 && Has_Stdcall_Convention (gnat_entity)))
1232 /* Convert the type of the object to a reference type that can
1233 alias everything as per 13.3(19). */
1234 gnu_type
1235 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1236 used_by_ref = true;
1237 gnu_size = NULL_TREE;
1239 /* No point in taking the address of an initializing expression
1240 that isn't going to be used. */
1241 gnu_expr = NULL_TREE;
1243 /* If it has an address clause whose value is known at compile
1244 time, make the object a CONST_DECL. This will avoid a
1245 useless dereference. */
1246 if (Present (Address_Clause (gnat_entity)))
1248 Node_Id gnat_address
1249 = Expression (Address_Clause (gnat_entity));
1251 if (compile_time_known_address_p (gnat_address))
1253 gnu_expr = gnat_to_gnu (gnat_address);
1254 const_flag = true;
1259 /* If we are at top level and this object is of variable size,
1260 make the actual type a hidden pointer to the real type and
1261 make the initializer be a memory allocation and initialization.
1262 Likewise for objects we aren't defining (presumed to be
1263 external references from other packages), but there we do
1264 not set up an initialization.
1266 If the object's size overflows, make an allocator too, so that
1267 Storage_Error gets raised. Note that we will never free
1268 such memory, so we presume it never will get allocated. */
1269 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1270 global_bindings_p ()
1271 || !definition
1272 || static_p)
1273 || (gnu_size
1274 && !allocatable_size_p (convert (sizetype,
1275 size_binop
1276 (CEIL_DIV_EXPR, gnu_size,
1277 bitsize_unit_node)),
1278 global_bindings_p ()
1279 || !definition
1280 || static_p)))
1282 gnu_type = build_reference_type (gnu_type);
1283 used_by_ref = true;
1284 const_flag = true;
1285 gnu_size = NULL_TREE;
1287 /* In case this was a aliased object whose nominal subtype is
1288 unconstrained, the pointer above will be a thin pointer and
1289 build_allocator will automatically make the template.
1291 If we have a template initializer only (that we made above),
1292 pretend there is none and rely on what build_allocator creates
1293 again anyway. Otherwise (if we have a full initializer), get
1294 the data part and feed that to build_allocator.
1296 If we are elaborating a mutable object, tell build_allocator to
1297 ignore a possibly simpler size from the initializer, if any, as
1298 we must allocate the maximum possible size in this case. */
1299 if (definition && !imported_p)
1301 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1303 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1304 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1306 gnu_alloc_type
1307 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1309 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1310 && vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)) == 1)
1311 gnu_expr = NULL_TREE;
1312 else
1313 gnu_expr
1314 = build_component_ref
1315 (gnu_expr, NULL_TREE,
1316 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1317 false);
1320 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1321 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1322 post_error ("?`Storage_Error` will be raised at run time!",
1323 gnat_entity);
1325 gnu_expr
1326 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1327 Empty, Empty, gnat_entity, mutable_p);
1329 else
1330 gnu_expr = NULL_TREE;
1333 /* If this object would go into the stack and has an alignment larger
1334 than the largest stack alignment the back-end can honor, resort to
1335 a variable of "aligning type". */
1336 if (definition
1337 && !global_bindings_p ()
1338 && !static_p
1339 && !imported_p
1340 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1342 /* Create the new variable. No need for extra room before the
1343 aligned field as this is in automatic storage. */
1344 tree gnu_new_type
1345 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1346 TYPE_SIZE_UNIT (gnu_type),
1347 BIGGEST_ALIGNMENT, 0, gnat_entity);
1348 tree gnu_new_var
1349 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1350 NULL_TREE, gnu_new_type, NULL_TREE, false,
1351 false, false, false, NULL, gnat_entity);
1352 DECL_ARTIFICIAL (gnu_new_var) = 1;
1354 /* Initialize the aligned field if we have an initializer. */
1355 if (gnu_expr)
1356 add_stmt_with_node
1357 (build_binary_op (INIT_EXPR, NULL_TREE,
1358 build_component_ref
1359 (gnu_new_var, NULL_TREE,
1360 TYPE_FIELDS (gnu_new_type), false),
1361 gnu_expr),
1362 gnat_entity);
1364 /* And setup this entity as a reference to the aligned field. */
1365 gnu_type = build_reference_type (gnu_type);
1366 gnu_expr
1367 = build_unary_op
1368 (ADDR_EXPR, NULL_TREE,
1369 build_component_ref (gnu_new_var, NULL_TREE,
1370 TYPE_FIELDS (gnu_new_type), false));
1371 TREE_CONSTANT (gnu_expr) = 1;
1373 used_by_ref = true;
1374 const_flag = true;
1375 gnu_size = NULL_TREE;
1378 /* If this is an aliased object with an unconstrained array nominal
1379 subtype, we make its type a thin reference, i.e. the reference
1380 counterpart of a thin pointer, so it points to the array part.
1381 This is aimed to make it easier for the debugger to decode the
1382 object. Note that we have to do it this late because of the
1383 couple of allocation adjustments that might be made above. */
1384 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1385 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1386 && !type_annotate_only)
1388 /* In case the object with the template has already been allocated
1389 just above, we have nothing to do here. */
1390 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1392 tree gnu_unc_var
1393 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1394 NULL_TREE, gnu_type, gnu_expr,
1395 const_flag, Is_Public (gnat_entity),
1396 imported_p || !definition, static_p,
1397 NULL, gnat_entity);
1398 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1399 TREE_CONSTANT (gnu_expr) = 1;
1401 used_by_ref = true;
1402 const_flag = true;
1403 inner_const_flag = TREE_READONLY (gnu_unc_var);
1404 gnu_size = NULL_TREE;
1407 tree gnu_array
1408 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1409 gnu_type
1410 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1413 if (const_flag)
1414 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1416 /* Convert the expression to the type of the object if need be. */
1417 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1418 gnu_expr = convert (gnu_type, gnu_expr);
1420 /* If this name is external or a name was specified, use it, but don't
1421 use the Interface_Name with an address clause (see cd30005). */
1422 if ((Present (Interface_Name (gnat_entity))
1423 && No (Address_Clause (gnat_entity)))
1424 || (Is_Public (gnat_entity)
1425 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1426 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1428 /* If this is an aggregate constant initialized to a constant, force it
1429 to be statically allocated. This saves an initialization copy. */
1430 if (!static_p
1431 && const_flag
1432 && gnu_expr && TREE_CONSTANT (gnu_expr)
1433 && AGGREGATE_TYPE_P (gnu_type)
1434 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1435 && !(TYPE_IS_PADDING_P (gnu_type)
1436 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1437 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1438 static_p = true;
1440 /* Deal with a pragma Linker_Section on a constant or variable. */
1441 if ((kind == E_Constant || kind == E_Variable)
1442 && Present (Linker_Section_Pragma (gnat_entity)))
1443 prepend_one_attribute_pragma (&attr_list,
1444 Linker_Section_Pragma (gnat_entity));
1446 /* Now create the variable or the constant and set various flags. */
1447 gnu_decl
1448 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1449 gnu_expr, const_flag, Is_Public (gnat_entity),
1450 imported_p || !definition, static_p,
1451 !renamed_obj, attr_list, gnat_entity);
1452 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1453 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1454 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1456 /* If we are defining an Out parameter and optimization isn't enabled,
1457 create a fake PARM_DECL for debugging purposes and make it point to
1458 the VAR_DECL. Suppress debug info for the latter but make sure it
1459 will live in memory so that it can be accessed from within the
1460 debugger through the PARM_DECL. */
1461 if (kind == E_Out_Parameter
1462 && definition
1463 && debug_info_p
1464 && !optimize
1465 && !flag_generate_lto)
1467 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1468 gnat_pushdecl (param, gnat_entity);
1469 SET_DECL_VALUE_EXPR (param, gnu_decl);
1470 DECL_HAS_VALUE_EXPR_P (param) = 1;
1471 DECL_IGNORED_P (gnu_decl) = 1;
1472 TREE_ADDRESSABLE (gnu_decl) = 1;
1475 /* If this is a loop parameter, set the corresponding flag. */
1476 else if (kind == E_Loop_Parameter)
1477 DECL_LOOP_PARM_P (gnu_decl) = 1;
1479 /* If this is a renaming pointer, attach the renamed object to it. */
1480 if (renamed_obj)
1481 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1483 /* If this is a constant and we are defining it or it generates a real
1484 symbol at the object level and we are referencing it, we may want
1485 or need to have a true variable to represent it:
1486 - if optimization isn't enabled, for debugging purposes,
1487 - if the constant is public and not overlaid on something else,
1488 - if its address is taken,
1489 - if either itself or its type is aliased. */
1490 if (TREE_CODE (gnu_decl) == CONST_DECL
1491 && (definition || Sloc (gnat_entity) > Standard_Location)
1492 && ((!optimize && debug_info_p)
1493 || (Is_Public (gnat_entity)
1494 && No (Address_Clause (gnat_entity)))
1495 || Address_Taken (gnat_entity)
1496 || Is_Aliased (gnat_entity)
1497 || Is_Aliased (Etype (gnat_entity))))
1499 tree gnu_corr_var
1500 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1501 gnu_expr, true, Is_Public (gnat_entity),
1502 !definition, static_p, attr_list,
1503 gnat_entity);
1505 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1507 /* As debugging information will be generated for the variable,
1508 do not generate debugging information for the constant. */
1509 if (debug_info_p)
1510 DECL_IGNORED_P (gnu_decl) = 1;
1511 else
1512 DECL_IGNORED_P (gnu_corr_var) = 1;
1515 /* If this is a constant, even if we don't need a true variable, we
1516 may need to avoid returning the initializer in every case. That
1517 can happen for the address of a (constant) constructor because,
1518 upon dereferencing it, the constructor will be reinjected in the
1519 tree, which may not be valid in every case; see lvalue_required_p
1520 for more details. */
1521 if (TREE_CODE (gnu_decl) == CONST_DECL)
1522 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1524 /* If this object is declared in a block that contains a block with an
1525 exception handler, and we aren't using the GCC exception mechanism,
1526 we must force this variable in memory in order to avoid an invalid
1527 optimization. */
1528 if (Exception_Mechanism != Back_End_Exceptions
1529 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1530 TREE_ADDRESSABLE (gnu_decl) = 1;
1532 /* If this is a local variable with non-BLKmode and aggregate type,
1533 and optimization isn't enabled, then force it in memory so that
1534 a register won't be allocated to it with possible subparts left
1535 uninitialized and reaching the register allocator. */
1536 else if (TREE_CODE (gnu_decl) == VAR_DECL
1537 && !DECL_EXTERNAL (gnu_decl)
1538 && !TREE_STATIC (gnu_decl)
1539 && DECL_MODE (gnu_decl) != BLKmode
1540 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1541 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1542 && !optimize)
1543 TREE_ADDRESSABLE (gnu_decl) = 1;
1545 /* If we are defining an object with variable size or an object with
1546 fixed size that will be dynamically allocated, and we are using the
1547 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1548 if (definition
1549 && Exception_Mechanism == Setjmp_Longjmp
1550 && get_block_jmpbuf_decl ()
1551 && DECL_SIZE_UNIT (gnu_decl)
1552 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1553 || (flag_stack_check == GENERIC_STACK_CHECK
1554 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1555 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1556 add_stmt_with_node (build_call_n_expr
1557 (update_setjmp_buf_decl, 1,
1558 build_unary_op (ADDR_EXPR, NULL_TREE,
1559 get_block_jmpbuf_decl ())),
1560 gnat_entity);
1562 /* Back-annotate Esize and Alignment of the object if not already
1563 known. Note that we pick the values of the type, not those of
1564 the object, to shield ourselves from low-level platform-dependent
1565 adjustments like alignment promotion. This is both consistent with
1566 all the treatment above, where alignment and size are set on the
1567 type of the object and not on the object directly, and makes it
1568 possible to support all confirming representation clauses. */
1569 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1570 used_by_ref);
1572 break;
1574 case E_Void:
1575 /* Return a TYPE_DECL for "void" that we previously made. */
1576 gnu_decl = TYPE_NAME (void_type_node);
1577 break;
1579 case E_Enumeration_Type:
1580 /* A special case: for the types Character and Wide_Character in
1581 Standard, we do not list all the literals. So if the literals
1582 are not specified, make this an unsigned integer type. */
1583 if (No (First_Literal (gnat_entity)))
1585 gnu_type = make_unsigned_type (esize);
1586 TYPE_NAME (gnu_type) = gnu_entity_name;
1588 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1589 This is needed by the DWARF-2 back-end to distinguish between
1590 unsigned integer types and character types. */
1591 TYPE_STRING_FLAG (gnu_type) = 1;
1593 else
1595 /* We have a list of enumeral constants in First_Literal. We make a
1596 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1597 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1598 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1599 value of the literal. But when we have a regular boolean type, we
1600 simplify this a little by using a BOOLEAN_TYPE. */
1601 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1602 && !Has_Non_Standard_Rep (gnat_entity);
1603 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1604 tree gnu_list = NULL_TREE;
1605 Entity_Id gnat_literal;
1607 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1608 TYPE_PRECISION (gnu_type) = esize;
1609 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1610 set_min_and_max_values_for_integral_type (gnu_type, esize,
1611 TYPE_SIGN (gnu_type));
1612 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1613 layout_type (gnu_type);
1615 for (gnat_literal = First_Literal (gnat_entity);
1616 Present (gnat_literal);
1617 gnat_literal = Next_Literal (gnat_literal))
1619 tree gnu_value
1620 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1621 tree gnu_literal
1622 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1623 gnu_type, gnu_value, true, false, false,
1624 false, NULL, gnat_literal);
1625 /* Do not generate debug info for individual enumerators. */
1626 DECL_IGNORED_P (gnu_literal) = 1;
1627 save_gnu_tree (gnat_literal, gnu_literal, false);
1628 gnu_list
1629 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1632 if (!is_boolean)
1633 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1635 /* Note that the bounds are updated at the end of this function
1636 to avoid an infinite recursion since they refer to the type. */
1637 goto discrete_type;
1639 break;
1641 case E_Signed_Integer_Type:
1642 case E_Ordinary_Fixed_Point_Type:
1643 case E_Decimal_Fixed_Point_Type:
1644 /* For integer types, just make a signed type the appropriate number
1645 of bits. */
1646 gnu_type = make_signed_type (esize);
1647 goto discrete_type;
1649 case E_Modular_Integer_Type:
1651 /* For modular types, make the unsigned type of the proper number
1652 of bits and then set up the modulus, if required. */
1653 tree gnu_modulus, gnu_high = NULL_TREE;
1655 /* Packed Array Impl. Types are supposed to be subtypes only. */
1656 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1658 gnu_type = make_unsigned_type (esize);
1660 /* Get the modulus in this type. If it overflows, assume it is because
1661 it is equal to 2**Esize. Note that there is no overflow checking
1662 done on unsigned type, so we detect the overflow by looking for
1663 a modulus of zero, which is otherwise invalid. */
1664 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1666 if (!integer_zerop (gnu_modulus))
1668 TYPE_MODULAR_P (gnu_type) = 1;
1669 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1670 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1671 convert (gnu_type, integer_one_node));
1674 /* If the upper bound is not maximal, make an extra subtype. */
1675 if (gnu_high
1676 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1678 tree gnu_subtype = make_unsigned_type (esize);
1679 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1680 TREE_TYPE (gnu_subtype) = gnu_type;
1681 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1682 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1683 gnu_type = gnu_subtype;
1686 goto discrete_type;
1688 case E_Signed_Integer_Subtype:
1689 case E_Enumeration_Subtype:
1690 case E_Modular_Integer_Subtype:
1691 case E_Ordinary_Fixed_Point_Subtype:
1692 case E_Decimal_Fixed_Point_Subtype:
1694 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1695 not want to call create_range_type since we would like each subtype
1696 node to be distinct. ??? Historically this was in preparation for
1697 when memory aliasing is implemented, but that's obsolete now given
1698 the call to relate_alias_sets below.
1700 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1701 this fact is used by the arithmetic conversion functions.
1703 We elaborate the Ancestor_Subtype if it is not in the current unit
1704 and one of our bounds is non-static. We do this to ensure consistent
1705 naming in the case where several subtypes share the same bounds, by
1706 elaborating the first such subtype first, thus using its name. */
1708 if (!definition
1709 && Present (Ancestor_Subtype (gnat_entity))
1710 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1711 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1712 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1713 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1715 /* Set the precision to the Esize except for bit-packed arrays. */
1716 if (Is_Packed_Array_Impl_Type (gnat_entity)
1717 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1718 esize = UI_To_Int (RM_Size (gnat_entity));
1720 /* This should be an unsigned type if the base type is unsigned or
1721 if the lower bound is constant and non-negative or if the type
1722 is biased. */
1723 if (Is_Unsigned_Type (Etype (gnat_entity))
1724 || Is_Unsigned_Type (gnat_entity)
1725 || Has_Biased_Representation (gnat_entity))
1726 gnu_type = make_unsigned_type (esize);
1727 else
1728 gnu_type = make_signed_type (esize);
1729 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1731 SET_TYPE_RM_MIN_VALUE
1732 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1733 gnat_entity, "L", definition, true,
1734 Needs_Debug_Info (gnat_entity)));
1736 SET_TYPE_RM_MAX_VALUE
1737 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1738 gnat_entity, "U", definition, true,
1739 Needs_Debug_Info (gnat_entity)));
1741 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1742 = Has_Biased_Representation (gnat_entity);
1744 /* Inherit our alias set from what we're a subtype of. Subtypes
1745 are not different types and a pointer can designate any instance
1746 within a subtype hierarchy. */
1747 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1749 /* One of the above calls might have caused us to be elaborated,
1750 so don't blow up if so. */
1751 if (present_gnu_tree (gnat_entity))
1753 maybe_present = true;
1754 break;
1757 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1758 TYPE_STUB_DECL (gnu_type)
1759 = create_type_stub_decl (gnu_entity_name, gnu_type);
1761 /* For a packed array, make the original array type a parallel type. */
1762 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1763 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1765 discrete_type:
1767 /* We have to handle clauses that under-align the type specially. */
1768 if ((Present (Alignment_Clause (gnat_entity))
1769 || (Is_Packed_Array_Impl_Type (gnat_entity)
1770 && Present
1771 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1772 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1774 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1775 if (align >= TYPE_ALIGN (gnu_type))
1776 align = 0;
1779 /* If the type we are dealing with represents a bit-packed array,
1780 we need to have the bits left justified on big-endian targets
1781 and right justified on little-endian targets. We also need to
1782 ensure that when the value is read (e.g. for comparison of two
1783 such values), we only get the good bits, since the unused bits
1784 are uninitialized. Both goals are accomplished by wrapping up
1785 the modular type in an enclosing record type. */
1786 if (Is_Packed_Array_Impl_Type (gnat_entity)
1787 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1789 tree gnu_field_type, gnu_field;
1791 /* Set the RM size before wrapping up the original type. */
1792 SET_TYPE_RM_SIZE (gnu_type,
1793 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1794 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1796 /* Create a stripped-down declaration, mainly for debugging. */
1797 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1798 gnat_entity);
1800 /* Now save it and build the enclosing record type. */
1801 gnu_field_type = gnu_type;
1803 gnu_type = make_node (RECORD_TYPE);
1804 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1805 TYPE_PACKED (gnu_type) = 1;
1806 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1807 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1808 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1810 /* Propagate the alignment of the modular type to the record type,
1811 unless there is an alignment clause that under-aligns the type.
1812 This means that bit-packed arrays are given "ceil" alignment for
1813 their size by default, which may seem counter-intuitive but makes
1814 it possible to overlay them on modular types easily. */
1815 TYPE_ALIGN (gnu_type)
1816 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1818 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1820 /* Don't declare the field as addressable since we won't be taking
1821 its address and this would prevent create_field_decl from making
1822 a bitfield. */
1823 gnu_field
1824 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1825 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1827 /* Do not emit debug info until after the parallel type is added. */
1828 finish_record_type (gnu_type, gnu_field, 2, false);
1829 compute_record_mode (gnu_type);
1830 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1832 if (debug_info_p)
1834 /* Make the original array type a parallel type. */
1835 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1837 rest_of_record_type_compilation (gnu_type);
1841 /* If the type we are dealing with has got a smaller alignment than the
1842 natural one, we need to wrap it up in a record type and misalign the
1843 latter; we reuse the padding machinery for this purpose. Note that,
1844 even if the record type is marked as packed because of misalignment,
1845 we don't pack the field so as to give it the size of the type. */
1846 else if (align > 0)
1848 tree gnu_field_type, gnu_field;
1850 /* Set the RM size before wrapping up the type. */
1851 SET_TYPE_RM_SIZE (gnu_type,
1852 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1854 /* Create a stripped-down declaration, mainly for debugging. */
1855 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1856 gnat_entity);
1858 /* Now save it and build the enclosing record type. */
1859 gnu_field_type = gnu_type;
1861 gnu_type = make_node (RECORD_TYPE);
1862 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1863 TYPE_PACKED (gnu_type) = 1;
1864 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1865 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1866 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1867 TYPE_ALIGN (gnu_type) = align;
1868 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1870 /* Don't declare the field as addressable since we won't be taking
1871 its address and this would prevent create_field_decl from making
1872 a bitfield. */
1873 gnu_field
1874 = create_field_decl (get_identifier ("F"), gnu_field_type,
1875 gnu_type, TYPE_SIZE (gnu_field_type),
1876 bitsize_zero_node, 0, 0);
1878 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1879 compute_record_mode (gnu_type);
1880 TYPE_PADDING_P (gnu_type) = 1;
1883 break;
1885 case E_Floating_Point_Type:
1886 /* The type of the Low and High bounds can be our type if this is
1887 a type from Standard, so set them at the end of the function. */
1888 gnu_type = make_node (REAL_TYPE);
1889 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1890 layout_type (gnu_type);
1891 break;
1893 case E_Floating_Point_Subtype:
1894 /* See the E_Signed_Integer_Subtype case for the rationale. */
1895 if (!definition
1896 && Present (Ancestor_Subtype (gnat_entity))
1897 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1898 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1899 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1900 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1902 gnu_type = make_node (REAL_TYPE);
1903 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1904 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1905 TYPE_GCC_MIN_VALUE (gnu_type)
1906 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1907 TYPE_GCC_MAX_VALUE (gnu_type)
1908 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1909 layout_type (gnu_type);
1911 SET_TYPE_RM_MIN_VALUE
1912 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1913 gnat_entity, "L", definition, true,
1914 Needs_Debug_Info (gnat_entity)));
1916 SET_TYPE_RM_MAX_VALUE
1917 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1918 gnat_entity, "U", definition, true,
1919 Needs_Debug_Info (gnat_entity)));
1921 /* Inherit our alias set from what we're a subtype of, as for
1922 integer subtypes. */
1923 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1925 /* One of the above calls might have caused us to be elaborated,
1926 so don't blow up if so. */
1927 maybe_present = true;
1928 break;
1930 /* Array Types and Subtypes
1932 Unconstrained array types are represented by E_Array_Type and
1933 constrained array types are represented by E_Array_Subtype. There
1934 are no actual objects of an unconstrained array type; all we have
1935 are pointers to that type.
1937 The following fields are defined on array types and subtypes:
1939 Component_Type Component type of the array.
1940 Number_Dimensions Number of dimensions (an int).
1941 First_Index Type of first index. */
1943 case E_Array_Type:
1945 const bool convention_fortran_p
1946 = (Convention (gnat_entity) == Convention_Fortran);
1947 const int ndim = Number_Dimensions (gnat_entity);
1948 tree gnu_template_type;
1949 tree gnu_ptr_template;
1950 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1951 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1952 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1953 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1954 Entity_Id gnat_index, gnat_name;
1955 int index;
1956 tree comp_type;
1958 /* Create the type for the component now, as it simplifies breaking
1959 type reference loops. */
1960 comp_type
1961 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
1962 if (present_gnu_tree (gnat_entity))
1964 /* As a side effect, the type may have been translated. */
1965 maybe_present = true;
1966 break;
1969 /* We complete an existing dummy fat pointer type in place. This both
1970 avoids further complex adjustments in update_pointer_to and yields
1971 better debugging information in DWARF by leveraging the support for
1972 incomplete declarations of "tagged" types in the DWARF back-end. */
1973 gnu_type = get_dummy_type (gnat_entity);
1974 if (gnu_type && TYPE_POINTER_TO (gnu_type))
1976 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
1977 TYPE_NAME (gnu_fat_type) = NULL_TREE;
1978 /* Save the contents of the dummy type for update_pointer_to. */
1979 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
1980 gnu_ptr_template =
1981 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1982 gnu_template_type = TREE_TYPE (gnu_ptr_template);
1984 else
1986 gnu_fat_type = make_node (RECORD_TYPE);
1987 gnu_template_type = make_node (RECORD_TYPE);
1988 gnu_ptr_template = build_pointer_type (gnu_template_type);
1991 /* Make a node for the array. If we are not defining the array
1992 suppress expanding incomplete types. */
1993 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1995 if (!definition)
1997 defer_incomplete_level++;
1998 this_deferred = true;
2001 /* Build the fat pointer type. Use a "void *" object instead of
2002 a pointer to the array type since we don't have the array type
2003 yet (it will reference the fat pointer via the bounds). */
2005 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2006 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2007 DECL_CHAIN (tem)
2008 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2009 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2011 if (COMPLETE_TYPE_P (gnu_fat_type))
2013 /* We are going to lay it out again so reset the alias set. */
2014 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2015 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2016 finish_fat_pointer_type (gnu_fat_type, tem);
2017 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2018 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2020 TYPE_FIELDS (t) = tem;
2021 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2024 else
2026 finish_fat_pointer_type (gnu_fat_type, tem);
2027 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2030 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2031 is the fat pointer. This will be used to access the individual
2032 fields once we build them. */
2033 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2034 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2035 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2036 gnu_template_reference
2037 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2038 TREE_READONLY (gnu_template_reference) = 1;
2039 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2041 /* Now create the GCC type for each index and add the fields for that
2042 index to the template. */
2043 for (index = (convention_fortran_p ? ndim - 1 : 0),
2044 gnat_index = First_Index (gnat_entity);
2045 0 <= index && index < ndim;
2046 index += (convention_fortran_p ? - 1 : 1),
2047 gnat_index = Next_Index (gnat_index))
2049 char field_name[16];
2050 tree gnu_index_base_type
2051 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2052 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2053 tree gnu_min, gnu_max, gnu_high;
2055 /* Make the FIELD_DECLs for the low and high bounds of this
2056 type and then make extractions of these fields from the
2057 template. */
2058 sprintf (field_name, "LB%d", index);
2059 gnu_lb_field = create_field_decl (get_identifier (field_name),
2060 gnu_index_base_type,
2061 gnu_template_type, NULL_TREE,
2062 NULL_TREE, 0, 0);
2063 Sloc_to_locus (Sloc (gnat_entity),
2064 &DECL_SOURCE_LOCATION (gnu_lb_field));
2066 field_name[0] = 'U';
2067 gnu_hb_field = create_field_decl (get_identifier (field_name),
2068 gnu_index_base_type,
2069 gnu_template_type, NULL_TREE,
2070 NULL_TREE, 0, 0);
2071 Sloc_to_locus (Sloc (gnat_entity),
2072 &DECL_SOURCE_LOCATION (gnu_hb_field));
2074 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2076 /* We can't use build_component_ref here since the template type
2077 isn't complete yet. */
2078 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2079 gnu_template_reference, gnu_lb_field,
2080 NULL_TREE);
2081 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2082 gnu_template_reference, gnu_hb_field,
2083 NULL_TREE);
2084 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2086 gnu_min = convert (sizetype, gnu_orig_min);
2087 gnu_max = convert (sizetype, gnu_orig_max);
2089 /* Compute the size of this dimension. See the E_Array_Subtype
2090 case below for the rationale. */
2091 gnu_high
2092 = build3 (COND_EXPR, sizetype,
2093 build2 (GE_EXPR, boolean_type_node,
2094 gnu_orig_max, gnu_orig_min),
2095 gnu_max,
2096 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2098 /* Make a range type with the new range in the Ada base type.
2099 Then make an index type with the size range in sizetype. */
2100 gnu_index_types[index]
2101 = create_index_type (gnu_min, gnu_high,
2102 create_range_type (gnu_index_base_type,
2103 gnu_orig_min,
2104 gnu_orig_max),
2105 gnat_entity);
2107 /* Update the maximum size of the array in elements. */
2108 if (gnu_max_size)
2110 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2111 tree gnu_min
2112 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2113 tree gnu_max
2114 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2115 tree gnu_this_max
2116 = size_binop (PLUS_EXPR, size_one_node,
2117 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2119 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2120 && TREE_OVERFLOW (gnu_this_max))
2121 gnu_max_size = NULL_TREE;
2122 else
2123 gnu_max_size
2124 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2127 TYPE_NAME (gnu_index_types[index])
2128 = create_concat_name (gnat_entity, field_name);
2131 /* Install all the fields into the template. */
2132 TYPE_NAME (gnu_template_type)
2133 = create_concat_name (gnat_entity, "XUB");
2134 gnu_template_fields = NULL_TREE;
2135 for (index = 0; index < ndim; index++)
2136 gnu_template_fields
2137 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2138 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2139 debug_info_p);
2140 TYPE_READONLY (gnu_template_type) = 1;
2142 /* If Component_Size is not already specified, annotate it with the
2143 size of the component. */
2144 if (Unknown_Component_Size (gnat_entity))
2145 Set_Component_Size (gnat_entity,
2146 annotate_value (TYPE_SIZE (comp_type)));
2148 /* Compute the maximum size of the array in units and bits. */
2149 if (gnu_max_size)
2151 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2152 TYPE_SIZE_UNIT (comp_type));
2153 gnu_max_size = size_binop (MULT_EXPR,
2154 convert (bitsizetype, gnu_max_size),
2155 TYPE_SIZE (comp_type));
2157 else
2158 gnu_max_size_unit = NULL_TREE;
2160 /* Now build the array type. */
2161 tem = comp_type;
2162 for (index = ndim - 1; index >= 0; index--)
2164 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2165 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2166 sorry ("non-default Scalar_Storage_Order");
2167 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2168 if (array_type_has_nonaliased_component (tem, gnat_entity))
2169 TYPE_NONALIASED_COMPONENT (tem) = 1;
2172 /* If an alignment is specified, use it if valid. But ignore it
2173 for the original type of packed array types. If the alignment
2174 was requested with an explicit alignment clause, state so. */
2175 if (No (Packed_Array_Impl_Type (gnat_entity))
2176 && Known_Alignment (gnat_entity))
2178 TYPE_ALIGN (tem)
2179 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2180 TYPE_ALIGN (tem));
2181 if (Present (Alignment_Clause (gnat_entity)))
2182 TYPE_USER_ALIGN (tem) = 1;
2185 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2187 if (Treat_As_Volatile (gnat_entity))
2188 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2190 /* Adjust the type of the pointer-to-array field of the fat pointer
2191 and record the aliasing relationships if necessary. */
2192 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2193 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2194 record_component_aliases (gnu_fat_type);
2196 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2197 corresponding fat pointer. */
2198 TREE_TYPE (gnu_type) = gnu_fat_type;
2199 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2200 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2201 SET_TYPE_MODE (gnu_type, BLKmode);
2202 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2204 /* If the maximum size doesn't overflow, use it. */
2205 if (gnu_max_size
2206 && TREE_CODE (gnu_max_size) == INTEGER_CST
2207 && !TREE_OVERFLOW (gnu_max_size)
2208 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2209 && !TREE_OVERFLOW (gnu_max_size_unit))
2211 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2212 TYPE_SIZE (tem));
2213 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2214 TYPE_SIZE_UNIT (tem));
2217 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2218 !Comes_From_Source (gnat_entity), debug_info_p,
2219 gnat_entity);
2221 /* Give the fat pointer type a name. If this is a packed array, tell
2222 the debugger how to interpret the underlying bits. */
2223 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2224 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2225 else
2226 gnat_name = gnat_entity;
2227 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2228 !Comes_From_Source (gnat_entity), debug_info_p,
2229 gnat_entity);
2231 /* Create the type to be designated by thin pointers: a record type for
2232 the array and its template. We used to shift the fields to have the
2233 template at a negative offset, but this was somewhat of a kludge; we
2234 now shift thin pointer values explicitly but only those which have a
2235 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2236 tem = build_unc_object_type (gnu_template_type, tem,
2237 create_concat_name (gnat_name, "XUT"),
2238 debug_info_p);
2240 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2241 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2243 break;
2245 case E_Array_Subtype:
2247 /* This is the actual data type for array variables. Multidimensional
2248 arrays are implemented as arrays of arrays. Note that arrays which
2249 have sparse enumeration subtypes as index components create sparse
2250 arrays, which is obviously space inefficient but so much easier to
2251 code for now.
2253 Also note that the subtype never refers to the unconstrained array
2254 type, which is somewhat at variance with Ada semantics.
2256 First check to see if this is simply a renaming of the array type.
2257 If so, the result is the array type. */
2259 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2260 if (!Is_Constrained (gnat_entity))
2262 else
2264 Entity_Id gnat_index, gnat_base_index;
2265 const bool convention_fortran_p
2266 = (Convention (gnat_entity) == Convention_Fortran);
2267 const int ndim = Number_Dimensions (gnat_entity);
2268 tree gnu_base_type = gnu_type;
2269 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2270 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2271 bool need_index_type_struct = false;
2272 int index;
2274 /* First create the GCC type for each index and find out whether
2275 special types are needed for debugging information. */
2276 for (index = (convention_fortran_p ? ndim - 1 : 0),
2277 gnat_index = First_Index (gnat_entity),
2278 gnat_base_index
2279 = First_Index (Implementation_Base_Type (gnat_entity));
2280 0 <= index && index < ndim;
2281 index += (convention_fortran_p ? - 1 : 1),
2282 gnat_index = Next_Index (gnat_index),
2283 gnat_base_index = Next_Index (gnat_base_index))
2285 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2286 tree gnu_index_base_type = get_base_type (gnu_index_type);
2287 tree gnu_orig_min
2288 = convert (gnu_index_base_type,
2289 TYPE_MIN_VALUE (gnu_index_type));
2290 tree gnu_orig_max
2291 = convert (gnu_index_base_type,
2292 TYPE_MAX_VALUE (gnu_index_type));
2293 tree gnu_min = convert (sizetype, gnu_orig_min);
2294 tree gnu_max = convert (sizetype, gnu_orig_max);
2295 tree gnu_base_index_type
2296 = get_unpadded_type (Etype (gnat_base_index));
2297 tree gnu_base_index_base_type
2298 = get_base_type (gnu_base_index_type);
2299 tree gnu_base_orig_min
2300 = convert (gnu_base_index_base_type,
2301 TYPE_MIN_VALUE (gnu_base_index_type));
2302 tree gnu_base_orig_max
2303 = convert (gnu_base_index_base_type,
2304 TYPE_MAX_VALUE (gnu_base_index_type));
2305 tree gnu_high;
2307 /* See if the base array type is already flat. If it is, we
2308 are probably compiling an ACATS test but it will cause the
2309 code below to malfunction if we don't handle it specially. */
2310 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2311 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2312 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2314 gnu_min = size_one_node;
2315 gnu_max = size_zero_node;
2316 gnu_high = gnu_max;
2319 /* Similarly, if one of the values overflows in sizetype and the
2320 range is null, use 1..0 for the sizetype bounds. */
2321 else if (TREE_CODE (gnu_min) == INTEGER_CST
2322 && TREE_CODE (gnu_max) == INTEGER_CST
2323 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2324 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2326 gnu_min = size_one_node;
2327 gnu_max = size_zero_node;
2328 gnu_high = gnu_max;
2331 /* If the minimum and maximum values both overflow in sizetype,
2332 but the difference in the original type does not overflow in
2333 sizetype, ignore the overflow indication. */
2334 else if (TREE_CODE (gnu_min) == INTEGER_CST
2335 && TREE_CODE (gnu_max) == INTEGER_CST
2336 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2337 && !TREE_OVERFLOW
2338 (convert (sizetype,
2339 fold_build2 (MINUS_EXPR, gnu_index_type,
2340 gnu_orig_max,
2341 gnu_orig_min))))
2343 TREE_OVERFLOW (gnu_min) = 0;
2344 TREE_OVERFLOW (gnu_max) = 0;
2345 gnu_high = gnu_max;
2348 /* Compute the size of this dimension in the general case. We
2349 need to provide GCC with an upper bound to use but have to
2350 deal with the "superflat" case. There are three ways to do
2351 this. If we can prove that the array can never be superflat,
2352 we can just use the high bound of the index type. */
2353 else if ((Nkind (gnat_index) == N_Range
2354 && cannot_be_superflat (gnat_index))
2355 /* Bit-Packed Array Impl. Types are never superflat. */
2356 || (Is_Packed_Array_Impl_Type (gnat_entity)
2357 && Is_Bit_Packed_Array
2358 (Original_Array_Type (gnat_entity))))
2359 gnu_high = gnu_max;
2361 /* Otherwise, if the high bound is constant but the low bound is
2362 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2363 lower bound. Note that the comparison must be done in the
2364 original type to avoid any overflow during the conversion. */
2365 else if (TREE_CODE (gnu_max) == INTEGER_CST
2366 && TREE_CODE (gnu_min) != INTEGER_CST)
2368 gnu_high = gnu_max;
2369 gnu_min
2370 = build_cond_expr (sizetype,
2371 build_binary_op (GE_EXPR,
2372 boolean_type_node,
2373 gnu_orig_max,
2374 gnu_orig_min),
2375 gnu_min,
2376 int_const_binop (PLUS_EXPR, gnu_max,
2377 size_one_node));
2380 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2381 in all the other cases. Note that, here as well as above,
2382 the condition used in the comparison must be equivalent to
2383 the condition (length != 0). This is relied upon in order
2384 to optimize array comparisons in compare_arrays. Moreover
2385 we use int_const_binop for the shift by 1 if the bound is
2386 constant to avoid any unwanted overflow. */
2387 else
2388 gnu_high
2389 = build_cond_expr (sizetype,
2390 build_binary_op (GE_EXPR,
2391 boolean_type_node,
2392 gnu_orig_max,
2393 gnu_orig_min),
2394 gnu_max,
2395 TREE_CODE (gnu_min) == INTEGER_CST
2396 ? int_const_binop (MINUS_EXPR, gnu_min,
2397 size_one_node)
2398 : size_binop (MINUS_EXPR, gnu_min,
2399 size_one_node));
2401 /* Reuse the index type for the range type. Then make an index
2402 type with the size range in sizetype. */
2403 gnu_index_types[index]
2404 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2405 gnat_entity);
2407 /* Update the maximum size of the array in elements. Here we
2408 see if any constraint on the index type of the base type
2409 can be used in the case of self-referential bound on the
2410 index type of the subtype. We look for a non-"infinite"
2411 and non-self-referential bound from any type involved and
2412 handle each bound separately. */
2413 if (gnu_max_size)
2415 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2416 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2417 tree gnu_base_index_base_type
2418 = get_base_type (gnu_base_index_type);
2419 tree gnu_base_base_min
2420 = convert (sizetype,
2421 TYPE_MIN_VALUE (gnu_base_index_base_type));
2422 tree gnu_base_base_max
2423 = convert (sizetype,
2424 TYPE_MAX_VALUE (gnu_base_index_base_type));
2426 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2427 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2428 && !TREE_OVERFLOW (gnu_base_min)))
2429 gnu_base_min = gnu_min;
2431 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2432 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2433 && !TREE_OVERFLOW (gnu_base_max)))
2434 gnu_base_max = gnu_max;
2436 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2437 && TREE_OVERFLOW (gnu_base_min))
2438 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2439 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2440 && TREE_OVERFLOW (gnu_base_max))
2441 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2442 gnu_max_size = NULL_TREE;
2443 else
2445 tree gnu_this_max;
2447 /* Use int_const_binop if the bounds are constant to
2448 avoid any unwanted overflow. */
2449 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2450 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2451 gnu_this_max
2452 = int_const_binop (PLUS_EXPR, size_one_node,
2453 int_const_binop (MINUS_EXPR,
2454 gnu_base_max,
2455 gnu_base_min));
2456 else
2457 gnu_this_max
2458 = size_binop (PLUS_EXPR, size_one_node,
2459 size_binop (MINUS_EXPR,
2460 gnu_base_max,
2461 gnu_base_min));
2463 gnu_max_size
2464 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2468 /* We need special types for debugging information to point to
2469 the index types if they have variable bounds, are not integer
2470 types or are biased. */
2471 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2472 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2473 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2474 || (TREE_TYPE (gnu_index_type)
2475 && TREE_CODE (TREE_TYPE (gnu_index_type))
2476 != INTEGER_TYPE)
2477 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2478 need_index_type_struct = true;
2481 /* Then flatten: create the array of arrays. For an array type
2482 used to implement a packed array, get the component type from
2483 the original array type since the representation clauses that
2484 can affect it are on the latter. */
2485 if (Is_Packed_Array_Impl_Type (gnat_entity)
2486 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2488 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2489 for (index = ndim - 1; index >= 0; index--)
2490 gnu_type = TREE_TYPE (gnu_type);
2492 /* One of the above calls might have caused us to be elaborated,
2493 so don't blow up if so. */
2494 if (present_gnu_tree (gnat_entity))
2496 maybe_present = true;
2497 break;
2500 else
2502 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2503 debug_info_p);
2505 /* One of the above calls might have caused us to be elaborated,
2506 so don't blow up if so. */
2507 if (present_gnu_tree (gnat_entity))
2509 maybe_present = true;
2510 break;
2514 /* Compute the maximum size of the array in units and bits. */
2515 if (gnu_max_size)
2517 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2518 TYPE_SIZE_UNIT (gnu_type));
2519 gnu_max_size = size_binop (MULT_EXPR,
2520 convert (bitsizetype, gnu_max_size),
2521 TYPE_SIZE (gnu_type));
2523 else
2524 gnu_max_size_unit = NULL_TREE;
2526 /* Now build the array type. */
2527 for (index = ndim - 1; index >= 0; index --)
2529 gnu_type = build_nonshared_array_type (gnu_type,
2530 gnu_index_types[index]);
2531 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2532 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2533 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2536 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2537 TYPE_STUB_DECL (gnu_type)
2538 = create_type_stub_decl (gnu_entity_name, gnu_type);
2540 /* If we are at file level and this is a multi-dimensional array,
2541 we need to make a variable corresponding to the stride of the
2542 inner dimensions. */
2543 if (global_bindings_p () && ndim > 1)
2545 tree gnu_arr_type;
2547 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2548 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2549 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2551 tree eltype = TREE_TYPE (gnu_arr_type);
2552 char stride_name[32];
2554 sprintf (stride_name, "ST%d", index);
2555 TYPE_SIZE (gnu_arr_type)
2556 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2557 gnat_entity, stride_name,
2558 definition, false);
2560 /* ??? For now, store the size as a multiple of the
2561 alignment of the element type in bytes so that we
2562 can see the alignment from the tree. */
2563 sprintf (stride_name, "ST%d_A_UNIT", index);
2564 TYPE_SIZE_UNIT (gnu_arr_type)
2565 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2566 gnat_entity, stride_name,
2567 definition, false,
2568 TYPE_ALIGN (eltype));
2570 /* ??? create_type_decl is not invoked on the inner types so
2571 the MULT_EXPR node built above will never be marked. */
2572 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2576 /* If we need to write out a record type giving the names of the
2577 bounds for debugging purposes, do it now and make the record
2578 type a parallel type. This is not needed for a packed array
2579 since the bounds are conveyed by the original array type. */
2580 if (need_index_type_struct
2581 && debug_info_p
2582 && !Is_Packed_Array_Impl_Type (gnat_entity))
2584 tree gnu_bound_rec = make_node (RECORD_TYPE);
2585 tree gnu_field_list = NULL_TREE;
2586 tree gnu_field;
2588 TYPE_NAME (gnu_bound_rec)
2589 = create_concat_name (gnat_entity, "XA");
2591 for (index = ndim - 1; index >= 0; index--)
2593 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2594 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2596 /* Make sure to reference the types themselves, and not just
2597 their names, as the debugger may fall back on them. */
2598 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2599 gnu_bound_rec, NULL_TREE,
2600 NULL_TREE, 0, 0);
2601 DECL_CHAIN (gnu_field) = gnu_field_list;
2602 gnu_field_list = gnu_field;
2605 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2606 add_parallel_type (gnu_type, gnu_bound_rec);
2609 /* If this is a packed array type, make the original array type a
2610 parallel type. Otherwise, do it for the base array type if it
2611 isn't artificial to make sure it is kept in the debug info. */
2612 if (debug_info_p)
2614 if (Is_Packed_Array_Impl_Type (gnat_entity))
2615 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2616 else
2618 tree gnu_base_decl
2619 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2620 if (!DECL_ARTIFICIAL (gnu_base_decl))
2621 add_parallel_type (gnu_type,
2622 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2626 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2627 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2628 = (Is_Packed_Array_Impl_Type (gnat_entity)
2629 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2631 /* If the size is self-referential and the maximum size doesn't
2632 overflow, use it. */
2633 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2634 && gnu_max_size
2635 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2636 && TREE_OVERFLOW (gnu_max_size))
2637 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2638 && TREE_OVERFLOW (gnu_max_size_unit)))
2640 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2641 TYPE_SIZE (gnu_type));
2642 TYPE_SIZE_UNIT (gnu_type)
2643 = size_binop (MIN_EXPR, gnu_max_size_unit,
2644 TYPE_SIZE_UNIT (gnu_type));
2647 /* Set our alias set to that of our base type. This gives all
2648 array subtypes the same alias set. */
2649 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2651 /* If this is a packed type, make this type the same as the packed
2652 array type, but do some adjusting in the type first. */
2653 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2655 Entity_Id gnat_index;
2656 tree gnu_inner;
2658 /* First finish the type we had been making so that we output
2659 debugging information for it. */
2660 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2661 if (Treat_As_Volatile (gnat_entity))
2663 const int quals
2664 = TYPE_QUAL_VOLATILE
2665 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2666 gnu_type = change_qualified_type (gnu_type, quals);
2668 /* Make it artificial only if the base type was artificial too.
2669 That's sort of "morally" true and will make it possible for
2670 the debugger to look it up by name in DWARF, which is needed
2671 in order to decode the packed array type. */
2672 gnu_decl
2673 = create_type_decl (gnu_entity_name, gnu_type,
2674 !Comes_From_Source (Etype (gnat_entity))
2675 && !Comes_From_Source (gnat_entity),
2676 debug_info_p, gnat_entity);
2678 /* Save it as our equivalent in case the call below elaborates
2679 this type again. */
2680 save_gnu_tree (gnat_entity, gnu_decl, false);
2682 gnu_decl
2683 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2684 NULL_TREE, 0);
2685 this_made_decl = true;
2686 gnu_type = TREE_TYPE (gnu_decl);
2687 save_gnu_tree (gnat_entity, NULL_TREE, false);
2689 gnu_inner = gnu_type;
2690 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2691 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2692 || TYPE_PADDING_P (gnu_inner)))
2693 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2695 /* We need to attach the index type to the type we just made so
2696 that the actual bounds can later be put into a template. */
2697 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2698 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2699 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2700 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2702 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2704 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2705 TYPE_MODULUS for modular types so we make an extra
2706 subtype if necessary. */
2707 if (TYPE_MODULAR_P (gnu_inner))
2709 tree gnu_subtype
2710 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2711 TREE_TYPE (gnu_subtype) = gnu_inner;
2712 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2713 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2714 TYPE_MIN_VALUE (gnu_inner));
2715 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2716 TYPE_MAX_VALUE (gnu_inner));
2717 gnu_inner = gnu_subtype;
2720 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2722 #ifdef ENABLE_CHECKING
2723 /* Check for other cases of overloading. */
2724 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2725 #endif
2728 for (gnat_index = First_Index (gnat_entity);
2729 Present (gnat_index);
2730 gnat_index = Next_Index (gnat_index))
2731 SET_TYPE_ACTUAL_BOUNDS
2732 (gnu_inner,
2733 tree_cons (NULL_TREE,
2734 get_unpadded_type (Etype (gnat_index)),
2735 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2737 if (Convention (gnat_entity) != Convention_Fortran)
2738 SET_TYPE_ACTUAL_BOUNDS
2739 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2741 if (TREE_CODE (gnu_type) == RECORD_TYPE
2742 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2743 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2747 else
2748 /* Abort if packed array with no Packed_Array_Impl_Type. */
2749 gcc_assert (!Is_Packed (gnat_entity));
2751 break;
2753 case E_String_Literal_Subtype:
2754 /* Create the type for a string literal. */
2756 Entity_Id gnat_full_type
2757 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2758 && Present (Full_View (Etype (gnat_entity)))
2759 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2760 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2761 tree gnu_string_array_type
2762 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2763 tree gnu_string_index_type
2764 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2765 (TYPE_DOMAIN (gnu_string_array_type))));
2766 tree gnu_lower_bound
2767 = convert (gnu_string_index_type,
2768 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2769 tree gnu_length
2770 = UI_To_gnu (String_Literal_Length (gnat_entity),
2771 gnu_string_index_type);
2772 tree gnu_upper_bound
2773 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2774 gnu_lower_bound,
2775 int_const_binop (MINUS_EXPR, gnu_length,
2776 convert (gnu_string_index_type,
2777 integer_one_node)));
2778 tree gnu_index_type
2779 = create_index_type (convert (sizetype, gnu_lower_bound),
2780 convert (sizetype, gnu_upper_bound),
2781 create_range_type (gnu_string_index_type,
2782 gnu_lower_bound,
2783 gnu_upper_bound),
2784 gnat_entity);
2786 gnu_type
2787 = build_nonshared_array_type (gnat_to_gnu_type
2788 (Component_Type (gnat_entity)),
2789 gnu_index_type);
2790 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2791 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2792 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2794 break;
2796 /* Record Types and Subtypes
2798 The following fields are defined on record types:
2800 Has_Discriminants True if the record has discriminants
2801 First_Discriminant Points to head of list of discriminants
2802 First_Entity Points to head of list of fields
2803 Is_Tagged_Type True if the record is tagged
2805 Implementation of Ada records and discriminated records:
2807 A record type definition is transformed into the equivalent of a C
2808 struct definition. The fields that are the discriminants which are
2809 found in the Full_Type_Declaration node and the elements of the
2810 Component_List found in the Record_Type_Definition node. The
2811 Component_List can be a recursive structure since each Variant of
2812 the Variant_Part of the Component_List has a Component_List.
2814 Processing of a record type definition comprises starting the list of
2815 field declarations here from the discriminants and the calling the
2816 function components_to_record to add the rest of the fields from the
2817 component list and return the gnu type node. The function
2818 components_to_record will call itself recursively as it traverses
2819 the tree. */
2821 case E_Record_Type:
2822 if (Has_Complex_Representation (gnat_entity))
2824 gnu_type
2825 = build_complex_type
2826 (get_unpadded_type
2827 (Etype (Defining_Entity
2828 (First (Component_Items
2829 (Component_List
2830 (Type_Definition
2831 (Declaration_Node (gnat_entity)))))))));
2833 break;
2837 Node_Id full_definition = Declaration_Node (gnat_entity);
2838 Node_Id record_definition = Type_Definition (full_definition);
2839 Node_Id gnat_constr;
2840 Entity_Id gnat_field;
2841 tree gnu_field, gnu_field_list = NULL_TREE;
2842 tree gnu_get_parent;
2843 /* Set PACKED in keeping with gnat_to_gnu_field. */
2844 const int packed
2845 = Is_Packed (gnat_entity)
2847 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2848 ? -1
2849 : (Known_Alignment (gnat_entity)
2850 || (Strict_Alignment (gnat_entity)
2851 && Known_RM_Size (gnat_entity)))
2852 ? -2
2853 : 0;
2854 const bool has_discr = Has_Discriminants (gnat_entity);
2855 const bool has_rep = Has_Specified_Layout (gnat_entity);
2856 const bool is_extension
2857 = (Is_Tagged_Type (gnat_entity)
2858 && Nkind (record_definition) == N_Derived_Type_Definition);
2859 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2860 bool all_rep = has_rep;
2862 /* See if all fields have a rep clause. Stop when we find one
2863 that doesn't. */
2864 if (all_rep)
2865 for (gnat_field = First_Entity (gnat_entity);
2866 Present (gnat_field);
2867 gnat_field = Next_Entity (gnat_field))
2868 if ((Ekind (gnat_field) == E_Component
2869 || Ekind (gnat_field) == E_Discriminant)
2870 && No (Component_Clause (gnat_field)))
2872 all_rep = false;
2873 break;
2876 /* If this is a record extension, go a level further to find the
2877 record definition. Also, verify we have a Parent_Subtype. */
2878 if (is_extension)
2880 if (!type_annotate_only
2881 || Present (Record_Extension_Part (record_definition)))
2882 record_definition = Record_Extension_Part (record_definition);
2884 gcc_assert (type_annotate_only
2885 || Present (Parent_Subtype (gnat_entity)));
2888 /* Make a node for the record. If we are not defining the record,
2889 suppress expanding incomplete types. */
2890 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2891 TYPE_NAME (gnu_type) = gnu_entity_name;
2892 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2893 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2894 sorry ("non-default Scalar_Storage_Order");
2895 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2897 if (!definition)
2899 defer_incomplete_level++;
2900 this_deferred = true;
2903 /* If both a size and rep clause was specified, put the size in
2904 the record type now so that it can get the proper mode. */
2905 if (has_rep && Known_RM_Size (gnat_entity))
2906 TYPE_SIZE (gnu_type)
2907 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2909 /* Always set the alignment here so that it can be used to
2910 set the mode, if it is making the alignment stricter. If
2911 it is invalid, it will be checked again below. If this is to
2912 be Atomic, choose a default alignment of a word unless we know
2913 the size and it's smaller. */
2914 if (Known_Alignment (gnat_entity))
2915 TYPE_ALIGN (gnu_type)
2916 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2917 else if (Is_Atomic_Or_VFA (gnat_entity) && Known_Esize (gnat_entity))
2919 unsigned int size = UI_To_Int (Esize (gnat_entity));
2920 TYPE_ALIGN (gnu_type)
2921 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2923 /* If a type needs strict alignment, the minimum size will be the
2924 type size instead of the RM size (see validate_size). Cap the
2925 alignment, lest it causes this type size to become too large. */
2926 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2928 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2929 unsigned int raw_align = raw_size & -raw_size;
2930 if (raw_align < BIGGEST_ALIGNMENT)
2931 TYPE_ALIGN (gnu_type) = raw_align;
2933 else
2934 TYPE_ALIGN (gnu_type) = 0;
2936 /* If we have a Parent_Subtype, make a field for the parent. If
2937 this record has rep clauses, force the position to zero. */
2938 if (Present (Parent_Subtype (gnat_entity)))
2940 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2941 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
2942 tree gnu_parent;
2944 /* A major complexity here is that the parent subtype will
2945 reference our discriminants in its Stored_Constraint list.
2946 But those must reference the parent component of this record
2947 which is precisely of the parent subtype we have not built yet!
2948 To break the circle we first build a dummy COMPONENT_REF which
2949 represents the "get to the parent" operation and initialize
2950 each of those discriminants to a COMPONENT_REF of the above
2951 dummy parent referencing the corresponding discriminant of the
2952 base type of the parent subtype. */
2953 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
2954 build0 (PLACEHOLDER_EXPR, gnu_type),
2955 build_decl (input_location,
2956 FIELD_DECL, NULL_TREE,
2957 gnu_dummy_parent_type),
2958 NULL_TREE);
2960 if (has_discr)
2961 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2962 Present (gnat_field);
2963 gnat_field = Next_Stored_Discriminant (gnat_field))
2964 if (Present (Corresponding_Discriminant (gnat_field)))
2966 tree gnu_field
2967 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2968 (gnat_field));
2969 save_gnu_tree
2970 (gnat_field,
2971 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2972 gnu_get_parent, gnu_field, NULL_TREE),
2973 true);
2976 /* Then we build the parent subtype. If it has discriminants but
2977 the type itself has unknown discriminants, this means that it
2978 doesn't contain information about how the discriminants are
2979 derived from those of the ancestor type, so it cannot be used
2980 directly. Instead it is built by cloning the parent subtype
2981 of the underlying record view of the type, for which the above
2982 derivation of discriminants has been made explicit. */
2983 if (Has_Discriminants (gnat_parent)
2984 && Has_Unknown_Discriminants (gnat_entity))
2986 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2988 /* If we are defining the type, the underlying record
2989 view must already have been elaborated at this point.
2990 Otherwise do it now as its parent subtype cannot be
2991 technically elaborated on its own. */
2992 if (definition)
2993 gcc_assert (present_gnu_tree (gnat_uview));
2994 else
2995 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2997 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2999 /* Substitute the "get to the parent" of the type for that
3000 of its underlying record view in the cloned type. */
3001 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3002 Present (gnat_field);
3003 gnat_field = Next_Stored_Discriminant (gnat_field))
3004 if (Present (Corresponding_Discriminant (gnat_field)))
3006 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3007 tree gnu_ref
3008 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3009 gnu_get_parent, gnu_field, NULL_TREE);
3010 gnu_parent
3011 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3014 else
3015 gnu_parent = gnat_to_gnu_type (gnat_parent);
3017 /* The parent field needs strict alignment so, if it is to
3018 be created with a component clause below, then we need
3019 to apply the same adjustment as in gnat_to_gnu_field. */
3020 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3021 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_parent);
3023 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3024 initially built. The discriminants must reference the fields
3025 of the parent subtype and not those of its base type for the
3026 placeholder machinery to properly work. */
3027 if (has_discr)
3029 /* The actual parent subtype is the full view. */
3030 if (IN (Ekind (gnat_parent), Private_Kind))
3032 if (Present (Full_View (gnat_parent)))
3033 gnat_parent = Full_View (gnat_parent);
3034 else
3035 gnat_parent = Underlying_Full_View (gnat_parent);
3038 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3039 Present (gnat_field);
3040 gnat_field = Next_Stored_Discriminant (gnat_field))
3041 if (Present (Corresponding_Discriminant (gnat_field)))
3043 Entity_Id field;
3044 for (field = First_Stored_Discriminant (gnat_parent);
3045 Present (field);
3046 field = Next_Stored_Discriminant (field))
3047 if (same_discriminant_p (gnat_field, field))
3048 break;
3049 gcc_assert (Present (field));
3050 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3051 = gnat_to_gnu_field_decl (field);
3055 /* The "get to the parent" COMPONENT_REF must be given its
3056 proper type... */
3057 TREE_TYPE (gnu_get_parent) = gnu_parent;
3059 /* ...and reference the _Parent field of this record. */
3060 gnu_field
3061 = create_field_decl (parent_name_id,
3062 gnu_parent, gnu_type,
3063 has_rep
3064 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3065 has_rep
3066 ? bitsize_zero_node : NULL_TREE,
3067 0, 1);
3068 DECL_INTERNAL_P (gnu_field) = 1;
3069 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3070 TYPE_FIELDS (gnu_type) = gnu_field;
3073 /* Make the fields for the discriminants and put them into the record
3074 unless it's an Unchecked_Union. */
3075 if (has_discr)
3076 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3077 Present (gnat_field);
3078 gnat_field = Next_Stored_Discriminant (gnat_field))
3080 /* If this is a record extension and this discriminant is the
3081 renaming of another discriminant, we've handled it above. */
3082 if (Present (Parent_Subtype (gnat_entity))
3083 && Present (Corresponding_Discriminant (gnat_field)))
3084 continue;
3086 gnu_field
3087 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3088 debug_info_p);
3090 /* Make an expression using a PLACEHOLDER_EXPR from the
3091 FIELD_DECL node just created and link that with the
3092 corresponding GNAT defining identifier. */
3093 save_gnu_tree (gnat_field,
3094 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3095 build0 (PLACEHOLDER_EXPR, gnu_type),
3096 gnu_field, NULL_TREE),
3097 true);
3099 if (!is_unchecked_union)
3101 DECL_CHAIN (gnu_field) = gnu_field_list;
3102 gnu_field_list = gnu_field;
3106 /* If we have a derived untagged type that renames discriminants in
3107 the root type, the (stored) discriminants are a just copy of the
3108 discriminants of the root type. This means that any constraints
3109 added by the renaming in the derivation are disregarded as far
3110 as the layout of the derived type is concerned. To rescue them,
3111 we change the type of the (stored) discriminants to a subtype
3112 with the bounds of the type of the visible discriminants. */
3113 if (has_discr
3114 && !is_extension
3115 && Stored_Constraint (gnat_entity) != No_Elist)
3116 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3117 gnat_constr != No_Elmt;
3118 gnat_constr = Next_Elmt (gnat_constr))
3119 if (Nkind (Node (gnat_constr)) == N_Identifier
3120 /* Ignore access discriminants. */
3121 && !Is_Access_Type (Etype (Node (gnat_constr)))
3122 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3124 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3125 tree gnu_discr_type, gnu_ref;
3127 /* If the scope of the discriminant is not the record type,
3128 this means that we're processing the implicit full view
3129 of a type derived from a private discriminated type: in
3130 this case, the Stored_Constraint list is simply copied
3131 from the partial view, see Build_Derived_Private_Type.
3132 So we need to retrieve the corresponding discriminant
3133 of the implicit full view, otherwise we will abort. */
3134 if (Scope (gnat_discr) != gnat_entity)
3136 Entity_Id field;
3137 for (field = First_Entity (gnat_entity);
3138 Present (field);
3139 field = Next_Entity (field))
3140 if (Ekind (field) == E_Discriminant
3141 && same_discriminant_p (gnat_discr, field))
3142 break;
3143 gcc_assert (Present (field));
3144 gnat_discr = field;
3147 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3148 gnu_ref
3149 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3150 NULL_TREE, 0);
3152 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3153 just above for one of the stored discriminants. */
3154 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3156 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3158 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3159 tree gnu_subtype
3160 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3161 ? make_unsigned_type (prec) : make_signed_type (prec);
3162 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3163 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3164 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3165 TYPE_MIN_VALUE (gnu_discr_type));
3166 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3167 TYPE_MAX_VALUE (gnu_discr_type));
3168 TREE_TYPE (gnu_ref)
3169 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3173 /* Add the fields into the record type and finish it up. */
3174 components_to_record (gnu_type, Component_List (record_definition),
3175 gnu_field_list, packed, definition, false,
3176 all_rep, is_unchecked_union,
3177 !Comes_From_Source (gnat_entity), debug_info_p,
3178 false, OK_To_Reorder_Components (gnat_entity),
3179 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3181 /* Fill in locations of fields. */
3182 annotate_rep (gnat_entity, gnu_type);
3184 /* If there are any entities in the chain corresponding to components
3185 that we did not elaborate, ensure we elaborate their types if they
3186 are Itypes. */
3187 for (gnat_temp = First_Entity (gnat_entity);
3188 Present (gnat_temp);
3189 gnat_temp = Next_Entity (gnat_temp))
3190 if ((Ekind (gnat_temp) == E_Component
3191 || Ekind (gnat_temp) == E_Discriminant)
3192 && Is_Itype (Etype (gnat_temp))
3193 && !present_gnu_tree (gnat_temp))
3194 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3196 /* If this is a record type associated with an exception definition,
3197 equate its fields to those of the standard exception type. This
3198 will make it possible to convert between them. */
3199 if (gnu_entity_name == exception_data_name_id)
3201 tree gnu_std_field;
3202 for (gnu_field = TYPE_FIELDS (gnu_type),
3203 gnu_std_field = TYPE_FIELDS (except_type_node);
3204 gnu_field;
3205 gnu_field = DECL_CHAIN (gnu_field),
3206 gnu_std_field = DECL_CHAIN (gnu_std_field))
3207 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3208 gcc_assert (!gnu_std_field);
3211 break;
3213 case E_Class_Wide_Subtype:
3214 /* If an equivalent type is present, that is what we should use.
3215 Otherwise, fall through to handle this like a record subtype
3216 since it may have constraints. */
3217 if (gnat_equiv_type != gnat_entity)
3219 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3220 maybe_present = true;
3221 break;
3224 /* ... fall through ... */
3226 case E_Record_Subtype:
3227 /* If Cloned_Subtype is Present it means this record subtype has
3228 identical layout to that type or subtype and we should use
3229 that GCC type for this one. The front end guarantees that
3230 the component list is shared. */
3231 if (Present (Cloned_Subtype (gnat_entity)))
3233 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3234 NULL_TREE, 0);
3235 maybe_present = true;
3236 break;
3239 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3240 changing the type, make a new type with each field having the type of
3241 the field in the new subtype but the position computed by transforming
3242 every discriminant reference according to the constraints. We don't
3243 see any difference between private and non-private type here since
3244 derivations from types should have been deferred until the completion
3245 of the private type. */
3246 else
3248 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3249 tree gnu_base_type;
3251 if (!definition)
3253 defer_incomplete_level++;
3254 this_deferred = true;
3257 gnu_base_type
3258 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3260 if (present_gnu_tree (gnat_entity))
3262 maybe_present = true;
3263 break;
3266 /* If this is a record subtype associated with a dispatch table,
3267 strip the suffix. This is necessary to make sure 2 different
3268 subtypes associated with the imported and exported views of a
3269 dispatch table are properly merged in LTO mode. */
3270 if (Is_Dispatch_Table_Entity (gnat_entity))
3272 char *p;
3273 Get_Encoded_Name (gnat_entity);
3274 p = strchr (Name_Buffer, '_');
3275 gcc_assert (p);
3276 strcpy (p+2, "dtS");
3277 gnu_entity_name = get_identifier (Name_Buffer);
3280 /* When the subtype has discriminants and these discriminants affect
3281 the initial shape it has inherited, factor them in. But for an
3282 Unchecked_Union (it must be an Itype), just return the type.
3283 We can't just test Is_Constrained because private subtypes without
3284 discriminants of types with discriminants with default expressions
3285 are Is_Constrained but aren't constrained! */
3286 if (IN (Ekind (gnat_base_type), Record_Kind)
3287 && !Is_Unchecked_Union (gnat_base_type)
3288 && !Is_For_Access_Subtype (gnat_entity)
3289 && Has_Discriminants (gnat_entity)
3290 && Is_Constrained (gnat_entity)
3291 && Stored_Constraint (gnat_entity) != No_Elist)
3293 vec<subst_pair> gnu_subst_list
3294 = build_subst_list (gnat_entity, gnat_base_type, definition);
3295 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3296 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3297 bool selected_variant = false, all_constant_pos = true;
3298 Entity_Id gnat_field;
3299 vec<variant_desc> gnu_variant_list;
3301 gnu_type = make_node (RECORD_TYPE);
3302 TYPE_NAME (gnu_type) = gnu_entity_name;
3303 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3304 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3306 /* Set the size, alignment and alias set of the new type to
3307 match that of the old one, doing required substitutions. */
3308 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3309 gnu_subst_list);
3311 if (TYPE_IS_PADDING_P (gnu_base_type))
3312 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3313 else
3314 gnu_unpad_base_type = gnu_base_type;
3316 /* Look for REP and variant parts in the base type. */
3317 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3318 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3320 /* If there is a variant part, we must compute whether the
3321 constraints statically select a particular variant. If
3322 so, we simply drop the qualified union and flatten the
3323 list of fields. Otherwise we'll build a new qualified
3324 union for the variants that are still relevant. */
3325 if (gnu_variant_part)
3327 variant_desc *v;
3328 unsigned int i;
3330 gnu_variant_list
3331 = build_variant_list (TREE_TYPE (gnu_variant_part),
3332 gnu_subst_list,
3333 vNULL);
3335 /* If all the qualifiers are unconditionally true, the
3336 innermost variant is statically selected. */
3337 selected_variant = true;
3338 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3339 if (!integer_onep (v->qual))
3341 selected_variant = false;
3342 break;
3345 /* Otherwise, create the new variants. */
3346 if (!selected_variant)
3347 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3349 tree old_variant = v->type;
3350 tree new_variant = make_node (RECORD_TYPE);
3351 tree suffix
3352 = concat_name (DECL_NAME (gnu_variant_part),
3353 IDENTIFIER_POINTER
3354 (DECL_NAME (v->field)));
3355 TYPE_NAME (new_variant)
3356 = concat_name (TYPE_NAME (gnu_type),
3357 IDENTIFIER_POINTER (suffix));
3358 copy_and_substitute_in_size (new_variant, old_variant,
3359 gnu_subst_list);
3360 v->new_type = new_variant;
3363 else
3365 gnu_variant_list.create (0);
3366 selected_variant = false;
3369 /* Make a list of fields and their position in the base type. */
3370 gnu_pos_list
3371 = build_position_list (gnu_unpad_base_type,
3372 gnu_variant_list.exists ()
3373 && !selected_variant,
3374 size_zero_node, bitsize_zero_node,
3375 BIGGEST_ALIGNMENT, NULL_TREE);
3377 /* Now go down every component in the subtype and compute its
3378 size and position from those of the component in the base
3379 type and from the constraints of the subtype. */
3380 for (gnat_field = First_Entity (gnat_entity);
3381 Present (gnat_field);
3382 gnat_field = Next_Entity (gnat_field))
3383 if ((Ekind (gnat_field) == E_Component
3384 || Ekind (gnat_field) == E_Discriminant)
3385 && !(Present (Corresponding_Discriminant (gnat_field))
3386 && Is_Tagged_Type (gnat_base_type))
3387 && Underlying_Type
3388 (Scope (Original_Record_Component (gnat_field)))
3389 == gnat_base_type)
3391 Name_Id gnat_name = Chars (gnat_field);
3392 Entity_Id gnat_old_field
3393 = Original_Record_Component (gnat_field);
3394 tree gnu_old_field
3395 = gnat_to_gnu_field_decl (gnat_old_field);
3396 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3397 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3398 tree gnu_cont_type, gnu_last = NULL_TREE;
3400 /* If the type is the same, retrieve the GCC type from the
3401 old field to take into account possible adjustments. */
3402 if (Etype (gnat_field) == Etype (gnat_old_field))
3403 gnu_field_type = TREE_TYPE (gnu_old_field);
3404 else
3405 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3407 /* If there was a component clause, the field types must be
3408 the same for the type and subtype, so copy the data from
3409 the old field to avoid recomputation here. Also if the
3410 field is justified modular and the optimization in
3411 gnat_to_gnu_field was applied. */
3412 if (Present (Component_Clause (gnat_old_field))
3413 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3414 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3415 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3416 == TREE_TYPE (gnu_old_field)))
3418 gnu_size = DECL_SIZE (gnu_old_field);
3419 gnu_field_type = TREE_TYPE (gnu_old_field);
3422 /* If the old field was packed and of constant size, we
3423 have to get the old size here, as it might differ from
3424 what the Etype conveys and the latter might overlap
3425 onto the following field. Try to arrange the type for
3426 possible better packing along the way. */
3427 else if (DECL_PACKED (gnu_old_field)
3428 && TREE_CODE (DECL_SIZE (gnu_old_field))
3429 == INTEGER_CST)
3431 gnu_size = DECL_SIZE (gnu_old_field);
3432 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3433 && !TYPE_FAT_POINTER_P (gnu_field_type)
3434 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3435 gnu_field_type
3436 = make_packable_type (gnu_field_type, true);
3439 else
3440 gnu_size = TYPE_SIZE (gnu_field_type);
3442 /* If the context of the old field is the base type or its
3443 REP part (if any), put the field directly in the new
3444 type; otherwise look up the context in the variant list
3445 and put the field either in the new type if there is a
3446 selected variant or in one of the new variants. */
3447 if (gnu_context == gnu_unpad_base_type
3448 || (gnu_rep_part
3449 && gnu_context == TREE_TYPE (gnu_rep_part)))
3450 gnu_cont_type = gnu_type;
3451 else
3453 variant_desc *v;
3454 unsigned int i;
3455 tree rep_part;
3457 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3458 if (gnu_context == v->type
3459 || ((rep_part = get_rep_part (v->type))
3460 && gnu_context == TREE_TYPE (rep_part)))
3461 break;
3462 if (v)
3464 if (selected_variant)
3465 gnu_cont_type = gnu_type;
3466 else
3467 gnu_cont_type = v->new_type;
3469 else
3470 /* The front-end may pass us "ghost" components if
3471 it fails to recognize that a constrained subtype
3472 is statically constrained. Discard them. */
3473 continue;
3476 /* Now create the new field modeled on the old one. */
3477 gnu_field
3478 = create_field_decl_from (gnu_old_field, gnu_field_type,
3479 gnu_cont_type, gnu_size,
3480 gnu_pos_list, gnu_subst_list);
3481 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3483 /* Put it in one of the new variants directly. */
3484 if (gnu_cont_type != gnu_type)
3486 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3487 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3490 /* To match the layout crafted in components_to_record,
3491 if this is the _Tag or _Parent field, put it before
3492 any other fields. */
3493 else if (gnat_name == Name_uTag
3494 || gnat_name == Name_uParent)
3495 gnu_field_list = chainon (gnu_field_list, gnu_field);
3497 /* Similarly, if this is the _Controller field, put
3498 it before the other fields except for the _Tag or
3499 _Parent field. */
3500 else if (gnat_name == Name_uController && gnu_last)
3502 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3503 DECL_CHAIN (gnu_last) = gnu_field;
3506 /* Otherwise, if this is a regular field, put it after
3507 the other fields. */
3508 else
3510 DECL_CHAIN (gnu_field) = gnu_field_list;
3511 gnu_field_list = gnu_field;
3512 if (!gnu_last)
3513 gnu_last = gnu_field;
3514 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3515 all_constant_pos = false;
3518 save_gnu_tree (gnat_field, gnu_field, false);
3521 /* If there is a variant list, a selected variant and the fields
3522 all have a constant position, put them in order of increasing
3523 position to match that of constant CONSTRUCTORs. Likewise if
3524 there is no variant list but a REP part, since the latter has
3525 been flattened in the process. */
3526 if (((gnu_variant_list.exists () && selected_variant)
3527 || (!gnu_variant_list.exists () && gnu_rep_part))
3528 && all_constant_pos)
3530 const int len = list_length (gnu_field_list);
3531 tree *field_arr = XALLOCAVEC (tree, len), t;
3532 int i;
3534 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3535 field_arr[i] = t;
3537 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3539 gnu_field_list = NULL_TREE;
3540 for (i = 0; i < len; i++)
3542 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3543 gnu_field_list = field_arr[i];
3547 /* If there is a variant list and no selected variant, we need
3548 to create the nest of variant parts from the old nest. */
3549 else if (gnu_variant_list.exists () && !selected_variant)
3551 tree new_variant_part
3552 = create_variant_part_from (gnu_variant_part,
3553 gnu_variant_list, gnu_type,
3554 gnu_pos_list, gnu_subst_list);
3555 DECL_CHAIN (new_variant_part) = gnu_field_list;
3556 gnu_field_list = new_variant_part;
3559 /* Now go through the entities again looking for Itypes that
3560 we have not elaborated but should (e.g., Etypes of fields
3561 that have Original_Components). */
3562 for (gnat_field = First_Entity (gnat_entity);
3563 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3564 if ((Ekind (gnat_field) == E_Discriminant
3565 || Ekind (gnat_field) == E_Component)
3566 && !present_gnu_tree (Etype (gnat_field)))
3567 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3569 /* Do not emit debug info for the type yet since we're going to
3570 modify it below. */
3571 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3572 false);
3573 compute_record_mode (gnu_type);
3575 /* Fill in locations of fields. */
3576 annotate_rep (gnat_entity, gnu_type);
3578 /* If debugging information is being written for the type, write
3579 a record that shows what we are a subtype of and also make a
3580 variable that indicates our size, if still variable. */
3581 if (debug_info_p)
3583 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3584 tree gnu_unpad_base_name
3585 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3586 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3588 TYPE_NAME (gnu_subtype_marker)
3589 = create_concat_name (gnat_entity, "XVS");
3590 finish_record_type (gnu_subtype_marker,
3591 create_field_decl (gnu_unpad_base_name,
3592 build_reference_type
3593 (gnu_unpad_base_type),
3594 gnu_subtype_marker,
3595 NULL_TREE, NULL_TREE,
3596 0, 0),
3597 0, true);
3599 add_parallel_type (gnu_type, gnu_subtype_marker);
3601 if (definition
3602 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3603 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3604 TYPE_SIZE_UNIT (gnu_subtype_marker)
3605 = create_var_decl (create_concat_name (gnat_entity,
3606 "XVZ"),
3607 NULL_TREE, sizetype, gnu_size_unit,
3608 false, false, false, false, NULL,
3609 gnat_entity);
3612 gnu_variant_list.release ();
3613 gnu_subst_list.release ();
3615 /* Now we can finalize it. */
3616 rest_of_record_type_compilation (gnu_type);
3619 /* Otherwise, go down all the components in the new type and make
3620 them equivalent to those in the base type. */
3621 else
3623 gnu_type = gnu_base_type;
3625 for (gnat_temp = First_Entity (gnat_entity);
3626 Present (gnat_temp);
3627 gnat_temp = Next_Entity (gnat_temp))
3628 if ((Ekind (gnat_temp) == E_Discriminant
3629 && !Is_Unchecked_Union (gnat_base_type))
3630 || Ekind (gnat_temp) == E_Component)
3631 save_gnu_tree (gnat_temp,
3632 gnat_to_gnu_field_decl
3633 (Original_Record_Component (gnat_temp)),
3634 false);
3637 break;
3639 case E_Access_Subprogram_Type:
3640 /* Use the special descriptor type for dispatch tables if needed,
3641 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3642 Note that we are only required to do so for static tables in
3643 order to be compatible with the C++ ABI, but Ada 2005 allows
3644 to extend library level tagged types at the local level so
3645 we do it in the non-static case as well. */
3646 if (TARGET_VTABLE_USES_DESCRIPTORS
3647 && Is_Dispatch_Table_Entity (gnat_entity))
3649 gnu_type = fdesc_type_node;
3650 gnu_size = TYPE_SIZE (gnu_type);
3651 break;
3654 /* ... fall through ... */
3656 case E_Anonymous_Access_Subprogram_Type:
3657 /* If we are not defining this entity, and we have incomplete
3658 entities being processed above us, make a dummy type and
3659 fill it in later. */
3660 if (!definition && defer_incomplete_level != 0)
3662 struct incomplete *p = XNEW (struct incomplete);
3664 gnu_type
3665 = build_pointer_type
3666 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3667 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3668 !Comes_From_Source (gnat_entity),
3669 debug_info_p, gnat_entity);
3670 this_made_decl = true;
3671 gnu_type = TREE_TYPE (gnu_decl);
3672 save_gnu_tree (gnat_entity, gnu_decl, false);
3673 saved = true;
3675 p->old_type = TREE_TYPE (gnu_type);
3676 p->full_type = Directly_Designated_Type (gnat_entity);
3677 p->next = defer_incomplete_list;
3678 defer_incomplete_list = p;
3679 break;
3682 /* ... fall through ... */
3684 case E_Allocator_Type:
3685 case E_Access_Type:
3686 case E_Access_Attribute_Type:
3687 case E_Anonymous_Access_Type:
3688 case E_General_Access_Type:
3690 /* The designated type and its equivalent type for gigi. */
3691 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3692 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3693 /* Whether it comes from a limited with. */
3694 bool is_from_limited_with
3695 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3696 && From_Limited_With (gnat_desig_equiv));
3697 /* The "full view" of the designated type. If this is an incomplete
3698 entity from a limited with, treat its non-limited view as the full
3699 view. Otherwise, if this is an incomplete or private type, use the
3700 full view. In the former case, we might point to a private type,
3701 in which case, we need its full view. Also, we want to look at the
3702 actual type used for the representation, so this takes a total of
3703 three steps. */
3704 Entity_Id gnat_desig_full_direct_first
3705 = (is_from_limited_with
3706 ? Non_Limited_View (gnat_desig_equiv)
3707 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3708 ? Full_View (gnat_desig_equiv) : Empty));
3709 Entity_Id gnat_desig_full_direct
3710 = ((is_from_limited_with
3711 && Present (gnat_desig_full_direct_first)
3712 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3713 ? Full_View (gnat_desig_full_direct_first)
3714 : gnat_desig_full_direct_first);
3715 Entity_Id gnat_desig_full
3716 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3717 /* The type actually used to represent the designated type, either
3718 gnat_desig_full or gnat_desig_equiv. */
3719 Entity_Id gnat_desig_rep;
3720 /* True if this is a pointer to an unconstrained array. */
3721 bool is_unconstrained_array;
3722 /* We want to know if we'll be seeing the freeze node for any
3723 incomplete type we may be pointing to. */
3724 bool in_main_unit
3725 = (Present (gnat_desig_full)
3726 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3727 : In_Extended_Main_Code_Unit (gnat_desig_type));
3728 /* True if we make a dummy type here. */
3729 bool made_dummy = false;
3730 /* The mode to be used for the pointer type. */
3731 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3732 /* The GCC type used for the designated type. */
3733 tree gnu_desig_type = NULL_TREE;
3735 if (!targetm.valid_pointer_mode (p_mode))
3736 p_mode = ptr_mode;
3738 /* If either the designated type or its full view is an unconstrained
3739 array subtype, replace it with the type it's a subtype of. This
3740 avoids problems with multiple copies of unconstrained array types.
3741 Likewise, if the designated type is a subtype of an incomplete
3742 record type, use the parent type to avoid order of elaboration
3743 issues. This can lose some code efficiency, but there is no
3744 alternative. */
3745 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3746 && !Is_Constrained (gnat_desig_equiv))
3747 gnat_desig_equiv = Etype (gnat_desig_equiv);
3748 if (Present (gnat_desig_full)
3749 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3750 && !Is_Constrained (gnat_desig_full))
3751 || (Ekind (gnat_desig_full) == E_Record_Subtype
3752 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3753 gnat_desig_full = Etype (gnat_desig_full);
3755 /* Set the type that's actually the representation of the designated
3756 type and also flag whether we have a unconstrained array. */
3757 gnat_desig_rep
3758 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3759 is_unconstrained_array
3760 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3762 /* If we are pointing to an incomplete type whose completion is an
3763 unconstrained array, make dummy fat and thin pointer types to it.
3764 Likewise if the type itself is dummy or an unconstrained array. */
3765 if (is_unconstrained_array
3766 && (Present (gnat_desig_full)
3767 || (present_gnu_tree (gnat_desig_equiv)
3768 && TYPE_IS_DUMMY_P
3769 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3770 || (!in_main_unit
3771 && defer_incomplete_level != 0
3772 && !present_gnu_tree (gnat_desig_equiv))
3773 || (in_main_unit
3774 && is_from_limited_with
3775 && Present (Freeze_Node (gnat_desig_equiv)))))
3777 if (present_gnu_tree (gnat_desig_rep))
3778 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3779 else
3781 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3782 made_dummy = true;
3785 /* If the call above got something that has a pointer, the pointer
3786 is our type. This could have happened either because the type
3787 was elaborated or because somebody else executed the code. */
3788 if (!TYPE_POINTER_TO (gnu_desig_type))
3789 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3790 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3793 /* If we already know what the full type is, use it. */
3794 else if (Present (gnat_desig_full)
3795 && present_gnu_tree (gnat_desig_full))
3796 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3798 /* Get the type of the thing we are to point to and build a pointer to
3799 it. If it is a reference to an incomplete or private type with a
3800 full view that is a record, make a dummy type node and get the
3801 actual type later when we have verified it is safe. */
3802 else if ((!in_main_unit
3803 && !present_gnu_tree (gnat_desig_equiv)
3804 && Present (gnat_desig_full)
3805 && !present_gnu_tree (gnat_desig_full)
3806 && Is_Record_Type (gnat_desig_full))
3807 /* Likewise if we are pointing to a record or array and we are
3808 to defer elaborating incomplete types. We do this as this
3809 access type may be the full view of a private type. Note
3810 that the unconstrained array case is handled above. */
3811 || ((!in_main_unit || imported_p)
3812 && defer_incomplete_level != 0
3813 && !present_gnu_tree (gnat_desig_equiv)
3814 && (Is_Record_Type (gnat_desig_rep)
3815 || Is_Array_Type (gnat_desig_rep)))
3816 /* If this is a reference from a limited_with type back to our
3817 main unit and there's a freeze node for it, either we have
3818 already processed the declaration and made the dummy type,
3819 in which case we just reuse the latter, or we have not yet,
3820 in which case we make the dummy type and it will be reused
3821 when the declaration is finally processed. In both cases,
3822 the pointer eventually created below will be automatically
3823 adjusted when the freeze node is processed. Note that the
3824 unconstrained array case is handled above. */
3825 || (in_main_unit
3826 && is_from_limited_with
3827 && Present (Freeze_Node (gnat_desig_rep))))
3829 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3830 made_dummy = true;
3833 /* Otherwise handle the case of a pointer to itself. */
3834 else if (gnat_desig_equiv == gnat_entity)
3836 gnu_type
3837 = build_pointer_type_for_mode (void_type_node, p_mode,
3838 No_Strict_Aliasing (gnat_entity));
3839 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3842 /* If expansion is disabled, the equivalent type of a concurrent type
3843 is absent, so build a dummy pointer type. */
3844 else if (type_annotate_only && No (gnat_desig_equiv))
3845 gnu_type = ptr_type_node;
3847 /* Finally, handle the default case where we can just elaborate our
3848 designated type. */
3849 else
3850 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3852 /* It is possible that a call to gnat_to_gnu_type above resolved our
3853 type. If so, just return it. */
3854 if (present_gnu_tree (gnat_entity))
3856 maybe_present = true;
3857 break;
3860 /* If we haven't done it yet, build the pointer type the usual way. */
3861 if (!gnu_type)
3863 /* Modify the designated type if we are pointing only to constant
3864 objects, but don't do it for unconstrained arrays. */
3865 if (Is_Access_Constant (gnat_entity)
3866 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3868 gnu_desig_type
3869 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3871 /* Some extra processing is required if we are building a
3872 pointer to an incomplete type (in the GCC sense). We might
3873 have such a type if we just made a dummy, or directly out
3874 of the call to gnat_to_gnu_type above if we are processing
3875 an access type for a record component designating the
3876 record type itself. */
3877 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3879 /* We must ensure that the pointer to variant we make will
3880 be processed by update_pointer_to when the initial type
3881 is completed. Pretend we made a dummy and let further
3882 processing act as usual. */
3883 made_dummy = true;
3885 /* We must ensure that update_pointer_to will not retrieve
3886 the dummy variant when building a properly qualified
3887 version of the complete type. We take advantage of the
3888 fact that get_qualified_type is requiring TYPE_NAMEs to
3889 match to influence build_qualified_type and then also
3890 update_pointer_to here. */
3891 TYPE_NAME (gnu_desig_type)
3892 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3896 gnu_type
3897 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3898 No_Strict_Aliasing (gnat_entity));
3901 /* If we are not defining this object and we have made a dummy pointer,
3902 save our current definition, evaluate the actual type, and replace
3903 the tentative type we made with the actual one. If we are to defer
3904 actually looking up the actual type, make an entry in the deferred
3905 list. If this is from a limited with, we may have to defer to the
3906 end of the current unit. */
3907 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3909 tree gnu_old_desig_type;
3911 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3913 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3914 if (esize == POINTER_SIZE)
3915 gnu_type = build_pointer_type
3916 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3918 else
3919 gnu_old_desig_type = TREE_TYPE (gnu_type);
3921 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3922 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3923 !Comes_From_Source (gnat_entity),
3924 debug_info_p, gnat_entity);
3925 this_made_decl = true;
3926 gnu_type = TREE_TYPE (gnu_decl);
3927 save_gnu_tree (gnat_entity, gnu_decl, false);
3928 saved = true;
3930 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3931 update gnu_old_desig_type directly, in which case it will not be
3932 a dummy type any more when we get into update_pointer_to.
3934 This can happen e.g. when the designated type is a record type,
3935 because their elaboration starts with an initial node from
3936 make_dummy_type, which may be the same node as the one we got.
3938 Besides, variants of this non-dummy type might have been created
3939 along the way. update_pointer_to is expected to properly take
3940 care of those situations. */
3941 if (defer_incomplete_level == 0 && !is_from_limited_with)
3943 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3944 gnat_to_gnu_type (gnat_desig_equiv));
3946 else
3948 struct incomplete *p = XNEW (struct incomplete);
3949 struct incomplete **head
3950 = (is_from_limited_with
3951 ? &defer_limited_with : &defer_incomplete_list);
3952 p->old_type = gnu_old_desig_type;
3953 p->full_type = gnat_desig_equiv;
3954 p->next = *head;
3955 *head = p;
3959 break;
3961 case E_Access_Protected_Subprogram_Type:
3962 case E_Anonymous_Access_Protected_Subprogram_Type:
3963 if (type_annotate_only && No (gnat_equiv_type))
3964 gnu_type = ptr_type_node;
3965 else
3967 /* The run-time representation is the equivalent type. */
3968 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3969 maybe_present = true;
3972 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3973 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3974 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3975 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3976 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3977 NULL_TREE, 0);
3979 break;
3981 case E_Access_Subtype:
3983 /* We treat this as identical to its base type; any constraint is
3984 meaningful only to the front-end.
3986 The designated type must be elaborated as well, if it does
3987 not have its own freeze node. Designated (sub)types created
3988 for constrained components of records with discriminants are
3989 not frozen by the front-end and thus not elaborated by gigi,
3990 because their use may appear before the base type is frozen,
3991 and because it is not clear that they are needed anywhere in
3992 gigi. With the current model, there is no correct place where
3993 they could be elaborated. */
3995 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3996 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3997 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3998 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3999 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4001 /* If we are not defining this entity, and we have incomplete
4002 entities being processed above us, make a dummy type and
4003 elaborate it later. */
4004 if (!definition && defer_incomplete_level != 0)
4006 struct incomplete *p = XNEW (struct incomplete);
4008 p->old_type
4009 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4010 p->full_type = Directly_Designated_Type (gnat_entity);
4011 p->next = defer_incomplete_list;
4012 defer_incomplete_list = p;
4014 else if (!IN (Ekind (Base_Type
4015 (Directly_Designated_Type (gnat_entity))),
4016 Incomplete_Or_Private_Kind))
4017 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4018 NULL_TREE, 0);
4021 maybe_present = true;
4022 break;
4024 /* Subprogram Entities
4026 The following access functions are defined for subprograms:
4028 Etype Return type or Standard_Void_Type.
4029 First_Formal The first formal parameter.
4030 Is_Imported Indicates that the subprogram has appeared in
4031 an INTERFACE or IMPORT pragma. For now we
4032 assume that the external language is C.
4033 Is_Exported Likewise but for an EXPORT pragma.
4034 Is_Inlined True if the subprogram is to be inlined.
4036 Each parameter is first checked by calling must_pass_by_ref on its
4037 type to determine if it is passed by reference. For parameters which
4038 are copied in, if they are Ada In Out or Out parameters, their return
4039 value becomes part of a record which becomes the return type of the
4040 function (C function - note that this applies only to Ada procedures
4041 so there is no Ada return type). Additional code to store back the
4042 parameters will be generated on the caller side. This transformation
4043 is done here, not in the front-end.
4045 The intended result of the transformation can be seen from the
4046 equivalent source rewritings that follow:
4048 struct temp {int a,b};
4049 procedure P (A,B: In Out ...) is temp P (int A,B)
4050 begin {
4051 .. ..
4052 end P; return {A,B};
4055 temp t;
4056 P(X,Y); t = P(X,Y);
4057 X = t.a , Y = t.b;
4059 For subprogram types we need to perform mainly the same conversions to
4060 GCC form that are needed for procedures and function declarations. The
4061 only difference is that at the end, we make a type declaration instead
4062 of a function declaration. */
4064 case E_Subprogram_Type:
4065 case E_Function:
4066 case E_Procedure:
4068 /* The type returned by a function or else Standard_Void_Type for a
4069 procedure. */
4070 Entity_Id gnat_return_type = Etype (gnat_entity);
4071 tree gnu_return_type;
4072 /* The first GCC parameter declaration (a PARM_DECL node). The
4073 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4074 actually is the head of this parameter list. */
4075 tree gnu_param_list = NULL_TREE;
4076 /* Non-null for subprograms containing parameters passed by copy-in
4077 copy-out (Ada In Out or Out parameters not passed by reference),
4078 in which case it is the list of nodes used to specify the values
4079 of the In Out/Out parameters that are returned as a record upon
4080 procedure return. The TREE_PURPOSE of an element of this list is
4081 a field of the record and the TREE_VALUE is the PARM_DECL
4082 corresponding to that field. This list will be saved in the
4083 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4084 tree gnu_cico_list = NULL_TREE;
4085 /* List of fields in return type of procedure with copy-in copy-out
4086 parameters. */
4087 tree gnu_field_list = NULL_TREE;
4088 /* If an import pragma asks to map this subprogram to a GCC builtin,
4089 this is the builtin DECL node. */
4090 tree gnu_builtin_decl = NULL_TREE;
4091 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4092 Entity_Id gnat_param;
4093 enum inline_status_t inline_status
4094 = Has_Pragma_No_Inline (gnat_entity)
4095 ? is_suppressed
4096 : Has_Pragma_Inline_Always (gnat_entity)
4097 ? is_required
4098 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4099 bool public_flag = Is_Public (gnat_entity) || imported_p;
4100 /* Subprograms marked both Intrinsic and Always_Inline need not
4101 have a body of their own. */
4102 bool extern_flag
4103 = ((Is_Public (gnat_entity) && !definition)
4104 || imported_p
4105 || (Convention (gnat_entity) == Convention_Intrinsic
4106 && Has_Pragma_Inline_Always (gnat_entity)));
4107 bool artificial_flag = !Comes_From_Source (gnat_entity);
4108 /* The semantics of "pure" in Ada essentially matches that of "const"
4109 in the back-end. In particular, both properties are orthogonal to
4110 the "nothrow" property if the EH circuitry is explicit in the
4111 internal representation of the back-end. If we are to completely
4112 hide the EH circuitry from it, we need to declare that calls to pure
4113 Ada subprograms that can throw have side effects since they can
4114 trigger an "abnormal" transfer of control flow; thus they can be
4115 neither "const" nor "pure" in the back-end sense. */
4116 bool const_flag
4117 = (Exception_Mechanism == Back_End_Exceptions
4118 && Is_Pure (gnat_entity));
4119 bool noreturn_flag = No_Return (gnat_entity);
4120 bool return_by_direct_ref_p = false;
4121 bool return_by_invisi_ref_p = false;
4122 bool return_unconstrained_p = false;
4123 int parmnum;
4125 /* A parameter may refer to this type, so defer completion of any
4126 incomplete types. */
4127 if (kind == E_Subprogram_Type && !definition)
4129 defer_incomplete_level++;
4130 this_deferred = true;
4133 /* If the subprogram has an alias, it is probably inherited, so
4134 we can use the original one. If the original "subprogram"
4135 is actually an enumeration literal, it may be the first use
4136 of its type, so we must elaborate that type now. */
4137 if (Present (Alias (gnat_entity)))
4139 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4140 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4142 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4144 /* Elaborate any Itypes in the parameters of this entity. */
4145 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4146 Present (gnat_temp);
4147 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4148 if (Is_Itype (Etype (gnat_temp)))
4149 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4151 break;
4154 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4155 corresponding DECL node. Proper generation of calls later on need
4156 proper parameter associations so we don't "break;" here. */
4157 if (Convention (gnat_entity) == Convention_Intrinsic
4158 && Present (Interface_Name (gnat_entity)))
4160 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4162 /* Inability to find the builtin decl most often indicates a
4163 genuine mistake, but imports of unregistered intrinsics are
4164 sometimes issued on purpose to allow hooking in alternate
4165 bodies. We post a warning conditioned on Wshadow in this case,
4166 to let developers be notified on demand without risking false
4167 positives with common default sets of options. */
4169 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4170 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4173 /* ??? What if we don't find the builtin node above ? warn ? err ?
4174 In the current state we neither warn nor err, and calls will just
4175 be handled as for regular subprograms. */
4177 /* Look into the return type and get its associated GCC tree. If it
4178 is not void, compute various flags for the subprogram type. */
4179 if (Ekind (gnat_return_type) == E_Void)
4180 gnu_return_type = void_type_node;
4181 else
4183 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4184 context may now appear in parameter and result profiles. If
4185 we are only annotating types, break circularities here. */
4186 if (type_annotate_only
4187 && is_from_limited_with_of_main (gnat_return_type))
4188 gnu_return_type = void_type_node;
4189 else
4190 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4192 /* If this function returns by reference, make the actual return
4193 type the pointer type and make a note of that. */
4194 if (Returns_By_Ref (gnat_entity))
4196 gnu_return_type = build_reference_type (gnu_return_type);
4197 return_by_direct_ref_p = true;
4200 /* If the return type is an unconstrained array type, the return
4201 value will be allocated on the secondary stack so the actual
4202 return type is the fat pointer type. */
4203 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4205 gnu_return_type = TREE_TYPE (gnu_return_type);
4206 return_unconstrained_p = true;
4209 /* Likewise, if the return type requires a transient scope, the
4210 return value will also be allocated on the secondary stack so
4211 the actual return type is the pointer type. */
4212 else if (Requires_Transient_Scope (gnat_return_type))
4214 gnu_return_type = build_reference_type (gnu_return_type);
4215 return_unconstrained_p = true;
4218 /* If the Mechanism is By_Reference, ensure this function uses the
4219 target's by-invisible-reference mechanism, which may not be the
4220 same as above (e.g. it might be passing an extra parameter). */
4221 else if (kind == E_Function
4222 && Mechanism (gnat_entity) == By_Reference)
4223 return_by_invisi_ref_p = true;
4225 /* Likewise, if the return type is itself By_Reference. */
4226 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4227 return_by_invisi_ref_p = true;
4229 /* If the type is a padded type and the underlying type would not
4230 be passed by reference or the function has a foreign convention,
4231 return the underlying type. */
4232 else if (TYPE_IS_PADDING_P (gnu_return_type)
4233 && (!default_pass_by_ref
4234 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4235 || Has_Foreign_Convention (gnat_entity)))
4236 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4238 /* If the return type is unconstrained, that means it must have a
4239 maximum size. Use the padded type as the effective return type.
4240 And ensure the function uses the target's by-invisible-reference
4241 mechanism to avoid copying too much data when it returns. */
4242 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4244 tree orig_type = gnu_return_type;
4245 tree max_return_size
4246 = max_size (TYPE_SIZE (gnu_return_type), true);
4248 /* If the size overflows to 0, set it to an arbitrary positive
4249 value so that assignments in the type are preserved. Their
4250 actual size is independent of this positive value. */
4251 if (TREE_CODE (max_return_size) == INTEGER_CST
4252 && TREE_OVERFLOW (max_return_size)
4253 && integer_zerop (max_return_size))
4255 max_return_size = copy_node (bitsize_unit_node);
4256 TREE_OVERFLOW (max_return_size) = 1;
4259 gnu_return_type
4260 = maybe_pad_type (gnu_return_type, max_return_size, 0,
4261 gnat_entity, false, false, definition,
4262 true);
4264 /* Declare it now since it will never be declared otherwise.
4265 This is necessary to ensure that its subtrees are properly
4266 marked. */
4267 if (gnu_return_type != orig_type
4268 && !DECL_P (TYPE_NAME (gnu_return_type)))
4269 create_type_decl (TYPE_NAME (gnu_return_type),
4270 gnu_return_type, true, debug_info_p,
4271 gnat_entity);
4273 return_by_invisi_ref_p = true;
4276 /* If the return type has a size that overflows, we cannot have
4277 a function that returns that type. This usage doesn't make
4278 sense anyway, so give an error here. */
4279 if (!return_by_invisi_ref_p
4280 && TYPE_SIZE_UNIT (gnu_return_type)
4281 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4282 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4284 post_error ("cannot return type whose size overflows",
4285 gnat_entity);
4286 gnu_return_type = copy_node (gnu_return_type);
4287 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4288 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4289 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4290 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4294 /* Loop over the parameters and get their associated GCC tree. While
4295 doing this, build a copy-in copy-out structure if we need one. */
4296 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4297 Present (gnat_param);
4298 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4300 Entity_Id gnat_param_type = Etype (gnat_param);
4301 tree gnu_param_name = get_entity_name (gnat_param);
4302 tree gnu_param_type, gnu_param, gnu_field;
4303 Mechanism_Type mech = Mechanism (gnat_param);
4304 bool copy_in_copy_out = false, fake_param_type;
4306 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4307 context may now appear in parameter and result profiles. If
4308 we are only annotating types, break circularities here. */
4309 if (type_annotate_only
4310 && is_from_limited_with_of_main (gnat_param_type))
4312 gnu_param_type = void_type_node;
4313 fake_param_type = true;
4315 else
4317 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4318 fake_param_type = false;
4321 /* Builtins are expanded inline and there is no real call sequence
4322 involved. So the type expected by the underlying expander is
4323 always the type of each argument "as is". */
4324 if (gnu_builtin_decl)
4325 mech = By_Copy;
4326 /* Handle the first parameter of a valued procedure specially. */
4327 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4328 mech = By_Copy_Return;
4329 /* Otherwise, see if a Mechanism was supplied that forced this
4330 parameter to be passed one way or another. */
4331 else if (mech == Default
4332 || mech == By_Copy
4333 || mech == By_Reference)
4335 else if (mech > 0)
4337 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4338 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4339 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4340 mech))
4341 mech = By_Reference;
4342 else
4343 mech = By_Copy;
4345 else
4347 post_error ("unsupported mechanism for&", gnat_param);
4348 mech = Default;
4351 /* Do not call gnat_to_gnu_param for a fake parameter type since
4352 it will try to use the real type again. */
4353 if (fake_param_type)
4355 if (Ekind (gnat_param) == E_Out_Parameter)
4356 gnu_param = NULL_TREE;
4357 else
4359 gnu_param
4360 = create_param_decl (gnu_param_name, gnu_param_type,
4361 false);
4362 Set_Mechanism (gnat_param,
4363 mech == Default ? By_Copy : mech);
4364 if (Ekind (gnat_param) == E_In_Out_Parameter)
4365 copy_in_copy_out = true;
4368 else
4369 gnu_param
4370 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4371 Has_Foreign_Convention (gnat_entity),
4372 &copy_in_copy_out);
4374 /* We are returned either a PARM_DECL or a type if no parameter
4375 needs to be passed; in either case, adjust the type. */
4376 if (DECL_P (gnu_param))
4377 gnu_param_type = TREE_TYPE (gnu_param);
4378 else
4380 gnu_param_type = gnu_param;
4381 gnu_param = NULL_TREE;
4384 /* The failure of this assertion will very likely come from an
4385 order of elaboration issue for the type of the parameter. */
4386 gcc_assert (kind == E_Subprogram_Type
4387 || !TYPE_IS_DUMMY_P (gnu_param_type)
4388 || type_annotate_only);
4390 if (gnu_param)
4392 gnu_param_list = chainon (gnu_param, gnu_param_list);
4393 Sloc_to_locus (Sloc (gnat_param),
4394 &DECL_SOURCE_LOCATION (gnu_param));
4395 save_gnu_tree (gnat_param, gnu_param, false);
4397 /* If a parameter is a pointer, this function may modify
4398 memory through it and thus shouldn't be considered
4399 a const function. Also, the memory may be modified
4400 between two calls, so they can't be CSE'ed. The latter
4401 case also handles by-ref parameters. */
4402 if (POINTER_TYPE_P (gnu_param_type)
4403 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4404 const_flag = false;
4407 if (copy_in_copy_out)
4409 if (!gnu_cico_list)
4411 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4413 /* If this is a function, we also need a field for the
4414 return value to be placed. */
4415 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4417 gnu_field
4418 = create_field_decl (get_identifier ("RETVAL"),
4419 gnu_return_type,
4420 gnu_new_ret_type, NULL_TREE,
4421 NULL_TREE, 0, 0);
4422 Sloc_to_locus (Sloc (gnat_entity),
4423 &DECL_SOURCE_LOCATION (gnu_field));
4424 gnu_field_list = gnu_field;
4425 gnu_cico_list
4426 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4429 gnu_return_type = gnu_new_ret_type;
4430 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4431 /* Set a default alignment to speed up accesses. But we
4432 shouldn't increase the size of the structure too much,
4433 lest it doesn't fit in return registers anymore. */
4434 TYPE_ALIGN (gnu_return_type)
4435 = get_mode_alignment (ptr_mode);
4438 gnu_field
4439 = create_field_decl (gnu_param_name, gnu_param_type,
4440 gnu_return_type, NULL_TREE, NULL_TREE,
4441 0, 0);
4442 Sloc_to_locus (Sloc (gnat_param),
4443 &DECL_SOURCE_LOCATION (gnu_field));
4444 DECL_CHAIN (gnu_field) = gnu_field_list;
4445 gnu_field_list = gnu_field;
4446 gnu_cico_list
4447 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4451 if (gnu_cico_list)
4453 /* If we have a CICO list but it has only one entry, we convert
4454 this function into a function that returns this object. */
4455 if (list_length (gnu_cico_list) == 1)
4456 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4458 /* Do not finalize the return type if the subprogram is stubbed
4459 since structures are incomplete for the back-end. */
4460 else if (Convention (gnat_entity) != Convention_Stubbed)
4462 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4463 0, false);
4465 /* Try to promote the mode of the return type if it is passed
4466 in registers, again to speed up accesses. */
4467 if (TYPE_MODE (gnu_return_type) == BLKmode
4468 && !targetm.calls.return_in_memory (gnu_return_type,
4469 NULL_TREE))
4471 unsigned int size
4472 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4473 unsigned int i = BITS_PER_UNIT;
4474 machine_mode mode;
4476 while (i < size)
4477 i <<= 1;
4478 mode = mode_for_size (i, MODE_INT, 0);
4479 if (mode != BLKmode)
4481 SET_TYPE_MODE (gnu_return_type, mode);
4482 TYPE_ALIGN (gnu_return_type)
4483 = GET_MODE_ALIGNMENT (mode);
4484 TYPE_SIZE (gnu_return_type)
4485 = bitsize_int (GET_MODE_BITSIZE (mode));
4486 TYPE_SIZE_UNIT (gnu_return_type)
4487 = size_int (GET_MODE_SIZE (mode));
4491 if (debug_info_p)
4492 rest_of_record_type_compilation (gnu_return_type);
4496 /* Deal with platform-specific calling conventions. */
4497 if (Has_Stdcall_Convention (gnat_entity))
4498 prepend_one_attribute
4499 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4500 get_identifier ("stdcall"), NULL_TREE,
4501 gnat_entity);
4502 else if (Has_Thiscall_Convention (gnat_entity))
4503 prepend_one_attribute
4504 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4505 get_identifier ("thiscall"), NULL_TREE,
4506 gnat_entity);
4508 /* If we should request stack realignment for a foreign convention
4509 subprogram, do so. Note that this applies to task entry points
4510 in particular. */
4511 if (FOREIGN_FORCE_REALIGN_STACK
4512 && Has_Foreign_Convention (gnat_entity))
4513 prepend_one_attribute
4514 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4515 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4516 gnat_entity);
4518 /* Deal with a pragma Linker_Section on a subprogram. */
4519 if ((kind == E_Function || kind == E_Procedure)
4520 && Present (Linker_Section_Pragma (gnat_entity)))
4521 prepend_one_attribute_pragma (&attr_list,
4522 Linker_Section_Pragma (gnat_entity));
4524 /* The lists have been built in reverse. */
4525 gnu_param_list = nreverse (gnu_param_list);
4526 gnu_cico_list = nreverse (gnu_cico_list);
4528 if (kind == E_Function)
4529 Set_Mechanism (gnat_entity, return_unconstrained_p
4530 || return_by_direct_ref_p
4531 || return_by_invisi_ref_p
4532 ? By_Reference : By_Copy);
4533 gnu_type
4534 = create_subprog_type (gnu_return_type, gnu_param_list,
4535 gnu_cico_list, return_unconstrained_p,
4536 return_by_direct_ref_p,
4537 return_by_invisi_ref_p);
4539 /* A procedure (something that doesn't return anything) shouldn't be
4540 considered const since there would be no reason for calling such a
4541 subprogram. Note that procedures with Out (or In Out) parameters
4542 have already been converted into a function with a return type.
4543 Similarly, if the function returns an unconstrained type, then the
4544 function will allocate the return value on the secondary stack and
4545 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4546 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4547 const_flag = false;
4549 if (const_flag || noreturn_flag)
4551 const int quals
4552 = (const_flag ? TYPE_QUAL_CONST : 0)
4553 | (noreturn_flag ? TYPE_QUAL_VOLATILE : 0);
4554 gnu_type = change_qualified_type (gnu_type, quals);
4557 /* If we have a builtin decl for that function, use it. Check if the
4558 profiles are compatible and warn if they are not. The checker is
4559 expected to post extra diagnostics in this case. */
4560 if (gnu_builtin_decl)
4562 intrin_binding_t inb;
4564 inb.gnat_entity = gnat_entity;
4565 inb.ada_fntype = gnu_type;
4566 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4568 if (!intrin_profiles_compatible_p (&inb))
4569 post_error
4570 ("?profile of& doesn''t match the builtin it binds!",
4571 gnat_entity);
4573 gnu_decl = gnu_builtin_decl;
4574 gnu_type = TREE_TYPE (gnu_builtin_decl);
4575 break;
4578 /* If there was no specified Interface_Name and the external and
4579 internal names of the subprogram are the same, only use the
4580 internal name to allow disambiguation of nested subprograms. */
4581 if (No (Interface_Name (gnat_entity))
4582 && gnu_ext_name == gnu_entity_name)
4583 gnu_ext_name = NULL_TREE;
4585 /* If we are defining the subprogram and it has an Address clause
4586 we must get the address expression from the saved GCC tree for the
4587 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4588 the address expression here since the front-end has guaranteed
4589 in that case that the elaboration has no effects. If there is
4590 an Address clause and we are not defining the object, just
4591 make it a constant. */
4592 if (Present (Address_Clause (gnat_entity)))
4594 tree gnu_address = NULL_TREE;
4596 if (definition)
4597 gnu_address
4598 = (present_gnu_tree (gnat_entity)
4599 ? get_gnu_tree (gnat_entity)
4600 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4602 save_gnu_tree (gnat_entity, NULL_TREE, false);
4604 /* Convert the type of the object to a reference type that can
4605 alias everything as per 13.3(19). */
4606 gnu_type
4607 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4608 if (gnu_address)
4609 gnu_address = convert (gnu_type, gnu_address);
4611 gnu_decl
4612 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4613 gnu_address, false, Is_Public (gnat_entity),
4614 extern_flag, false, NULL, gnat_entity);
4615 DECL_BY_REF_P (gnu_decl) = 1;
4618 else if (kind == E_Subprogram_Type)
4620 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4621 gnu_decl
4622 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4623 debug_info_p, gnat_entity);
4625 else
4627 gnu_decl
4628 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4629 gnu_param_list, inline_status,
4630 public_flag, extern_flag, artificial_flag,
4631 attr_list, gnat_entity);
4632 /* This is unrelated to the stub built right above. */
4633 DECL_STUBBED_P (gnu_decl)
4634 = Convention (gnat_entity) == Convention_Stubbed;
4637 break;
4639 case E_Incomplete_Type:
4640 case E_Incomplete_Subtype:
4641 case E_Private_Type:
4642 case E_Private_Subtype:
4643 case E_Limited_Private_Type:
4644 case E_Limited_Private_Subtype:
4645 case E_Record_Type_With_Private:
4646 case E_Record_Subtype_With_Private:
4648 bool is_from_limited_with
4649 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4650 /* Get the "full view" of this entity. If this is an incomplete
4651 entity from a limited with, treat its non-limited view as the
4652 full view. Otherwise, use either the full view or the underlying
4653 full view, whichever is present. This is used in all the tests
4654 below. */
4655 Entity_Id full_view
4656 = is_from_limited_with
4657 ? Non_Limited_View (gnat_entity)
4658 : Present (Full_View (gnat_entity))
4659 ? Full_View (gnat_entity)
4660 : IN (kind, Private_Kind)
4661 ? Underlying_Full_View (gnat_entity)
4662 : Empty;
4664 /* If this is an incomplete type with no full view, it must be a Taft
4665 Amendment type, in which case we return a dummy type. Otherwise,
4666 just get the type from its Etype. */
4667 if (No (full_view))
4669 if (kind == E_Incomplete_Type)
4671 gnu_type = make_dummy_type (gnat_entity);
4672 gnu_decl = TYPE_STUB_DECL (gnu_type);
4674 else
4676 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4677 NULL_TREE, 0);
4678 maybe_present = true;
4680 break;
4683 /* If we already made a type for the full view, reuse it. */
4684 else if (present_gnu_tree (full_view))
4686 gnu_decl = get_gnu_tree (full_view);
4687 break;
4690 /* Otherwise, if we are not defining the type now, get the type
4691 from the full view. But always get the type from the full view
4692 for define on use types, since otherwise we won't see them.
4693 Likewise if this is a non-limited view not declared in the main
4694 unit, which can happen for incomplete formal types instantiated
4695 on a type coming from a limited_with clause. */
4696 else if (!definition
4697 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4698 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view)))
4699 || (is_from_limited_with
4700 && !In_Extended_Main_Code_Unit (full_view)))
4702 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4703 maybe_present = true;
4704 break;
4707 /* For incomplete types, make a dummy type entry which will be
4708 replaced later. Save it as the full declaration's type so
4709 we can do any needed updates when we see it. */
4710 gnu_type = make_dummy_type (gnat_entity);
4711 gnu_decl = TYPE_STUB_DECL (gnu_type);
4712 if (Has_Completion_In_Body (gnat_entity))
4713 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4714 save_gnu_tree (full_view, gnu_decl, 0);
4715 break;
4718 case E_Class_Wide_Type:
4719 /* Class-wide types are always transformed into their root type. */
4720 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4721 maybe_present = true;
4722 break;
4724 case E_Task_Type:
4725 case E_Task_Subtype:
4726 case E_Protected_Type:
4727 case E_Protected_Subtype:
4728 /* Concurrent types are always transformed into their record type. */
4729 if (type_annotate_only && No (gnat_equiv_type))
4730 gnu_type = void_type_node;
4731 else
4732 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4733 maybe_present = true;
4734 break;
4736 case E_Label:
4737 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4738 break;
4740 case E_Block:
4741 case E_Loop:
4742 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4743 we've already saved it, so we don't try to. */
4744 gnu_decl = error_mark_node;
4745 saved = true;
4746 break;
4748 case E_Abstract_State:
4749 /* This is a SPARK annotation that only reaches here when compiling in
4750 ASIS mode. */
4751 gcc_assert (type_annotate_only);
4752 gnu_decl = error_mark_node;
4753 saved = true;
4754 break;
4756 default:
4757 gcc_unreachable ();
4760 /* If we had a case where we evaluated another type and it might have
4761 defined this one, handle it here. */
4762 if (maybe_present && present_gnu_tree (gnat_entity))
4764 gnu_decl = get_gnu_tree (gnat_entity);
4765 saved = true;
4768 /* If we are processing a type and there is either no decl for it or
4769 we just made one, do some common processing for the type, such as
4770 handling alignment and possible padding. */
4771 if (is_type && (!gnu_decl || this_made_decl))
4773 /* Process the attributes, if not already done. Note that the type is
4774 already defined so we cannot pass true for IN_PLACE here. */
4775 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4777 /* Tell the middle-end that objects of tagged types are guaranteed to
4778 be properly aligned. This is necessary because conversions to the
4779 class-wide type are translated into conversions to the root type,
4780 which can be less aligned than some of its derived types. */
4781 if (Is_Tagged_Type (gnat_entity)
4782 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4783 TYPE_ALIGN_OK (gnu_type) = 1;
4785 /* Record whether the type is passed by reference. */
4786 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4787 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4789 /* ??? Don't set the size for a String_Literal since it is either
4790 confirming or we don't handle it properly (if the low bound is
4791 non-constant). */
4792 if (!gnu_size && kind != E_String_Literal_Subtype)
4794 Uint gnat_size = Known_Esize (gnat_entity)
4795 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4796 gnu_size
4797 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4798 false, Has_Size_Clause (gnat_entity));
4801 /* If a size was specified, see if we can make a new type of that size
4802 by rearranging the type, for example from a fat to a thin pointer. */
4803 if (gnu_size)
4805 gnu_type
4806 = make_type_from_size (gnu_type, gnu_size,
4807 Has_Biased_Representation (gnat_entity));
4809 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4810 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4811 gnu_size = NULL_TREE;
4814 /* If the alignment has not already been processed and this is not
4815 an unconstrained array type, see if an alignment is specified.
4816 If not, we pick a default alignment for atomic objects. */
4817 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4819 else if (Known_Alignment (gnat_entity))
4821 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4822 TYPE_ALIGN (gnu_type));
4824 /* Warn on suspiciously large alignments. This should catch
4825 errors about the (alignment,byte)/(size,bit) discrepancy. */
4826 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4828 tree size;
4830 /* If a size was specified, take it into account. Otherwise
4831 use the RM size for records or unions as the type size has
4832 already been adjusted to the alignment. */
4833 if (gnu_size)
4834 size = gnu_size;
4835 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4836 && !TYPE_FAT_POINTER_P (gnu_type))
4837 size = rm_size (gnu_type);
4838 else
4839 size = TYPE_SIZE (gnu_type);
4841 /* Consider an alignment as suspicious if the alignment/size
4842 ratio is greater or equal to the byte/bit ratio. */
4843 if (tree_fits_uhwi_p (size)
4844 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4845 post_error_ne ("?suspiciously large alignment specified for&",
4846 Expression (Alignment_Clause (gnat_entity)),
4847 gnat_entity);
4850 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4851 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4852 && integer_pow2p (TYPE_SIZE (gnu_type)))
4853 align = MIN (BIGGEST_ALIGNMENT,
4854 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4855 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4856 && tree_fits_uhwi_p (gnu_size)
4857 && integer_pow2p (gnu_size))
4858 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4860 /* See if we need to pad the type. If we did, and made a record,
4861 the name of the new type may be changed. So get it back for
4862 us when we make the new TYPE_DECL below. */
4863 if (gnu_size || align > 0)
4864 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4865 false, !gnu_decl, definition, false);
4867 if (TYPE_IS_PADDING_P (gnu_type))
4868 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4870 /* Now set the RM size of the type. We cannot do it before padding
4871 because we need to accept arbitrary RM sizes on integral types. */
4872 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4874 /* If we are at global level, GCC will have applied variable_size to
4875 the type, but that won't have done anything. So, if it's not
4876 a constant or self-referential, call elaborate_expression_1 to
4877 make a variable for the size rather than calculating it each time.
4878 Handle both the RM size and the actual size. */
4879 if (global_bindings_p ()
4880 && TYPE_SIZE (gnu_type)
4881 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4882 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4884 tree size = TYPE_SIZE (gnu_type);
4886 TYPE_SIZE (gnu_type)
4887 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4888 false);
4890 /* ??? For now, store the size as a multiple of the alignment in
4891 bytes so that we can see the alignment from the tree. */
4892 TYPE_SIZE_UNIT (gnu_type)
4893 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4894 "SIZE_A_UNIT", definition, false,
4895 TYPE_ALIGN (gnu_type));
4897 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4898 may not be marked by the call to create_type_decl below. */
4899 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4901 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4903 tree variant_part = get_variant_part (gnu_type);
4904 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4906 if (variant_part)
4908 tree union_type = TREE_TYPE (variant_part);
4909 tree offset = DECL_FIELD_OFFSET (variant_part);
4911 /* If the position of the variant part is constant, subtract
4912 it from the size of the type of the parent to get the new
4913 size. This manual CSE reduces the data size. */
4914 if (TREE_CODE (offset) == INTEGER_CST)
4916 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4917 TYPE_SIZE (union_type)
4918 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4919 bit_from_pos (offset, bitpos));
4920 TYPE_SIZE_UNIT (union_type)
4921 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4922 byte_from_pos (offset, bitpos));
4924 else
4926 TYPE_SIZE (union_type)
4927 = elaborate_expression_1 (TYPE_SIZE (union_type),
4928 gnat_entity, "VSIZE",
4929 definition, false);
4931 /* ??? For now, store the size as a multiple of the
4932 alignment in bytes so that we can see the alignment
4933 from the tree. */
4934 TYPE_SIZE_UNIT (union_type)
4935 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4936 gnat_entity, "VSIZE_A_UNIT",
4937 definition, false,
4938 TYPE_ALIGN (union_type));
4940 /* ??? For now, store the offset as a multiple of the
4941 alignment in bytes so that we can see the alignment
4942 from the tree. */
4943 DECL_FIELD_OFFSET (variant_part)
4944 = elaborate_expression_2 (offset, gnat_entity,
4945 "VOFFSET", definition, false,
4946 DECL_OFFSET_ALIGN
4947 (variant_part));
4950 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4951 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4954 if (operand_equal_p (ada_size, size, 0))
4955 ada_size = TYPE_SIZE (gnu_type);
4956 else
4957 ada_size
4958 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4959 definition, false);
4960 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4964 /* If this is a record type or subtype, call elaborate_expression_2 on
4965 any field position. Do this for both global and local types.
4966 Skip any fields that we haven't made trees for to avoid problems with
4967 class wide types. */
4968 if (IN (kind, Record_Kind))
4969 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4970 gnat_temp = Next_Entity (gnat_temp))
4971 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4973 tree gnu_field = get_gnu_tree (gnat_temp);
4975 /* ??? For now, store the offset as a multiple of the alignment
4976 in bytes so that we can see the alignment from the tree. */
4977 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4979 DECL_FIELD_OFFSET (gnu_field)
4980 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4981 gnat_temp, "OFFSET", definition,
4982 false,
4983 DECL_OFFSET_ALIGN (gnu_field));
4985 /* ??? The context of gnu_field is not necessarily gnu_type
4986 so the MULT_EXPR node built above may not be marked by
4987 the call to create_type_decl below. */
4988 if (global_bindings_p ())
4989 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4993 if (Is_Atomic_Or_VFA (gnat_entity))
4994 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4996 /* If this is not an unconstrained array type, set some flags. */
4997 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4999 if (Present (Alignment_Clause (gnat_entity)))
5000 TYPE_USER_ALIGN (gnu_type) = 1;
5002 if (Universal_Aliasing (gnat_entity))
5003 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
5005 /* If it is passed by reference, force BLKmode to ensure that
5006 objects of this type will always be put in memory. */
5007 if (TYPE_MODE (gnu_type) != BLKmode
5008 && AGGREGATE_TYPE_P (gnu_type)
5009 && TYPE_BY_REFERENCE_P (gnu_type))
5010 SET_TYPE_MODE (gnu_type, BLKmode);
5012 if (Treat_As_Volatile (gnat_entity))
5014 const int quals
5015 = TYPE_QUAL_VOLATILE
5016 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
5017 gnu_type = change_qualified_type (gnu_type, quals);
5021 if (!gnu_decl)
5022 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5023 !Comes_From_Source (gnat_entity),
5024 debug_info_p, gnat_entity);
5025 else
5027 TREE_TYPE (gnu_decl) = gnu_type;
5028 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5032 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5034 gnu_type = TREE_TYPE (gnu_decl);
5036 /* If this is a derived type, relate its alias set to that of its parent
5037 to avoid troubles when a call to an inherited primitive is inlined in
5038 a context where a derived object is accessed. The inlined code works
5039 on the parent view so the resulting code may access the same object
5040 using both the parent and the derived alias sets, which thus have to
5041 conflict. As the same issue arises with component references, the
5042 parent alias set also has to conflict with composite types enclosing
5043 derived components. For instance, if we have:
5045 type D is new T;
5046 type R is record
5047 Component : D;
5048 end record;
5050 we want T to conflict with both D and R, in addition to R being a
5051 superset of D by record/component construction.
5053 One way to achieve this is to perform an alias set copy from the
5054 parent to the derived type. This is not quite appropriate, though,
5055 as we don't want separate derived types to conflict with each other:
5057 type I1 is new Integer;
5058 type I2 is new Integer;
5060 We want I1 and I2 to both conflict with Integer but we do not want
5061 I1 to conflict with I2, and an alias set copy on derivation would
5062 have that effect.
5064 The option chosen is to make the alias set of the derived type a
5065 superset of that of its parent type. It trivially fulfills the
5066 simple requirement for the Integer derivation example above, and
5067 the component case as well by superset transitivity:
5069 superset superset
5070 R ----------> D ----------> T
5072 However, for composite types, conversions between derived types are
5073 translated into VIEW_CONVERT_EXPRs so a sequence like:
5075 type Comp1 is new Comp;
5076 type Comp2 is new Comp;
5077 procedure Proc (C : Comp1);
5079 C : Comp2;
5080 Proc (Comp1 (C));
5082 is translated into:
5084 C : Comp2;
5085 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5087 and gimplified into:
5089 C : Comp2;
5090 Comp1 *C.0;
5091 C.0 = (Comp1 *) &C;
5092 Proc (C.0);
5094 i.e. generates code involving type punning. Therefore, Comp1 needs
5095 to conflict with Comp2 and an alias set copy is required.
5097 The language rules ensure the parent type is already frozen here. */
5098 if (kind != E_Subprogram_Type
5099 && Is_Derived_Type (gnat_entity)
5100 && !type_annotate_only)
5102 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5103 /* For constrained packed array subtypes, the implementation type is
5104 used instead of the nominal type. */
5105 if (kind == E_Array_Subtype
5106 && Is_Constrained (gnat_entity)
5107 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5108 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5109 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5110 Is_Composite_Type (gnat_entity)
5111 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5114 /* Back-annotate the Alignment of the type if not already in the
5115 tree. Likewise for sizes. */
5116 if (Unknown_Alignment (gnat_entity))
5118 unsigned int double_align, align;
5119 bool is_capped_double, align_clause;
5121 /* If the default alignment of "double" or larger scalar types is
5122 specifically capped and this is not an array with an alignment
5123 clause on the component type, return the cap. */
5124 if ((double_align = double_float_alignment) > 0)
5125 is_capped_double
5126 = is_double_float_or_array (gnat_entity, &align_clause);
5127 else if ((double_align = double_scalar_alignment) > 0)
5128 is_capped_double
5129 = is_double_scalar_or_array (gnat_entity, &align_clause);
5130 else
5131 is_capped_double = align_clause = false;
5133 if (is_capped_double && !align_clause)
5134 align = double_align;
5135 else
5136 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5138 Set_Alignment (gnat_entity, UI_From_Int (align));
5141 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5143 tree gnu_size = TYPE_SIZE (gnu_type);
5145 /* If the size is self-referential, annotate the maximum value. */
5146 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5147 gnu_size = max_size (gnu_size, true);
5149 /* If we are just annotating types and the type is tagged, the tag
5150 and the parent components are not generated by the front-end so
5151 sizes must be adjusted if there is no representation clause. */
5152 if (type_annotate_only
5153 && Is_Tagged_Type (gnat_entity)
5154 && !VOID_TYPE_P (gnu_type)
5155 && (!TYPE_FIELDS (gnu_type)
5156 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5158 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5159 Uint uint_size;
5161 if (Is_Derived_Type (gnat_entity))
5163 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5164 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5165 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5167 else
5168 offset = pointer_size;
5170 if (TYPE_FIELDS (gnu_type))
5171 offset
5172 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5174 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5175 gnu_size = round_up (gnu_size, POINTER_SIZE);
5176 uint_size = annotate_value (gnu_size);
5177 Set_Esize (gnat_entity, uint_size);
5178 Set_RM_Size (gnat_entity, uint_size);
5180 else
5181 Set_Esize (gnat_entity, annotate_value (gnu_size));
5184 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5185 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5188 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5189 cannot do so if we are reusing the ..._DECL node made for an equivalent
5190 type or an alias or a renamed object as the predicates don't apply to it
5191 but to GNAT_ENTITY. */
5192 if (DECL_P (gnu_decl)
5193 && !(is_type && gnat_equiv_type != gnat_entity)
5194 && !Present (Alias (gnat_entity))
5195 && !(Present (Renamed_Object (gnat_entity)) && saved))
5197 /* ??? DECL_ARTIFICIAL, and possibly DECL_IGNORED_P below, should
5198 be set before calling rest_of_decl_compilation above (through
5199 create_var_decl_1). This is because rest_of_decl_compilation
5200 calls the debugging backend and will create a DIE without
5201 DW_AT_artificial.
5203 This is currently causing gnat.dg/specs/debug1.ads to FAIL. */
5204 if (!Comes_From_Source (gnat_entity))
5205 DECL_ARTIFICIAL (gnu_decl) = 1;
5207 if (!debug_info_p)
5208 DECL_IGNORED_P (gnu_decl) = 1;
5211 /* If we haven't already, associate the ..._DECL node that we just made with
5212 the input GNAT entity node. */
5213 if (!saved)
5214 save_gnu_tree (gnat_entity, gnu_decl, false);
5216 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5217 eliminate as many deferred computations as possible. */
5218 process_deferred_decl_context (false);
5220 /* If this is an enumeration or floating-point type, we were not able to set
5221 the bounds since they refer to the type. These are always static. */
5222 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5223 || (kind == E_Floating_Point_Type))
5225 tree gnu_scalar_type = gnu_type;
5226 tree gnu_low_bound, gnu_high_bound;
5228 /* If this is a padded type, we need to use the underlying type. */
5229 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5230 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5232 /* If this is a floating point type and we haven't set a floating
5233 point type yet, use this in the evaluation of the bounds. */
5234 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5235 longest_float_type_node = gnu_scalar_type;
5237 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5238 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5240 if (kind == E_Enumeration_Type)
5242 /* Enumeration types have specific RM bounds. */
5243 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5244 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5246 else
5248 /* Floating-point types don't have specific RM bounds. */
5249 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5250 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5254 /* If we deferred processing of incomplete types, re-enable it. If there
5255 were no other disables and we have deferred types to process, do so. */
5256 if (this_deferred
5257 && --defer_incomplete_level == 0
5258 && defer_incomplete_list)
5260 struct incomplete *p, *next;
5262 /* We are back to level 0 for the deferring of incomplete types.
5263 But processing these incomplete types below may itself require
5264 deferring, so preserve what we have and restart from scratch. */
5265 p = defer_incomplete_list;
5266 defer_incomplete_list = NULL;
5268 for (; p; p = next)
5270 next = p->next;
5272 if (p->old_type)
5273 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5274 gnat_to_gnu_type (p->full_type));
5275 free (p);
5279 /* If we are not defining this type, see if it's on one of the lists of
5280 incomplete types. If so, handle the list entry now. */
5281 if (is_type && !definition)
5283 struct incomplete *p;
5285 for (p = defer_incomplete_list; p; p = p->next)
5286 if (p->old_type && p->full_type == gnat_entity)
5288 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5289 TREE_TYPE (gnu_decl));
5290 p->old_type = NULL_TREE;
5293 for (p = defer_limited_with; p; p = p->next)
5294 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5296 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5297 TREE_TYPE (gnu_decl));
5298 p->old_type = NULL_TREE;
5302 if (this_global)
5303 force_global--;
5305 /* If this is a packed array type whose original array type is itself
5306 an Itype without freeze node, make sure the latter is processed. */
5307 if (Is_Packed_Array_Impl_Type (gnat_entity)
5308 && Is_Itype (Original_Array_Type (gnat_entity))
5309 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5310 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5311 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5313 return gnu_decl;
5316 /* Similar, but if the returned value is a COMPONENT_REF, return the
5317 FIELD_DECL. */
5319 tree
5320 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5322 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5324 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5325 gnu_field = TREE_OPERAND (gnu_field, 1);
5327 return gnu_field;
5330 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5331 the GCC type corresponding to that entity. */
5333 tree
5334 gnat_to_gnu_type (Entity_Id gnat_entity)
5336 tree gnu_decl;
5338 /* The back end never attempts to annotate generic types. */
5339 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5340 return void_type_node;
5342 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5343 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5345 return TREE_TYPE (gnu_decl);
5348 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5349 the unpadded version of the GCC type corresponding to that entity. */
5351 tree
5352 get_unpadded_type (Entity_Id gnat_entity)
5354 tree type = gnat_to_gnu_type (gnat_entity);
5356 if (TYPE_IS_PADDING_P (type))
5357 type = TREE_TYPE (TYPE_FIELDS (type));
5359 return type;
5362 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5363 type has been changed to that of the parameterless procedure, except if an
5364 alias is already present, in which case it is returned instead. */
5366 tree
5367 get_minimal_subprog_decl (Entity_Id gnat_entity)
5369 tree gnu_entity_name, gnu_ext_name;
5370 struct attrib *attr_list = NULL;
5372 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5373 of the handling applied here. */
5375 while (Present (Alias (gnat_entity)))
5377 gnat_entity = Alias (gnat_entity);
5378 if (present_gnu_tree (gnat_entity))
5379 return get_gnu_tree (gnat_entity);
5382 gnu_entity_name = get_entity_name (gnat_entity);
5383 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5385 if (Has_Stdcall_Convention (gnat_entity))
5386 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5387 get_identifier ("stdcall"), NULL_TREE,
5388 gnat_entity);
5389 else if (Has_Thiscall_Convention (gnat_entity))
5390 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5391 get_identifier ("thiscall"), NULL_TREE,
5392 gnat_entity);
5394 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5395 gnu_ext_name = NULL_TREE;
5397 return
5398 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5399 is_disabled, true, true, true, attr_list, gnat_entity);
5402 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5403 a C++ imported method or equivalent.
5405 We use the predicate on 32-bit x86/Windows to find out whether we need to
5406 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5407 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5409 bool
5410 is_cplusplus_method (Entity_Id gnat_entity)
5412 if (Convention (gnat_entity) != Convention_CPP)
5413 return false;
5415 /* This is the main case: C++ method imported as a primitive operation.
5416 Note that a C++ class with no virtual functions can be imported as a
5417 limited record type so the operation is not necessarily dispatching. */
5418 if (Is_Primitive (gnat_entity))
5419 return true;
5421 /* A thunk needs to be handled like its associated primitive operation. */
5422 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5423 return true;
5425 /* A constructor is a method on the C++ side. */
5426 if (Is_Constructor (gnat_entity))
5427 return true;
5429 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5430 if (Is_Dispatch_Table_Entity (gnat_entity))
5431 return true;
5433 return false;
5436 /* Finalize the processing of From_Limited_With incomplete types. */
5438 void
5439 finalize_from_limited_with (void)
5441 struct incomplete *p, *next;
5443 p = defer_limited_with;
5444 defer_limited_with = NULL;
5446 for (; p; p = next)
5448 next = p->next;
5450 if (p->old_type)
5451 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5452 gnat_to_gnu_type (p->full_type));
5453 free (p);
5457 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5458 kind of type (such E_Task_Type) that has a different type which Gigi
5459 uses for its representation. If the type does not have a special type
5460 for its representation, return GNAT_ENTITY. If a type is supposed to
5461 exist, but does not, abort unless annotating types, in which case
5462 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5464 Entity_Id
5465 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5467 Entity_Id gnat_equiv = gnat_entity;
5469 if (No (gnat_entity))
5470 return gnat_entity;
5472 switch (Ekind (gnat_entity))
5474 case E_Class_Wide_Subtype:
5475 if (Present (Equivalent_Type (gnat_entity)))
5476 gnat_equiv = Equivalent_Type (gnat_entity);
5477 break;
5479 case E_Access_Protected_Subprogram_Type:
5480 case E_Anonymous_Access_Protected_Subprogram_Type:
5481 gnat_equiv = Equivalent_Type (gnat_entity);
5482 break;
5484 case E_Class_Wide_Type:
5485 gnat_equiv = Root_Type (gnat_entity);
5486 break;
5488 case E_Task_Type:
5489 case E_Task_Subtype:
5490 case E_Protected_Type:
5491 case E_Protected_Subtype:
5492 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5493 break;
5495 default:
5496 break;
5499 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5501 return gnat_equiv;
5504 /* Return a GCC tree for a type corresponding to the component type of the
5505 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5506 is for an array being defined. DEBUG_INFO_P is true if we need to write
5507 debug information for other types that we may create in the process. */
5509 static tree
5510 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5511 bool debug_info_p)
5513 const Entity_Id gnat_type = Component_Type (gnat_array);
5514 tree gnu_type = gnat_to_gnu_type (gnat_type);
5515 tree gnu_comp_size;
5517 /* Try to get a smaller form of the component if needed. */
5518 if ((Is_Packed (gnat_array)
5519 || Has_Component_Size_Clause (gnat_array))
5520 && !Is_Bit_Packed_Array (gnat_array)
5521 && !Has_Aliased_Components (gnat_array)
5522 && !Strict_Alignment (gnat_type)
5523 && RECORD_OR_UNION_TYPE_P (gnu_type)
5524 && !TYPE_FAT_POINTER_P (gnu_type)
5525 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5526 gnu_type = make_packable_type (gnu_type, false);
5528 if (Has_Atomic_Components (gnat_array))
5529 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5531 /* Get and validate any specified Component_Size. */
5532 gnu_comp_size
5533 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5534 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5535 true, Has_Component_Size_Clause (gnat_array));
5537 /* If the array has aliased components and the component size can be zero,
5538 force at least unit size to ensure that the components have distinct
5539 addresses. */
5540 if (!gnu_comp_size
5541 && Has_Aliased_Components (gnat_array)
5542 && (integer_zerop (TYPE_SIZE (gnu_type))
5543 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5544 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5545 gnu_comp_size
5546 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5548 /* If the component type is a RECORD_TYPE that has a self-referential size,
5549 then use the maximum size for the component size. */
5550 if (!gnu_comp_size
5551 && TREE_CODE (gnu_type) == RECORD_TYPE
5552 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5553 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5555 /* Honor the component size. This is not needed for bit-packed arrays. */
5556 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5558 tree orig_type = gnu_type;
5559 unsigned int max_align;
5561 /* If an alignment is specified, use it as a cap on the component type
5562 so that it can be honored for the whole type. But ignore it for the
5563 original type of packed array types. */
5564 if (No (Packed_Array_Impl_Type (gnat_array))
5565 && Known_Alignment (gnat_array))
5566 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5567 else
5568 max_align = 0;
5570 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5571 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5572 gnu_type = orig_type;
5573 else
5574 orig_type = gnu_type;
5576 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5577 true, false, definition, true);
5579 /* If a padding record was made, declare it now since it will never be
5580 declared otherwise. This is necessary to ensure that its subtrees
5581 are properly marked. */
5582 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5583 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5584 gnat_array);
5587 if (Has_Volatile_Components (gnat_array))
5589 const int quals
5590 = TYPE_QUAL_VOLATILE
5591 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5592 gnu_type = change_qualified_type (gnu_type, quals);
5595 return gnu_type;
5598 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5599 using MECH as its passing mechanism, to be placed in the parameter
5600 list built for GNAT_SUBPROG. Assume a foreign convention for the
5601 latter if FOREIGN is true. Also set CICO to true if the parameter
5602 must use the copy-in copy-out implementation mechanism.
5604 The returned tree is a PARM_DECL, except for those cases where no
5605 parameter needs to be actually passed to the subprogram; the type
5606 of this "shadow" parameter is then returned instead. */
5608 static tree
5609 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5610 Entity_Id gnat_subprog, bool foreign, bool *cico)
5612 tree gnu_param_name = get_entity_name (gnat_param);
5613 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5614 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5615 /* The parameter can be indirectly modified if its address is taken. */
5616 bool ro_param = in_param && !Address_Taken (gnat_param);
5617 bool by_return = false, by_component_ptr = false;
5618 bool by_ref = false;
5619 tree gnu_param;
5621 /* Copy-return is used only for the first parameter of a valued procedure.
5622 It's a copy mechanism for which a parameter is never allocated. */
5623 if (mech == By_Copy_Return)
5625 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5626 mech = By_Copy;
5627 by_return = true;
5630 /* If this is either a foreign function or if the underlying type won't
5631 be passed by reference and is as aligned as the original type, strip
5632 off possible padding type. */
5633 if (TYPE_IS_PADDING_P (gnu_param_type))
5635 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5637 if (foreign
5638 || (!must_pass_by_ref (unpadded_type)
5639 && mech != By_Reference
5640 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5641 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5642 gnu_param_type = unpadded_type;
5645 /* If this is a read-only parameter, make a variant of the type that is
5646 read-only. ??? However, if this is an unconstrained array, that type
5647 can be very complex, so skip it for now. Likewise for any other
5648 self-referential type. */
5649 if (ro_param
5650 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5651 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5652 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5654 /* For foreign conventions, pass arrays as pointers to the element type.
5655 First check for unconstrained array and get the underlying array. */
5656 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5657 gnu_param_type
5658 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5660 /* For GCC builtins, pass Address integer types as (void *) */
5661 if (Convention (gnat_subprog) == Convention_Intrinsic
5662 && Present (Interface_Name (gnat_subprog))
5663 && Is_Descendent_Of_Address (Etype (gnat_param)))
5664 gnu_param_type = ptr_type_node;
5666 /* Arrays are passed as pointers to element type for foreign conventions. */
5667 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5669 /* Strip off any multi-dimensional entries, then strip
5670 off the last array to get the component type. */
5671 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5672 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5673 gnu_param_type = TREE_TYPE (gnu_param_type);
5675 by_component_ptr = true;
5676 gnu_param_type = TREE_TYPE (gnu_param_type);
5678 if (ro_param)
5679 gnu_param_type
5680 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5682 gnu_param_type = build_pointer_type (gnu_param_type);
5685 /* Fat pointers are passed as thin pointers for foreign conventions. */
5686 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5687 gnu_param_type
5688 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5690 /* If we must pass or were requested to pass by reference, do so.
5691 If we were requested to pass by copy, do so.
5692 Otherwise, for foreign conventions, pass In Out or Out parameters
5693 or aggregates by reference. For COBOL and Fortran, pass all
5694 integer and FP types that way too. For Convention Ada, use
5695 the standard Ada default. */
5696 else if (must_pass_by_ref (gnu_param_type)
5697 || mech == By_Reference
5698 || (mech != By_Copy
5699 && ((foreign
5700 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5701 || (foreign
5702 && (Convention (gnat_subprog) == Convention_Fortran
5703 || Convention (gnat_subprog) == Convention_COBOL)
5704 && (INTEGRAL_TYPE_P (gnu_param_type)
5705 || FLOAT_TYPE_P (gnu_param_type)))
5706 || (!foreign
5707 && default_pass_by_ref (gnu_param_type)))))
5709 /* We take advantage of 6.2(12) by considering that references built for
5710 parameters whose type isn't by-ref and for which the mechanism hasn't
5711 been forced to by-ref are restrict-qualified in the C sense. */
5712 bool restrict_p
5713 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5714 gnu_param_type = build_reference_type (gnu_param_type);
5715 if (restrict_p)
5716 gnu_param_type
5717 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5718 by_ref = true;
5721 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5722 else if (!in_param)
5723 *cico = true;
5725 if (mech == By_Copy && (by_ref || by_component_ptr))
5726 post_error ("?cannot pass & by copy", gnat_param);
5728 /* If this is an Out parameter that isn't passed by reference and isn't
5729 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5730 it will be a VAR_DECL created when we process the procedure, so just
5731 return its type. For the special parameter of a valued procedure,
5732 never pass it in.
5734 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5735 Out parameters with discriminants or implicit initial values to be
5736 handled like In Out parameters. These type are normally built as
5737 aggregates, hence passed by reference, except for some packed arrays
5738 which end up encoded in special integer types. Note that scalars can
5739 be given implicit initial values using the Default_Value aspect.
5741 The exception we need to make is then for packed arrays of records
5742 with discriminants or implicit initial values. We have no light/easy
5743 way to check for the latter case, so we merely check for packed arrays
5744 of records. This may lead to useless copy-in operations, but in very
5745 rare cases only, as these would be exceptions in a set of already
5746 exceptional situations. */
5747 if (Ekind (gnat_param) == E_Out_Parameter
5748 && !by_ref
5749 && (by_return
5750 || (!POINTER_TYPE_P (gnu_param_type)
5751 && !AGGREGATE_TYPE_P (gnu_param_type)
5752 && !Has_Default_Aspect (Etype (gnat_param))))
5753 && !(Is_Array_Type (Etype (gnat_param))
5754 && Is_Packed (Etype (gnat_param))
5755 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5756 return gnu_param_type;
5758 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5759 ro_param || by_ref || by_component_ptr);
5760 DECL_BY_REF_P (gnu_param) = by_ref;
5761 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5762 DECL_POINTS_TO_READONLY_P (gnu_param)
5763 = (ro_param && (by_ref || by_component_ptr));
5764 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5766 /* If no Mechanism was specified, indicate what we're using, then
5767 back-annotate it. */
5768 if (mech == Default)
5769 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5771 Set_Mechanism (gnat_param, mech);
5772 return gnu_param;
5775 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5776 with of the main unit and whose full view has not been elaborated yet. */
5778 static bool
5779 is_from_limited_with_of_main (Entity_Id gnat_entity)
5781 /* Class-wide types are always transformed into their root type. */
5782 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5783 gnat_entity = Root_Type (gnat_entity);
5785 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5786 && From_Limited_With (gnat_entity))
5788 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5790 if (present_gnu_tree (gnat_full_view))
5791 return false;
5793 return In_Extended_Main_Code_Unit (gnat_full_view);
5796 return false;
5799 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5800 qualifiers on TYPE. */
5802 static tree
5803 change_qualified_type (tree type, int type_quals)
5805 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5808 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5810 static bool
5811 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5813 while (Present (Corresponding_Discriminant (discr1)))
5814 discr1 = Corresponding_Discriminant (discr1);
5816 while (Present (Corresponding_Discriminant (discr2)))
5817 discr2 = Corresponding_Discriminant (discr2);
5819 return
5820 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5823 /* Return true if the array type GNU_TYPE, which represents a dimension of
5824 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5826 static bool
5827 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5829 /* If the array type is not the innermost dimension of the GNAT type,
5830 then it has a non-aliased component. */
5831 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5832 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5833 return true;
5835 /* If the array type has an aliased component in the front-end sense,
5836 then it also has an aliased component in the back-end sense. */
5837 if (Has_Aliased_Components (gnat_type))
5838 return false;
5840 /* If this is a derived type, then it has a non-aliased component if
5841 and only if its parent type also has one. */
5842 if (Is_Derived_Type (gnat_type))
5844 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5845 int index;
5846 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5847 gnu_parent_type
5848 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5849 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5850 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5851 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5854 /* Otherwise, rely exclusively on properties of the element type. */
5855 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5858 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5860 static bool
5861 compile_time_known_address_p (Node_Id gnat_address)
5863 /* Catch System'To_Address. */
5864 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5865 gnat_address = Expression (gnat_address);
5867 return Compile_Time_Known_Value (gnat_address);
5870 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5871 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5873 static bool
5874 cannot_be_superflat (Node_Id gnat_range)
5876 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5877 Node_Id scalar_range;
5878 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5880 /* If the low bound is not constant, try to find an upper bound. */
5881 while (Nkind (gnat_lb) != N_Integer_Literal
5882 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5883 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5884 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5885 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5886 || Nkind (scalar_range) == N_Range))
5887 gnat_lb = High_Bound (scalar_range);
5889 /* If the high bound is not constant, try to find a lower bound. */
5890 while (Nkind (gnat_hb) != N_Integer_Literal
5891 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5892 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5893 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5894 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5895 || Nkind (scalar_range) == N_Range))
5896 gnat_hb = Low_Bound (scalar_range);
5898 /* If we have failed to find constant bounds, punt. */
5899 if (Nkind (gnat_lb) != N_Integer_Literal
5900 || Nkind (gnat_hb) != N_Integer_Literal)
5901 return false;
5903 /* We need at least a signed 64-bit type to catch most cases. */
5904 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5905 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5906 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5907 return false;
5909 /* If the low bound is the smallest integer, nothing can be smaller. */
5910 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5911 if (TREE_OVERFLOW (gnu_lb_minus_one))
5912 return true;
5914 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5917 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5919 static bool
5920 constructor_address_p (tree gnu_expr)
5922 while (TREE_CODE (gnu_expr) == NOP_EXPR
5923 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5924 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5925 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5927 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5928 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5931 /* Return true if the size in units represented by GNU_SIZE can be handled by
5932 an allocation. If STATIC_P is true, consider only what can be done with a
5933 static allocation. */
5935 static bool
5936 allocatable_size_p (tree gnu_size, bool static_p)
5938 /* We can allocate a fixed size if it is a valid for the middle-end. */
5939 if (TREE_CODE (gnu_size) == INTEGER_CST)
5940 return valid_constant_size_p (gnu_size);
5942 /* We can allocate a variable size if this isn't a static allocation. */
5943 else
5944 return !static_p;
5947 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
5948 initial value of an object of GNU_TYPE. */
5950 static bool
5951 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
5953 /* Do not convert if the object's type is unconstrained because this would
5954 generate useless evaluations of the CONSTRUCTOR to compute the size. */
5955 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
5956 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5957 return false;
5959 /* Do not convert if the object's type is a padding record whose field is of
5960 self-referential size because we want to copy only the actual data. */
5961 if (type_is_padding_self_referential (gnu_type))
5962 return false;
5964 /* Do not convert a call to a function that returns with variable size since
5965 we want to use the return slot optimization in this case. */
5966 if (TREE_CODE (gnu_expr) == CALL_EXPR
5967 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
5968 return false;
5970 /* Do not convert to a record type with a variant part from a record type
5971 without one, to keep the object simpler. */
5972 if (TREE_CODE (gnu_type) == RECORD_TYPE
5973 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
5974 && get_variant_part (gnu_type) != NULL_TREE
5975 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE)
5976 return false;
5978 /* In all the other cases, convert the expression to the object's type. */
5979 return true;
5982 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5983 be elaborated at the point of its definition, but do nothing else. */
5985 void
5986 elaborate_entity (Entity_Id gnat_entity)
5988 switch (Ekind (gnat_entity))
5990 case E_Signed_Integer_Subtype:
5991 case E_Modular_Integer_Subtype:
5992 case E_Enumeration_Subtype:
5993 case E_Ordinary_Fixed_Point_Subtype:
5994 case E_Decimal_Fixed_Point_Subtype:
5995 case E_Floating_Point_Subtype:
5997 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5998 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6000 /* ??? Tests to avoid Constraint_Error in static expressions
6001 are needed until after the front stops generating bogus
6002 conversions on bounds of real types. */
6003 if (!Raises_Constraint_Error (gnat_lb))
6004 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6005 Needs_Debug_Info (gnat_entity));
6006 if (!Raises_Constraint_Error (gnat_hb))
6007 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6008 Needs_Debug_Info (gnat_entity));
6009 break;
6012 case E_Record_Subtype:
6013 case E_Private_Subtype:
6014 case E_Limited_Private_Subtype:
6015 case E_Record_Subtype_With_Private:
6016 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6018 Node_Id gnat_discriminant_expr;
6019 Entity_Id gnat_field;
6021 for (gnat_field
6022 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6023 gnat_discriminant_expr
6024 = First_Elmt (Discriminant_Constraint (gnat_entity));
6025 Present (gnat_field);
6026 gnat_field = Next_Discriminant (gnat_field),
6027 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6028 /* Ignore access discriminants. */
6029 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6030 elaborate_expression (Node (gnat_discriminant_expr),
6031 gnat_entity, get_entity_char (gnat_field),
6032 true, false, false);
6034 break;
6039 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6040 NAME, ARGS and ERROR_POINT. */
6042 static void
6043 prepend_one_attribute (struct attrib **attr_list,
6044 enum attr_type attr_type,
6045 tree attr_name,
6046 tree attr_args,
6047 Node_Id attr_error_point)
6049 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6051 attr->type = attr_type;
6052 attr->name = attr_name;
6053 attr->args = attr_args;
6054 attr->error_point = attr_error_point;
6056 attr->next = *attr_list;
6057 *attr_list = attr;
6060 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6062 static void
6063 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6065 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6066 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6067 enum attr_type etype;
6069 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6070 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6072 case Pragma_Machine_Attribute:
6073 etype = ATTR_MACHINE_ATTRIBUTE;
6074 break;
6076 case Pragma_Linker_Alias:
6077 etype = ATTR_LINK_ALIAS;
6078 break;
6080 case Pragma_Linker_Section:
6081 etype = ATTR_LINK_SECTION;
6082 break;
6084 case Pragma_Linker_Constructor:
6085 etype = ATTR_LINK_CONSTRUCTOR;
6086 break;
6088 case Pragma_Linker_Destructor:
6089 etype = ATTR_LINK_DESTRUCTOR;
6090 break;
6092 case Pragma_Weak_External:
6093 etype = ATTR_WEAK_EXTERNAL;
6094 break;
6096 case Pragma_Thread_Local_Storage:
6097 etype = ATTR_THREAD_LOCAL_STORAGE;
6098 break;
6100 default:
6101 return;
6104 /* See what arguments we have and turn them into GCC trees for attribute
6105 handlers. These expect identifier for strings. We handle at most two
6106 arguments and static expressions only. */
6107 if (Present (gnat_arg) && Present (First (gnat_arg)))
6109 Node_Id gnat_arg0 = Next (First (gnat_arg));
6110 Node_Id gnat_arg1 = Empty;
6112 if (Present (gnat_arg0)
6113 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6115 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6117 if (TREE_CODE (gnu_arg0) == STRING_CST)
6119 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6120 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6121 return;
6124 gnat_arg1 = Next (gnat_arg0);
6127 if (Present (gnat_arg1)
6128 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6130 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6132 if (TREE_CODE (gnu_arg1) == STRING_CST)
6133 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6137 /* Prepend to the list. Make a list of the argument we might have, as GCC
6138 expects it. */
6139 prepend_one_attribute (attr_list, etype, gnu_arg0,
6140 gnu_arg1
6141 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6142 Present (Next (First (gnat_arg)))
6143 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6146 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6148 static void
6149 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6151 Node_Id gnat_temp;
6153 /* Attributes are stored as Representation Item pragmas. */
6154 for (gnat_temp = First_Rep_Item (gnat_entity);
6155 Present (gnat_temp);
6156 gnat_temp = Next_Rep_Item (gnat_temp))
6157 if (Nkind (gnat_temp) == N_Pragma)
6158 prepend_one_attribute_pragma (attr_list, gnat_temp);
6161 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6162 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6163 return the GCC tree to use for that expression. S is the suffix to use
6164 if a variable needs to be created and DEFINITION is true if this is done
6165 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6166 otherwise, we are just elaborating the expression for side-effects. If
6167 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6168 isn't needed for code generation. */
6170 static tree
6171 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6172 bool definition, bool need_value, bool need_debug)
6174 tree gnu_expr;
6176 /* If we already elaborated this expression (e.g. it was involved
6177 in the definition of a private type), use the old value. */
6178 if (present_gnu_tree (gnat_expr))
6179 return get_gnu_tree (gnat_expr);
6181 /* If we don't need a value and this is static or a discriminant,
6182 we don't need to do anything. */
6183 if (!need_value
6184 && (Is_OK_Static_Expression (gnat_expr)
6185 || (Nkind (gnat_expr) == N_Identifier
6186 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6187 return NULL_TREE;
6189 /* If it's a static expression, we don't need a variable for debugging. */
6190 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6191 need_debug = false;
6193 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6194 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6195 definition, need_debug);
6197 /* Save the expression in case we try to elaborate this entity again. Since
6198 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6199 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6200 save_gnu_tree (gnat_expr, gnu_expr, true);
6202 return need_value ? gnu_expr : error_mark_node;
6205 /* Similar, but take a GNU expression and always return a result. */
6207 static tree
6208 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6209 bool definition, bool need_debug)
6211 const bool expr_public_p = Is_Public (gnat_entity);
6212 const bool expr_global_p = expr_public_p || global_bindings_p ();
6213 bool expr_variable_p, use_variable;
6215 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6216 that an expression cannot contain both a discriminant and a variable. */
6217 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6218 return gnu_expr;
6220 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6221 a variable that is initialized to contain the expression when the package
6222 containing the definition is elaborated. If this entity is defined at top
6223 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6224 if this is necessary. */
6225 if (TREE_CONSTANT (gnu_expr))
6226 expr_variable_p = false;
6227 else
6229 /* Skip any conversions and simple constant arithmetics to see if the
6230 expression is based on a read-only variable. */
6231 tree inner = remove_conversions (gnu_expr, true);
6233 inner = skip_simple_constant_arithmetic (inner);
6235 if (handled_component_p (inner))
6236 inner = get_inner_constant_reference (inner);
6238 expr_variable_p
6239 = !(inner
6240 && TREE_CODE (inner) == VAR_DECL
6241 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6244 /* We only need to use the variable if we are in a global context since GCC
6245 can do the right thing in the local case. However, when not optimizing,
6246 use it for bounds of loop iteration scheme to avoid code duplication. */
6247 use_variable = expr_variable_p
6248 && (expr_global_p
6249 || (!optimize
6250 && definition
6251 && Is_Itype (gnat_entity)
6252 && Nkind (Associated_Node_For_Itype (gnat_entity))
6253 == N_Loop_Parameter_Specification));
6255 /* Now create it, possibly only for debugging purposes. */
6256 if (use_variable || need_debug)
6258 /* The following variable creation can happen when processing the body
6259 of subprograms that are defined out of the extended main unit and
6260 inlined. In this case, we are not at the global scope, and thus the
6261 new variable must not be tagged "external", as we used to do here as
6262 soon as DEFINITION was false. */
6263 tree gnu_decl
6264 = create_var_decl_1 (create_concat_name (gnat_entity, s), NULL_TREE,
6265 TREE_TYPE (gnu_expr), gnu_expr, true,
6266 expr_public_p, !definition && expr_global_p,
6267 expr_global_p, !need_debug, NULL, gnat_entity);
6269 /* Whether or not gnat_entity comes from source, this variable is a
6270 compilation artifact. */
6271 DECL_ARTIFICIAL (gnu_decl) = 1;
6273 /* Using this variable at debug time (if need_debug is true) requires a
6274 proper location. The back-end will compute a location for this
6275 variable only if the variable is used by the generated code.
6276 Returning the variable ensures the caller will use it in generated
6277 code. Note that there is no need for a location if the debug info
6278 contains an integer constant.
6279 FIXME: when the encoding-based debug scheme is dropped, move this
6280 condition to the top-level IF block: we will not need to create a
6281 variable anymore in such cases, then. */
6282 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6283 return gnu_decl;
6286 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6289 /* Similar, but take an alignment factor and make it explicit in the tree. */
6291 static tree
6292 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6293 bool definition, bool need_debug, unsigned int align)
6295 tree unit_align = size_int (align / BITS_PER_UNIT);
6296 return
6297 size_binop (MULT_EXPR,
6298 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6299 gnu_expr,
6300 unit_align),
6301 gnat_entity, s, definition,
6302 need_debug),
6303 unit_align);
6306 /* Structure to hold internal data for elaborate_reference. */
6308 struct er_data
6310 Entity_Id entity;
6311 bool definition;
6312 unsigned int n;
6315 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6317 static tree
6318 elaborate_reference_1 (tree ref, void *data)
6320 struct er_data *er = (struct er_data *)data;
6321 char suffix[16];
6323 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6324 if (TREE_CONSTANT (ref))
6325 return ref;
6327 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6328 pointer. This may be more efficient, but will also allow us to more
6329 easily find the match for the PLACEHOLDER_EXPR. */
6330 if (TREE_CODE (ref) == COMPONENT_REF
6331 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6332 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6333 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6334 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
6336 sprintf (suffix, "EXP%d", ++er->n);
6337 return
6338 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6341 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6342 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6343 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6345 static tree
6346 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6347 tree *init)
6349 struct er_data er = { gnat_entity, definition, 0 };
6350 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6353 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6354 the value passed against the list of choices. */
6356 tree
6357 choices_to_gnu (tree operand, Node_Id choices)
6359 Node_Id choice;
6360 Node_Id gnat_temp;
6361 tree result = boolean_false_node;
6362 tree this_test, low = 0, high = 0, single = 0;
6364 for (choice = First (choices); Present (choice); choice = Next (choice))
6366 switch (Nkind (choice))
6368 case N_Range:
6369 low = gnat_to_gnu (Low_Bound (choice));
6370 high = gnat_to_gnu (High_Bound (choice));
6372 this_test
6373 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6374 build_binary_op (GE_EXPR, boolean_type_node,
6375 operand, low),
6376 build_binary_op (LE_EXPR, boolean_type_node,
6377 operand, high));
6379 break;
6381 case N_Subtype_Indication:
6382 gnat_temp = Range_Expression (Constraint (choice));
6383 low = gnat_to_gnu (Low_Bound (gnat_temp));
6384 high = gnat_to_gnu (High_Bound (gnat_temp));
6386 this_test
6387 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6388 build_binary_op (GE_EXPR, boolean_type_node,
6389 operand, low),
6390 build_binary_op (LE_EXPR, boolean_type_node,
6391 operand, high));
6392 break;
6394 case N_Identifier:
6395 case N_Expanded_Name:
6396 /* This represents either a subtype range, an enumeration
6397 literal, or a constant Ekind says which. If an enumeration
6398 literal or constant, fall through to the next case. */
6399 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6400 && Ekind (Entity (choice)) != E_Constant)
6402 tree type = gnat_to_gnu_type (Entity (choice));
6404 low = TYPE_MIN_VALUE (type);
6405 high = TYPE_MAX_VALUE (type);
6407 this_test
6408 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6409 build_binary_op (GE_EXPR, boolean_type_node,
6410 operand, low),
6411 build_binary_op (LE_EXPR, boolean_type_node,
6412 operand, high));
6413 break;
6416 /* ... fall through ... */
6418 case N_Character_Literal:
6419 case N_Integer_Literal:
6420 single = gnat_to_gnu (choice);
6421 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6422 single);
6423 break;
6425 case N_Others_Choice:
6426 this_test = boolean_true_node;
6427 break;
6429 default:
6430 gcc_unreachable ();
6433 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6434 this_test);
6437 return result;
6440 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6441 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6443 static int
6444 adjust_packed (tree field_type, tree record_type, int packed)
6446 /* If the field contains an item of variable size, we cannot pack it
6447 because we cannot create temporaries of non-fixed size in case
6448 we need to take the address of the field. See addressable_p and
6449 the notes on the addressability issues for further details. */
6450 if (type_has_variable_size (field_type))
6451 return 0;
6453 /* If the alignment of the record is specified and the field type
6454 is over-aligned, request Storage_Unit alignment for the field. */
6455 if (packed == -2)
6457 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6458 return -1;
6459 else
6460 return 0;
6463 return packed;
6466 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6467 placed in GNU_RECORD_TYPE.
6469 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6470 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6471 record has a specified alignment.
6473 DEFINITION is true if this field is for a record being defined.
6475 DEBUG_INFO_P is true if we need to write debug information for types
6476 that we may create in the process. */
6478 static tree
6479 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6480 bool definition, bool debug_info_p)
6482 const Entity_Id gnat_field_type = Etype (gnat_field);
6483 const bool is_aliased
6484 = Is_Aliased (gnat_field);
6485 const bool is_atomic
6486 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6487 const bool is_independent
6488 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6489 const bool is_volatile
6490 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6491 const bool needs_strict_alignment
6492 = (is_aliased
6493 || is_independent
6494 || is_volatile
6495 || Strict_Alignment (gnat_field_type));
6496 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6497 tree gnu_field_id = get_entity_name (gnat_field);
6498 tree gnu_field, gnu_size, gnu_pos;
6500 /* If this field requires strict alignment, we cannot pack it because
6501 it would very likely be under-aligned in the record. */
6502 if (needs_strict_alignment)
6503 packed = 0;
6504 else
6505 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6507 /* If a size is specified, use it. Otherwise, if the record type is packed,
6508 use the official RM size. See "Handling of Type'Size Values" in Einfo
6509 for further details. */
6510 if (Known_Esize (gnat_field))
6511 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6512 gnat_field, FIELD_DECL, false, true);
6513 else if (packed == 1)
6514 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6515 gnat_field, FIELD_DECL, false, true);
6516 else
6517 gnu_size = NULL_TREE;
6519 /* If we have a specified size that is smaller than that of the field's type,
6520 or a position is specified, and the field's type is a record that doesn't
6521 require strict alignment, see if we can get either an integral mode form
6522 of the type or a smaller form. If we can, show a size was specified for
6523 the field if there wasn't one already, so we know to make this a bitfield
6524 and avoid making things wider.
6526 Changing to an integral mode form is useful when the record is packed as
6527 we can then place the field at a non-byte-aligned position and so achieve
6528 tighter packing. This is in addition required if the field shares a byte
6529 with another field and the front-end lets the back-end handle the access
6530 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6532 Changing to a smaller form is required if the specified size is smaller
6533 than that of the field's type and the type contains sub-fields that are
6534 padded, in order to avoid generating accesses to these sub-fields that
6535 are wider than the field.
6537 We avoid the transformation if it is not required or potentially useful,
6538 as it might entail an increase of the field's alignment and have ripple
6539 effects on the outer record type. A typical case is a field known to be
6540 byte-aligned and not to share a byte with another field. */
6541 if (!needs_strict_alignment
6542 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6543 && !TYPE_FAT_POINTER_P (gnu_field_type)
6544 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6545 && (packed == 1
6546 || (gnu_size
6547 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6548 || (Present (Component_Clause (gnat_field))
6549 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6550 % BITS_PER_UNIT == 0
6551 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6553 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6554 if (gnu_packable_type != gnu_field_type)
6556 gnu_field_type = gnu_packable_type;
6557 if (!gnu_size)
6558 gnu_size = rm_size (gnu_field_type);
6562 if (Is_Atomic_Or_VFA (gnat_field))
6563 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6565 if (Present (Component_Clause (gnat_field)))
6567 Node_Id gnat_clause = Component_Clause (gnat_field);
6568 Entity_Id gnat_parent
6569 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6571 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6572 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6573 gnat_field, FIELD_DECL, false, true);
6575 /* Ensure the position does not overlap with the parent subtype, if there
6576 is one. This test is omitted if the parent of the tagged type has a
6577 full rep clause since, in this case, component clauses are allowed to
6578 overlay the space allocated for the parent type and the front-end has
6579 checked that there are no overlapping components. */
6580 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6582 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6584 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6585 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6586 post_error_ne_tree
6587 ("offset of& must be beyond parent{, minimum allowed is ^}",
6588 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6591 /* If this field needs strict alignment, make sure that the record is
6592 sufficiently aligned and that the position and size are consistent
6593 with the type. But don't do it if we are just annotating types and
6594 the field's type is tagged, since tagged types aren't fully laid out
6595 in this mode. Also, note that atomic implies volatile so the inner
6596 test sequences ordering is significant here. */
6597 if (needs_strict_alignment
6598 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6600 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6602 if (TYPE_ALIGN (gnu_record_type) < type_align)
6603 TYPE_ALIGN (gnu_record_type) = type_align;
6605 /* If the position is not a multiple of the alignment of the type,
6606 then error out and reset the position. */
6607 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6608 bitsize_int (type_align))))
6610 const char *s;
6612 if (is_atomic)
6613 s = "position of atomic field& must be multiple of ^ bits";
6614 else if (is_aliased)
6615 s = "position of aliased field& must be multiple of ^ bits";
6616 else if (is_independent)
6617 s = "position of independent field& must be multiple of ^ bits";
6618 else if (is_volatile)
6619 s = "position of volatile field& must be multiple of ^ bits";
6620 else if (Strict_Alignment (gnat_field_type))
6621 s = "position of & with aliased or tagged part must be"
6622 " multiple of ^ bits";
6623 else
6624 gcc_unreachable ();
6626 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6627 type_align);
6628 gnu_pos = NULL_TREE;
6631 if (gnu_size)
6633 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6634 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6636 /* If the size is lower than that of the type, or greater for
6637 atomic and aliased, then error out and reset the size. */
6638 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6640 const char *s;
6642 if (is_atomic)
6643 s = "size of atomic field& must be ^ bits";
6644 else if (is_aliased)
6645 s = "size of aliased field& must be ^ bits";
6646 else if (is_independent)
6647 s = "size of independent field& must be at least ^ bits";
6648 else if (is_volatile)
6649 s = "size of volatile field& must be at least ^ bits";
6650 else if (Strict_Alignment (gnat_field_type))
6651 s = "size of & with aliased or tagged part must be"
6652 " at least ^ bits";
6653 else
6654 gcc_unreachable ();
6656 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6657 gnu_type_size);
6658 gnu_size = NULL_TREE;
6661 /* Likewise if the size is not a multiple of a byte, */
6662 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6663 bitsize_unit_node)))
6665 const char *s;
6667 if (is_independent)
6668 s = "size of independent field& must be multiple of"
6669 " Storage_Unit";
6670 else if (is_volatile)
6671 s = "size of volatile field& must be multiple of"
6672 " Storage_Unit";
6673 else if (Strict_Alignment (gnat_field_type))
6674 s = "size of & with aliased or tagged part must be"
6675 " multiple of Storage_Unit";
6676 else
6677 gcc_unreachable ();
6679 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6680 gnu_size = NULL_TREE;
6686 /* If the record has rep clauses and this is the tag field, make a rep
6687 clause for it as well. */
6688 else if (Has_Specified_Layout (Scope (gnat_field))
6689 && Chars (gnat_field) == Name_uTag)
6691 gnu_pos = bitsize_zero_node;
6692 gnu_size = TYPE_SIZE (gnu_field_type);
6695 else
6697 gnu_pos = NULL_TREE;
6699 /* If we are packing the record and the field is BLKmode, round the
6700 size up to a byte boundary. */
6701 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6702 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6705 /* We need to make the size the maximum for the type if it is
6706 self-referential and an unconstrained type. In that case, we can't
6707 pack the field since we can't make a copy to align it. */
6708 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6709 && !gnu_size
6710 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6711 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6713 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6714 packed = 0;
6717 /* If a size is specified, adjust the field's type to it. */
6718 if (gnu_size)
6720 tree orig_field_type;
6722 /* If the field's type is justified modular, we would need to remove
6723 the wrapper to (better) meet the layout requirements. However we
6724 can do so only if the field is not aliased to preserve the unique
6725 layout and if the prescribed size is not greater than that of the
6726 packed array to preserve the justification. */
6727 if (!needs_strict_alignment
6728 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6729 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6730 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6731 <= 0)
6732 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6734 /* Similarly if the field's type is a misaligned integral type, but
6735 there is no restriction on the size as there is no justification. */
6736 if (!needs_strict_alignment
6737 && TYPE_IS_PADDING_P (gnu_field_type)
6738 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6739 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6741 gnu_field_type
6742 = make_type_from_size (gnu_field_type, gnu_size,
6743 Has_Biased_Representation (gnat_field));
6745 orig_field_type = gnu_field_type;
6746 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6747 false, false, definition, true);
6749 /* If a padding record was made, declare it now since it will never be
6750 declared otherwise. This is necessary to ensure that its subtrees
6751 are properly marked. */
6752 if (gnu_field_type != orig_field_type
6753 && !DECL_P (TYPE_NAME (gnu_field_type)))
6754 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6755 debug_info_p, gnat_field);
6758 /* Otherwise (or if there was an error), don't specify a position. */
6759 else
6760 gnu_pos = NULL_TREE;
6762 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6763 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6765 /* Now create the decl for the field. */
6766 gnu_field
6767 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6768 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6769 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6770 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6771 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6773 if (Ekind (gnat_field) == E_Discriminant)
6774 DECL_DISCRIMINANT_NUMBER (gnu_field)
6775 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6777 return gnu_field;
6780 /* Return true if at least one member of COMPONENT_LIST needs strict
6781 alignment. */
6783 static bool
6784 components_need_strict_alignment (Node_Id component_list)
6786 Node_Id component_decl;
6788 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6789 Present (component_decl);
6790 component_decl = Next_Non_Pragma (component_decl))
6792 Entity_Id gnat_field = Defining_Entity (component_decl);
6794 if (Is_Aliased (gnat_field))
6795 return true;
6797 if (Strict_Alignment (Etype (gnat_field)))
6798 return true;
6801 return false;
6804 /* Return true if TYPE is a type with variable size or a padding type with a
6805 field of variable size or a record that has a field with such a type. */
6807 static bool
6808 type_has_variable_size (tree type)
6810 tree field;
6812 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6813 return true;
6815 if (TYPE_IS_PADDING_P (type)
6816 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6817 return true;
6819 if (!RECORD_OR_UNION_TYPE_P (type))
6820 return false;
6822 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6823 if (type_has_variable_size (TREE_TYPE (field)))
6824 return true;
6826 return false;
6829 /* Return true if FIELD is an artificial field. */
6831 static bool
6832 field_is_artificial (tree field)
6834 /* These fields are generated by the front-end proper. */
6835 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6836 return true;
6838 /* These fields are generated by gigi. */
6839 if (DECL_INTERNAL_P (field))
6840 return true;
6842 return false;
6845 /* Return true if FIELD is a non-artificial aliased field. */
6847 static bool
6848 field_is_aliased (tree field)
6850 if (field_is_artificial (field))
6851 return false;
6853 return DECL_ALIASED_P (field);
6856 /* Return true if FIELD is a non-artificial field with self-referential
6857 size. */
6859 static bool
6860 field_has_self_size (tree field)
6862 if (field_is_artificial (field))
6863 return false;
6865 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6866 return false;
6868 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6871 /* Return true if FIELD is a non-artificial field with variable size. */
6873 static bool
6874 field_has_variable_size (tree field)
6876 if (field_is_artificial (field))
6877 return false;
6879 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6880 return false;
6882 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6885 /* qsort comparer for the bit positions of two record components. */
6887 static int
6888 compare_field_bitpos (const PTR rt1, const PTR rt2)
6890 const_tree const field1 = * (const_tree const *) rt1;
6891 const_tree const field2 = * (const_tree const *) rt2;
6892 const int ret
6893 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6895 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6898 /* Structure holding information for a given variant. */
6899 typedef struct vinfo
6901 /* The record type of the variant. */
6902 tree type;
6904 /* The name of the variant. */
6905 tree name;
6907 /* The qualifier of the variant. */
6908 tree qual;
6910 /* Whether the variant has a rep clause. */
6911 bool has_rep;
6913 /* Whether the variant is packed. */
6914 bool packed;
6916 } vinfo_t;
6918 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6919 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6920 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6921 When called from gnat_to_gnu_entity during the processing of a record type
6922 definition, the GCC node for the parent, if any, will be the single field
6923 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6924 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6925 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6927 PACKED is 1 if this is for a packed record, -1 if this is for a record
6928 with Component_Alignment of Storage_Unit, -2 if this is for a record
6929 with a specified alignment.
6931 DEFINITION is true if we are defining this record type.
6933 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6934 out the record. This means the alignment only serves to force fields to
6935 be bitfields, but not to require the record to be that aligned. This is
6936 used for variants.
6938 ALL_REP is true if a rep clause is present for all the fields.
6940 UNCHECKED_UNION is true if we are building this type for a record with a
6941 Pragma Unchecked_Union.
6943 ARTIFICIAL is true if this is a type that was generated by the compiler.
6945 DEBUG_INFO is true if we need to write debug information about the type.
6947 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6948 mean that its contents may be unused as well, only the container itself.
6950 REORDER is true if we are permitted to reorder components of this type.
6952 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6953 the outer record type down to this variant level. It is nonzero only if
6954 all the fields down to this level have a rep clause and ALL_REP is false.
6956 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6957 with a rep clause is to be added; in this case, that is all that should
6958 be done with such fields and the return value will be false. */
6960 static bool
6961 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6962 tree gnu_field_list, int packed, bool definition,
6963 bool cancel_alignment, bool all_rep,
6964 bool unchecked_union, bool artificial,
6965 bool debug_info, bool maybe_unused, bool reorder,
6966 tree first_free_pos, tree *p_gnu_rep_list)
6968 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6969 bool variants_have_rep = all_rep;
6970 bool layout_with_rep = false;
6971 bool has_self_field = false;
6972 bool has_aliased_after_self_field = false;
6973 Node_Id component_decl, variant_part;
6974 tree gnu_field, gnu_next, gnu_last;
6975 tree gnu_variant_part = NULL_TREE;
6976 tree gnu_rep_list = NULL_TREE;
6977 tree gnu_var_list = NULL_TREE;
6978 tree gnu_self_list = NULL_TREE;
6979 tree gnu_zero_list = NULL_TREE;
6981 /* For each component referenced in a component declaration create a GCC
6982 field and add it to the list, skipping pragmas in the GNAT list. */
6983 gnu_last = tree_last (gnu_field_list);
6984 if (Present (Component_Items (gnat_component_list)))
6985 for (component_decl
6986 = First_Non_Pragma (Component_Items (gnat_component_list));
6987 Present (component_decl);
6988 component_decl = Next_Non_Pragma (component_decl))
6990 Entity_Id gnat_field = Defining_Entity (component_decl);
6991 Name_Id gnat_name = Chars (gnat_field);
6993 /* If present, the _Parent field must have been created as the single
6994 field of the record type. Put it before any other fields. */
6995 if (gnat_name == Name_uParent)
6997 gnu_field = TYPE_FIELDS (gnu_record_type);
6998 gnu_field_list = chainon (gnu_field_list, gnu_field);
7000 else
7002 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7003 definition, debug_info);
7005 /* If this is the _Tag field, put it before any other fields. */
7006 if (gnat_name == Name_uTag)
7007 gnu_field_list = chainon (gnu_field_list, gnu_field);
7009 /* If this is the _Controller field, put it before the other
7010 fields except for the _Tag or _Parent field. */
7011 else if (gnat_name == Name_uController && gnu_last)
7013 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7014 DECL_CHAIN (gnu_last) = gnu_field;
7017 /* If this is a regular field, put it after the other fields. */
7018 else
7020 DECL_CHAIN (gnu_field) = gnu_field_list;
7021 gnu_field_list = gnu_field;
7022 if (!gnu_last)
7023 gnu_last = gnu_field;
7025 /* And record information for the final layout. */
7026 if (field_has_self_size (gnu_field))
7027 has_self_field = true;
7028 else if (has_self_field && field_is_aliased (gnu_field))
7029 has_aliased_after_self_field = true;
7033 save_gnu_tree (gnat_field, gnu_field, false);
7036 /* At the end of the component list there may be a variant part. */
7037 variant_part = Variant_Part (gnat_component_list);
7039 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7040 mutually exclusive and should go in the same memory. To do this we need
7041 to treat each variant as a record whose elements are created from the
7042 component list for the variant. So here we create the records from the
7043 lists for the variants and put them all into the QUAL_UNION_TYPE.
7044 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7045 use GNU_RECORD_TYPE if there are no fields so far. */
7046 if (Present (variant_part))
7048 Node_Id gnat_discr = Name (variant_part), variant;
7049 tree gnu_discr = gnat_to_gnu (gnat_discr);
7050 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7051 tree gnu_var_name
7052 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7053 "XVN");
7054 tree gnu_union_type, gnu_union_name;
7055 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7056 bool union_field_needs_strict_alignment = false;
7057 auto_vec <vinfo_t, 16> variant_types;
7058 vinfo_t *gnu_variant;
7059 unsigned int variants_align = 0;
7060 unsigned int i;
7062 gnu_union_name
7063 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7065 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7066 are all in the variant part, to match the layout of C unions. There
7067 is an associated check below. */
7068 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7069 gnu_union_type = gnu_record_type;
7070 else
7072 gnu_union_type
7073 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7075 TYPE_NAME (gnu_union_type) = gnu_union_name;
7076 TYPE_ALIGN (gnu_union_type) = 0;
7077 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7080 /* If all the fields down to this level have a rep clause, find out
7081 whether all the fields at this level also have one. If so, then
7082 compute the new first free position to be passed downward. */
7083 this_first_free_pos = first_free_pos;
7084 if (this_first_free_pos)
7086 for (gnu_field = gnu_field_list;
7087 gnu_field;
7088 gnu_field = DECL_CHAIN (gnu_field))
7089 if (DECL_FIELD_OFFSET (gnu_field))
7091 tree pos = bit_position (gnu_field);
7092 if (!tree_int_cst_lt (pos, this_first_free_pos))
7093 this_first_free_pos
7094 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7096 else
7098 this_first_free_pos = NULL_TREE;
7099 break;
7103 /* We build the variants in two passes. The bulk of the work is done in
7104 the first pass, that is to say translating the GNAT nodes, building
7105 the container types and computing the associated properties. However
7106 we cannot finish up the container types during this pass because we
7107 don't know where the variant part will be placed until the end. */
7108 for (variant = First_Non_Pragma (Variants (variant_part));
7109 Present (variant);
7110 variant = Next_Non_Pragma (variant))
7112 tree gnu_variant_type = make_node (RECORD_TYPE);
7113 tree gnu_inner_name, gnu_qual;
7114 bool has_rep;
7115 int field_packed;
7116 vinfo_t vinfo;
7118 Get_Variant_Encoding (variant);
7119 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7120 TYPE_NAME (gnu_variant_type)
7121 = concat_name (gnu_union_name,
7122 IDENTIFIER_POINTER (gnu_inner_name));
7124 /* Set the alignment of the inner type in case we need to make
7125 inner objects into bitfields, but then clear it out so the
7126 record actually gets only the alignment required. */
7127 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7128 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7130 /* Similarly, if the outer record has a size specified and all
7131 the fields have a rep clause, we can propagate the size. */
7132 if (all_rep_and_size)
7134 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7135 TYPE_SIZE_UNIT (gnu_variant_type)
7136 = TYPE_SIZE_UNIT (gnu_record_type);
7139 /* Add the fields into the record type for the variant. Note that
7140 we aren't sure to really use it at this point, see below. */
7141 has_rep
7142 = components_to_record (gnu_variant_type, Component_List (variant),
7143 NULL_TREE, packed, definition,
7144 !all_rep_and_size, all_rep,
7145 unchecked_union,
7146 true, debug_info, true, reorder,
7147 this_first_free_pos,
7148 all_rep || this_first_free_pos
7149 ? NULL : &gnu_rep_list);
7151 /* Translate the qualifier and annotate the GNAT node. */
7152 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7153 Set_Present_Expr (variant, annotate_value (gnu_qual));
7155 /* Deal with packedness like in gnat_to_gnu_field. */
7156 if (components_need_strict_alignment (Component_List (variant)))
7158 field_packed = 0;
7159 union_field_needs_strict_alignment = true;
7161 else
7162 field_packed
7163 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7165 /* Push this variant onto the stack for the second pass. */
7166 vinfo.type = gnu_variant_type;
7167 vinfo.name = gnu_inner_name;
7168 vinfo.qual = gnu_qual;
7169 vinfo.has_rep = has_rep;
7170 vinfo.packed = field_packed;
7171 variant_types.safe_push (vinfo);
7173 /* Compute the global properties that will determine the placement of
7174 the variant part. */
7175 variants_have_rep |= has_rep;
7176 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7177 variants_align = TYPE_ALIGN (gnu_variant_type);
7180 /* Round up the first free position to the alignment of the variant part
7181 for the variants without rep clause. This will guarantee a consistent
7182 layout independently of the placement of the variant part. */
7183 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7184 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7186 /* In the second pass, the container types are adjusted if necessary and
7187 finished up, then the corresponding fields of the variant part are
7188 built with their qualifier, unless this is an unchecked union. */
7189 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7191 tree gnu_variant_type = gnu_variant->type;
7192 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7194 /* If this is an Unchecked_Union whose fields are all in the variant
7195 part and we have a single field with no representation clause or
7196 placed at offset zero, use the field directly to match the layout
7197 of C unions. */
7198 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7199 && gnu_field_list
7200 && !DECL_CHAIN (gnu_field_list)
7201 && (!DECL_FIELD_OFFSET (gnu_field_list)
7202 || integer_zerop (bit_position (gnu_field_list))))
7204 gnu_field = gnu_field_list;
7205 DECL_CONTEXT (gnu_field) = gnu_record_type;
7207 else
7209 /* Finalize the variant type now. We used to throw away empty
7210 record types but we no longer do that because we need them to
7211 generate complete debug info for the variant; otherwise, the
7212 union type definition will be lacking the fields associated
7213 with these empty variants. */
7214 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7216 /* The variant part will be at offset 0 so we need to ensure
7217 that the fields are laid out starting from the first free
7218 position at this level. */
7219 tree gnu_rep_type = make_node (RECORD_TYPE);
7220 tree gnu_rep_part;
7221 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7222 gnu_rep_part
7223 = create_rep_part (gnu_rep_type, gnu_variant_type,
7224 this_first_free_pos);
7225 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7226 gnu_field_list = gnu_rep_part;
7227 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7228 false);
7231 if (debug_info)
7232 rest_of_record_type_compilation (gnu_variant_type);
7233 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7234 true, debug_info, gnat_component_list);
7236 gnu_field
7237 = create_field_decl (gnu_variant->name, gnu_variant_type,
7238 gnu_union_type,
7239 all_rep_and_size
7240 ? TYPE_SIZE (gnu_variant_type) : 0,
7241 variants_have_rep ? bitsize_zero_node : 0,
7242 gnu_variant->packed, 0);
7244 DECL_INTERNAL_P (gnu_field) = 1;
7246 if (!unchecked_union)
7247 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7250 DECL_CHAIN (gnu_field) = gnu_variant_list;
7251 gnu_variant_list = gnu_field;
7254 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7255 if (gnu_variant_list)
7257 int union_field_packed;
7259 if (all_rep_and_size)
7261 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7262 TYPE_SIZE_UNIT (gnu_union_type)
7263 = TYPE_SIZE_UNIT (gnu_record_type);
7266 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7267 all_rep_and_size ? 1 : 0, debug_info);
7269 /* If GNU_UNION_TYPE is our record type, it means we must have an
7270 Unchecked_Union with no fields. Verify that and, if so, just
7271 return. */
7272 if (gnu_union_type == gnu_record_type)
7274 gcc_assert (unchecked_union
7275 && !gnu_field_list
7276 && !gnu_rep_list);
7277 return variants_have_rep;
7280 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7281 debug_info, gnat_component_list);
7283 /* Deal with packedness like in gnat_to_gnu_field. */
7284 if (union_field_needs_strict_alignment)
7285 union_field_packed = 0;
7286 else
7287 union_field_packed
7288 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7290 gnu_variant_part
7291 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7292 all_rep_and_size
7293 ? TYPE_SIZE (gnu_union_type) : 0,
7294 variants_have_rep ? bitsize_zero_node : 0,
7295 union_field_packed, 0);
7297 DECL_INTERNAL_P (gnu_variant_part) = 1;
7301 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7302 permitted to reorder components, self-referential sizes or variable sizes.
7303 If they do, pull them out and put them onto the appropriate list. We have
7304 to do this in a separate pass since we want to handle the discriminants
7305 but can't play with them until we've used them in debugging data above.
7307 Similarly, pull out the fields with zero size and no rep clause, as they
7308 would otherwise modify the layout and thus very likely run afoul of the
7309 Ada semantics, which are different from those of C here.
7311 ??? If we reorder them, debugging information will be wrong but there is
7312 nothing that can be done about this at the moment. */
7313 gnu_last = NULL_TREE;
7315 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7316 do { \
7317 if (gnu_last) \
7318 DECL_CHAIN (gnu_last) = gnu_next; \
7319 else \
7320 gnu_field_list = gnu_next; \
7322 DECL_CHAIN (gnu_field) = (LIST); \
7323 (LIST) = gnu_field; \
7324 } while (0)
7326 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7328 gnu_next = DECL_CHAIN (gnu_field);
7330 if (DECL_FIELD_OFFSET (gnu_field))
7332 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7333 continue;
7336 if ((reorder || has_aliased_after_self_field)
7337 && field_has_self_size (gnu_field))
7339 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7340 continue;
7343 if (reorder && field_has_variable_size (gnu_field))
7345 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7346 continue;
7349 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7351 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7352 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7353 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7354 if (field_is_aliased (gnu_field))
7355 TYPE_ALIGN (gnu_record_type)
7356 = MAX (TYPE_ALIGN (gnu_record_type),
7357 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7358 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7359 continue;
7362 gnu_last = gnu_field;
7365 #undef MOVE_FROM_FIELD_LIST_TO
7367 gnu_field_list = nreverse (gnu_field_list);
7369 /* If permitted, we reorder the fields as follows:
7371 1) all fixed length fields,
7372 2) all fields whose length doesn't depend on discriminants,
7373 3) all fields whose length depends on discriminants,
7374 4) the variant part,
7376 within the record and within each variant recursively. */
7377 if (reorder)
7378 gnu_field_list
7379 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7381 /* Otherwise, if there is an aliased field placed after a field whose length
7382 depends on discriminants, we put all the fields of the latter sort, last.
7383 We need to do this in case an object of this record type is mutable. */
7384 else if (has_aliased_after_self_field)
7385 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7387 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7388 in our REP list to the previous level because this level needs them in
7389 order to do a correct layout, i.e. avoid having overlapping fields. */
7390 if (p_gnu_rep_list && gnu_rep_list)
7391 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7393 /* Otherwise, sort the fields by bit position and put them into their own
7394 record, before the others, if we also have fields without rep clause. */
7395 else if (gnu_rep_list)
7397 tree gnu_rep_type, gnu_rep_part;
7398 int i, len = list_length (gnu_rep_list);
7399 tree *gnu_arr = XALLOCAVEC (tree, len);
7401 /* If all the fields have a rep clause, we can do a flat layout. */
7402 layout_with_rep = !gnu_field_list
7403 && (!gnu_variant_part || variants_have_rep);
7404 gnu_rep_type
7405 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7407 for (gnu_field = gnu_rep_list, i = 0;
7408 gnu_field;
7409 gnu_field = DECL_CHAIN (gnu_field), i++)
7410 gnu_arr[i] = gnu_field;
7412 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7414 /* Put the fields in the list in order of increasing position, which
7415 means we start from the end. */
7416 gnu_rep_list = NULL_TREE;
7417 for (i = len - 1; i >= 0; i--)
7419 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7420 gnu_rep_list = gnu_arr[i];
7421 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7424 if (layout_with_rep)
7425 gnu_field_list = gnu_rep_list;
7426 else
7428 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7430 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7431 without rep clause are laid out starting from this position.
7432 Therefore, we force it as a minimal size on the REP part. */
7433 gnu_rep_part
7434 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7436 /* Chain the REP part at the beginning of the field list. */
7437 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7438 gnu_field_list = gnu_rep_part;
7442 /* Chain the variant part at the end of the field list. */
7443 if (gnu_variant_part)
7444 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7446 if (cancel_alignment)
7447 TYPE_ALIGN (gnu_record_type) = 0;
7449 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7451 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7452 debug_info && !maybe_unused);
7454 /* Chain the fields with zero size at the beginning of the field list. */
7455 if (gnu_zero_list)
7456 TYPE_FIELDS (gnu_record_type)
7457 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7459 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7462 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7463 placed into an Esize, Component_Bit_Offset, or Component_Size value
7464 in the GNAT tree. */
7466 static Uint
7467 annotate_value (tree gnu_size)
7469 TCode tcode;
7470 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7471 struct tree_int_map in;
7472 int i;
7474 /* See if we've already saved the value for this node. */
7475 if (EXPR_P (gnu_size))
7477 struct tree_int_map *e;
7479 in.base.from = gnu_size;
7480 e = annotate_value_cache->find (&in);
7482 if (e)
7483 return (Node_Ref_Or_Val) e->to;
7485 else
7486 in.base.from = NULL_TREE;
7488 /* If we do not return inside this switch, TCODE will be set to the
7489 code to use for a Create_Node operand and LEN (set above) will be
7490 the number of recursive calls for us to make. */
7492 switch (TREE_CODE (gnu_size))
7494 case INTEGER_CST:
7495 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7497 case COMPONENT_REF:
7498 /* The only case we handle here is a simple discriminant reference. */
7499 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7501 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7503 /* Climb up the chain of successive extensions, if any. */
7504 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7505 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7506 == parent_name_id)
7507 gnu_size = TREE_OPERAND (gnu_size, 0);
7509 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7510 return
7511 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7514 return No_Uint;
7516 CASE_CONVERT: case NON_LVALUE_EXPR:
7517 return annotate_value (TREE_OPERAND (gnu_size, 0));
7519 /* Now just list the operations we handle. */
7520 case COND_EXPR: tcode = Cond_Expr; break;
7521 case PLUS_EXPR: tcode = Plus_Expr; break;
7522 case MINUS_EXPR: tcode = Minus_Expr; break;
7523 case MULT_EXPR: tcode = Mult_Expr; break;
7524 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7525 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7526 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7527 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7528 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7529 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7530 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7531 case NEGATE_EXPR: tcode = Negate_Expr; break;
7532 case MIN_EXPR: tcode = Min_Expr; break;
7533 case MAX_EXPR: tcode = Max_Expr; break;
7534 case ABS_EXPR: tcode = Abs_Expr; break;
7535 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7536 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7537 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7538 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7539 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7540 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7541 case LT_EXPR: tcode = Lt_Expr; break;
7542 case LE_EXPR: tcode = Le_Expr; break;
7543 case GT_EXPR: tcode = Gt_Expr; break;
7544 case GE_EXPR: tcode = Ge_Expr; break;
7545 case EQ_EXPR: tcode = Eq_Expr; break;
7546 case NE_EXPR: tcode = Ne_Expr; break;
7548 case BIT_AND_EXPR:
7549 tcode = Bit_And_Expr;
7550 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7551 Such values appear in expressions with aligning patterns. Note that,
7552 since sizetype is unsigned, we have to jump through some hoops. */
7553 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7555 tree op1 = TREE_OPERAND (gnu_size, 1);
7556 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7557 if (wi::neg_p (signed_op1))
7559 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7560 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7563 break;
7565 case CALL_EXPR:
7566 /* In regular mode, inline back only if symbolic annotation is requested
7567 in order to avoid memory explosion on big discriminated record types.
7568 But not in ASIS mode, as symbolic annotation is required for DDA. */
7569 if (List_Representation_Info == 3 || type_annotate_only)
7571 tree t = maybe_inline_call_in_expr (gnu_size);
7572 if (t)
7573 return annotate_value (t);
7575 else
7576 return Uint_Minus_1;
7578 /* Fall through... */
7580 default:
7581 return No_Uint;
7584 /* Now get each of the operands that's relevant for this code. If any
7585 cannot be expressed as a repinfo node, say we can't. */
7586 for (i = 0; i < 3; i++)
7587 ops[i] = No_Uint;
7589 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7591 if (i == 1 && pre_op1 != No_Uint)
7592 ops[i] = pre_op1;
7593 else
7594 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7595 if (ops[i] == No_Uint)
7596 return No_Uint;
7599 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7601 /* Save the result in the cache. */
7602 if (in.base.from)
7604 struct tree_int_map **h;
7605 /* We can't assume the hash table data hasn't moved since the initial
7606 look up, so we have to search again. Allocating and inserting an
7607 entry at that point would be an alternative, but then we'd better
7608 discard the entry if we decided not to cache it. */
7609 h = annotate_value_cache->find_slot (&in, INSERT);
7610 gcc_assert (!*h);
7611 *h = ggc_alloc<tree_int_map> ();
7612 (*h)->base.from = gnu_size;
7613 (*h)->to = ret;
7616 return ret;
7619 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7620 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7621 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7622 BY_REF is true if the object is used by reference. */
7624 void
7625 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7627 if (by_ref)
7629 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7630 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7631 else
7632 gnu_type = TREE_TYPE (gnu_type);
7635 if (Unknown_Esize (gnat_entity))
7637 if (TREE_CODE (gnu_type) == RECORD_TYPE
7638 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7639 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7640 else if (!size)
7641 size = TYPE_SIZE (gnu_type);
7643 if (size)
7644 Set_Esize (gnat_entity, annotate_value (size));
7647 if (Unknown_Alignment (gnat_entity))
7648 Set_Alignment (gnat_entity,
7649 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7652 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7653 Return NULL_TREE if there is no such element in the list. */
7655 static tree
7656 purpose_member_field (const_tree elem, tree list)
7658 while (list)
7660 tree field = TREE_PURPOSE (list);
7661 if (SAME_FIELD_P (field, elem))
7662 return list;
7663 list = TREE_CHAIN (list);
7665 return NULL_TREE;
7668 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7669 set Component_Bit_Offset and Esize of the components to the position and
7670 size used by Gigi. */
7672 static void
7673 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7675 Entity_Id gnat_field;
7676 tree gnu_list;
7678 /* We operate by first making a list of all fields and their position (we
7679 can get the size easily) and then update all the sizes in the tree. */
7680 gnu_list
7681 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7682 BIGGEST_ALIGNMENT, NULL_TREE);
7684 for (gnat_field = First_Entity (gnat_entity);
7685 Present (gnat_field);
7686 gnat_field = Next_Entity (gnat_field))
7687 if (Ekind (gnat_field) == E_Component
7688 || (Ekind (gnat_field) == E_Discriminant
7689 && !Is_Unchecked_Union (Scope (gnat_field))))
7691 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7692 gnu_list);
7693 if (t)
7695 tree parent_offset;
7697 /* If we are just annotating types and the type is tagged, the tag
7698 and the parent components are not generated by the front-end so
7699 we need to add the appropriate offset to each component without
7700 representation clause. */
7701 if (type_annotate_only
7702 && Is_Tagged_Type (gnat_entity)
7703 && No (Component_Clause (gnat_field)))
7705 /* For a component appearing in the current extension, the
7706 offset is the size of the parent. */
7707 if (Is_Derived_Type (gnat_entity)
7708 && Original_Record_Component (gnat_field) == gnat_field)
7709 parent_offset
7710 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7711 bitsizetype);
7712 else
7713 parent_offset = bitsize_int (POINTER_SIZE);
7715 if (TYPE_FIELDS (gnu_type))
7716 parent_offset
7717 = round_up (parent_offset,
7718 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7720 else
7721 parent_offset = bitsize_zero_node;
7723 Set_Component_Bit_Offset
7724 (gnat_field,
7725 annotate_value
7726 (size_binop (PLUS_EXPR,
7727 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7728 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7729 parent_offset)));
7731 Set_Esize (gnat_field,
7732 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7734 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7736 /* If there is no entry, this is an inherited component whose
7737 position is the same as in the parent type. */
7738 Set_Component_Bit_Offset
7739 (gnat_field,
7740 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7742 Set_Esize (gnat_field,
7743 Esize (Original_Record_Component (gnat_field)));
7748 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7749 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7750 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7751 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7752 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7753 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7754 pre-existing list to be chained to the newly created entries. */
7756 static tree
7757 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7758 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7760 tree gnu_field;
7762 for (gnu_field = TYPE_FIELDS (gnu_type);
7763 gnu_field;
7764 gnu_field = DECL_CHAIN (gnu_field))
7766 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7767 DECL_FIELD_BIT_OFFSET (gnu_field));
7768 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7769 DECL_FIELD_OFFSET (gnu_field));
7770 unsigned int our_offset_align
7771 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7772 tree v = make_tree_vec (3);
7774 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7775 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7776 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7777 gnu_list = tree_cons (gnu_field, v, gnu_list);
7779 /* Recurse on internal fields, flattening the nested fields except for
7780 those in the variant part, if requested. */
7781 if (DECL_INTERNAL_P (gnu_field))
7783 tree gnu_field_type = TREE_TYPE (gnu_field);
7784 if (do_not_flatten_variant
7785 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7786 gnu_list
7787 = build_position_list (gnu_field_type, do_not_flatten_variant,
7788 size_zero_node, bitsize_zero_node,
7789 BIGGEST_ALIGNMENT, gnu_list);
7790 else
7791 gnu_list
7792 = build_position_list (gnu_field_type, do_not_flatten_variant,
7793 gnu_our_offset, gnu_our_bitpos,
7794 our_offset_align, gnu_list);
7798 return gnu_list;
7801 /* Return a list describing the substitutions needed to reflect the
7802 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7803 be in any order. The values in an element of the list are in the form
7804 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7805 a definition of GNAT_SUBTYPE. */
7807 static vec<subst_pair>
7808 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7810 vec<subst_pair> gnu_list = vNULL;
7811 Entity_Id gnat_discrim;
7812 Node_Id gnat_constr;
7814 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7815 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7816 Present (gnat_discrim);
7817 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7818 gnat_constr = Next_Elmt (gnat_constr))
7819 /* Ignore access discriminants. */
7820 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7822 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7823 tree replacement = convert (TREE_TYPE (gnu_field),
7824 elaborate_expression
7825 (Node (gnat_constr), gnat_subtype,
7826 get_entity_char (gnat_discrim),
7827 definition, true, false));
7828 subst_pair s = {gnu_field, replacement};
7829 gnu_list.safe_push (s);
7832 return gnu_list;
7835 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7836 variants of QUAL_UNION_TYPE that are still relevant after applying
7837 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7838 list to be prepended to the newly created entries. */
7840 static vec<variant_desc>
7841 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7842 vec<variant_desc> gnu_list)
7844 tree gnu_field;
7846 for (gnu_field = TYPE_FIELDS (qual_union_type);
7847 gnu_field;
7848 gnu_field = DECL_CHAIN (gnu_field))
7850 tree qual = DECL_QUALIFIER (gnu_field);
7851 unsigned int i;
7852 subst_pair *s;
7854 FOR_EACH_VEC_ELT (subst_list, i, s)
7855 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7857 /* If the new qualifier is not unconditionally false, its variant may
7858 still be accessed. */
7859 if (!integer_zerop (qual))
7861 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7862 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7864 gnu_list.safe_push (v);
7866 /* Recurse on the variant subpart of the variant, if any. */
7867 variant_subpart = get_variant_part (variant_type);
7868 if (variant_subpart)
7869 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7870 subst_list, gnu_list);
7872 /* If the new qualifier is unconditionally true, the subsequent
7873 variants cannot be accessed. */
7874 if (integer_onep (qual))
7875 break;
7879 return gnu_list;
7882 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7883 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7884 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7885 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7886 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7887 true if we are being called to process the Component_Size of GNAT_OBJECT;
7888 this is used only for error messages. ZERO_OK is true if a size of zero
7889 is permitted; if ZERO_OK is false, it means that a size of zero should be
7890 treated as an unspecified size. */
7892 static tree
7893 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7894 enum tree_code kind, bool component_p, bool zero_ok)
7896 Node_Id gnat_error_node;
7897 tree type_size, size;
7899 /* Return 0 if no size was specified. */
7900 if (uint_size == No_Uint)
7901 return NULL_TREE;
7903 /* Ignore a negative size since that corresponds to our back-annotation. */
7904 if (UI_Lt (uint_size, Uint_0))
7905 return NULL_TREE;
7907 /* Find the node to use for error messages. */
7908 if ((Ekind (gnat_object) == E_Component
7909 || Ekind (gnat_object) == E_Discriminant)
7910 && Present (Component_Clause (gnat_object)))
7911 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7912 else if (Present (Size_Clause (gnat_object)))
7913 gnat_error_node = Expression (Size_Clause (gnat_object));
7914 else
7915 gnat_error_node = gnat_object;
7917 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7918 but cannot be represented in bitsizetype. */
7919 size = UI_To_gnu (uint_size, bitsizetype);
7920 if (TREE_OVERFLOW (size))
7922 if (component_p)
7923 post_error_ne ("component size for& is too large", gnat_error_node,
7924 gnat_object);
7925 else
7926 post_error_ne ("size for& is too large", gnat_error_node,
7927 gnat_object);
7928 return NULL_TREE;
7931 /* Ignore a zero size if it is not permitted. */
7932 if (!zero_ok && integer_zerop (size))
7933 return NULL_TREE;
7935 /* The size of objects is always a multiple of a byte. */
7936 if (kind == VAR_DECL
7937 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7939 if (component_p)
7940 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7941 gnat_error_node, gnat_object);
7942 else
7943 post_error_ne ("size for& is not a multiple of Storage_Unit",
7944 gnat_error_node, gnat_object);
7945 return NULL_TREE;
7948 /* If this is an integral type or a packed array type, the front-end has
7949 already verified the size, so we need not do it here (which would mean
7950 checking against the bounds). However, if this is an aliased object,
7951 it may not be smaller than the type of the object. */
7952 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7953 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7954 return size;
7956 /* If the object is a record that contains a template, add the size of the
7957 template to the specified size. */
7958 if (TREE_CODE (gnu_type) == RECORD_TYPE
7959 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7960 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7962 if (kind == VAR_DECL
7963 /* If a type needs strict alignment, a component of this type in
7964 a packed record cannot be packed and thus uses the type size. */
7965 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7966 type_size = TYPE_SIZE (gnu_type);
7967 else
7968 type_size = rm_size (gnu_type);
7970 /* Modify the size of a discriminated type to be the maximum size. */
7971 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7972 type_size = max_size (type_size, true);
7974 /* If this is an access type or a fat pointer, the minimum size is that given
7975 by the smallest integral mode that's valid for pointers. */
7976 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7978 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7979 while (!targetm.valid_pointer_mode (p_mode))
7980 p_mode = GET_MODE_WIDER_MODE (p_mode);
7981 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7984 /* Issue an error either if the default size of the object isn't a constant
7985 or if the new size is smaller than it. */
7986 if (TREE_CODE (type_size) != INTEGER_CST
7987 || TREE_OVERFLOW (type_size)
7988 || tree_int_cst_lt (size, type_size))
7990 if (component_p)
7991 post_error_ne_tree
7992 ("component size for& too small{, minimum allowed is ^}",
7993 gnat_error_node, gnat_object, type_size);
7994 else
7995 post_error_ne_tree
7996 ("size for& too small{, minimum allowed is ^}",
7997 gnat_error_node, gnat_object, type_size);
7998 return NULL_TREE;
8001 return size;
8004 /* Similarly, but both validate and process a value of RM size. This routine
8005 is only called for types. */
8007 static void
8008 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8010 Node_Id gnat_attr_node;
8011 tree old_size, size;
8013 /* Do nothing if no size was specified. */
8014 if (uint_size == No_Uint)
8015 return;
8017 /* Ignore a negative size since that corresponds to our back-annotation. */
8018 if (UI_Lt (uint_size, Uint_0))
8019 return;
8021 /* Only issue an error if a Value_Size clause was explicitly given.
8022 Otherwise, we'd be duplicating an error on the Size clause. */
8023 gnat_attr_node
8024 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8026 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8027 but cannot be represented in bitsizetype. */
8028 size = UI_To_gnu (uint_size, bitsizetype);
8029 if (TREE_OVERFLOW (size))
8031 if (Present (gnat_attr_node))
8032 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8033 gnat_entity);
8034 return;
8037 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8038 exists, or this is an integer type, in which case the front-end will
8039 have always set it. */
8040 if (No (gnat_attr_node)
8041 && integer_zerop (size)
8042 && !Has_Size_Clause (gnat_entity)
8043 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8044 return;
8046 old_size = rm_size (gnu_type);
8048 /* If the old size is self-referential, get the maximum size. */
8049 if (CONTAINS_PLACEHOLDER_P (old_size))
8050 old_size = max_size (old_size, true);
8052 /* Issue an error either if the old size of the object isn't a constant or
8053 if the new size is smaller than it. The front-end has already verified
8054 this for scalar and packed array types. */
8055 if (TREE_CODE (old_size) != INTEGER_CST
8056 || TREE_OVERFLOW (old_size)
8057 || (AGGREGATE_TYPE_P (gnu_type)
8058 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8059 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8060 && !(TYPE_IS_PADDING_P (gnu_type)
8061 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8062 && TYPE_PACKED_ARRAY_TYPE_P
8063 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8064 && tree_int_cst_lt (size, old_size)))
8066 if (Present (gnat_attr_node))
8067 post_error_ne_tree
8068 ("Value_Size for& too small{, minimum allowed is ^}",
8069 gnat_attr_node, gnat_entity, old_size);
8070 return;
8073 /* Otherwise, set the RM size proper for integral types... */
8074 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8075 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8076 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8077 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8078 SET_TYPE_RM_SIZE (gnu_type, size);
8080 /* ...or the Ada size for record and union types. */
8081 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8082 && !TYPE_FAT_POINTER_P (gnu_type))
8083 SET_TYPE_ADA_SIZE (gnu_type, size);
8086 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8087 a type or object whose present alignment is ALIGN. If this alignment is
8088 valid, return it. Otherwise, give an error and return ALIGN. */
8090 static unsigned int
8091 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8093 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8094 unsigned int new_align;
8095 Node_Id gnat_error_node;
8097 /* Don't worry about checking alignment if alignment was not specified
8098 by the source program and we already posted an error for this entity. */
8099 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8100 return align;
8102 /* Post the error on the alignment clause if any. Note, for the implicit
8103 base type of an array type, the alignment clause is on the first
8104 subtype. */
8105 if (Present (Alignment_Clause (gnat_entity)))
8106 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8108 else if (Is_Itype (gnat_entity)
8109 && Is_Array_Type (gnat_entity)
8110 && Etype (gnat_entity) == gnat_entity
8111 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8112 gnat_error_node =
8113 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8115 else
8116 gnat_error_node = gnat_entity;
8118 /* Within GCC, an alignment is an integer, so we must make sure a value is
8119 specified that fits in that range. Also, there is an upper bound to
8120 alignments we can support/allow. */
8121 if (!UI_Is_In_Int_Range (alignment)
8122 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8123 post_error_ne_num ("largest supported alignment for& is ^",
8124 gnat_error_node, gnat_entity, max_allowed_alignment);
8125 else if (!(Present (Alignment_Clause (gnat_entity))
8126 && From_At_Mod (Alignment_Clause (gnat_entity)))
8127 && new_align * BITS_PER_UNIT < align)
8129 unsigned int double_align;
8130 bool is_capped_double, align_clause;
8132 /* If the default alignment of "double" or larger scalar types is
8133 specifically capped and the new alignment is above the cap, do
8134 not post an error and change the alignment only if there is an
8135 alignment clause; this makes it possible to have the associated
8136 GCC type overaligned by default for performance reasons. */
8137 if ((double_align = double_float_alignment) > 0)
8139 Entity_Id gnat_type
8140 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8141 is_capped_double
8142 = is_double_float_or_array (gnat_type, &align_clause);
8144 else if ((double_align = double_scalar_alignment) > 0)
8146 Entity_Id gnat_type
8147 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8148 is_capped_double
8149 = is_double_scalar_or_array (gnat_type, &align_clause);
8151 else
8152 is_capped_double = align_clause = false;
8154 if (is_capped_double && new_align >= double_align)
8156 if (align_clause)
8157 align = new_align * BITS_PER_UNIT;
8159 else
8161 if (is_capped_double)
8162 align = double_align * BITS_PER_UNIT;
8164 post_error_ne_num ("alignment for& must be at least ^",
8165 gnat_error_node, gnat_entity,
8166 align / BITS_PER_UNIT);
8169 else
8171 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8172 if (new_align > align)
8173 align = new_align;
8176 return align;
8179 /* Verify that TYPE is something we can implement atomically. If not, issue
8180 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8181 process a component type. */
8183 static void
8184 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8186 Node_Id gnat_error_point = gnat_entity;
8187 Node_Id gnat_node;
8188 machine_mode mode;
8189 enum mode_class mclass;
8190 unsigned int align;
8191 tree size;
8193 /* If this is an anonymous base type, nothing to check, the error will be
8194 reported on the source type if need be. */
8195 if (!Comes_From_Source (gnat_entity))
8196 return;
8198 mode = TYPE_MODE (type);
8199 mclass = GET_MODE_CLASS (mode);
8200 align = TYPE_ALIGN (type);
8201 size = TYPE_SIZE (type);
8203 /* Consider all aligned floating-point types atomic and any aligned types
8204 that are represented by integers no wider than a machine word. */
8205 if ((mclass == MODE_FLOAT
8206 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8207 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8208 && align >= GET_MODE_ALIGNMENT (mode))
8209 return;
8211 /* For the moment, also allow anything that has an alignment equal to its
8212 size and which is smaller than a word. */
8213 if (size
8214 && TREE_CODE (size) == INTEGER_CST
8215 && compare_tree_int (size, align) == 0
8216 && align <= BITS_PER_WORD)
8217 return;
8219 for (gnat_node = First_Rep_Item (gnat_entity);
8220 Present (gnat_node);
8221 gnat_node = Next_Rep_Item (gnat_node))
8222 if (Nkind (gnat_node) == N_Pragma)
8224 unsigned char pragma_id
8225 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8227 if ((pragma_id == Pragma_Atomic && !component_p)
8228 || (pragma_id == Pragma_Atomic_Components && component_p))
8230 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8231 break;
8235 if (component_p)
8236 post_error_ne ("atomic access to component of & cannot be guaranteed",
8237 gnat_error_point, gnat_entity);
8238 else if (Is_Volatile_Full_Access (gnat_entity))
8239 post_error_ne ("volatile full access to & cannot be guaranteed",
8240 gnat_error_point, gnat_entity);
8241 else
8242 post_error_ne ("atomic access to & cannot be guaranteed",
8243 gnat_error_point, gnat_entity);
8247 /* Helper for the intrin compatibility checks family. Evaluate whether
8248 two types are definitely incompatible. */
8250 static bool
8251 intrin_types_incompatible_p (tree t1, tree t2)
8253 enum tree_code code;
8255 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8256 return false;
8258 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8259 return true;
8261 if (TREE_CODE (t1) != TREE_CODE (t2))
8262 return true;
8264 code = TREE_CODE (t1);
8266 switch (code)
8268 case INTEGER_TYPE:
8269 case REAL_TYPE:
8270 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8272 case POINTER_TYPE:
8273 case REFERENCE_TYPE:
8274 /* Assume designated types are ok. We'd need to account for char * and
8275 void * variants to do better, which could rapidly get messy and isn't
8276 clearly worth the effort. */
8277 return false;
8279 default:
8280 break;
8283 return false;
8286 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8287 on the Ada/builtin argument lists for the INB binding. */
8289 static bool
8290 intrin_arglists_compatible_p (intrin_binding_t * inb)
8292 function_args_iterator ada_iter, btin_iter;
8294 function_args_iter_init (&ada_iter, inb->ada_fntype);
8295 function_args_iter_init (&btin_iter, inb->btin_fntype);
8297 /* Sequence position of the last argument we checked. */
8298 int argpos = 0;
8300 while (1)
8302 tree ada_type = function_args_iter_cond (&ada_iter);
8303 tree btin_type = function_args_iter_cond (&btin_iter);
8305 /* If we've exhausted both lists simultaneously, we're done. */
8306 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8307 break;
8309 /* If one list is shorter than the other, they fail to match. */
8310 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8311 return false;
8313 /* If we're done with the Ada args and not with the internal builtin
8314 args, or the other way around, complain. */
8315 if (ada_type == void_type_node
8316 && btin_type != void_type_node)
8318 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8319 return false;
8322 if (btin_type == void_type_node
8323 && ada_type != void_type_node)
8325 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8326 inb->gnat_entity, inb->gnat_entity, argpos);
8327 return false;
8330 /* Otherwise, check that types match for the current argument. */
8331 argpos ++;
8332 if (intrin_types_incompatible_p (ada_type, btin_type))
8334 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8335 inb->gnat_entity, inb->gnat_entity, argpos);
8336 return false;
8340 function_args_iter_next (&ada_iter);
8341 function_args_iter_next (&btin_iter);
8344 return true;
8347 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8348 on the Ada/builtin return values for the INB binding. */
8350 static bool
8351 intrin_return_compatible_p (intrin_binding_t * inb)
8353 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8354 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8356 /* Accept function imported as procedure, common and convenient. */
8357 if (VOID_TYPE_P (ada_return_type)
8358 && !VOID_TYPE_P (btin_return_type))
8359 return true;
8361 /* If return type is Address (integer type), map it to void *. */
8362 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8363 ada_return_type = ptr_type_node;
8365 /* Check return types compatibility otherwise. Note that this
8366 handles void/void as well. */
8367 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8369 post_error ("?intrinsic binding type mismatch on return value!",
8370 inb->gnat_entity);
8371 return false;
8374 return true;
8377 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8378 compatible. Issue relevant warnings when they are not.
8380 This is intended as a light check to diagnose the most obvious cases, not
8381 as a full fledged type compatibility predicate. It is the programmer's
8382 responsibility to ensure correctness of the Ada declarations in Imports,
8383 especially when binding straight to a compiler internal. */
8385 static bool
8386 intrin_profiles_compatible_p (intrin_binding_t * inb)
8388 /* Check compatibility on return values and argument lists, each responsible
8389 for posting warnings as appropriate. Ensure use of the proper sloc for
8390 this purpose. */
8392 bool arglists_compatible_p, return_compatible_p;
8393 location_t saved_location = input_location;
8395 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8397 return_compatible_p = intrin_return_compatible_p (inb);
8398 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8400 input_location = saved_location;
8402 return return_compatible_p && arglists_compatible_p;
8405 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8406 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8407 specified size for this field. POS_LIST is a position list describing
8408 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8409 to this layout. */
8411 static tree
8412 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8413 tree size, tree pos_list,
8414 vec<subst_pair> subst_list)
8416 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8417 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8418 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8419 tree new_pos, new_field;
8420 unsigned int i;
8421 subst_pair *s;
8423 if (CONTAINS_PLACEHOLDER_P (pos))
8424 FOR_EACH_VEC_ELT (subst_list, i, s)
8425 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8427 /* If the position is now a constant, we can set it as the position of the
8428 field when we make it. Otherwise, we need to deal with it specially. */
8429 if (TREE_CONSTANT (pos))
8430 new_pos = bit_from_pos (pos, bitpos);
8431 else
8432 new_pos = NULL_TREE;
8434 new_field
8435 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8436 size, new_pos, DECL_PACKED (old_field),
8437 !DECL_NONADDRESSABLE_P (old_field));
8439 if (!new_pos)
8441 normalize_offset (&pos, &bitpos, offset_align);
8442 /* Finalize the position. */
8443 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8444 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8445 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8446 DECL_SIZE (new_field) = size;
8447 DECL_SIZE_UNIT (new_field)
8448 = convert (sizetype,
8449 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8450 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8453 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8454 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8455 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8456 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8458 return new_field;
8461 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8462 it is the minimal size the REP_PART must have. */
8464 static tree
8465 create_rep_part (tree rep_type, tree record_type, tree min_size)
8467 tree field;
8469 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8470 min_size = NULL_TREE;
8472 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8473 min_size, NULL_TREE, 0, 1);
8474 DECL_INTERNAL_P (field) = 1;
8476 return field;
8479 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8481 static tree
8482 get_rep_part (tree record_type)
8484 tree field = TYPE_FIELDS (record_type);
8486 /* The REP part is the first field, internal, another record, and its name
8487 starts with an 'R'. */
8488 if (field
8489 && DECL_INTERNAL_P (field)
8490 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8491 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8492 return field;
8494 return NULL_TREE;
8497 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8499 tree
8500 get_variant_part (tree record_type)
8502 tree field;
8504 /* The variant part is the only internal field that is a qualified union. */
8505 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8506 if (DECL_INTERNAL_P (field)
8507 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8508 return field;
8510 return NULL_TREE;
8513 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8514 the list of variants to be used and RECORD_TYPE is the type of the parent.
8515 POS_LIST is a position list describing the layout of fields present in
8516 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8517 layout. */
8519 static tree
8520 create_variant_part_from (tree old_variant_part,
8521 vec<variant_desc> variant_list,
8522 tree record_type, tree pos_list,
8523 vec<subst_pair> subst_list)
8525 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8526 tree old_union_type = TREE_TYPE (old_variant_part);
8527 tree new_union_type, new_variant_part;
8528 tree union_field_list = NULL_TREE;
8529 variant_desc *v;
8530 unsigned int i;
8532 /* First create the type of the variant part from that of the old one. */
8533 new_union_type = make_node (QUAL_UNION_TYPE);
8534 TYPE_NAME (new_union_type)
8535 = concat_name (TYPE_NAME (record_type),
8536 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8538 /* If the position of the variant part is constant, subtract it from the
8539 size of the type of the parent to get the new size. This manual CSE
8540 reduces the code size when not optimizing. */
8541 if (TREE_CODE (offset) == INTEGER_CST)
8543 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8544 tree first_bit = bit_from_pos (offset, bitpos);
8545 TYPE_SIZE (new_union_type)
8546 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8547 TYPE_SIZE_UNIT (new_union_type)
8548 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8549 byte_from_pos (offset, bitpos));
8550 SET_TYPE_ADA_SIZE (new_union_type,
8551 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8552 first_bit));
8553 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8554 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8556 else
8557 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8559 /* Now finish up the new variants and populate the union type. */
8560 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8562 tree old_field = v->field, new_field;
8563 tree old_variant, old_variant_subpart, new_variant, field_list;
8565 /* Skip variants that don't belong to this nesting level. */
8566 if (DECL_CONTEXT (old_field) != old_union_type)
8567 continue;
8569 /* Retrieve the list of fields already added to the new variant. */
8570 new_variant = v->new_type;
8571 field_list = TYPE_FIELDS (new_variant);
8573 /* If the old variant had a variant subpart, we need to create a new
8574 variant subpart and add it to the field list. */
8575 old_variant = v->type;
8576 old_variant_subpart = get_variant_part (old_variant);
8577 if (old_variant_subpart)
8579 tree new_variant_subpart
8580 = create_variant_part_from (old_variant_subpart, variant_list,
8581 new_variant, pos_list, subst_list);
8582 DECL_CHAIN (new_variant_subpart) = field_list;
8583 field_list = new_variant_subpart;
8586 /* Finish up the new variant and create the field. No need for debug
8587 info thanks to the XVS type. */
8588 finish_record_type (new_variant, nreverse (field_list), 2, false);
8589 compute_record_mode (new_variant);
8590 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8591 Empty);
8593 new_field
8594 = create_field_decl_from (old_field, new_variant, new_union_type,
8595 TYPE_SIZE (new_variant),
8596 pos_list, subst_list);
8597 DECL_QUALIFIER (new_field) = v->qual;
8598 DECL_INTERNAL_P (new_field) = 1;
8599 DECL_CHAIN (new_field) = union_field_list;
8600 union_field_list = new_field;
8603 /* Finish up the union type and create the variant part. No need for debug
8604 info thanks to the XVS type. Note that we don't reverse the field list
8605 because VARIANT_LIST has been traversed in reverse order. */
8606 finish_record_type (new_union_type, union_field_list, 2, false);
8607 compute_record_mode (new_union_type);
8608 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8609 Empty);
8611 new_variant_part
8612 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8613 TYPE_SIZE (new_union_type),
8614 pos_list, subst_list);
8615 DECL_INTERNAL_P (new_variant_part) = 1;
8617 /* With multiple discriminants it is possible for an inner variant to be
8618 statically selected while outer ones are not; in this case, the list
8619 of fields of the inner variant is not flattened and we end up with a
8620 qualified union with a single member. Drop the useless container. */
8621 if (!DECL_CHAIN (union_field_list))
8623 DECL_CONTEXT (union_field_list) = record_type;
8624 DECL_FIELD_OFFSET (union_field_list)
8625 = DECL_FIELD_OFFSET (new_variant_part);
8626 DECL_FIELD_BIT_OFFSET (union_field_list)
8627 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8628 SET_DECL_OFFSET_ALIGN (union_field_list,
8629 DECL_OFFSET_ALIGN (new_variant_part));
8630 new_variant_part = union_field_list;
8633 return new_variant_part;
8636 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8637 which are both RECORD_TYPE, after applying the substitutions described
8638 in SUBST_LIST. */
8640 static void
8641 copy_and_substitute_in_size (tree new_type, tree old_type,
8642 vec<subst_pair> subst_list)
8644 unsigned int i;
8645 subst_pair *s;
8647 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8648 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8649 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8650 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8651 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8653 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8654 FOR_EACH_VEC_ELT (subst_list, i, s)
8655 TYPE_SIZE (new_type)
8656 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8657 s->discriminant, s->replacement);
8659 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8660 FOR_EACH_VEC_ELT (subst_list, i, s)
8661 TYPE_SIZE_UNIT (new_type)
8662 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8663 s->discriminant, s->replacement);
8665 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8666 FOR_EACH_VEC_ELT (subst_list, i, s)
8667 SET_TYPE_ADA_SIZE
8668 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8669 s->discriminant, s->replacement));
8671 /* Finalize the size. */
8672 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8673 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8676 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8677 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8678 The parallel type is the original array type if it has been translated. */
8680 static void
8681 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8683 Entity_Id gnat_original_array_type
8684 = Underlying_Type (Original_Array_Type (gnat_entity));
8685 tree gnu_original_array_type;
8687 if (!present_gnu_tree (gnat_original_array_type))
8688 return;
8690 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8692 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8693 return;
8695 add_parallel_type (gnu_type, gnu_original_array_type);
8698 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8699 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8700 updated by replacing F with R.
8702 The function doesn't update the layout of the type, i.e. it assumes
8703 that the substitution is purely formal. That's why the replacement
8704 value R must itself contain a PLACEHOLDER_EXPR. */
8706 tree
8707 substitute_in_type (tree t, tree f, tree r)
8709 tree nt;
8711 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8713 switch (TREE_CODE (t))
8715 case INTEGER_TYPE:
8716 case ENUMERAL_TYPE:
8717 case BOOLEAN_TYPE:
8718 case REAL_TYPE:
8720 /* First the domain types of arrays. */
8721 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8722 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8724 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8725 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8727 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8728 return t;
8730 nt = copy_type (t);
8731 TYPE_GCC_MIN_VALUE (nt) = low;
8732 TYPE_GCC_MAX_VALUE (nt) = high;
8734 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8735 SET_TYPE_INDEX_TYPE
8736 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8738 return nt;
8741 /* Then the subtypes. */
8742 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8743 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8745 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8746 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8748 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8749 return t;
8751 nt = copy_type (t);
8752 SET_TYPE_RM_MIN_VALUE (nt, low);
8753 SET_TYPE_RM_MAX_VALUE (nt, high);
8755 return nt;
8758 return t;
8760 case COMPLEX_TYPE:
8761 nt = substitute_in_type (TREE_TYPE (t), f, r);
8762 if (nt == TREE_TYPE (t))
8763 return t;
8765 return build_complex_type (nt);
8767 case FUNCTION_TYPE:
8768 /* These should never show up here. */
8769 gcc_unreachable ();
8771 case ARRAY_TYPE:
8773 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8774 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8776 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8777 return t;
8779 nt = build_nonshared_array_type (component, domain);
8780 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8781 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8782 SET_TYPE_MODE (nt, TYPE_MODE (t));
8783 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8784 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8785 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8786 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8787 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8788 return nt;
8791 case RECORD_TYPE:
8792 case UNION_TYPE:
8793 case QUAL_UNION_TYPE:
8795 bool changed_field = false;
8796 tree field;
8798 /* Start out with no fields, make new fields, and chain them
8799 in. If we haven't actually changed the type of any field,
8800 discard everything we've done and return the old type. */
8801 nt = copy_type (t);
8802 TYPE_FIELDS (nt) = NULL_TREE;
8804 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8806 tree new_field = copy_node (field), new_n;
8808 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8809 if (new_n != TREE_TYPE (field))
8811 TREE_TYPE (new_field) = new_n;
8812 changed_field = true;
8815 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8816 if (new_n != DECL_FIELD_OFFSET (field))
8818 DECL_FIELD_OFFSET (new_field) = new_n;
8819 changed_field = true;
8822 /* Do the substitution inside the qualifier, if any. */
8823 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8825 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8826 if (new_n != DECL_QUALIFIER (field))
8828 DECL_QUALIFIER (new_field) = new_n;
8829 changed_field = true;
8833 DECL_CONTEXT (new_field) = nt;
8834 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8836 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8837 TYPE_FIELDS (nt) = new_field;
8840 if (!changed_field)
8841 return t;
8843 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8844 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8845 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8846 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8847 return nt;
8850 default:
8851 return t;
8855 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8856 needed to represent the object. */
8858 tree
8859 rm_size (tree gnu_type)
8861 /* For integral types, we store the RM size explicitly. */
8862 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8863 return TYPE_RM_SIZE (gnu_type);
8865 /* Return the RM size of the actual data plus the size of the template. */
8866 if (TREE_CODE (gnu_type) == RECORD_TYPE
8867 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8868 return
8869 size_binop (PLUS_EXPR,
8870 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8871 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8873 /* For record or union types, we store the size explicitly. */
8874 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8875 && !TYPE_FAT_POINTER_P (gnu_type)
8876 && TYPE_ADA_SIZE (gnu_type))
8877 return TYPE_ADA_SIZE (gnu_type);
8879 /* For other types, this is just the size. */
8880 return TYPE_SIZE (gnu_type);
8883 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8884 fully-qualified name, possibly with type information encoding.
8885 Otherwise, return the name. */
8887 static const char *
8888 get_entity_char (Entity_Id gnat_entity)
8890 Get_Encoded_Name (gnat_entity);
8891 return ggc_strdup (Name_Buffer);
8894 tree
8895 get_entity_name (Entity_Id gnat_entity)
8897 Get_Encoded_Name (gnat_entity);
8898 return get_identifier_with_length (Name_Buffer, Name_Len);
8901 /* Return an identifier representing the external name to be used for
8902 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8903 and the specified suffix. */
8905 tree
8906 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8908 const Entity_Kind kind = Ekind (gnat_entity);
8909 const bool has_suffix = (suffix != NULL);
8910 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8911 String_Pointer sp = {suffix, &temp};
8913 Get_External_Name (gnat_entity, has_suffix, sp);
8915 /* A variable using the Stdcall convention lives in a DLL. We adjust
8916 its name to use the jump table, the _imp__NAME contains the address
8917 for the NAME variable. */
8918 if ((kind == E_Variable || kind == E_Constant)
8919 && Has_Stdcall_Convention (gnat_entity))
8921 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8922 char *new_name = (char *) alloca (len + 1);
8923 strcpy (new_name, STDCALL_PREFIX);
8924 strcat (new_name, Name_Buffer);
8925 return get_identifier_with_length (new_name, len);
8928 return get_identifier_with_length (Name_Buffer, Name_Len);
8931 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8932 string, return a new IDENTIFIER_NODE that is the concatenation of
8933 the name followed by "___" and the specified suffix. */
8935 tree
8936 concat_name (tree gnu_name, const char *suffix)
8938 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8939 char *new_name = (char *) alloca (len + 1);
8940 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8941 strcat (new_name, "___");
8942 strcat (new_name, suffix);
8943 return get_identifier_with_length (new_name, len);
8946 /* Initialize data structures of the decl.c module. */
8948 void
8949 init_gnat_decl (void)
8951 /* Initialize the cache of annotated values. */
8952 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8955 /* Destroy data structures of the decl.c module. */
8957 void
8958 destroy_gnat_decl (void)
8960 /* Destroy the cache of annotated values. */
8961 annotate_value_cache->empty ();
8962 annotate_value_cache = NULL;
8965 #include "gt-ada-decl.h"