2015-05-12 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobd908a1b750d7357228c8433a6f2ce8839b6db779
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2015, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "stringpool.h"
42 #include "stor-layout.h"
43 #include "flags.h"
44 #include "toplev.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "tree-inline.h"
48 #include "diagnostic-core.h"
50 #include "ada.h"
51 #include "types.h"
52 #include "atree.h"
53 #include "elists.h"
54 #include "namet.h"
55 #include "nlists.h"
56 #include "repinfo.h"
57 #include "snames.h"
58 #include "stringt.h"
59 #include "uintp.h"
60 #include "fe.h"
61 #include "sinfo.h"
62 #include "einfo.h"
63 #include "ada-tree.h"
64 #include "gigi.h"
66 /* "stdcall" and "thiscall" conventions should be processed in a specific way
67 on 32-bit x86/Windows only. The macros below are helpers to avoid having
68 to check for a Windows specific attribute throughout this unit. */
70 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
71 #ifdef TARGET_64BIT
72 #define Has_Stdcall_Convention(E) \
73 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
74 #define Has_Thiscall_Convention(E) \
75 (!TARGET_64BIT && is_cplusplus_method (E))
76 #else
77 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
78 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
79 #endif
80 #else
81 #define Has_Stdcall_Convention(E) 0
82 #define Has_Thiscall_Convention(E) 0
83 #endif
85 #define STDCALL_PREFIX "_imp__"
87 /* Stack realignment is necessary for functions with foreign conventions when
88 the ABI doesn't mandate as much as what the compiler assumes - that is, up
89 to PREFERRED_STACK_BOUNDARY.
91 Such realignment can be requested with a dedicated function type attribute
92 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
93 characterize the situations where the attribute should be set. We rely on
94 compiler configuration settings for 'main' to decide. */
96 #ifdef MAIN_STACK_BOUNDARY
97 #define FOREIGN_FORCE_REALIGN_STACK \
98 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
99 #else
100 #define FOREIGN_FORCE_REALIGN_STACK 0
101 #endif
103 struct incomplete
105 struct incomplete *next;
106 tree old_type;
107 Entity_Id full_type;
110 /* These variables are used to defer recursively expanding incomplete types
111 while we are processing an array, a record or a subprogram type. */
112 static int defer_incomplete_level = 0;
113 static struct incomplete *defer_incomplete_list;
115 /* This variable is used to delay expanding From_Limited_With types until the
116 end of the spec. */
117 static struct incomplete *defer_limited_with;
119 typedef struct subst_pair_d {
120 tree discriminant;
121 tree replacement;
122 } subst_pair;
125 typedef struct variant_desc_d {
126 /* The type of the variant. */
127 tree type;
129 /* The associated field. */
130 tree field;
132 /* The value of the qualifier. */
133 tree qual;
135 /* The type of the variant after transformation. */
136 tree new_type;
137 } variant_desc;
140 /* A hash table used to cache the result of annotate_value. */
142 struct value_annotation_hasher : ggc_cache_hasher<tree_int_map *>
144 static inline hashval_t
145 hash (tree_int_map *m)
147 return htab_hash_pointer (m->base.from);
150 static inline bool
151 equal (tree_int_map *a, tree_int_map *b)
153 return a->base.from == b->base.from;
156 static void
157 handle_cache_entry (tree_int_map *&m)
159 extern void gt_ggc_mx (tree_int_map *&);
160 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
161 return;
162 else if (ggc_marked_p (m->base.from))
163 gt_ggc_mx (m);
164 else
165 m = static_cast<tree_int_map *> (HTAB_DELETED_ENTRY);
169 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
171 static bool allocatable_size_p (tree, bool);
172 static void prepend_one_attribute (struct attrib **,
173 enum attr_type, tree, tree, Node_Id);
174 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
175 static void prepend_attributes (struct attrib **, Entity_Id);
176 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
177 static bool type_has_variable_size (tree);
178 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
179 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
180 unsigned int);
181 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
182 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
183 bool *);
184 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
185 static bool is_from_limited_with_of_main (Entity_Id);
186 static tree change_qualified_type (tree, int);
187 static bool same_discriminant_p (Entity_Id, Entity_Id);
188 static bool array_type_has_nonaliased_component (tree, Entity_Id);
189 static bool compile_time_known_address_p (Node_Id);
190 static bool cannot_be_superflat_p (Node_Id);
191 static bool constructor_address_p (tree);
192 static int compare_field_bitpos (const PTR, const PTR);
193 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
194 bool, bool, bool, bool, bool, tree, tree *);
195 static Uint annotate_value (tree);
196 static void annotate_rep (Entity_Id, tree);
197 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
198 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
199 static vec<variant_desc> build_variant_list (tree,
200 vec<subst_pair> ,
201 vec<variant_desc> );
202 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
203 static void set_rm_size (Uint, tree, Entity_Id);
204 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
205 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
206 static tree create_field_decl_from (tree, tree, tree, tree, tree,
207 vec<subst_pair> );
208 static tree create_rep_part (tree, tree, tree);
209 static tree get_rep_part (tree);
210 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
211 tree, vec<subst_pair> );
212 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
213 static void add_parallel_type_for_packed_array (tree, Entity_Id);
215 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
216 to pass around calls performing profile compatibility checks. */
218 typedef struct {
219 Entity_Id gnat_entity; /* The Ada subprogram entity. */
220 tree ada_fntype; /* The corresponding GCC type node. */
221 tree btin_fntype; /* The GCC builtin function type node. */
222 } intrin_binding_t;
224 static bool intrin_profiles_compatible_p (intrin_binding_t *);
226 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
227 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
228 and associate the ..._DECL node with the input GNAT defining identifier.
230 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
231 initial value (in GCC tree form). This is optional for a variable. For
232 a renamed entity, GNU_EXPR gives the object being renamed.
234 DEFINITION is nonzero if this call is intended for a definition. This is
235 used for separate compilation where it is necessary to know whether an
236 external declaration or a definition must be created if the GCC equivalent
237 was not created previously. The value of 1 is normally used for a nonzero
238 DEFINITION, but a value of 2 is used in special circumstances, defined in
239 the code. */
241 tree
242 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
244 /* Contains the kind of the input GNAT node. */
245 const Entity_Kind kind = Ekind (gnat_entity);
246 /* True if this is a type. */
247 const bool is_type = IN (kind, Type_Kind);
248 /* True if debug info is requested for this entity. */
249 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
250 /* True if this entity is to be considered as imported. */
251 const bool imported_p
252 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
253 /* For a type, contains the equivalent GNAT node to be used in gigi. */
254 Entity_Id gnat_equiv_type = Empty;
255 /* Temporary used to walk the GNAT tree. */
256 Entity_Id gnat_temp;
257 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
258 This node will be associated with the GNAT node by calling at the end
259 of the `switch' statement. */
260 tree gnu_decl = NULL_TREE;
261 /* Contains the GCC type to be used for the GCC node. */
262 tree gnu_type = NULL_TREE;
263 /* Contains the GCC size tree to be used for the GCC node. */
264 tree gnu_size = NULL_TREE;
265 /* Contains the GCC name to be used for the GCC node. */
266 tree gnu_entity_name;
267 /* True if we have already saved gnu_decl as a GNAT association. */
268 bool saved = false;
269 /* True if we incremented defer_incomplete_level. */
270 bool this_deferred = false;
271 /* True if we incremented force_global. */
272 bool this_global = false;
273 /* True if we should check to see if elaborated during processing. */
274 bool maybe_present = false;
275 /* True if we made GNU_DECL and its type here. */
276 bool this_made_decl = false;
277 /* Size and alignment of the GCC node, if meaningful. */
278 unsigned int esize = 0, align = 0;
279 /* Contains the list of attributes directly attached to the entity. */
280 struct attrib *attr_list = NULL;
282 /* Since a use of an Itype is a definition, process it as such if it
283 is not in a with'ed unit. */
284 if (!definition
285 && is_type
286 && Is_Itype (gnat_entity)
287 && !present_gnu_tree (gnat_entity)
288 && In_Extended_Main_Code_Unit (gnat_entity))
290 /* Ensure that we are in a subprogram mentioned in the Scope chain of
291 this entity, our current scope is global, or we encountered a task
292 or entry (where we can't currently accurately check scoping). */
293 if (!current_function_decl
294 || DECL_ELABORATION_PROC_P (current_function_decl))
296 process_type (gnat_entity);
297 return get_gnu_tree (gnat_entity);
300 for (gnat_temp = Scope (gnat_entity);
301 Present (gnat_temp);
302 gnat_temp = Scope (gnat_temp))
304 if (Is_Type (gnat_temp))
305 gnat_temp = Underlying_Type (gnat_temp);
307 if (Ekind (gnat_temp) == E_Subprogram_Body)
308 gnat_temp
309 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
311 if (IN (Ekind (gnat_temp), Subprogram_Kind)
312 && Present (Protected_Body_Subprogram (gnat_temp)))
313 gnat_temp = Protected_Body_Subprogram (gnat_temp);
315 if (Ekind (gnat_temp) == E_Entry
316 || Ekind (gnat_temp) == E_Entry_Family
317 || Ekind (gnat_temp) == E_Task_Type
318 || (IN (Ekind (gnat_temp), Subprogram_Kind)
319 && present_gnu_tree (gnat_temp)
320 && (current_function_decl
321 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
323 process_type (gnat_entity);
324 return get_gnu_tree (gnat_entity);
328 /* This abort means the Itype has an incorrect scope, i.e. that its
329 scope does not correspond to the subprogram it is declared in. */
330 gcc_unreachable ();
333 /* If we've already processed this entity, return what we got last time.
334 If we are defining the node, we should not have already processed it.
335 In that case, we will abort below when we try to save a new GCC tree
336 for this object. We also need to handle the case of getting a dummy
337 type when a Full_View exists but be careful so as not to trigger its
338 premature elaboration. */
339 if ((!definition || (is_type && imported_p))
340 && present_gnu_tree (gnat_entity))
342 gnu_decl = get_gnu_tree (gnat_entity);
344 if (TREE_CODE (gnu_decl) == TYPE_DECL
345 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
346 && IN (kind, Incomplete_Or_Private_Kind)
347 && Present (Full_View (gnat_entity))
348 && (present_gnu_tree (Full_View (gnat_entity))
349 || No (Freeze_Node (Full_View (gnat_entity)))))
351 gnu_decl
352 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
353 save_gnu_tree (gnat_entity, NULL_TREE, false);
354 save_gnu_tree (gnat_entity, gnu_decl, false);
357 return gnu_decl;
360 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
361 must be specified unless it was specified by the programmer. Exceptions
362 are for access-to-protected-subprogram types and all access subtypes, as
363 another GNAT type is used to lay out the GCC type for them. */
364 gcc_assert (!Unknown_Esize (gnat_entity)
365 || Has_Size_Clause (gnat_entity)
366 || (!IN (kind, Numeric_Kind)
367 && !IN (kind, Enumeration_Kind)
368 && (!IN (kind, Access_Kind)
369 || kind == E_Access_Protected_Subprogram_Type
370 || kind == E_Anonymous_Access_Protected_Subprogram_Type
371 || kind == E_Access_Subtype
372 || type_annotate_only)));
374 /* The RM size must be specified for all discrete and fixed-point types. */
375 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
376 && Unknown_RM_Size (gnat_entity)));
378 /* If we get here, it means we have not yet done anything with this entity.
379 If we are not defining it, it must be a type or an entity that is defined
380 elsewhere or externally, otherwise we should have defined it already. */
381 gcc_assert (definition
382 || type_annotate_only
383 || is_type
384 || kind == E_Discriminant
385 || kind == E_Component
386 || kind == E_Label
387 || (kind == E_Constant && Present (Full_View (gnat_entity)))
388 || Is_Public (gnat_entity));
390 /* Get the name of the entity and set up the line number and filename of
391 the original definition for use in any decl we make. Make sure we do not
392 inherit another source location. */
393 gnu_entity_name = get_entity_name (gnat_entity);
394 if (Sloc (gnat_entity) != No_Location
395 && !renaming_from_generic_instantiation_p (gnat_entity))
396 Sloc_to_locus (Sloc (gnat_entity), &input_location);
398 /* For cases when we are not defining (i.e., we are referencing from
399 another compilation unit) public entities, show we are at global level
400 for the purpose of computing scopes. Don't do this for components or
401 discriminants since the relevant test is whether or not the record is
402 being defined. */
403 if (!definition
404 && kind != E_Component
405 && kind != E_Discriminant
406 && Is_Public (gnat_entity)
407 && !Is_Statically_Allocated (gnat_entity))
408 force_global++, this_global = true;
410 /* Handle any attributes directly attached to the entity. */
411 if (Has_Gigi_Rep_Item (gnat_entity))
412 prepend_attributes (&attr_list, gnat_entity);
414 /* Do some common processing for types. */
415 if (is_type)
417 /* Compute the equivalent type to be used in gigi. */
418 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
420 /* Machine_Attributes on types are expected to be propagated to
421 subtypes. The corresponding Gigi_Rep_Items are only attached
422 to the first subtype though, so we handle the propagation here. */
423 if (Base_Type (gnat_entity) != gnat_entity
424 && !Is_First_Subtype (gnat_entity)
425 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
426 prepend_attributes (&attr_list,
427 First_Subtype (Base_Type (gnat_entity)));
429 /* Compute a default value for the size of an elementary type. */
430 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
432 unsigned int max_esize;
434 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
435 esize = UI_To_Int (Esize (gnat_entity));
437 if (IN (kind, Float_Kind))
438 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
439 else if (IN (kind, Access_Kind))
440 max_esize = POINTER_SIZE * 2;
441 else
442 max_esize = LONG_LONG_TYPE_SIZE;
444 if (esize > max_esize)
445 esize = max_esize;
449 switch (kind)
451 case E_Constant:
452 /* If this is a use of a deferred constant without address clause,
453 get its full definition. */
454 if (!definition
455 && No (Address_Clause (gnat_entity))
456 && Present (Full_View (gnat_entity)))
458 gnu_decl
459 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
460 saved = true;
461 break;
464 /* If we have an external constant that we are not defining, get the
465 expression that is was defined to represent. We may throw it away
466 later if it is not a constant. But do not retrieve the expression
467 if it is an allocator because the designated type might be dummy
468 at this point. */
469 if (!definition
470 && !No_Initialization (Declaration_Node (gnat_entity))
471 && Present (Expression (Declaration_Node (gnat_entity)))
472 && Nkind (Expression (Declaration_Node (gnat_entity)))
473 != N_Allocator)
475 bool went_into_elab_proc = false;
476 int save_force_global = force_global;
478 /* The expression may contain N_Expression_With_Actions nodes and
479 thus object declarations from other units. In this case, even
480 though the expression will eventually be discarded since not a
481 constant, the declarations would be stuck either in the global
482 varpool or in the current scope. Therefore we force the local
483 context and create a fake scope that we'll zap at the end. */
484 if (!current_function_decl)
486 current_function_decl = get_elaboration_procedure ();
487 went_into_elab_proc = true;
489 force_global = 0;
490 gnat_pushlevel ();
492 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
494 gnat_zaplevel ();
495 force_global = save_force_global;
496 if (went_into_elab_proc)
497 current_function_decl = NULL_TREE;
500 /* Ignore deferred constant definitions without address clause since
501 they are processed fully in the front-end. If No_Initialization
502 is set, this is not a deferred constant but a constant whose value
503 is built manually. And constants that are renamings are handled
504 like variables. */
505 if (definition
506 && !gnu_expr
507 && No (Address_Clause (gnat_entity))
508 && !No_Initialization (Declaration_Node (gnat_entity))
509 && No (Renamed_Object (gnat_entity)))
511 gnu_decl = error_mark_node;
512 saved = true;
513 break;
516 /* Ignore constant definitions already marked with the error node. See
517 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
518 if (definition
519 && gnu_expr
520 && present_gnu_tree (gnat_entity)
521 && get_gnu_tree (gnat_entity) == error_mark_node)
523 maybe_present = true;
524 break;
527 goto object;
529 case E_Exception:
530 goto object;
532 case E_Component:
533 case E_Discriminant:
535 /* The GNAT record where the component was defined. */
536 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
538 /* If the entity is a discriminant of an extended tagged type used to
539 rename a discriminant of the parent type, return the latter. */
540 if (Is_Tagged_Type (gnat_record)
541 && Present (Corresponding_Discriminant (gnat_entity)))
543 gnu_decl
544 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
545 gnu_expr, definition);
546 saved = true;
547 break;
550 /* If the entity is an inherited component (in the case of extended
551 tagged record types), just return the original entity, which must
552 be a FIELD_DECL. Likewise for discriminants. If the entity is a
553 non-girder discriminant (in the case of derived untagged record
554 types), return the stored discriminant it renames. */
555 else if (Present (Original_Record_Component (gnat_entity))
556 && Original_Record_Component (gnat_entity) != gnat_entity)
558 gnu_decl
559 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
560 gnu_expr, definition);
561 saved = true;
562 break;
565 /* Otherwise, if we are not defining this and we have no GCC type
566 for the containing record, make one for it. Then we should
567 have made our own equivalent. */
568 else if (!definition && !present_gnu_tree (gnat_record))
570 /* ??? If this is in a record whose scope is a protected
571 type and we have an Original_Record_Component, use it.
572 This is a workaround for major problems in protected type
573 handling. */
574 Entity_Id Scop = Scope (Scope (gnat_entity));
575 if (Is_Protected_Type (Underlying_Type (Scop))
576 && Present (Original_Record_Component (gnat_entity)))
578 gnu_decl
579 = gnat_to_gnu_entity (Original_Record_Component
580 (gnat_entity),
581 gnu_expr, 0);
582 saved = true;
583 break;
586 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
587 gnu_decl = get_gnu_tree (gnat_entity);
588 saved = true;
589 break;
592 else
593 /* Here we have no GCC type and this is a reference rather than a
594 definition. This should never happen. Most likely the cause is
595 reference before declaration in the GNAT tree for gnat_entity. */
596 gcc_unreachable ();
599 case E_Loop_Parameter:
600 case E_Out_Parameter:
601 case E_Variable:
603 /* Simple variables, loop variables, Out parameters and exceptions. */
604 object:
606 /* Always create a variable for volatile objects and variables seen
607 constant but with a Linker_Section pragma. */
608 bool const_flag
609 = ((kind == E_Constant || kind == E_Variable)
610 && Is_True_Constant (gnat_entity)
611 && !(kind == E_Variable
612 && Present (Linker_Section_Pragma (gnat_entity)))
613 && !Treat_As_Volatile (gnat_entity)
614 && (((Nkind (Declaration_Node (gnat_entity))
615 == N_Object_Declaration)
616 && Present (Expression (Declaration_Node (gnat_entity))))
617 || Present (Renamed_Object (gnat_entity))
618 || imported_p));
619 bool inner_const_flag = const_flag;
620 bool static_p = Is_Statically_Allocated (gnat_entity);
621 bool mutable_p = false;
622 bool used_by_ref = false;
623 tree gnu_ext_name = NULL_TREE;
624 tree renamed_obj = NULL_TREE;
625 tree gnu_object_size;
627 if (Present (Renamed_Object (gnat_entity)) && !definition)
629 if (kind == E_Exception)
630 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
631 NULL_TREE, 0);
632 else
633 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
636 /* Get the type after elaborating the renamed object. */
637 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
639 /* If this is a standard exception definition, then use the standard
640 exception type. This is necessary to make sure that imported and
641 exported views of exceptions are properly merged in LTO mode. */
642 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
643 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
644 gnu_type = except_type_node;
646 /* For a debug renaming declaration, build a debug-only entity. */
647 if (Present (Debug_Renaming_Link (gnat_entity)))
649 /* Force a non-null value to make sure the symbol is retained. */
650 tree value = build1 (INDIRECT_REF, gnu_type,
651 build1 (NOP_EXPR,
652 build_pointer_type (gnu_type),
653 integer_minus_one_node));
654 gnu_decl = build_decl (input_location,
655 VAR_DECL, gnu_entity_name, gnu_type);
656 SET_DECL_VALUE_EXPR (gnu_decl, value);
657 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
658 gnat_pushdecl (gnu_decl, gnat_entity);
659 break;
662 /* If this is a loop variable, its type should be the base type.
663 This is because the code for processing a loop determines whether
664 a normal loop end test can be done by comparing the bounds of the
665 loop against those of the base type, which is presumed to be the
666 size used for computation. But this is not correct when the size
667 of the subtype is smaller than the type. */
668 if (kind == E_Loop_Parameter)
669 gnu_type = get_base_type (gnu_type);
671 /* Reject non-renamed objects whose type is an unconstrained array or
672 any object whose type is a dummy type or void. */
673 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
674 && No (Renamed_Object (gnat_entity)))
675 || TYPE_IS_DUMMY_P (gnu_type)
676 || TREE_CODE (gnu_type) == VOID_TYPE)
678 gcc_assert (type_annotate_only);
679 if (this_global)
680 force_global--;
681 return error_mark_node;
684 /* If an alignment is specified, use it if valid. Note that exceptions
685 are objects but don't have an alignment. We must do this before we
686 validate the size, since the alignment can affect the size. */
687 if (kind != E_Exception && Known_Alignment (gnat_entity))
689 gcc_assert (Present (Alignment (gnat_entity)));
691 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
692 TYPE_ALIGN (gnu_type));
694 /* No point in changing the type if there is an address clause
695 as the final type of the object will be a reference type. */
696 if (Present (Address_Clause (gnat_entity)))
697 align = 0;
698 else
700 tree orig_type = gnu_type;
702 gnu_type
703 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
704 false, false, definition, true);
706 /* If a padding record was made, declare it now since it will
707 never be declared otherwise. This is necessary to ensure
708 that its subtrees are properly marked. */
709 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
710 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
711 debug_info_p, gnat_entity);
715 /* If we are defining the object, see if it has a Size and validate it
716 if so. If we are not defining the object and a Size clause applies,
717 simply retrieve the value. We don't want to ignore the clause and
718 it is expected to have been validated already. Then get the new
719 type, if any. */
720 if (definition)
721 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
722 gnat_entity, VAR_DECL, false,
723 Has_Size_Clause (gnat_entity));
724 else if (Has_Size_Clause (gnat_entity))
725 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
727 if (gnu_size)
729 gnu_type
730 = make_type_from_size (gnu_type, gnu_size,
731 Has_Biased_Representation (gnat_entity));
733 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
734 gnu_size = NULL_TREE;
737 /* If this object has self-referential size, it must be a record with
738 a default discriminant. We are supposed to allocate an object of
739 the maximum size in this case, unless it is a constant with an
740 initializing expression, in which case we can get the size from
741 that. Note that the resulting size may still be a variable, so
742 this may end up with an indirect allocation. */
743 if (No (Renamed_Object (gnat_entity))
744 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
746 if (gnu_expr && kind == E_Constant)
748 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
749 if (CONTAINS_PLACEHOLDER_P (size))
751 /* If the initializing expression is itself a constant,
752 despite having a nominal type with self-referential
753 size, we can get the size directly from it. */
754 if (TREE_CODE (gnu_expr) == COMPONENT_REF
755 && TYPE_IS_PADDING_P
756 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
757 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
758 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
759 || DECL_READONLY_ONCE_ELAB
760 (TREE_OPERAND (gnu_expr, 0))))
761 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
762 else
763 gnu_size
764 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
766 else
767 gnu_size = size;
769 /* We may have no GNU_EXPR because No_Initialization is
770 set even though there's an Expression. */
771 else if (kind == E_Constant
772 && (Nkind (Declaration_Node (gnat_entity))
773 == N_Object_Declaration)
774 && Present (Expression (Declaration_Node (gnat_entity))))
775 gnu_size
776 = TYPE_SIZE (gnat_to_gnu_type
777 (Etype
778 (Expression (Declaration_Node (gnat_entity)))));
779 else
781 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
782 mutable_p = true;
785 /* If we are at global level and the size isn't constant, call
786 elaborate_expression_1 to make a variable for it rather than
787 calculating it each time. */
788 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
789 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
790 get_identifier ("SIZE"),
791 definition, false);
794 /* If the size is zero byte, make it one byte since some linkers have
795 troubles with zero-sized objects. If the object will have a
796 template, that will make it nonzero so don't bother. Also avoid
797 doing that for an object renaming or an object with an address
798 clause, as we would lose useful information on the view size
799 (e.g. for null array slices) and we are not allocating the object
800 here anyway. */
801 if (((gnu_size
802 && integer_zerop (gnu_size)
803 && !TREE_OVERFLOW (gnu_size))
804 || (TYPE_SIZE (gnu_type)
805 && integer_zerop (TYPE_SIZE (gnu_type))
806 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
807 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
808 && No (Renamed_Object (gnat_entity))
809 && No (Address_Clause (gnat_entity)))
810 gnu_size = bitsize_unit_node;
812 /* If this is an object with no specified size and alignment, and
813 if either it is atomic or we are not optimizing alignment for
814 space and it is composite and not an exception, an Out parameter
815 or a reference to another object, and the size of its type is a
816 constant, set the alignment to the smallest one which is not
817 smaller than the size, with an appropriate cap. */
818 if (!gnu_size && align == 0
819 && (Is_Atomic (gnat_entity)
820 || (!Optimize_Alignment_Space (gnat_entity)
821 && kind != E_Exception
822 && kind != E_Out_Parameter
823 && Is_Composite_Type (Etype (gnat_entity))
824 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
825 && !Is_Exported (gnat_entity)
826 && !imported_p
827 && No (Renamed_Object (gnat_entity))
828 && No (Address_Clause (gnat_entity))))
829 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
831 unsigned int size_cap, align_cap;
833 /* No point in promoting the alignment if this doesn't prevent
834 BLKmode access to the object, in particular block copy, as
835 this will for example disable the NRV optimization for it.
836 No point in jumping through all the hoops needed in order
837 to support BIGGEST_ALIGNMENT if we don't really have to.
838 So we cap to the smallest alignment that corresponds to
839 a known efficient memory access pattern of the target. */
840 if (Is_Atomic (gnat_entity))
842 size_cap = UINT_MAX;
843 align_cap = BIGGEST_ALIGNMENT;
845 else
847 size_cap = MAX_FIXED_MODE_SIZE;
848 align_cap = get_mode_alignment (ptr_mode);
851 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
852 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
853 align = 0;
854 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
855 align = align_cap;
856 else
857 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
859 /* But make sure not to under-align the object. */
860 if (align <= TYPE_ALIGN (gnu_type))
861 align = 0;
863 /* And honor the minimum valid atomic alignment, if any. */
864 #ifdef MINIMUM_ATOMIC_ALIGNMENT
865 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
866 align = MINIMUM_ATOMIC_ALIGNMENT;
867 #endif
870 /* If the object is set to have atomic components, find the component
871 type and validate it.
873 ??? Note that we ignore Has_Volatile_Components on objects; it's
874 not at all clear what to do in that case. */
875 if (Has_Atomic_Components (gnat_entity))
877 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
878 ? TREE_TYPE (gnu_type) : gnu_type);
880 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
881 && TYPE_MULTI_ARRAY_P (gnu_inner))
882 gnu_inner = TREE_TYPE (gnu_inner);
884 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
887 /* Now check if the type of the object allows atomic access. Note
888 that we must test the type, even if this object has size and
889 alignment to allow such access, because we will be going inside
890 the padded record to assign to the object. We could fix this by
891 always copying via an intermediate value, but it's not clear it's
892 worth the effort. */
893 if (Is_Atomic (gnat_entity))
894 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
896 /* If this is an aliased object with an unconstrained nominal subtype,
897 make a type that includes the template. */
898 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
899 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
900 && !type_annotate_only)
902 tree gnu_array
903 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
904 gnu_type
905 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
906 gnu_type,
907 concat_name (gnu_entity_name,
908 "UNC"),
909 debug_info_p);
912 /* ??? If this is an object of CW type initialized to a value, try to
913 ensure that the object is sufficient aligned for this value, but
914 without pessimizing the allocation. This is a kludge necessary
915 because we don't support dynamic alignment. */
916 if (align == 0
917 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
918 && No (Renamed_Object (gnat_entity))
919 && No (Address_Clause (gnat_entity)))
920 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
922 #ifdef MINIMUM_ATOMIC_ALIGNMENT
923 /* If the size is a constant and no alignment is specified, force
924 the alignment to be the minimum valid atomic alignment. The
925 restriction on constant size avoids problems with variable-size
926 temporaries; if the size is variable, there's no issue with
927 atomic access. Also don't do this for a constant, since it isn't
928 necessary and can interfere with constant replacement. Finally,
929 do not do it for Out parameters since that creates an
930 size inconsistency with In parameters. */
931 if (align == 0
932 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
933 && !FLOAT_TYPE_P (gnu_type)
934 && !const_flag && No (Renamed_Object (gnat_entity))
935 && !imported_p && No (Address_Clause (gnat_entity))
936 && kind != E_Out_Parameter
937 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
938 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
939 align = MINIMUM_ATOMIC_ALIGNMENT;
940 #endif
942 /* Make a new type with the desired size and alignment, if needed.
943 But do not take into account alignment promotions to compute the
944 size of the object. */
945 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
946 if (gnu_size || align > 0)
948 tree orig_type = gnu_type;
950 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
951 false, false, definition, true);
953 /* If a padding record was made, declare it now since it will
954 never be declared otherwise. This is necessary to ensure
955 that its subtrees are properly marked. */
956 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
957 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
958 debug_info_p, gnat_entity);
961 /* If this is a renaming, avoid as much as possible to create a new
962 object. However, in several cases, creating it is required.
963 This processing needs to be applied to the raw expression so
964 as to make it more likely to rename the underlying object. */
965 if (Present (Renamed_Object (gnat_entity)))
967 bool create_normal_object = false;
969 /* If the renamed object had padding, strip off the reference
970 to the inner object and reset our type. */
971 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
972 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
973 /* Strip useless conversions around the object. */
974 || gnat_useless_type_conversion (gnu_expr))
976 gnu_expr = TREE_OPERAND (gnu_expr, 0);
977 gnu_type = TREE_TYPE (gnu_expr);
980 /* Or else, if the renamed object has an unconstrained type with
981 default discriminant, use the padded type. */
982 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
983 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
984 == gnu_type
985 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
986 gnu_type = TREE_TYPE (gnu_expr);
988 /* Case 1: If this is a constant renaming stemming from a function
989 call, treat it as a normal object whose initial value is what is
990 being renamed. RM 3.3 says that the result of evaluating a
991 function call is a constant object. Treat constant literals
992 the same way. As a consequence, it can be the inner object of
993 a constant renaming. In this case, the renaming must be fully
994 instantiated, i.e. it cannot be a mere reference to (part of) an
995 existing object. */
996 if (const_flag)
998 tree inner_object = gnu_expr;
999 while (handled_component_p (inner_object))
1000 inner_object = TREE_OPERAND (inner_object, 0);
1001 if (TREE_CODE (inner_object) == CALL_EXPR
1002 || CONSTANT_CLASS_P (inner_object))
1003 create_normal_object = true;
1006 /* Otherwise, see if we can proceed with a stabilized version of
1007 the renamed entity or if we need to make a new object. */
1008 if (!create_normal_object)
1010 tree maybe_stable_expr = NULL_TREE;
1011 bool stable = false;
1013 /* Case 2: If the renaming entity need not be materialized and
1014 the renamed expression is something we can stabilize, use
1015 that for the renaming. At the global level, we can only do
1016 this if we know no SAVE_EXPRs need be made, because the
1017 expression we return might be used in arbitrary conditional
1018 branches so we must force the evaluation of the SAVE_EXPRs
1019 immediately and this requires a proper function context.
1020 Note that an external constant is at the global level. */
1021 if (!Materialize_Entity (gnat_entity)
1022 && (!((!definition && kind == E_Constant)
1023 || global_bindings_p ())
1024 || (staticp (gnu_expr)
1025 && !TREE_SIDE_EFFECTS (gnu_expr))))
1027 maybe_stable_expr
1028 = gnat_stabilize_reference (gnu_expr, true, &stable);
1030 if (stable)
1032 /* ??? No DECL_EXPR is created so we need to mark
1033 the expression manually lest it is shared. */
1034 if ((!definition && kind == E_Constant)
1035 || global_bindings_p ())
1036 MARK_VISITED (maybe_stable_expr);
1037 gnu_decl = maybe_stable_expr;
1038 save_gnu_tree (gnat_entity, gnu_decl, true);
1039 saved = true;
1040 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1041 false);
1042 /* This assertion will fail if the renamed object
1043 isn't aligned enough as to make it possible to
1044 honor the alignment set on the renaming. */
1045 if (align)
1047 unsigned int renamed_align
1048 = DECL_P (gnu_decl)
1049 ? DECL_ALIGN (gnu_decl)
1050 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1051 gcc_assert (renamed_align >= align);
1053 break;
1056 /* The stabilization failed. Keep maybe_stable_expr
1057 untouched here to let the pointer case below know
1058 about that failure. */
1061 /* Case 3: Make this into a constant pointer to the object we
1062 are to rename and attach the object to the pointer if it is
1063 something we can stabilize.
1065 From the proper scope, attached objects will be referenced
1066 directly instead of indirectly via the pointer to avoid
1067 subtle aliasing problems with non-addressable entities.
1068 They have to be stable because we must not evaluate the
1069 variables in the expression every time the renaming is used.
1070 The pointer is called a "renaming" pointer in this case.
1072 In the rare cases where we cannot stabilize the renamed
1073 object, we just make a "bare" pointer and the renamed
1074 object will always be accessed indirectly through it.
1076 Note that we need to preserve the volatility of the renamed
1077 object through the indirection. */
1078 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1079 gnu_type
1080 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1081 gnu_type = build_reference_type (gnu_type);
1082 inner_const_flag = TREE_READONLY (gnu_expr);
1083 const_flag = true;
1085 /* If the previous attempt at stabilizing failed, there is
1086 no point in trying again and we reuse the result without
1087 attaching it to the pointer. In this case it will only
1088 be used as the initializing expression of the pointer and
1089 thus needs no special treatment with regard to multiple
1090 evaluations.
1092 Otherwise, try to stabilize and attach the expression to
1093 the pointer if the stabilization succeeds.
1095 Note that this might introduce SAVE_EXPRs and we don't
1096 check whether we are at the global level or not. This
1097 is fine since we are building a pointer initializer and
1098 neither the pointer nor the initializing expression can
1099 be accessed before the pointer elaboration has taken
1100 place in a correct program.
1102 These SAVE_EXPRs will be evaluated at the right place
1103 by either the evaluation of the initializer for the
1104 non-global case or the elaboration code for the global
1105 case, and will be attached to the elaboration procedure
1106 in the latter case. */
1107 if (!maybe_stable_expr)
1109 maybe_stable_expr
1110 = gnat_stabilize_reference (gnu_expr, true, &stable);
1112 if (stable)
1113 renamed_obj = maybe_stable_expr;
1116 if (type_annotate_only
1117 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1118 gnu_expr = NULL_TREE;
1119 else
1120 gnu_expr
1121 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1123 gnu_size = NULL_TREE;
1124 used_by_ref = true;
1128 /* Make a volatile version of this object's type if we are to make
1129 the object volatile. We also interpret 13.3(19) conservatively
1130 and disallow any optimizations for such a non-constant object. */
1131 if ((Treat_As_Volatile (gnat_entity)
1132 || (!const_flag
1133 && gnu_type != except_type_node
1134 && (Is_Exported (gnat_entity)
1135 || imported_p
1136 || Present (Address_Clause (gnat_entity)))))
1137 && !TYPE_VOLATILE (gnu_type))
1138 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1140 /* If we are defining an aliased object whose nominal subtype is
1141 unconstrained, the object is a record that contains both the
1142 template and the object. If there is an initializer, it will
1143 have already been converted to the right type, but we need to
1144 create the template if there is no initializer. */
1145 if (definition
1146 && !gnu_expr
1147 && TREE_CODE (gnu_type) == RECORD_TYPE
1148 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1149 /* Beware that padding might have been introduced above. */
1150 || (TYPE_PADDING_P (gnu_type)
1151 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1152 == RECORD_TYPE
1153 && TYPE_CONTAINS_TEMPLATE_P
1154 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1156 tree template_field
1157 = TYPE_PADDING_P (gnu_type)
1158 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1159 : TYPE_FIELDS (gnu_type);
1160 vec<constructor_elt, va_gc> *v;
1161 vec_alloc (v, 1);
1162 tree t = build_template (TREE_TYPE (template_field),
1163 TREE_TYPE (DECL_CHAIN (template_field)),
1164 NULL_TREE);
1165 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1166 gnu_expr = gnat_build_constructor (gnu_type, v);
1169 /* Convert the expression to the type of the object except in the
1170 case where the object's type is unconstrained or the object's type
1171 is a padded record whose field is of self-referential size. In
1172 the former case, converting will generate unnecessary evaluations
1173 of the CONSTRUCTOR to compute the size and in the latter case, we
1174 want to only copy the actual data. Also don't convert to a record
1175 type with a variant part from a record type without one, to keep
1176 the object simpler. */
1177 if (gnu_expr
1178 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1179 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1180 && !(TYPE_IS_PADDING_P (gnu_type)
1181 && CONTAINS_PLACEHOLDER_P
1182 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1183 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1184 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1185 && get_variant_part (gnu_type) != NULL_TREE
1186 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1187 gnu_expr = convert (gnu_type, gnu_expr);
1189 /* If this is a pointer that doesn't have an initializing expression,
1190 initialize it to NULL, unless the object is imported. */
1191 if (definition
1192 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1193 && !gnu_expr
1194 && !Is_Imported (gnat_entity))
1195 gnu_expr = integer_zero_node;
1197 /* If we are defining the object and it has an Address clause, we must
1198 either get the address expression from the saved GCC tree for the
1199 object if it has a Freeze node, or elaborate the address expression
1200 here since the front-end has guaranteed that the elaboration has no
1201 effects in this case. */
1202 if (definition && Present (Address_Clause (gnat_entity)))
1204 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1205 tree gnu_address
1206 = present_gnu_tree (gnat_entity)
1207 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1209 save_gnu_tree (gnat_entity, NULL_TREE, false);
1211 /* Ignore the size. It's either meaningless or was handled
1212 above. */
1213 gnu_size = NULL_TREE;
1214 /* Convert the type of the object to a reference type that can
1215 alias everything as per 13.3(19). */
1216 gnu_type
1217 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1218 gnu_address = convert (gnu_type, gnu_address);
1219 used_by_ref = true;
1220 const_flag
1221 = !Is_Public (gnat_entity)
1222 || compile_time_known_address_p (gnat_expr);
1224 /* If this is a deferred constant, the initializer is attached to
1225 the full view. */
1226 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1227 gnu_expr
1228 = gnat_to_gnu
1229 (Expression (Declaration_Node (Full_View (gnat_entity))));
1231 /* If we don't have an initializing expression for the underlying
1232 variable, the initializing expression for the pointer is the
1233 specified address. Otherwise, we have to make a COMPOUND_EXPR
1234 to assign both the address and the initial value. */
1235 if (!gnu_expr)
1236 gnu_expr = gnu_address;
1237 else
1238 gnu_expr
1239 = build2 (COMPOUND_EXPR, gnu_type,
1240 build_binary_op
1241 (MODIFY_EXPR, NULL_TREE,
1242 build_unary_op (INDIRECT_REF, NULL_TREE,
1243 gnu_address),
1244 gnu_expr),
1245 gnu_address);
1248 /* If it has an address clause and we are not defining it, mark it
1249 as an indirect object. Likewise for Stdcall objects that are
1250 imported. */
1251 if ((!definition && Present (Address_Clause (gnat_entity)))
1252 || (Is_Imported (gnat_entity)
1253 && Has_Stdcall_Convention (gnat_entity)))
1255 /* Convert the type of the object to a reference type that can
1256 alias everything as per 13.3(19). */
1257 gnu_type
1258 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1259 gnu_size = NULL_TREE;
1261 /* No point in taking the address of an initializing expression
1262 that isn't going to be used. */
1263 gnu_expr = NULL_TREE;
1265 /* If it has an address clause whose value is known at compile
1266 time, make the object a CONST_DECL. This will avoid a
1267 useless dereference. */
1268 if (Present (Address_Clause (gnat_entity)))
1270 Node_Id gnat_address
1271 = Expression (Address_Clause (gnat_entity));
1273 if (compile_time_known_address_p (gnat_address))
1275 gnu_expr = gnat_to_gnu (gnat_address);
1276 const_flag = true;
1280 used_by_ref = true;
1283 /* If we are at top level and this object is of variable size,
1284 make the actual type a hidden pointer to the real type and
1285 make the initializer be a memory allocation and initialization.
1286 Likewise for objects we aren't defining (presumed to be
1287 external references from other packages), but there we do
1288 not set up an initialization.
1290 If the object's size overflows, make an allocator too, so that
1291 Storage_Error gets raised. Note that we will never free
1292 such memory, so we presume it never will get allocated. */
1293 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1294 global_bindings_p ()
1295 || !definition
1296 || static_p)
1297 || (gnu_size
1298 && !allocatable_size_p (convert (sizetype,
1299 size_binop
1300 (CEIL_DIV_EXPR, gnu_size,
1301 bitsize_unit_node)),
1302 global_bindings_p ()
1303 || !definition
1304 || static_p)))
1306 gnu_type = build_reference_type (gnu_type);
1307 gnu_size = NULL_TREE;
1308 used_by_ref = true;
1310 /* In case this was a aliased object whose nominal subtype is
1311 unconstrained, the pointer above will be a thin pointer and
1312 build_allocator will automatically make the template.
1314 If we have a template initializer only (that we made above),
1315 pretend there is none and rely on what build_allocator creates
1316 again anyway. Otherwise (if we have a full initializer), get
1317 the data part and feed that to build_allocator.
1319 If we are elaborating a mutable object, tell build_allocator to
1320 ignore a possibly simpler size from the initializer, if any, as
1321 we must allocate the maximum possible size in this case. */
1322 if (definition && !imported_p)
1324 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1326 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1327 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1329 gnu_alloc_type
1330 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1332 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1333 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1334 gnu_expr = 0;
1335 else
1336 gnu_expr
1337 = build_component_ref
1338 (gnu_expr, NULL_TREE,
1339 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1340 false);
1343 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1344 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1345 post_error ("?`Storage_Error` will be raised at run time!",
1346 gnat_entity);
1348 gnu_expr
1349 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1350 Empty, Empty, gnat_entity, mutable_p);
1351 const_flag = true;
1353 else
1355 gnu_expr = NULL_TREE;
1356 const_flag = false;
1360 /* If this object would go into the stack and has an alignment larger
1361 than the largest stack alignment the back-end can honor, resort to
1362 a variable of "aligning type". */
1363 if (!global_bindings_p () && !static_p && definition
1364 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1366 /* Create the new variable. No need for extra room before the
1367 aligned field as this is in automatic storage. */
1368 tree gnu_new_type
1369 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1370 TYPE_SIZE_UNIT (gnu_type),
1371 BIGGEST_ALIGNMENT, 0, gnat_entity);
1372 tree gnu_new_var
1373 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1374 NULL_TREE, gnu_new_type, NULL_TREE, false,
1375 false, false, false, NULL, gnat_entity);
1377 /* Initialize the aligned field if we have an initializer. */
1378 if (gnu_expr)
1379 add_stmt_with_node
1380 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1381 build_component_ref
1382 (gnu_new_var, NULL_TREE,
1383 TYPE_FIELDS (gnu_new_type), false),
1384 gnu_expr),
1385 gnat_entity);
1387 /* And setup this entity as a reference to the aligned field. */
1388 gnu_type = build_reference_type (gnu_type);
1389 gnu_expr
1390 = build_unary_op
1391 (ADDR_EXPR, gnu_type,
1392 build_component_ref (gnu_new_var, NULL_TREE,
1393 TYPE_FIELDS (gnu_new_type), false));
1395 gnu_size = NULL_TREE;
1396 used_by_ref = true;
1397 const_flag = true;
1400 /* If this is an aliased object with an unconstrained nominal subtype,
1401 we make its type a thin reference, i.e. the reference counterpart
1402 of a thin pointer, so that it points to the array part. This is
1403 aimed at making it easier for the debugger to decode the object.
1404 Note that we have to do that this late because of the couple of
1405 allocation adjustments that might be made just above. */
1406 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1407 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1408 && !type_annotate_only)
1410 tree gnu_array
1411 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1413 /* In case the object with the template has already been allocated
1414 just above, we have nothing to do here. */
1415 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1417 tree gnu_unc_var
1418 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1419 NULL_TREE, gnu_type, gnu_expr,
1420 const_flag, Is_Public (gnat_entity),
1421 imported_p || !definition, static_p,
1422 NULL, gnat_entity);
1423 gnu_expr
1424 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1425 TREE_CONSTANT (gnu_expr) = 1;
1427 gnu_size = NULL_TREE;
1428 used_by_ref = true;
1429 inner_const_flag = TREE_READONLY (gnu_unc_var);
1430 const_flag = true;
1433 gnu_type
1434 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1437 if (const_flag)
1438 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1440 /* Convert the expression to the type of the object except in the
1441 case where the object's type is unconstrained or the object's type
1442 is a padded record whose field is of self-referential size. In
1443 the former case, converting will generate unnecessary evaluations
1444 of the CONSTRUCTOR to compute the size and in the latter case, we
1445 want to only copy the actual data. Also don't convert to a record
1446 type with a variant part from a record type without one, to keep
1447 the object simpler. */
1448 if (gnu_expr
1449 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1450 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1451 && !(TYPE_IS_PADDING_P (gnu_type)
1452 && CONTAINS_PLACEHOLDER_P
1453 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1454 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1455 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1456 && get_variant_part (gnu_type) != NULL_TREE
1457 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1458 gnu_expr = convert (gnu_type, gnu_expr);
1460 /* If this name is external or a name was specified, use it, but don't
1461 use the Interface_Name with an address clause (see cd30005). */
1462 if ((Present (Interface_Name (gnat_entity))
1463 && No (Address_Clause (gnat_entity)))
1464 || (Is_Public (gnat_entity)
1465 && (!Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1466 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1468 /* If this is an aggregate constant initialized to a constant, force it
1469 to be statically allocated. This saves an initialization copy. */
1470 if (!static_p
1471 && const_flag
1472 && gnu_expr && TREE_CONSTANT (gnu_expr)
1473 && AGGREGATE_TYPE_P (gnu_type)
1474 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1475 && !(TYPE_IS_PADDING_P (gnu_type)
1476 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1477 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1478 static_p = true;
1480 /* Deal with a pragma Linker_Section on a constant or variable. */
1481 if ((kind == E_Constant || kind == E_Variable)
1482 && Present (Linker_Section_Pragma (gnat_entity)))
1483 prepend_one_attribute_pragma (&attr_list,
1484 Linker_Section_Pragma (gnat_entity));
1486 /* Now create the variable or the constant and set various flags. */
1487 gnu_decl
1488 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1489 gnu_expr, const_flag, Is_Public (gnat_entity),
1490 imported_p || !definition, static_p,
1491 !renamed_obj, attr_list, gnat_entity);
1492 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1493 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1494 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1496 /* If we are defining an Out parameter and optimization isn't enabled,
1497 create a fake PARM_DECL for debugging purposes and make it point to
1498 the VAR_DECL. Suppress debug info for the latter but make sure it
1499 will live in memory so that it can be accessed from within the
1500 debugger through the PARM_DECL. */
1501 if (kind == E_Out_Parameter
1502 && definition
1503 && debug_info_p
1504 && !optimize
1505 && !flag_generate_lto)
1507 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1508 gnat_pushdecl (param, gnat_entity);
1509 SET_DECL_VALUE_EXPR (param, gnu_decl);
1510 DECL_HAS_VALUE_EXPR_P (param) = 1;
1511 DECL_IGNORED_P (gnu_decl) = 1;
1512 TREE_ADDRESSABLE (gnu_decl) = 1;
1515 /* If this is a loop parameter, set the corresponding flag. */
1516 else if (kind == E_Loop_Parameter)
1517 DECL_LOOP_PARM_P (gnu_decl) = 1;
1519 /* If this is a renaming pointer, attach the renamed object to it and
1520 register it if we are at the global level. Note that an external
1521 constant is at the global level. */
1522 if (renamed_obj)
1524 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1525 if ((!definition && kind == E_Constant) || global_bindings_p ())
1527 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1528 record_global_renaming_pointer (gnu_decl);
1532 /* If this is a constant and we are defining it or it generates a real
1533 symbol at the object level and we are referencing it, we may want
1534 or need to have a true variable to represent it:
1535 - if optimization isn't enabled, for debugging purposes,
1536 - if the constant is public and not overlaid on something else,
1537 - if its address is taken,
1538 - if either itself or its type is aliased. */
1539 if (TREE_CODE (gnu_decl) == CONST_DECL
1540 && (definition || Sloc (gnat_entity) > Standard_Location)
1541 && ((!optimize && debug_info_p)
1542 || (Is_Public (gnat_entity)
1543 && No (Address_Clause (gnat_entity)))
1544 || Address_Taken (gnat_entity)
1545 || Is_Aliased (gnat_entity)
1546 || Is_Aliased (Etype (gnat_entity))))
1548 tree gnu_corr_var
1549 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1550 gnu_expr, true, Is_Public (gnat_entity),
1551 !definition, static_p, attr_list,
1552 gnat_entity);
1554 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1556 /* As debugging information will be generated for the variable,
1557 do not generate debugging information for the constant. */
1558 if (debug_info_p)
1559 DECL_IGNORED_P (gnu_decl) = 1;
1560 else
1561 DECL_IGNORED_P (gnu_corr_var) = 1;
1564 /* If this is a constant, even if we don't need a true variable, we
1565 may need to avoid returning the initializer in every case. That
1566 can happen for the address of a (constant) constructor because,
1567 upon dereferencing it, the constructor will be reinjected in the
1568 tree, which may not be valid in every case; see lvalue_required_p
1569 for more details. */
1570 if (TREE_CODE (gnu_decl) == CONST_DECL)
1571 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1573 /* If this object is declared in a block that contains a block with an
1574 exception handler, and we aren't using the GCC exception mechanism,
1575 we must force this variable in memory in order to avoid an invalid
1576 optimization. */
1577 if (Exception_Mechanism != Back_End_Exceptions
1578 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1579 TREE_ADDRESSABLE (gnu_decl) = 1;
1581 /* If this is a local variable with non-BLKmode and aggregate type,
1582 and optimization isn't enabled, then force it in memory so that
1583 a register won't be allocated to it with possible subparts left
1584 uninitialized and reaching the register allocator. */
1585 else if (TREE_CODE (gnu_decl) == VAR_DECL
1586 && !DECL_EXTERNAL (gnu_decl)
1587 && !TREE_STATIC (gnu_decl)
1588 && DECL_MODE (gnu_decl) != BLKmode
1589 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1590 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1591 && !optimize)
1592 TREE_ADDRESSABLE (gnu_decl) = 1;
1594 /* If we are defining an object with variable size or an object with
1595 fixed size that will be dynamically allocated, and we are using the
1596 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1597 if (definition
1598 && Exception_Mechanism == Setjmp_Longjmp
1599 && get_block_jmpbuf_decl ()
1600 && DECL_SIZE_UNIT (gnu_decl)
1601 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1602 || (flag_stack_check == GENERIC_STACK_CHECK
1603 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1604 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1605 add_stmt_with_node (build_call_n_expr
1606 (update_setjmp_buf_decl, 1,
1607 build_unary_op (ADDR_EXPR, NULL_TREE,
1608 get_block_jmpbuf_decl ())),
1609 gnat_entity);
1611 /* Back-annotate Esize and Alignment of the object if not already
1612 known. Note that we pick the values of the type, not those of
1613 the object, to shield ourselves from low-level platform-dependent
1614 adjustments like alignment promotion. This is both consistent with
1615 all the treatment above, where alignment and size are set on the
1616 type of the object and not on the object directly, and makes it
1617 possible to support all confirming representation clauses. */
1618 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1619 used_by_ref);
1621 break;
1623 case E_Void:
1624 /* Return a TYPE_DECL for "void" that we previously made. */
1625 gnu_decl = TYPE_NAME (void_type_node);
1626 break;
1628 case E_Enumeration_Type:
1629 /* A special case: for the types Character and Wide_Character in
1630 Standard, we do not list all the literals. So if the literals
1631 are not specified, make this an unsigned integer type. */
1632 if (No (First_Literal (gnat_entity)))
1634 gnu_type = make_unsigned_type (esize);
1635 TYPE_NAME (gnu_type) = gnu_entity_name;
1637 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1638 This is needed by the DWARF-2 back-end to distinguish between
1639 unsigned integer types and character types. */
1640 TYPE_STRING_FLAG (gnu_type) = 1;
1642 else
1644 /* We have a list of enumeral constants in First_Literal. We make a
1645 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1646 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1647 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1648 value of the literal. But when we have a regular boolean type, we
1649 simplify this a little by using a BOOLEAN_TYPE. */
1650 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1651 && !Has_Non_Standard_Rep (gnat_entity);
1652 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1653 tree gnu_list = NULL_TREE;
1654 Entity_Id gnat_literal;
1656 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1657 TYPE_PRECISION (gnu_type) = esize;
1658 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1659 set_min_and_max_values_for_integral_type (gnu_type, esize,
1660 TYPE_SIGN (gnu_type));
1661 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1662 layout_type (gnu_type);
1664 for (gnat_literal = First_Literal (gnat_entity);
1665 Present (gnat_literal);
1666 gnat_literal = Next_Literal (gnat_literal))
1668 tree gnu_value
1669 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1670 tree gnu_literal
1671 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1672 gnu_type, gnu_value, true, false, false,
1673 false, NULL, gnat_literal);
1674 /* Do not generate debug info for individual enumerators. */
1675 DECL_IGNORED_P (gnu_literal) = 1;
1676 save_gnu_tree (gnat_literal, gnu_literal, false);
1677 gnu_list
1678 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1681 if (!is_boolean)
1682 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1684 /* Note that the bounds are updated at the end of this function
1685 to avoid an infinite recursion since they refer to the type. */
1686 goto discrete_type;
1688 break;
1690 case E_Signed_Integer_Type:
1691 case E_Ordinary_Fixed_Point_Type:
1692 case E_Decimal_Fixed_Point_Type:
1693 /* For integer types, just make a signed type the appropriate number
1694 of bits. */
1695 gnu_type = make_signed_type (esize);
1696 goto discrete_type;
1698 case E_Modular_Integer_Type:
1700 /* For modular types, make the unsigned type of the proper number
1701 of bits and then set up the modulus, if required. */
1702 tree gnu_modulus, gnu_high = NULL_TREE;
1704 /* Packed Array Impl. Types are supposed to be subtypes only. */
1705 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1707 gnu_type = make_unsigned_type (esize);
1709 /* Get the modulus in this type. If it overflows, assume it is because
1710 it is equal to 2**Esize. Note that there is no overflow checking
1711 done on unsigned type, so we detect the overflow by looking for
1712 a modulus of zero, which is otherwise invalid. */
1713 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1715 if (!integer_zerop (gnu_modulus))
1717 TYPE_MODULAR_P (gnu_type) = 1;
1718 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1719 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1720 convert (gnu_type, integer_one_node));
1723 /* If the upper bound is not maximal, make an extra subtype. */
1724 if (gnu_high
1725 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1727 tree gnu_subtype = make_unsigned_type (esize);
1728 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1729 TREE_TYPE (gnu_subtype) = gnu_type;
1730 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1731 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1732 gnu_type = gnu_subtype;
1735 goto discrete_type;
1737 case E_Signed_Integer_Subtype:
1738 case E_Enumeration_Subtype:
1739 case E_Modular_Integer_Subtype:
1740 case E_Ordinary_Fixed_Point_Subtype:
1741 case E_Decimal_Fixed_Point_Subtype:
1743 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1744 not want to call create_range_type since we would like each subtype
1745 node to be distinct. ??? Historically this was in preparation for
1746 when memory aliasing is implemented, but that's obsolete now given
1747 the call to relate_alias_sets below.
1749 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1750 this fact is used by the arithmetic conversion functions.
1752 We elaborate the Ancestor_Subtype if it is not in the current unit
1753 and one of our bounds is non-static. We do this to ensure consistent
1754 naming in the case where several subtypes share the same bounds, by
1755 elaborating the first such subtype first, thus using its name. */
1757 if (!definition
1758 && Present (Ancestor_Subtype (gnat_entity))
1759 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1760 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1761 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1762 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1764 /* Set the precision to the Esize except for bit-packed arrays. */
1765 if (Is_Packed_Array_Impl_Type (gnat_entity)
1766 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1767 esize = UI_To_Int (RM_Size (gnat_entity));
1769 /* This should be an unsigned type if the base type is unsigned or
1770 if the lower bound is constant and non-negative or if the type
1771 is biased. */
1772 if (Is_Unsigned_Type (Etype (gnat_entity))
1773 || Is_Unsigned_Type (gnat_entity)
1774 || Has_Biased_Representation (gnat_entity))
1775 gnu_type = make_unsigned_type (esize);
1776 else
1777 gnu_type = make_signed_type (esize);
1778 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1780 SET_TYPE_RM_MIN_VALUE
1781 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1782 gnat_entity, get_identifier ("L"),
1783 definition, true,
1784 Needs_Debug_Info (gnat_entity)));
1786 SET_TYPE_RM_MAX_VALUE
1787 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1788 gnat_entity, get_identifier ("U"),
1789 definition, true,
1790 Needs_Debug_Info (gnat_entity)));
1792 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1793 = Has_Biased_Representation (gnat_entity);
1795 /* Inherit our alias set from what we're a subtype of. Subtypes
1796 are not different types and a pointer can designate any instance
1797 within a subtype hierarchy. */
1798 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1800 /* One of the above calls might have caused us to be elaborated,
1801 so don't blow up if so. */
1802 if (present_gnu_tree (gnat_entity))
1804 maybe_present = true;
1805 break;
1808 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1809 TYPE_STUB_DECL (gnu_type)
1810 = create_type_stub_decl (gnu_entity_name, gnu_type);
1812 /* For a packed array, make the original array type a parallel type. */
1813 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1814 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1816 discrete_type:
1818 /* We have to handle clauses that under-align the type specially. */
1819 if ((Present (Alignment_Clause (gnat_entity))
1820 || (Is_Packed_Array_Impl_Type (gnat_entity)
1821 && Present
1822 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1823 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1825 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1826 if (align >= TYPE_ALIGN (gnu_type))
1827 align = 0;
1830 /* If the type we are dealing with represents a bit-packed array,
1831 we need to have the bits left justified on big-endian targets
1832 and right justified on little-endian targets. We also need to
1833 ensure that when the value is read (e.g. for comparison of two
1834 such values), we only get the good bits, since the unused bits
1835 are uninitialized. Both goals are accomplished by wrapping up
1836 the modular type in an enclosing record type. */
1837 if (Is_Packed_Array_Impl_Type (gnat_entity)
1838 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1840 tree gnu_field_type, gnu_field;
1842 /* Set the RM size before wrapping up the original type. */
1843 SET_TYPE_RM_SIZE (gnu_type,
1844 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1845 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1847 /* Create a stripped-down declaration, mainly for debugging. */
1848 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1849 gnat_entity);
1851 /* Now save it and build the enclosing record type. */
1852 gnu_field_type = gnu_type;
1854 gnu_type = make_node (RECORD_TYPE);
1855 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1856 TYPE_PACKED (gnu_type) = 1;
1857 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1858 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1859 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1861 /* Propagate the alignment of the modular type to the record type,
1862 unless there is an alignment clause that under-aligns the type.
1863 This means that bit-packed arrays are given "ceil" alignment for
1864 their size by default, which may seem counter-intuitive but makes
1865 it possible to overlay them on modular types easily. */
1866 TYPE_ALIGN (gnu_type)
1867 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1869 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1871 /* Don't declare the field as addressable since we won't be taking
1872 its address and this would prevent create_field_decl from making
1873 a bitfield. */
1874 gnu_field
1875 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1876 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1878 /* Do not emit debug info until after the parallel type is added. */
1879 finish_record_type (gnu_type, gnu_field, 2, false);
1880 compute_record_mode (gnu_type);
1881 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1883 if (debug_info_p)
1885 /* Make the original array type a parallel type. */
1886 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
1888 rest_of_record_type_compilation (gnu_type);
1892 /* If the type we are dealing with has got a smaller alignment than the
1893 natural one, we need to wrap it up in a record type and misalign the
1894 latter; we reuse the padding machinery for this purpose. Note that,
1895 even if the record type is marked as packed because of misalignment,
1896 we don't pack the field so as to give it the size of the type. */
1897 else if (align > 0)
1899 tree gnu_field_type, gnu_field;
1901 /* Set the RM size before wrapping up the type. */
1902 SET_TYPE_RM_SIZE (gnu_type,
1903 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1905 /* Create a stripped-down declaration, mainly for debugging. */
1906 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1907 gnat_entity);
1909 /* Now save it and build the enclosing record type. */
1910 gnu_field_type = gnu_type;
1912 gnu_type = make_node (RECORD_TYPE);
1913 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1914 TYPE_PACKED (gnu_type) = 1;
1915 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1916 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1917 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1918 TYPE_ALIGN (gnu_type) = align;
1919 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1921 /* Don't declare the field as addressable since we won't be taking
1922 its address and this would prevent create_field_decl from making
1923 a bitfield. */
1924 gnu_field
1925 = create_field_decl (get_identifier ("F"), gnu_field_type,
1926 gnu_type, TYPE_SIZE (gnu_field_type),
1927 bitsize_zero_node, 0, 0);
1929 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1930 compute_record_mode (gnu_type);
1931 TYPE_PADDING_P (gnu_type) = 1;
1934 break;
1936 case E_Floating_Point_Type:
1937 /* The type of the Low and High bounds can be our type if this is
1938 a type from Standard, so set them at the end of the function. */
1939 gnu_type = make_node (REAL_TYPE);
1940 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1941 layout_type (gnu_type);
1942 break;
1944 case E_Floating_Point_Subtype:
1945 /* See the E_Signed_Integer_Subtype case for the rationale. */
1946 if (!definition
1947 && Present (Ancestor_Subtype (gnat_entity))
1948 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1949 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1950 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1951 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1953 gnu_type = make_node (REAL_TYPE);
1954 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1955 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1956 TYPE_GCC_MIN_VALUE (gnu_type)
1957 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1958 TYPE_GCC_MAX_VALUE (gnu_type)
1959 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1960 layout_type (gnu_type);
1962 SET_TYPE_RM_MIN_VALUE
1963 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1964 gnat_entity, get_identifier ("L"),
1965 definition, true,
1966 Needs_Debug_Info (gnat_entity)));
1968 SET_TYPE_RM_MAX_VALUE
1969 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1970 gnat_entity, get_identifier ("U"),
1971 definition, true,
1972 Needs_Debug_Info (gnat_entity)));
1974 /* Inherit our alias set from what we're a subtype of, as for
1975 integer subtypes. */
1976 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1978 /* One of the above calls might have caused us to be elaborated,
1979 so don't blow up if so. */
1980 maybe_present = true;
1981 break;
1983 /* Array Types and Subtypes
1985 Unconstrained array types are represented by E_Array_Type and
1986 constrained array types are represented by E_Array_Subtype. There
1987 are no actual objects of an unconstrained array type; all we have
1988 are pointers to that type.
1990 The following fields are defined on array types and subtypes:
1992 Component_Type Component type of the array.
1993 Number_Dimensions Number of dimensions (an int).
1994 First_Index Type of first index. */
1996 case E_Array_Type:
1998 const bool convention_fortran_p
1999 = (Convention (gnat_entity) == Convention_Fortran);
2000 const int ndim = Number_Dimensions (gnat_entity);
2001 tree gnu_template_type;
2002 tree gnu_ptr_template;
2003 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2004 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2005 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2006 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2007 Entity_Id gnat_index, gnat_name;
2008 int index;
2009 tree comp_type;
2011 /* Create the type for the component now, as it simplifies breaking
2012 type reference loops. */
2013 comp_type
2014 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2015 if (present_gnu_tree (gnat_entity))
2017 /* As a side effect, the type may have been translated. */
2018 maybe_present = true;
2019 break;
2022 /* We complete an existing dummy fat pointer type in place. This both
2023 avoids further complex adjustments in update_pointer_to and yields
2024 better debugging information in DWARF by leveraging the support for
2025 incomplete declarations of "tagged" types in the DWARF back-end. */
2026 gnu_type = get_dummy_type (gnat_entity);
2027 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2029 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2030 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2031 /* Save the contents of the dummy type for update_pointer_to. */
2032 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2033 gnu_ptr_template =
2034 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2035 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2037 else
2039 gnu_fat_type = make_node (RECORD_TYPE);
2040 gnu_template_type = make_node (RECORD_TYPE);
2041 gnu_ptr_template = build_pointer_type (gnu_template_type);
2044 /* Make a node for the array. If we are not defining the array
2045 suppress expanding incomplete types. */
2046 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2048 if (!definition)
2050 defer_incomplete_level++;
2051 this_deferred = true;
2054 /* Build the fat pointer type. Use a "void *" object instead of
2055 a pointer to the array type since we don't have the array type
2056 yet (it will reference the fat pointer via the bounds). */
2058 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2059 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2060 DECL_CHAIN (tem)
2061 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2062 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2064 if (COMPLETE_TYPE_P (gnu_fat_type))
2066 /* We are going to lay it out again so reset the alias set. */
2067 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2068 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2069 finish_fat_pointer_type (gnu_fat_type, tem);
2070 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2071 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2073 TYPE_FIELDS (t) = tem;
2074 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2077 else
2079 finish_fat_pointer_type (gnu_fat_type, tem);
2080 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2083 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2084 is the fat pointer. This will be used to access the individual
2085 fields once we build them. */
2086 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2087 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2088 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2089 gnu_template_reference
2090 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2091 TREE_READONLY (gnu_template_reference) = 1;
2092 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2094 /* Now create the GCC type for each index and add the fields for that
2095 index to the template. */
2096 for (index = (convention_fortran_p ? ndim - 1 : 0),
2097 gnat_index = First_Index (gnat_entity);
2098 0 <= index && index < ndim;
2099 index += (convention_fortran_p ? - 1 : 1),
2100 gnat_index = Next_Index (gnat_index))
2102 char field_name[16];
2103 tree gnu_index_base_type
2104 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2105 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2106 tree gnu_min, gnu_max, gnu_high;
2108 /* Make the FIELD_DECLs for the low and high bounds of this
2109 type and then make extractions of these fields from the
2110 template. */
2111 sprintf (field_name, "LB%d", index);
2112 gnu_lb_field = create_field_decl (get_identifier (field_name),
2113 gnu_index_base_type,
2114 gnu_template_type, NULL_TREE,
2115 NULL_TREE, 0, 0);
2116 Sloc_to_locus (Sloc (gnat_entity),
2117 &DECL_SOURCE_LOCATION (gnu_lb_field));
2119 field_name[0] = 'U';
2120 gnu_hb_field = create_field_decl (get_identifier (field_name),
2121 gnu_index_base_type,
2122 gnu_template_type, NULL_TREE,
2123 NULL_TREE, 0, 0);
2124 Sloc_to_locus (Sloc (gnat_entity),
2125 &DECL_SOURCE_LOCATION (gnu_hb_field));
2127 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2129 /* We can't use build_component_ref here since the template type
2130 isn't complete yet. */
2131 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2132 gnu_template_reference, gnu_lb_field,
2133 NULL_TREE);
2134 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2135 gnu_template_reference, gnu_hb_field,
2136 NULL_TREE);
2137 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2139 gnu_min = convert (sizetype, gnu_orig_min);
2140 gnu_max = convert (sizetype, gnu_orig_max);
2142 /* Compute the size of this dimension. See the E_Array_Subtype
2143 case below for the rationale. */
2144 gnu_high
2145 = build3 (COND_EXPR, sizetype,
2146 build2 (GE_EXPR, boolean_type_node,
2147 gnu_orig_max, gnu_orig_min),
2148 gnu_max,
2149 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2151 /* Make a range type with the new range in the Ada base type.
2152 Then make an index type with the size range in sizetype. */
2153 gnu_index_types[index]
2154 = create_index_type (gnu_min, gnu_high,
2155 create_range_type (gnu_index_base_type,
2156 gnu_orig_min,
2157 gnu_orig_max),
2158 gnat_entity);
2160 /* Update the maximum size of the array in elements. */
2161 if (gnu_max_size)
2163 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2164 tree gnu_min
2165 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2166 tree gnu_max
2167 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2168 tree gnu_this_max
2169 = size_binop (PLUS_EXPR, size_one_node,
2170 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2172 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2173 && TREE_OVERFLOW (gnu_this_max))
2174 gnu_max_size = NULL_TREE;
2175 else
2176 gnu_max_size
2177 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2180 TYPE_NAME (gnu_index_types[index])
2181 = create_concat_name (gnat_entity, field_name);
2184 /* Install all the fields into the template. */
2185 TYPE_NAME (gnu_template_type)
2186 = create_concat_name (gnat_entity, "XUB");
2187 gnu_template_fields = NULL_TREE;
2188 for (index = 0; index < ndim; index++)
2189 gnu_template_fields
2190 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2191 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2192 debug_info_p);
2193 TYPE_READONLY (gnu_template_type) = 1;
2195 /* If Component_Size is not already specified, annotate it with the
2196 size of the component. */
2197 if (Unknown_Component_Size (gnat_entity))
2198 Set_Component_Size (gnat_entity,
2199 annotate_value (TYPE_SIZE (comp_type)));
2201 /* Compute the maximum size of the array in units and bits. */
2202 if (gnu_max_size)
2204 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2205 TYPE_SIZE_UNIT (comp_type));
2206 gnu_max_size = size_binop (MULT_EXPR,
2207 convert (bitsizetype, gnu_max_size),
2208 TYPE_SIZE (comp_type));
2210 else
2211 gnu_max_size_unit = NULL_TREE;
2213 /* Now build the array type. */
2214 tem = comp_type;
2215 for (index = ndim - 1; index >= 0; index--)
2217 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2218 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2219 sorry ("non-default Scalar_Storage_Order");
2220 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2221 if (array_type_has_nonaliased_component (tem, gnat_entity))
2222 TYPE_NONALIASED_COMPONENT (tem) = 1;
2224 /* If it is passed by reference, force BLKmode to ensure that
2225 objects of this type will always be put in memory. */
2226 if (TYPE_MODE (tem) != BLKmode
2227 && Is_By_Reference_Type (gnat_entity))
2228 SET_TYPE_MODE (tem, BLKmode);
2231 TYPE_VOLATILE (tem) = Treat_As_Volatile (gnat_entity);
2233 /* If an alignment is specified, use it if valid. But ignore it
2234 for the original type of packed array types. If the alignment
2235 was requested with an explicit alignment clause, state so. */
2236 if (No (Packed_Array_Impl_Type (gnat_entity))
2237 && Known_Alignment (gnat_entity))
2239 TYPE_ALIGN (tem)
2240 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2241 TYPE_ALIGN (tem));
2242 if (Present (Alignment_Clause (gnat_entity)))
2243 TYPE_USER_ALIGN (tem) = 1;
2246 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2248 /* Adjust the type of the pointer-to-array field of the fat pointer
2249 and record the aliasing relationships if necessary. */
2250 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2251 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2252 record_component_aliases (gnu_fat_type);
2254 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2255 corresponding fat pointer. */
2256 TREE_TYPE (gnu_type) = gnu_fat_type;
2257 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2258 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2259 SET_TYPE_MODE (gnu_type, BLKmode);
2260 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2262 /* If the maximum size doesn't overflow, use it. */
2263 if (gnu_max_size
2264 && TREE_CODE (gnu_max_size) == INTEGER_CST
2265 && !TREE_OVERFLOW (gnu_max_size)
2266 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2267 && !TREE_OVERFLOW (gnu_max_size_unit))
2269 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2270 TYPE_SIZE (tem));
2271 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2272 TYPE_SIZE_UNIT (tem));
2275 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2276 !Comes_From_Source (gnat_entity), debug_info_p,
2277 gnat_entity);
2279 /* Give the fat pointer type a name. If this is a packed array, tell
2280 the debugger how to interpret the underlying bits. */
2281 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2282 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2283 else
2284 gnat_name = gnat_entity;
2285 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2286 !Comes_From_Source (gnat_entity), debug_info_p,
2287 gnat_entity);
2289 /* Create the type to be designated by thin pointers: a record type for
2290 the array and its template. We used to shift the fields to have the
2291 template at a negative offset, but this was somewhat of a kludge; we
2292 now shift thin pointer values explicitly but only those which have a
2293 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2294 tem = build_unc_object_type (gnu_template_type, tem,
2295 create_concat_name (gnat_name, "XUT"),
2296 debug_info_p);
2298 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2299 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2301 break;
2303 case E_Array_Subtype:
2305 /* This is the actual data type for array variables. Multidimensional
2306 arrays are implemented as arrays of arrays. Note that arrays which
2307 have sparse enumeration subtypes as index components create sparse
2308 arrays, which is obviously space inefficient but so much easier to
2309 code for now.
2311 Also note that the subtype never refers to the unconstrained array
2312 type, which is somewhat at variance with Ada semantics.
2314 First check to see if this is simply a renaming of the array type.
2315 If so, the result is the array type. */
2317 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2318 if (!Is_Constrained (gnat_entity))
2320 else
2322 Entity_Id gnat_index, gnat_base_index;
2323 const bool convention_fortran_p
2324 = (Convention (gnat_entity) == Convention_Fortran);
2325 const int ndim = Number_Dimensions (gnat_entity);
2326 tree gnu_base_type = gnu_type;
2327 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2328 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2329 bool need_index_type_struct = false;
2330 int index;
2332 /* First create the GCC type for each index and find out whether
2333 special types are needed for debugging information. */
2334 for (index = (convention_fortran_p ? ndim - 1 : 0),
2335 gnat_index = First_Index (gnat_entity),
2336 gnat_base_index
2337 = First_Index (Implementation_Base_Type (gnat_entity));
2338 0 <= index && index < ndim;
2339 index += (convention_fortran_p ? - 1 : 1),
2340 gnat_index = Next_Index (gnat_index),
2341 gnat_base_index = Next_Index (gnat_base_index))
2343 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2344 tree gnu_index_base_type = get_base_type (gnu_index_type);
2345 tree gnu_orig_min
2346 = convert (gnu_index_base_type,
2347 TYPE_MIN_VALUE (gnu_index_type));
2348 tree gnu_orig_max
2349 = convert (gnu_index_base_type,
2350 TYPE_MAX_VALUE (gnu_index_type));
2351 tree gnu_min = convert (sizetype, gnu_orig_min);
2352 tree gnu_max = convert (sizetype, gnu_orig_max);
2353 tree gnu_base_index_type
2354 = get_unpadded_type (Etype (gnat_base_index));
2355 tree gnu_base_index_base_type
2356 = get_base_type (gnu_base_index_type);
2357 tree gnu_base_orig_min
2358 = convert (gnu_base_index_base_type,
2359 TYPE_MIN_VALUE (gnu_base_index_type));
2360 tree gnu_base_orig_max
2361 = convert (gnu_base_index_base_type,
2362 TYPE_MAX_VALUE (gnu_base_index_type));
2363 tree gnu_high;
2365 /* See if the base array type is already flat. If it is, we
2366 are probably compiling an ACATS test but it will cause the
2367 code below to malfunction if we don't handle it specially. */
2368 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2369 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2370 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2372 gnu_min = size_one_node;
2373 gnu_max = size_zero_node;
2374 gnu_high = gnu_max;
2377 /* Similarly, if one of the values overflows in sizetype and the
2378 range is null, use 1..0 for the sizetype bounds. */
2379 else if (TREE_CODE (gnu_min) == INTEGER_CST
2380 && TREE_CODE (gnu_max) == INTEGER_CST
2381 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2382 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2384 gnu_min = size_one_node;
2385 gnu_max = size_zero_node;
2386 gnu_high = gnu_max;
2389 /* If the minimum and maximum values both overflow in sizetype,
2390 but the difference in the original type does not overflow in
2391 sizetype, ignore the overflow indication. */
2392 else if (TREE_CODE (gnu_min) == INTEGER_CST
2393 && TREE_CODE (gnu_max) == INTEGER_CST
2394 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2395 && !TREE_OVERFLOW
2396 (convert (sizetype,
2397 fold_build2 (MINUS_EXPR, gnu_index_type,
2398 gnu_orig_max,
2399 gnu_orig_min))))
2401 TREE_OVERFLOW (gnu_min) = 0;
2402 TREE_OVERFLOW (gnu_max) = 0;
2403 gnu_high = gnu_max;
2406 /* Compute the size of this dimension in the general case. We
2407 need to provide GCC with an upper bound to use but have to
2408 deal with the "superflat" case. There are three ways to do
2409 this. If we can prove that the array can never be superflat,
2410 we can just use the high bound of the index type. */
2411 else if ((Nkind (gnat_index) == N_Range
2412 && cannot_be_superflat_p (gnat_index))
2413 /* Bit-Packed Array Impl. Types are never superflat. */
2414 || (Is_Packed_Array_Impl_Type (gnat_entity)
2415 && Is_Bit_Packed_Array
2416 (Original_Array_Type (gnat_entity))))
2417 gnu_high = gnu_max;
2419 /* Otherwise, if the high bound is constant but the low bound is
2420 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2421 lower bound. Note that the comparison must be done in the
2422 original type to avoid any overflow during the conversion. */
2423 else if (TREE_CODE (gnu_max) == INTEGER_CST
2424 && TREE_CODE (gnu_min) != INTEGER_CST)
2426 gnu_high = gnu_max;
2427 gnu_min
2428 = build_cond_expr (sizetype,
2429 build_binary_op (GE_EXPR,
2430 boolean_type_node,
2431 gnu_orig_max,
2432 gnu_orig_min),
2433 gnu_min,
2434 int_const_binop (PLUS_EXPR, gnu_max,
2435 size_one_node));
2438 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2439 in all the other cases. Note that, here as well as above,
2440 the condition used in the comparison must be equivalent to
2441 the condition (length != 0). This is relied upon in order
2442 to optimize array comparisons in compare_arrays. Moreover
2443 we use int_const_binop for the shift by 1 if the bound is
2444 constant to avoid any unwanted overflow. */
2445 else
2446 gnu_high
2447 = build_cond_expr (sizetype,
2448 build_binary_op (GE_EXPR,
2449 boolean_type_node,
2450 gnu_orig_max,
2451 gnu_orig_min),
2452 gnu_max,
2453 TREE_CODE (gnu_min) == INTEGER_CST
2454 ? int_const_binop (MINUS_EXPR, gnu_min,
2455 size_one_node)
2456 : size_binop (MINUS_EXPR, gnu_min,
2457 size_one_node));
2459 /* Reuse the index type for the range type. Then make an index
2460 type with the size range in sizetype. */
2461 gnu_index_types[index]
2462 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2463 gnat_entity);
2465 /* Update the maximum size of the array in elements. Here we
2466 see if any constraint on the index type of the base type
2467 can be used in the case of self-referential bound on the
2468 index type of the subtype. We look for a non-"infinite"
2469 and non-self-referential bound from any type involved and
2470 handle each bound separately. */
2471 if (gnu_max_size)
2473 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2474 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2475 tree gnu_base_index_base_type
2476 = get_base_type (gnu_base_index_type);
2477 tree gnu_base_base_min
2478 = convert (sizetype,
2479 TYPE_MIN_VALUE (gnu_base_index_base_type));
2480 tree gnu_base_base_max
2481 = convert (sizetype,
2482 TYPE_MAX_VALUE (gnu_base_index_base_type));
2484 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2485 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2486 && !TREE_OVERFLOW (gnu_base_min)))
2487 gnu_base_min = gnu_min;
2489 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2490 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2491 && !TREE_OVERFLOW (gnu_base_max)))
2492 gnu_base_max = gnu_max;
2494 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2495 && TREE_OVERFLOW (gnu_base_min))
2496 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2497 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2498 && TREE_OVERFLOW (gnu_base_max))
2499 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2500 gnu_max_size = NULL_TREE;
2501 else
2503 tree gnu_this_max;
2505 /* Use int_const_binop if the bounds are constant to
2506 avoid any unwanted overflow. */
2507 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2508 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2509 gnu_this_max
2510 = int_const_binop (PLUS_EXPR, size_one_node,
2511 int_const_binop (MINUS_EXPR,
2512 gnu_base_max,
2513 gnu_base_min));
2514 else
2515 gnu_this_max
2516 = size_binop (PLUS_EXPR, size_one_node,
2517 size_binop (MINUS_EXPR,
2518 gnu_base_max,
2519 gnu_base_min));
2521 gnu_max_size
2522 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2526 /* We need special types for debugging information to point to
2527 the index types if they have variable bounds, are not integer
2528 types or are biased. */
2529 if (TREE_CODE (gnu_orig_min) != INTEGER_CST
2530 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2531 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2532 || (TREE_TYPE (gnu_index_type)
2533 && TREE_CODE (TREE_TYPE (gnu_index_type))
2534 != INTEGER_TYPE)
2535 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2536 need_index_type_struct = true;
2539 /* Then flatten: create the array of arrays. For an array type
2540 used to implement a packed array, get the component type from
2541 the original array type since the representation clauses that
2542 can affect it are on the latter. */
2543 if (Is_Packed_Array_Impl_Type (gnat_entity)
2544 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2546 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2547 for (index = ndim - 1; index >= 0; index--)
2548 gnu_type = TREE_TYPE (gnu_type);
2550 /* One of the above calls might have caused us to be elaborated,
2551 so don't blow up if so. */
2552 if (present_gnu_tree (gnat_entity))
2554 maybe_present = true;
2555 break;
2558 else
2560 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2561 debug_info_p);
2563 /* One of the above calls might have caused us to be elaborated,
2564 so don't blow up if so. */
2565 if (present_gnu_tree (gnat_entity))
2567 maybe_present = true;
2568 break;
2572 /* Compute the maximum size of the array in units and bits. */
2573 if (gnu_max_size)
2575 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2576 TYPE_SIZE_UNIT (gnu_type));
2577 gnu_max_size = size_binop (MULT_EXPR,
2578 convert (bitsizetype, gnu_max_size),
2579 TYPE_SIZE (gnu_type));
2581 else
2582 gnu_max_size_unit = NULL_TREE;
2584 /* Now build the array type. */
2585 for (index = ndim - 1; index >= 0; index --)
2587 gnu_type = build_nonshared_array_type (gnu_type,
2588 gnu_index_types[index]);
2589 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2590 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2591 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2593 /* See the E_Array_Type case for the rationale. */
2594 if (TYPE_MODE (gnu_type) != BLKmode
2595 && Is_By_Reference_Type (gnat_entity))
2596 SET_TYPE_MODE (gnu_type, BLKmode);
2599 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2601 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2602 TYPE_STUB_DECL (gnu_type)
2603 = create_type_stub_decl (gnu_entity_name, gnu_type);
2605 /* If we are at file level and this is a multi-dimensional array,
2606 we need to make a variable corresponding to the stride of the
2607 inner dimensions. */
2608 if (global_bindings_p () && ndim > 1)
2610 tree gnu_st_name = get_identifier ("ST");
2611 tree gnu_arr_type;
2613 for (gnu_arr_type = TREE_TYPE (gnu_type);
2614 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2615 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2616 gnu_st_name = concat_name (gnu_st_name, "ST"))
2618 tree eltype = TREE_TYPE (gnu_arr_type);
2620 TYPE_SIZE (gnu_arr_type)
2621 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2622 gnat_entity, gnu_st_name,
2623 definition, false);
2625 /* ??? For now, store the size as a multiple of the
2626 alignment of the element type in bytes so that we
2627 can see the alignment from the tree. */
2628 TYPE_SIZE_UNIT (gnu_arr_type)
2629 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2630 gnat_entity,
2631 concat_name (gnu_st_name, "A_U"),
2632 definition, false,
2633 TYPE_ALIGN (eltype));
2635 /* ??? create_type_decl is not invoked on the inner types so
2636 the MULT_EXPR node built above will never be marked. */
2637 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2641 /* If we need to write out a record type giving the names of the
2642 bounds for debugging purposes, do it now and make the record
2643 type a parallel type. This is not needed for a packed array
2644 since the bounds are conveyed by the original array type. */
2645 if (need_index_type_struct
2646 && debug_info_p
2647 && !Is_Packed_Array_Impl_Type (gnat_entity))
2649 tree gnu_bound_rec = make_node (RECORD_TYPE);
2650 tree gnu_field_list = NULL_TREE;
2651 tree gnu_field;
2653 TYPE_NAME (gnu_bound_rec)
2654 = create_concat_name (gnat_entity, "XA");
2656 for (index = ndim - 1; index >= 0; index--)
2658 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2659 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2661 /* Make sure to reference the types themselves, and not just
2662 their names, as the debugger may fall back on them. */
2663 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2664 gnu_bound_rec, NULL_TREE,
2665 NULL_TREE, 0, 0);
2666 DECL_CHAIN (gnu_field) = gnu_field_list;
2667 gnu_field_list = gnu_field;
2670 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2671 add_parallel_type (gnu_type, gnu_bound_rec);
2674 /* If this is a packed array type, make the original array type a
2675 parallel type. Otherwise, do it for the base array type if it
2676 isn't artificial to make sure it is kept in the debug info. */
2677 if (debug_info_p)
2679 if (Is_Packed_Array_Impl_Type (gnat_entity))
2680 add_parallel_type_for_packed_array (gnu_type, gnat_entity);
2681 else
2683 tree gnu_base_decl
2684 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2685 if (!DECL_ARTIFICIAL (gnu_base_decl))
2686 add_parallel_type (gnu_type,
2687 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2691 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2692 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2693 = (Is_Packed_Array_Impl_Type (gnat_entity)
2694 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2696 /* If the size is self-referential and the maximum size doesn't
2697 overflow, use it. */
2698 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2699 && gnu_max_size
2700 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2701 && TREE_OVERFLOW (gnu_max_size))
2702 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2703 && TREE_OVERFLOW (gnu_max_size_unit)))
2705 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2706 TYPE_SIZE (gnu_type));
2707 TYPE_SIZE_UNIT (gnu_type)
2708 = size_binop (MIN_EXPR, gnu_max_size_unit,
2709 TYPE_SIZE_UNIT (gnu_type));
2712 /* Set our alias set to that of our base type. This gives all
2713 array subtypes the same alias set. */
2714 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2716 /* If this is a packed type, make this type the same as the packed
2717 array type, but do some adjusting in the type first. */
2718 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2720 Entity_Id gnat_index;
2721 tree gnu_inner;
2723 /* First finish the type we had been making so that we output
2724 debugging information for it. */
2725 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2726 if (Treat_As_Volatile (gnat_entity))
2727 gnu_type
2728 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
2729 /* Make it artificial only if the base type was artificial too.
2730 That's sort of "morally" true and will make it possible for
2731 the debugger to look it up by name in DWARF, which is needed
2732 in order to decode the packed array type. */
2733 gnu_decl
2734 = create_type_decl (gnu_entity_name, gnu_type,
2735 !Comes_From_Source (Etype (gnat_entity))
2736 && !Comes_From_Source (gnat_entity),
2737 debug_info_p, gnat_entity);
2739 /* Save it as our equivalent in case the call below elaborates
2740 this type again. */
2741 save_gnu_tree (gnat_entity, gnu_decl, false);
2743 gnu_decl
2744 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2745 NULL_TREE, 0);
2746 this_made_decl = true;
2747 gnu_type = TREE_TYPE (gnu_decl);
2748 save_gnu_tree (gnat_entity, NULL_TREE, false);
2750 gnu_inner = gnu_type;
2751 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2752 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2753 || TYPE_PADDING_P (gnu_inner)))
2754 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2756 /* We need to attach the index type to the type we just made so
2757 that the actual bounds can later be put into a template. */
2758 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2759 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2760 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2761 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2763 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2765 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2766 TYPE_MODULUS for modular types so we make an extra
2767 subtype if necessary. */
2768 if (TYPE_MODULAR_P (gnu_inner))
2770 tree gnu_subtype
2771 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2772 TREE_TYPE (gnu_subtype) = gnu_inner;
2773 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2774 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2775 TYPE_MIN_VALUE (gnu_inner));
2776 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2777 TYPE_MAX_VALUE (gnu_inner));
2778 gnu_inner = gnu_subtype;
2781 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2783 #ifdef ENABLE_CHECKING
2784 /* Check for other cases of overloading. */
2785 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2786 #endif
2789 for (gnat_index = First_Index (gnat_entity);
2790 Present (gnat_index);
2791 gnat_index = Next_Index (gnat_index))
2792 SET_TYPE_ACTUAL_BOUNDS
2793 (gnu_inner,
2794 tree_cons (NULL_TREE,
2795 get_unpadded_type (Etype (gnat_index)),
2796 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2798 if (Convention (gnat_entity) != Convention_Fortran)
2799 SET_TYPE_ACTUAL_BOUNDS
2800 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2802 if (TREE_CODE (gnu_type) == RECORD_TYPE
2803 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2804 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2808 else
2809 /* Abort if packed array with no Packed_Array_Impl_Type. */
2810 gcc_assert (!Is_Packed (gnat_entity));
2812 break;
2814 case E_String_Literal_Subtype:
2815 /* Create the type for a string literal. */
2817 Entity_Id gnat_full_type
2818 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2819 && Present (Full_View (Etype (gnat_entity)))
2820 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2821 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2822 tree gnu_string_array_type
2823 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2824 tree gnu_string_index_type
2825 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2826 (TYPE_DOMAIN (gnu_string_array_type))));
2827 tree gnu_lower_bound
2828 = convert (gnu_string_index_type,
2829 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2830 tree gnu_length
2831 = UI_To_gnu (String_Literal_Length (gnat_entity),
2832 gnu_string_index_type);
2833 tree gnu_upper_bound
2834 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2835 gnu_lower_bound,
2836 int_const_binop (MINUS_EXPR, gnu_length,
2837 convert (gnu_string_index_type,
2838 integer_one_node)));
2839 tree gnu_index_type
2840 = create_index_type (convert (sizetype, gnu_lower_bound),
2841 convert (sizetype, gnu_upper_bound),
2842 create_range_type (gnu_string_index_type,
2843 gnu_lower_bound,
2844 gnu_upper_bound),
2845 gnat_entity);
2847 gnu_type
2848 = build_nonshared_array_type (gnat_to_gnu_type
2849 (Component_Type (gnat_entity)),
2850 gnu_index_type);
2851 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2852 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2853 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2855 break;
2857 /* Record Types and Subtypes
2859 The following fields are defined on record types:
2861 Has_Discriminants True if the record has discriminants
2862 First_Discriminant Points to head of list of discriminants
2863 First_Entity Points to head of list of fields
2864 Is_Tagged_Type True if the record is tagged
2866 Implementation of Ada records and discriminated records:
2868 A record type definition is transformed into the equivalent of a C
2869 struct definition. The fields that are the discriminants which are
2870 found in the Full_Type_Declaration node and the elements of the
2871 Component_List found in the Record_Type_Definition node. The
2872 Component_List can be a recursive structure since each Variant of
2873 the Variant_Part of the Component_List has a Component_List.
2875 Processing of a record type definition comprises starting the list of
2876 field declarations here from the discriminants and the calling the
2877 function components_to_record to add the rest of the fields from the
2878 component list and return the gnu type node. The function
2879 components_to_record will call itself recursively as it traverses
2880 the tree. */
2882 case E_Record_Type:
2883 if (Has_Complex_Representation (gnat_entity))
2885 gnu_type
2886 = build_complex_type
2887 (get_unpadded_type
2888 (Etype (Defining_Entity
2889 (First (Component_Items
2890 (Component_List
2891 (Type_Definition
2892 (Declaration_Node (gnat_entity)))))))));
2894 break;
2898 Node_Id full_definition = Declaration_Node (gnat_entity);
2899 Node_Id record_definition = Type_Definition (full_definition);
2900 Node_Id gnat_constr;
2901 Entity_Id gnat_field;
2902 tree gnu_field, gnu_field_list = NULL_TREE;
2903 tree gnu_get_parent;
2904 /* Set PACKED in keeping with gnat_to_gnu_field. */
2905 const int packed
2906 = Is_Packed (gnat_entity)
2908 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2909 ? -1
2910 : (Known_Alignment (gnat_entity)
2911 || (Strict_Alignment (gnat_entity)
2912 && Known_RM_Size (gnat_entity)))
2913 ? -2
2914 : 0;
2915 const bool has_discr = Has_Discriminants (gnat_entity);
2916 const bool has_rep = Has_Specified_Layout (gnat_entity);
2917 const bool is_extension
2918 = (Is_Tagged_Type (gnat_entity)
2919 && Nkind (record_definition) == N_Derived_Type_Definition);
2920 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2921 bool all_rep = has_rep;
2923 /* See if all fields have a rep clause. Stop when we find one
2924 that doesn't. */
2925 if (all_rep)
2926 for (gnat_field = First_Entity (gnat_entity);
2927 Present (gnat_field);
2928 gnat_field = Next_Entity (gnat_field))
2929 if ((Ekind (gnat_field) == E_Component
2930 || Ekind (gnat_field) == E_Discriminant)
2931 && No (Component_Clause (gnat_field)))
2933 all_rep = false;
2934 break;
2937 /* If this is a record extension, go a level further to find the
2938 record definition. Also, verify we have a Parent_Subtype. */
2939 if (is_extension)
2941 if (!type_annotate_only
2942 || Present (Record_Extension_Part (record_definition)))
2943 record_definition = Record_Extension_Part (record_definition);
2945 gcc_assert (type_annotate_only
2946 || Present (Parent_Subtype (gnat_entity)));
2949 /* Make a node for the record. If we are not defining the record,
2950 suppress expanding incomplete types. */
2951 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2952 TYPE_NAME (gnu_type) = gnu_entity_name;
2953 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2954 if (Reverse_Storage_Order (gnat_entity) && !GNAT_Mode)
2955 sorry ("non-default Scalar_Storage_Order");
2956 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2958 if (!definition)
2960 defer_incomplete_level++;
2961 this_deferred = true;
2964 /* If both a size and rep clause was specified, put the size in
2965 the record type now so that it can get the proper mode. */
2966 if (has_rep && Known_RM_Size (gnat_entity))
2967 TYPE_SIZE (gnu_type)
2968 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2970 /* Always set the alignment here so that it can be used to
2971 set the mode, if it is making the alignment stricter. If
2972 it is invalid, it will be checked again below. If this is to
2973 be Atomic, choose a default alignment of a word unless we know
2974 the size and it's smaller. */
2975 if (Known_Alignment (gnat_entity))
2976 TYPE_ALIGN (gnu_type)
2977 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2978 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2980 unsigned int size = UI_To_Int (Esize (gnat_entity));
2981 TYPE_ALIGN (gnu_type)
2982 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2984 /* If a type needs strict alignment, the minimum size will be the
2985 type size instead of the RM size (see validate_size). Cap the
2986 alignment, lest it causes this type size to become too large. */
2987 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2989 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2990 unsigned int raw_align = raw_size & -raw_size;
2991 if (raw_align < BIGGEST_ALIGNMENT)
2992 TYPE_ALIGN (gnu_type) = raw_align;
2994 else
2995 TYPE_ALIGN (gnu_type) = 0;
2997 /* If we have a Parent_Subtype, make a field for the parent. If
2998 this record has rep clauses, force the position to zero. */
2999 if (Present (Parent_Subtype (gnat_entity)))
3001 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3002 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3003 tree gnu_parent;
3005 /* A major complexity here is that the parent subtype will
3006 reference our discriminants in its Stored_Constraint list.
3007 But those must reference the parent component of this record
3008 which is precisely of the parent subtype we have not built yet!
3009 To break the circle we first build a dummy COMPONENT_REF which
3010 represents the "get to the parent" operation and initialize
3011 each of those discriminants to a COMPONENT_REF of the above
3012 dummy parent referencing the corresponding discriminant of the
3013 base type of the parent subtype. */
3014 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3015 build0 (PLACEHOLDER_EXPR, gnu_type),
3016 build_decl (input_location,
3017 FIELD_DECL, NULL_TREE,
3018 gnu_dummy_parent_type),
3019 NULL_TREE);
3021 if (has_discr)
3022 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3023 Present (gnat_field);
3024 gnat_field = Next_Stored_Discriminant (gnat_field))
3025 if (Present (Corresponding_Discriminant (gnat_field)))
3027 tree gnu_field
3028 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3029 (gnat_field));
3030 save_gnu_tree
3031 (gnat_field,
3032 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3033 gnu_get_parent, gnu_field, NULL_TREE),
3034 true);
3037 /* Then we build the parent subtype. If it has discriminants but
3038 the type itself has unknown discriminants, this means that it
3039 doesn't contain information about how the discriminants are
3040 derived from those of the ancestor type, so it cannot be used
3041 directly. Instead it is built by cloning the parent subtype
3042 of the underlying record view of the type, for which the above
3043 derivation of discriminants has been made explicit. */
3044 if (Has_Discriminants (gnat_parent)
3045 && Has_Unknown_Discriminants (gnat_entity))
3047 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3049 /* If we are defining the type, the underlying record
3050 view must already have been elaborated at this point.
3051 Otherwise do it now as its parent subtype cannot be
3052 technically elaborated on its own. */
3053 if (definition)
3054 gcc_assert (present_gnu_tree (gnat_uview));
3055 else
3056 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3058 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3060 /* Substitute the "get to the parent" of the type for that
3061 of its underlying record view in the cloned type. */
3062 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3063 Present (gnat_field);
3064 gnat_field = Next_Stored_Discriminant (gnat_field))
3065 if (Present (Corresponding_Discriminant (gnat_field)))
3067 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3068 tree gnu_ref
3069 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3070 gnu_get_parent, gnu_field, NULL_TREE);
3071 gnu_parent
3072 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3075 else
3076 gnu_parent = gnat_to_gnu_type (gnat_parent);
3078 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3079 initially built. The discriminants must reference the fields
3080 of the parent subtype and not those of its base type for the
3081 placeholder machinery to properly work. */
3082 if (has_discr)
3084 /* The actual parent subtype is the full view. */
3085 if (IN (Ekind (gnat_parent), Private_Kind))
3087 if (Present (Full_View (gnat_parent)))
3088 gnat_parent = Full_View (gnat_parent);
3089 else
3090 gnat_parent = Underlying_Full_View (gnat_parent);
3093 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3094 Present (gnat_field);
3095 gnat_field = Next_Stored_Discriminant (gnat_field))
3096 if (Present (Corresponding_Discriminant (gnat_field)))
3098 Entity_Id field;
3099 for (field = First_Stored_Discriminant (gnat_parent);
3100 Present (field);
3101 field = Next_Stored_Discriminant (field))
3102 if (same_discriminant_p (gnat_field, field))
3103 break;
3104 gcc_assert (Present (field));
3105 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3106 = gnat_to_gnu_field_decl (field);
3110 /* The "get to the parent" COMPONENT_REF must be given its
3111 proper type... */
3112 TREE_TYPE (gnu_get_parent) = gnu_parent;
3114 /* ...and reference the _Parent field of this record. */
3115 gnu_field
3116 = create_field_decl (parent_name_id,
3117 gnu_parent, gnu_type,
3118 has_rep
3119 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3120 has_rep
3121 ? bitsize_zero_node : NULL_TREE,
3122 0, 1);
3123 DECL_INTERNAL_P (gnu_field) = 1;
3124 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3125 TYPE_FIELDS (gnu_type) = gnu_field;
3128 /* Make the fields for the discriminants and put them into the record
3129 unless it's an Unchecked_Union. */
3130 if (has_discr)
3131 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3132 Present (gnat_field);
3133 gnat_field = Next_Stored_Discriminant (gnat_field))
3135 /* If this is a record extension and this discriminant is the
3136 renaming of another discriminant, we've handled it above. */
3137 if (Present (Parent_Subtype (gnat_entity))
3138 && Present (Corresponding_Discriminant (gnat_field)))
3139 continue;
3141 gnu_field
3142 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3143 debug_info_p);
3145 /* Make an expression using a PLACEHOLDER_EXPR from the
3146 FIELD_DECL node just created and link that with the
3147 corresponding GNAT defining identifier. */
3148 save_gnu_tree (gnat_field,
3149 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3150 build0 (PLACEHOLDER_EXPR, gnu_type),
3151 gnu_field, NULL_TREE),
3152 true);
3154 if (!is_unchecked_union)
3156 DECL_CHAIN (gnu_field) = gnu_field_list;
3157 gnu_field_list = gnu_field;
3161 /* If we have a derived untagged type that renames discriminants in
3162 the root type, the (stored) discriminants are a just copy of the
3163 discriminants of the root type. This means that any constraints
3164 added by the renaming in the derivation are disregarded as far
3165 as the layout of the derived type is concerned. To rescue them,
3166 we change the type of the (stored) discriminants to a subtype
3167 with the bounds of the type of the visible discriminants. */
3168 if (has_discr
3169 && !is_extension
3170 && Stored_Constraint (gnat_entity) != No_Elist)
3171 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3172 gnat_constr != No_Elmt;
3173 gnat_constr = Next_Elmt (gnat_constr))
3174 if (Nkind (Node (gnat_constr)) == N_Identifier
3175 /* Ignore access discriminants. */
3176 && !Is_Access_Type (Etype (Node (gnat_constr)))
3177 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3179 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3180 tree gnu_discr_type, gnu_ref;
3182 /* If the scope of the discriminant is not the record type,
3183 this means that we're processing the implicit full view
3184 of a type derived from a private discriminated type: in
3185 this case, the Stored_Constraint list is simply copied
3186 from the partial view, see Build_Derived_Private_Type.
3187 So we need to retrieve the corresponding discriminant
3188 of the implicit full view, otherwise we will abort. */
3189 if (Scope (gnat_discr) != gnat_entity)
3191 Entity_Id field;
3192 for (field = First_Entity (gnat_entity);
3193 Present (field);
3194 field = Next_Entity (field))
3195 if (Ekind (field) == E_Discriminant
3196 && same_discriminant_p (gnat_discr, field))
3197 break;
3198 gcc_assert (Present (field));
3199 gnat_discr = field;
3202 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3203 gnu_ref
3204 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3205 NULL_TREE, 0);
3207 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3208 just above for one of the stored discriminants. */
3209 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3211 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3213 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3214 tree gnu_subtype
3215 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3216 ? make_unsigned_type (prec) : make_signed_type (prec);
3217 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3218 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3219 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3220 TYPE_MIN_VALUE (gnu_discr_type));
3221 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3222 TYPE_MAX_VALUE (gnu_discr_type));
3223 TREE_TYPE (gnu_ref)
3224 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3228 /* Add the fields into the record type and finish it up. */
3229 components_to_record (gnu_type, Component_List (record_definition),
3230 gnu_field_list, packed, definition, false,
3231 all_rep, is_unchecked_union,
3232 !Comes_From_Source (gnat_entity), debug_info_p,
3233 false, OK_To_Reorder_Components (gnat_entity),
3234 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3236 /* If it is passed by reference, force BLKmode to ensure that objects
3237 of this type will always be put in memory. */
3238 if (TYPE_MODE (gnu_type) != BLKmode
3239 && Is_By_Reference_Type (gnat_entity))
3240 SET_TYPE_MODE (gnu_type, BLKmode);
3242 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3244 /* Fill in locations of fields. */
3245 annotate_rep (gnat_entity, gnu_type);
3247 /* If there are any entities in the chain corresponding to components
3248 that we did not elaborate, ensure we elaborate their types if they
3249 are Itypes. */
3250 for (gnat_temp = First_Entity (gnat_entity);
3251 Present (gnat_temp);
3252 gnat_temp = Next_Entity (gnat_temp))
3253 if ((Ekind (gnat_temp) == E_Component
3254 || Ekind (gnat_temp) == E_Discriminant)
3255 && Is_Itype (Etype (gnat_temp))
3256 && !present_gnu_tree (gnat_temp))
3257 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3259 /* If this is a record type associated with an exception definition,
3260 equate its fields to those of the standard exception type. This
3261 will make it possible to convert between them. */
3262 if (gnu_entity_name == exception_data_name_id)
3264 tree gnu_std_field;
3265 for (gnu_field = TYPE_FIELDS (gnu_type),
3266 gnu_std_field = TYPE_FIELDS (except_type_node);
3267 gnu_field;
3268 gnu_field = DECL_CHAIN (gnu_field),
3269 gnu_std_field = DECL_CHAIN (gnu_std_field))
3270 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3271 gcc_assert (!gnu_std_field);
3274 break;
3276 case E_Class_Wide_Subtype:
3277 /* If an equivalent type is present, that is what we should use.
3278 Otherwise, fall through to handle this like a record subtype
3279 since it may have constraints. */
3280 if (gnat_equiv_type != gnat_entity)
3282 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3283 maybe_present = true;
3284 break;
3287 /* ... fall through ... */
3289 case E_Record_Subtype:
3290 /* If Cloned_Subtype is Present it means this record subtype has
3291 identical layout to that type or subtype and we should use
3292 that GCC type for this one. The front end guarantees that
3293 the component list is shared. */
3294 if (Present (Cloned_Subtype (gnat_entity)))
3296 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3297 NULL_TREE, 0);
3298 maybe_present = true;
3299 break;
3302 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3303 changing the type, make a new type with each field having the type of
3304 the field in the new subtype but the position computed by transforming
3305 every discriminant reference according to the constraints. We don't
3306 see any difference between private and non-private type here since
3307 derivations from types should have been deferred until the completion
3308 of the private type. */
3309 else
3311 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3312 tree gnu_base_type;
3314 if (!definition)
3316 defer_incomplete_level++;
3317 this_deferred = true;
3320 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3322 if (present_gnu_tree (gnat_entity))
3324 maybe_present = true;
3325 break;
3328 /* If this is a record subtype associated with a dispatch table,
3329 strip the suffix. This is necessary to make sure 2 different
3330 subtypes associated with the imported and exported views of a
3331 dispatch table are properly merged in LTO mode. */
3332 if (Is_Dispatch_Table_Entity (gnat_entity))
3334 char *p;
3335 Get_Encoded_Name (gnat_entity);
3336 p = strchr (Name_Buffer, '_');
3337 gcc_assert (p);
3338 strcpy (p+2, "dtS");
3339 gnu_entity_name = get_identifier (Name_Buffer);
3342 /* When the subtype has discriminants and these discriminants affect
3343 the initial shape it has inherited, factor them in. But for an
3344 Unchecked_Union (it must be an Itype), just return the type.
3345 We can't just test Is_Constrained because private subtypes without
3346 discriminants of types with discriminants with default expressions
3347 are Is_Constrained but aren't constrained! */
3348 if (IN (Ekind (gnat_base_type), Record_Kind)
3349 && !Is_Unchecked_Union (gnat_base_type)
3350 && !Is_For_Access_Subtype (gnat_entity)
3351 && Has_Discriminants (gnat_entity)
3352 && Is_Constrained (gnat_entity)
3353 && Stored_Constraint (gnat_entity) != No_Elist)
3355 vec<subst_pair> gnu_subst_list
3356 = build_subst_list (gnat_entity, gnat_base_type, definition);
3357 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3358 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3359 bool selected_variant = false, all_constant_pos = true;
3360 Entity_Id gnat_field;
3361 vec<variant_desc> gnu_variant_list;
3363 gnu_type = make_node (RECORD_TYPE);
3364 TYPE_NAME (gnu_type) = gnu_entity_name;
3365 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3366 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3368 /* Set the size, alignment and alias set of the new type to
3369 match that of the old one, doing required substitutions. */
3370 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3371 gnu_subst_list);
3373 if (TYPE_IS_PADDING_P (gnu_base_type))
3374 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3375 else
3376 gnu_unpad_base_type = gnu_base_type;
3378 /* Look for REP and variant parts in the base type. */
3379 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3380 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3382 /* If there is a variant part, we must compute whether the
3383 constraints statically select a particular variant. If
3384 so, we simply drop the qualified union and flatten the
3385 list of fields. Otherwise we'll build a new qualified
3386 union for the variants that are still relevant. */
3387 if (gnu_variant_part)
3389 variant_desc *v;
3390 unsigned int i;
3392 gnu_variant_list
3393 = build_variant_list (TREE_TYPE (gnu_variant_part),
3394 gnu_subst_list,
3395 vNULL);
3397 /* If all the qualifiers are unconditionally true, the
3398 innermost variant is statically selected. */
3399 selected_variant = true;
3400 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3401 if (!integer_onep (v->qual))
3403 selected_variant = false;
3404 break;
3407 /* Otherwise, create the new variants. */
3408 if (!selected_variant)
3409 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3411 tree old_variant = v->type;
3412 tree new_variant = make_node (RECORD_TYPE);
3413 tree suffix
3414 = concat_name (DECL_NAME (gnu_variant_part),
3415 IDENTIFIER_POINTER
3416 (DECL_NAME (v->field)));
3417 TYPE_NAME (new_variant)
3418 = concat_name (TYPE_NAME (gnu_type),
3419 IDENTIFIER_POINTER (suffix));
3420 copy_and_substitute_in_size (new_variant, old_variant,
3421 gnu_subst_list);
3422 v->new_type = new_variant;
3425 else
3427 gnu_variant_list.create (0);
3428 selected_variant = false;
3431 /* Make a list of fields and their position in the base type. */
3432 gnu_pos_list
3433 = build_position_list (gnu_unpad_base_type,
3434 gnu_variant_list.exists ()
3435 && !selected_variant,
3436 size_zero_node, bitsize_zero_node,
3437 BIGGEST_ALIGNMENT, NULL_TREE);
3439 /* Now go down every component in the subtype and compute its
3440 size and position from those of the component in the base
3441 type and from the constraints of the subtype. */
3442 for (gnat_field = First_Entity (gnat_entity);
3443 Present (gnat_field);
3444 gnat_field = Next_Entity (gnat_field))
3445 if ((Ekind (gnat_field) == E_Component
3446 || Ekind (gnat_field) == E_Discriminant)
3447 && !(Present (Corresponding_Discriminant (gnat_field))
3448 && Is_Tagged_Type (gnat_base_type))
3449 && Underlying_Type
3450 (Scope (Original_Record_Component (gnat_field)))
3451 == gnat_base_type)
3453 Name_Id gnat_name = Chars (gnat_field);
3454 Entity_Id gnat_old_field
3455 = Original_Record_Component (gnat_field);
3456 tree gnu_old_field
3457 = gnat_to_gnu_field_decl (gnat_old_field);
3458 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3459 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3460 tree gnu_cont_type, gnu_last = NULL_TREE;
3462 /* If the type is the same, retrieve the GCC type from the
3463 old field to take into account possible adjustments. */
3464 if (Etype (gnat_field) == Etype (gnat_old_field))
3465 gnu_field_type = TREE_TYPE (gnu_old_field);
3466 else
3467 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3469 /* If there was a component clause, the field types must be
3470 the same for the type and subtype, so copy the data from
3471 the old field to avoid recomputation here. Also if the
3472 field is justified modular and the optimization in
3473 gnat_to_gnu_field was applied. */
3474 if (Present (Component_Clause (gnat_old_field))
3475 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3476 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3477 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3478 == TREE_TYPE (gnu_old_field)))
3480 gnu_size = DECL_SIZE (gnu_old_field);
3481 gnu_field_type = TREE_TYPE (gnu_old_field);
3484 /* If the old field was packed and of constant size, we
3485 have to get the old size here, as it might differ from
3486 what the Etype conveys and the latter might overlap
3487 onto the following field. Try to arrange the type for
3488 possible better packing along the way. */
3489 else if (DECL_PACKED (gnu_old_field)
3490 && TREE_CODE (DECL_SIZE (gnu_old_field))
3491 == INTEGER_CST)
3493 gnu_size = DECL_SIZE (gnu_old_field);
3494 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3495 && !TYPE_FAT_POINTER_P (gnu_field_type)
3496 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3497 gnu_field_type
3498 = make_packable_type (gnu_field_type, true);
3501 else
3502 gnu_size = TYPE_SIZE (gnu_field_type);
3504 /* If the context of the old field is the base type or its
3505 REP part (if any), put the field directly in the new
3506 type; otherwise look up the context in the variant list
3507 and put the field either in the new type if there is a
3508 selected variant or in one of the new variants. */
3509 if (gnu_context == gnu_unpad_base_type
3510 || (gnu_rep_part
3511 && gnu_context == TREE_TYPE (gnu_rep_part)))
3512 gnu_cont_type = gnu_type;
3513 else
3515 variant_desc *v;
3516 unsigned int i;
3517 tree rep_part;
3519 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3520 if (gnu_context == v->type
3521 || ((rep_part = get_rep_part (v->type))
3522 && gnu_context == TREE_TYPE (rep_part)))
3523 break;
3524 if (v)
3526 if (selected_variant)
3527 gnu_cont_type = gnu_type;
3528 else
3529 gnu_cont_type = v->new_type;
3531 else
3532 /* The front-end may pass us "ghost" components if
3533 it fails to recognize that a constrained subtype
3534 is statically constrained. Discard them. */
3535 continue;
3538 /* Now create the new field modeled on the old one. */
3539 gnu_field
3540 = create_field_decl_from (gnu_old_field, gnu_field_type,
3541 gnu_cont_type, gnu_size,
3542 gnu_pos_list, gnu_subst_list);
3543 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3545 /* Put it in one of the new variants directly. */
3546 if (gnu_cont_type != gnu_type)
3548 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3549 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3552 /* To match the layout crafted in components_to_record,
3553 if this is the _Tag or _Parent field, put it before
3554 any other fields. */
3555 else if (gnat_name == Name_uTag
3556 || gnat_name == Name_uParent)
3557 gnu_field_list = chainon (gnu_field_list, gnu_field);
3559 /* Similarly, if this is the _Controller field, put
3560 it before the other fields except for the _Tag or
3561 _Parent field. */
3562 else if (gnat_name == Name_uController && gnu_last)
3564 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3565 DECL_CHAIN (gnu_last) = gnu_field;
3568 /* Otherwise, if this is a regular field, put it after
3569 the other fields. */
3570 else
3572 DECL_CHAIN (gnu_field) = gnu_field_list;
3573 gnu_field_list = gnu_field;
3574 if (!gnu_last)
3575 gnu_last = gnu_field;
3576 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3577 all_constant_pos = false;
3580 save_gnu_tree (gnat_field, gnu_field, false);
3583 /* If there is a variant list, a selected variant and the fields
3584 all have a constant position, put them in order of increasing
3585 position to match that of constant CONSTRUCTORs. Likewise if
3586 there is no variant list but a REP part, since the latter has
3587 been flattened in the process. */
3588 if (((gnu_variant_list.exists () && selected_variant)
3589 || (!gnu_variant_list.exists () && gnu_rep_part))
3590 && all_constant_pos)
3592 const int len = list_length (gnu_field_list);
3593 tree *field_arr = XALLOCAVEC (tree, len), t;
3594 int i;
3596 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3597 field_arr[i] = t;
3599 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3601 gnu_field_list = NULL_TREE;
3602 for (i = 0; i < len; i++)
3604 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3605 gnu_field_list = field_arr[i];
3609 /* If there is a variant list and no selected variant, we need
3610 to create the nest of variant parts from the old nest. */
3611 else if (gnu_variant_list.exists () && !selected_variant)
3613 tree new_variant_part
3614 = create_variant_part_from (gnu_variant_part,
3615 gnu_variant_list, gnu_type,
3616 gnu_pos_list, gnu_subst_list);
3617 DECL_CHAIN (new_variant_part) = gnu_field_list;
3618 gnu_field_list = new_variant_part;
3621 /* Now go through the entities again looking for Itypes that
3622 we have not elaborated but should (e.g., Etypes of fields
3623 that have Original_Components). */
3624 for (gnat_field = First_Entity (gnat_entity);
3625 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3626 if ((Ekind (gnat_field) == E_Discriminant
3627 || Ekind (gnat_field) == E_Component)
3628 && !present_gnu_tree (Etype (gnat_field)))
3629 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3631 /* Do not emit debug info for the type yet since we're going to
3632 modify it below. */
3633 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3634 false);
3635 compute_record_mode (gnu_type);
3637 /* See the E_Record_Type case for the rationale. */
3638 if (TYPE_MODE (gnu_type) != BLKmode
3639 && Is_By_Reference_Type (gnat_entity))
3640 SET_TYPE_MODE (gnu_type, BLKmode);
3642 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3644 /* Fill in locations of fields. */
3645 annotate_rep (gnat_entity, gnu_type);
3647 /* If debugging information is being written for the type, write
3648 a record that shows what we are a subtype of and also make a
3649 variable that indicates our size, if still variable. */
3650 if (debug_info_p)
3652 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3653 tree gnu_unpad_base_name
3654 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3655 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3657 TYPE_NAME (gnu_subtype_marker)
3658 = create_concat_name (gnat_entity, "XVS");
3659 finish_record_type (gnu_subtype_marker,
3660 create_field_decl (gnu_unpad_base_name,
3661 build_reference_type
3662 (gnu_unpad_base_type),
3663 gnu_subtype_marker,
3664 NULL_TREE, NULL_TREE,
3665 0, 0),
3666 0, true);
3668 add_parallel_type (gnu_type, gnu_subtype_marker);
3670 if (definition
3671 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3672 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3673 TYPE_SIZE_UNIT (gnu_subtype_marker)
3674 = create_var_decl (create_concat_name (gnat_entity,
3675 "XVZ"),
3676 NULL_TREE, sizetype, gnu_size_unit,
3677 false, false, false, false, NULL,
3678 gnat_entity);
3681 gnu_variant_list.release ();
3682 gnu_subst_list.release ();
3684 /* Now we can finalize it. */
3685 rest_of_record_type_compilation (gnu_type);
3688 /* Otherwise, go down all the components in the new type and make
3689 them equivalent to those in the base type. */
3690 else
3692 gnu_type = gnu_base_type;
3694 for (gnat_temp = First_Entity (gnat_entity);
3695 Present (gnat_temp);
3696 gnat_temp = Next_Entity (gnat_temp))
3697 if ((Ekind (gnat_temp) == E_Discriminant
3698 && !Is_Unchecked_Union (gnat_base_type))
3699 || Ekind (gnat_temp) == E_Component)
3700 save_gnu_tree (gnat_temp,
3701 gnat_to_gnu_field_decl
3702 (Original_Record_Component (gnat_temp)),
3703 false);
3706 break;
3708 case E_Access_Subprogram_Type:
3709 /* Use the special descriptor type for dispatch tables if needed,
3710 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3711 Note that we are only required to do so for static tables in
3712 order to be compatible with the C++ ABI, but Ada 2005 allows
3713 to extend library level tagged types at the local level so
3714 we do it in the non-static case as well. */
3715 if (TARGET_VTABLE_USES_DESCRIPTORS
3716 && Is_Dispatch_Table_Entity (gnat_entity))
3718 gnu_type = fdesc_type_node;
3719 gnu_size = TYPE_SIZE (gnu_type);
3720 break;
3723 /* ... fall through ... */
3725 case E_Anonymous_Access_Subprogram_Type:
3726 /* If we are not defining this entity, and we have incomplete
3727 entities being processed above us, make a dummy type and
3728 fill it in later. */
3729 if (!definition && defer_incomplete_level != 0)
3731 struct incomplete *p = XNEW (struct incomplete);
3733 gnu_type
3734 = build_pointer_type
3735 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3736 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3737 !Comes_From_Source (gnat_entity),
3738 debug_info_p, gnat_entity);
3739 this_made_decl = true;
3740 gnu_type = TREE_TYPE (gnu_decl);
3741 save_gnu_tree (gnat_entity, gnu_decl, false);
3742 saved = true;
3744 p->old_type = TREE_TYPE (gnu_type);
3745 p->full_type = Directly_Designated_Type (gnat_entity);
3746 p->next = defer_incomplete_list;
3747 defer_incomplete_list = p;
3748 break;
3751 /* ... fall through ... */
3753 case E_Allocator_Type:
3754 case E_Access_Type:
3755 case E_Access_Attribute_Type:
3756 case E_Anonymous_Access_Type:
3757 case E_General_Access_Type:
3759 /* The designated type and its equivalent type for gigi. */
3760 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3761 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3762 /* Whether it comes from a limited with. */
3763 bool is_from_limited_with
3764 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3765 && From_Limited_With (gnat_desig_equiv));
3766 /* The "full view" of the designated type. If this is an incomplete
3767 entity from a limited with, treat its non-limited view as the full
3768 view. Otherwise, if this is an incomplete or private type, use the
3769 full view. In the former case, we might point to a private type,
3770 in which case, we need its full view. Also, we want to look at the
3771 actual type used for the representation, so this takes a total of
3772 three steps. */
3773 Entity_Id gnat_desig_full_direct_first
3774 = (is_from_limited_with
3775 ? Non_Limited_View (gnat_desig_equiv)
3776 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3777 ? Full_View (gnat_desig_equiv) : Empty));
3778 Entity_Id gnat_desig_full_direct
3779 = ((is_from_limited_with
3780 && Present (gnat_desig_full_direct_first)
3781 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3782 ? Full_View (gnat_desig_full_direct_first)
3783 : gnat_desig_full_direct_first);
3784 Entity_Id gnat_desig_full
3785 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3786 /* The type actually used to represent the designated type, either
3787 gnat_desig_full or gnat_desig_equiv. */
3788 Entity_Id gnat_desig_rep;
3789 /* True if this is a pointer to an unconstrained array. */
3790 bool is_unconstrained_array;
3791 /* We want to know if we'll be seeing the freeze node for any
3792 incomplete type we may be pointing to. */
3793 bool in_main_unit
3794 = (Present (gnat_desig_full)
3795 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3796 : In_Extended_Main_Code_Unit (gnat_desig_type));
3797 /* True if we make a dummy type here. */
3798 bool made_dummy = false;
3799 /* The mode to be used for the pointer type. */
3800 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3801 /* The GCC type used for the designated type. */
3802 tree gnu_desig_type = NULL_TREE;
3804 if (!targetm.valid_pointer_mode (p_mode))
3805 p_mode = ptr_mode;
3807 /* If either the designated type or its full view is an unconstrained
3808 array subtype, replace it with the type it's a subtype of. This
3809 avoids problems with multiple copies of unconstrained array types.
3810 Likewise, if the designated type is a subtype of an incomplete
3811 record type, use the parent type to avoid order of elaboration
3812 issues. This can lose some code efficiency, but there is no
3813 alternative. */
3814 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3815 && !Is_Constrained (gnat_desig_equiv))
3816 gnat_desig_equiv = Etype (gnat_desig_equiv);
3817 if (Present (gnat_desig_full)
3818 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3819 && !Is_Constrained (gnat_desig_full))
3820 || (Ekind (gnat_desig_full) == E_Record_Subtype
3821 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3822 gnat_desig_full = Etype (gnat_desig_full);
3824 /* Set the type that's actually the representation of the designated
3825 type and also flag whether we have a unconstrained array. */
3826 gnat_desig_rep
3827 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3828 is_unconstrained_array
3829 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3831 /* If we are pointing to an incomplete type whose completion is an
3832 unconstrained array, make dummy fat and thin pointer types to it.
3833 Likewise if the type itself is dummy or an unconstrained array. */
3834 if (is_unconstrained_array
3835 && (Present (gnat_desig_full)
3836 || (present_gnu_tree (gnat_desig_equiv)
3837 && TYPE_IS_DUMMY_P
3838 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3839 || (!in_main_unit
3840 && defer_incomplete_level != 0
3841 && !present_gnu_tree (gnat_desig_equiv))
3842 || (in_main_unit
3843 && is_from_limited_with
3844 && Present (Freeze_Node (gnat_desig_equiv)))))
3846 if (present_gnu_tree (gnat_desig_rep))
3847 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3848 else
3850 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3851 made_dummy = true;
3854 /* If the call above got something that has a pointer, the pointer
3855 is our type. This could have happened either because the type
3856 was elaborated or because somebody else executed the code. */
3857 if (!TYPE_POINTER_TO (gnu_desig_type))
3858 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3859 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3862 /* If we already know what the full type is, use it. */
3863 else if (Present (gnat_desig_full)
3864 && present_gnu_tree (gnat_desig_full))
3865 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3867 /* Get the type of the thing we are to point to and build a pointer to
3868 it. If it is a reference to an incomplete or private type with a
3869 full view that is a record, make a dummy type node and get the
3870 actual type later when we have verified it is safe. */
3871 else if ((!in_main_unit
3872 && !present_gnu_tree (gnat_desig_equiv)
3873 && Present (gnat_desig_full)
3874 && !present_gnu_tree (gnat_desig_full)
3875 && Is_Record_Type (gnat_desig_full))
3876 /* Likewise if we are pointing to a record or array and we are
3877 to defer elaborating incomplete types. We do this as this
3878 access type may be the full view of a private type. Note
3879 that the unconstrained array case is handled above. */
3880 || ((!in_main_unit || imported_p)
3881 && defer_incomplete_level != 0
3882 && !present_gnu_tree (gnat_desig_equiv)
3883 && (Is_Record_Type (gnat_desig_rep)
3884 || Is_Array_Type (gnat_desig_rep)))
3885 /* If this is a reference from a limited_with type back to our
3886 main unit and there's a freeze node for it, either we have
3887 already processed the declaration and made the dummy type,
3888 in which case we just reuse the latter, or we have not yet,
3889 in which case we make the dummy type and it will be reused
3890 when the declaration is finally processed. In both cases,
3891 the pointer eventually created below will be automatically
3892 adjusted when the freeze node is processed. Note that the
3893 unconstrained array case is handled above. */
3894 || (in_main_unit
3895 && is_from_limited_with
3896 && Present (Freeze_Node (gnat_desig_rep))))
3898 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3899 made_dummy = true;
3902 /* Otherwise handle the case of a pointer to itself. */
3903 else if (gnat_desig_equiv == gnat_entity)
3905 gnu_type
3906 = build_pointer_type_for_mode (void_type_node, p_mode,
3907 No_Strict_Aliasing (gnat_entity));
3908 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3911 /* If expansion is disabled, the equivalent type of a concurrent type
3912 is absent, so build a dummy pointer type. */
3913 else if (type_annotate_only && No (gnat_desig_equiv))
3914 gnu_type = ptr_void_type_node;
3916 /* Finally, handle the default case where we can just elaborate our
3917 designated type. */
3918 else
3919 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3921 /* It is possible that a call to gnat_to_gnu_type above resolved our
3922 type. If so, just return it. */
3923 if (present_gnu_tree (gnat_entity))
3925 maybe_present = true;
3926 break;
3929 /* If we haven't done it yet, build the pointer type the usual way. */
3930 if (!gnu_type)
3932 /* Modify the designated type if we are pointing only to constant
3933 objects, but don't do it for unconstrained arrays. */
3934 if (Is_Access_Constant (gnat_entity)
3935 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3937 gnu_desig_type
3938 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3940 /* Some extra processing is required if we are building a
3941 pointer to an incomplete type (in the GCC sense). We might
3942 have such a type if we just made a dummy, or directly out
3943 of the call to gnat_to_gnu_type above if we are processing
3944 an access type for a record component designating the
3945 record type itself. */
3946 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3948 /* We must ensure that the pointer to variant we make will
3949 be processed by update_pointer_to when the initial type
3950 is completed. Pretend we made a dummy and let further
3951 processing act as usual. */
3952 made_dummy = true;
3954 /* We must ensure that update_pointer_to will not retrieve
3955 the dummy variant when building a properly qualified
3956 version of the complete type. We take advantage of the
3957 fact that get_qualified_type is requiring TYPE_NAMEs to
3958 match to influence build_qualified_type and then also
3959 update_pointer_to here. */
3960 TYPE_NAME (gnu_desig_type)
3961 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3965 gnu_type
3966 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3967 No_Strict_Aliasing (gnat_entity));
3970 /* If we are not defining this object and we have made a dummy pointer,
3971 save our current definition, evaluate the actual type, and replace
3972 the tentative type we made with the actual one. If we are to defer
3973 actually looking up the actual type, make an entry in the deferred
3974 list. If this is from a limited with, we may have to defer to the
3975 end of the current unit. */
3976 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3978 tree gnu_old_desig_type;
3980 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3982 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3983 if (esize == POINTER_SIZE)
3984 gnu_type = build_pointer_type
3985 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3987 else
3988 gnu_old_desig_type = TREE_TYPE (gnu_type);
3990 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3991 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3992 !Comes_From_Source (gnat_entity),
3993 debug_info_p, gnat_entity);
3994 this_made_decl = true;
3995 gnu_type = TREE_TYPE (gnu_decl);
3996 save_gnu_tree (gnat_entity, gnu_decl, false);
3997 saved = true;
3999 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4000 update gnu_old_desig_type directly, in which case it will not be
4001 a dummy type any more when we get into update_pointer_to.
4003 This can happen e.g. when the designated type is a record type,
4004 because their elaboration starts with an initial node from
4005 make_dummy_type, which may be the same node as the one we got.
4007 Besides, variants of this non-dummy type might have been created
4008 along the way. update_pointer_to is expected to properly take
4009 care of those situations. */
4010 if (defer_incomplete_level == 0 && !is_from_limited_with)
4012 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4013 gnat_to_gnu_type (gnat_desig_equiv));
4015 else
4017 struct incomplete *p = XNEW (struct incomplete);
4018 struct incomplete **head
4019 = (is_from_limited_with
4020 ? &defer_limited_with : &defer_incomplete_list);
4021 p->old_type = gnu_old_desig_type;
4022 p->full_type = gnat_desig_equiv;
4023 p->next = *head;
4024 *head = p;
4028 break;
4030 case E_Access_Protected_Subprogram_Type:
4031 case E_Anonymous_Access_Protected_Subprogram_Type:
4032 if (type_annotate_only && No (gnat_equiv_type))
4033 gnu_type = ptr_void_type_node;
4034 else
4036 /* The run-time representation is the equivalent type. */
4037 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4038 maybe_present = true;
4041 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4042 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4043 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4044 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4045 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4046 NULL_TREE, 0);
4048 break;
4050 case E_Access_Subtype:
4052 /* We treat this as identical to its base type; any constraint is
4053 meaningful only to the front-end.
4055 The designated type must be elaborated as well, if it does
4056 not have its own freeze node. Designated (sub)types created
4057 for constrained components of records with discriminants are
4058 not frozen by the front-end and thus not elaborated by gigi,
4059 because their use may appear before the base type is frozen,
4060 and because it is not clear that they are needed anywhere in
4061 gigi. With the current model, there is no correct place where
4062 they could be elaborated. */
4064 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4065 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4066 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4067 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4068 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4070 /* If we are not defining this entity, and we have incomplete
4071 entities being processed above us, make a dummy type and
4072 elaborate it later. */
4073 if (!definition && defer_incomplete_level != 0)
4075 struct incomplete *p = XNEW (struct incomplete);
4077 p->old_type
4078 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4079 p->full_type = Directly_Designated_Type (gnat_entity);
4080 p->next = defer_incomplete_list;
4081 defer_incomplete_list = p;
4083 else if (!IN (Ekind (Base_Type
4084 (Directly_Designated_Type (gnat_entity))),
4085 Incomplete_Or_Private_Kind))
4086 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4087 NULL_TREE, 0);
4090 maybe_present = true;
4091 break;
4093 /* Subprogram Entities
4095 The following access functions are defined for subprograms:
4097 Etype Return type or Standard_Void_Type.
4098 First_Formal The first formal parameter.
4099 Is_Imported Indicates that the subprogram has appeared in
4100 an INTERFACE or IMPORT pragma. For now we
4101 assume that the external language is C.
4102 Is_Exported Likewise but for an EXPORT pragma.
4103 Is_Inlined True if the subprogram is to be inlined.
4105 Each parameter is first checked by calling must_pass_by_ref on its
4106 type to determine if it is passed by reference. For parameters which
4107 are copied in, if they are Ada In Out or Out parameters, their return
4108 value becomes part of a record which becomes the return type of the
4109 function (C function - note that this applies only to Ada procedures
4110 so there is no Ada return type). Additional code to store back the
4111 parameters will be generated on the caller side. This transformation
4112 is done here, not in the front-end.
4114 The intended result of the transformation can be seen from the
4115 equivalent source rewritings that follow:
4117 struct temp {int a,b};
4118 procedure P (A,B: In Out ...) is temp P (int A,B)
4119 begin {
4120 .. ..
4121 end P; return {A,B};
4124 temp t;
4125 P(X,Y); t = P(X,Y);
4126 X = t.a , Y = t.b;
4128 For subprogram types we need to perform mainly the same conversions to
4129 GCC form that are needed for procedures and function declarations. The
4130 only difference is that at the end, we make a type declaration instead
4131 of a function declaration. */
4133 case E_Subprogram_Type:
4134 case E_Function:
4135 case E_Procedure:
4137 /* The type returned by a function or else Standard_Void_Type for a
4138 procedure. */
4139 Entity_Id gnat_return_type = Etype (gnat_entity);
4140 tree gnu_return_type;
4141 /* The first GCC parameter declaration (a PARM_DECL node). The
4142 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4143 actually is the head of this parameter list. */
4144 tree gnu_param_list = NULL_TREE;
4145 /* Non-null for subprograms containing parameters passed by copy-in
4146 copy-out (Ada In Out or Out parameters not passed by reference),
4147 in which case it is the list of nodes used to specify the values
4148 of the In Out/Out parameters that are returned as a record upon
4149 procedure return. The TREE_PURPOSE of an element of this list is
4150 a field of the record and the TREE_VALUE is the PARM_DECL
4151 corresponding to that field. This list will be saved in the
4152 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4153 tree gnu_cico_list = NULL_TREE;
4154 /* List of fields in return type of procedure with copy-in copy-out
4155 parameters. */
4156 tree gnu_field_list = NULL_TREE;
4157 /* If an import pragma asks to map this subprogram to a GCC builtin,
4158 this is the builtin DECL node. */
4159 tree gnu_builtin_decl = NULL_TREE;
4160 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4161 Entity_Id gnat_param;
4162 enum inline_status_t inline_status
4163 = Has_Pragma_No_Inline (gnat_entity)
4164 ? is_suppressed
4165 : Has_Pragma_Inline_Always (gnat_entity)
4166 ? is_required
4167 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4168 bool public_flag = Is_Public (gnat_entity) || imported_p;
4169 /* Subprograms marked both Intrinsic and Always_Inline need not
4170 have a body of their own. */
4171 bool extern_flag
4172 = ((Is_Public (gnat_entity) && !definition)
4173 || imported_p
4174 || (Convention (gnat_entity) == Convention_Intrinsic
4175 && Has_Pragma_Inline_Always (gnat_entity)));
4176 bool artificial_flag = !Comes_From_Source (gnat_entity);
4177 /* The semantics of "pure" in Ada essentially matches that of "const"
4178 in the back-end. In particular, both properties are orthogonal to
4179 the "nothrow" property if the EH circuitry is explicit in the
4180 internal representation of the back-end. If we are to completely
4181 hide the EH circuitry from it, we need to declare that calls to pure
4182 Ada subprograms that can throw have side effects since they can
4183 trigger an "abnormal" transfer of control flow; thus they can be
4184 neither "const" nor "pure" in the back-end sense. */
4185 bool const_flag
4186 = (Exception_Mechanism == Back_End_Exceptions
4187 && Is_Pure (gnat_entity));
4188 bool volatile_flag = No_Return (gnat_entity);
4189 bool return_by_direct_ref_p = false;
4190 bool return_by_invisi_ref_p = false;
4191 bool return_unconstrained_p = false;
4192 int parmnum;
4194 /* A parameter may refer to this type, so defer completion of any
4195 incomplete types. */
4196 if (kind == E_Subprogram_Type && !definition)
4198 defer_incomplete_level++;
4199 this_deferred = true;
4202 /* If the subprogram has an alias, it is probably inherited, so
4203 we can use the original one. If the original "subprogram"
4204 is actually an enumeration literal, it may be the first use
4205 of its type, so we must elaborate that type now. */
4206 if (Present (Alias (gnat_entity)))
4208 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4209 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4211 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4213 /* Elaborate any Itypes in the parameters of this entity. */
4214 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4215 Present (gnat_temp);
4216 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4217 if (Is_Itype (Etype (gnat_temp)))
4218 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4220 break;
4223 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4224 corresponding DECL node. Proper generation of calls later on need
4225 proper parameter associations so we don't "break;" here. */
4226 if (Convention (gnat_entity) == Convention_Intrinsic
4227 && Present (Interface_Name (gnat_entity)))
4229 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4231 /* Inability to find the builtin decl most often indicates a
4232 genuine mistake, but imports of unregistered intrinsics are
4233 sometimes issued on purpose to allow hooking in alternate
4234 bodies. We post a warning conditioned on Wshadow in this case,
4235 to let developers be notified on demand without risking false
4236 positives with common default sets of options. */
4238 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4239 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4242 /* ??? What if we don't find the builtin node above ? warn ? err ?
4243 In the current state we neither warn nor err, and calls will just
4244 be handled as for regular subprograms. */
4246 /* Look into the return type and get its associated GCC tree. If it
4247 is not void, compute various flags for the subprogram type. */
4248 if (Ekind (gnat_return_type) == E_Void)
4249 gnu_return_type = void_type_node;
4250 else
4252 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4253 context may now appear in parameter and result profiles. If
4254 we are only annotating types, break circularities here. */
4255 if (type_annotate_only
4256 && is_from_limited_with_of_main (gnat_return_type))
4257 gnu_return_type = ptr_void_type_node;
4258 else
4259 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4261 /* If this function returns by reference, make the actual return
4262 type the pointer type and make a note of that. */
4263 if (Returns_By_Ref (gnat_entity))
4265 gnu_return_type = build_pointer_type (gnu_return_type);
4266 return_by_direct_ref_p = true;
4269 /* If the return type is an unconstrained array type, the return
4270 value will be allocated on the secondary stack so the actual
4271 return type is the fat pointer type. */
4272 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4274 gnu_return_type = TREE_TYPE (gnu_return_type);
4275 return_unconstrained_p = true;
4278 /* Likewise, if the return type requires a transient scope, the
4279 return value will also be allocated on the secondary stack so
4280 the actual return type is the pointer type. */
4281 else if (Requires_Transient_Scope (gnat_return_type))
4283 gnu_return_type = build_pointer_type (gnu_return_type);
4284 return_unconstrained_p = true;
4287 /* If the Mechanism is By_Reference, ensure this function uses the
4288 target's by-invisible-reference mechanism, which may not be the
4289 same as above (e.g. it might be passing an extra parameter). */
4290 else if (kind == E_Function
4291 && Mechanism (gnat_entity) == By_Reference)
4292 return_by_invisi_ref_p = true;
4294 /* Likewise, if the return type is itself By_Reference. */
4295 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4296 return_by_invisi_ref_p = true;
4298 /* If the type is a padded type and the underlying type would not
4299 be passed by reference or the function has a foreign convention,
4300 return the underlying type. */
4301 else if (TYPE_IS_PADDING_P (gnu_return_type)
4302 && (!default_pass_by_ref
4303 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4304 || Has_Foreign_Convention (gnat_entity)))
4305 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4307 /* If the return type is unconstrained, that means it must have a
4308 maximum size. Use the padded type as the effective return type.
4309 And ensure the function uses the target's by-invisible-reference
4310 mechanism to avoid copying too much data when it returns. */
4311 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4313 tree orig_type = gnu_return_type;
4315 gnu_return_type
4316 = maybe_pad_type (gnu_return_type,
4317 max_size (TYPE_SIZE (gnu_return_type),
4318 true),
4319 0, gnat_entity, false, false, false, true);
4321 /* Declare it now since it will never be declared otherwise.
4322 This is necessary to ensure that its subtrees are properly
4323 marked. */
4324 if (gnu_return_type != orig_type
4325 && !DECL_P (TYPE_NAME (gnu_return_type)))
4326 create_type_decl (TYPE_NAME (gnu_return_type),
4327 gnu_return_type, true, debug_info_p,
4328 gnat_entity);
4330 return_by_invisi_ref_p = true;
4333 /* If the return type has a size that overflows, we cannot have
4334 a function that returns that type. This usage doesn't make
4335 sense anyway, so give an error here. */
4336 if (TYPE_SIZE_UNIT (gnu_return_type)
4337 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4338 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4340 post_error ("cannot return type whose size overflows",
4341 gnat_entity);
4342 gnu_return_type = copy_node (gnu_return_type);
4343 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4344 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4345 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4346 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4350 /* Loop over the parameters and get their associated GCC tree. While
4351 doing this, build a copy-in copy-out structure if we need one. */
4352 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4353 Present (gnat_param);
4354 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4356 Entity_Id gnat_param_type = Etype (gnat_param);
4357 tree gnu_param_name = get_entity_name (gnat_param);
4358 tree gnu_param_type, gnu_param, gnu_field;
4359 Mechanism_Type mech = Mechanism (gnat_param);
4360 bool copy_in_copy_out = false, fake_param_type;
4362 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4363 context may now appear in parameter and result profiles. If
4364 we are only annotating types, break circularities here. */
4365 if (type_annotate_only
4366 && is_from_limited_with_of_main (gnat_param_type))
4368 gnu_param_type = ptr_void_type_node;
4369 fake_param_type = true;
4371 else
4373 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4374 fake_param_type = false;
4377 /* Builtins are expanded inline and there is no real call sequence
4378 involved. So the type expected by the underlying expander is
4379 always the type of each argument "as is". */
4380 if (gnu_builtin_decl)
4381 mech = By_Copy;
4382 /* Handle the first parameter of a valued procedure specially. */
4383 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4384 mech = By_Copy_Return;
4385 /* Otherwise, see if a Mechanism was supplied that forced this
4386 parameter to be passed one way or another. */
4387 else if (mech == Default
4388 || mech == By_Copy
4389 || mech == By_Reference)
4391 else if (mech > 0)
4393 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4394 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4395 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4396 mech))
4397 mech = By_Reference;
4398 else
4399 mech = By_Copy;
4401 else
4403 post_error ("unsupported mechanism for&", gnat_param);
4404 mech = Default;
4407 /* Do not call gnat_to_gnu_param for a fake parameter type since
4408 it will try to use the real type again. */
4409 if (fake_param_type)
4411 if (Ekind (gnat_param) == E_Out_Parameter)
4412 gnu_param = NULL_TREE;
4413 else
4415 gnu_param
4416 = create_param_decl (gnu_param_name, gnu_param_type,
4417 false);
4418 Set_Mechanism (gnat_param,
4419 mech == Default ? By_Copy : mech);
4420 if (Ekind (gnat_param) == E_In_Out_Parameter)
4421 copy_in_copy_out = true;
4424 else
4425 gnu_param
4426 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4427 Has_Foreign_Convention (gnat_entity),
4428 &copy_in_copy_out);
4430 /* We are returned either a PARM_DECL or a type if no parameter
4431 needs to be passed; in either case, adjust the type. */
4432 if (DECL_P (gnu_param))
4433 gnu_param_type = TREE_TYPE (gnu_param);
4434 else
4436 gnu_param_type = gnu_param;
4437 gnu_param = NULL_TREE;
4440 /* The failure of this assertion will very likely come from an
4441 order of elaboration issue for the type of the parameter. */
4442 gcc_assert (kind == E_Subprogram_Type
4443 || !TYPE_IS_DUMMY_P (gnu_param_type)
4444 || type_annotate_only);
4446 if (gnu_param)
4448 gnu_param_list = chainon (gnu_param, gnu_param_list);
4449 Sloc_to_locus (Sloc (gnat_param),
4450 &DECL_SOURCE_LOCATION (gnu_param));
4451 save_gnu_tree (gnat_param, gnu_param, false);
4453 /* If a parameter is a pointer, this function may modify
4454 memory through it and thus shouldn't be considered
4455 a const function. Also, the memory may be modified
4456 between two calls, so they can't be CSE'ed. The latter
4457 case also handles by-ref parameters. */
4458 if (POINTER_TYPE_P (gnu_param_type)
4459 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4460 const_flag = false;
4463 if (copy_in_copy_out)
4465 if (!gnu_cico_list)
4467 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4469 /* If this is a function, we also need a field for the
4470 return value to be placed. */
4471 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4473 gnu_field
4474 = create_field_decl (get_identifier ("RETVAL"),
4475 gnu_return_type,
4476 gnu_new_ret_type, NULL_TREE,
4477 NULL_TREE, 0, 0);
4478 Sloc_to_locus (Sloc (gnat_entity),
4479 &DECL_SOURCE_LOCATION (gnu_field));
4480 gnu_field_list = gnu_field;
4481 gnu_cico_list
4482 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4485 gnu_return_type = gnu_new_ret_type;
4486 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4487 /* Set a default alignment to speed up accesses. But we
4488 shouldn't increase the size of the structure too much,
4489 lest it doesn't fit in return registers anymore. */
4490 TYPE_ALIGN (gnu_return_type)
4491 = get_mode_alignment (ptr_mode);
4494 gnu_field
4495 = create_field_decl (gnu_param_name, gnu_param_type,
4496 gnu_return_type, NULL_TREE, NULL_TREE,
4497 0, 0);
4498 Sloc_to_locus (Sloc (gnat_param),
4499 &DECL_SOURCE_LOCATION (gnu_field));
4500 DECL_CHAIN (gnu_field) = gnu_field_list;
4501 gnu_field_list = gnu_field;
4502 gnu_cico_list
4503 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4507 if (gnu_cico_list)
4509 /* If we have a CICO list but it has only one entry, we convert
4510 this function into a function that returns this object. */
4511 if (list_length (gnu_cico_list) == 1)
4512 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4514 /* Do not finalize the return type if the subprogram is stubbed
4515 since structures are incomplete for the back-end. */
4516 else if (Convention (gnat_entity) != Convention_Stubbed)
4518 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4519 0, false);
4521 /* Try to promote the mode of the return type if it is passed
4522 in registers, again to speed up accesses. */
4523 if (TYPE_MODE (gnu_return_type) == BLKmode
4524 && !targetm.calls.return_in_memory (gnu_return_type,
4525 NULL_TREE))
4527 unsigned int size
4528 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4529 unsigned int i = BITS_PER_UNIT;
4530 machine_mode mode;
4532 while (i < size)
4533 i <<= 1;
4534 mode = mode_for_size (i, MODE_INT, 0);
4535 if (mode != BLKmode)
4537 SET_TYPE_MODE (gnu_return_type, mode);
4538 TYPE_ALIGN (gnu_return_type)
4539 = GET_MODE_ALIGNMENT (mode);
4540 TYPE_SIZE (gnu_return_type)
4541 = bitsize_int (GET_MODE_BITSIZE (mode));
4542 TYPE_SIZE_UNIT (gnu_return_type)
4543 = size_int (GET_MODE_SIZE (mode));
4547 if (debug_info_p)
4548 rest_of_record_type_compilation (gnu_return_type);
4552 /* Deal with platform-specific calling conventions. */
4553 if (Has_Stdcall_Convention (gnat_entity))
4554 prepend_one_attribute
4555 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4556 get_identifier ("stdcall"), NULL_TREE,
4557 gnat_entity);
4558 else if (Has_Thiscall_Convention (gnat_entity))
4559 prepend_one_attribute
4560 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4561 get_identifier ("thiscall"), NULL_TREE,
4562 gnat_entity);
4564 /* If we should request stack realignment for a foreign convention
4565 subprogram, do so. Note that this applies to task entry points
4566 in particular. */
4567 if (FOREIGN_FORCE_REALIGN_STACK
4568 && Has_Foreign_Convention (gnat_entity))
4569 prepend_one_attribute
4570 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4571 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4572 gnat_entity);
4574 /* Deal with a pragma Linker_Section on a subprogram. */
4575 if ((kind == E_Function || kind == E_Procedure)
4576 && Present (Linker_Section_Pragma (gnat_entity)))
4577 prepend_one_attribute_pragma (&attr_list,
4578 Linker_Section_Pragma (gnat_entity));
4580 /* The lists have been built in reverse. */
4581 gnu_param_list = nreverse (gnu_param_list);
4582 gnu_cico_list = nreverse (gnu_cico_list);
4584 if (kind == E_Function)
4585 Set_Mechanism (gnat_entity, return_unconstrained_p
4586 || return_by_direct_ref_p
4587 || return_by_invisi_ref_p
4588 ? By_Reference : By_Copy);
4589 gnu_type
4590 = create_subprog_type (gnu_return_type, gnu_param_list,
4591 gnu_cico_list, return_unconstrained_p,
4592 return_by_direct_ref_p,
4593 return_by_invisi_ref_p);
4595 /* A procedure (something that doesn't return anything) shouldn't be
4596 considered const since there would be no reason for calling such a
4597 subprogram. Note that procedures with Out (or In Out) parameters
4598 have already been converted into a function with a return type.
4599 Similarly, if the function returns an unconstrained type, then the
4600 function will allocate the return value on the secondary stack and
4601 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4602 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4603 const_flag = false;
4605 if (const_flag || volatile_flag)
4607 const int quals
4608 = (const_flag ? TYPE_QUAL_CONST : 0)
4609 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4611 gnu_type = change_qualified_type (gnu_type, quals);
4614 /* If we have a builtin decl for that function, use it. Check if the
4615 profiles are compatible and warn if they are not. The checker is
4616 expected to post extra diagnostics in this case. */
4617 if (gnu_builtin_decl)
4619 intrin_binding_t inb;
4621 inb.gnat_entity = gnat_entity;
4622 inb.ada_fntype = gnu_type;
4623 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4625 if (!intrin_profiles_compatible_p (&inb))
4626 post_error
4627 ("?profile of& doesn''t match the builtin it binds!",
4628 gnat_entity);
4630 gnu_decl = gnu_builtin_decl;
4631 gnu_type = TREE_TYPE (gnu_builtin_decl);
4632 break;
4635 /* If there was no specified Interface_Name and the external and
4636 internal names of the subprogram are the same, only use the
4637 internal name to allow disambiguation of nested subprograms. */
4638 if (No (Interface_Name (gnat_entity))
4639 && gnu_ext_name == gnu_entity_name)
4640 gnu_ext_name = NULL_TREE;
4642 /* If we are defining the subprogram and it has an Address clause
4643 we must get the address expression from the saved GCC tree for the
4644 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4645 the address expression here since the front-end has guaranteed
4646 in that case that the elaboration has no effects. If there is
4647 an Address clause and we are not defining the object, just
4648 make it a constant. */
4649 if (Present (Address_Clause (gnat_entity)))
4651 tree gnu_address = NULL_TREE;
4653 if (definition)
4654 gnu_address
4655 = (present_gnu_tree (gnat_entity)
4656 ? get_gnu_tree (gnat_entity)
4657 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4659 save_gnu_tree (gnat_entity, NULL_TREE, false);
4661 /* Convert the type of the object to a reference type that can
4662 alias everything as per 13.3(19). */
4663 gnu_type
4664 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4665 if (gnu_address)
4666 gnu_address = convert (gnu_type, gnu_address);
4668 gnu_decl
4669 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4670 gnu_address, false, Is_Public (gnat_entity),
4671 extern_flag, false, NULL, gnat_entity);
4672 DECL_BY_REF_P (gnu_decl) = 1;
4675 else if (kind == E_Subprogram_Type)
4677 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4678 gnu_decl
4679 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4680 debug_info_p, gnat_entity);
4682 else
4684 gnu_decl
4685 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4686 gnu_param_list, inline_status,
4687 public_flag, extern_flag, artificial_flag,
4688 attr_list, gnat_entity);
4689 /* This is unrelated to the stub built right above. */
4690 DECL_STUBBED_P (gnu_decl)
4691 = Convention (gnat_entity) == Convention_Stubbed;
4694 break;
4696 case E_Incomplete_Type:
4697 case E_Incomplete_Subtype:
4698 case E_Private_Type:
4699 case E_Private_Subtype:
4700 case E_Limited_Private_Type:
4701 case E_Limited_Private_Subtype:
4702 case E_Record_Type_With_Private:
4703 case E_Record_Subtype_With_Private:
4705 /* Get the "full view" of this entity. If this is an incomplete
4706 entity from a limited with, treat its non-limited view as the
4707 full view. Otherwise, use either the full view or the underlying
4708 full view, whichever is present. This is used in all the tests
4709 below. */
4710 Entity_Id full_view
4711 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4712 ? Non_Limited_View (gnat_entity)
4713 : Present (Full_View (gnat_entity))
4714 ? Full_View (gnat_entity)
4715 : IN (kind, Private_Kind)
4716 ? Underlying_Full_View (gnat_entity)
4717 : Empty;
4719 /* If this is an incomplete type with no full view, it must be a Taft
4720 Amendment type, in which case we return a dummy type. Otherwise,
4721 just get the type from its Etype. */
4722 if (No (full_view))
4724 if (kind == E_Incomplete_Type)
4726 gnu_type = make_dummy_type (gnat_entity);
4727 gnu_decl = TYPE_STUB_DECL (gnu_type);
4729 else
4731 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4732 NULL_TREE, 0);
4733 maybe_present = true;
4735 break;
4738 /* If we already made a type for the full view, reuse it. */
4739 else if (present_gnu_tree (full_view))
4741 gnu_decl = get_gnu_tree (full_view);
4742 break;
4745 /* Otherwise, if we are not defining the type now, get the type
4746 from the full view. But always get the type from the full view
4747 for define on use types, since otherwise we won't see them! */
4748 else if (!definition
4749 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4750 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4752 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4753 maybe_present = true;
4754 break;
4757 /* For incomplete types, make a dummy type entry which will be
4758 replaced later. Save it as the full declaration's type so
4759 we can do any needed updates when we see it. */
4760 gnu_type = make_dummy_type (gnat_entity);
4761 gnu_decl = TYPE_STUB_DECL (gnu_type);
4762 if (Has_Completion_In_Body (gnat_entity))
4763 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4764 save_gnu_tree (full_view, gnu_decl, 0);
4765 break;
4768 case E_Class_Wide_Type:
4769 /* Class-wide types are always transformed into their root type. */
4770 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4771 maybe_present = true;
4772 break;
4774 case E_Task_Type:
4775 case E_Task_Subtype:
4776 case E_Protected_Type:
4777 case E_Protected_Subtype:
4778 /* Concurrent types are always transformed into their record type. */
4779 if (type_annotate_only && No (gnat_equiv_type))
4780 gnu_type = void_type_node;
4781 else
4782 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4783 maybe_present = true;
4784 break;
4786 case E_Label:
4787 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4788 break;
4790 case E_Block:
4791 case E_Loop:
4792 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4793 we've already saved it, so we don't try to. */
4794 gnu_decl = error_mark_node;
4795 saved = true;
4796 break;
4798 case E_Abstract_State:
4799 /* This is a SPARK annotation that only reaches here when compiling in
4800 ASIS mode. */
4801 gcc_assert (type_annotate_only);
4802 gnu_decl = error_mark_node;
4803 saved = true;
4804 break;
4806 default:
4807 gcc_unreachable ();
4810 /* If we had a case where we evaluated another type and it might have
4811 defined this one, handle it here. */
4812 if (maybe_present && present_gnu_tree (gnat_entity))
4814 gnu_decl = get_gnu_tree (gnat_entity);
4815 saved = true;
4818 /* If we are processing a type and there is either no decl for it or
4819 we just made one, do some common processing for the type, such as
4820 handling alignment and possible padding. */
4821 if (is_type && (!gnu_decl || this_made_decl))
4823 /* Process the attributes, if not already done. Note that the type is
4824 already defined so we cannot pass true for IN_PLACE here. */
4825 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4827 /* Tell the middle-end that objects of tagged types are guaranteed to
4828 be properly aligned. This is necessary because conversions to the
4829 class-wide type are translated into conversions to the root type,
4830 which can be less aligned than some of its derived types. */
4831 if (Is_Tagged_Type (gnat_entity)
4832 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4833 TYPE_ALIGN_OK (gnu_type) = 1;
4835 /* Record whether the type is passed by reference. */
4836 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4837 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4839 /* ??? Don't set the size for a String_Literal since it is either
4840 confirming or we don't handle it properly (if the low bound is
4841 non-constant). */
4842 if (!gnu_size && kind != E_String_Literal_Subtype)
4844 Uint gnat_size = Known_Esize (gnat_entity)
4845 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4846 gnu_size
4847 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4848 false, Has_Size_Clause (gnat_entity));
4851 /* If a size was specified, see if we can make a new type of that size
4852 by rearranging the type, for example from a fat to a thin pointer. */
4853 if (gnu_size)
4855 gnu_type
4856 = make_type_from_size (gnu_type, gnu_size,
4857 Has_Biased_Representation (gnat_entity));
4859 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4860 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4861 gnu_size = NULL_TREE;
4864 /* If the alignment has not already been processed and this is not
4865 an unconstrained array type, see if an alignment is specified.
4866 If not, we pick a default alignment for atomic objects. */
4867 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4869 else if (Known_Alignment (gnat_entity))
4871 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4872 TYPE_ALIGN (gnu_type));
4874 /* Warn on suspiciously large alignments. This should catch
4875 errors about the (alignment,byte)/(size,bit) discrepancy. */
4876 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4878 tree size;
4880 /* If a size was specified, take it into account. Otherwise
4881 use the RM size for records or unions as the type size has
4882 already been adjusted to the alignment. */
4883 if (gnu_size)
4884 size = gnu_size;
4885 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4886 && !TYPE_FAT_POINTER_P (gnu_type))
4887 size = rm_size (gnu_type);
4888 else
4889 size = TYPE_SIZE (gnu_type);
4891 /* Consider an alignment as suspicious if the alignment/size
4892 ratio is greater or equal to the byte/bit ratio. */
4893 if (tree_fits_uhwi_p (size)
4894 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4895 post_error_ne ("?suspiciously large alignment specified for&",
4896 Expression (Alignment_Clause (gnat_entity)),
4897 gnat_entity);
4900 else if (Is_Atomic (gnat_entity) && !gnu_size
4901 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4902 && integer_pow2p (TYPE_SIZE (gnu_type)))
4903 align = MIN (BIGGEST_ALIGNMENT,
4904 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4905 else if (Is_Atomic (gnat_entity) && gnu_size
4906 && tree_fits_uhwi_p (gnu_size)
4907 && integer_pow2p (gnu_size))
4908 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4910 /* See if we need to pad the type. If we did, and made a record,
4911 the name of the new type may be changed. So get it back for
4912 us when we make the new TYPE_DECL below. */
4913 if (gnu_size || align > 0)
4914 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4915 false, !gnu_decl, definition, false);
4917 if (TYPE_IS_PADDING_P (gnu_type))
4918 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4920 /* Now set the RM size of the type. We cannot do it before padding
4921 because we need to accept arbitrary RM sizes on integral types. */
4922 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4924 /* If we are at global level, GCC will have applied variable_size to
4925 the type, but that won't have done anything. So, if it's not
4926 a constant or self-referential, call elaborate_expression_1 to
4927 make a variable for the size rather than calculating it each time.
4928 Handle both the RM size and the actual size. */
4929 if (global_bindings_p ()
4930 && TYPE_SIZE (gnu_type)
4931 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4932 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4934 tree size = TYPE_SIZE (gnu_type);
4936 TYPE_SIZE (gnu_type)
4937 = elaborate_expression_1 (size, gnat_entity,
4938 get_identifier ("SIZE"),
4939 definition, false);
4941 /* ??? For now, store the size as a multiple of the alignment in
4942 bytes so that we can see the alignment from the tree. */
4943 TYPE_SIZE_UNIT (gnu_type)
4944 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4945 get_identifier ("SIZE_A_UNIT"),
4946 definition, false,
4947 TYPE_ALIGN (gnu_type));
4949 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4950 may not be marked by the call to create_type_decl below. */
4951 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4953 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4955 tree variant_part = get_variant_part (gnu_type);
4956 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4958 if (variant_part)
4960 tree union_type = TREE_TYPE (variant_part);
4961 tree offset = DECL_FIELD_OFFSET (variant_part);
4963 /* If the position of the variant part is constant, subtract
4964 it from the size of the type of the parent to get the new
4965 size. This manual CSE reduces the data size. */
4966 if (TREE_CODE (offset) == INTEGER_CST)
4968 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4969 TYPE_SIZE (union_type)
4970 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4971 bit_from_pos (offset, bitpos));
4972 TYPE_SIZE_UNIT (union_type)
4973 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4974 byte_from_pos (offset, bitpos));
4976 else
4978 TYPE_SIZE (union_type)
4979 = elaborate_expression_1 (TYPE_SIZE (union_type),
4980 gnat_entity,
4981 get_identifier ("VSIZE"),
4982 definition, false);
4984 /* ??? For now, store the size as a multiple of the
4985 alignment in bytes so that we can see the alignment
4986 from the tree. */
4987 TYPE_SIZE_UNIT (union_type)
4988 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4989 gnat_entity,
4990 get_identifier
4991 ("VSIZE_A_UNIT"),
4992 definition, false,
4993 TYPE_ALIGN (union_type));
4995 /* ??? For now, store the offset as a multiple of the
4996 alignment in bytes so that we can see the alignment
4997 from the tree. */
4998 DECL_FIELD_OFFSET (variant_part)
4999 = elaborate_expression_2 (offset,
5000 gnat_entity,
5001 get_identifier ("VOFFSET"),
5002 definition, false,
5003 DECL_OFFSET_ALIGN
5004 (variant_part));
5007 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5008 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5011 if (operand_equal_p (ada_size, size, 0))
5012 ada_size = TYPE_SIZE (gnu_type);
5013 else
5014 ada_size
5015 = elaborate_expression_1 (ada_size, gnat_entity,
5016 get_identifier ("RM_SIZE"),
5017 definition, false);
5018 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5022 /* If this is a record type or subtype, call elaborate_expression_2 on
5023 any field position. Do this for both global and local types.
5024 Skip any fields that we haven't made trees for to avoid problems with
5025 class wide types. */
5026 if (IN (kind, Record_Kind))
5027 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5028 gnat_temp = Next_Entity (gnat_temp))
5029 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5031 tree gnu_field = get_gnu_tree (gnat_temp);
5033 /* ??? For now, store the offset as a multiple of the alignment
5034 in bytes so that we can see the alignment from the tree. */
5035 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5037 DECL_FIELD_OFFSET (gnu_field)
5038 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5039 gnat_temp,
5040 get_identifier ("OFFSET"),
5041 definition, false,
5042 DECL_OFFSET_ALIGN (gnu_field));
5044 /* ??? The context of gnu_field is not necessarily gnu_type
5045 so the MULT_EXPR node built above may not be marked by
5046 the call to create_type_decl below. */
5047 if (global_bindings_p ())
5048 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5052 if (Is_Atomic (gnat_entity))
5053 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5055 /* If this is not an unconstrained array type, set some flags. */
5056 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5058 if (Treat_As_Volatile (gnat_entity))
5059 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5061 if (Present (Alignment_Clause (gnat_entity)))
5062 TYPE_USER_ALIGN (gnu_type) = 1;
5064 if (Universal_Aliasing (gnat_entity))
5065 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5068 if (!gnu_decl)
5069 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5070 !Comes_From_Source (gnat_entity),
5071 debug_info_p, gnat_entity);
5072 else
5074 TREE_TYPE (gnu_decl) = gnu_type;
5075 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5079 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5081 gnu_type = TREE_TYPE (gnu_decl);
5083 /* If this is a derived type, relate its alias set to that of its parent
5084 to avoid troubles when a call to an inherited primitive is inlined in
5085 a context where a derived object is accessed. The inlined code works
5086 on the parent view so the resulting code may access the same object
5087 using both the parent and the derived alias sets, which thus have to
5088 conflict. As the same issue arises with component references, the
5089 parent alias set also has to conflict with composite types enclosing
5090 derived components. For instance, if we have:
5092 type D is new T;
5093 type R is record
5094 Component : D;
5095 end record;
5097 we want T to conflict with both D and R, in addition to R being a
5098 superset of D by record/component construction.
5100 One way to achieve this is to perform an alias set copy from the
5101 parent to the derived type. This is not quite appropriate, though,
5102 as we don't want separate derived types to conflict with each other:
5104 type I1 is new Integer;
5105 type I2 is new Integer;
5107 We want I1 and I2 to both conflict with Integer but we do not want
5108 I1 to conflict with I2, and an alias set copy on derivation would
5109 have that effect.
5111 The option chosen is to make the alias set of the derived type a
5112 superset of that of its parent type. It trivially fulfills the
5113 simple requirement for the Integer derivation example above, and
5114 the component case as well by superset transitivity:
5116 superset superset
5117 R ----------> D ----------> T
5119 However, for composite types, conversions between derived types are
5120 translated into VIEW_CONVERT_EXPRs so a sequence like:
5122 type Comp1 is new Comp;
5123 type Comp2 is new Comp;
5124 procedure Proc (C : Comp1);
5126 C : Comp2;
5127 Proc (Comp1 (C));
5129 is translated into:
5131 C : Comp2;
5132 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5134 and gimplified into:
5136 C : Comp2;
5137 Comp1 *C.0;
5138 C.0 = (Comp1 *) &C;
5139 Proc (C.0);
5141 i.e. generates code involving type punning. Therefore, Comp1 needs
5142 to conflict with Comp2 and an alias set copy is required.
5144 The language rules ensure the parent type is already frozen here. */
5145 if (kind != E_Subprogram_Type
5146 && Is_Derived_Type (gnat_entity)
5147 && !type_annotate_only)
5149 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5150 /* For constrained packed array subtypes, the implementation type is
5151 used instead of the nominal type. */
5152 if (kind == E_Array_Subtype
5153 && Is_Constrained (gnat_entity)
5154 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5155 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5156 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5157 Is_Composite_Type (gnat_entity)
5158 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5161 /* Back-annotate the Alignment of the type if not already in the
5162 tree. Likewise for sizes. */
5163 if (Unknown_Alignment (gnat_entity))
5165 unsigned int double_align, align;
5166 bool is_capped_double, align_clause;
5168 /* If the default alignment of "double" or larger scalar types is
5169 specifically capped and this is not an array with an alignment
5170 clause on the component type, return the cap. */
5171 if ((double_align = double_float_alignment) > 0)
5172 is_capped_double
5173 = is_double_float_or_array (gnat_entity, &align_clause);
5174 else if ((double_align = double_scalar_alignment) > 0)
5175 is_capped_double
5176 = is_double_scalar_or_array (gnat_entity, &align_clause);
5177 else
5178 is_capped_double = align_clause = false;
5180 if (is_capped_double && !align_clause)
5181 align = double_align;
5182 else
5183 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5185 Set_Alignment (gnat_entity, UI_From_Int (align));
5188 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5190 tree gnu_size = TYPE_SIZE (gnu_type);
5192 /* If the size is self-referential, annotate the maximum value. */
5193 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5194 gnu_size = max_size (gnu_size, true);
5196 /* If we are just annotating types and the type is tagged, the tag
5197 and the parent components are not generated by the front-end so
5198 sizes must be adjusted if there is no representation clause. */
5199 if (type_annotate_only
5200 && Is_Tagged_Type (gnat_entity)
5201 && !VOID_TYPE_P (gnu_type)
5202 && (!TYPE_FIELDS (gnu_type)
5203 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5205 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5206 Uint uint_size;
5208 if (Is_Derived_Type (gnat_entity))
5210 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5211 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5212 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5214 else
5215 offset = pointer_size;
5217 if (TYPE_FIELDS (gnu_type))
5218 offset
5219 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5221 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5222 gnu_size = round_up (gnu_size, POINTER_SIZE);
5223 uint_size = annotate_value (gnu_size);
5224 Set_Esize (gnat_entity, uint_size);
5225 Set_RM_Size (gnat_entity, uint_size);
5227 else
5228 Set_Esize (gnat_entity, annotate_value (gnu_size));
5231 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5232 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5235 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5236 cannot do so if we are reusing the ..._DECL node made for an equivalent
5237 type or an alias or a renamed object as the predicates don't apply to it
5238 but to GNAT_ENTITY. */
5239 if (DECL_P (gnu_decl)
5240 && !(is_type && gnat_equiv_type != gnat_entity)
5241 && !Present (Alias (gnat_entity))
5242 && !(Present (Renamed_Object (gnat_entity)) && saved))
5244 if (!Comes_From_Source (gnat_entity))
5245 DECL_ARTIFICIAL (gnu_decl) = 1;
5247 if (!debug_info_p)
5248 DECL_IGNORED_P (gnu_decl) = 1;
5251 /* If we haven't already, associate the ..._DECL node that we just made with
5252 the input GNAT entity node. */
5253 if (!saved)
5254 save_gnu_tree (gnat_entity, gnu_decl, false);
5256 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5257 eliminate as many deferred computations as possible. */
5258 process_deferred_decl_context (false);
5260 /* If this is an enumeration or floating-point type, we were not able to set
5261 the bounds since they refer to the type. These are always static. */
5262 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5263 || (kind == E_Floating_Point_Type))
5265 tree gnu_scalar_type = gnu_type;
5266 tree gnu_low_bound, gnu_high_bound;
5268 /* If this is a padded type, we need to use the underlying type. */
5269 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5270 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5272 /* If this is a floating point type and we haven't set a floating
5273 point type yet, use this in the evaluation of the bounds. */
5274 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5275 longest_float_type_node = gnu_scalar_type;
5277 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5278 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5280 if (kind == E_Enumeration_Type)
5282 /* Enumeration types have specific RM bounds. */
5283 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5284 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5286 else
5288 /* Floating-point types don't have specific RM bounds. */
5289 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5290 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5294 /* If we deferred processing of incomplete types, re-enable it. If there
5295 were no other disables and we have deferred types to process, do so. */
5296 if (this_deferred
5297 && --defer_incomplete_level == 0
5298 && defer_incomplete_list)
5300 struct incomplete *p, *next;
5302 /* We are back to level 0 for the deferring of incomplete types.
5303 But processing these incomplete types below may itself require
5304 deferring, so preserve what we have and restart from scratch. */
5305 p = defer_incomplete_list;
5306 defer_incomplete_list = NULL;
5308 for (; p; p = next)
5310 next = p->next;
5312 if (p->old_type)
5313 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5314 gnat_to_gnu_type (p->full_type));
5315 free (p);
5319 /* If we are not defining this type, see if it's on one of the lists of
5320 incomplete types. If so, handle the list entry now. */
5321 if (is_type && !definition)
5323 struct incomplete *p;
5325 for (p = defer_incomplete_list; p; p = p->next)
5326 if (p->old_type && p->full_type == gnat_entity)
5328 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5329 TREE_TYPE (gnu_decl));
5330 p->old_type = NULL_TREE;
5333 for (p = defer_limited_with; p; p = p->next)
5334 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5336 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5337 TREE_TYPE (gnu_decl));
5338 p->old_type = NULL_TREE;
5342 if (this_global)
5343 force_global--;
5345 /* If this is a packed array type whose original array type is itself
5346 an Itype without freeze node, make sure the latter is processed. */
5347 if (Is_Packed_Array_Impl_Type (gnat_entity)
5348 && Is_Itype (Original_Array_Type (gnat_entity))
5349 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5350 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5351 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5353 return gnu_decl;
5356 /* Similar, but if the returned value is a COMPONENT_REF, return the
5357 FIELD_DECL. */
5359 tree
5360 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5362 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5364 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5365 gnu_field = TREE_OPERAND (gnu_field, 1);
5367 return gnu_field;
5370 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5371 the GCC type corresponding to that entity. */
5373 tree
5374 gnat_to_gnu_type (Entity_Id gnat_entity)
5376 tree gnu_decl;
5378 /* The back end never attempts to annotate generic types. */
5379 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5380 return void_type_node;
5382 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5383 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5385 return TREE_TYPE (gnu_decl);
5388 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5389 the unpadded version of the GCC type corresponding to that entity. */
5391 tree
5392 get_unpadded_type (Entity_Id gnat_entity)
5394 tree type = gnat_to_gnu_type (gnat_entity);
5396 if (TYPE_IS_PADDING_P (type))
5397 type = TREE_TYPE (TYPE_FIELDS (type));
5399 return type;
5402 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5403 type has been changed to that of the parameterless procedure, except if an
5404 alias is already present, in which case it is returned instead. */
5406 tree
5407 get_minimal_subprog_decl (Entity_Id gnat_entity)
5409 tree gnu_entity_name, gnu_ext_name;
5410 struct attrib *attr_list = NULL;
5412 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5413 of the handling applied here. */
5415 while (Present (Alias (gnat_entity)))
5417 gnat_entity = Alias (gnat_entity);
5418 if (present_gnu_tree (gnat_entity))
5419 return get_gnu_tree (gnat_entity);
5422 gnu_entity_name = get_entity_name (gnat_entity);
5423 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5425 if (Has_Stdcall_Convention (gnat_entity))
5426 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5427 get_identifier ("stdcall"), NULL_TREE,
5428 gnat_entity);
5429 else if (Has_Thiscall_Convention (gnat_entity))
5430 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5431 get_identifier ("thiscall"), NULL_TREE,
5432 gnat_entity);
5434 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5435 gnu_ext_name = NULL_TREE;
5437 return
5438 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5439 is_disabled, true, true, true, attr_list, gnat_entity);
5442 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5443 a C++ imported method or equivalent.
5445 We use the predicate on 32-bit x86/Windows to find out whether we need to
5446 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5447 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5449 bool
5450 is_cplusplus_method (Entity_Id gnat_entity)
5452 if (Convention (gnat_entity) != Convention_CPP)
5453 return false;
5455 /* This is the main case: C++ method imported as a primitive operation.
5456 Note that a C++ class with no virtual functions can be imported as a
5457 limited record type so the operation is not necessarily dispatching. */
5458 if (Is_Primitive (gnat_entity))
5459 return true;
5461 /* A thunk needs to be handled like its associated primitive operation. */
5462 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5463 return true;
5465 /* A constructor is a method on the C++ side. */
5466 if (Is_Constructor (gnat_entity))
5467 return true;
5469 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5470 if (Is_Dispatch_Table_Entity (gnat_entity))
5471 return true;
5473 return false;
5476 /* Finalize the processing of From_Limited_With incomplete types. */
5478 void
5479 finalize_from_limited_with (void)
5481 struct incomplete *p, *next;
5483 p = defer_limited_with;
5484 defer_limited_with = NULL;
5486 for (; p; p = next)
5488 next = p->next;
5490 if (p->old_type)
5491 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5492 gnat_to_gnu_type (p->full_type));
5493 free (p);
5497 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5498 kind of type (such E_Task_Type) that has a different type which Gigi
5499 uses for its representation. If the type does not have a special type
5500 for its representation, return GNAT_ENTITY. If a type is supposed to
5501 exist, but does not, abort unless annotating types, in which case
5502 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5504 Entity_Id
5505 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5507 Entity_Id gnat_equiv = gnat_entity;
5509 if (No (gnat_entity))
5510 return gnat_entity;
5512 switch (Ekind (gnat_entity))
5514 case E_Class_Wide_Subtype:
5515 if (Present (Equivalent_Type (gnat_entity)))
5516 gnat_equiv = Equivalent_Type (gnat_entity);
5517 break;
5519 case E_Access_Protected_Subprogram_Type:
5520 case E_Anonymous_Access_Protected_Subprogram_Type:
5521 gnat_equiv = Equivalent_Type (gnat_entity);
5522 break;
5524 case E_Class_Wide_Type:
5525 gnat_equiv = Root_Type (gnat_entity);
5526 break;
5528 case E_Task_Type:
5529 case E_Task_Subtype:
5530 case E_Protected_Type:
5531 case E_Protected_Subtype:
5532 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5533 break;
5535 default:
5536 break;
5539 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5541 return gnat_equiv;
5544 /* Return a GCC tree for a type corresponding to the component type of the
5545 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5546 is for an array being defined. DEBUG_INFO_P is true if we need to write
5547 debug information for other types that we may create in the process. */
5549 static tree
5550 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5551 bool debug_info_p)
5553 const Entity_Id gnat_type = Component_Type (gnat_array);
5554 tree gnu_type = gnat_to_gnu_type (gnat_type);
5555 tree gnu_comp_size;
5557 /* Try to get a smaller form of the component if needed. */
5558 if ((Is_Packed (gnat_array)
5559 || Has_Component_Size_Clause (gnat_array))
5560 && !Is_Bit_Packed_Array (gnat_array)
5561 && !Has_Aliased_Components (gnat_array)
5562 && !Strict_Alignment (gnat_type)
5563 && RECORD_OR_UNION_TYPE_P (gnu_type)
5564 && !TYPE_FAT_POINTER_P (gnu_type)
5565 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5566 gnu_type = make_packable_type (gnu_type, false);
5568 if (Has_Atomic_Components (gnat_array))
5569 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5571 /* Get and validate any specified Component_Size. */
5572 gnu_comp_size
5573 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5574 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5575 true, Has_Component_Size_Clause (gnat_array));
5577 /* If the array has aliased components and the component size can be zero,
5578 force at least unit size to ensure that the components have distinct
5579 addresses. */
5580 if (!gnu_comp_size
5581 && Has_Aliased_Components (gnat_array)
5582 && (integer_zerop (TYPE_SIZE (gnu_type))
5583 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5584 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5585 gnu_comp_size
5586 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5588 /* If the component type is a RECORD_TYPE that has a self-referential size,
5589 then use the maximum size for the component size. */
5590 if (!gnu_comp_size
5591 && TREE_CODE (gnu_type) == RECORD_TYPE
5592 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5593 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5595 /* Honor the component size. This is not needed for bit-packed arrays. */
5596 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5598 tree orig_type = gnu_type;
5599 unsigned int max_align;
5601 /* If an alignment is specified, use it as a cap on the component type
5602 so that it can be honored for the whole type. But ignore it for the
5603 original type of packed array types. */
5604 if (No (Packed_Array_Impl_Type (gnat_array))
5605 && Known_Alignment (gnat_array))
5606 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5607 else
5608 max_align = 0;
5610 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5611 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5612 gnu_type = orig_type;
5613 else
5614 orig_type = gnu_type;
5616 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5617 true, false, definition, true);
5619 /* If a padding record was made, declare it now since it will never be
5620 declared otherwise. This is necessary to ensure that its subtrees
5621 are properly marked. */
5622 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5623 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5624 gnat_array);
5627 if (Has_Volatile_Components (gnat_array))
5628 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
5630 return gnu_type;
5633 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5634 using MECH as its passing mechanism, to be placed in the parameter
5635 list built for GNAT_SUBPROG. Assume a foreign convention for the
5636 latter if FOREIGN is true. Also set CICO to true if the parameter
5637 must use the copy-in copy-out implementation mechanism.
5639 The returned tree is a PARM_DECL, except for those cases where no
5640 parameter needs to be actually passed to the subprogram; the type
5641 of this "shadow" parameter is then returned instead. */
5643 static tree
5644 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5645 Entity_Id gnat_subprog, bool foreign, bool *cico)
5647 tree gnu_param_name = get_entity_name (gnat_param);
5648 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5649 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5650 /* The parameter can be indirectly modified if its address is taken. */
5651 bool ro_param = in_param && !Address_Taken (gnat_param);
5652 bool by_return = false, by_component_ptr = false;
5653 bool by_ref = false;
5654 tree gnu_param;
5656 /* Copy-return is used only for the first parameter of a valued procedure.
5657 It's a copy mechanism for which a parameter is never allocated. */
5658 if (mech == By_Copy_Return)
5660 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5661 mech = By_Copy;
5662 by_return = true;
5665 /* If this is either a foreign function or if the underlying type won't
5666 be passed by reference and is as aligned as the original type, strip
5667 off possible padding type. */
5668 if (TYPE_IS_PADDING_P (gnu_param_type))
5670 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5672 if (foreign
5673 || (!must_pass_by_ref (unpadded_type)
5674 && mech != By_Reference
5675 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5676 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5677 gnu_param_type = unpadded_type;
5680 /* If this is a read-only parameter, make a variant of the type that is
5681 read-only. ??? However, if this is an unconstrained array, that type
5682 can be very complex, so skip it for now. Likewise for any other
5683 self-referential type. */
5684 if (ro_param
5685 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5686 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5687 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5689 /* For foreign conventions, pass arrays as pointers to the element type.
5690 First check for unconstrained array and get the underlying array. */
5691 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5692 gnu_param_type
5693 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5695 /* For GCC builtins, pass Address integer types as (void *) */
5696 if (Convention (gnat_subprog) == Convention_Intrinsic
5697 && Present (Interface_Name (gnat_subprog))
5698 && Is_Descendent_Of_Address (Etype (gnat_param)))
5699 gnu_param_type = ptr_void_type_node;
5701 /* Arrays are passed as pointers to element type for foreign conventions. */
5702 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5704 /* Strip off any multi-dimensional entries, then strip
5705 off the last array to get the component type. */
5706 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5707 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5708 gnu_param_type = TREE_TYPE (gnu_param_type);
5710 by_component_ptr = true;
5711 gnu_param_type = TREE_TYPE (gnu_param_type);
5713 if (ro_param)
5714 gnu_param_type
5715 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5717 gnu_param_type = build_pointer_type (gnu_param_type);
5720 /* Fat pointers are passed as thin pointers for foreign conventions. */
5721 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5722 gnu_param_type
5723 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5725 /* If we must pass or were requested to pass by reference, do so.
5726 If we were requested to pass by copy, do so.
5727 Otherwise, for foreign conventions, pass In Out or Out parameters
5728 or aggregates by reference. For COBOL and Fortran, pass all
5729 integer and FP types that way too. For Convention Ada, use
5730 the standard Ada default. */
5731 else if (must_pass_by_ref (gnu_param_type)
5732 || mech == By_Reference
5733 || (mech != By_Copy
5734 && ((foreign
5735 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5736 || (foreign
5737 && (Convention (gnat_subprog) == Convention_Fortran
5738 || Convention (gnat_subprog) == Convention_COBOL)
5739 && (INTEGRAL_TYPE_P (gnu_param_type)
5740 || FLOAT_TYPE_P (gnu_param_type)))
5741 || (!foreign
5742 && default_pass_by_ref (gnu_param_type)))))
5744 /* We take advantage of 6.2(12) by considering that references built for
5745 parameters whose type isn't by-ref and for which the mechanism hasn't
5746 been forced to by-ref are restrict-qualified in the C sense. */
5747 bool restrict_p
5748 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5749 gnu_param_type = build_reference_type (gnu_param_type);
5750 if (restrict_p)
5751 gnu_param_type
5752 = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5753 by_ref = true;
5756 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5757 else if (!in_param)
5758 *cico = true;
5760 if (mech == By_Copy && (by_ref || by_component_ptr))
5761 post_error ("?cannot pass & by copy", gnat_param);
5763 /* If this is an Out parameter that isn't passed by reference and isn't
5764 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5765 it will be a VAR_DECL created when we process the procedure, so just
5766 return its type. For the special parameter of a valued procedure,
5767 never pass it in.
5769 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5770 Out parameters with discriminants or implicit initial values to be
5771 handled like In Out parameters. These type are normally built as
5772 aggregates, hence passed by reference, except for some packed arrays
5773 which end up encoded in special integer types. Note that scalars can
5774 be given implicit initial values using the Default_Value aspect.
5776 The exception we need to make is then for packed arrays of records
5777 with discriminants or implicit initial values. We have no light/easy
5778 way to check for the latter case, so we merely check for packed arrays
5779 of records. This may lead to useless copy-in operations, but in very
5780 rare cases only, as these would be exceptions in a set of already
5781 exceptional situations. */
5782 if (Ekind (gnat_param) == E_Out_Parameter
5783 && !by_ref
5784 && (by_return
5785 || (!POINTER_TYPE_P (gnu_param_type)
5786 && !AGGREGATE_TYPE_P (gnu_param_type)
5787 && !Has_Default_Aspect (Etype (gnat_param))))
5788 && !(Is_Array_Type (Etype (gnat_param))
5789 && Is_Packed (Etype (gnat_param))
5790 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5791 return gnu_param_type;
5793 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5794 ro_param || by_ref || by_component_ptr);
5795 DECL_BY_REF_P (gnu_param) = by_ref;
5796 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5797 DECL_POINTS_TO_READONLY_P (gnu_param)
5798 = (ro_param && (by_ref || by_component_ptr));
5799 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5801 /* If no Mechanism was specified, indicate what we're using, then
5802 back-annotate it. */
5803 if (mech == Default)
5804 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5806 Set_Mechanism (gnat_param, mech);
5807 return gnu_param;
5810 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5811 with of the main unit and whose full view has not been elaborated yet. */
5813 static bool
5814 is_from_limited_with_of_main (Entity_Id gnat_entity)
5816 /* Class-wide types are always transformed into their root type. */
5817 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5818 gnat_entity = Root_Type (gnat_entity);
5820 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5821 && From_Limited_With (gnat_entity))
5823 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5825 if (present_gnu_tree (gnat_full_view))
5826 return false;
5828 return In_Extended_Main_Code_Unit (gnat_full_view);
5831 return false;
5834 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5835 qualifiers on TYPE. */
5837 static tree
5838 change_qualified_type (tree type, int type_quals)
5840 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
5843 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5845 static bool
5846 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5848 while (Present (Corresponding_Discriminant (discr1)))
5849 discr1 = Corresponding_Discriminant (discr1);
5851 while (Present (Corresponding_Discriminant (discr2)))
5852 discr2 = Corresponding_Discriminant (discr2);
5854 return
5855 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5858 /* Return true if the array type GNU_TYPE, which represents a dimension of
5859 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5861 static bool
5862 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5864 /* If the array type is not the innermost dimension of the GNAT type,
5865 then it has a non-aliased component. */
5866 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5867 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5868 return true;
5870 /* If the array type has an aliased component in the front-end sense,
5871 then it also has an aliased component in the back-end sense. */
5872 if (Has_Aliased_Components (gnat_type))
5873 return false;
5875 /* If this is a derived type, then it has a non-aliased component if
5876 and only if its parent type also has one. */
5877 if (Is_Derived_Type (gnat_type))
5879 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5880 int index;
5881 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5882 gnu_parent_type
5883 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5884 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5885 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5886 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5889 /* Otherwise, rely exclusively on properties of the element type. */
5890 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5893 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5895 static bool
5896 compile_time_known_address_p (Node_Id gnat_address)
5898 /* Catch System'To_Address. */
5899 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5900 gnat_address = Expression (gnat_address);
5902 return Compile_Time_Known_Value (gnat_address);
5905 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5906 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5908 static bool
5909 cannot_be_superflat_p (Node_Id gnat_range)
5911 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5912 Node_Id scalar_range;
5913 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5915 /* If the low bound is not constant, try to find an upper bound. */
5916 while (Nkind (gnat_lb) != N_Integer_Literal
5917 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5918 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5919 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5920 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5921 || Nkind (scalar_range) == N_Range))
5922 gnat_lb = High_Bound (scalar_range);
5924 /* If the high bound is not constant, try to find a lower bound. */
5925 while (Nkind (gnat_hb) != N_Integer_Literal
5926 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5927 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5928 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5929 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5930 || Nkind (scalar_range) == N_Range))
5931 gnat_hb = Low_Bound (scalar_range);
5933 /* If we have failed to find constant bounds, punt. */
5934 if (Nkind (gnat_lb) != N_Integer_Literal
5935 || Nkind (gnat_hb) != N_Integer_Literal)
5936 return false;
5938 /* We need at least a signed 64-bit type to catch most cases. */
5939 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5940 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5941 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5942 return false;
5944 /* If the low bound is the smallest integer, nothing can be smaller. */
5945 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5946 if (TREE_OVERFLOW (gnu_lb_minus_one))
5947 return true;
5949 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5952 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5954 static bool
5955 constructor_address_p (tree gnu_expr)
5957 while (TREE_CODE (gnu_expr) == NOP_EXPR
5958 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5959 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5960 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5962 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5963 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5966 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5967 be elaborated at the point of its definition, but do nothing else. */
5969 void
5970 elaborate_entity (Entity_Id gnat_entity)
5972 switch (Ekind (gnat_entity))
5974 case E_Signed_Integer_Subtype:
5975 case E_Modular_Integer_Subtype:
5976 case E_Enumeration_Subtype:
5977 case E_Ordinary_Fixed_Point_Subtype:
5978 case E_Decimal_Fixed_Point_Subtype:
5979 case E_Floating_Point_Subtype:
5981 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5982 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5984 /* ??? Tests to avoid Constraint_Error in static expressions
5985 are needed until after the front stops generating bogus
5986 conversions on bounds of real types. */
5987 if (!Raises_Constraint_Error (gnat_lb))
5988 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5989 true, false, Needs_Debug_Info (gnat_entity));
5990 if (!Raises_Constraint_Error (gnat_hb))
5991 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5992 true, false, Needs_Debug_Info (gnat_entity));
5993 break;
5996 case E_Record_Subtype:
5997 case E_Private_Subtype:
5998 case E_Limited_Private_Subtype:
5999 case E_Record_Subtype_With_Private:
6000 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6002 Node_Id gnat_discriminant_expr;
6003 Entity_Id gnat_field;
6005 for (gnat_field
6006 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6007 gnat_discriminant_expr
6008 = First_Elmt (Discriminant_Constraint (gnat_entity));
6009 Present (gnat_field);
6010 gnat_field = Next_Discriminant (gnat_field),
6011 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6012 /* Ignore access discriminants. */
6013 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6014 elaborate_expression (Node (gnat_discriminant_expr),
6015 gnat_entity, get_entity_name (gnat_field),
6016 true, false, false);
6018 break;
6023 /* Return true if the size in units represented by GNU_SIZE can be handled by
6024 an allocation. If STATIC_P is true, consider only what can be done with a
6025 static allocation. */
6027 static bool
6028 allocatable_size_p (tree gnu_size, bool static_p)
6030 /* We can allocate a fixed size if it is a valid for the middle-end. */
6031 if (TREE_CODE (gnu_size) == INTEGER_CST)
6032 return valid_constant_size_p (gnu_size);
6034 /* We can allocate a variable size if this isn't a static allocation. */
6035 else
6036 return !static_p;
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. GNU_NAME is the suffix
6164 to use if a variable needs to be created and DEFINITION is true if this
6165 is 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, tree gnu_name,
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,
6195 gnu_name, 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, tree gnu_name,
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 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6216 reference will have been replaced with a COMPONENT_REF when the type
6217 is being elaborated. However, there are some cases involving child
6218 types where we will. So convert it to a COMPONENT_REF. We hope it
6219 will be at the highest level of the expression in these cases. */
6220 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6221 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6222 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6223 gnu_expr, NULL_TREE);
6225 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6226 that an expression cannot contain both a discriminant and a variable. */
6227 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6228 return gnu_expr;
6230 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6231 a variable that is initialized to contain the expression when the package
6232 containing the definition is elaborated. If this entity is defined at top
6233 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6234 if this is necessary. */
6235 if (CONSTANT_CLASS_P (gnu_expr))
6236 expr_variable_p = false;
6237 else
6239 /* Skip any conversions and simple constant arithmetics to see if the
6240 expression is based on a read-only variable.
6241 ??? This really should remain read-only, but we have to think about
6242 the typing of the tree here. */
6243 tree inner = remove_conversions (gnu_expr, true);
6245 inner = skip_simple_constant_arithmetic (inner);
6247 if (handled_component_p (inner))
6249 HOST_WIDE_INT bitsize, bitpos;
6250 tree offset;
6251 machine_mode mode;
6252 int unsignedp, volatilep;
6254 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6255 &mode, &unsignedp, &volatilep, false);
6256 /* If the offset is variable, err on the side of caution. */
6257 if (offset)
6258 inner = NULL_TREE;
6261 expr_variable_p
6262 = !(inner
6263 && TREE_CODE (inner) == VAR_DECL
6264 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6267 /* We only need to use the variable if we are in a global context since GCC
6268 can do the right thing in the local case. However, when not optimizing,
6269 use it for bounds of loop iteration scheme to avoid code duplication. */
6270 use_variable = expr_variable_p
6271 && (expr_global_p
6272 || (!optimize
6273 && definition
6274 && Is_Itype (gnat_entity)
6275 && Nkind (Associated_Node_For_Itype (gnat_entity))
6276 == N_Loop_Parameter_Specification));
6278 /* Now create it, possibly only for debugging purposes. */
6279 if (use_variable || need_debug)
6281 /* The following variable creation can happen when processing the body of
6282 subprograms that are defined out of the extended main unit and
6283 inlined. In this case, we are not at the global scope, and thus the
6284 new variable must not be tagged "external", as we used to do here as
6285 long as definition == 0. */
6286 const bool external_flag = !definition && expr_global_p;
6287 tree gnu_decl
6288 = create_var_decl_1
6289 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6290 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6291 external_flag, expr_global_p, !need_debug, NULL, gnat_entity);
6293 DECL_ARTIFICIAL (gnu_decl) = 1;
6295 /* Using this variable at debug time (if need_debug is true) requires a
6296 proper location. The back-end will compute a location for this
6297 variable only if the variable is used by the generated code.
6298 Returning the variable ensures the caller will use it in generated
6299 code. Note that there is no need for a location if the debug info
6300 contains an integer constant.
6301 FIXME: when the encoding-based debug scheme is dropped, move this
6302 condition to the top-level IF block: we will not need to create a
6303 variable anymore in such cases, then. */
6304 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6305 return gnu_decl;
6308 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6311 /* Similar, but take an alignment factor and make it explicit in the tree. */
6313 static tree
6314 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6315 bool definition, bool need_debug, unsigned int align)
6317 tree unit_align = size_int (align / BITS_PER_UNIT);
6318 return
6319 size_binop (MULT_EXPR,
6320 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6321 gnu_expr,
6322 unit_align),
6323 gnat_entity, gnu_name, definition,
6324 need_debug),
6325 unit_align);
6328 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6329 the value passed against the list of choices. */
6331 tree
6332 choices_to_gnu (tree operand, Node_Id choices)
6334 Node_Id choice;
6335 Node_Id gnat_temp;
6336 tree result = boolean_false_node;
6337 tree this_test, low = 0, high = 0, single = 0;
6339 for (choice = First (choices); Present (choice); choice = Next (choice))
6341 switch (Nkind (choice))
6343 case N_Range:
6344 low = gnat_to_gnu (Low_Bound (choice));
6345 high = gnat_to_gnu (High_Bound (choice));
6347 this_test
6348 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6349 build_binary_op (GE_EXPR, boolean_type_node,
6350 operand, low),
6351 build_binary_op (LE_EXPR, boolean_type_node,
6352 operand, high));
6354 break;
6356 case N_Subtype_Indication:
6357 gnat_temp = Range_Expression (Constraint (choice));
6358 low = gnat_to_gnu (Low_Bound (gnat_temp));
6359 high = gnat_to_gnu (High_Bound (gnat_temp));
6361 this_test
6362 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6363 build_binary_op (GE_EXPR, boolean_type_node,
6364 operand, low),
6365 build_binary_op (LE_EXPR, boolean_type_node,
6366 operand, high));
6367 break;
6369 case N_Identifier:
6370 case N_Expanded_Name:
6371 /* This represents either a subtype range, an enumeration
6372 literal, or a constant Ekind says which. If an enumeration
6373 literal or constant, fall through to the next case. */
6374 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6375 && Ekind (Entity (choice)) != E_Constant)
6377 tree type = gnat_to_gnu_type (Entity (choice));
6379 low = TYPE_MIN_VALUE (type);
6380 high = TYPE_MAX_VALUE (type);
6382 this_test
6383 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6384 build_binary_op (GE_EXPR, boolean_type_node,
6385 operand, low),
6386 build_binary_op (LE_EXPR, boolean_type_node,
6387 operand, high));
6388 break;
6391 /* ... fall through ... */
6393 case N_Character_Literal:
6394 case N_Integer_Literal:
6395 single = gnat_to_gnu (choice);
6396 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6397 single);
6398 break;
6400 case N_Others_Choice:
6401 this_test = boolean_true_node;
6402 break;
6404 default:
6405 gcc_unreachable ();
6408 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6409 this_test);
6412 return result;
6415 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6416 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6418 static int
6419 adjust_packed (tree field_type, tree record_type, int packed)
6421 /* If the field contains an item of variable size, we cannot pack it
6422 because we cannot create temporaries of non-fixed size in case
6423 we need to take the address of the field. See addressable_p and
6424 the notes on the addressability issues for further details. */
6425 if (type_has_variable_size (field_type))
6426 return 0;
6428 /* If the alignment of the record is specified and the field type
6429 is over-aligned, request Storage_Unit alignment for the field. */
6430 if (packed == -2)
6432 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6433 return -1;
6434 else
6435 return 0;
6438 return packed;
6441 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6442 placed in GNU_RECORD_TYPE.
6444 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6445 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6446 record has a specified alignment.
6448 DEFINITION is true if this field is for a record being defined.
6450 DEBUG_INFO_P is true if we need to write debug information for types
6451 that we may create in the process. */
6453 static tree
6454 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6455 bool definition, bool debug_info_p)
6457 const Entity_Id gnat_field_type = Etype (gnat_field);
6458 const bool is_aliased
6459 = Is_Aliased (gnat_field);
6460 const bool is_atomic
6461 = (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type));
6462 const bool is_independent
6463 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6464 const bool is_volatile
6465 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6466 const bool needs_strict_alignment
6467 = (is_aliased
6468 || is_independent
6469 || is_volatile
6470 || Strict_Alignment (gnat_field_type));
6471 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6472 tree gnu_field_id = get_entity_name (gnat_field);
6473 tree gnu_field, gnu_size, gnu_pos;
6475 /* If this field requires strict alignment, we cannot pack it because
6476 it would very likely be under-aligned in the record. */
6477 if (needs_strict_alignment)
6478 packed = 0;
6479 else
6480 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6482 /* If a size is specified, use it. Otherwise, if the record type is packed,
6483 use the official RM size. See "Handling of Type'Size Values" in Einfo
6484 for further details. */
6485 if (Known_Esize (gnat_field))
6486 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6487 gnat_field, FIELD_DECL, false, true);
6488 else if (packed == 1)
6489 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6490 gnat_field, FIELD_DECL, false, true);
6491 else
6492 gnu_size = NULL_TREE;
6494 /* If we have a specified size that is smaller than that of the field's type,
6495 or a position is specified, and the field's type is a record that doesn't
6496 require strict alignment, see if we can get either an integral mode form
6497 of the type or a smaller form. If we can, show a size was specified for
6498 the field if there wasn't one already, so we know to make this a bitfield
6499 and avoid making things wider.
6501 Changing to an integral mode form is useful when the record is packed as
6502 we can then place the field at a non-byte-aligned position and so achieve
6503 tighter packing. This is in addition required if the field shares a byte
6504 with another field and the front-end lets the back-end handle the access
6505 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6507 Changing to a smaller form is required if the specified size is smaller
6508 than that of the field's type and the type contains sub-fields that are
6509 padded, in order to avoid generating accesses to these sub-fields that
6510 are wider than the field.
6512 We avoid the transformation if it is not required or potentially useful,
6513 as it might entail an increase of the field's alignment and have ripple
6514 effects on the outer record type. A typical case is a field known to be
6515 byte-aligned and not to share a byte with another field. */
6516 if (!needs_strict_alignment
6517 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6518 && !TYPE_FAT_POINTER_P (gnu_field_type)
6519 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6520 && (packed == 1
6521 || (gnu_size
6522 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6523 || (Present (Component_Clause (gnat_field))
6524 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6525 % BITS_PER_UNIT == 0
6526 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6528 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6529 if (gnu_packable_type != gnu_field_type)
6531 gnu_field_type = gnu_packable_type;
6532 if (!gnu_size)
6533 gnu_size = rm_size (gnu_field_type);
6537 if (Is_Atomic (gnat_field))
6538 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6540 if (Present (Component_Clause (gnat_field)))
6542 Node_Id gnat_clause = Component_Clause (gnat_field);
6543 Entity_Id gnat_parent
6544 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6546 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6547 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6548 gnat_field, FIELD_DECL, false, true);
6550 /* Ensure the position does not overlap with the parent subtype, if there
6551 is one. This test is omitted if the parent of the tagged type has a
6552 full rep clause since, in this case, component clauses are allowed to
6553 overlay the space allocated for the parent type and the front-end has
6554 checked that there are no overlapping components. */
6555 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6557 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6559 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6560 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6561 post_error_ne_tree
6562 ("offset of& must be beyond parent{, minimum allowed is ^}",
6563 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6566 /* If this field needs strict alignment, make sure that the record is
6567 sufficiently aligned and that the position and size are consistent
6568 with the type. But don't do it if we are just annotating types and
6569 the field's type is tagged, since tagged types aren't fully laid out
6570 in this mode. Also, note that atomic implies volatile so the inner
6571 test sequences ordering is significant here. */
6572 if (needs_strict_alignment
6573 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6575 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6577 if (TYPE_ALIGN (gnu_record_type) < type_align)
6578 TYPE_ALIGN (gnu_record_type) = type_align;
6580 /* If the position is not a multiple of the alignment of the type,
6581 then error out and reset the position. */
6582 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6583 bitsize_int (type_align))))
6585 const char *s;
6587 if (is_atomic)
6588 s = "position of atomic field& must be multiple of ^ bits";
6589 else if (is_aliased)
6590 s = "position of aliased field& must be multiple of ^ bits";
6591 else if (is_independent)
6592 s = "position of independent field& must be multiple of ^ bits";
6593 else if (is_volatile)
6594 s = "position of volatile field& must be multiple of ^ bits";
6595 else if (Strict_Alignment (gnat_field_type))
6596 s = "position of & with aliased or tagged part must be"
6597 " multiple of ^ bits";
6598 else
6599 gcc_unreachable ();
6601 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6602 type_align);
6603 gnu_pos = NULL_TREE;
6606 if (gnu_size)
6608 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6609 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6611 /* If the size is lower than that of the type, or greater for
6612 atomic and aliased, then error out and reset the size. */
6613 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6615 const char *s;
6617 if (is_atomic)
6618 s = "size of atomic field& must be ^ bits";
6619 else if (is_aliased)
6620 s = "size of aliased field& must be ^ bits";
6621 else if (is_independent)
6622 s = "size of independent field& must be at least ^ bits";
6623 else if (is_volatile)
6624 s = "size of volatile field& must be at least ^ bits";
6625 else if (Strict_Alignment (gnat_field_type))
6626 s = "size of & with aliased or tagged part must be"
6627 " at least ^ bits";
6628 else
6629 gcc_unreachable ();
6631 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6632 gnu_type_size);
6633 gnu_size = NULL_TREE;
6636 /* Likewise if the size is not a multiple of a byte, */
6637 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6638 bitsize_unit_node)))
6640 const char *s;
6642 if (is_independent)
6643 s = "size of independent field& must be multiple of"
6644 " Storage_Unit";
6645 else if (is_volatile)
6646 s = "size of volatile field& must be multiple of"
6647 " Storage_Unit";
6648 else if (Strict_Alignment (gnat_field_type))
6649 s = "size of & with aliased or tagged part must be"
6650 " multiple of Storage_Unit";
6651 else
6652 gcc_unreachable ();
6654 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6655 gnu_size = NULL_TREE;
6661 /* If the record has rep clauses and this is the tag field, make a rep
6662 clause for it as well. */
6663 else if (Has_Specified_Layout (Scope (gnat_field))
6664 && Chars (gnat_field) == Name_uTag)
6666 gnu_pos = bitsize_zero_node;
6667 gnu_size = TYPE_SIZE (gnu_field_type);
6670 else
6672 gnu_pos = NULL_TREE;
6674 /* If we are packing the record and the field is BLKmode, round the
6675 size up to a byte boundary. */
6676 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6677 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6680 /* We need to make the size the maximum for the type if it is
6681 self-referential and an unconstrained type. In that case, we can't
6682 pack the field since we can't make a copy to align it. */
6683 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6684 && !gnu_size
6685 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6686 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6688 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6689 packed = 0;
6692 /* If a size is specified, adjust the field's type to it. */
6693 if (gnu_size)
6695 tree orig_field_type;
6697 /* If the field's type is justified modular, we would need to remove
6698 the wrapper to (better) meet the layout requirements. However we
6699 can do so only if the field is not aliased to preserve the unique
6700 layout and if the prescribed size is not greater than that of the
6701 packed array to preserve the justification. */
6702 if (!needs_strict_alignment
6703 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6704 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6705 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6706 <= 0)
6707 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6709 /* Similarly if the field's type is a misaligned integral type, but
6710 there is no restriction on the size as there is no justification. */
6711 if (!needs_strict_alignment
6712 && TYPE_IS_PADDING_P (gnu_field_type)
6713 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6714 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6716 gnu_field_type
6717 = make_type_from_size (gnu_field_type, gnu_size,
6718 Has_Biased_Representation (gnat_field));
6720 orig_field_type = gnu_field_type;
6721 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6722 false, false, definition, true);
6724 /* If a padding record was made, declare it now since it will never be
6725 declared otherwise. This is necessary to ensure that its subtrees
6726 are properly marked. */
6727 if (gnu_field_type != orig_field_type
6728 && !DECL_P (TYPE_NAME (gnu_field_type)))
6729 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6730 debug_info_p, gnat_field);
6733 /* Otherwise (or if there was an error), don't specify a position. */
6734 else
6735 gnu_pos = NULL_TREE;
6737 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6738 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6740 /* Now create the decl for the field. */
6741 gnu_field
6742 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6743 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6744 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6745 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6746 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6748 if (Ekind (gnat_field) == E_Discriminant)
6749 DECL_DISCRIMINANT_NUMBER (gnu_field)
6750 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6752 return gnu_field;
6755 /* Return true if at least one member of COMPONENT_LIST needs strict
6756 alignment. */
6758 static bool
6759 components_need_strict_alignment (Node_Id component_list)
6761 Node_Id component_decl;
6763 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6764 Present (component_decl);
6765 component_decl = Next_Non_Pragma (component_decl))
6767 Entity_Id gnat_field = Defining_Entity (component_decl);
6769 if (Is_Aliased (gnat_field))
6770 return true;
6772 if (Strict_Alignment (Etype (gnat_field)))
6773 return true;
6776 return false;
6779 /* Return true if TYPE is a type with variable size or a padding type with a
6780 field of variable size or a record that has a field with such a type. */
6782 static bool
6783 type_has_variable_size (tree type)
6785 tree field;
6787 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6788 return true;
6790 if (TYPE_IS_PADDING_P (type)
6791 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6792 return true;
6794 if (!RECORD_OR_UNION_TYPE_P (type))
6795 return false;
6797 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6798 if (type_has_variable_size (TREE_TYPE (field)))
6799 return true;
6801 return false;
6804 /* Return true if FIELD is an artificial field. */
6806 static bool
6807 field_is_artificial (tree field)
6809 /* These fields are generated by the front-end proper. */
6810 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6811 return true;
6813 /* These fields are generated by gigi. */
6814 if (DECL_INTERNAL_P (field))
6815 return true;
6817 return false;
6820 /* Return true if FIELD is a non-artificial aliased field. */
6822 static bool
6823 field_is_aliased (tree field)
6825 if (field_is_artificial (field))
6826 return false;
6828 return DECL_ALIASED_P (field);
6831 /* Return true if FIELD is a non-artificial field with self-referential
6832 size. */
6834 static bool
6835 field_has_self_size (tree field)
6837 if (field_is_artificial (field))
6838 return false;
6840 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6841 return false;
6843 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6846 /* Return true if FIELD is a non-artificial field with variable size. */
6848 static bool
6849 field_has_variable_size (tree field)
6851 if (field_is_artificial (field))
6852 return false;
6854 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6855 return false;
6857 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6860 /* qsort comparer for the bit positions of two record components. */
6862 static int
6863 compare_field_bitpos (const PTR rt1, const PTR rt2)
6865 const_tree const field1 = * (const_tree const *) rt1;
6866 const_tree const field2 = * (const_tree const *) rt2;
6867 const int ret
6868 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6870 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6873 /* Structure holding information for a given variant. */
6874 typedef struct vinfo
6876 /* The record type of the variant. */
6877 tree type;
6879 /* The name of the variant. */
6880 tree name;
6882 /* The qualifier of the variant. */
6883 tree qual;
6885 /* Whether the variant has a rep clause. */
6886 bool has_rep;
6888 /* Whether the variant is packed. */
6889 bool packed;
6891 } vinfo_t;
6893 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6894 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6895 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6896 When called from gnat_to_gnu_entity during the processing of a record type
6897 definition, the GCC node for the parent, if any, will be the single field
6898 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6899 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6900 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6902 PACKED is 1 if this is for a packed record, -1 if this is for a record
6903 with Component_Alignment of Storage_Unit, -2 if this is for a record
6904 with a specified alignment.
6906 DEFINITION is true if we are defining this record type.
6908 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6909 out the record. This means the alignment only serves to force fields to
6910 be bitfields, but not to require the record to be that aligned. This is
6911 used for variants.
6913 ALL_REP is true if a rep clause is present for all the fields.
6915 UNCHECKED_UNION is true if we are building this type for a record with a
6916 Pragma Unchecked_Union.
6918 ARTIFICIAL is true if this is a type that was generated by the compiler.
6920 DEBUG_INFO is true if we need to write debug information about the type.
6922 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6923 mean that its contents may be unused as well, only the container itself.
6925 REORDER is true if we are permitted to reorder components of this type.
6927 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6928 the outer record type down to this variant level. It is nonzero only if
6929 all the fields down to this level have a rep clause and ALL_REP is false.
6931 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6932 with a rep clause is to be added; in this case, that is all that should
6933 be done with such fields and the return value will be false. */
6935 static bool
6936 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6937 tree gnu_field_list, int packed, bool definition,
6938 bool cancel_alignment, bool all_rep,
6939 bool unchecked_union, bool artificial,
6940 bool debug_info, bool maybe_unused, bool reorder,
6941 tree first_free_pos, tree *p_gnu_rep_list)
6943 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6944 bool variants_have_rep = all_rep;
6945 bool layout_with_rep = false;
6946 bool has_self_field = false;
6947 bool has_aliased_after_self_field = false;
6948 Node_Id component_decl, variant_part;
6949 tree gnu_field, gnu_next, gnu_last;
6950 tree gnu_variant_part = NULL_TREE;
6951 tree gnu_rep_list = NULL_TREE;
6952 tree gnu_var_list = NULL_TREE;
6953 tree gnu_self_list = NULL_TREE;
6954 tree gnu_zero_list = NULL_TREE;
6956 /* For each component referenced in a component declaration create a GCC
6957 field and add it to the list, skipping pragmas in the GNAT list. */
6958 gnu_last = tree_last (gnu_field_list);
6959 if (Present (Component_Items (gnat_component_list)))
6960 for (component_decl
6961 = First_Non_Pragma (Component_Items (gnat_component_list));
6962 Present (component_decl);
6963 component_decl = Next_Non_Pragma (component_decl))
6965 Entity_Id gnat_field = Defining_Entity (component_decl);
6966 Name_Id gnat_name = Chars (gnat_field);
6968 /* If present, the _Parent field must have been created as the single
6969 field of the record type. Put it before any other fields. */
6970 if (gnat_name == Name_uParent)
6972 gnu_field = TYPE_FIELDS (gnu_record_type);
6973 gnu_field_list = chainon (gnu_field_list, gnu_field);
6975 else
6977 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6978 definition, debug_info);
6980 /* If this is the _Tag field, put it before any other fields. */
6981 if (gnat_name == Name_uTag)
6982 gnu_field_list = chainon (gnu_field_list, gnu_field);
6984 /* If this is the _Controller field, put it before the other
6985 fields except for the _Tag or _Parent field. */
6986 else if (gnat_name == Name_uController && gnu_last)
6988 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6989 DECL_CHAIN (gnu_last) = gnu_field;
6992 /* If this is a regular field, put it after the other fields. */
6993 else
6995 DECL_CHAIN (gnu_field) = gnu_field_list;
6996 gnu_field_list = gnu_field;
6997 if (!gnu_last)
6998 gnu_last = gnu_field;
7000 /* And record information for the final layout. */
7001 if (field_has_self_size (gnu_field))
7002 has_self_field = true;
7003 else if (has_self_field && field_is_aliased (gnu_field))
7004 has_aliased_after_self_field = true;
7008 save_gnu_tree (gnat_field, gnu_field, false);
7011 /* At the end of the component list there may be a variant part. */
7012 variant_part = Variant_Part (gnat_component_list);
7014 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7015 mutually exclusive and should go in the same memory. To do this we need
7016 to treat each variant as a record whose elements are created from the
7017 component list for the variant. So here we create the records from the
7018 lists for the variants and put them all into the QUAL_UNION_TYPE.
7019 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7020 use GNU_RECORD_TYPE if there are no fields so far. */
7021 if (Present (variant_part))
7023 Node_Id gnat_discr = Name (variant_part), variant;
7024 tree gnu_discr = gnat_to_gnu (gnat_discr);
7025 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7026 tree gnu_var_name
7027 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7028 "XVN");
7029 tree gnu_union_type, gnu_union_name;
7030 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7031 bool union_field_needs_strict_alignment = false;
7032 auto_vec <vinfo_t, 16> variant_types;
7033 vinfo_t *gnu_variant;
7034 unsigned int variants_align = 0;
7035 unsigned int i;
7037 gnu_union_name
7038 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7040 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7041 are all in the variant part, to match the layout of C unions. There
7042 is an associated check below. */
7043 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7044 gnu_union_type = gnu_record_type;
7045 else
7047 gnu_union_type
7048 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7050 TYPE_NAME (gnu_union_type) = gnu_union_name;
7051 TYPE_ALIGN (gnu_union_type) = 0;
7052 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7055 /* If all the fields down to this level have a rep clause, find out
7056 whether all the fields at this level also have one. If so, then
7057 compute the new first free position to be passed downward. */
7058 this_first_free_pos = first_free_pos;
7059 if (this_first_free_pos)
7061 for (gnu_field = gnu_field_list;
7062 gnu_field;
7063 gnu_field = DECL_CHAIN (gnu_field))
7064 if (DECL_FIELD_OFFSET (gnu_field))
7066 tree pos = bit_position (gnu_field);
7067 if (!tree_int_cst_lt (pos, this_first_free_pos))
7068 this_first_free_pos
7069 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7071 else
7073 this_first_free_pos = NULL_TREE;
7074 break;
7078 /* We build the variants in two passes. The bulk of the work is done in
7079 the first pass, that is to say translating the GNAT nodes, building
7080 the container types and computing the associated properties. However
7081 we cannot finish up the container types during this pass because we
7082 don't know where the variant part will be placed until the end. */
7083 for (variant = First_Non_Pragma (Variants (variant_part));
7084 Present (variant);
7085 variant = Next_Non_Pragma (variant))
7087 tree gnu_variant_type = make_node (RECORD_TYPE);
7088 tree gnu_inner_name, gnu_qual;
7089 bool has_rep;
7090 int field_packed;
7091 vinfo_t vinfo;
7093 Get_Variant_Encoding (variant);
7094 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7095 TYPE_NAME (gnu_variant_type)
7096 = concat_name (gnu_union_name,
7097 IDENTIFIER_POINTER (gnu_inner_name));
7099 /* Set the alignment of the inner type in case we need to make
7100 inner objects into bitfields, but then clear it out so the
7101 record actually gets only the alignment required. */
7102 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7103 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7105 /* Similarly, if the outer record has a size specified and all
7106 the fields have a rep clause, we can propagate the size. */
7107 if (all_rep_and_size)
7109 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7110 TYPE_SIZE_UNIT (gnu_variant_type)
7111 = TYPE_SIZE_UNIT (gnu_record_type);
7114 /* Add the fields into the record type for the variant. Note that
7115 we aren't sure to really use it at this point, see below. */
7116 has_rep
7117 = components_to_record (gnu_variant_type, Component_List (variant),
7118 NULL_TREE, packed, definition,
7119 !all_rep_and_size, all_rep,
7120 unchecked_union,
7121 true, debug_info, true, reorder,
7122 this_first_free_pos,
7123 all_rep || this_first_free_pos
7124 ? NULL : &gnu_rep_list);
7126 /* Translate the qualifier and annotate the GNAT node. */
7127 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7128 Set_Present_Expr (variant, annotate_value (gnu_qual));
7130 /* Deal with packedness like in gnat_to_gnu_field. */
7131 if (components_need_strict_alignment (Component_List (variant)))
7133 field_packed = 0;
7134 union_field_needs_strict_alignment = true;
7136 else
7137 field_packed
7138 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7140 /* Push this variant onto the stack for the second pass. */
7141 vinfo.type = gnu_variant_type;
7142 vinfo.name = gnu_inner_name;
7143 vinfo.qual = gnu_qual;
7144 vinfo.has_rep = has_rep;
7145 vinfo.packed = field_packed;
7146 variant_types.safe_push (vinfo);
7148 /* Compute the global properties that will determine the placement of
7149 the variant part. */
7150 variants_have_rep |= has_rep;
7151 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7152 variants_align = TYPE_ALIGN (gnu_variant_type);
7155 /* Round up the first free position to the alignment of the variant part
7156 for the variants without rep clause. This will guarantee a consistent
7157 layout independently of the placement of the variant part. */
7158 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7159 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7161 /* In the second pass, the container types are adjusted if necessary and
7162 finished up, then the corresponding fields of the variant part are
7163 built with their qualifier, unless this is an unchecked union. */
7164 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7166 tree gnu_variant_type = gnu_variant->type;
7167 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7169 /* If this is an Unchecked_Union whose fields are all in the variant
7170 part and we have a single field with no representation clause or
7171 placed at offset zero, use the field directly to match the layout
7172 of C unions. */
7173 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7174 && gnu_field_list
7175 && !DECL_CHAIN (gnu_field_list)
7176 && (!DECL_FIELD_OFFSET (gnu_field_list)
7177 || integer_zerop (bit_position (gnu_field_list))))
7179 gnu_field = gnu_field_list;
7180 DECL_CONTEXT (gnu_field) = gnu_record_type;
7182 else
7184 /* Finalize the variant type now. We used to throw away empty
7185 record types but we no longer do that because we need them to
7186 generate complete debug info for the variant; otherwise, the
7187 union type definition will be lacking the fields associated
7188 with these empty variants. */
7189 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7191 /* The variant part will be at offset 0 so we need to ensure
7192 that the fields are laid out starting from the first free
7193 position at this level. */
7194 tree gnu_rep_type = make_node (RECORD_TYPE);
7195 tree gnu_rep_part;
7196 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7197 gnu_rep_part
7198 = create_rep_part (gnu_rep_type, gnu_variant_type,
7199 this_first_free_pos);
7200 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7201 gnu_field_list = gnu_rep_part;
7202 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7203 false);
7206 if (debug_info)
7207 rest_of_record_type_compilation (gnu_variant_type);
7208 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7209 true, debug_info, gnat_component_list);
7211 gnu_field
7212 = create_field_decl (gnu_variant->name, gnu_variant_type,
7213 gnu_union_type,
7214 all_rep_and_size
7215 ? TYPE_SIZE (gnu_variant_type) : 0,
7216 variants_have_rep ? bitsize_zero_node : 0,
7217 gnu_variant->packed, 0);
7219 DECL_INTERNAL_P (gnu_field) = 1;
7221 if (!unchecked_union)
7222 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7225 DECL_CHAIN (gnu_field) = gnu_variant_list;
7226 gnu_variant_list = gnu_field;
7229 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7230 if (gnu_variant_list)
7232 int union_field_packed;
7234 if (all_rep_and_size)
7236 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7237 TYPE_SIZE_UNIT (gnu_union_type)
7238 = TYPE_SIZE_UNIT (gnu_record_type);
7241 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7242 all_rep_and_size ? 1 : 0, debug_info);
7244 /* If GNU_UNION_TYPE is our record type, it means we must have an
7245 Unchecked_Union with no fields. Verify that and, if so, just
7246 return. */
7247 if (gnu_union_type == gnu_record_type)
7249 gcc_assert (unchecked_union
7250 && !gnu_field_list
7251 && !gnu_rep_list);
7252 return variants_have_rep;
7255 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7256 debug_info, gnat_component_list);
7258 /* Deal with packedness like in gnat_to_gnu_field. */
7259 if (union_field_needs_strict_alignment)
7260 union_field_packed = 0;
7261 else
7262 union_field_packed
7263 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7265 gnu_variant_part
7266 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7267 all_rep_and_size
7268 ? TYPE_SIZE (gnu_union_type) : 0,
7269 variants_have_rep ? bitsize_zero_node : 0,
7270 union_field_packed, 0);
7272 DECL_INTERNAL_P (gnu_variant_part) = 1;
7276 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7277 permitted to reorder components, self-referential sizes or variable sizes.
7278 If they do, pull them out and put them onto the appropriate list. We have
7279 to do this in a separate pass since we want to handle the discriminants
7280 but can't play with them until we've used them in debugging data above.
7282 Similarly, pull out the fields with zero size and no rep clause, as they
7283 would otherwise modify the layout and thus very likely run afoul of the
7284 Ada semantics, which are different from those of C here.
7286 ??? If we reorder them, debugging information will be wrong but there is
7287 nothing that can be done about this at the moment. */
7288 gnu_last = NULL_TREE;
7290 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7291 do { \
7292 if (gnu_last) \
7293 DECL_CHAIN (gnu_last) = gnu_next; \
7294 else \
7295 gnu_field_list = gnu_next; \
7297 DECL_CHAIN (gnu_field) = (LIST); \
7298 (LIST) = gnu_field; \
7299 } while (0)
7301 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7303 gnu_next = DECL_CHAIN (gnu_field);
7305 if (DECL_FIELD_OFFSET (gnu_field))
7307 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7308 continue;
7311 if ((reorder || has_aliased_after_self_field)
7312 && field_has_self_size (gnu_field))
7314 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7315 continue;
7318 if (reorder && field_has_variable_size (gnu_field))
7320 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7321 continue;
7324 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7326 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7327 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7328 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7329 if (field_is_aliased (gnu_field))
7330 TYPE_ALIGN (gnu_record_type)
7331 = MAX (TYPE_ALIGN (gnu_record_type),
7332 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7333 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7334 continue;
7337 gnu_last = gnu_field;
7340 #undef MOVE_FROM_FIELD_LIST_TO
7342 gnu_field_list = nreverse (gnu_field_list);
7344 /* If permitted, we reorder the fields as follows:
7346 1) all fixed length fields,
7347 2) all fields whose length doesn't depend on discriminants,
7348 3) all fields whose length depends on discriminants,
7349 4) the variant part,
7351 within the record and within each variant recursively. */
7352 if (reorder)
7353 gnu_field_list
7354 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7356 /* Otherwise, if there is an aliased field placed after a field whose length
7357 depends on discriminants, we put all the fields of the latter sort, last.
7358 We need to do this in case an object of this record type is mutable. */
7359 else if (has_aliased_after_self_field)
7360 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7362 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7363 in our REP list to the previous level because this level needs them in
7364 order to do a correct layout, i.e. avoid having overlapping fields. */
7365 if (p_gnu_rep_list && gnu_rep_list)
7366 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7368 /* Otherwise, sort the fields by bit position and put them into their own
7369 record, before the others, if we also have fields without rep clause. */
7370 else if (gnu_rep_list)
7372 tree gnu_rep_type, gnu_rep_part;
7373 int i, len = list_length (gnu_rep_list);
7374 tree *gnu_arr = XALLOCAVEC (tree, len);
7376 /* If all the fields have a rep clause, we can do a flat layout. */
7377 layout_with_rep = !gnu_field_list
7378 && (!gnu_variant_part || variants_have_rep);
7379 gnu_rep_type
7380 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7382 for (gnu_field = gnu_rep_list, i = 0;
7383 gnu_field;
7384 gnu_field = DECL_CHAIN (gnu_field), i++)
7385 gnu_arr[i] = gnu_field;
7387 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7389 /* Put the fields in the list in order of increasing position, which
7390 means we start from the end. */
7391 gnu_rep_list = NULL_TREE;
7392 for (i = len - 1; i >= 0; i--)
7394 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7395 gnu_rep_list = gnu_arr[i];
7396 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7399 if (layout_with_rep)
7400 gnu_field_list = gnu_rep_list;
7401 else
7403 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7405 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7406 without rep clause are laid out starting from this position.
7407 Therefore, we force it as a minimal size on the REP part. */
7408 gnu_rep_part
7409 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7411 /* Chain the REP part at the beginning of the field list. */
7412 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7413 gnu_field_list = gnu_rep_part;
7417 /* Chain the variant part at the end of the field list. */
7418 if (gnu_variant_part)
7419 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7421 if (cancel_alignment)
7422 TYPE_ALIGN (gnu_record_type) = 0;
7424 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7426 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7427 debug_info && !maybe_unused);
7429 /* Chain the fields with zero size at the beginning of the field list. */
7430 if (gnu_zero_list)
7431 TYPE_FIELDS (gnu_record_type)
7432 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7434 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7437 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7438 placed into an Esize, Component_Bit_Offset, or Component_Size value
7439 in the GNAT tree. */
7441 static Uint
7442 annotate_value (tree gnu_size)
7444 TCode tcode;
7445 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7446 struct tree_int_map in;
7447 int i;
7449 /* See if we've already saved the value for this node. */
7450 if (EXPR_P (gnu_size))
7452 struct tree_int_map *e;
7454 in.base.from = gnu_size;
7455 e = annotate_value_cache->find (&in);
7457 if (e)
7458 return (Node_Ref_Or_Val) e->to;
7460 else
7461 in.base.from = NULL_TREE;
7463 /* If we do not return inside this switch, TCODE will be set to the
7464 code to use for a Create_Node operand and LEN (set above) will be
7465 the number of recursive calls for us to make. */
7467 switch (TREE_CODE (gnu_size))
7469 case INTEGER_CST:
7470 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7472 case COMPONENT_REF:
7473 /* The only case we handle here is a simple discriminant reference. */
7474 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7476 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7478 /* Climb up the chain of successive extensions, if any. */
7479 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7480 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7481 == parent_name_id)
7482 gnu_size = TREE_OPERAND (gnu_size, 0);
7484 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7485 return
7486 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7489 return No_Uint;
7491 CASE_CONVERT: case NON_LVALUE_EXPR:
7492 return annotate_value (TREE_OPERAND (gnu_size, 0));
7494 /* Now just list the operations we handle. */
7495 case COND_EXPR: tcode = Cond_Expr; break;
7496 case PLUS_EXPR: tcode = Plus_Expr; break;
7497 case MINUS_EXPR: tcode = Minus_Expr; break;
7498 case MULT_EXPR: tcode = Mult_Expr; break;
7499 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7500 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7501 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7502 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7503 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7504 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7505 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7506 case NEGATE_EXPR: tcode = Negate_Expr; break;
7507 case MIN_EXPR: tcode = Min_Expr; break;
7508 case MAX_EXPR: tcode = Max_Expr; break;
7509 case ABS_EXPR: tcode = Abs_Expr; break;
7510 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7511 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7512 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7513 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7514 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7515 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7516 case LT_EXPR: tcode = Lt_Expr; break;
7517 case LE_EXPR: tcode = Le_Expr; break;
7518 case GT_EXPR: tcode = Gt_Expr; break;
7519 case GE_EXPR: tcode = Ge_Expr; break;
7520 case EQ_EXPR: tcode = Eq_Expr; break;
7521 case NE_EXPR: tcode = Ne_Expr; break;
7523 case BIT_AND_EXPR:
7524 tcode = Bit_And_Expr;
7525 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7526 Such values appear in expressions with aligning patterns. Note that,
7527 since sizetype is unsigned, we have to jump through some hoops. */
7528 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7530 tree op1 = TREE_OPERAND (gnu_size, 1);
7531 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7532 if (wi::neg_p (signed_op1))
7534 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7535 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7538 break;
7540 case CALL_EXPR:
7541 /* In regular mode, inline back only if symbolic annotation is requested
7542 in order to avoid memory explosion on big discriminated record types.
7543 But not in ASIS mode, as symbolic annotation is required for DDA. */
7544 if (List_Representation_Info == 3 || type_annotate_only)
7546 tree t = maybe_inline_call_in_expr (gnu_size);
7547 if (t)
7548 return annotate_value (t);
7550 else
7551 return Uint_Minus_1;
7553 /* Fall through... */
7555 default:
7556 return No_Uint;
7559 /* Now get each of the operands that's relevant for this code. If any
7560 cannot be expressed as a repinfo node, say we can't. */
7561 for (i = 0; i < 3; i++)
7562 ops[i] = No_Uint;
7564 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7566 if (i == 1 && pre_op1 != No_Uint)
7567 ops[i] = pre_op1;
7568 else
7569 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7570 if (ops[i] == No_Uint)
7571 return No_Uint;
7574 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7576 /* Save the result in the cache. */
7577 if (in.base.from)
7579 struct tree_int_map **h;
7580 /* We can't assume the hash table data hasn't moved since the initial
7581 look up, so we have to search again. Allocating and inserting an
7582 entry at that point would be an alternative, but then we'd better
7583 discard the entry if we decided not to cache it. */
7584 h = annotate_value_cache->find_slot (&in, INSERT);
7585 gcc_assert (!*h);
7586 *h = ggc_alloc<tree_int_map> ();
7587 (*h)->base.from = gnu_size;
7588 (*h)->to = ret;
7591 return ret;
7594 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7595 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7596 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7597 BY_REF is true if the object is used by reference. */
7599 void
7600 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7602 if (by_ref)
7604 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7605 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7606 else
7607 gnu_type = TREE_TYPE (gnu_type);
7610 if (Unknown_Esize (gnat_entity))
7612 if (TREE_CODE (gnu_type) == RECORD_TYPE
7613 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7614 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7615 else if (!size)
7616 size = TYPE_SIZE (gnu_type);
7618 if (size)
7619 Set_Esize (gnat_entity, annotate_value (size));
7622 if (Unknown_Alignment (gnat_entity))
7623 Set_Alignment (gnat_entity,
7624 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7627 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7628 Return NULL_TREE if there is no such element in the list. */
7630 static tree
7631 purpose_member_field (const_tree elem, tree list)
7633 while (list)
7635 tree field = TREE_PURPOSE (list);
7636 if (SAME_FIELD_P (field, elem))
7637 return list;
7638 list = TREE_CHAIN (list);
7640 return NULL_TREE;
7643 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7644 set Component_Bit_Offset and Esize of the components to the position and
7645 size used by Gigi. */
7647 static void
7648 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7650 Entity_Id gnat_field;
7651 tree gnu_list;
7653 /* We operate by first making a list of all fields and their position (we
7654 can get the size easily) and then update all the sizes in the tree. */
7655 gnu_list
7656 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7657 BIGGEST_ALIGNMENT, NULL_TREE);
7659 for (gnat_field = First_Entity (gnat_entity);
7660 Present (gnat_field);
7661 gnat_field = Next_Entity (gnat_field))
7662 if (Ekind (gnat_field) == E_Component
7663 || (Ekind (gnat_field) == E_Discriminant
7664 && !Is_Unchecked_Union (Scope (gnat_field))))
7666 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7667 gnu_list);
7668 if (t)
7670 tree parent_offset;
7672 /* If we are just annotating types and the type is tagged, the tag
7673 and the parent components are not generated by the front-end so
7674 we need to add the appropriate offset to each component without
7675 representation clause. */
7676 if (type_annotate_only
7677 && Is_Tagged_Type (gnat_entity)
7678 && No (Component_Clause (gnat_field)))
7680 /* For a component appearing in the current extension, the
7681 offset is the size of the parent. */
7682 if (Is_Derived_Type (gnat_entity)
7683 && Original_Record_Component (gnat_field) == gnat_field)
7684 parent_offset
7685 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7686 bitsizetype);
7687 else
7688 parent_offset = bitsize_int (POINTER_SIZE);
7690 if (TYPE_FIELDS (gnu_type))
7691 parent_offset
7692 = round_up (parent_offset,
7693 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7695 else
7696 parent_offset = bitsize_zero_node;
7698 Set_Component_Bit_Offset
7699 (gnat_field,
7700 annotate_value
7701 (size_binop (PLUS_EXPR,
7702 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7703 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7704 parent_offset)));
7706 Set_Esize (gnat_field,
7707 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7709 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7711 /* If there is no entry, this is an inherited component whose
7712 position is the same as in the parent type. */
7713 Set_Component_Bit_Offset
7714 (gnat_field,
7715 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7717 Set_Esize (gnat_field,
7718 Esize (Original_Record_Component (gnat_field)));
7723 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7724 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7725 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7726 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7727 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7728 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7729 pre-existing list to be chained to the newly created entries. */
7731 static tree
7732 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7733 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7735 tree gnu_field;
7737 for (gnu_field = TYPE_FIELDS (gnu_type);
7738 gnu_field;
7739 gnu_field = DECL_CHAIN (gnu_field))
7741 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7742 DECL_FIELD_BIT_OFFSET (gnu_field));
7743 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7744 DECL_FIELD_OFFSET (gnu_field));
7745 unsigned int our_offset_align
7746 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7747 tree v = make_tree_vec (3);
7749 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7750 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7751 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7752 gnu_list = tree_cons (gnu_field, v, gnu_list);
7754 /* Recurse on internal fields, flattening the nested fields except for
7755 those in the variant part, if requested. */
7756 if (DECL_INTERNAL_P (gnu_field))
7758 tree gnu_field_type = TREE_TYPE (gnu_field);
7759 if (do_not_flatten_variant
7760 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7761 gnu_list
7762 = build_position_list (gnu_field_type, do_not_flatten_variant,
7763 size_zero_node, bitsize_zero_node,
7764 BIGGEST_ALIGNMENT, gnu_list);
7765 else
7766 gnu_list
7767 = build_position_list (gnu_field_type, do_not_flatten_variant,
7768 gnu_our_offset, gnu_our_bitpos,
7769 our_offset_align, gnu_list);
7773 return gnu_list;
7776 /* Return a list describing the substitutions needed to reflect the
7777 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7778 be in any order. The values in an element of the list are in the form
7779 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7780 a definition of GNAT_SUBTYPE. */
7782 static vec<subst_pair>
7783 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7785 vec<subst_pair> gnu_list = vNULL;
7786 Entity_Id gnat_discrim;
7787 Node_Id gnat_constr;
7789 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7790 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7791 Present (gnat_discrim);
7792 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7793 gnat_constr = Next_Elmt (gnat_constr))
7794 /* Ignore access discriminants. */
7795 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7797 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7798 tree replacement = convert (TREE_TYPE (gnu_field),
7799 elaborate_expression
7800 (Node (gnat_constr), gnat_subtype,
7801 get_entity_name (gnat_discrim),
7802 definition, true, false));
7803 subst_pair s = {gnu_field, replacement};
7804 gnu_list.safe_push (s);
7807 return gnu_list;
7810 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7811 variants of QUAL_UNION_TYPE that are still relevant after applying
7812 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7813 list to be prepended to the newly created entries. */
7815 static vec<variant_desc>
7816 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7817 vec<variant_desc> gnu_list)
7819 tree gnu_field;
7821 for (gnu_field = TYPE_FIELDS (qual_union_type);
7822 gnu_field;
7823 gnu_field = DECL_CHAIN (gnu_field))
7825 tree qual = DECL_QUALIFIER (gnu_field);
7826 unsigned int i;
7827 subst_pair *s;
7829 FOR_EACH_VEC_ELT (subst_list, i, s)
7830 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7832 /* If the new qualifier is not unconditionally false, its variant may
7833 still be accessed. */
7834 if (!integer_zerop (qual))
7836 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7837 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7839 gnu_list.safe_push (v);
7841 /* Recurse on the variant subpart of the variant, if any. */
7842 variant_subpart = get_variant_part (variant_type);
7843 if (variant_subpart)
7844 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7845 subst_list, gnu_list);
7847 /* If the new qualifier is unconditionally true, the subsequent
7848 variants cannot be accessed. */
7849 if (integer_onep (qual))
7850 break;
7854 return gnu_list;
7857 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7858 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7859 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7860 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7861 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7862 true if we are being called to process the Component_Size of GNAT_OBJECT;
7863 this is used only for error messages. ZERO_OK is true if a size of zero
7864 is permitted; if ZERO_OK is false, it means that a size of zero should be
7865 treated as an unspecified size. */
7867 static tree
7868 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7869 enum tree_code kind, bool component_p, bool zero_ok)
7871 Node_Id gnat_error_node;
7872 tree type_size, size;
7874 /* Return 0 if no size was specified. */
7875 if (uint_size == No_Uint)
7876 return NULL_TREE;
7878 /* Ignore a negative size since that corresponds to our back-annotation. */
7879 if (UI_Lt (uint_size, Uint_0))
7880 return NULL_TREE;
7882 /* Find the node to use for error messages. */
7883 if ((Ekind (gnat_object) == E_Component
7884 || Ekind (gnat_object) == E_Discriminant)
7885 && Present (Component_Clause (gnat_object)))
7886 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7887 else if (Present (Size_Clause (gnat_object)))
7888 gnat_error_node = Expression (Size_Clause (gnat_object));
7889 else
7890 gnat_error_node = gnat_object;
7892 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7893 but cannot be represented in bitsizetype. */
7894 size = UI_To_gnu (uint_size, bitsizetype);
7895 if (TREE_OVERFLOW (size))
7897 if (component_p)
7898 post_error_ne ("component size for& is too large", gnat_error_node,
7899 gnat_object);
7900 else
7901 post_error_ne ("size for& is too large", gnat_error_node,
7902 gnat_object);
7903 return NULL_TREE;
7906 /* Ignore a zero size if it is not permitted. */
7907 if (!zero_ok && integer_zerop (size))
7908 return NULL_TREE;
7910 /* The size of objects is always a multiple of a byte. */
7911 if (kind == VAR_DECL
7912 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7914 if (component_p)
7915 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7916 gnat_error_node, gnat_object);
7917 else
7918 post_error_ne ("size for& is not a multiple of Storage_Unit",
7919 gnat_error_node, gnat_object);
7920 return NULL_TREE;
7923 /* If this is an integral type or a packed array type, the front-end has
7924 already verified the size, so we need not do it here (which would mean
7925 checking against the bounds). However, if this is an aliased object,
7926 it may not be smaller than the type of the object. */
7927 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7928 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7929 return size;
7931 /* If the object is a record that contains a template, add the size of the
7932 template to the specified size. */
7933 if (TREE_CODE (gnu_type) == RECORD_TYPE
7934 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7935 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7937 if (kind == VAR_DECL
7938 /* If a type needs strict alignment, a component of this type in
7939 a packed record cannot be packed and thus uses the type size. */
7940 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7941 type_size = TYPE_SIZE (gnu_type);
7942 else
7943 type_size = rm_size (gnu_type);
7945 /* Modify the size of a discriminated type to be the maximum size. */
7946 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7947 type_size = max_size (type_size, true);
7949 /* If this is an access type or a fat pointer, the minimum size is that given
7950 by the smallest integral mode that's valid for pointers. */
7951 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7953 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7954 while (!targetm.valid_pointer_mode (p_mode))
7955 p_mode = GET_MODE_WIDER_MODE (p_mode);
7956 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7959 /* Issue an error either if the default size of the object isn't a constant
7960 or if the new size is smaller than it. */
7961 if (TREE_CODE (type_size) != INTEGER_CST
7962 || TREE_OVERFLOW (type_size)
7963 || tree_int_cst_lt (size, type_size))
7965 if (component_p)
7966 post_error_ne_tree
7967 ("component size for& too small{, minimum allowed is ^}",
7968 gnat_error_node, gnat_object, type_size);
7969 else
7970 post_error_ne_tree
7971 ("size for& too small{, minimum allowed is ^}",
7972 gnat_error_node, gnat_object, type_size);
7973 return NULL_TREE;
7976 return size;
7979 /* Similarly, but both validate and process a value of RM size. This routine
7980 is only called for types. */
7982 static void
7983 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7985 Node_Id gnat_attr_node;
7986 tree old_size, size;
7988 /* Do nothing if no size was specified. */
7989 if (uint_size == No_Uint)
7990 return;
7992 /* Ignore a negative size since that corresponds to our back-annotation. */
7993 if (UI_Lt (uint_size, Uint_0))
7994 return;
7996 /* Only issue an error if a Value_Size clause was explicitly given.
7997 Otherwise, we'd be duplicating an error on the Size clause. */
7998 gnat_attr_node
7999 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8001 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8002 but cannot be represented in bitsizetype. */
8003 size = UI_To_gnu (uint_size, bitsizetype);
8004 if (TREE_OVERFLOW (size))
8006 if (Present (gnat_attr_node))
8007 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8008 gnat_entity);
8009 return;
8012 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8013 exists, or this is an integer type, in which case the front-end will
8014 have always set it. */
8015 if (No (gnat_attr_node)
8016 && integer_zerop (size)
8017 && !Has_Size_Clause (gnat_entity)
8018 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8019 return;
8021 old_size = rm_size (gnu_type);
8023 /* If the old size is self-referential, get the maximum size. */
8024 if (CONTAINS_PLACEHOLDER_P (old_size))
8025 old_size = max_size (old_size, true);
8027 /* Issue an error either if the old size of the object isn't a constant or
8028 if the new size is smaller than it. The front-end has already verified
8029 this for scalar and packed array types. */
8030 if (TREE_CODE (old_size) != INTEGER_CST
8031 || TREE_OVERFLOW (old_size)
8032 || (AGGREGATE_TYPE_P (gnu_type)
8033 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8034 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8035 && !(TYPE_IS_PADDING_P (gnu_type)
8036 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8037 && TYPE_PACKED_ARRAY_TYPE_P
8038 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8039 && tree_int_cst_lt (size, old_size)))
8041 if (Present (gnat_attr_node))
8042 post_error_ne_tree
8043 ("Value_Size for& too small{, minimum allowed is ^}",
8044 gnat_attr_node, gnat_entity, old_size);
8045 return;
8048 /* Otherwise, set the RM size proper for integral types... */
8049 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8050 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8051 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8052 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8053 SET_TYPE_RM_SIZE (gnu_type, size);
8055 /* ...or the Ada size for record and union types. */
8056 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8057 && !TYPE_FAT_POINTER_P (gnu_type))
8058 SET_TYPE_ADA_SIZE (gnu_type, size);
8061 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8062 a type or object whose present alignment is ALIGN. If this alignment is
8063 valid, return it. Otherwise, give an error and return ALIGN. */
8065 static unsigned int
8066 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8068 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8069 unsigned int new_align;
8070 Node_Id gnat_error_node;
8072 /* Don't worry about checking alignment if alignment was not specified
8073 by the source program and we already posted an error for this entity. */
8074 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8075 return align;
8077 /* Post the error on the alignment clause if any. Note, for the implicit
8078 base type of an array type, the alignment clause is on the first
8079 subtype. */
8080 if (Present (Alignment_Clause (gnat_entity)))
8081 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8083 else if (Is_Itype (gnat_entity)
8084 && Is_Array_Type (gnat_entity)
8085 && Etype (gnat_entity) == gnat_entity
8086 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8087 gnat_error_node =
8088 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8090 else
8091 gnat_error_node = gnat_entity;
8093 /* Within GCC, an alignment is an integer, so we must make sure a value is
8094 specified that fits in that range. Also, there is an upper bound to
8095 alignments we can support/allow. */
8096 if (!UI_Is_In_Int_Range (alignment)
8097 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8098 post_error_ne_num ("largest supported alignment for& is ^",
8099 gnat_error_node, gnat_entity, max_allowed_alignment);
8100 else if (!(Present (Alignment_Clause (gnat_entity))
8101 && From_At_Mod (Alignment_Clause (gnat_entity)))
8102 && new_align * BITS_PER_UNIT < align)
8104 unsigned int double_align;
8105 bool is_capped_double, align_clause;
8107 /* If the default alignment of "double" or larger scalar types is
8108 specifically capped and the new alignment is above the cap, do
8109 not post an error and change the alignment only if there is an
8110 alignment clause; this makes it possible to have the associated
8111 GCC type overaligned by default for performance reasons. */
8112 if ((double_align = double_float_alignment) > 0)
8114 Entity_Id gnat_type
8115 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8116 is_capped_double
8117 = is_double_float_or_array (gnat_type, &align_clause);
8119 else if ((double_align = double_scalar_alignment) > 0)
8121 Entity_Id gnat_type
8122 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8123 is_capped_double
8124 = is_double_scalar_or_array (gnat_type, &align_clause);
8126 else
8127 is_capped_double = align_clause = false;
8129 if (is_capped_double && new_align >= double_align)
8131 if (align_clause)
8132 align = new_align * BITS_PER_UNIT;
8134 else
8136 if (is_capped_double)
8137 align = double_align * BITS_PER_UNIT;
8139 post_error_ne_num ("alignment for& must be at least ^",
8140 gnat_error_node, gnat_entity,
8141 align / BITS_PER_UNIT);
8144 else
8146 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8147 if (new_align > align)
8148 align = new_align;
8151 return align;
8154 /* Verify that TYPE is something we can implement atomically. If not, issue
8155 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8156 process a component type. */
8158 static void
8159 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8161 Node_Id gnat_error_point = gnat_entity;
8162 Node_Id gnat_node;
8163 machine_mode mode;
8164 enum mode_class mclass;
8165 unsigned int align;
8166 tree size;
8168 /* If this is an anonymous base type, nothing to check, the error will be
8169 reported on the source type if need be. */
8170 if (!Comes_From_Source (gnat_entity))
8171 return;
8173 mode = TYPE_MODE (type);
8174 mclass = GET_MODE_CLASS (mode);
8175 align = TYPE_ALIGN (type);
8176 size = TYPE_SIZE (type);
8178 /* Consider all aligned floating-point types atomic and any aligned types
8179 that are represented by integers no wider than a machine word. */
8180 if ((mclass == MODE_FLOAT
8181 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8182 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8183 && align >= GET_MODE_ALIGNMENT (mode))
8184 return;
8186 /* For the moment, also allow anything that has an alignment equal to its
8187 size and which is smaller than a word. */
8188 if (size
8189 && TREE_CODE (size) == INTEGER_CST
8190 && compare_tree_int (size, align) == 0
8191 && align <= BITS_PER_WORD)
8192 return;
8194 for (gnat_node = First_Rep_Item (gnat_entity);
8195 Present (gnat_node);
8196 gnat_node = Next_Rep_Item (gnat_node))
8197 if (Nkind (gnat_node) == N_Pragma)
8199 unsigned char pragma_id
8200 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8202 if ((pragma_id == Pragma_Atomic && !component_p)
8203 || (pragma_id == Pragma_Atomic_Components && component_p))
8205 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8206 break;
8210 if (component_p)
8211 post_error_ne ("atomic access to component of & cannot be guaranteed",
8212 gnat_error_point, gnat_entity);
8213 else
8214 post_error_ne ("atomic access to & cannot be guaranteed",
8215 gnat_error_point, gnat_entity);
8219 /* Helper for the intrin compatibility checks family. Evaluate whether
8220 two types are definitely incompatible. */
8222 static bool
8223 intrin_types_incompatible_p (tree t1, tree t2)
8225 enum tree_code code;
8227 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8228 return false;
8230 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8231 return true;
8233 if (TREE_CODE (t1) != TREE_CODE (t2))
8234 return true;
8236 code = TREE_CODE (t1);
8238 switch (code)
8240 case INTEGER_TYPE:
8241 case REAL_TYPE:
8242 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8244 case POINTER_TYPE:
8245 case REFERENCE_TYPE:
8246 /* Assume designated types are ok. We'd need to account for char * and
8247 void * variants to do better, which could rapidly get messy and isn't
8248 clearly worth the effort. */
8249 return false;
8251 default:
8252 break;
8255 return false;
8258 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8259 on the Ada/builtin argument lists for the INB binding. */
8261 static bool
8262 intrin_arglists_compatible_p (intrin_binding_t * inb)
8264 function_args_iterator ada_iter, btin_iter;
8266 function_args_iter_init (&ada_iter, inb->ada_fntype);
8267 function_args_iter_init (&btin_iter, inb->btin_fntype);
8269 /* Sequence position of the last argument we checked. */
8270 int argpos = 0;
8272 while (1)
8274 tree ada_type = function_args_iter_cond (&ada_iter);
8275 tree btin_type = function_args_iter_cond (&btin_iter);
8277 /* If we've exhausted both lists simultaneously, we're done. */
8278 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8279 break;
8281 /* If one list is shorter than the other, they fail to match. */
8282 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8283 return false;
8285 /* If we're done with the Ada args and not with the internal builtin
8286 args, or the other way around, complain. */
8287 if (ada_type == void_type_node
8288 && btin_type != void_type_node)
8290 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8291 return false;
8294 if (btin_type == void_type_node
8295 && ada_type != void_type_node)
8297 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8298 inb->gnat_entity, inb->gnat_entity, argpos);
8299 return false;
8302 /* Otherwise, check that types match for the current argument. */
8303 argpos ++;
8304 if (intrin_types_incompatible_p (ada_type, btin_type))
8306 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8307 inb->gnat_entity, inb->gnat_entity, argpos);
8308 return false;
8312 function_args_iter_next (&ada_iter);
8313 function_args_iter_next (&btin_iter);
8316 return true;
8319 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8320 on the Ada/builtin return values for the INB binding. */
8322 static bool
8323 intrin_return_compatible_p (intrin_binding_t * inb)
8325 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8326 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8328 /* Accept function imported as procedure, common and convenient. */
8329 if (VOID_TYPE_P (ada_return_type)
8330 && !VOID_TYPE_P (btin_return_type))
8331 return true;
8333 /* If return type is Address (integer type), map it to void *. */
8334 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8335 ada_return_type = ptr_void_type_node;
8337 /* Check return types compatibility otherwise. Note that this
8338 handles void/void as well. */
8339 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8341 post_error ("?intrinsic binding type mismatch on return value!",
8342 inb->gnat_entity);
8343 return false;
8346 return true;
8349 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8350 compatible. Issue relevant warnings when they are not.
8352 This is intended as a light check to diagnose the most obvious cases, not
8353 as a full fledged type compatibility predicate. It is the programmer's
8354 responsibility to ensure correctness of the Ada declarations in Imports,
8355 especially when binding straight to a compiler internal. */
8357 static bool
8358 intrin_profiles_compatible_p (intrin_binding_t * inb)
8360 /* Check compatibility on return values and argument lists, each responsible
8361 for posting warnings as appropriate. Ensure use of the proper sloc for
8362 this purpose. */
8364 bool arglists_compatible_p, return_compatible_p;
8365 location_t saved_location = input_location;
8367 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8369 return_compatible_p = intrin_return_compatible_p (inb);
8370 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8372 input_location = saved_location;
8374 return return_compatible_p && arglists_compatible_p;
8377 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8378 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8379 specified size for this field. POS_LIST is a position list describing
8380 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8381 to this layout. */
8383 static tree
8384 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8385 tree size, tree pos_list,
8386 vec<subst_pair> subst_list)
8388 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8389 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8390 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8391 tree new_pos, new_field;
8392 unsigned int i;
8393 subst_pair *s;
8395 if (CONTAINS_PLACEHOLDER_P (pos))
8396 FOR_EACH_VEC_ELT (subst_list, i, s)
8397 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8399 /* If the position is now a constant, we can set it as the position of the
8400 field when we make it. Otherwise, we need to deal with it specially. */
8401 if (TREE_CONSTANT (pos))
8402 new_pos = bit_from_pos (pos, bitpos);
8403 else
8404 new_pos = NULL_TREE;
8406 new_field
8407 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8408 size, new_pos, DECL_PACKED (old_field),
8409 !DECL_NONADDRESSABLE_P (old_field));
8411 if (!new_pos)
8413 normalize_offset (&pos, &bitpos, offset_align);
8414 /* Finalize the position. */
8415 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8416 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8417 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8418 DECL_SIZE (new_field) = size;
8419 DECL_SIZE_UNIT (new_field)
8420 = convert (sizetype,
8421 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8422 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8425 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8426 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8427 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8428 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8430 return new_field;
8433 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8434 it is the minimal size the REP_PART must have. */
8436 static tree
8437 create_rep_part (tree rep_type, tree record_type, tree min_size)
8439 tree field;
8441 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8442 min_size = NULL_TREE;
8444 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8445 min_size, NULL_TREE, 0, 1);
8446 DECL_INTERNAL_P (field) = 1;
8448 return field;
8451 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8453 static tree
8454 get_rep_part (tree record_type)
8456 tree field = TYPE_FIELDS (record_type);
8458 /* The REP part is the first field, internal, another record, and its name
8459 starts with an 'R'. */
8460 if (field
8461 && DECL_INTERNAL_P (field)
8462 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8463 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8464 return field;
8466 return NULL_TREE;
8469 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8471 tree
8472 get_variant_part (tree record_type)
8474 tree field;
8476 /* The variant part is the only internal field that is a qualified union. */
8477 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8478 if (DECL_INTERNAL_P (field)
8479 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8480 return field;
8482 return NULL_TREE;
8485 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8486 the list of variants to be used and RECORD_TYPE is the type of the parent.
8487 POS_LIST is a position list describing the layout of fields present in
8488 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8489 layout. */
8491 static tree
8492 create_variant_part_from (tree old_variant_part,
8493 vec<variant_desc> variant_list,
8494 tree record_type, tree pos_list,
8495 vec<subst_pair> subst_list)
8497 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8498 tree old_union_type = TREE_TYPE (old_variant_part);
8499 tree new_union_type, new_variant_part;
8500 tree union_field_list = NULL_TREE;
8501 variant_desc *v;
8502 unsigned int i;
8504 /* First create the type of the variant part from that of the old one. */
8505 new_union_type = make_node (QUAL_UNION_TYPE);
8506 TYPE_NAME (new_union_type)
8507 = concat_name (TYPE_NAME (record_type),
8508 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8510 /* If the position of the variant part is constant, subtract it from the
8511 size of the type of the parent to get the new size. This manual CSE
8512 reduces the code size when not optimizing. */
8513 if (TREE_CODE (offset) == INTEGER_CST)
8515 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8516 tree first_bit = bit_from_pos (offset, bitpos);
8517 TYPE_SIZE (new_union_type)
8518 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8519 TYPE_SIZE_UNIT (new_union_type)
8520 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8521 byte_from_pos (offset, bitpos));
8522 SET_TYPE_ADA_SIZE (new_union_type,
8523 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8524 first_bit));
8525 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8526 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8528 else
8529 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8531 /* Now finish up the new variants and populate the union type. */
8532 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8534 tree old_field = v->field, new_field;
8535 tree old_variant, old_variant_subpart, new_variant, field_list;
8537 /* Skip variants that don't belong to this nesting level. */
8538 if (DECL_CONTEXT (old_field) != old_union_type)
8539 continue;
8541 /* Retrieve the list of fields already added to the new variant. */
8542 new_variant = v->new_type;
8543 field_list = TYPE_FIELDS (new_variant);
8545 /* If the old variant had a variant subpart, we need to create a new
8546 variant subpart and add it to the field list. */
8547 old_variant = v->type;
8548 old_variant_subpart = get_variant_part (old_variant);
8549 if (old_variant_subpart)
8551 tree new_variant_subpart
8552 = create_variant_part_from (old_variant_subpart, variant_list,
8553 new_variant, pos_list, subst_list);
8554 DECL_CHAIN (new_variant_subpart) = field_list;
8555 field_list = new_variant_subpart;
8558 /* Finish up the new variant and create the field. No need for debug
8559 info thanks to the XVS type. */
8560 finish_record_type (new_variant, nreverse (field_list), 2, false);
8561 compute_record_mode (new_variant);
8562 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8563 Empty);
8565 new_field
8566 = create_field_decl_from (old_field, new_variant, new_union_type,
8567 TYPE_SIZE (new_variant),
8568 pos_list, subst_list);
8569 DECL_QUALIFIER (new_field) = v->qual;
8570 DECL_INTERNAL_P (new_field) = 1;
8571 DECL_CHAIN (new_field) = union_field_list;
8572 union_field_list = new_field;
8575 /* Finish up the union type and create the variant part. No need for debug
8576 info thanks to the XVS type. Note that we don't reverse the field list
8577 because VARIANT_LIST has been traversed in reverse order. */
8578 finish_record_type (new_union_type, union_field_list, 2, false);
8579 compute_record_mode (new_union_type);
8580 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8581 Empty);
8583 new_variant_part
8584 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8585 TYPE_SIZE (new_union_type),
8586 pos_list, subst_list);
8587 DECL_INTERNAL_P (new_variant_part) = 1;
8589 /* With multiple discriminants it is possible for an inner variant to be
8590 statically selected while outer ones are not; in this case, the list
8591 of fields of the inner variant is not flattened and we end up with a
8592 qualified union with a single member. Drop the useless container. */
8593 if (!DECL_CHAIN (union_field_list))
8595 DECL_CONTEXT (union_field_list) = record_type;
8596 DECL_FIELD_OFFSET (union_field_list)
8597 = DECL_FIELD_OFFSET (new_variant_part);
8598 DECL_FIELD_BIT_OFFSET (union_field_list)
8599 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8600 SET_DECL_OFFSET_ALIGN (union_field_list,
8601 DECL_OFFSET_ALIGN (new_variant_part));
8602 new_variant_part = union_field_list;
8605 return new_variant_part;
8608 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8609 which are both RECORD_TYPE, after applying the substitutions described
8610 in SUBST_LIST. */
8612 static void
8613 copy_and_substitute_in_size (tree new_type, tree old_type,
8614 vec<subst_pair> subst_list)
8616 unsigned int i;
8617 subst_pair *s;
8619 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8620 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8621 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8622 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8623 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8625 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8626 FOR_EACH_VEC_ELT (subst_list, i, s)
8627 TYPE_SIZE (new_type)
8628 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8629 s->discriminant, s->replacement);
8631 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8632 FOR_EACH_VEC_ELT (subst_list, i, s)
8633 TYPE_SIZE_UNIT (new_type)
8634 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8635 s->discriminant, s->replacement);
8637 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8638 FOR_EACH_VEC_ELT (subst_list, i, s)
8639 SET_TYPE_ADA_SIZE
8640 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8641 s->discriminant, s->replacement));
8643 /* Finalize the size. */
8644 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8645 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8648 /* Add a parallel type to GNU_TYPE, the translation of GNAT_ENTITY, which is
8649 the implementation type of a packed array type (Is_Packed_Array_Impl_Type).
8650 The parallel type is the original array type if it has been translated. */
8652 static void
8653 add_parallel_type_for_packed_array (tree gnu_type, Entity_Id gnat_entity)
8655 Entity_Id gnat_original_array_type
8656 = Underlying_Type (Original_Array_Type (gnat_entity));
8657 tree gnu_original_array_type;
8659 if (!present_gnu_tree (gnat_original_array_type))
8660 return;
8662 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8664 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8665 return;
8667 add_parallel_type (gnu_type, gnu_original_array_type);
8670 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8671 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8672 updated by replacing F with R.
8674 The function doesn't update the layout of the type, i.e. it assumes
8675 that the substitution is purely formal. That's why the replacement
8676 value R must itself contain a PLACEHOLDER_EXPR. */
8678 tree
8679 substitute_in_type (tree t, tree f, tree r)
8681 tree nt;
8683 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8685 switch (TREE_CODE (t))
8687 case INTEGER_TYPE:
8688 case ENUMERAL_TYPE:
8689 case BOOLEAN_TYPE:
8690 case REAL_TYPE:
8692 /* First the domain types of arrays. */
8693 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8694 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8696 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8697 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8699 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8700 return t;
8702 nt = copy_type (t);
8703 TYPE_GCC_MIN_VALUE (nt) = low;
8704 TYPE_GCC_MAX_VALUE (nt) = high;
8706 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8707 SET_TYPE_INDEX_TYPE
8708 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8710 return nt;
8713 /* Then the subtypes. */
8714 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8715 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8717 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8718 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8720 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8721 return t;
8723 nt = copy_type (t);
8724 SET_TYPE_RM_MIN_VALUE (nt, low);
8725 SET_TYPE_RM_MAX_VALUE (nt, high);
8727 return nt;
8730 return t;
8732 case COMPLEX_TYPE:
8733 nt = substitute_in_type (TREE_TYPE (t), f, r);
8734 if (nt == TREE_TYPE (t))
8735 return t;
8737 return build_complex_type (nt);
8739 case FUNCTION_TYPE:
8740 /* These should never show up here. */
8741 gcc_unreachable ();
8743 case ARRAY_TYPE:
8745 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8746 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8748 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8749 return t;
8751 nt = build_nonshared_array_type (component, domain);
8752 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8753 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8754 SET_TYPE_MODE (nt, TYPE_MODE (t));
8755 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8756 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8757 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8758 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8759 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8760 return nt;
8763 case RECORD_TYPE:
8764 case UNION_TYPE:
8765 case QUAL_UNION_TYPE:
8767 bool changed_field = false;
8768 tree field;
8770 /* Start out with no fields, make new fields, and chain them
8771 in. If we haven't actually changed the type of any field,
8772 discard everything we've done and return the old type. */
8773 nt = copy_type (t);
8774 TYPE_FIELDS (nt) = NULL_TREE;
8776 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8778 tree new_field = copy_node (field), new_n;
8780 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8781 if (new_n != TREE_TYPE (field))
8783 TREE_TYPE (new_field) = new_n;
8784 changed_field = true;
8787 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8788 if (new_n != DECL_FIELD_OFFSET (field))
8790 DECL_FIELD_OFFSET (new_field) = new_n;
8791 changed_field = true;
8794 /* Do the substitution inside the qualifier, if any. */
8795 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8797 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8798 if (new_n != DECL_QUALIFIER (field))
8800 DECL_QUALIFIER (new_field) = new_n;
8801 changed_field = true;
8805 DECL_CONTEXT (new_field) = nt;
8806 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8808 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8809 TYPE_FIELDS (nt) = new_field;
8812 if (!changed_field)
8813 return t;
8815 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8816 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8817 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8818 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8819 return nt;
8822 default:
8823 return t;
8827 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8828 needed to represent the object. */
8830 tree
8831 rm_size (tree gnu_type)
8833 /* For integral types, we store the RM size explicitly. */
8834 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8835 return TYPE_RM_SIZE (gnu_type);
8837 /* Return the RM size of the actual data plus the size of the template. */
8838 if (TREE_CODE (gnu_type) == RECORD_TYPE
8839 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8840 return
8841 size_binop (PLUS_EXPR,
8842 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8843 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8845 /* For record or union types, we store the size explicitly. */
8846 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8847 && !TYPE_FAT_POINTER_P (gnu_type)
8848 && TYPE_ADA_SIZE (gnu_type))
8849 return TYPE_ADA_SIZE (gnu_type);
8851 /* For other types, this is just the size. */
8852 return TYPE_SIZE (gnu_type);
8855 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8856 fully-qualified name, possibly with type information encoding.
8857 Otherwise, return the name. */
8859 tree
8860 get_entity_name (Entity_Id gnat_entity)
8862 Get_Encoded_Name (gnat_entity);
8863 return get_identifier_with_length (Name_Buffer, Name_Len);
8866 /* Return an identifier representing the external name to be used for
8867 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8868 and the specified suffix. */
8870 tree
8871 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8873 const Entity_Kind kind = Ekind (gnat_entity);
8874 const bool has_suffix = (suffix != NULL);
8875 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8876 String_Pointer sp = {suffix, &temp};
8878 Get_External_Name (gnat_entity, has_suffix, sp);
8880 /* A variable using the Stdcall convention lives in a DLL. We adjust
8881 its name to use the jump table, the _imp__NAME contains the address
8882 for the NAME variable. */
8883 if ((kind == E_Variable || kind == E_Constant)
8884 && Has_Stdcall_Convention (gnat_entity))
8886 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8887 char *new_name = (char *) alloca (len + 1);
8888 strcpy (new_name, STDCALL_PREFIX);
8889 strcat (new_name, Name_Buffer);
8890 return get_identifier_with_length (new_name, len);
8893 return get_identifier_with_length (Name_Buffer, Name_Len);
8896 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8897 string, return a new IDENTIFIER_NODE that is the concatenation of
8898 the name followed by "___" and the specified suffix. */
8900 tree
8901 concat_name (tree gnu_name, const char *suffix)
8903 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8904 char *new_name = (char *) alloca (len + 1);
8905 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8906 strcat (new_name, "___");
8907 strcat (new_name, suffix);
8908 return get_identifier_with_length (new_name, len);
8911 /* Initialize data structures of the decl.c module. */
8913 void
8914 init_gnat_decl (void)
8916 /* Initialize the cache of annotated values. */
8917 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
8920 /* Destroy data structures of the decl.c module. */
8922 void
8923 destroy_gnat_decl (void)
8925 /* Destroy the cache of annotated values. */
8926 annotate_value_cache->empty ();
8927 annotate_value_cache = NULL;
8930 #include "gt-ada-decl.h"