* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Constant>: Do not get
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob2eb156b94d09e2edf12266c3f0c22447b5ac0c25
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2018, 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 "target.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
37 #include "demangle.h"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "uintp.h"
48 #include "urealp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
55 /* "stdcall" and "thiscall" conventions should be processed in a specific way
56 on 32-bit x86/Windows only. The macros below are helpers to avoid having
57 to check for a Windows specific attribute throughout this unit. */
59 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #ifdef TARGET_64BIT
61 #define Has_Stdcall_Convention(E) \
62 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
63 #define Has_Thiscall_Convention(E) \
64 (!TARGET_64BIT && is_cplusplus_method (E))
65 #else
66 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
67 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
68 #endif
69 #else
70 #define Has_Stdcall_Convention(E) 0
71 #define Has_Thiscall_Convention(E) 0
72 #endif
74 #define STDCALL_PREFIX "_imp__"
76 /* Stack realignment is necessary for functions with foreign conventions when
77 the ABI doesn't mandate as much as what the compiler assumes - that is, up
78 to PREFERRED_STACK_BOUNDARY.
80 Such realignment can be requested with a dedicated function type attribute
81 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
82 characterize the situations where the attribute should be set. We rely on
83 compiler configuration settings for 'main' to decide. */
85 #ifdef MAIN_STACK_BOUNDARY
86 #define FOREIGN_FORCE_REALIGN_STACK \
87 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
88 #else
89 #define FOREIGN_FORCE_REALIGN_STACK 0
90 #endif
92 struct incomplete
94 struct incomplete *next;
95 tree old_type;
96 Entity_Id full_type;
99 /* These variables are used to defer recursively expanding incomplete types
100 while we are processing a record, an array or a subprogram type. */
101 static int defer_incomplete_level = 0;
102 static struct incomplete *defer_incomplete_list;
104 /* This variable is used to delay expanding types coming from a limited with
105 clause and completed Taft Amendment types until the end of the spec. */
106 static struct incomplete *defer_limited_with_list;
108 typedef struct subst_pair_d {
109 tree discriminant;
110 tree replacement;
111 } subst_pair;
114 typedef struct variant_desc_d {
115 /* The type of the variant. */
116 tree type;
118 /* The associated field. */
119 tree field;
121 /* The value of the qualifier. */
122 tree qual;
124 /* The type of the variant after transformation. */
125 tree new_type;
127 /* The auxiliary data. */
128 tree aux;
129 } variant_desc;
132 /* A map used to cache the result of annotate_value. */
133 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
135 static inline hashval_t
136 hash (tree_int_map *m)
138 return htab_hash_pointer (m->base.from);
141 static inline bool
142 equal (tree_int_map *a, tree_int_map *b)
144 return a->base.from == b->base.from;
147 static int
148 keep_cache_entry (tree_int_map *&m)
150 return ggc_marked_p (m->base.from);
154 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
156 /* A map used to associate a dummy type with a list of subprogram entities. */
157 struct GTY((for_user)) tree_entity_vec_map
159 struct tree_map_base base;
160 vec<Entity_Id, va_gc_atomic> *to;
163 void
164 gt_pch_nx (Entity_Id &)
168 void
169 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
171 op (x, cookie);
174 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
176 static inline hashval_t
177 hash (tree_entity_vec_map *m)
179 return htab_hash_pointer (m->base.from);
182 static inline bool
183 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
185 return a->base.from == b->base.from;
188 static int
189 keep_cache_entry (tree_entity_vec_map *&m)
191 return ggc_marked_p (m->base.from);
195 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
197 static void prepend_one_attribute (struct attrib **,
198 enum attrib_type, tree, tree, Node_Id);
199 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
200 static void prepend_attributes (struct attrib **, Entity_Id);
201 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
202 bool);
203 static bool type_has_variable_size (tree);
204 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
205 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
206 unsigned int);
207 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
208 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
209 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
210 static int adjust_packed (tree, tree, int);
211 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
212 static tree gnu_ext_name_for_subprog (Entity_Id, tree);
213 static tree change_qualified_type (tree, int);
214 static void set_nonaliased_component_on_array_type (tree);
215 static void set_reverse_storage_order_on_array_type (tree);
216 static bool same_discriminant_p (Entity_Id, Entity_Id);
217 static bool array_type_has_nonaliased_component (tree, Entity_Id);
218 static bool compile_time_known_address_p (Node_Id);
219 static bool cannot_be_superflat (Node_Id);
220 static bool constructor_address_p (tree);
221 static bool allocatable_size_p (tree, bool);
222 static bool initial_value_needs_conversion (tree, tree);
223 static int compare_field_bitpos (const PTR, const PTR);
224 static bool components_to_record (Node_Id, Entity_Id, tree, tree, int, bool,
225 bool, bool, bool, bool, bool, bool, tree,
226 tree *);
227 static Uint annotate_value (tree);
228 static void annotate_rep (Entity_Id, tree);
229 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
230 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
231 static vec<variant_desc> build_variant_list (tree, vec<subst_pair>,
232 vec<variant_desc>);
233 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
234 static void set_rm_size (Uint, tree, Entity_Id);
235 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
236 static unsigned int promote_object_alignment (tree, Entity_Id);
237 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
238 static tree create_field_decl_from (tree, tree, tree, tree, tree,
239 vec<subst_pair>);
240 static tree create_rep_part (tree, tree, tree);
241 static tree get_rep_part (tree);
242 static tree create_variant_part_from (tree, vec<variant_desc>, tree,
243 tree, vec<subst_pair>, bool);
244 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair>);
245 static void copy_and_substitute_in_layout (Entity_Id, Entity_Id, tree, tree,
246 vec<subst_pair>, bool);
247 static void associate_original_type_to_packed_array (tree, Entity_Id);
248 static const char *get_entity_char (Entity_Id);
250 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
251 to pass around calls performing profile compatibility checks. */
253 typedef struct {
254 Entity_Id gnat_entity; /* The Ada subprogram entity. */
255 tree ada_fntype; /* The corresponding GCC type node. */
256 tree btin_fntype; /* The GCC builtin function type node. */
257 } intrin_binding_t;
259 static bool intrin_profiles_compatible_p (intrin_binding_t *);
261 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
262 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
263 and associate the ..._DECL node with the input GNAT defining identifier.
265 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
266 initial value (in GCC tree form). This is optional for a variable. For
267 a renamed entity, GNU_EXPR gives the object being renamed.
269 DEFINITION is true if this call is intended for a definition. This is used
270 for separate compilation where it is necessary to know whether an external
271 declaration or a definition must be created if the GCC equivalent was not
272 created previously. */
274 tree
275 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
277 /* Contains the kind of the input GNAT node. */
278 const Entity_Kind kind = Ekind (gnat_entity);
279 /* True if this is a type. */
280 const bool is_type = IN (kind, Type_Kind);
281 /* True if this is an artificial entity. */
282 const bool artificial_p = !Comes_From_Source (gnat_entity);
283 /* True if debug info is requested for this entity. */
284 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
285 /* True if this entity is to be considered as imported. */
286 const bool imported_p
287 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
288 /* True if this entity has a foreign convention. */
289 const bool foreign = Has_Foreign_Convention (gnat_entity);
290 /* For a type, contains the equivalent GNAT node to be used in gigi. */
291 Entity_Id gnat_equiv_type = Empty;
292 /* Temporary used to walk the GNAT tree. */
293 Entity_Id gnat_temp;
294 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
295 This node will be associated with the GNAT node by calling at the end
296 of the `switch' statement. */
297 tree gnu_decl = NULL_TREE;
298 /* Contains the GCC type to be used for the GCC node. */
299 tree gnu_type = NULL_TREE;
300 /* Contains the GCC size tree to be used for the GCC node. */
301 tree gnu_size = NULL_TREE;
302 /* Contains the GCC name to be used for the GCC node. */
303 tree gnu_entity_name;
304 /* True if we have already saved gnu_decl as a GNAT association. */
305 bool saved = false;
306 /* True if we incremented defer_incomplete_level. */
307 bool this_deferred = false;
308 /* True if we incremented force_global. */
309 bool this_global = false;
310 /* True if we should check to see if elaborated during processing. */
311 bool maybe_present = false;
312 /* True if we made GNU_DECL and its type here. */
313 bool this_made_decl = false;
314 /* Size and alignment of the GCC node, if meaningful. */
315 unsigned int esize = 0, align = 0;
316 /* Contains the list of attributes directly attached to the entity. */
317 struct attrib *attr_list = NULL;
319 /* Since a use of an Itype is a definition, process it as such if it is in
320 the main unit, except for E_Access_Subtype because it's actually a use
321 of its base type, and for E_Record_Subtype with cloned subtype because
322 it's actually a use of the cloned subtype, see below. */
323 if (!definition
324 && is_type
325 && Is_Itype (gnat_entity)
326 && !(kind == E_Access_Subtype
327 || (kind == E_Record_Subtype
328 && Present (Cloned_Subtype (gnat_entity))))
329 && !present_gnu_tree (gnat_entity)
330 && In_Extended_Main_Code_Unit (gnat_entity))
332 /* Ensure that we are in a subprogram mentioned in the Scope chain of
333 this entity, our current scope is global, or we encountered a task
334 or entry (where we can't currently accurately check scoping). */
335 if (!current_function_decl
336 || DECL_ELABORATION_PROC_P (current_function_decl))
338 process_type (gnat_entity);
339 return get_gnu_tree (gnat_entity);
342 for (gnat_temp = Scope (gnat_entity);
343 Present (gnat_temp);
344 gnat_temp = Scope (gnat_temp))
346 if (Is_Type (gnat_temp))
347 gnat_temp = Underlying_Type (gnat_temp);
349 if (Ekind (gnat_temp) == E_Subprogram_Body)
350 gnat_temp
351 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
353 if (Is_Subprogram (gnat_temp)
354 && Present (Protected_Body_Subprogram (gnat_temp)))
355 gnat_temp = Protected_Body_Subprogram (gnat_temp);
357 if (Ekind (gnat_temp) == E_Entry
358 || Ekind (gnat_temp) == E_Entry_Family
359 || Ekind (gnat_temp) == E_Task_Type
360 || (Is_Subprogram (gnat_temp)
361 && present_gnu_tree (gnat_temp)
362 && (current_function_decl
363 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
365 process_type (gnat_entity);
366 return get_gnu_tree (gnat_entity);
370 /* This abort means the Itype has an incorrect scope, i.e. that its
371 scope does not correspond to the subprogram it is declared in. */
372 gcc_unreachable ();
375 /* If we've already processed this entity, return what we got last time.
376 If we are defining the node, we should not have already processed it.
377 In that case, we will abort below when we try to save a new GCC tree
378 for this object. We also need to handle the case of getting a dummy
379 type when a Full_View exists but be careful so as not to trigger its
380 premature elaboration. */
381 if ((!definition || (is_type && imported_p))
382 && present_gnu_tree (gnat_entity))
384 gnu_decl = get_gnu_tree (gnat_entity);
386 if (TREE_CODE (gnu_decl) == TYPE_DECL
387 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
388 && IN (kind, Incomplete_Or_Private_Kind)
389 && Present (Full_View (gnat_entity))
390 && (present_gnu_tree (Full_View (gnat_entity))
391 || No (Freeze_Node (Full_View (gnat_entity)))))
393 gnu_decl
394 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, false);
395 save_gnu_tree (gnat_entity, NULL_TREE, false);
396 save_gnu_tree (gnat_entity, gnu_decl, false);
399 return gnu_decl;
402 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
403 must be specified unless it was specified by the programmer. Exceptions
404 are for access-to-protected-subprogram types and all access subtypes, as
405 another GNAT type is used to lay out the GCC type for them. */
406 gcc_assert (!is_type
407 || Known_Esize (gnat_entity)
408 || Has_Size_Clause (gnat_entity)
409 || (!IN (kind, Numeric_Kind)
410 && !IN (kind, Enumeration_Kind)
411 && (!IN (kind, Access_Kind)
412 || kind == E_Access_Protected_Subprogram_Type
413 || kind == E_Anonymous_Access_Protected_Subprogram_Type
414 || kind == E_Access_Subtype
415 || type_annotate_only)));
417 /* The RM size must be specified for all discrete and fixed-point types. */
418 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
419 && Unknown_RM_Size (gnat_entity)));
421 /* If we get here, it means we have not yet done anything with this entity.
422 If we are not defining it, it must be a type or an entity that is defined
423 elsewhere or externally, otherwise we should have defined it already. */
424 gcc_assert (definition
425 || type_annotate_only
426 || is_type
427 || kind == E_Discriminant
428 || kind == E_Component
429 || kind == E_Label
430 || (kind == E_Constant && Present (Full_View (gnat_entity)))
431 || Is_Public (gnat_entity));
433 /* Get the name of the entity and set up the line number and filename of
434 the original definition for use in any decl we make. Make sure we do not
435 inherit another source location. */
436 gnu_entity_name = get_entity_name (gnat_entity);
437 if (Sloc (gnat_entity) != No_Location
438 && !renaming_from_instantiation_p (gnat_entity))
439 Sloc_to_locus (Sloc (gnat_entity), &input_location);
441 /* For cases when we are not defining (i.e., we are referencing from
442 another compilation unit) public entities, show we are at global level
443 for the purpose of computing scopes. Don't do this for components or
444 discriminants since the relevant test is whether or not the record is
445 being defined. */
446 if (!definition
447 && kind != E_Component
448 && kind != E_Discriminant
449 && Is_Public (gnat_entity)
450 && !Is_Statically_Allocated (gnat_entity))
451 force_global++, this_global = true;
453 /* Handle any attributes directly attached to the entity. */
454 if (Has_Gigi_Rep_Item (gnat_entity))
455 prepend_attributes (&attr_list, gnat_entity);
457 /* Do some common processing for types. */
458 if (is_type)
460 /* Compute the equivalent type to be used in gigi. */
461 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
463 /* Machine_Attributes on types are expected to be propagated to
464 subtypes. The corresponding Gigi_Rep_Items are only attached
465 to the first subtype though, so we handle the propagation here. */
466 if (Base_Type (gnat_entity) != gnat_entity
467 && !Is_First_Subtype (gnat_entity)
468 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
469 prepend_attributes (&attr_list,
470 First_Subtype (Base_Type (gnat_entity)));
472 /* Compute a default value for the size of an elementary type. */
473 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
475 unsigned int max_esize;
477 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
478 esize = UI_To_Int (Esize (gnat_entity));
480 if (IN (kind, Float_Kind))
481 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
482 else if (IN (kind, Access_Kind))
483 max_esize = POINTER_SIZE * 2;
484 else
485 max_esize = LONG_LONG_TYPE_SIZE;
487 if (esize > max_esize)
488 esize = max_esize;
492 switch (kind)
494 case E_Component:
495 case E_Discriminant:
497 /* The GNAT record where the component was defined. */
498 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
500 /* If the entity is a discriminant of an extended tagged type used to
501 rename a discriminant of the parent type, return the latter. */
502 if (kind == E_Discriminant
503 && Present (Corresponding_Discriminant (gnat_entity))
504 && Is_Tagged_Type (gnat_record))
506 gnu_decl
507 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
508 gnu_expr, definition);
509 saved = true;
510 break;
513 /* If the entity is an inherited component (in the case of extended
514 tagged record types), just return the original entity, which must
515 be a FIELD_DECL. Likewise for discriminants. If the entity is a
516 non-girder discriminant (in the case of derived untagged record
517 types), return the stored discriminant it renames. */
518 if (Present (Original_Record_Component (gnat_entity))
519 && Original_Record_Component (gnat_entity) != gnat_entity)
521 gnu_decl
522 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
523 gnu_expr, definition);
524 /* GNU_DECL contains a PLACEHOLDER_EXPR for discriminants. */
525 if (kind == E_Discriminant)
526 saved = true;
527 break;
530 /* Otherwise, if we are not defining this and we have no GCC type
531 for the containing record, make one for it. Then we should
532 have made our own equivalent. */
533 if (!definition && !present_gnu_tree (gnat_record))
535 /* ??? If this is in a record whose scope is a protected
536 type and we have an Original_Record_Component, use it.
537 This is a workaround for major problems in protected type
538 handling. */
539 Entity_Id Scop = Scope (Scope (gnat_entity));
540 if (Is_Protected_Type (Underlying_Type (Scop))
541 && Present (Original_Record_Component (gnat_entity)))
543 gnu_decl
544 = gnat_to_gnu_entity (Original_Record_Component
545 (gnat_entity),
546 gnu_expr, false);
548 else
550 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
551 gnu_decl = get_gnu_tree (gnat_entity);
554 saved = true;
555 break;
558 /* Here we have no GCC type and this is a reference rather than a
559 definition. This should never happen. Most likely the cause is
560 reference before declaration in the GNAT tree for gnat_entity. */
561 gcc_unreachable ();
564 case E_Constant:
565 /* Ignore constant definitions already marked with the error node. See
566 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
567 if (definition
568 && present_gnu_tree (gnat_entity)
569 && get_gnu_tree (gnat_entity) == error_mark_node)
571 maybe_present = true;
572 break;
575 /* Ignore deferred constant definitions without address clause since
576 they are processed fully in the front-end. If No_Initialization
577 is set, this is not a deferred constant but a constant whose value
578 is built manually. And constants that are renamings are handled
579 like variables. */
580 if (definition
581 && !gnu_expr
582 && No (Address_Clause (gnat_entity))
583 && !No_Initialization (Declaration_Node (gnat_entity))
584 && No (Renamed_Object (gnat_entity)))
586 gnu_decl = error_mark_node;
587 saved = true;
588 break;
591 /* If this is a use of a deferred constant without address clause,
592 get its full definition. */
593 if (!definition
594 && No (Address_Clause (gnat_entity))
595 && Present (Full_View (gnat_entity)))
597 gnu_decl
598 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
599 saved = true;
600 break;
603 /* If we have a constant that we are not defining, get the expression it
604 was defined to represent. This is necessary to avoid generating dumb
605 elaboration code in simple cases, but we may throw it away later if it
606 is not a constant. But do not do it for dispatch tables because they
607 are only referenced indirectly and we need to have a consistent view
608 of the exported and of the imported declarations of the tables from
609 external units for them to be properly merged in LTO mode. Moreover
610 simply do not retrieve the expression it if it is an allocator since
611 the designated type might still be dummy at this point. Note that we
612 invoke gnat_to_gnu_external and not gnat_to_gnu because the expression
613 may contain N_Expression_With_Actions nodes and thus declarations of
614 objects from other units that we need to discard. */
615 if (!definition
616 && !No_Initialization (Declaration_Node (gnat_entity))
617 && !Is_Dispatch_Table_Entity (gnat_entity)
618 && Present (gnat_temp = Expression (Declaration_Node (gnat_entity)))
619 && Nkind (gnat_temp) != N_Allocator
620 && (!type_annotate_only || Compile_Time_Known_Value (gnat_temp)))
621 gnu_expr = gnat_to_gnu_external (gnat_temp);
623 /* ... fall through ... */
625 case E_Exception:
626 case E_Loop_Parameter:
627 case E_Out_Parameter:
628 case E_Variable:
630 const Entity_Id gnat_type = Etype (gnat_entity);
631 /* Always create a variable for volatile objects and variables seen
632 constant but with a Linker_Section pragma. */
633 bool const_flag
634 = ((kind == E_Constant || kind == E_Variable)
635 && Is_True_Constant (gnat_entity)
636 && !(kind == E_Variable
637 && Present (Linker_Section_Pragma (gnat_entity)))
638 && !Treat_As_Volatile (gnat_entity)
639 && (((Nkind (Declaration_Node (gnat_entity))
640 == N_Object_Declaration)
641 && Present (Expression (Declaration_Node (gnat_entity))))
642 || Present (Renamed_Object (gnat_entity))
643 || imported_p));
644 bool inner_const_flag = const_flag;
645 bool static_flag = Is_Statically_Allocated (gnat_entity);
646 /* We implement RM 13.3(19) for exported and imported (non-constant)
647 objects by making them volatile. */
648 bool volatile_flag
649 = (Treat_As_Volatile (gnat_entity)
650 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
651 bool mutable_p = false;
652 bool used_by_ref = false;
653 tree gnu_ext_name = NULL_TREE;
654 tree renamed_obj = NULL_TREE;
655 tree gnu_object_size;
657 /* We need to translate the renamed object even though we are only
658 referencing the renaming. But it may contain a call for which
659 we'll generate a temporary to hold the return value and which
660 is part of the definition of the renaming, so discard it. */
661 if (Present (Renamed_Object (gnat_entity)) && !definition)
663 if (kind == E_Exception)
664 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
665 NULL_TREE, false);
666 else
667 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
670 /* Get the type after elaborating the renamed object. */
671 if (foreign && Is_Descendant_Of_Address (Underlying_Type (gnat_type)))
672 gnu_type = ptr_type_node;
673 else
675 gnu_type = gnat_to_gnu_type (gnat_type);
677 /* If this is a standard exception definition, use the standard
678 exception type. This is necessary to make sure that imported
679 and exported views of exceptions are merged in LTO mode. */
680 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
681 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
682 gnu_type = except_type_node;
685 /* For a debug renaming declaration, build a debug-only entity. */
686 if (Present (Debug_Renaming_Link (gnat_entity)))
688 /* Force a non-null value to make sure the symbol is retained. */
689 tree value = build1 (INDIRECT_REF, gnu_type,
690 build1 (NOP_EXPR,
691 build_pointer_type (gnu_type),
692 integer_minus_one_node));
693 gnu_decl = build_decl (input_location,
694 VAR_DECL, gnu_entity_name, gnu_type);
695 SET_DECL_VALUE_EXPR (gnu_decl, value);
696 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
697 TREE_STATIC (gnu_decl) = global_bindings_p ();
698 gnat_pushdecl (gnu_decl, gnat_entity);
699 break;
702 /* If this is a loop variable, its type should be the base type.
703 This is because the code for processing a loop determines whether
704 a normal loop end test can be done by comparing the bounds of the
705 loop against those of the base type, which is presumed to be the
706 size used for computation. But this is not correct when the size
707 of the subtype is smaller than the type. */
708 if (kind == E_Loop_Parameter)
709 gnu_type = get_base_type (gnu_type);
711 /* Reject non-renamed objects whose type is an unconstrained array or
712 any object whose type is a dummy type or void. */
713 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
714 && No (Renamed_Object (gnat_entity)))
715 || TYPE_IS_DUMMY_P (gnu_type)
716 || TREE_CODE (gnu_type) == VOID_TYPE)
718 gcc_assert (type_annotate_only);
719 if (this_global)
720 force_global--;
721 return error_mark_node;
724 /* If an alignment is specified, use it if valid. Note that exceptions
725 are objects but don't have an alignment and there is also no point in
726 setting it for an address clause, since the final type of the object
727 will be a reference type. */
728 if (Known_Alignment (gnat_entity)
729 && kind != E_Exception
730 && No (Address_Clause (gnat_entity)))
731 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
732 TYPE_ALIGN (gnu_type));
734 /* Likewise, if a size is specified, use it if valid. */
735 if (Known_Esize (gnat_entity))
736 gnu_size
737 = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
738 VAR_DECL, false, Has_Size_Clause (gnat_entity));
739 if (gnu_size)
741 gnu_type
742 = make_type_from_size (gnu_type, gnu_size,
743 Has_Biased_Representation (gnat_entity));
745 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
746 gnu_size = NULL_TREE;
749 /* If this object has self-referential size, it must be a record with
750 a default discriminant. We are supposed to allocate an object of
751 the maximum size in this case, unless it is a constant with an
752 initializing expression, in which case we can get the size from
753 that. Note that the resulting size may still be a variable, so
754 this may end up with an indirect allocation. */
755 if (No (Renamed_Object (gnat_entity))
756 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
758 if (gnu_expr && kind == E_Constant)
760 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
761 if (CONTAINS_PLACEHOLDER_P (size))
763 /* If the initializing expression is itself a constant,
764 despite having a nominal type with self-referential
765 size, we can get the size directly from it. */
766 if (TREE_CODE (gnu_expr) == COMPONENT_REF
767 && TYPE_IS_PADDING_P
768 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
769 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
770 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
771 || DECL_READONLY_ONCE_ELAB
772 (TREE_OPERAND (gnu_expr, 0))))
773 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
774 else
775 gnu_size
776 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
778 else
779 gnu_size = size;
781 /* We may have no GNU_EXPR because No_Initialization is
782 set even though there's an Expression. */
783 else if (kind == E_Constant
784 && (Nkind (Declaration_Node (gnat_entity))
785 == N_Object_Declaration)
786 && Present (Expression (Declaration_Node (gnat_entity))))
787 gnu_size
788 = TYPE_SIZE (gnat_to_gnu_type
789 (Etype
790 (Expression (Declaration_Node (gnat_entity)))));
791 else
793 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
794 mutable_p = true;
797 /* If the size isn't constant and we are at global level, call
798 elaborate_expression_1 to make a variable for it rather than
799 calculating it each time. */
800 if (!TREE_CONSTANT (gnu_size) && global_bindings_p ())
801 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
802 "SIZE", definition, false);
805 /* If the size is zero byte, make it one byte since some linkers have
806 troubles with zero-sized objects. If the object will have a
807 template, that will make it nonzero so don't bother. Also avoid
808 doing that for an object renaming or an object with an address
809 clause, as we would lose useful information on the view size
810 (e.g. for null array slices) and we are not allocating the object
811 here anyway. */
812 if (((gnu_size
813 && integer_zerop (gnu_size)
814 && !TREE_OVERFLOW (gnu_size))
815 || (TYPE_SIZE (gnu_type)
816 && integer_zerop (TYPE_SIZE (gnu_type))
817 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
818 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
819 && No (Renamed_Object (gnat_entity))
820 && No (Address_Clause (gnat_entity)))
821 gnu_size = bitsize_unit_node;
823 /* If this is an object with no specified size and alignment, and
824 if either it is atomic or we are not optimizing alignment for
825 space and it is composite and not an exception, an Out parameter
826 or a reference to another object, and the size of its type is a
827 constant, set the alignment to the smallest one which is not
828 smaller than the size, with an appropriate cap. */
829 if (!gnu_size && align == 0
830 && (Is_Atomic_Or_VFA (gnat_entity)
831 || (!Optimize_Alignment_Space (gnat_entity)
832 && kind != E_Exception
833 && kind != E_Out_Parameter
834 && Is_Composite_Type (gnat_type)
835 && !Is_Constr_Subt_For_UN_Aliased (gnat_type)
836 && !Is_Exported (gnat_entity)
837 && !imported_p
838 && No (Renamed_Object (gnat_entity))
839 && No (Address_Clause (gnat_entity))))
840 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
841 align = promote_object_alignment (gnu_type, gnat_entity);
843 /* If the object is set to have atomic components, find the component
844 type and validate it.
846 ??? Note that we ignore Has_Volatile_Components on objects; it's
847 not at all clear what to do in that case. */
848 if (Has_Atomic_Components (gnat_entity))
850 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
851 ? TREE_TYPE (gnu_type) : gnu_type);
853 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
854 && TYPE_MULTI_ARRAY_P (gnu_inner))
855 gnu_inner = TREE_TYPE (gnu_inner);
857 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
860 /* If this is an aliased object with an unconstrained array nominal
861 subtype, make a type that includes the template. We will either
862 allocate or create a variable of that type, see below. */
863 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
864 && Is_Array_Type (Underlying_Type (gnat_type))
865 && !type_annotate_only)
867 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
868 gnu_type
869 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
870 gnu_type,
871 concat_name (gnu_entity_name,
872 "UNC"),
873 debug_info_p);
876 /* ??? If this is an object of CW type initialized to a value, try to
877 ensure that the object is sufficient aligned for this value, but
878 without pessimizing the allocation. This is a kludge necessary
879 because we don't support dynamic alignment. */
880 if (align == 0
881 && Ekind (gnat_type) == E_Class_Wide_Subtype
882 && No (Renamed_Object (gnat_entity))
883 && No (Address_Clause (gnat_entity)))
884 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
886 #ifdef MINIMUM_ATOMIC_ALIGNMENT
887 /* If the size is a constant and no alignment is specified, force
888 the alignment to be the minimum valid atomic alignment. The
889 restriction on constant size avoids problems with variable-size
890 temporaries; if the size is variable, there's no issue with
891 atomic access. Also don't do this for a constant, since it isn't
892 necessary and can interfere with constant replacement. Finally,
893 do not do it for Out parameters since that creates an
894 size inconsistency with In parameters. */
895 if (align == 0
896 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
897 && !FLOAT_TYPE_P (gnu_type)
898 && !const_flag && No (Renamed_Object (gnat_entity))
899 && !imported_p && No (Address_Clause (gnat_entity))
900 && kind != E_Out_Parameter
901 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
902 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
903 align = MINIMUM_ATOMIC_ALIGNMENT;
904 #endif
906 /* Make a new type with the desired size and alignment, if needed.
907 But do not take into account alignment promotions to compute the
908 size of the object. */
909 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
910 if (gnu_size || align > 0)
912 tree orig_type = gnu_type;
914 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
915 false, false, definition, true);
917 /* If a padding record was made, declare it now since it will
918 never be declared otherwise. This is necessary to ensure
919 that its subtrees are properly marked. */
920 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
921 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
922 debug_info_p, gnat_entity);
925 /* Now check if the type of the object allows atomic access. */
926 if (Is_Atomic_Or_VFA (gnat_entity))
927 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
929 /* If this is a renaming, avoid as much as possible to create a new
930 object. However, in some cases, creating it is required because
931 renaming can be applied to objects that are not names in Ada.
932 This processing needs to be applied to the raw expression so as
933 to make it more likely to rename the underlying object. */
934 if (Present (Renamed_Object (gnat_entity)))
936 /* If the renamed object had padding, strip off the reference to
937 the inner object and reset our type. */
938 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
939 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
940 /* Strip useless conversions around the object. */
941 || gnat_useless_type_conversion (gnu_expr))
943 gnu_expr = TREE_OPERAND (gnu_expr, 0);
944 gnu_type = TREE_TYPE (gnu_expr);
947 /* Or else, if the renamed object has an unconstrained type with
948 default discriminant, use the padded type. */
949 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
950 gnu_type = TREE_TYPE (gnu_expr);
952 /* Case 1: if this is a constant renaming stemming from a function
953 call, treat it as a normal object whose initial value is what
954 is being renamed. RM 3.3 says that the result of evaluating a
955 function call is a constant object. Therefore, it can be the
956 inner object of a constant renaming and the renaming must be
957 fully instantiated, i.e. it cannot be a reference to (part of)
958 an existing object. And treat other rvalues (addresses, null
959 expressions, constructors and literals) the same way. */
960 tree inner = gnu_expr;
961 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
962 inner = TREE_OPERAND (inner, 0);
963 /* Expand_Dispatching_Call can prepend a comparison of the tags
964 before the call to "=". */
965 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
966 || TREE_CODE (inner) == COMPOUND_EXPR)
967 inner = TREE_OPERAND (inner, 1);
968 if ((TREE_CODE (inner) == CALL_EXPR
969 && !call_is_atomic_load (inner))
970 || TREE_CODE (inner) == ADDR_EXPR
971 || TREE_CODE (inner) == NULL_EXPR
972 || TREE_CODE (inner) == PLUS_EXPR
973 || TREE_CODE (inner) == CONSTRUCTOR
974 || CONSTANT_CLASS_P (inner)
975 /* We need to detect the case where a temporary is created to
976 hold the return value, since we cannot safely rename it at
977 top level as it lives only in the elaboration routine. */
978 || (TREE_CODE (inner) == VAR_DECL
979 && DECL_RETURN_VALUE_P (inner))
980 /* We also need to detect the case where the front-end creates
981 a dangling 'reference to a function call at top level and
982 substitutes it in the renaming, for example:
984 q__b : boolean renames r__f.e (1);
986 can be rewritten into:
988 q__R1s : constant q__A2s := r__f'reference;
989 [...]
990 q__b : boolean renames q__R1s.all.e (1);
992 We cannot safely rename the rewritten expression since the
993 underlying object lives only in the elaboration routine. */
994 || (TREE_CODE (inner) == INDIRECT_REF
995 && (inner
996 = remove_conversions (TREE_OPERAND (inner, 0), true))
997 && TREE_CODE (inner) == VAR_DECL
998 && DECL_RETURN_VALUE_P (inner)))
1001 /* Case 2: if the renaming entity need not be materialized, use
1002 the elaborated renamed expression for the renaming. But this
1003 means that the caller is responsible for evaluating the address
1004 of the renaming in the correct place for the definition case to
1005 instantiate the SAVE_EXPRs. */
1006 else if (!Materialize_Entity (gnat_entity))
1008 tree init = NULL_TREE;
1010 gnu_decl
1011 = elaborate_reference (gnu_expr, gnat_entity, definition,
1012 &init);
1014 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1015 correct place for this case. */
1016 gcc_assert (!init);
1018 /* No DECL_EXPR will be created so the expression needs to be
1019 marked manually because it will likely be shared. */
1020 if (global_bindings_p ())
1021 MARK_VISITED (gnu_decl);
1023 /* This assertion will fail if the renamed object isn't aligned
1024 enough as to make it possible to honor the alignment set on
1025 the renaming. */
1026 if (align)
1028 unsigned int ralign = DECL_P (gnu_decl)
1029 ? DECL_ALIGN (gnu_decl)
1030 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1031 gcc_assert (ralign >= align);
1034 /* The expression might not be a DECL so save it manually. */
1035 save_gnu_tree (gnat_entity, gnu_decl, true);
1036 saved = true;
1037 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1038 break;
1041 /* Case 3: otherwise, make a constant pointer to the object we
1042 are renaming and attach the object to the pointer after it is
1043 elaborated. The object will be referenced directly instead
1044 of indirectly via the pointer to avoid aliasing problems with
1045 non-addressable entities. The pointer is called a "renaming"
1046 pointer in this case. Note that we also need to preserve the
1047 volatility of the renamed object through the indirection. */
1048 else
1050 tree init = NULL_TREE;
1052 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1053 gnu_type
1054 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1055 gnu_type = build_reference_type (gnu_type);
1056 used_by_ref = true;
1057 const_flag = true;
1058 volatile_flag = false;
1059 inner_const_flag = TREE_READONLY (gnu_expr);
1060 gnu_size = NULL_TREE;
1062 renamed_obj
1063 = elaborate_reference (gnu_expr, gnat_entity, definition,
1064 &init);
1066 /* The expression needs to be marked manually because it will
1067 likely be shared, even for a definition since the ADDR_EXPR
1068 built below can cause the first few nodes to be folded. */
1069 if (global_bindings_p ())
1070 MARK_VISITED (renamed_obj);
1072 if (type_annotate_only
1073 && TREE_CODE (renamed_obj) == ERROR_MARK)
1074 gnu_expr = NULL_TREE;
1075 else
1077 gnu_expr
1078 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1079 if (init)
1080 gnu_expr
1081 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1082 gnu_expr);
1087 /* If we are defining an aliased object whose nominal subtype is
1088 unconstrained, the object is a record that contains both the
1089 template and the object. If there is an initializer, it will
1090 have already been converted to the right type, but we need to
1091 create the template if there is no initializer. */
1092 if (definition
1093 && !gnu_expr
1094 && TREE_CODE (gnu_type) == RECORD_TYPE
1095 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1096 /* Beware that padding might have been introduced above. */
1097 || (TYPE_PADDING_P (gnu_type)
1098 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1099 == RECORD_TYPE
1100 && TYPE_CONTAINS_TEMPLATE_P
1101 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1103 tree template_field
1104 = TYPE_PADDING_P (gnu_type)
1105 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1106 : TYPE_FIELDS (gnu_type);
1107 vec<constructor_elt, va_gc> *v;
1108 vec_alloc (v, 1);
1109 tree t = build_template (TREE_TYPE (template_field),
1110 TREE_TYPE (DECL_CHAIN (template_field)),
1111 NULL_TREE);
1112 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1113 gnu_expr = gnat_build_constructor (gnu_type, v);
1116 /* Convert the expression to the type of the object if need be. */
1117 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1118 gnu_expr = convert (gnu_type, gnu_expr);
1120 /* If this is a pointer that doesn't have an initializing expression,
1121 initialize it to NULL, unless the object is declared imported as
1122 per RM B.1(24). */
1123 if (definition
1124 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1125 && !gnu_expr
1126 && !Is_Imported (gnat_entity))
1127 gnu_expr = integer_zero_node;
1129 /* If we are defining the object and it has an Address clause, we must
1130 either get the address expression from the saved GCC tree for the
1131 object if it has a Freeze node, or elaborate the address expression
1132 here since the front-end has guaranteed that the elaboration has no
1133 effects in this case. */
1134 if (definition && Present (Address_Clause (gnat_entity)))
1136 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1137 Node_Id gnat_address = Expression (gnat_clause);
1138 tree gnu_address
1139 = present_gnu_tree (gnat_entity)
1140 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1142 save_gnu_tree (gnat_entity, NULL_TREE, false);
1144 /* Convert the type of the object to a reference type that can
1145 alias everything as per RM 13.3(19). */
1146 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1147 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1148 gnu_type
1149 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1150 gnu_address = convert (gnu_type, gnu_address);
1151 used_by_ref = true;
1152 const_flag
1153 = (!Is_Public (gnat_entity)
1154 || compile_time_known_address_p (gnat_address));
1155 volatile_flag = false;
1156 gnu_size = NULL_TREE;
1158 /* If this is an aliased object with an unconstrained array nominal
1159 subtype, then it can overlay only another aliased object with an
1160 unconstrained array nominal subtype and compatible template. */
1161 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1162 && Is_Array_Type (Underlying_Type (gnat_type))
1163 && !type_annotate_only)
1165 tree rec_type = TREE_TYPE (gnu_type);
1166 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1168 /* This is the pattern built for a regular object. */
1169 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1170 && TREE_OPERAND (gnu_address, 1) == off)
1171 gnu_address = TREE_OPERAND (gnu_address, 0);
1172 /* This is the pattern built for an overaligned object. */
1173 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1174 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1175 == PLUS_EXPR
1176 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1177 == off)
1178 gnu_address
1179 = build2 (POINTER_PLUS_EXPR, gnu_type,
1180 TREE_OPERAND (gnu_address, 0),
1181 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1182 else
1184 post_error_ne ("aliased object& with unconstrained array "
1185 "nominal subtype", gnat_clause,
1186 gnat_entity);
1187 post_error ("\\can overlay only aliased object with "
1188 "compatible subtype", gnat_clause);
1192 /* If we don't have an initializing expression for the underlying
1193 variable, the initializing expression for the pointer is the
1194 specified address. Otherwise, we have to make a COMPOUND_EXPR
1195 to assign both the address and the initial value. */
1196 if (!gnu_expr)
1197 gnu_expr = gnu_address;
1198 else
1199 gnu_expr
1200 = build2 (COMPOUND_EXPR, gnu_type,
1201 build_binary_op (INIT_EXPR, NULL_TREE,
1202 build_unary_op (INDIRECT_REF,
1203 NULL_TREE,
1204 gnu_address),
1205 gnu_expr),
1206 gnu_address);
1209 /* If it has an address clause and we are not defining it, mark it
1210 as an indirect object. Likewise for Stdcall objects that are
1211 imported. */
1212 if ((!definition && Present (Address_Clause (gnat_entity)))
1213 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1215 /* Convert the type of the object to a reference type that can
1216 alias everything as per RM 13.3(19). */
1217 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1218 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1219 gnu_type
1220 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1221 used_by_ref = true;
1222 const_flag = false;
1223 volatile_flag = false;
1224 gnu_size = NULL_TREE;
1226 /* No point in taking the address of an initializing expression
1227 that isn't going to be used. */
1228 gnu_expr = NULL_TREE;
1230 /* If it has an address clause whose value is known at compile
1231 time, make the object a CONST_DECL. This will avoid a
1232 useless dereference. */
1233 if (Present (Address_Clause (gnat_entity)))
1235 Node_Id gnat_address
1236 = Expression (Address_Clause (gnat_entity));
1238 if (compile_time_known_address_p (gnat_address))
1240 gnu_expr = gnat_to_gnu (gnat_address);
1241 const_flag = true;
1246 /* If we are at top level and this object is of variable size,
1247 make the actual type a hidden pointer to the real type and
1248 make the initializer be a memory allocation and initialization.
1249 Likewise for objects we aren't defining (presumed to be
1250 external references from other packages), but there we do
1251 not set up an initialization.
1253 If the object's size overflows, make an allocator too, so that
1254 Storage_Error gets raised. Note that we will never free
1255 such memory, so we presume it never will get allocated. */
1256 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1257 global_bindings_p ()
1258 || !definition
1259 || static_flag)
1260 || (gnu_size
1261 && !allocatable_size_p (convert (sizetype,
1262 size_binop
1263 (CEIL_DIV_EXPR, gnu_size,
1264 bitsize_unit_node)),
1265 global_bindings_p ()
1266 || !definition
1267 || static_flag)))
1269 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1270 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1271 gnu_type = build_reference_type (gnu_type);
1272 used_by_ref = true;
1273 const_flag = true;
1274 volatile_flag = false;
1275 gnu_size = NULL_TREE;
1277 /* In case this was a aliased object whose nominal subtype is
1278 unconstrained, the pointer above will be a thin pointer and
1279 build_allocator will automatically make the template.
1281 If we have a template initializer only (that we made above),
1282 pretend there is none and rely on what build_allocator creates
1283 again anyway. Otherwise (if we have a full initializer), get
1284 the data part and feed that to build_allocator.
1286 If we are elaborating a mutable object, tell build_allocator to
1287 ignore a possibly simpler size from the initializer, if any, as
1288 we must allocate the maximum possible size in this case. */
1289 if (definition && !imported_p)
1291 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1293 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1294 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1296 gnu_alloc_type
1297 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1299 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1300 && CONSTRUCTOR_NELTS (gnu_expr) == 1)
1301 gnu_expr = NULL_TREE;
1302 else
1303 gnu_expr
1304 = build_component_ref
1305 (gnu_expr,
1306 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1307 false);
1310 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1311 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1312 post_error ("?`Storage_Error` will be raised at run time!",
1313 gnat_entity);
1315 gnu_expr
1316 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1317 Empty, Empty, gnat_entity, mutable_p);
1319 else
1320 gnu_expr = NULL_TREE;
1323 /* If this object would go into the stack and has an alignment larger
1324 than the largest stack alignment the back-end can honor, resort to
1325 a variable of "aligning type". */
1326 if (definition
1327 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT
1328 && !imported_p
1329 && !static_flag
1330 && !global_bindings_p ())
1332 /* Create the new variable. No need for extra room before the
1333 aligned field as this is in automatic storage. */
1334 tree gnu_new_type
1335 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1336 TYPE_SIZE_UNIT (gnu_type),
1337 BIGGEST_ALIGNMENT, 0, gnat_entity);
1338 tree gnu_new_var
1339 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1340 NULL_TREE, gnu_new_type, NULL_TREE,
1341 false, false, false, false, false,
1342 true, debug_info_p && definition, NULL,
1343 gnat_entity);
1345 /* Initialize the aligned field if we have an initializer. */
1346 if (gnu_expr)
1347 add_stmt_with_node
1348 (build_binary_op (INIT_EXPR, NULL_TREE,
1349 build_component_ref
1350 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1351 false),
1352 gnu_expr),
1353 gnat_entity);
1355 /* And setup this entity as a reference to the aligned field. */
1356 gnu_type = build_reference_type (gnu_type);
1357 gnu_expr
1358 = build_unary_op
1359 (ADDR_EXPR, NULL_TREE,
1360 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1361 false));
1362 TREE_CONSTANT (gnu_expr) = 1;
1364 used_by_ref = true;
1365 const_flag = true;
1366 volatile_flag = false;
1367 gnu_size = NULL_TREE;
1370 /* If this is an aggregate constant initialized to a constant, force it
1371 to be statically allocated. This saves an initialization copy. */
1372 if (!static_flag
1373 && const_flag
1374 && gnu_expr
1375 && TREE_CONSTANT (gnu_expr)
1376 && AGGREGATE_TYPE_P (gnu_type)
1377 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1378 && !(TYPE_IS_PADDING_P (gnu_type)
1379 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1380 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1381 static_flag = true;
1383 /* If this is an aliased object with an unconstrained array nominal
1384 subtype, we make its type a thin reference, i.e. the reference
1385 counterpart of a thin pointer, so it points to the array part.
1386 This is aimed to make it easier for the debugger to decode the
1387 object. Note that we have to do it this late because of the
1388 couple of allocation adjustments that might be made above. */
1389 if (Is_Constr_Subt_For_UN_Aliased (gnat_type)
1390 && Is_Array_Type (Underlying_Type (gnat_type))
1391 && !type_annotate_only)
1393 /* In case the object with the template has already been allocated
1394 just above, we have nothing to do here. */
1395 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1397 /* This variable is a GNAT encoding used by Workbench: let it
1398 go through the debugging information but mark it as
1399 artificial: users are not interested in it. */
1400 tree gnu_unc_var
1401 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1402 NULL_TREE, gnu_type, gnu_expr,
1403 const_flag, Is_Public (gnat_entity),
1404 imported_p || !definition, static_flag,
1405 volatile_flag, true,
1406 debug_info_p && definition,
1407 NULL, gnat_entity);
1408 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1409 TREE_CONSTANT (gnu_expr) = 1;
1411 used_by_ref = true;
1412 const_flag = true;
1413 volatile_flag = false;
1414 inner_const_flag = TREE_READONLY (gnu_unc_var);
1415 gnu_size = NULL_TREE;
1418 tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type));
1419 gnu_type
1420 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1423 /* Convert the expression to the type of the object if need be. */
1424 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1425 gnu_expr = convert (gnu_type, gnu_expr);
1427 /* If this name is external or a name was specified, use it, but don't
1428 use the Interface_Name with an address clause (see cd30005). */
1429 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1430 || (Present (Interface_Name (gnat_entity))
1431 && No (Address_Clause (gnat_entity))))
1432 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1434 /* Deal with a pragma Linker_Section on a constant or variable. */
1435 if ((kind == E_Constant || kind == E_Variable)
1436 && Present (Linker_Section_Pragma (gnat_entity)))
1437 prepend_one_attribute_pragma (&attr_list,
1438 Linker_Section_Pragma (gnat_entity));
1440 /* Now create the variable or the constant and set various flags. */
1441 gnu_decl
1442 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1443 gnu_expr, const_flag, Is_Public (gnat_entity),
1444 imported_p || !definition, static_flag,
1445 volatile_flag, artificial_p,
1446 debug_info_p && definition, attr_list,
1447 gnat_entity, !renamed_obj);
1448 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1449 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1450 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1452 /* If we are defining an Out parameter and optimization isn't enabled,
1453 create a fake PARM_DECL for debugging purposes and make it point to
1454 the VAR_DECL. Suppress debug info for the latter but make sure it
1455 will live in memory so that it can be accessed from within the
1456 debugger through the PARM_DECL. */
1457 if (kind == E_Out_Parameter
1458 && definition
1459 && debug_info_p
1460 && !optimize
1461 && !flag_generate_lto)
1463 tree param = create_param_decl (gnu_entity_name, gnu_type);
1464 gnat_pushdecl (param, gnat_entity);
1465 SET_DECL_VALUE_EXPR (param, gnu_decl);
1466 DECL_HAS_VALUE_EXPR_P (param) = 1;
1467 DECL_IGNORED_P (gnu_decl) = 1;
1468 TREE_ADDRESSABLE (gnu_decl) = 1;
1471 /* If this is a loop parameter, set the corresponding flag. */
1472 else if (kind == E_Loop_Parameter)
1473 DECL_LOOP_PARM_P (gnu_decl) = 1;
1475 /* If this is a renaming pointer, attach the renamed object to it. */
1476 if (renamed_obj)
1477 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1479 /* If this is a constant and we are defining it or it generates a real
1480 symbol at the object level and we are referencing it, we may want
1481 or need to have a true variable to represent it:
1482 - if optimization isn't enabled, for debugging purposes,
1483 - if the constant is public and not overlaid on something else,
1484 - if its address is taken,
1485 - if either itself or its type is aliased. */
1486 if (TREE_CODE (gnu_decl) == CONST_DECL
1487 && (definition || Sloc (gnat_entity) > Standard_Location)
1488 && ((!optimize && debug_info_p)
1489 || (Is_Public (gnat_entity)
1490 && No (Address_Clause (gnat_entity)))
1491 || Address_Taken (gnat_entity)
1492 || Is_Aliased (gnat_entity)
1493 || Is_Aliased (gnat_type)))
1495 tree gnu_corr_var
1496 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1497 gnu_expr, true, Is_Public (gnat_entity),
1498 !definition, static_flag, volatile_flag,
1499 artificial_p, debug_info_p && definition,
1500 attr_list, gnat_entity, false);
1502 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1505 /* If this is a constant, even if we don't need a true variable, we
1506 may need to avoid returning the initializer in every case. That
1507 can happen for the address of a (constant) constructor because,
1508 upon dereferencing it, the constructor will be reinjected in the
1509 tree, which may not be valid in every case; see lvalue_required_p
1510 for more details. */
1511 if (TREE_CODE (gnu_decl) == CONST_DECL)
1512 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1514 /* If this object is declared in a block that contains a block with an
1515 exception handler, and we aren't using the GCC exception mechanism,
1516 we must force this variable in memory in order to avoid an invalid
1517 optimization. */
1518 if (Front_End_Exceptions ()
1519 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1520 TREE_ADDRESSABLE (gnu_decl) = 1;
1522 /* If this is a local variable with non-BLKmode and aggregate type,
1523 and optimization isn't enabled, then force it in memory so that
1524 a register won't be allocated to it with possible subparts left
1525 uninitialized and reaching the register allocator. */
1526 else if (TREE_CODE (gnu_decl) == VAR_DECL
1527 && !DECL_EXTERNAL (gnu_decl)
1528 && !TREE_STATIC (gnu_decl)
1529 && DECL_MODE (gnu_decl) != BLKmode
1530 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1531 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1532 && !optimize)
1533 TREE_ADDRESSABLE (gnu_decl) = 1;
1535 /* If we are defining an object with variable size or an object with
1536 fixed size that will be dynamically allocated, and we are using the
1537 front-end setjmp/longjmp exception mechanism, update the setjmp
1538 buffer. */
1539 if (definition
1540 && Exception_Mechanism == Front_End_SJLJ
1541 && get_block_jmpbuf_decl ()
1542 && DECL_SIZE_UNIT (gnu_decl)
1543 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1544 || (flag_stack_check == GENERIC_STACK_CHECK
1545 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1546 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1547 add_stmt_with_node (build_call_n_expr
1548 (update_setjmp_buf_decl, 1,
1549 build_unary_op (ADDR_EXPR, NULL_TREE,
1550 get_block_jmpbuf_decl ())),
1551 gnat_entity);
1553 /* Back-annotate Esize and Alignment of the object if not already
1554 known. Note that we pick the values of the type, not those of
1555 the object, to shield ourselves from low-level platform-dependent
1556 adjustments like alignment promotion. This is both consistent with
1557 all the treatment above, where alignment and size are set on the
1558 type of the object and not on the object directly, and makes it
1559 possible to support all confirming representation clauses. */
1560 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1561 used_by_ref);
1563 break;
1565 case E_Void:
1566 /* Return a TYPE_DECL for "void" that we previously made. */
1567 gnu_decl = TYPE_NAME (void_type_node);
1568 break;
1570 case E_Enumeration_Type:
1571 /* A special case: for the types Character and Wide_Character in
1572 Standard, we do not list all the literals. So if the literals
1573 are not specified, make this an integer type. */
1574 if (No (First_Literal (gnat_entity)))
1576 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1577 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1578 else
1579 gnu_type = make_unsigned_type (esize);
1580 TYPE_NAME (gnu_type) = gnu_entity_name;
1582 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1583 This is needed by the DWARF-2 back-end to distinguish between
1584 unsigned integer types and character types. */
1585 TYPE_STRING_FLAG (gnu_type) = 1;
1587 /* This flag is needed by the call just below. */
1588 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1590 finish_character_type (gnu_type);
1592 else
1594 /* We have a list of enumeral constants in First_Literal. We make a
1595 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1596 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1597 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1598 value of the literal. But when we have a regular boolean type, we
1599 simplify this a little by using a BOOLEAN_TYPE. */
1600 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1601 && !Has_Non_Standard_Rep (gnat_entity);
1602 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1603 tree gnu_list = NULL_TREE;
1604 Entity_Id gnat_literal;
1606 /* Boolean types with foreign convention have precision 1. */
1607 if (is_boolean && foreign)
1608 esize = 1;
1610 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1611 TYPE_PRECISION (gnu_type) = esize;
1612 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1613 set_min_and_max_values_for_integral_type (gnu_type, esize,
1614 TYPE_SIGN (gnu_type));
1615 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1616 layout_type (gnu_type);
1618 for (gnat_literal = First_Literal (gnat_entity);
1619 Present (gnat_literal);
1620 gnat_literal = Next_Literal (gnat_literal))
1622 tree gnu_value
1623 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1624 /* Do not generate debug info for individual enumerators. */
1625 tree gnu_literal
1626 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1627 gnu_type, gnu_value, true, false, false,
1628 false, false, artificial_p, false,
1629 NULL, gnat_literal);
1630 save_gnu_tree (gnat_literal, gnu_literal, false);
1631 gnu_list
1632 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1635 if (!is_boolean)
1636 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1638 /* Note that the bounds are updated at the end of this function
1639 to avoid an infinite recursion since they refer to the type. */
1640 goto discrete_type;
1642 break;
1644 case E_Signed_Integer_Type:
1645 /* For integer types, just make a signed type the appropriate number
1646 of bits. */
1647 gnu_type = make_signed_type (esize);
1648 goto discrete_type;
1650 case E_Ordinary_Fixed_Point_Type:
1651 case E_Decimal_Fixed_Point_Type:
1653 /* Small_Value is the scale factor. */
1654 const Ureal gnat_small_value = Small_Value (gnat_entity);
1655 tree scale_factor = NULL_TREE;
1657 gnu_type = make_signed_type (esize);
1659 /* Try to decode the scale factor and to save it for the fixed-point
1660 types debug hook. */
1662 /* There are various ways to describe the scale factor, however there
1663 are cases where back-end internals cannot hold it. In such cases,
1664 we output invalid scale factor for such cases (i.e. the 0/0
1665 rational constant) but we expect GNAT to output GNAT encodings,
1666 then. Thus, keep this in sync with
1667 Exp_Dbug.Is_Handled_Scale_Factor. */
1669 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1670 binary or decimal scale: it is easier to read for humans. */
1671 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1672 && (Rbase (gnat_small_value) == 2
1673 || Rbase (gnat_small_value) == 10))
1675 /* Given RM restrictions on 'Small values, we assume here that
1676 the denominator fits in an int. */
1677 const tree base = build_int_cst (integer_type_node,
1678 Rbase (gnat_small_value));
1679 const tree exponent
1680 = build_int_cst (integer_type_node,
1681 UI_To_Int (Denominator (gnat_small_value)));
1682 scale_factor
1683 = build2 (RDIV_EXPR, integer_type_node,
1684 integer_one_node,
1685 build2 (POWER_EXPR, integer_type_node,
1686 base, exponent));
1689 /* Default to arbitrary scale factors descriptions. */
1690 else
1692 const Uint num = Norm_Num (gnat_small_value);
1693 const Uint den = Norm_Den (gnat_small_value);
1695 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1697 const tree gnu_num
1698 = build_int_cst (integer_type_node,
1699 UI_To_Int (Norm_Num (gnat_small_value)));
1700 const tree gnu_den
1701 = build_int_cst (integer_type_node,
1702 UI_To_Int (Norm_Den (gnat_small_value)));
1703 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1704 gnu_num, gnu_den);
1706 else
1707 /* If compiler internals cannot represent arbitrary scale
1708 factors, output an invalid scale factor so that debugger
1709 don't try to handle them but so that we still have a type
1710 in the output. Note that GNAT */
1711 scale_factor = integer_zero_node;
1714 TYPE_FIXED_POINT_P (gnu_type) = 1;
1715 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1717 goto discrete_type;
1719 case E_Modular_Integer_Type:
1721 /* For modular types, make the unsigned type of the proper number
1722 of bits and then set up the modulus, if required. */
1723 tree gnu_modulus, gnu_high = NULL_TREE;
1725 /* Packed Array Impl. Types are supposed to be subtypes only. */
1726 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1728 gnu_type = make_unsigned_type (esize);
1730 /* Get the modulus in this type. If it overflows, assume it is because
1731 it is equal to 2**Esize. Note that there is no overflow checking
1732 done on unsigned type, so we detect the overflow by looking for
1733 a modulus of zero, which is otherwise invalid. */
1734 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1736 if (!integer_zerop (gnu_modulus))
1738 TYPE_MODULAR_P (gnu_type) = 1;
1739 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1740 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1741 build_int_cst (gnu_type, 1));
1744 /* If the upper bound is not maximal, make an extra subtype. */
1745 if (gnu_high
1746 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1748 tree gnu_subtype = make_unsigned_type (esize);
1749 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1750 TREE_TYPE (gnu_subtype) = gnu_type;
1751 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1752 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1753 gnu_type = gnu_subtype;
1756 goto discrete_type;
1758 case E_Signed_Integer_Subtype:
1759 case E_Enumeration_Subtype:
1760 case E_Modular_Integer_Subtype:
1761 case E_Ordinary_Fixed_Point_Subtype:
1762 case E_Decimal_Fixed_Point_Subtype:
1764 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1765 not want to call create_range_type since we would like each subtype
1766 node to be distinct. ??? Historically this was in preparation for
1767 when memory aliasing is implemented, but that's obsolete now given
1768 the call to relate_alias_sets below.
1770 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1771 this fact is used by the arithmetic conversion functions.
1773 We elaborate the Ancestor_Subtype if it is not in the current unit
1774 and one of our bounds is non-static. We do this to ensure consistent
1775 naming in the case where several subtypes share the same bounds, by
1776 elaborating the first such subtype first, thus using its name. */
1778 if (!definition
1779 && Present (Ancestor_Subtype (gnat_entity))
1780 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1781 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1782 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1783 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1785 /* Set the precision to the Esize except for bit-packed arrays. */
1786 if (Is_Packed_Array_Impl_Type (gnat_entity)
1787 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1788 esize = UI_To_Int (RM_Size (gnat_entity));
1790 /* Boolean types with foreign convention have precision 1. */
1791 if (Is_Boolean_Type (gnat_entity) && foreign)
1793 gnu_type = make_node (BOOLEAN_TYPE);
1794 TYPE_PRECISION (gnu_type) = 1;
1795 TYPE_UNSIGNED (gnu_type) = 1;
1796 set_min_and_max_values_for_integral_type (gnu_type, 1, UNSIGNED);
1797 layout_type (gnu_type);
1799 /* First subtypes of Character are treated as Character; otherwise
1800 this should be an unsigned type if the base type is unsigned or
1801 if the lower bound is constant and non-negative or if the type
1802 is biased. However, even if the lower bound is constant and
1803 non-negative, we use a signed type for a subtype with the same
1804 size as its signed base type, because this eliminates useless
1805 conversions to it and gives more leeway to the optimizer; but
1806 this means that we will need to explicitly test for this case
1807 when we change the representation based on the RM size. */
1808 else if (kind == E_Enumeration_Subtype
1809 && No (First_Literal (Etype (gnat_entity)))
1810 && Esize (gnat_entity) == RM_Size (gnat_entity)
1811 && esize == CHAR_TYPE_SIZE
1812 && flag_signed_char)
1813 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1814 else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity)))
1815 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1816 && Is_Unsigned_Type (gnat_entity))
1817 || Has_Biased_Representation (gnat_entity))
1818 gnu_type = make_unsigned_type (esize);
1819 else
1820 gnu_type = make_signed_type (esize);
1821 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1823 SET_TYPE_RM_MIN_VALUE
1824 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1825 gnat_entity, "L", definition, true,
1826 debug_info_p));
1828 SET_TYPE_RM_MAX_VALUE
1829 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1830 gnat_entity, "U", definition, true,
1831 debug_info_p));
1833 if (TREE_CODE (gnu_type) == INTEGER_TYPE)
1834 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1835 = Has_Biased_Representation (gnat_entity);
1837 /* Do the same processing for Character subtypes as for types. */
1838 if (TYPE_STRING_FLAG (TREE_TYPE (gnu_type)))
1840 TYPE_NAME (gnu_type) = gnu_entity_name;
1841 TYPE_STRING_FLAG (gnu_type) = 1;
1842 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1843 finish_character_type (gnu_type);
1846 /* Inherit our alias set from what we're a subtype of. Subtypes
1847 are not different types and a pointer can designate any instance
1848 within a subtype hierarchy. */
1849 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1851 /* One of the above calls might have caused us to be elaborated,
1852 so don't blow up if so. */
1853 if (present_gnu_tree (gnat_entity))
1855 maybe_present = true;
1856 break;
1859 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1860 TYPE_STUB_DECL (gnu_type)
1861 = create_type_stub_decl (gnu_entity_name, gnu_type);
1863 /* For a packed array, make the original array type a parallel/debug
1864 type. */
1865 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1866 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1868 discrete_type:
1870 /* We have to handle clauses that under-align the type specially. */
1871 if ((Present (Alignment_Clause (gnat_entity))
1872 || (Is_Packed_Array_Impl_Type (gnat_entity)
1873 && Present
1874 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1875 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1877 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1878 if (align >= TYPE_ALIGN (gnu_type))
1879 align = 0;
1882 /* If the type we are dealing with represents a bit-packed array,
1883 we need to have the bits left justified on big-endian targets
1884 and right justified on little-endian targets. We also need to
1885 ensure that when the value is read (e.g. for comparison of two
1886 such values), we only get the good bits, since the unused bits
1887 are uninitialized. Both goals are accomplished by wrapping up
1888 the modular type in an enclosing record type. */
1889 if (Is_Packed_Array_Impl_Type (gnat_entity)
1890 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1892 tree gnu_field_type, gnu_field;
1894 /* Set the RM size before wrapping up the original type. */
1895 SET_TYPE_RM_SIZE (gnu_type,
1896 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1897 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1899 /* Create a stripped-down declaration, mainly for debugging. */
1900 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1901 gnat_entity);
1903 /* Now save it and build the enclosing record type. */
1904 gnu_field_type = gnu_type;
1906 gnu_type = make_node (RECORD_TYPE);
1907 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1908 TYPE_PACKED (gnu_type) = 1;
1909 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1910 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1911 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1913 /* Propagate the alignment of the modular type to the record type,
1914 unless there is an alignment clause that under-aligns the type.
1915 This means that bit-packed arrays are given "ceil" alignment for
1916 their size by default, which may seem counter-intuitive but makes
1917 it possible to overlay them on modular types easily. */
1918 SET_TYPE_ALIGN (gnu_type,
1919 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1921 /* Propagate the reverse storage order flag to the record type so
1922 that the required byte swapping is performed when retrieving the
1923 enclosed modular value. */
1924 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1925 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1927 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1929 /* Don't declare the field as addressable since we won't be taking
1930 its address and this would prevent create_field_decl from making
1931 a bitfield. */
1932 gnu_field
1933 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1934 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1936 /* We will output additional debug info manually below. */
1937 finish_record_type (gnu_type, gnu_field, 2, false);
1938 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1940 if (debug_info_p)
1942 /* Make the original array type a parallel/debug type. */
1943 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1945 /* Since GNU_TYPE is a padding type around the packed array
1946 implementation type, the padded type is its debug type. */
1947 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1948 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1952 /* If the type we are dealing with has got a smaller alignment than the
1953 natural one, we need to wrap it up in a record type and misalign the
1954 latter; we reuse the padding machinery for this purpose. */
1955 else if (align > 0)
1957 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1959 /* Set the RM size before wrapping the type. */
1960 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
1962 gnu_type
1963 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
1964 gnat_entity, false, true, definition, false);
1966 TYPE_PACKED (gnu_type) = 1;
1967 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
1970 break;
1972 case E_Floating_Point_Type:
1973 /* The type of the Low and High bounds can be our type if this is
1974 a type from Standard, so set them at the end of the function. */
1975 gnu_type = make_node (REAL_TYPE);
1976 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1977 layout_type (gnu_type);
1978 break;
1980 case E_Floating_Point_Subtype:
1981 /* See the E_Signed_Integer_Subtype case for the rationale. */
1982 if (!definition
1983 && Present (Ancestor_Subtype (gnat_entity))
1984 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1985 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1986 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1987 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1989 gnu_type = make_node (REAL_TYPE);
1990 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1991 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1992 TYPE_GCC_MIN_VALUE (gnu_type)
1993 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1994 TYPE_GCC_MAX_VALUE (gnu_type)
1995 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1996 layout_type (gnu_type);
1998 SET_TYPE_RM_MIN_VALUE
1999 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2000 gnat_entity, "L", definition, true,
2001 debug_info_p));
2003 SET_TYPE_RM_MAX_VALUE
2004 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2005 gnat_entity, "U", definition, true,
2006 debug_info_p));
2008 /* Inherit our alias set from what we're a subtype of, as for
2009 integer subtypes. */
2010 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2012 /* One of the above calls might have caused us to be elaborated,
2013 so don't blow up if so. */
2014 maybe_present = true;
2015 break;
2017 /* Array Types and Subtypes
2019 Unconstrained array types are represented by E_Array_Type and
2020 constrained array types are represented by E_Array_Subtype. There
2021 are no actual objects of an unconstrained array type; all we have
2022 are pointers to that type.
2024 The following fields are defined on array types and subtypes:
2026 Component_Type Component type of the array.
2027 Number_Dimensions Number of dimensions (an int).
2028 First_Index Type of first index. */
2030 case E_Array_Type:
2032 const bool convention_fortran_p
2033 = (Convention (gnat_entity) == Convention_Fortran);
2034 const int ndim = Number_Dimensions (gnat_entity);
2035 tree gnu_template_type;
2036 tree gnu_ptr_template;
2037 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2038 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2039 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2040 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2041 Entity_Id gnat_index, gnat_name;
2042 int index;
2043 tree comp_type;
2045 /* Create the type for the component now, as it simplifies breaking
2046 type reference loops. */
2047 comp_type
2048 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2049 if (present_gnu_tree (gnat_entity))
2051 /* As a side effect, the type may have been translated. */
2052 maybe_present = true;
2053 break;
2056 /* We complete an existing dummy fat pointer type in place. This both
2057 avoids further complex adjustments in update_pointer_to and yields
2058 better debugging information in DWARF by leveraging the support for
2059 incomplete declarations of "tagged" types in the DWARF back-end. */
2060 gnu_type = get_dummy_type (gnat_entity);
2061 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2063 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2064 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2065 gnu_ptr_template =
2066 TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2067 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2069 /* Save the contents of the dummy type for update_pointer_to. */
2070 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2071 TYPE_FIELDS (TYPE_POINTER_TO (gnu_type))
2072 = copy_node (TYPE_FIELDS (gnu_fat_type));
2073 DECL_CHAIN (TYPE_FIELDS (TYPE_POINTER_TO (gnu_type)))
2074 = copy_node (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2076 else
2078 gnu_fat_type = make_node (RECORD_TYPE);
2079 gnu_template_type = make_node (RECORD_TYPE);
2080 gnu_ptr_template = build_pointer_type (gnu_template_type);
2083 /* Make a node for the array. If we are not defining the array
2084 suppress expanding incomplete types. */
2085 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2087 if (!definition)
2089 defer_incomplete_level++;
2090 this_deferred = true;
2093 /* Build the fat pointer type. Use a "void *" object instead of
2094 a pointer to the array type since we don't have the array type
2095 yet (it will reference the fat pointer via the bounds). Note
2096 that we reuse the existing fields of a dummy type because for:
2098 type Arr is array (Positive range <>) of Element_Type;
2099 type Array_Ref is access Arr;
2100 Var : Array_Ref := Null;
2102 in a declarative part, Arr will be frozen only after Var, which
2103 means that the fields used in the CONSTRUCTOR built for Null are
2104 those of the dummy type, which in turn means that COMPONENT_REFs
2105 of Var may be built with these fields. Now if COMPONENT_REFs of
2106 Var are also built later with the fields of the final type, the
2107 aliasing machinery may consider that the accesses are distinct
2108 if the FIELD_DECLs are distinct as objects. */
2109 if (COMPLETE_TYPE_P (gnu_fat_type))
2111 tem = TYPE_FIELDS (gnu_fat_type);
2112 TREE_TYPE (tem) = ptr_type_node;
2113 TREE_TYPE (DECL_CHAIN (tem)) = gnu_ptr_template;
2114 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 0;
2115 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2116 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2118 else
2121 = create_field_decl (get_identifier ("P_ARRAY"),
2122 ptr_type_node, gnu_fat_type,
2123 NULL_TREE, NULL_TREE, 0, 0);
2124 DECL_CHAIN (tem)
2125 = create_field_decl (get_identifier ("P_BOUNDS"),
2126 gnu_ptr_template, gnu_fat_type,
2127 NULL_TREE, NULL_TREE, 0, 0);
2128 finish_fat_pointer_type (gnu_fat_type, tem);
2129 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2132 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2133 is the fat pointer. This will be used to access the individual
2134 fields once we build them. */
2135 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2136 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2137 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2138 gnu_template_reference
2139 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2140 TREE_READONLY (gnu_template_reference) = 1;
2141 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2143 /* Now create the GCC type for each index and add the fields for that
2144 index to the template. */
2145 for (index = (convention_fortran_p ? ndim - 1 : 0),
2146 gnat_index = First_Index (gnat_entity);
2147 IN_RANGE (index, 0, ndim - 1);
2148 index += (convention_fortran_p ? - 1 : 1),
2149 gnat_index = Next_Index (gnat_index))
2151 char field_name[16];
2152 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2153 tree gnu_index_base_type
2154 = maybe_character_type (get_base_type (gnu_index_type));
2155 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2156 tree gnu_min, gnu_max, gnu_high;
2158 /* Make the FIELD_DECLs for the low and high bounds of this
2159 type and then make extractions of these fields from the
2160 template. */
2161 sprintf (field_name, "LB%d", index);
2162 gnu_lb_field = create_field_decl (get_identifier (field_name),
2163 gnu_index_base_type,
2164 gnu_template_type, NULL_TREE,
2165 NULL_TREE, 0, 0);
2166 Sloc_to_locus (Sloc (gnat_entity),
2167 &DECL_SOURCE_LOCATION (gnu_lb_field));
2169 field_name[0] = 'U';
2170 gnu_hb_field = create_field_decl (get_identifier (field_name),
2171 gnu_index_base_type,
2172 gnu_template_type, NULL_TREE,
2173 NULL_TREE, 0, 0);
2174 Sloc_to_locus (Sloc (gnat_entity),
2175 &DECL_SOURCE_LOCATION (gnu_hb_field));
2177 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2179 /* We can't use build_component_ref here since the template type
2180 isn't complete yet. */
2181 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2182 gnu_template_reference, gnu_lb_field,
2183 NULL_TREE);
2184 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2185 gnu_template_reference, gnu_hb_field,
2186 NULL_TREE);
2187 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2189 gnu_min = convert (sizetype, gnu_orig_min);
2190 gnu_max = convert (sizetype, gnu_orig_max);
2192 /* Compute the size of this dimension. See the E_Array_Subtype
2193 case below for the rationale. */
2194 gnu_high
2195 = build3 (COND_EXPR, sizetype,
2196 build2 (GE_EXPR, boolean_type_node,
2197 gnu_orig_max, gnu_orig_min),
2198 gnu_max,
2199 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2201 /* Make a range type with the new range in the Ada base type.
2202 Then make an index type with the size range in sizetype. */
2203 gnu_index_types[index]
2204 = create_index_type (gnu_min, gnu_high,
2205 create_range_type (gnu_index_base_type,
2206 gnu_orig_min,
2207 gnu_orig_max),
2208 gnat_entity);
2210 /* Update the maximum size of the array in elements. */
2211 if (gnu_max_size)
2213 tree gnu_min
2214 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2215 tree gnu_max
2216 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2217 tree gnu_this_max
2218 = size_binop (PLUS_EXPR, size_one_node,
2219 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2221 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2222 && TREE_OVERFLOW (gnu_this_max))
2223 gnu_max_size = NULL_TREE;
2224 else
2225 gnu_max_size
2226 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2229 TYPE_NAME (gnu_index_types[index])
2230 = create_concat_name (gnat_entity, field_name);
2233 /* Install all the fields into the template. */
2234 TYPE_NAME (gnu_template_type)
2235 = create_concat_name (gnat_entity, "XUB");
2236 gnu_template_fields = NULL_TREE;
2237 for (index = 0; index < ndim; index++)
2238 gnu_template_fields
2239 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2240 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2241 debug_info_p);
2242 TYPE_READONLY (gnu_template_type) = 1;
2244 /* If Component_Size is not already specified, annotate it with the
2245 size of the component. */
2246 if (Unknown_Component_Size (gnat_entity))
2247 Set_Component_Size (gnat_entity,
2248 annotate_value (TYPE_SIZE (comp_type)));
2250 /* Compute the maximum size of the array in units and bits. */
2251 if (gnu_max_size)
2253 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2254 TYPE_SIZE_UNIT (comp_type));
2255 gnu_max_size = size_binop (MULT_EXPR,
2256 convert (bitsizetype, gnu_max_size),
2257 TYPE_SIZE (comp_type));
2259 else
2260 gnu_max_size_unit = NULL_TREE;
2262 /* Now build the array type. */
2263 tem = comp_type;
2264 for (index = ndim - 1; index >= 0; index--)
2266 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2267 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2268 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2269 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2270 set_reverse_storage_order_on_array_type (tem);
2271 if (array_type_has_nonaliased_component (tem, gnat_entity))
2272 set_nonaliased_component_on_array_type (tem);
2275 /* If an alignment is specified, use it if valid. But ignore it
2276 for the original type of packed array types. If the alignment
2277 was requested with an explicit alignment clause, state so. */
2278 if (No (Packed_Array_Impl_Type (gnat_entity))
2279 && Known_Alignment (gnat_entity))
2281 SET_TYPE_ALIGN (tem,
2282 validate_alignment (Alignment (gnat_entity),
2283 gnat_entity,
2284 TYPE_ALIGN (tem)));
2285 if (Present (Alignment_Clause (gnat_entity)))
2286 TYPE_USER_ALIGN (tem) = 1;
2289 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2290 implementation types as such so that the debug information back-end
2291 can output the appropriate description for them. */
2292 TYPE_PACKED (tem)
2293 = (Is_Packed (gnat_entity)
2294 || Is_Packed_Array_Impl_Type (gnat_entity));
2296 if (Treat_As_Volatile (gnat_entity))
2297 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2299 /* Adjust the type of the pointer-to-array field of the fat pointer
2300 and record the aliasing relationships if necessary. */
2301 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2302 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2303 record_component_aliases (gnu_fat_type);
2305 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2306 corresponding fat pointer. */
2307 TREE_TYPE (gnu_type) = gnu_fat_type;
2308 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2309 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2310 SET_TYPE_MODE (gnu_type, BLKmode);
2311 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem));
2313 /* If the maximum size doesn't overflow, use it. */
2314 if (gnu_max_size
2315 && TREE_CODE (gnu_max_size) == INTEGER_CST
2316 && !TREE_OVERFLOW (gnu_max_size)
2317 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2318 && !TREE_OVERFLOW (gnu_max_size_unit))
2320 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2321 TYPE_SIZE (tem));
2322 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2323 TYPE_SIZE_UNIT (tem));
2326 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2327 artificial_p, debug_info_p, gnat_entity);
2329 /* If told to generate GNAT encodings for them (GDB rely on them at the
2330 moment): give the fat pointer type a name. If this is a packed
2331 array, tell the debugger how to interpret the underlying bits. */
2332 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2333 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2334 else
2335 gnat_name = gnat_entity;
2336 tree xup_name
2337 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2338 ? get_entity_name (gnat_name)
2339 : create_concat_name (gnat_name, "XUP");
2340 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2341 gnat_entity);
2343 /* Create the type to be designated by thin pointers: a record type for
2344 the array and its template. We used to shift the fields to have the
2345 template at a negative offset, but this was somewhat of a kludge; we
2346 now shift thin pointer values explicitly but only those which have a
2347 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2348 Note that GDB can handle standard DWARF information for them, so we
2349 don't have to name them as a GNAT encoding, except if specifically
2350 asked to. */
2351 tree xut_name
2352 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2353 ? get_entity_name (gnat_name)
2354 : create_concat_name (gnat_name, "XUT");
2355 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2356 debug_info_p);
2358 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2359 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2361 break;
2363 case E_Array_Subtype:
2365 /* This is the actual data type for array variables. Multidimensional
2366 arrays are implemented as arrays of arrays. Note that arrays which
2367 have sparse enumeration subtypes as index components create sparse
2368 arrays, which is obviously space inefficient but so much easier to
2369 code for now.
2371 Also note that the subtype never refers to the unconstrained array
2372 type, which is somewhat at variance with Ada semantics.
2374 First check to see if this is simply a renaming of the array type.
2375 If so, the result is the array type. */
2377 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2378 if (!Is_Constrained (gnat_entity))
2380 else
2382 Entity_Id gnat_index, gnat_base_index;
2383 const bool convention_fortran_p
2384 = (Convention (gnat_entity) == Convention_Fortran);
2385 const int ndim = Number_Dimensions (gnat_entity);
2386 tree gnu_base_type = gnu_type;
2387 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2388 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2389 bool need_index_type_struct = false;
2390 int index;
2392 /* First create the GCC type for each index and find out whether
2393 special types are needed for debugging information. */
2394 for (index = (convention_fortran_p ? ndim - 1 : 0),
2395 gnat_index = First_Index (gnat_entity),
2396 gnat_base_index
2397 = First_Index (Implementation_Base_Type (gnat_entity));
2398 IN_RANGE (index, 0, ndim - 1);
2399 index += (convention_fortran_p ? - 1 : 1),
2400 gnat_index = Next_Index (gnat_index),
2401 gnat_base_index = Next_Index (gnat_base_index))
2403 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2404 tree gnu_index_base_type
2405 = maybe_character_type (get_base_type (gnu_index_type));
2406 tree gnu_orig_min
2407 = convert (gnu_index_base_type,
2408 TYPE_MIN_VALUE (gnu_index_type));
2409 tree gnu_orig_max
2410 = convert (gnu_index_base_type,
2411 TYPE_MAX_VALUE (gnu_index_type));
2412 tree gnu_min = convert (sizetype, gnu_orig_min);
2413 tree gnu_max = convert (sizetype, gnu_orig_max);
2414 tree gnu_base_index_type
2415 = get_unpadded_type (Etype (gnat_base_index));
2416 tree gnu_base_index_base_type
2417 = maybe_character_type (get_base_type (gnu_base_index_type));
2418 tree gnu_base_orig_min
2419 = convert (gnu_base_index_base_type,
2420 TYPE_MIN_VALUE (gnu_base_index_type));
2421 tree gnu_base_orig_max
2422 = convert (gnu_base_index_base_type,
2423 TYPE_MAX_VALUE (gnu_base_index_type));
2424 tree gnu_high;
2426 /* See if the base array type is already flat. If it is, we
2427 are probably compiling an ACATS test but it will cause the
2428 code below to malfunction if we don't handle it specially. */
2429 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2430 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2431 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2433 gnu_min = size_one_node;
2434 gnu_max = size_zero_node;
2435 gnu_high = gnu_max;
2438 /* Similarly, if one of the values overflows in sizetype and the
2439 range is null, use 1..0 for the sizetype bounds. */
2440 else if (TREE_CODE (gnu_min) == INTEGER_CST
2441 && TREE_CODE (gnu_max) == INTEGER_CST
2442 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2443 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2445 gnu_min = size_one_node;
2446 gnu_max = size_zero_node;
2447 gnu_high = gnu_max;
2450 /* If the minimum and maximum values both overflow in sizetype,
2451 but the difference in the original type does not overflow in
2452 sizetype, ignore the overflow indication. */
2453 else if (TREE_CODE (gnu_min) == INTEGER_CST
2454 && TREE_CODE (gnu_max) == INTEGER_CST
2455 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2456 && !TREE_OVERFLOW
2457 (convert (sizetype,
2458 fold_build2 (MINUS_EXPR, gnu_index_type,
2459 gnu_orig_max,
2460 gnu_orig_min))))
2462 TREE_OVERFLOW (gnu_min) = 0;
2463 TREE_OVERFLOW (gnu_max) = 0;
2464 gnu_high = gnu_max;
2467 /* Compute the size of this dimension in the general case. We
2468 need to provide GCC with an upper bound to use but have to
2469 deal with the "superflat" case. There are three ways to do
2470 this. If we can prove that the array can never be superflat,
2471 we can just use the high bound of the index type. */
2472 else if ((Nkind (gnat_index) == N_Range
2473 && cannot_be_superflat (gnat_index))
2474 /* Bit-Packed Array Impl. Types are never superflat. */
2475 || (Is_Packed_Array_Impl_Type (gnat_entity)
2476 && Is_Bit_Packed_Array
2477 (Original_Array_Type (gnat_entity))))
2478 gnu_high = gnu_max;
2480 /* Otherwise, if the high bound is constant but the low bound is
2481 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2482 lower bound. Note that the comparison must be done in the
2483 original type to avoid any overflow during the conversion. */
2484 else if (TREE_CODE (gnu_max) == INTEGER_CST
2485 && TREE_CODE (gnu_min) != INTEGER_CST)
2487 gnu_high = gnu_max;
2488 gnu_min
2489 = build_cond_expr (sizetype,
2490 build_binary_op (GE_EXPR,
2491 boolean_type_node,
2492 gnu_orig_max,
2493 gnu_orig_min),
2494 gnu_min,
2495 int_const_binop (PLUS_EXPR, gnu_max,
2496 size_one_node));
2499 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2500 in all the other cases. Note that, here as well as above,
2501 the condition used in the comparison must be equivalent to
2502 the condition (length != 0). This is relied upon in order
2503 to optimize array comparisons in compare_arrays. Moreover
2504 we use int_const_binop for the shift by 1 if the bound is
2505 constant to avoid any unwanted overflow. */
2506 else
2507 gnu_high
2508 = build_cond_expr (sizetype,
2509 build_binary_op (GE_EXPR,
2510 boolean_type_node,
2511 gnu_orig_max,
2512 gnu_orig_min),
2513 gnu_max,
2514 TREE_CODE (gnu_min) == INTEGER_CST
2515 ? int_const_binop (MINUS_EXPR, gnu_min,
2516 size_one_node)
2517 : size_binop (MINUS_EXPR, gnu_min,
2518 size_one_node));
2520 /* Reuse the index type for the range type. Then make an index
2521 type with the size range in sizetype. */
2522 gnu_index_types[index]
2523 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2524 gnat_entity);
2526 /* Update the maximum size of the array in elements. Here we
2527 see if any constraint on the index type of the base type
2528 can be used in the case of self-referential bound on the
2529 index type of the subtype. We look for a non-"infinite"
2530 and non-self-referential bound from any type involved and
2531 handle each bound separately. */
2532 if (gnu_max_size)
2534 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2535 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2536 tree gnu_base_base_min
2537 = convert (sizetype,
2538 TYPE_MIN_VALUE (gnu_base_index_base_type));
2539 tree gnu_base_base_max
2540 = convert (sizetype,
2541 TYPE_MAX_VALUE (gnu_base_index_base_type));
2543 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2544 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2545 && !TREE_OVERFLOW (gnu_base_min)))
2546 gnu_base_min = gnu_min;
2548 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2549 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2550 && !TREE_OVERFLOW (gnu_base_max)))
2551 gnu_base_max = gnu_max;
2553 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2554 && TREE_OVERFLOW (gnu_base_min))
2555 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2556 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2557 && TREE_OVERFLOW (gnu_base_max))
2558 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2559 gnu_max_size = NULL_TREE;
2560 else
2562 tree gnu_this_max;
2564 /* Use int_const_binop if the bounds are constant to
2565 avoid any unwanted overflow. */
2566 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2567 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2568 gnu_this_max
2569 = int_const_binop (PLUS_EXPR, size_one_node,
2570 int_const_binop (MINUS_EXPR,
2571 gnu_base_max,
2572 gnu_base_min));
2573 else
2574 gnu_this_max
2575 = size_binop (PLUS_EXPR, size_one_node,
2576 size_binop (MINUS_EXPR,
2577 gnu_base_max,
2578 gnu_base_min));
2580 gnu_max_size
2581 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2585 /* We need special types for debugging information to point to
2586 the index types if they have variable bounds, are not integer
2587 types, are biased or are wider than sizetype. These are GNAT
2588 encodings, so we have to include them only when all encodings
2589 are requested. */
2590 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2591 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2592 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2593 || (TREE_TYPE (gnu_index_type)
2594 && TREE_CODE (TREE_TYPE (gnu_index_type))
2595 != INTEGER_TYPE)
2596 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2597 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2598 need_index_type_struct = true;
2601 /* Then flatten: create the array of arrays. For an array type
2602 used to implement a packed array, get the component type from
2603 the original array type since the representation clauses that
2604 can affect it are on the latter. */
2605 if (Is_Packed_Array_Impl_Type (gnat_entity)
2606 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2608 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2609 for (index = ndim - 1; index >= 0; index--)
2610 gnu_type = TREE_TYPE (gnu_type);
2612 /* One of the above calls might have caused us to be elaborated,
2613 so don't blow up if so. */
2614 if (present_gnu_tree (gnat_entity))
2616 maybe_present = true;
2617 break;
2620 else
2622 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2623 debug_info_p);
2625 /* One of the above calls might have caused us to be elaborated,
2626 so don't blow up if so. */
2627 if (present_gnu_tree (gnat_entity))
2629 maybe_present = true;
2630 break;
2634 /* Compute the maximum size of the array in units and bits. */
2635 if (gnu_max_size)
2637 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2638 TYPE_SIZE_UNIT (gnu_type));
2639 gnu_max_size = size_binop (MULT_EXPR,
2640 convert (bitsizetype, gnu_max_size),
2641 TYPE_SIZE (gnu_type));
2643 else
2644 gnu_max_size_unit = NULL_TREE;
2646 /* Now build the array type. */
2647 for (index = ndim - 1; index >= 0; index --)
2649 gnu_type = build_nonshared_array_type (gnu_type,
2650 gnu_index_types[index]);
2651 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2652 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2653 if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity))
2654 set_reverse_storage_order_on_array_type (gnu_type);
2655 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2656 set_nonaliased_component_on_array_type (gnu_type);
2659 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2660 TYPE_STUB_DECL (gnu_type)
2661 = create_type_stub_decl (gnu_entity_name, gnu_type);
2663 /* If this is a multi-dimensional array and we are at global level,
2664 we need to make a variable corresponding to the stride of the
2665 inner dimensions. */
2666 if (ndim > 1 && global_bindings_p ())
2668 tree gnu_arr_type;
2670 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2671 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2672 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2674 tree eltype = TREE_TYPE (gnu_arr_type);
2675 char stride_name[32];
2677 sprintf (stride_name, "ST%d", index);
2678 TYPE_SIZE (gnu_arr_type)
2679 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2680 gnat_entity, stride_name,
2681 definition, false);
2683 /* ??? For now, store the size as a multiple of the
2684 alignment of the element type in bytes so that we
2685 can see the alignment from the tree. */
2686 sprintf (stride_name, "ST%d_A_UNIT", index);
2687 TYPE_SIZE_UNIT (gnu_arr_type)
2688 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2689 gnat_entity, stride_name,
2690 definition, false,
2691 TYPE_ALIGN (eltype));
2693 /* ??? create_type_decl is not invoked on the inner types so
2694 the MULT_EXPR node built above will never be marked. */
2695 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2699 /* If we need to write out a record type giving the names of the
2700 bounds for debugging purposes, do it now and make the record
2701 type a parallel type. This is not needed for a packed array
2702 since the bounds are conveyed by the original array type. */
2703 if (need_index_type_struct
2704 && debug_info_p
2705 && !Is_Packed_Array_Impl_Type (gnat_entity))
2707 tree gnu_bound_rec = make_node (RECORD_TYPE);
2708 tree gnu_field_list = NULL_TREE;
2709 tree gnu_field;
2711 TYPE_NAME (gnu_bound_rec)
2712 = create_concat_name (gnat_entity, "XA");
2714 for (index = ndim - 1; index >= 0; index--)
2716 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2717 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2719 /* Make sure to reference the types themselves, and not just
2720 their names, as the debugger may fall back on them. */
2721 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2722 gnu_bound_rec, NULL_TREE,
2723 NULL_TREE, 0, 0);
2724 DECL_CHAIN (gnu_field) = gnu_field_list;
2725 gnu_field_list = gnu_field;
2728 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2729 add_parallel_type (gnu_type, gnu_bound_rec);
2732 /* If this is a packed array type, make the original array type a
2733 parallel/debug type. Otherwise, if such GNAT encodings are
2734 required, do it for the base array type if it isn't artificial to
2735 make sure it is kept in the debug info. */
2736 if (debug_info_p)
2738 if (Is_Packed_Array_Impl_Type (gnat_entity))
2739 associate_original_type_to_packed_array (gnu_type,
2740 gnat_entity);
2741 else
2743 tree gnu_base_decl
2744 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2745 false);
2746 if (!DECL_ARTIFICIAL (gnu_base_decl)
2747 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2748 add_parallel_type (gnu_type,
2749 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2753 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2754 = (Is_Packed_Array_Impl_Type (gnat_entity)
2755 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2757 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2758 implementation types as such so that the debug information back-end
2759 can output the appropriate description for them. */
2760 TYPE_PACKED (gnu_type)
2761 = (Is_Packed (gnat_entity)
2762 || Is_Packed_Array_Impl_Type (gnat_entity));
2764 /* If the size is self-referential and the maximum size doesn't
2765 overflow, use it. */
2766 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2767 && gnu_max_size
2768 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2769 && TREE_OVERFLOW (gnu_max_size))
2770 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2771 && TREE_OVERFLOW (gnu_max_size_unit)))
2773 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2774 TYPE_SIZE (gnu_type));
2775 TYPE_SIZE_UNIT (gnu_type)
2776 = size_binop (MIN_EXPR, gnu_max_size_unit,
2777 TYPE_SIZE_UNIT (gnu_type));
2780 /* Set our alias set to that of our base type. This gives all
2781 array subtypes the same alias set. */
2782 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2784 /* If this is a packed type implemented specially, then replace our
2785 type with the implementation type. */
2786 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2788 /* First finish the type we had been making so that we output
2789 debugging information for it. */
2790 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2791 if (Treat_As_Volatile (gnat_entity))
2793 const int quals
2794 = TYPE_QUAL_VOLATILE
2795 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2796 gnu_type = change_qualified_type (gnu_type, quals);
2798 /* Make it artificial only if the base type was artificial too.
2799 That's sort of "morally" true and will make it possible for
2800 the debugger to look it up by name in DWARF, which is needed
2801 in order to decode the packed array type. */
2802 tree gnu_tmp_decl
2803 = create_type_decl (gnu_entity_name, gnu_type,
2804 !Comes_From_Source (Etype (gnat_entity))
2805 && artificial_p, debug_info_p,
2806 gnat_entity);
2807 /* Save it as our equivalent in case the call below elaborates
2808 this type again. */
2809 save_gnu_tree (gnat_entity, gnu_tmp_decl, false);
2811 gnu_type
2812 = gnat_to_gnu_type (Packed_Array_Impl_Type (gnat_entity));
2813 save_gnu_tree (gnat_entity, NULL_TREE, false);
2815 /* Set the ___XP suffix for GNAT encodings. */
2816 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2817 gnu_entity_name = DECL_NAME (TYPE_NAME (gnu_type));
2819 tree gnu_inner = gnu_type;
2820 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2821 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2822 || TYPE_PADDING_P (gnu_inner)))
2823 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2825 /* We need to attach the index type to the type we just made so
2826 that the actual bounds can later be put into a template. */
2827 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2828 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2829 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2830 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2832 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2834 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2835 TYPE_MODULUS for modular types so we make an extra
2836 subtype if necessary. */
2837 if (TYPE_MODULAR_P (gnu_inner))
2839 tree gnu_subtype
2840 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2841 TREE_TYPE (gnu_subtype) = gnu_inner;
2842 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2843 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2844 TYPE_MIN_VALUE (gnu_inner));
2845 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2846 TYPE_MAX_VALUE (gnu_inner));
2847 gnu_inner = gnu_subtype;
2850 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2852 /* Check for other cases of overloading. */
2853 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2856 for (Entity_Id gnat_index = First_Index (gnat_entity);
2857 Present (gnat_index);
2858 gnat_index = Next_Index (gnat_index))
2859 SET_TYPE_ACTUAL_BOUNDS
2860 (gnu_inner,
2861 tree_cons (NULL_TREE,
2862 get_unpadded_type (Etype (gnat_index)),
2863 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2865 if (Convention (gnat_entity) != Convention_Fortran)
2866 SET_TYPE_ACTUAL_BOUNDS
2867 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2869 if (TREE_CODE (gnu_type) == RECORD_TYPE
2870 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2871 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2875 break;
2877 case E_String_Literal_Subtype:
2878 /* Create the type for a string literal. */
2880 Entity_Id gnat_full_type
2881 = (Is_Private_Type (Etype (gnat_entity))
2882 && Present (Full_View (Etype (gnat_entity)))
2883 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2884 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2885 tree gnu_string_array_type
2886 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2887 tree gnu_string_index_type
2888 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2889 (TYPE_DOMAIN (gnu_string_array_type))));
2890 tree gnu_lower_bound
2891 = convert (gnu_string_index_type,
2892 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2893 tree gnu_length
2894 = UI_To_gnu (String_Literal_Length (gnat_entity),
2895 gnu_string_index_type);
2896 tree gnu_upper_bound
2897 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2898 gnu_lower_bound,
2899 int_const_binop (MINUS_EXPR, gnu_length,
2900 convert (gnu_string_index_type,
2901 integer_one_node)));
2902 tree gnu_index_type
2903 = create_index_type (convert (sizetype, gnu_lower_bound),
2904 convert (sizetype, gnu_upper_bound),
2905 create_range_type (gnu_string_index_type,
2906 gnu_lower_bound,
2907 gnu_upper_bound),
2908 gnat_entity);
2910 gnu_type
2911 = build_nonshared_array_type (gnat_to_gnu_type
2912 (Component_Type (gnat_entity)),
2913 gnu_index_type);
2914 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2915 set_nonaliased_component_on_array_type (gnu_type);
2916 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2918 break;
2920 /* Record Types and Subtypes
2922 The following fields are defined on record types:
2924 Has_Discriminants True if the record has discriminants
2925 First_Discriminant Points to head of list of discriminants
2926 First_Entity Points to head of list of fields
2927 Is_Tagged_Type True if the record is tagged
2929 Implementation of Ada records and discriminated records:
2931 A record type definition is transformed into the equivalent of a C
2932 struct definition. The fields that are the discriminants which are
2933 found in the Full_Type_Declaration node and the elements of the
2934 Component_List found in the Record_Type_Definition node. The
2935 Component_List can be a recursive structure since each Variant of
2936 the Variant_Part of the Component_List has a Component_List.
2938 Processing of a record type definition comprises starting the list of
2939 field declarations here from the discriminants and the calling the
2940 function components_to_record to add the rest of the fields from the
2941 component list and return the gnu type node. The function
2942 components_to_record will call itself recursively as it traverses
2943 the tree. */
2945 case E_Record_Type:
2946 if (Has_Complex_Representation (gnat_entity))
2948 gnu_type
2949 = build_complex_type
2950 (get_unpadded_type
2951 (Etype (Defining_Entity
2952 (First (Component_Items
2953 (Component_List
2954 (Type_Definition
2955 (Declaration_Node (gnat_entity)))))))));
2957 break;
2961 Node_Id full_definition = Declaration_Node (gnat_entity);
2962 Node_Id record_definition = Type_Definition (full_definition);
2963 Node_Id gnat_constr;
2964 Entity_Id gnat_field, gnat_parent_type;
2965 tree gnu_field, gnu_field_list = NULL_TREE;
2966 tree gnu_get_parent;
2967 /* Set PACKED in keeping with gnat_to_gnu_field. */
2968 const int packed
2969 = Is_Packed (gnat_entity)
2971 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2972 ? -1
2973 : 0;
2974 const bool has_align = Known_Alignment (gnat_entity);
2975 const bool has_discr = Has_Discriminants (gnat_entity);
2976 const bool has_rep = Has_Specified_Layout (gnat_entity);
2977 const bool is_extension
2978 = (Is_Tagged_Type (gnat_entity)
2979 && Nkind (record_definition) == N_Derived_Type_Definition);
2980 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2981 bool all_rep = has_rep;
2983 /* See if all fields have a rep clause. Stop when we find one
2984 that doesn't. */
2985 if (all_rep)
2986 for (gnat_field = First_Entity (gnat_entity);
2987 Present (gnat_field);
2988 gnat_field = Next_Entity (gnat_field))
2989 if ((Ekind (gnat_field) == E_Component
2990 || Ekind (gnat_field) == E_Discriminant)
2991 && No (Component_Clause (gnat_field)))
2993 all_rep = false;
2994 break;
2997 /* If this is a record extension, go a level further to find the
2998 record definition. Also, verify we have a Parent_Subtype. */
2999 if (is_extension)
3001 if (!type_annotate_only
3002 || Present (Record_Extension_Part (record_definition)))
3003 record_definition = Record_Extension_Part (record_definition);
3005 gcc_assert (type_annotate_only
3006 || Present (Parent_Subtype (gnat_entity)));
3009 /* Make a node for the record. If we are not defining the record,
3010 suppress expanding incomplete types. */
3011 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3012 TYPE_NAME (gnu_type) = gnu_entity_name;
3013 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3014 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3015 = Reverse_Storage_Order (gnat_entity);
3016 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3018 if (!definition)
3020 defer_incomplete_level++;
3021 this_deferred = true;
3024 /* If both a size and rep clause were specified, put the size on
3025 the record type now so that it can get the proper layout. */
3026 if (has_rep && Known_RM_Size (gnat_entity))
3027 TYPE_SIZE (gnu_type)
3028 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3030 /* Always set the alignment on the record type here so that it can
3031 get the proper layout. */
3032 if (has_align)
3033 SET_TYPE_ALIGN (gnu_type,
3034 validate_alignment (Alignment (gnat_entity),
3035 gnat_entity, 0));
3036 else
3038 SET_TYPE_ALIGN (gnu_type, 0);
3040 /* If a type needs strict alignment, the minimum size will be the
3041 type size instead of the RM size (see validate_size). Cap the
3042 alignment lest it causes this type size to become too large. */
3043 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3045 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3046 unsigned int max_align = max_size & -max_size;
3047 if (max_align < BIGGEST_ALIGNMENT)
3048 TYPE_MAX_ALIGN (gnu_type) = max_align;
3052 /* If we have a Parent_Subtype, make a field for the parent. If
3053 this record has rep clauses, force the position to zero. */
3054 if (Present (Parent_Subtype (gnat_entity)))
3056 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3057 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3058 tree gnu_parent;
3059 int parent_packed = 0;
3061 /* A major complexity here is that the parent subtype will
3062 reference our discriminants in its Stored_Constraint list.
3063 But those must reference the parent component of this record
3064 which is precisely of the parent subtype we have not built yet!
3065 To break the circle we first build a dummy COMPONENT_REF which
3066 represents the "get to the parent" operation and initialize
3067 each of those discriminants to a COMPONENT_REF of the above
3068 dummy parent referencing the corresponding discriminant of the
3069 base type of the parent subtype. */
3070 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3071 build0 (PLACEHOLDER_EXPR, gnu_type),
3072 build_decl (input_location,
3073 FIELD_DECL, NULL_TREE,
3074 gnu_dummy_parent_type),
3075 NULL_TREE);
3077 if (has_discr)
3078 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3079 Present (gnat_field);
3080 gnat_field = Next_Stored_Discriminant (gnat_field))
3081 if (Present (Corresponding_Discriminant (gnat_field)))
3083 tree gnu_field
3084 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3085 (gnat_field));
3086 save_gnu_tree
3087 (gnat_field,
3088 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3089 gnu_get_parent, gnu_field, NULL_TREE),
3090 true);
3093 /* Then we build the parent subtype. If it has discriminants but
3094 the type itself has unknown discriminants, this means that it
3095 doesn't contain information about how the discriminants are
3096 derived from those of the ancestor type, so it cannot be used
3097 directly. Instead it is built by cloning the parent subtype
3098 of the underlying record view of the type, for which the above
3099 derivation of discriminants has been made explicit. */
3100 if (Has_Discriminants (gnat_parent)
3101 && Has_Unknown_Discriminants (gnat_entity))
3103 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3105 /* If we are defining the type, the underlying record
3106 view must already have been elaborated at this point.
3107 Otherwise do it now as its parent subtype cannot be
3108 technically elaborated on its own. */
3109 if (definition)
3110 gcc_assert (present_gnu_tree (gnat_uview));
3111 else
3112 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3114 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3116 /* Substitute the "get to the parent" of the type for that
3117 of its underlying record view in the cloned type. */
3118 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3119 Present (gnat_field);
3120 gnat_field = Next_Stored_Discriminant (gnat_field))
3121 if (Present (Corresponding_Discriminant (gnat_field)))
3123 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3124 tree gnu_ref
3125 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3126 gnu_get_parent, gnu_field, NULL_TREE);
3127 gnu_parent
3128 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3131 else
3132 gnu_parent = gnat_to_gnu_type (gnat_parent);
3134 /* The parent field needs strict alignment so, if it is to
3135 be created with a component clause below, then we need
3136 to apply the same adjustment as in gnat_to_gnu_field. */
3137 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3139 /* ??? For historical reasons, we do it on strict-alignment
3140 platforms only, where it is really required. This means
3141 that a confirming representation clause will change the
3142 behavior of the compiler on the other platforms. */
3143 if (STRICT_ALIGNMENT)
3144 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3145 else
3146 parent_packed
3147 = adjust_packed (gnu_parent, gnu_type, parent_packed);
3150 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3151 initially built. The discriminants must reference the fields
3152 of the parent subtype and not those of its base type for the
3153 placeholder machinery to properly work. */
3154 if (has_discr)
3156 /* The actual parent subtype is the full view. */
3157 if (Is_Private_Type (gnat_parent))
3159 if (Present (Full_View (gnat_parent)))
3160 gnat_parent = Full_View (gnat_parent);
3161 else
3162 gnat_parent = Underlying_Full_View (gnat_parent);
3165 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3166 Present (gnat_field);
3167 gnat_field = Next_Stored_Discriminant (gnat_field))
3168 if (Present (Corresponding_Discriminant (gnat_field)))
3170 Entity_Id field;
3171 for (field = First_Stored_Discriminant (gnat_parent);
3172 Present (field);
3173 field = Next_Stored_Discriminant (field))
3174 if (same_discriminant_p (gnat_field, field))
3175 break;
3176 gcc_assert (Present (field));
3177 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3178 = gnat_to_gnu_field_decl (field);
3182 /* The "get to the parent" COMPONENT_REF must be given its
3183 proper type... */
3184 TREE_TYPE (gnu_get_parent) = gnu_parent;
3186 /* ...and reference the _Parent field of this record. */
3187 gnu_field
3188 = create_field_decl (parent_name_id,
3189 gnu_parent, gnu_type,
3190 has_rep
3191 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3192 has_rep
3193 ? bitsize_zero_node : NULL_TREE,
3194 parent_packed, 1);
3195 DECL_INTERNAL_P (gnu_field) = 1;
3196 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3197 TYPE_FIELDS (gnu_type) = gnu_field;
3200 /* Make the fields for the discriminants and put them into the record
3201 unless it's an Unchecked_Union. */
3202 if (has_discr)
3203 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3204 Present (gnat_field);
3205 gnat_field = Next_Stored_Discriminant (gnat_field))
3207 /* If this is a record extension and this discriminant is the
3208 renaming of another discriminant, we've handled it above. */
3209 if (is_extension
3210 && Present (Corresponding_Discriminant (gnat_field)))
3211 continue;
3213 gnu_field
3214 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3215 debug_info_p);
3217 /* Make an expression using a PLACEHOLDER_EXPR from the
3218 FIELD_DECL node just created and link that with the
3219 corresponding GNAT defining identifier. */
3220 save_gnu_tree (gnat_field,
3221 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3222 build0 (PLACEHOLDER_EXPR, gnu_type),
3223 gnu_field, NULL_TREE),
3224 true);
3226 if (!is_unchecked_union)
3228 DECL_CHAIN (gnu_field) = gnu_field_list;
3229 gnu_field_list = gnu_field;
3233 /* If we have a derived untagged type that renames discriminants in
3234 the parent type, the (stored) discriminants are just a copy of the
3235 discriminants of the parent type. This means that any constraints
3236 added by the renaming in the derivation are disregarded as far as
3237 the layout of the derived type is concerned. To rescue them, we
3238 change the type of the (stored) discriminants to a subtype with
3239 the bounds of the type of the visible discriminants. */
3240 if (has_discr
3241 && !is_extension
3242 && Stored_Constraint (gnat_entity) != No_Elist)
3243 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3244 gnat_constr != No_Elmt;
3245 gnat_constr = Next_Elmt (gnat_constr))
3246 if (Nkind (Node (gnat_constr)) == N_Identifier
3247 /* Ignore access discriminants. */
3248 && !Is_Access_Type (Etype (Node (gnat_constr)))
3249 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3251 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3252 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3253 tree gnu_ref
3254 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3255 NULL_TREE, false);
3257 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3258 just above for one of the stored discriminants. */
3259 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3261 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3263 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3264 tree gnu_subtype
3265 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3266 ? make_unsigned_type (prec) : make_signed_type (prec);
3267 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3268 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3269 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3270 TYPE_MIN_VALUE (gnu_discr_type));
3271 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3272 TYPE_MAX_VALUE (gnu_discr_type));
3273 TREE_TYPE (gnu_ref)
3274 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3278 /* If this is a derived type with discriminants and these discriminants
3279 affect the initial shape it has inherited, factor them in. */
3280 if (has_discr
3281 && !is_extension
3282 && !Has_Record_Rep_Clause (gnat_entity)
3283 && Stored_Constraint (gnat_entity) != No_Elist
3284 && (gnat_parent_type = Underlying_Type (Etype (gnat_entity)))
3285 && Is_Record_Type (gnat_parent_type)
3286 && Is_Unchecked_Union (gnat_entity)
3287 == Is_Unchecked_Union (gnat_parent_type)
3288 && No_Reordering (gnat_entity) == No_Reordering (gnat_parent_type))
3290 tree gnu_parent_type
3291 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type));
3293 if (TYPE_IS_PADDING_P (gnu_parent_type))
3294 gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type));
3296 vec<subst_pair> gnu_subst_list
3297 = build_subst_list (gnat_entity, gnat_parent_type, definition);
3299 /* Set the layout of the type to match that of the parent type,
3300 doing required substitutions. If we are in minimal GNAT
3301 encodings mode, we don't need debug info for the inner record
3302 types, as they will be part of the embedding variant record's
3303 debug info. */
3304 copy_and_substitute_in_layout
3305 (gnat_entity, gnat_parent_type, gnu_type, gnu_parent_type,
3306 gnu_subst_list,
3307 debug_info_p && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL);
3309 else
3311 /* Add the fields into the record type and finish it up. */
3312 components_to_record (Component_List (record_definition),
3313 gnat_entity, gnu_field_list, gnu_type,
3314 packed, definition, false, all_rep,
3315 is_unchecked_union, artificial_p,
3316 debug_info_p, false,
3317 all_rep ? NULL_TREE : bitsize_zero_node,
3318 NULL);
3320 /* Empty classes have the size of a storage unit in C++. */
3321 if (TYPE_SIZE (gnu_type) == bitsize_zero_node
3322 && Convention (gnat_entity) == Convention_CPP)
3324 TYPE_SIZE (gnu_type) = bitsize_unit_node;
3325 TYPE_SIZE_UNIT (gnu_type) = size_one_node;
3326 compute_record_mode (gnu_type);
3329 /* If there are entities in the chain corresponding to components
3330 that we did not elaborate, ensure we elaborate their types if
3331 they are Itypes. */
3332 for (gnat_temp = First_Entity (gnat_entity);
3333 Present (gnat_temp);
3334 gnat_temp = Next_Entity (gnat_temp))
3335 if ((Ekind (gnat_temp) == E_Component
3336 || Ekind (gnat_temp) == E_Discriminant)
3337 && Is_Itype (Etype (gnat_temp))
3338 && !present_gnu_tree (gnat_temp))
3339 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3342 /* Fill in locations of fields. */
3343 annotate_rep (gnat_entity, gnu_type);
3345 /* If this is a record type associated with an exception definition,
3346 equate its fields to those of the standard exception type. This
3347 will make it possible to convert between them. */
3348 if (gnu_entity_name == exception_data_name_id)
3350 tree gnu_std_field;
3351 for (gnu_field = TYPE_FIELDS (gnu_type),
3352 gnu_std_field = TYPE_FIELDS (except_type_node);
3353 gnu_field;
3354 gnu_field = DECL_CHAIN (gnu_field),
3355 gnu_std_field = DECL_CHAIN (gnu_std_field))
3356 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3357 gcc_assert (!gnu_std_field);
3360 break;
3362 case E_Class_Wide_Subtype:
3363 /* If an equivalent type is present, that is what we should use.
3364 Otherwise, fall through to handle this like a record subtype
3365 since it may have constraints. */
3366 if (gnat_equiv_type != gnat_entity)
3368 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3369 maybe_present = true;
3370 break;
3373 /* ... fall through ... */
3375 case E_Record_Subtype:
3376 /* If Cloned_Subtype is Present it means this record subtype has
3377 identical layout to that type or subtype and we should use
3378 that GCC type for this one. The front end guarantees that
3379 the component list is shared. */
3380 if (Present (Cloned_Subtype (gnat_entity)))
3382 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3383 NULL_TREE, false);
3384 saved = true;
3385 break;
3388 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3389 changing the type, make a new type with each field having the type of
3390 the field in the new subtype but the position computed by transforming
3391 every discriminant reference according to the constraints. We don't
3392 see any difference between private and non-private type here since
3393 derivations from types should have been deferred until the completion
3394 of the private type. */
3395 else
3397 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3399 if (!definition)
3401 defer_incomplete_level++;
3402 this_deferred = true;
3405 tree gnu_base_type
3406 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3408 if (present_gnu_tree (gnat_entity))
3410 maybe_present = true;
3411 break;
3414 /* When the subtype has discriminants and these discriminants affect
3415 the initial shape it has inherited, factor them in. But for an
3416 Unchecked_Union (it must be an Itype), just return the type. */
3417 if (Has_Discriminants (gnat_entity)
3418 && Stored_Constraint (gnat_entity) != No_Elist
3419 && !Is_For_Access_Subtype (gnat_entity)
3420 && Is_Record_Type (gnat_base_type)
3421 && !Is_Unchecked_Union (gnat_base_type))
3423 vec<subst_pair> gnu_subst_list
3424 = build_subst_list (gnat_entity, gnat_base_type, definition);
3425 tree gnu_unpad_base_type;
3427 gnu_type = make_node (RECORD_TYPE);
3428 TYPE_NAME (gnu_type) = gnu_entity_name;
3429 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3431 /* Use the ultimate base record type as the debug type.
3432 Subtypes and derived types bring no useful
3433 information. */
3434 Entity_Id gnat_debug_type = gnat_entity;
3435 while (Etype (gnat_debug_type) != gnat_debug_type)
3436 gnat_debug_type = Etype (gnat_debug_type);
3437 tree gnu_debug_type
3438 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_debug_type));
3439 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_debug_type);
3441 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3442 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3443 = Reverse_Storage_Order (gnat_entity);
3444 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3446 /* Set the size, alignment and alias set of the type to match
3447 those of the base type, doing required substitutions. */
3448 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3449 gnu_subst_list);
3451 if (TYPE_IS_PADDING_P (gnu_base_type))
3452 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3453 else
3454 gnu_unpad_base_type = gnu_base_type;
3456 /* Set the layout of the type to match that of the base type,
3457 doing required substitutions. We will output debug info
3458 manually below so pass false as last argument. */
3459 copy_and_substitute_in_layout (gnat_entity, gnat_base_type,
3460 gnu_type, gnu_unpad_base_type,
3461 gnu_subst_list, false);
3463 /* Fill in locations of fields. */
3464 annotate_rep (gnat_entity, gnu_type);
3466 /* If debugging information is being written for the type and if
3467 we are asked to output such encodings, write a record that
3468 shows what we are a subtype of and also make a variable that
3469 indicates our size, if still variable. */
3470 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3472 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3473 tree gnu_unpad_base_name
3474 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3475 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3477 TYPE_NAME (gnu_subtype_marker)
3478 = create_concat_name (gnat_entity, "XVS");
3479 finish_record_type (gnu_subtype_marker,
3480 create_field_decl (gnu_unpad_base_name,
3481 build_reference_type
3482 (gnu_unpad_base_type),
3483 gnu_subtype_marker,
3484 NULL_TREE, NULL_TREE,
3485 0, 0),
3486 0, true);
3488 add_parallel_type (gnu_type, gnu_subtype_marker);
3490 if (definition
3491 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3492 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3493 TYPE_SIZE_UNIT (gnu_subtype_marker)
3494 = create_var_decl (create_concat_name (gnat_entity,
3495 "XVZ"),
3496 NULL_TREE, sizetype, gnu_size_unit,
3497 false, false, false, false, false,
3498 true, debug_info_p,
3499 NULL, gnat_entity);
3503 /* Otherwise, go down all the components in the new type and make
3504 them equivalent to those in the base type. */
3505 else
3507 gnu_type = gnu_base_type;
3509 for (gnat_temp = First_Entity (gnat_entity);
3510 Present (gnat_temp);
3511 gnat_temp = Next_Entity (gnat_temp))
3512 if ((Ekind (gnat_temp) == E_Discriminant
3513 && !Is_Unchecked_Union (gnat_base_type))
3514 || Ekind (gnat_temp) == E_Component)
3515 save_gnu_tree (gnat_temp,
3516 gnat_to_gnu_field_decl
3517 (Original_Record_Component (gnat_temp)),
3518 false);
3521 break;
3523 case E_Access_Subprogram_Type:
3524 case E_Anonymous_Access_Subprogram_Type:
3525 /* Use the special descriptor type for dispatch tables if needed,
3526 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3527 Note that we are only required to do so for static tables in
3528 order to be compatible with the C++ ABI, but Ada 2005 allows
3529 to extend library level tagged types at the local level so
3530 we do it in the non-static case as well. */
3531 if (TARGET_VTABLE_USES_DESCRIPTORS
3532 && Is_Dispatch_Table_Entity (gnat_entity))
3534 gnu_type = fdesc_type_node;
3535 gnu_size = TYPE_SIZE (gnu_type);
3536 break;
3539 /* ... fall through ... */
3541 case E_Allocator_Type:
3542 case E_Access_Type:
3543 case E_Access_Attribute_Type:
3544 case E_Anonymous_Access_Type:
3545 case E_General_Access_Type:
3547 /* The designated type and its equivalent type for gigi. */
3548 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3549 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3550 /* Whether it comes from a limited with. */
3551 const bool is_from_limited_with
3552 = (Is_Incomplete_Type (gnat_desig_equiv)
3553 && From_Limited_With (gnat_desig_equiv));
3554 /* Whether it is a completed Taft Amendment type. Such a type is to
3555 be treated as coming from a limited with clause if it is not in
3556 the main unit, i.e. we break potential circularities here in case
3557 the body of an external unit is loaded for inter-unit inlining. */
3558 const bool is_completed_taft_type
3559 = (Is_Incomplete_Type (gnat_desig_equiv)
3560 && Has_Completion_In_Body (gnat_desig_equiv)
3561 && Present (Full_View (gnat_desig_equiv)));
3562 /* The "full view" of the designated type. If this is an incomplete
3563 entity from a limited with, treat its non-limited view as the full
3564 view. Otherwise, if this is an incomplete or private type, use the
3565 full view. In the former case, we might point to a private type,
3566 in which case, we need its full view. Also, we want to look at the
3567 actual type used for the representation, so this takes a total of
3568 three steps. */
3569 Entity_Id gnat_desig_full_direct_first
3570 = (is_from_limited_with
3571 ? Non_Limited_View (gnat_desig_equiv)
3572 : (Is_Incomplete_Or_Private_Type (gnat_desig_equiv)
3573 ? Full_View (gnat_desig_equiv) : Empty));
3574 Entity_Id gnat_desig_full_direct
3575 = ((is_from_limited_with
3576 && Present (gnat_desig_full_direct_first)
3577 && Is_Private_Type (gnat_desig_full_direct_first))
3578 ? Full_View (gnat_desig_full_direct_first)
3579 : gnat_desig_full_direct_first);
3580 Entity_Id gnat_desig_full
3581 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3582 /* The type actually used to represent the designated type, either
3583 gnat_desig_full or gnat_desig_equiv. */
3584 Entity_Id gnat_desig_rep;
3585 /* We want to know if we'll be seeing the freeze node for any
3586 incomplete type we may be pointing to. */
3587 const bool in_main_unit
3588 = (Present (gnat_desig_full)
3589 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3590 : In_Extended_Main_Code_Unit (gnat_desig_type));
3591 /* True if we make a dummy type here. */
3592 bool made_dummy = false;
3593 /* The mode to be used for the pointer type. */
3594 scalar_int_mode p_mode;
3595 /* The GCC type used for the designated type. */
3596 tree gnu_desig_type = NULL_TREE;
3598 if (!int_mode_for_size (esize, 0).exists (&p_mode)
3599 || !targetm.valid_pointer_mode (p_mode))
3600 p_mode = ptr_mode;
3602 /* If either the designated type or its full view is an unconstrained
3603 array subtype, replace it with the type it's a subtype of. This
3604 avoids problems with multiple copies of unconstrained array types.
3605 Likewise, if the designated type is a subtype of an incomplete
3606 record type, use the parent type to avoid order of elaboration
3607 issues. This can lose some code efficiency, but there is no
3608 alternative. */
3609 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3610 && !Is_Constrained (gnat_desig_equiv))
3611 gnat_desig_equiv = Etype (gnat_desig_equiv);
3612 if (Present (gnat_desig_full)
3613 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3614 && !Is_Constrained (gnat_desig_full))
3615 || (Ekind (gnat_desig_full) == E_Record_Subtype
3616 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3617 gnat_desig_full = Etype (gnat_desig_full);
3619 /* Set the type that's the representation of the designated type. */
3620 gnat_desig_rep
3621 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3623 /* If we already know what the full type is, use it. */
3624 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3625 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3627 /* Get the type of the thing we are to point to and build a pointer to
3628 it. If it is a reference to an incomplete or private type with a
3629 full view that is a record, an array or an access, make a dummy type
3630 and get the actual type later when we have verified it is safe. */
3631 else if ((!in_main_unit
3632 && !present_gnu_tree (gnat_desig_equiv)
3633 && Present (gnat_desig_full)
3634 && (Is_Record_Type (gnat_desig_full)
3635 || Is_Array_Type (gnat_desig_full)
3636 || Is_Access_Type (gnat_desig_full)))
3637 /* Likewise if this is a reference to a record, an array or a
3638 subprogram type and we are to defer elaborating incomplete
3639 types. We do this because this access type may be the full
3640 view of a private type. */
3641 || ((!in_main_unit || imported_p)
3642 && defer_incomplete_level != 0
3643 && !present_gnu_tree (gnat_desig_equiv)
3644 && (Is_Record_Type (gnat_desig_rep)
3645 || Is_Array_Type (gnat_desig_rep)
3646 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3647 /* If this is a reference from a limited_with type back to our
3648 main unit and there's a freeze node for it, either we have
3649 already processed the declaration and made the dummy type,
3650 in which case we just reuse the latter, or we have not yet,
3651 in which case we make the dummy type and it will be reused
3652 when the declaration is finally processed. In both cases,
3653 the pointer eventually created below will be automatically
3654 adjusted when the freeze node is processed. */
3655 || (in_main_unit
3656 && is_from_limited_with
3657 && Present (Freeze_Node (gnat_desig_rep))))
3659 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3660 made_dummy = true;
3663 /* Otherwise handle the case of a pointer to itself. */
3664 else if (gnat_desig_equiv == gnat_entity)
3666 gnu_type
3667 = build_pointer_type_for_mode (void_type_node, p_mode,
3668 No_Strict_Aliasing (gnat_entity));
3669 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3672 /* If expansion is disabled, the equivalent type of a concurrent type
3673 is absent, so we use the void pointer type. */
3674 else if (type_annotate_only && No (gnat_desig_equiv))
3675 gnu_type = ptr_type_node;
3677 /* If the ultimately designated type is an incomplete type with no full
3678 view, we use the void pointer type in LTO mode to avoid emitting a
3679 dummy type in the GIMPLE IR. We cannot do that in regular mode as
3680 the name of the dummy type in used by GDB for a global lookup. */
3681 else if (Ekind (gnat_desig_rep) == E_Incomplete_Type
3682 && No (Full_View (gnat_desig_rep))
3683 && flag_generate_lto)
3684 gnu_type = ptr_type_node;
3686 /* Finally, handle the default case where we can just elaborate our
3687 designated type. */
3688 else
3689 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3691 /* It is possible that a call to gnat_to_gnu_type above resolved our
3692 type. If so, just return it. */
3693 if (present_gnu_tree (gnat_entity))
3695 maybe_present = true;
3696 break;
3699 /* Access-to-unconstrained-array types need a special treatment. */
3700 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3702 /* If the processing above got something that has a pointer, then
3703 we are done. This could have happened either because the type
3704 was elaborated or because somebody else executed the code. */
3705 if (!TYPE_POINTER_TO (gnu_desig_type))
3706 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3708 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3711 /* If we haven't done it yet, build the pointer type the usual way. */
3712 else if (!gnu_type)
3714 /* Modify the designated type if we are pointing only to constant
3715 objects, but don't do it for a dummy type. */
3716 if (Is_Access_Constant (gnat_entity)
3717 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3718 gnu_desig_type
3719 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3721 gnu_type
3722 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3723 No_Strict_Aliasing (gnat_entity));
3726 /* If the designated type is not declared in the main unit and we made
3727 a dummy node for it, save our definition, elaborate the actual type
3728 and replace the dummy type we made with the actual one. But if we
3729 are to defer actually looking up the actual type, make an entry in
3730 the deferred list instead. If this is from a limited with, we may
3731 have to defer until the end of the current unit. */
3732 if (!in_main_unit && made_dummy)
3734 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3735 gnu_type
3736 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3738 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3739 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3740 artificial_p, debug_info_p,
3741 gnat_entity);
3742 this_made_decl = true;
3743 gnu_type = TREE_TYPE (gnu_decl);
3744 save_gnu_tree (gnat_entity, gnu_decl, false);
3745 saved = true;
3747 if (defer_incomplete_level == 0
3748 && !is_from_limited_with
3749 && !is_completed_taft_type)
3751 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3752 gnat_to_gnu_type (gnat_desig_equiv));
3754 else
3756 struct incomplete *p = XNEW (struct incomplete);
3757 struct incomplete **head
3758 = (is_from_limited_with || is_completed_taft_type
3759 ? &defer_limited_with_list : &defer_incomplete_list);
3761 p->old_type = gnu_desig_type;
3762 p->full_type = gnat_desig_equiv;
3763 p->next = *head;
3764 *head = p;
3768 break;
3770 case E_Access_Protected_Subprogram_Type:
3771 case E_Anonymous_Access_Protected_Subprogram_Type:
3772 /* If we are just annotating types and have no equivalent record type,
3773 just use the void pointer type. */
3774 if (type_annotate_only && gnat_equiv_type == gnat_entity)
3775 gnu_type = ptr_type_node;
3777 /* The run-time representation is the equivalent type. */
3778 else
3780 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3781 maybe_present = true;
3784 /* The designated subtype must be elaborated as well, if it does
3785 not have its own freeze node. */
3786 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3787 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3788 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3789 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3790 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3791 NULL_TREE, false);
3793 break;
3795 case E_Access_Subtype:
3796 /* We treat this as identical to its base type; any constraint is
3797 meaningful only to the front-end. */
3798 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
3799 saved = true;
3801 /* The designated subtype must be elaborated as well, if it does
3802 not have its own freeze node. But designated subtypes created
3803 for constrained components of records with discriminants are
3804 not frozen by the front-end and not elaborated here, because
3805 their use may appear before the base type is frozen and it is
3806 not clear that they are needed in gigi. With the current model,
3807 there is no correct place where they could be elaborated. */
3808 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3809 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3810 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3811 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3813 /* If we are to defer elaborating incomplete types, make a dummy
3814 type node and elaborate it later. */
3815 if (defer_incomplete_level != 0)
3817 struct incomplete *p = XNEW (struct incomplete);
3819 p->old_type
3820 = make_dummy_type (Directly_Designated_Type (gnat_entity));
3821 p->full_type = Directly_Designated_Type (gnat_entity);
3822 p->next = defer_incomplete_list;
3823 defer_incomplete_list = p;
3825 else if (!Is_Incomplete_Or_Private_Type
3826 (Base_Type (Directly_Designated_Type (gnat_entity))))
3827 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3828 NULL_TREE, false);
3830 break;
3832 /* Subprogram Entities
3834 The following access functions are defined for subprograms:
3836 Etype Return type or Standard_Void_Type.
3837 First_Formal The first formal parameter.
3838 Is_Imported Indicates that the subprogram has appeared in
3839 an INTERFACE or IMPORT pragma. For now we
3840 assume that the external language is C.
3841 Is_Exported Likewise but for an EXPORT pragma.
3842 Is_Inlined True if the subprogram is to be inlined.
3844 Each parameter is first checked by calling must_pass_by_ref on its
3845 type to determine if it is passed by reference. For parameters which
3846 are copied in, if they are Ada In Out or Out parameters, their return
3847 value becomes part of a record which becomes the return type of the
3848 function (C function - note that this applies only to Ada procedures
3849 so there is no Ada return type). Additional code to store back the
3850 parameters will be generated on the caller side. This transformation
3851 is done here, not in the front-end.
3853 The intended result of the transformation can be seen from the
3854 equivalent source rewritings that follow:
3856 struct temp {int a,b};
3857 procedure P (A,B: In Out ...) is temp P (int A,B)
3858 begin {
3859 .. ..
3860 end P; return {A,B};
3863 temp t;
3864 P(X,Y); t = P(X,Y);
3865 X = t.a , Y = t.b;
3867 For subprogram types we need to perform mainly the same conversions to
3868 GCC form that are needed for procedures and function declarations. The
3869 only difference is that at the end, we make a type declaration instead
3870 of a function declaration. */
3872 case E_Subprogram_Type:
3873 case E_Function:
3874 case E_Procedure:
3876 tree gnu_ext_name
3877 = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name);
3878 enum inline_status_t inline_status
3879 = Has_Pragma_No_Inline (gnat_entity)
3880 ? is_suppressed
3881 : Has_Pragma_Inline_Always (gnat_entity)
3882 ? is_required
3883 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
3884 bool public_flag = Is_Public (gnat_entity) || imported_p;
3885 /* Subprograms marked both Intrinsic and Always_Inline need not
3886 have a body of their own. */
3887 bool extern_flag
3888 = ((Is_Public (gnat_entity) && !definition)
3889 || imported_p
3890 || (Convention (gnat_entity) == Convention_Intrinsic
3891 && Has_Pragma_Inline_Always (gnat_entity)));
3892 tree gnu_param_list;
3894 /* A parameter may refer to this type, so defer completion of any
3895 incomplete types. */
3896 if (kind == E_Subprogram_Type && !definition)
3898 defer_incomplete_level++;
3899 this_deferred = true;
3902 /* If the subprogram has an alias, it is probably inherited, so
3903 we can use the original one. If the original "subprogram"
3904 is actually an enumeration literal, it may be the first use
3905 of its type, so we must elaborate that type now. */
3906 if (Present (Alias (gnat_entity)))
3908 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
3910 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3911 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
3912 false);
3914 gnu_decl
3915 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
3917 /* Elaborate any Itypes in the parameters of this entity. */
3918 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3919 Present (gnat_temp);
3920 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3921 if (Is_Itype (Etype (gnat_temp)))
3922 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3924 /* Materialize renamed subprograms in the debugging information
3925 when the renamed object is compile time known. We can consider
3926 such renamings as imported declarations.
3928 Because the parameters in generics instantiation are generally
3929 materialized as renamings, we ofter end up having both the
3930 renamed subprogram and the renaming in the same context and with
3931 the same name: in this case, renaming is both useless debug-wise
3932 and potentially harmful as name resolution in the debugger could
3933 return twice the same entity! So avoid this case. */
3934 if (debug_info_p && !artificial_p
3935 && !(get_debug_scope (gnat_entity, NULL)
3936 == get_debug_scope (gnat_renamed, NULL)
3937 && Name_Equals (Chars (gnat_entity),
3938 Chars (gnat_renamed)))
3939 && Present (gnat_renamed)
3940 && (Ekind (gnat_renamed) == E_Function
3941 || Ekind (gnat_renamed) == E_Procedure)
3942 && gnu_decl
3943 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
3945 tree decl = build_decl (input_location, IMPORTED_DECL,
3946 gnu_entity_name, void_type_node);
3947 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
3948 gnat_pushdecl (decl, gnat_entity);
3951 break;
3954 /* Get the GCC tree for the (underlying) subprogram type. If the
3955 entity is an actual subprogram, also get the parameter list. */
3956 gnu_type
3957 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
3958 &gnu_param_list);
3959 if (DECL_P (gnu_type))
3961 gnu_decl = gnu_type;
3962 gnu_type = TREE_TYPE (gnu_decl);
3963 break;
3966 /* Deal with platform-specific calling conventions. */
3967 if (Has_Stdcall_Convention (gnat_entity))
3968 prepend_one_attribute
3969 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3970 get_identifier ("stdcall"), NULL_TREE,
3971 gnat_entity);
3972 else if (Has_Thiscall_Convention (gnat_entity))
3973 prepend_one_attribute
3974 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3975 get_identifier ("thiscall"), NULL_TREE,
3976 gnat_entity);
3978 /* If we should request stack realignment for a foreign convention
3979 subprogram, do so. Note that this applies to task entry points
3980 in particular. */
3981 if (FOREIGN_FORCE_REALIGN_STACK && foreign)
3982 prepend_one_attribute
3983 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3984 get_identifier ("force_align_arg_pointer"), NULL_TREE,
3985 gnat_entity);
3987 /* Deal with a pragma Linker_Section on a subprogram. */
3988 if ((kind == E_Function || kind == E_Procedure)
3989 && Present (Linker_Section_Pragma (gnat_entity)))
3990 prepend_one_attribute_pragma (&attr_list,
3991 Linker_Section_Pragma (gnat_entity));
3993 /* If we are defining the subprogram and it has an Address clause
3994 we must get the address expression from the saved GCC tree for the
3995 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3996 the address expression here since the front-end has guaranteed
3997 in that case that the elaboration has no effects. If there is
3998 an Address clause and we are not defining the object, just
3999 make it a constant. */
4000 if (Present (Address_Clause (gnat_entity)))
4002 tree gnu_address = NULL_TREE;
4004 if (definition)
4005 gnu_address
4006 = (present_gnu_tree (gnat_entity)
4007 ? get_gnu_tree (gnat_entity)
4008 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4010 save_gnu_tree (gnat_entity, NULL_TREE, false);
4012 /* Convert the type of the object to a reference type that can
4013 alias everything as per RM 13.3(19). */
4014 gnu_type
4015 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4016 if (gnu_address)
4017 gnu_address = convert (gnu_type, gnu_address);
4019 gnu_decl
4020 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4021 gnu_address, false, Is_Public (gnat_entity),
4022 extern_flag, false, false, artificial_p,
4023 debug_info_p, NULL, gnat_entity);
4024 DECL_BY_REF_P (gnu_decl) = 1;
4027 /* If this is a mere subprogram type, just create the declaration. */
4028 else if (kind == E_Subprogram_Type)
4030 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4032 gnu_decl
4033 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4034 debug_info_p, gnat_entity);
4037 /* Otherwise create the subprogram declaration with the external name,
4038 the type and the parameter list. However, if this a reference to
4039 the allocation routines, reuse the canonical declaration nodes as
4040 they come with special properties. */
4041 else
4043 if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl))
4044 gnu_decl = malloc_decl;
4045 else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl))
4046 gnu_decl = realloc_decl;
4047 else
4049 gnu_decl
4050 = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4051 gnu_type, gnu_param_list,
4052 inline_status, public_flag,
4053 extern_flag, artificial_p,
4054 debug_info_p,
4055 definition && imported_p, attr_list,
4056 gnat_entity);
4058 DECL_STUBBED_P (gnu_decl)
4059 = (Convention (gnat_entity) == Convention_Stubbed);
4063 break;
4065 case E_Incomplete_Type:
4066 case E_Incomplete_Subtype:
4067 case E_Private_Type:
4068 case E_Private_Subtype:
4069 case E_Limited_Private_Type:
4070 case E_Limited_Private_Subtype:
4071 case E_Record_Type_With_Private:
4072 case E_Record_Subtype_With_Private:
4074 const bool is_from_limited_with
4075 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4076 /* Get the "full view" of this entity. If this is an incomplete
4077 entity from a limited with, treat its non-limited view as the
4078 full view. Otherwise, use either the full view or the underlying
4079 full view, whichever is present. This is used in all the tests
4080 below. */
4081 const Entity_Id full_view
4082 = is_from_limited_with
4083 ? Non_Limited_View (gnat_entity)
4084 : Present (Full_View (gnat_entity))
4085 ? Full_View (gnat_entity)
4086 : IN (kind, Private_Kind)
4087 ? Underlying_Full_View (gnat_entity)
4088 : Empty;
4090 /* If this is an incomplete type with no full view, it must be a Taft
4091 Amendment type or an incomplete type coming from a limited context,
4092 in which cases we return a dummy type. Otherwise, we just get the
4093 type from its Etype. */
4094 if (No (full_view))
4096 if (kind == E_Incomplete_Type)
4098 gnu_type = make_dummy_type (gnat_entity);
4099 gnu_decl = TYPE_STUB_DECL (gnu_type);
4101 else
4103 gnu_decl
4104 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4105 maybe_present = true;
4109 /* Or else, if we already made a type for the full view, reuse it. */
4110 else if (present_gnu_tree (full_view))
4111 gnu_decl = get_gnu_tree (full_view);
4113 /* Or else, if we are not defining the type or there is no freeze
4114 node on it, get the type for the full view. Likewise if this is
4115 a limited_with'ed type not declared in the main unit, which can
4116 happen for incomplete formal types instantiated on a type coming
4117 from a limited_with clause. */
4118 else if (!definition
4119 || No (Freeze_Node (full_view))
4120 || (is_from_limited_with
4121 && !In_Extended_Main_Code_Unit (full_view)))
4123 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4124 maybe_present = true;
4127 /* Otherwise, make a dummy type entry which will be replaced later.
4128 Save it as the full declaration's type so we can do any needed
4129 updates when we see it. */
4130 else
4132 gnu_type = make_dummy_type (gnat_entity);
4133 gnu_decl = TYPE_STUB_DECL (gnu_type);
4134 if (Has_Completion_In_Body (gnat_entity))
4135 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4136 save_gnu_tree (full_view, gnu_decl, false);
4139 break;
4141 case E_Class_Wide_Type:
4142 /* Class-wide types are always transformed into their root type. */
4143 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4144 maybe_present = true;
4145 break;
4147 case E_Protected_Type:
4148 case E_Protected_Subtype:
4149 case E_Task_Type:
4150 case E_Task_Subtype:
4151 /* If we are just annotating types and have no equivalent record type,
4152 just return void_type, except for root types that have discriminants
4153 because the discriminants will very likely be used in the declarative
4154 part of the associated body so they need to be translated. */
4155 if (type_annotate_only && gnat_equiv_type == gnat_entity)
4157 if (Has_Discriminants (gnat_entity)
4158 && Root_Type (gnat_entity) == gnat_entity)
4160 tree gnu_field_list = NULL_TREE;
4161 Entity_Id gnat_field;
4163 /* This is a minimal version of the E_Record_Type handling. */
4164 gnu_type = make_node (RECORD_TYPE);
4165 TYPE_NAME (gnu_type) = gnu_entity_name;
4167 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4168 Present (gnat_field);
4169 gnat_field = Next_Stored_Discriminant (gnat_field))
4171 tree gnu_field
4172 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4173 definition, debug_info_p);
4175 save_gnu_tree (gnat_field,
4176 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4177 build0 (PLACEHOLDER_EXPR, gnu_type),
4178 gnu_field, NULL_TREE),
4179 true);
4181 DECL_CHAIN (gnu_field) = gnu_field_list;
4182 gnu_field_list = gnu_field;
4185 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4186 false);
4188 else
4189 gnu_type = void_type_node;
4192 /* Concurrent types are always transformed into their record type. */
4193 else
4194 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4195 maybe_present = true;
4196 break;
4198 case E_Label:
4199 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4200 break;
4202 case E_Block:
4203 case E_Loop:
4204 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4205 we've already saved it, so we don't try to. */
4206 gnu_decl = error_mark_node;
4207 saved = true;
4208 break;
4210 case E_Abstract_State:
4211 /* This is a SPARK annotation that only reaches here when compiling in
4212 ASIS mode. */
4213 gcc_assert (type_annotate_only);
4214 gnu_decl = error_mark_node;
4215 saved = true;
4216 break;
4218 default:
4219 gcc_unreachable ();
4222 /* If we had a case where we evaluated another type and it might have
4223 defined this one, handle it here. */
4224 if (maybe_present && present_gnu_tree (gnat_entity))
4226 gnu_decl = get_gnu_tree (gnat_entity);
4227 saved = true;
4230 /* If we are processing a type and there is either no decl for it or
4231 we just made one, do some common processing for the type, such as
4232 handling alignment and possible padding. */
4233 if (is_type && (!gnu_decl || this_made_decl))
4235 gcc_assert (!TYPE_IS_DUMMY_P (gnu_type));
4237 /* Process the attributes, if not already done. Note that the type is
4238 already defined so we cannot pass true for IN_PLACE here. */
4239 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4241 /* ??? Don't set the size for a String_Literal since it is either
4242 confirming or we don't handle it properly (if the low bound is
4243 non-constant). */
4244 if (!gnu_size && kind != E_String_Literal_Subtype)
4246 Uint gnat_size = Known_Esize (gnat_entity)
4247 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4248 gnu_size
4249 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4250 false, Has_Size_Clause (gnat_entity));
4253 /* If a size was specified, see if we can make a new type of that size
4254 by rearranging the type, for example from a fat to a thin pointer. */
4255 if (gnu_size)
4257 gnu_type
4258 = make_type_from_size (gnu_type, gnu_size,
4259 Has_Biased_Representation (gnat_entity));
4261 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4262 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4263 gnu_size = NULL_TREE;
4266 /* If the alignment has not already been processed and this is not
4267 an unconstrained array type, see if an alignment is specified.
4268 If not, we pick a default alignment for atomic objects. */
4269 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4271 else if (Known_Alignment (gnat_entity))
4273 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4274 TYPE_ALIGN (gnu_type));
4276 /* Warn on suspiciously large alignments. This should catch
4277 errors about the (alignment,byte)/(size,bit) discrepancy. */
4278 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4280 tree size;
4282 /* If a size was specified, take it into account. Otherwise
4283 use the RM size for records or unions as the type size has
4284 already been adjusted to the alignment. */
4285 if (gnu_size)
4286 size = gnu_size;
4287 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4288 && !TYPE_FAT_POINTER_P (gnu_type))
4289 size = rm_size (gnu_type);
4290 else
4291 size = TYPE_SIZE (gnu_type);
4293 /* Consider an alignment as suspicious if the alignment/size
4294 ratio is greater or equal to the byte/bit ratio. */
4295 if (tree_fits_uhwi_p (size)
4296 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4297 post_error_ne ("?suspiciously large alignment specified for&",
4298 Expression (Alignment_Clause (gnat_entity)),
4299 gnat_entity);
4302 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4303 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4304 && integer_pow2p (TYPE_SIZE (gnu_type)))
4305 align = MIN (BIGGEST_ALIGNMENT,
4306 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4307 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4308 && tree_fits_uhwi_p (gnu_size)
4309 && integer_pow2p (gnu_size))
4310 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4312 /* See if we need to pad the type. If we did, and made a record,
4313 the name of the new type may be changed. So get it back for
4314 us when we make the new TYPE_DECL below. */
4315 if (gnu_size || align > 0)
4316 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4317 false, !gnu_decl, definition, false);
4319 if (TYPE_IS_PADDING_P (gnu_type))
4320 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4322 /* Now set the RM size of the type. We cannot do it before padding
4323 because we need to accept arbitrary RM sizes on integral types. */
4324 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4326 /* If we are at global level, GCC will have applied variable_size to
4327 the type, but that won't have done anything. So, if it's not
4328 a constant or self-referential, call elaborate_expression_1 to
4329 make a variable for the size rather than calculating it each time.
4330 Handle both the RM size and the actual size. */
4331 if (TYPE_SIZE (gnu_type)
4332 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4333 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
4334 && global_bindings_p ())
4336 tree size = TYPE_SIZE (gnu_type);
4338 TYPE_SIZE (gnu_type)
4339 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4340 false);
4342 /* ??? For now, store the size as a multiple of the alignment in
4343 bytes so that we can see the alignment from the tree. */
4344 TYPE_SIZE_UNIT (gnu_type)
4345 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4346 "SIZE_A_UNIT", definition, false,
4347 TYPE_ALIGN (gnu_type));
4349 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4350 may not be marked by the call to create_type_decl below. */
4351 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4353 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4355 tree variant_part = get_variant_part (gnu_type);
4356 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4358 if (variant_part)
4360 tree union_type = TREE_TYPE (variant_part);
4361 tree offset = DECL_FIELD_OFFSET (variant_part);
4363 /* If the position of the variant part is constant, subtract
4364 it from the size of the type of the parent to get the new
4365 size. This manual CSE reduces the data size. */
4366 if (TREE_CODE (offset) == INTEGER_CST)
4368 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4369 TYPE_SIZE (union_type)
4370 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4371 bit_from_pos (offset, bitpos));
4372 TYPE_SIZE_UNIT (union_type)
4373 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4374 byte_from_pos (offset, bitpos));
4376 else
4378 TYPE_SIZE (union_type)
4379 = elaborate_expression_1 (TYPE_SIZE (union_type),
4380 gnat_entity, "VSIZE",
4381 definition, false);
4383 /* ??? For now, store the size as a multiple of the
4384 alignment in bytes so that we can see the alignment
4385 from the tree. */
4386 TYPE_SIZE_UNIT (union_type)
4387 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4388 gnat_entity, "VSIZE_A_UNIT",
4389 definition, false,
4390 TYPE_ALIGN (union_type));
4392 /* ??? For now, store the offset as a multiple of the
4393 alignment in bytes so that we can see the alignment
4394 from the tree. */
4395 DECL_FIELD_OFFSET (variant_part)
4396 = elaborate_expression_2 (offset, gnat_entity,
4397 "VOFFSET", definition, false,
4398 DECL_OFFSET_ALIGN
4399 (variant_part));
4402 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4403 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4406 if (operand_equal_p (ada_size, size, 0))
4407 ada_size = TYPE_SIZE (gnu_type);
4408 else
4409 ada_size
4410 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4411 definition, false);
4412 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4416 /* Similarly, if this is a record type or subtype at global level, call
4417 elaborate_expression_2 on any field position. Skip any fields that
4418 we haven't made trees for to avoid problems with class-wide types. */
4419 if (IN (kind, Record_Kind) && global_bindings_p ())
4420 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4421 gnat_temp = Next_Entity (gnat_temp))
4422 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4424 tree gnu_field = get_gnu_tree (gnat_temp);
4426 /* ??? For now, store the offset as a multiple of the alignment
4427 in bytes so that we can see the alignment from the tree. */
4428 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field))
4429 && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4431 DECL_FIELD_OFFSET (gnu_field)
4432 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4433 gnat_temp, "OFFSET", definition,
4434 false,
4435 DECL_OFFSET_ALIGN (gnu_field));
4437 /* ??? The context of gnu_field is not necessarily gnu_type
4438 so the MULT_EXPR node built above may not be marked by
4439 the call to create_type_decl below. */
4440 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4444 if (Is_Atomic_Or_VFA (gnat_entity))
4445 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4447 /* If this is not an unconstrained array type, set some flags. */
4448 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4450 /* Tell the middle-end that objects of tagged types are guaranteed to
4451 be properly aligned. This is necessary because conversions to the
4452 class-wide type are translated into conversions to the root type,
4453 which can be less aligned than some of its derived types. */
4454 if (Is_Tagged_Type (gnat_entity)
4455 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4456 TYPE_ALIGN_OK (gnu_type) = 1;
4458 /* Record whether the type is passed by reference. */
4459 if (Is_By_Reference_Type (gnat_entity) && !VOID_TYPE_P (gnu_type))
4460 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4462 /* Record whether an alignment clause was specified. */
4463 if (Present (Alignment_Clause (gnat_entity)))
4464 TYPE_USER_ALIGN (gnu_type) = 1;
4466 /* Record whether a pragma Universal_Aliasing was specified. */
4467 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4468 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4470 /* If it is passed by reference, force BLKmode to ensure that
4471 objects of this type will always be put in memory. */
4472 if (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type))
4473 SET_TYPE_MODE (gnu_type, BLKmode);
4476 /* If this is a derived type, relate its alias set to that of its parent
4477 to avoid troubles when a call to an inherited primitive is inlined in
4478 a context where a derived object is accessed. The inlined code works
4479 on the parent view so the resulting code may access the same object
4480 using both the parent and the derived alias sets, which thus have to
4481 conflict. As the same issue arises with component references, the
4482 parent alias set also has to conflict with composite types enclosing
4483 derived components. For instance, if we have:
4485 type D is new T;
4486 type R is record
4487 Component : D;
4488 end record;
4490 we want T to conflict with both D and R, in addition to R being a
4491 superset of D by record/component construction.
4493 One way to achieve this is to perform an alias set copy from the
4494 parent to the derived type. This is not quite appropriate, though,
4495 as we don't want separate derived types to conflict with each other:
4497 type I1 is new Integer;
4498 type I2 is new Integer;
4500 We want I1 and I2 to both conflict with Integer but we do not want
4501 I1 to conflict with I2, and an alias set copy on derivation would
4502 have that effect.
4504 The option chosen is to make the alias set of the derived type a
4505 superset of that of its parent type. It trivially fulfills the
4506 simple requirement for the Integer derivation example above, and
4507 the component case as well by superset transitivity:
4509 superset superset
4510 R ----------> D ----------> T
4512 However, for composite types, conversions between derived types are
4513 translated into VIEW_CONVERT_EXPRs so a sequence like:
4515 type Comp1 is new Comp;
4516 type Comp2 is new Comp;
4517 procedure Proc (C : Comp1);
4519 C : Comp2;
4520 Proc (Comp1 (C));
4522 is translated into:
4524 C : Comp2;
4525 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4527 and gimplified into:
4529 C : Comp2;
4530 Comp1 *C.0;
4531 C.0 = (Comp1 *) &C;
4532 Proc (C.0);
4534 i.e. generates code involving type punning. Therefore, Comp1 needs
4535 to conflict with Comp2 and an alias set copy is required.
4537 The language rules ensure the parent type is already frozen here. */
4538 if (kind != E_Subprogram_Type
4539 && Is_Derived_Type (gnat_entity)
4540 && !type_annotate_only)
4542 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4543 /* For constrained packed array subtypes, the implementation type is
4544 used instead of the nominal type. */
4545 if (kind == E_Array_Subtype
4546 && Is_Constrained (gnat_entity)
4547 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4548 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4549 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4550 Is_Composite_Type (gnat_entity)
4551 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4554 /* Finally get to the appropriate variant, except for the implementation
4555 type of a packed array because the GNU type might be further adjusted
4556 when the original array type is itself processed. */
4557 if (Treat_As_Volatile (gnat_entity)
4558 && !Is_Packed_Array_Impl_Type (gnat_entity))
4560 const int quals
4561 = TYPE_QUAL_VOLATILE
4562 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4563 gnu_type = change_qualified_type (gnu_type, quals);
4566 /* If we already made a decl, just set the type, otherwise create it. */
4567 if (gnu_decl)
4569 TREE_TYPE (gnu_decl) = gnu_type;
4570 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4572 else
4573 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4574 debug_info_p, gnat_entity);
4577 /* If we got a type that is not dummy, back-annotate the alignment of the
4578 type if not already in the tree. Likewise for the size, if any. */
4579 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4581 gnu_type = TREE_TYPE (gnu_decl);
4583 if (Unknown_Alignment (gnat_entity))
4585 unsigned int double_align, align;
4586 bool is_capped_double, align_clause;
4588 /* If the default alignment of "double" or larger scalar types is
4589 specifically capped and this is not an array with an alignment
4590 clause on the component type, return the cap. */
4591 if ((double_align = double_float_alignment) > 0)
4592 is_capped_double
4593 = is_double_float_or_array (gnat_entity, &align_clause);
4594 else if ((double_align = double_scalar_alignment) > 0)
4595 is_capped_double
4596 = is_double_scalar_or_array (gnat_entity, &align_clause);
4597 else
4598 is_capped_double = align_clause = false;
4600 if (is_capped_double && !align_clause)
4601 align = double_align;
4602 else
4603 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4605 Set_Alignment (gnat_entity, UI_From_Int (align));
4608 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4610 tree gnu_size = TYPE_SIZE (gnu_type);
4612 /* If the size is self-referential, annotate the maximum value. */
4613 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4614 gnu_size = max_size (gnu_size, true);
4616 /* If we are just annotating types and the type is tagged, the tag
4617 and the parent components are not generated by the front-end so
4618 alignment and sizes must be adjusted if there is no rep clause. */
4619 if (type_annotate_only
4620 && Is_Tagged_Type (gnat_entity)
4621 && Unknown_RM_Size (gnat_entity)
4622 && !VOID_TYPE_P (gnu_type)
4623 && (!TYPE_FIELDS (gnu_type)
4624 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4626 tree offset;
4628 if (Is_Derived_Type (gnat_entity))
4630 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4631 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4632 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4634 else
4636 unsigned int align
4637 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4638 offset = bitsize_int (POINTER_SIZE);
4639 Set_Alignment (gnat_entity, UI_From_Int (align));
4642 if (TYPE_FIELDS (gnu_type))
4643 offset
4644 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4646 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4647 gnu_size = round_up (gnu_size, POINTER_SIZE);
4648 Uint uint_size = annotate_value (gnu_size);
4649 Set_RM_Size (gnat_entity, uint_size);
4650 Set_Esize (gnat_entity, uint_size);
4653 /* If there is a rep clause, only adjust alignment and Esize. */
4654 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4656 unsigned int align
4657 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4658 Set_Alignment (gnat_entity, UI_From_Int (align));
4659 gnu_size = round_up (gnu_size, POINTER_SIZE);
4660 Set_Esize (gnat_entity, annotate_value (gnu_size));
4663 /* Otherwise no adjustment is needed. */
4664 else
4665 Set_Esize (gnat_entity, annotate_value (gnu_size));
4668 if (Unknown_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type))
4669 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4672 /* If we haven't already, associate the ..._DECL node that we just made with
4673 the input GNAT entity node. */
4674 if (!saved)
4675 save_gnu_tree (gnat_entity, gnu_decl, false);
4677 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4678 eliminate as many deferred computations as possible. */
4679 process_deferred_decl_context (false);
4681 /* If this is an enumeration or floating-point type, we were not able to set
4682 the bounds since they refer to the type. These are always static. */
4683 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4684 || (kind == E_Floating_Point_Type))
4686 tree gnu_scalar_type = gnu_type;
4687 tree gnu_low_bound, gnu_high_bound;
4689 /* If this is a padded type, we need to use the underlying type. */
4690 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4691 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4693 /* If this is a floating point type and we haven't set a floating
4694 point type yet, use this in the evaluation of the bounds. */
4695 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4696 longest_float_type_node = gnu_scalar_type;
4698 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4699 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4701 if (kind == E_Enumeration_Type)
4703 /* Enumeration types have specific RM bounds. */
4704 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4705 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4707 else
4709 /* Floating-point types don't have specific RM bounds. */
4710 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4711 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4715 /* If we deferred processing of incomplete types, re-enable it. If there
4716 were no other disables and we have deferred types to process, do so. */
4717 if (this_deferred
4718 && --defer_incomplete_level == 0
4719 && defer_incomplete_list)
4721 struct incomplete *p, *next;
4723 /* We are back to level 0 for the deferring of incomplete types.
4724 But processing these incomplete types below may itself require
4725 deferring, so preserve what we have and restart from scratch. */
4726 p = defer_incomplete_list;
4727 defer_incomplete_list = NULL;
4729 for (; p; p = next)
4731 next = p->next;
4733 if (p->old_type)
4734 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4735 gnat_to_gnu_type (p->full_type));
4736 free (p);
4740 /* If we are not defining this type, see if it's on one of the lists of
4741 incomplete types. If so, handle the list entry now. */
4742 if (is_type && !definition)
4744 struct incomplete *p;
4746 for (p = defer_incomplete_list; p; p = p->next)
4747 if (p->old_type && p->full_type == gnat_entity)
4749 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4750 TREE_TYPE (gnu_decl));
4751 p->old_type = NULL_TREE;
4754 for (p = defer_limited_with_list; p; p = p->next)
4755 if (p->old_type
4756 && (Non_Limited_View (p->full_type) == gnat_entity
4757 || Full_View (p->full_type) == gnat_entity))
4759 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4760 TREE_TYPE (gnu_decl));
4761 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4762 update_profiles_with (p->old_type);
4763 p->old_type = NULL_TREE;
4767 if (this_global)
4768 force_global--;
4770 /* If this is a packed array type whose original array type is itself
4771 an Itype without freeze node, make sure the latter is processed. */
4772 if (Is_Packed_Array_Impl_Type (gnat_entity)
4773 && Is_Itype (Original_Array_Type (gnat_entity))
4774 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4775 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4776 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
4778 return gnu_decl;
4781 /* Similar, but if the returned value is a COMPONENT_REF, return the
4782 FIELD_DECL. */
4784 tree
4785 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4787 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4789 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4790 gnu_field = TREE_OPERAND (gnu_field, 1);
4792 return gnu_field;
4795 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4796 the GCC type corresponding to that entity. */
4798 tree
4799 gnat_to_gnu_type (Entity_Id gnat_entity)
4801 tree gnu_decl;
4803 /* The back end never attempts to annotate generic types. */
4804 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4805 return void_type_node;
4807 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
4808 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4810 return TREE_TYPE (gnu_decl);
4813 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4814 the unpadded version of the GCC type corresponding to that entity. */
4816 tree
4817 get_unpadded_type (Entity_Id gnat_entity)
4819 tree type = gnat_to_gnu_type (gnat_entity);
4821 if (TYPE_IS_PADDING_P (type))
4822 type = TREE_TYPE (TYPE_FIELDS (type));
4824 return type;
4827 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
4828 a C++ imported method or equivalent.
4830 We use the predicate on 32-bit x86/Windows to find out whether we need to
4831 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
4832 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
4834 bool
4835 is_cplusplus_method (Entity_Id gnat_entity)
4837 /* A constructor is a method on the C++ side. We deal with it now because
4838 it is declared without the 'this' parameter in the sources and, although
4839 the front-end will create a version with the 'this' parameter for code
4840 generation purposes, we want to return true for both versions. */
4841 if (Is_Constructor (gnat_entity))
4842 return true;
4844 /* Check that the subprogram has C++ convention. */
4845 if (Convention (gnat_entity) != Convention_CPP)
4846 return false;
4848 /* And that the type of the first parameter (indirectly) has it too. */
4849 Entity_Id gnat_first = First_Formal (gnat_entity);
4850 if (No (gnat_first))
4851 return false;
4853 Entity_Id gnat_type = Etype (gnat_first);
4854 if (Is_Access_Type (gnat_type))
4855 gnat_type = Directly_Designated_Type (gnat_type);
4856 if (Convention (gnat_type) != Convention_CPP)
4857 return false;
4859 /* This is the main case: a C++ virtual method imported as a primitive
4860 operation of a tagged type. */
4861 if (Is_Dispatching_Operation (gnat_entity))
4862 return true;
4864 /* This is set on the E_Subprogram_Type built for a dispatching call. */
4865 if (Is_Dispatch_Table_Entity (gnat_entity))
4866 return true;
4868 /* A thunk needs to be handled like its associated primitive operation. */
4869 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
4870 return true;
4872 /* Now on to the annoying case: a C++ non-virtual method, imported either
4873 as a non-primitive operation of a tagged type or as a primitive operation
4874 of an untagged type. We cannot reliably differentiate these cases from
4875 their static member or regular function equivalents in Ada, so we ask
4876 the C++ side through the mangled name of the function, as the implicit
4877 'this' parameter is not encoded in the mangled name of a method. */
4878 if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity)))
4880 String_Pointer sp = { NULL, NULL };
4881 Get_External_Name (gnat_entity, false, sp);
4883 void *mem;
4884 struct demangle_component *cmp
4885 = cplus_demangle_v3_components (Name_Buffer,
4886 DMGL_GNU_V3
4887 | DMGL_TYPES
4888 | DMGL_PARAMS
4889 | DMGL_RET_DROP,
4890 &mem);
4891 if (!cmp)
4892 return false;
4894 /* We need to release MEM once we have a successful demangling. */
4895 bool ret = false;
4897 if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME
4898 && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE
4899 && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL
4900 && cmp->type == DEMANGLE_COMPONENT_ARGLIST)
4902 /* Make sure there is at least one parameter in C++ too. */
4903 if (cmp->u.s_binary.left)
4905 unsigned int n_ada_args = 0;
4906 do {
4907 n_ada_args++;
4908 gnat_first = Next_Formal (gnat_first);
4909 } while (Present (gnat_first));
4911 unsigned int n_cpp_args = 0;
4912 do {
4913 n_cpp_args++;
4914 cmp = cmp->u.s_binary.right;
4915 } while (cmp);
4917 if (n_cpp_args < n_ada_args)
4918 ret = true;
4920 else
4921 ret = true;
4924 free (mem);
4926 return ret;
4929 return false;
4932 /* Finalize the processing of From_Limited_With incomplete types. */
4934 void
4935 finalize_from_limited_with (void)
4937 struct incomplete *p, *next;
4939 p = defer_limited_with_list;
4940 defer_limited_with_list = NULL;
4942 for (; p; p = next)
4944 next = p->next;
4946 if (p->old_type)
4948 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4949 gnat_to_gnu_type (p->full_type));
4950 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
4951 update_profiles_with (p->old_type);
4954 free (p);
4958 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind
4959 of type (such E_Task_Type) that has a different type which Gigi uses
4960 for its representation. If the type does not have a special type for
4961 its representation, return GNAT_ENTITY. */
4963 Entity_Id
4964 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4966 Entity_Id gnat_equiv = gnat_entity;
4968 if (No (gnat_entity))
4969 return gnat_entity;
4971 switch (Ekind (gnat_entity))
4973 case E_Class_Wide_Subtype:
4974 if (Present (Equivalent_Type (gnat_entity)))
4975 gnat_equiv = Equivalent_Type (gnat_entity);
4976 break;
4978 case E_Access_Protected_Subprogram_Type:
4979 case E_Anonymous_Access_Protected_Subprogram_Type:
4980 if (Present (Equivalent_Type (gnat_entity)))
4981 gnat_equiv = Equivalent_Type (gnat_entity);
4982 break;
4984 case E_Class_Wide_Type:
4985 gnat_equiv = Root_Type (gnat_entity);
4986 break;
4988 case E_Protected_Type:
4989 case E_Protected_Subtype:
4990 case E_Task_Type:
4991 case E_Task_Subtype:
4992 if (Present (Corresponding_Record_Type (gnat_entity)))
4993 gnat_equiv = Corresponding_Record_Type (gnat_entity);
4994 break;
4996 default:
4997 break;
5000 return gnat_equiv;
5003 /* Return a GCC tree for a type corresponding to the component type of the
5004 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5005 is for an array being defined. DEBUG_INFO_P is true if we need to write
5006 debug information for other types that we may create in the process. */
5008 static tree
5009 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5010 bool debug_info_p)
5012 const Entity_Id gnat_type = Component_Type (gnat_array);
5013 tree gnu_type = gnat_to_gnu_type (gnat_type);
5014 tree gnu_comp_size;
5015 unsigned int max_align;
5017 /* If an alignment is specified, use it as a cap on the component type
5018 so that it can be honored for the whole type. But ignore it for the
5019 original type of packed array types. */
5020 if (No (Packed_Array_Impl_Type (gnat_array))
5021 && Known_Alignment (gnat_array))
5022 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5023 else
5024 max_align = 0;
5026 /* Try to get a smaller form of the component if needed. */
5027 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5028 && !Is_Bit_Packed_Array (gnat_array)
5029 && !Has_Aliased_Components (gnat_array)
5030 && !Strict_Alignment (gnat_type)
5031 && RECORD_OR_UNION_TYPE_P (gnu_type)
5032 && !TYPE_FAT_POINTER_P (gnu_type)
5033 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5034 gnu_type = make_packable_type (gnu_type, false, max_align);
5036 /* Get and validate any specified Component_Size. */
5037 gnu_comp_size
5038 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5039 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5040 true, Has_Component_Size_Clause (gnat_array));
5042 /* If the component type is a RECORD_TYPE that has a self-referential size,
5043 then use the maximum size for the component size. */
5044 if (!gnu_comp_size
5045 && TREE_CODE (gnu_type) == RECORD_TYPE
5046 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5047 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5049 /* If the array has aliased components and the component size is zero, force
5050 the unit size to ensure that the components have distinct addresses. */
5051 if (!gnu_comp_size
5052 && Has_Aliased_Components (gnat_array)
5053 && integer_zerop (TYPE_SIZE (gnu_type)))
5054 gnu_comp_size = bitsize_unit_node;
5056 /* Honor the component size. This is not needed for bit-packed arrays. */
5057 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5059 tree orig_type = gnu_type;
5061 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5062 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5063 gnu_type = orig_type;
5064 else
5065 orig_type = gnu_type;
5067 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5068 true, false, definition, true);
5070 /* If a padding record was made, declare it now since it will never be
5071 declared otherwise. This is necessary to ensure that its subtrees
5072 are properly marked. */
5073 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5074 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5075 gnat_array);
5078 /* This is a very special case where the array has aliased components and the
5079 component size might be zero at run time. As explained above, we force at
5080 least the unit size but we don't want to build a distinct padding type for
5081 each invocation (they are not canonicalized if they have variable size) so
5082 we cache this special padding type as TYPE_PADDING_FOR_COMPONENT. */
5083 else if (Has_Aliased_Components (gnat_array)
5084 && TREE_CODE (gnu_type) == ARRAY_TYPE
5085 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))
5087 if (TYPE_PADDING_FOR_COMPONENT (gnu_type))
5088 gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type);
5089 else
5091 gnu_comp_size
5092 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5093 TYPE_PADDING_FOR_COMPONENT (gnu_type)
5094 = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5095 true, false, definition, true);
5096 gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type);
5097 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5098 gnat_array);
5102 if (Has_Atomic_Components (gnat_array) || Is_Atomic_Or_VFA (gnat_type))
5103 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5105 /* If the component type is a padded type made for a non-bit-packed array
5106 of scalars with reverse storage order, we need to propagate the reverse
5107 storage order to the padding type since it is the innermost enclosing
5108 aggregate type around the scalar. */
5109 if (TYPE_IS_PADDING_P (gnu_type)
5110 && Reverse_Storage_Order (gnat_array)
5111 && !Is_Bit_Packed_Array (gnat_array)
5112 && Is_Scalar_Type (gnat_type))
5113 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5115 if (Has_Volatile_Components (gnat_array))
5117 const int quals
5118 = TYPE_QUAL_VOLATILE
5119 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5120 gnu_type = change_qualified_type (gnu_type, quals);
5123 return gnu_type;
5126 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5127 in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for
5128 the type of the parameter. FIRST is true if this is the first parameter in
5129 the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use
5130 the copy-in copy-out implementation mechanism.
5132 The returned tree is a PARM_DECL, except for the cases where no parameter
5133 needs to be actually passed to the subprogram; the type of this "shadow"
5134 parameter is then returned instead. */
5136 static tree
5137 gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
5138 Entity_Id gnat_subprog, bool *cico)
5140 Entity_Id gnat_param_type = Etype (gnat_param);
5141 Mechanism_Type mech = Mechanism (gnat_param);
5142 tree gnu_param_name = get_entity_name (gnat_param);
5143 bool foreign = Has_Foreign_Convention (gnat_subprog);
5144 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5145 /* The parameter can be indirectly modified if its address is taken. */
5146 bool ro_param = in_param && !Address_Taken (gnat_param);
5147 bool by_return = false, by_component_ptr = false;
5148 bool by_ref = false;
5149 bool restricted_aliasing_p = false;
5150 location_t saved_location = input_location;
5151 tree gnu_param;
5153 /* Make sure to use the proper SLOC for vector ABI warnings. */
5154 if (VECTOR_TYPE_P (gnu_param_type))
5155 Sloc_to_locus (Sloc (gnat_subprog), &input_location);
5157 /* Builtins are expanded inline and there is no real call sequence involved.
5158 So the type expected by the underlying expander is always the type of the
5159 argument "as is". */
5160 if (Convention (gnat_subprog) == Convention_Intrinsic
5161 && Present (Interface_Name (gnat_subprog)))
5162 mech = By_Copy;
5164 /* Handle the first parameter of a valued procedure specially: it's a copy
5165 mechanism for which the parameter is never allocated. */
5166 else if (first && Is_Valued_Procedure (gnat_subprog))
5168 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5169 mech = By_Copy;
5170 by_return = true;
5173 /* Or else, see if a Mechanism was supplied that forced this parameter
5174 to be passed one way or another. */
5175 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5178 /* Positive mechanism means by copy for sufficiently small parameters. */
5179 else if (mech > 0)
5181 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5182 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5183 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5184 mech = By_Reference;
5185 else
5186 mech = By_Copy;
5189 /* Otherwise, it's an unsupported mechanism so error out. */
5190 else
5192 post_error ("unsupported mechanism for&", gnat_param);
5193 mech = Default;
5196 /* If this is either a foreign function or if the underlying type won't
5197 be passed by reference and is as aligned as the original type, strip
5198 off possible padding type. */
5199 if (TYPE_IS_PADDING_P (gnu_param_type))
5201 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5203 if (foreign
5204 || (!must_pass_by_ref (unpadded_type)
5205 && mech != By_Reference
5206 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5207 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5208 gnu_param_type = unpadded_type;
5211 /* If this is a read-only parameter, make a variant of the type that is
5212 read-only. ??? However, if this is a self-referential type, the type
5213 can be very complex, so skip it for now. */
5214 if (ro_param && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5215 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5217 /* For foreign conventions, pass arrays as pointers to the element type.
5218 First check for unconstrained array and get the underlying array. */
5219 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5220 gnu_param_type
5221 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5223 /* Arrays are passed as pointers to element type for foreign conventions. */
5224 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5226 /* Strip off any multi-dimensional entries, then strip
5227 off the last array to get the component type. */
5228 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5229 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5230 gnu_param_type = TREE_TYPE (gnu_param_type);
5232 by_component_ptr = true;
5233 gnu_param_type = TREE_TYPE (gnu_param_type);
5235 if (ro_param)
5236 gnu_param_type
5237 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5239 gnu_param_type = build_pointer_type (gnu_param_type);
5242 /* Fat pointers are passed as thin pointers for foreign conventions. */
5243 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5244 gnu_param_type
5245 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5247 /* If we were requested or muss pass by reference, do so.
5248 If we were requested to pass by copy, do so.
5249 Otherwise, for foreign conventions, pass In Out or Out parameters
5250 or aggregates by reference. For COBOL and Fortran, pass all
5251 integer and FP types that way too. For Convention Ada, use
5252 the standard Ada default. */
5253 else if (mech == By_Reference
5254 || must_pass_by_ref (gnu_param_type)
5255 || (mech != By_Copy
5256 && ((foreign
5257 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5258 || (foreign
5259 && (Convention (gnat_subprog) == Convention_Fortran
5260 || Convention (gnat_subprog) == Convention_COBOL)
5261 && (INTEGRAL_TYPE_P (gnu_param_type)
5262 || FLOAT_TYPE_P (gnu_param_type)))
5263 || (!foreign
5264 && default_pass_by_ref (gnu_param_type)))))
5266 /* We take advantage of 6.2(12) by considering that references built for
5267 parameters whose type isn't by-ref and for which the mechanism hasn't
5268 been forced to by-ref allow only a restricted form of aliasing. */
5269 restricted_aliasing_p
5270 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5271 gnu_param_type = build_reference_type (gnu_param_type);
5272 by_ref = true;
5275 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5276 else if (!in_param)
5277 *cico = true;
5279 input_location = saved_location;
5281 if (mech == By_Copy && (by_ref || by_component_ptr))
5282 post_error ("?cannot pass & by copy", gnat_param);
5284 /* If this is an Out parameter that isn't passed by reference and isn't
5285 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5286 it will be a VAR_DECL created when we process the procedure, so just
5287 return its type. For the special parameter of a valued procedure,
5288 never pass it in.
5290 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5291 Out parameters with discriminants or implicit initial values to be
5292 handled like In Out parameters. These type are normally built as
5293 aggregates, hence passed by reference, except for some packed arrays
5294 which end up encoded in special integer types. Note that scalars can
5295 be given implicit initial values using the Default_Value aspect.
5297 The exception we need to make is then for packed arrays of records
5298 with discriminants or implicit initial values. We have no light/easy
5299 way to check for the latter case, so we merely check for packed arrays
5300 of records. This may lead to useless copy-in operations, but in very
5301 rare cases only, as these would be exceptions in a set of already
5302 exceptional situations. */
5303 if (Ekind (gnat_param) == E_Out_Parameter
5304 && !by_ref
5305 && (by_return
5306 || (!POINTER_TYPE_P (gnu_param_type)
5307 && !AGGREGATE_TYPE_P (gnu_param_type)
5308 && !Has_Default_Aspect (gnat_param_type)))
5309 && !(Is_Array_Type (gnat_param_type)
5310 && Is_Packed (gnat_param_type)
5311 && Is_Composite_Type (Component_Type (gnat_param_type))))
5312 return gnu_param_type;
5314 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5315 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5316 DECL_BY_REF_P (gnu_param) = by_ref;
5317 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5318 DECL_POINTS_TO_READONLY_P (gnu_param)
5319 = (ro_param && (by_ref || by_component_ptr));
5320 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5321 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5322 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5324 /* If no Mechanism was specified, indicate what we're using, then
5325 back-annotate it. */
5326 if (mech == Default)
5327 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5329 Set_Mechanism (gnat_param, mech);
5330 return gnu_param;
5333 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5334 GNAT_SUBPROG is updated when GNU_TYPE is completed.
5336 Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze
5337 the corresponding profile, which means that, by the time the freeze node
5338 of the subprogram is encountered, types involved in its profile may still
5339 be not yet frozen. That's why we need to update GNAT_SUBPROG when we see
5340 the freeze node of types involved in its profile, either types of formal
5341 parameters or the return type. */
5343 static void
5344 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5346 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5348 struct tree_entity_vec_map in;
5349 in.base.from = gnu_type;
5350 struct tree_entity_vec_map **slot
5351 = dummy_to_subprog_map->find_slot (&in, INSERT);
5352 if (!*slot)
5354 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5355 e->base.from = gnu_type;
5356 e->to = NULL;
5357 *slot = e;
5360 /* Even if there is already a slot for GNU_TYPE, we need to set the flag
5361 because the vector might have been just emptied by update_profiles_with.
5362 This can happen when there are 2 freeze nodes associated with different
5363 views of the same type; the type will be really complete only after the
5364 second freeze node is encountered. */
5365 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5367 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5369 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5370 since this would mean updating twice its profile. */
5371 if (v)
5373 const unsigned len = v->length ();
5374 unsigned int l = 0, u = len;
5376 /* Entity_Id is a simple integer so we can implement a stable order on
5377 the vector with an ordered insertion scheme and binary search. */
5378 while (l < u)
5380 unsigned int m = (l + u) / 2;
5381 int diff = (int) (*v)[m] - (int) gnat_subprog;
5382 if (diff > 0)
5383 u = m;
5384 else if (diff < 0)
5385 l = m + 1;
5386 else
5387 return;
5390 /* l == u and therefore is the insertion point. */
5391 vec_safe_insert (v, l, gnat_subprog);
5393 else
5394 vec_safe_push (v, gnat_subprog);
5396 (*slot)->to = v;
5399 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5401 static void
5402 update_profile (Entity_Id gnat_subprog)
5404 tree gnu_param_list;
5405 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5406 Needs_Debug_Info (gnat_subprog),
5407 &gnu_param_list);
5408 if (DECL_P (gnu_type))
5410 /* Builtins cannot have their address taken so we can reset them. */
5411 gcc_assert (DECL_BUILT_IN (gnu_type));
5412 save_gnu_tree (gnat_subprog, NULL_TREE, false);
5413 save_gnu_tree (gnat_subprog, gnu_type, false);
5414 return;
5417 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5419 TREE_TYPE (gnu_subprog) = gnu_type;
5421 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5422 and needs to be adjusted too. */
5423 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5425 tree gnu_entity_name = get_entity_name (gnat_subprog);
5426 tree gnu_ext_name
5427 = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name);
5429 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5430 finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type);
5434 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5435 a dummy type which appears in profiles. */
5437 void
5438 update_profiles_with (tree gnu_type)
5440 struct tree_entity_vec_map in;
5441 in.base.from = gnu_type;
5442 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5443 gcc_assert (e);
5444 vec<Entity_Id, va_gc_atomic> *v = e->to;
5445 e->to = NULL;
5447 /* The flag needs to be reset before calling update_profile, in case
5448 associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */
5449 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5451 unsigned int i;
5452 Entity_Id *iter;
5453 FOR_EACH_VEC_ELT (*v, i, iter)
5454 update_profile (*iter);
5456 vec_free (v);
5459 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5461 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5462 context may now appear as parameter and result types. As a consequence,
5463 we may need to defer their translation until after a freeze node is seen
5464 or to the end of the current unit. We also aim at handling temporarily
5465 incomplete types created by the usual delayed elaboration scheme. */
5467 static tree
5468 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5470 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5471 so the rationale is exposed in that place. These processings probably
5472 ought to be merged at some point. */
5473 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5474 const bool is_from_limited_with
5475 = (Is_Incomplete_Type (gnat_equiv)
5476 && From_Limited_With (gnat_equiv));
5477 Entity_Id gnat_full_direct_first
5478 = (is_from_limited_with
5479 ? Non_Limited_View (gnat_equiv)
5480 : (Is_Incomplete_Or_Private_Type (gnat_equiv)
5481 ? Full_View (gnat_equiv) : Empty));
5482 Entity_Id gnat_full_direct
5483 = ((is_from_limited_with
5484 && Present (gnat_full_direct_first)
5485 && Is_Private_Type (gnat_full_direct_first))
5486 ? Full_View (gnat_full_direct_first)
5487 : gnat_full_direct_first);
5488 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5489 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5490 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5491 tree gnu_type;
5493 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5494 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5496 else if (is_from_limited_with
5497 && ((!in_main_unit
5498 && !present_gnu_tree (gnat_equiv)
5499 && Present (gnat_full)
5500 && (Is_Record_Type (gnat_full)
5501 || Is_Array_Type (gnat_full)
5502 || Is_Access_Type (gnat_full)))
5503 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5505 gnu_type = make_dummy_type (gnat_equiv);
5507 if (!in_main_unit)
5509 struct incomplete *p = XNEW (struct incomplete);
5511 p->old_type = gnu_type;
5512 p->full_type = gnat_equiv;
5513 p->next = defer_limited_with_list;
5514 defer_limited_with_list = p;
5518 else if (type_annotate_only && No (gnat_equiv))
5519 gnu_type = void_type_node;
5521 else
5522 gnu_type = gnat_to_gnu_type (gnat_equiv);
5524 /* Access-to-unconstrained-array types need a special treatment. */
5525 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5527 if (!TYPE_POINTER_TO (gnu_type))
5528 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5531 return gnu_type;
5534 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5535 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5536 is true if we need to write debug information for other types that we may
5537 create in the process. Also set PARAM_LIST to the list of parameters.
5538 If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin
5539 directly instead of its type. */
5541 static tree
5542 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5543 bool debug_info_p, tree *param_list)
5545 const Entity_Kind kind = Ekind (gnat_subprog);
5546 Entity_Id gnat_return_type = Etype (gnat_subprog);
5547 Entity_Id gnat_param;
5548 tree gnu_type = present_gnu_tree (gnat_subprog)
5549 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
5550 tree gnu_return_type;
5551 tree gnu_param_type_list = NULL_TREE;
5552 tree gnu_param_list = NULL_TREE;
5553 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5554 (In Out or Out parameters not passed by reference), in which case it is
5555 the list of nodes used to specify the values of the In Out/Out parameters
5556 that are returned as a record upon procedure return. The TREE_PURPOSE of
5557 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5558 is the PARM_DECL corresponding to that field. This list will be saved in
5559 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5560 tree gnu_cico_list = NULL_TREE;
5561 tree gnu_cico_return_type = NULL_TREE;
5562 /* Fields in return type of procedure with copy-in copy-out parameters. */
5563 tree gnu_field_list = NULL_TREE;
5564 /* The semantics of "pure" in Ada essentially matches that of "const"
5565 in the back-end. In particular, both properties are orthogonal to
5566 the "nothrow" property if the EH circuitry is explicit in the
5567 internal representation of the back-end. If we are to completely
5568 hide the EH circuitry from it, we need to declare that calls to pure
5569 Ada subprograms that can throw have side effects since they can
5570 trigger an "abnormal" transfer of control flow; thus they can be
5571 neither "const" nor "pure" in the back-end sense. */
5572 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5573 bool return_by_direct_ref_p = false;
5574 bool return_by_invisi_ref_p = false;
5575 bool return_unconstrained_p = false;
5576 bool incomplete_profile_p = false;
5577 unsigned int num;
5579 /* Look into the return type and get its associated GCC tree if it is not
5580 void, and then compute various flags for the subprogram type. But make
5581 sure not to do this processing multiple times. */
5582 if (Ekind (gnat_return_type) == E_Void)
5583 gnu_return_type = void_type_node;
5585 else if (gnu_type
5586 && TREE_CODE (gnu_type) == FUNCTION_TYPE
5587 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type)))
5589 gnu_return_type = TREE_TYPE (gnu_type);
5590 return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type);
5591 return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type);
5592 return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type);
5595 else
5597 /* For foreign convention subprograms, return System.Address as void *
5598 or equivalent. Note that this comprises GCC builtins. */
5599 if (Has_Foreign_Convention (gnat_subprog)
5600 && Is_Descendant_Of_Address (Underlying_Type (gnat_return_type)))
5601 gnu_return_type = ptr_type_node;
5602 else
5603 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5605 /* If this function returns by reference, make the actual return type
5606 the reference type and make a note of that. */
5607 if (Returns_By_Ref (gnat_subprog))
5609 gnu_return_type = build_reference_type (gnu_return_type);
5610 return_by_direct_ref_p = true;
5613 /* If the return type is an unconstrained array type, the return value
5614 will be allocated on the secondary stack so the actual return type
5615 is the fat pointer type. */
5616 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5618 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5619 return_unconstrained_p = true;
5622 /* This is the same unconstrained array case, but for a dummy type. */
5623 else if (TYPE_REFERENCE_TO (gnu_return_type)
5624 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5626 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5627 return_unconstrained_p = true;
5630 /* Likewise, if the return type requires a transient scope, the return
5631 value will also be allocated on the secondary stack so the actual
5632 return type is the reference type. */
5633 else if (Requires_Transient_Scope (gnat_return_type))
5635 gnu_return_type = build_reference_type (gnu_return_type);
5636 return_unconstrained_p = true;
5639 /* If the Mechanism is By_Reference, ensure this function uses the
5640 target's by-invisible-reference mechanism, which may not be the
5641 same as above (e.g. it might be passing an extra parameter). */
5642 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5643 return_by_invisi_ref_p = true;
5645 /* Likewise, if the return type is itself By_Reference. */
5646 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5647 return_by_invisi_ref_p = true;
5649 /* If the type is a padded type and the underlying type would not be
5650 passed by reference or the function has a foreign convention, return
5651 the underlying type. */
5652 else if (TYPE_IS_PADDING_P (gnu_return_type)
5653 && (!default_pass_by_ref
5654 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5655 || Has_Foreign_Convention (gnat_subprog)))
5656 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5658 /* If the return type is unconstrained, it must have a maximum size.
5659 Use the padded type as the effective return type. And ensure the
5660 function uses the target's by-invisible-reference mechanism to
5661 avoid copying too much data when it returns. */
5662 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5664 tree orig_type = gnu_return_type;
5665 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5667 /* If the size overflows to 0, set it to an arbitrary positive
5668 value so that assignments in the type are preserved. Their
5669 actual size is independent of this positive value. */
5670 if (TREE_CODE (max_return_size) == INTEGER_CST
5671 && TREE_OVERFLOW (max_return_size)
5672 && integer_zerop (max_return_size))
5674 max_return_size = copy_node (bitsize_unit_node);
5675 TREE_OVERFLOW (max_return_size) = 1;
5678 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5679 0, gnat_subprog, false, false,
5680 definition, true);
5682 /* Declare it now since it will never be declared otherwise. This
5683 is necessary to ensure that its subtrees are properly marked. */
5684 if (gnu_return_type != orig_type
5685 && !DECL_P (TYPE_NAME (gnu_return_type)))
5686 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5687 true, debug_info_p, gnat_subprog);
5689 return_by_invisi_ref_p = true;
5692 /* If the return type has a size that overflows, we usually cannot have
5693 a function that returns that type. This usage doesn't really make
5694 sense anyway, so issue an error here. */
5695 if (!return_by_invisi_ref_p
5696 && TYPE_SIZE_UNIT (gnu_return_type)
5697 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5698 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5700 post_error ("cannot return type whose size overflows", gnat_subprog);
5701 gnu_return_type = copy_type (gnu_return_type);
5702 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5703 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5706 /* If the return type is incomplete, there are 2 cases: if the function
5707 returns by reference, then the return type is only linked indirectly
5708 in the profile, so the profile can be seen as complete since it need
5709 not be further modified, only the reference types need be adjusted;
5710 otherwise the profile is incomplete and need be adjusted too. */
5711 if (TYPE_IS_DUMMY_P (gnu_return_type))
5713 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5714 incomplete_profile_p = true;
5717 if (kind == E_Function)
5718 Set_Mechanism (gnat_subprog, return_unconstrained_p
5719 || return_by_direct_ref_p
5720 || return_by_invisi_ref_p
5721 ? By_Reference : By_Copy);
5724 /* A procedure (something that doesn't return anything) shouldn't be
5725 considered const since there would be no reason for calling such a
5726 subprogram. Note that procedures with Out (or In Out) parameters
5727 have already been converted into a function with a return type.
5728 Similarly, if the function returns an unconstrained type, then the
5729 function will allocate the return value on the secondary stack and
5730 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5731 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5732 const_flag = false;
5734 /* Loop over the parameters and get their associated GCC tree. While doing
5735 this, build a copy-in copy-out structure if we need one. */
5736 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5737 Present (gnat_param);
5738 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5740 const bool mech_is_by_ref
5741 = Mechanism (gnat_param) == By_Reference
5742 && !(num == 0 && Is_Valued_Procedure (gnat_subprog));
5743 tree gnu_param_name = get_entity_name (gnat_param);
5744 tree gnu_param, gnu_param_type;
5745 bool cico = false;
5747 /* Fetch an existing parameter with complete type and reuse it. But we
5748 didn't save the CICO property so we can only do it for In parameters
5749 or parameters passed by reference. */
5750 if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref)
5751 && present_gnu_tree (gnat_param)
5752 && (gnu_param = get_gnu_tree (gnat_param))
5753 && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param)))
5755 DECL_CHAIN (gnu_param) = NULL_TREE;
5756 gnu_param_type = TREE_TYPE (gnu_param);
5759 /* Otherwise translate the parameter type and act accordingly. */
5760 else
5762 Entity_Id gnat_param_type = Etype (gnat_param);
5764 /* For foreign convention subprograms, pass System.Address as void *
5765 or equivalent. Note that this comprises GCC builtins. */
5766 if (Has_Foreign_Convention (gnat_subprog)
5767 && Is_Descendant_Of_Address (Underlying_Type (gnat_param_type)))
5768 gnu_param_type = ptr_type_node;
5769 else
5770 gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5772 /* If the parameter type is incomplete, there are 2 cases: if it is
5773 passed by reference, then the type is only linked indirectly in
5774 the profile, so the profile can be seen as complete since it need
5775 not be further modified, only the reference type need be adjusted;
5776 otherwise the profile is incomplete and need be adjusted too. */
5777 if (TYPE_IS_DUMMY_P (gnu_param_type))
5779 Node_Id gnat_decl;
5781 if (mech_is_by_ref
5782 || (TYPE_REFERENCE_TO (gnu_param_type)
5783 && TYPE_IS_FAT_POINTER_P
5784 (TYPE_REFERENCE_TO (gnu_param_type)))
5785 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5787 gnu_param_type = build_reference_type (gnu_param_type);
5788 gnu_param
5789 = create_param_decl (gnu_param_name, gnu_param_type);
5790 TREE_READONLY (gnu_param) = 1;
5791 DECL_BY_REF_P (gnu_param) = 1;
5792 DECL_POINTS_TO_READONLY_P (gnu_param)
5793 = (Ekind (gnat_param) == E_In_Parameter
5794 && !Address_Taken (gnat_param));
5795 Set_Mechanism (gnat_param, By_Reference);
5796 Sloc_to_locus (Sloc (gnat_param),
5797 &DECL_SOURCE_LOCATION (gnu_param));
5800 /* ??? This is a kludge to support null procedures in spec taking
5801 a parameter with an untagged incomplete type coming from a
5802 limited context. The front-end creates a body without knowing
5803 anything about the non-limited view, which is illegal Ada and
5804 cannot be supported. Create a parameter with a fake type. */
5805 else if (kind == E_Procedure
5806 && (gnat_decl = Parent (gnat_subprog))
5807 && Nkind (gnat_decl) == N_Procedure_Specification
5808 && Null_Present (gnat_decl)
5809 && Is_Incomplete_Type (gnat_param_type))
5810 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5812 else
5814 /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that
5815 Call_to_gnu will stop if it encounters the PARM_DECL. */
5816 gnu_param
5817 = build_decl (input_location, PARM_DECL, gnu_param_name,
5818 gnu_param_type);
5819 associate_subprog_with_dummy_type (gnat_subprog,
5820 gnu_param_type);
5821 incomplete_profile_p = true;
5825 /* Otherwise build the parameter declaration normally. */
5826 else
5828 gnu_param
5829 = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0,
5830 gnat_subprog, &cico);
5832 /* We are returned either a PARM_DECL or a type if no parameter
5833 needs to be passed; in either case, adjust the type. */
5834 if (DECL_P (gnu_param))
5835 gnu_param_type = TREE_TYPE (gnu_param);
5836 else
5838 gnu_param_type = gnu_param;
5839 gnu_param = NULL_TREE;
5844 /* If we have a GCC tree for the parameter, register it. */
5845 save_gnu_tree (gnat_param, NULL_TREE, false);
5846 if (gnu_param)
5848 gnu_param_type_list
5849 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
5850 gnu_param_list = chainon (gnu_param, gnu_param_list);
5851 save_gnu_tree (gnat_param, gnu_param, false);
5853 /* If a parameter is a pointer, a function may modify memory through
5854 it and thus shouldn't be considered a const function. Also, the
5855 memory may be modified between two calls, so they can't be CSE'ed.
5856 The latter case also handles by-ref parameters. */
5857 if (POINTER_TYPE_P (gnu_param_type)
5858 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
5859 const_flag = false;
5862 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5863 for it in the return type and register the association. */
5864 if (cico && !incomplete_profile_p)
5866 if (!gnu_cico_list)
5868 gnu_cico_return_type = make_node (RECORD_TYPE);
5870 /* If this is a function, we also need a field for the
5871 return value to be placed. */
5872 if (!VOID_TYPE_P (gnu_return_type))
5874 tree gnu_field
5875 = create_field_decl (get_identifier ("RETVAL"),
5876 gnu_return_type,
5877 gnu_cico_return_type, NULL_TREE,
5878 NULL_TREE, 0, 0);
5879 Sloc_to_locus (Sloc (gnat_subprog),
5880 &DECL_SOURCE_LOCATION (gnu_field));
5881 gnu_field_list = gnu_field;
5882 gnu_cico_list
5883 = tree_cons (gnu_field, void_type_node, NULL_TREE);
5886 TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN");
5887 /* Set a default alignment to speed up accesses. But we should
5888 not increase the size of the structure too much, lest it does
5889 not fit in return registers anymore. */
5890 SET_TYPE_ALIGN (gnu_cico_return_type,
5891 get_mode_alignment (ptr_mode));
5894 tree gnu_field
5895 = create_field_decl (gnu_param_name, gnu_param_type,
5896 gnu_cico_return_type, NULL_TREE, NULL_TREE,
5897 0, 0);
5898 Sloc_to_locus (Sloc (gnat_param),
5899 &DECL_SOURCE_LOCATION (gnu_field));
5900 DECL_CHAIN (gnu_field) = gnu_field_list;
5901 gnu_field_list = gnu_field;
5902 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
5906 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5907 and finish up the return type. */
5908 if (gnu_cico_list && !incomplete_profile_p)
5910 /* If we have a CICO list but it has only one entry, we convert
5911 this function into a function that returns this object. */
5912 if (list_length (gnu_cico_list) == 1)
5913 gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
5915 /* Do not finalize the return type if the subprogram is stubbed
5916 since structures are incomplete for the back-end. */
5917 else if (Convention (gnat_subprog) != Convention_Stubbed)
5919 finish_record_type (gnu_cico_return_type, nreverse (gnu_field_list),
5920 0, false);
5922 /* Try to promote the mode of the return type if it is passed
5923 in registers, again to speed up accesses. */
5924 if (TYPE_MODE (gnu_cico_return_type) == BLKmode
5925 && !targetm.calls.return_in_memory (gnu_cico_return_type,
5926 NULL_TREE))
5928 unsigned int size
5929 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type));
5930 unsigned int i = BITS_PER_UNIT;
5931 scalar_int_mode mode;
5933 while (i < size)
5934 i <<= 1;
5935 if (int_mode_for_size (i, 0).exists (&mode))
5937 SET_TYPE_MODE (gnu_cico_return_type, mode);
5938 SET_TYPE_ALIGN (gnu_cico_return_type,
5939 GET_MODE_ALIGNMENT (mode));
5940 TYPE_SIZE (gnu_cico_return_type)
5941 = bitsize_int (GET_MODE_BITSIZE (mode));
5942 TYPE_SIZE_UNIT (gnu_cico_return_type)
5943 = size_int (GET_MODE_SIZE (mode));
5947 if (debug_info_p)
5948 rest_of_record_type_compilation (gnu_cico_return_type);
5951 gnu_return_type = gnu_cico_return_type;
5954 /* The lists have been built in reverse. */
5955 gnu_param_type_list = nreverse (gnu_param_type_list);
5956 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
5957 *param_list = nreverse (gnu_param_list);
5958 gnu_cico_list = nreverse (gnu_cico_list);
5960 /* If the profile is incomplete, we only set the (temporary) return and
5961 parameter types; otherwise, we build the full type. In either case,
5962 we reuse an already existing GCC tree that we built previously here. */
5963 if (incomplete_profile_p)
5965 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5967 else
5968 gnu_type = make_node (FUNCTION_TYPE);
5969 TREE_TYPE (gnu_type) = gnu_return_type;
5970 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5971 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5972 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5973 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5975 else
5977 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
5979 TREE_TYPE (gnu_type) = gnu_return_type;
5980 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
5981 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
5982 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
5983 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
5984 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
5985 TYPE_CANONICAL (gnu_type) = gnu_type;
5986 layout_type (gnu_type);
5988 else
5990 gnu_type
5991 = build_function_type (gnu_return_type, gnu_param_type_list);
5993 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
5994 has a different TYPE_CI_CO_LIST or flags. */
5995 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
5996 return_unconstrained_p,
5997 return_by_direct_ref_p,
5998 return_by_invisi_ref_p))
6000 gnu_type = copy_type (gnu_type);
6001 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6002 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6003 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6004 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6008 if (const_flag)
6009 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6011 if (No_Return (gnat_subprog))
6012 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6014 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
6015 corresponding DECL node and check the parameter association. */
6016 if (Convention (gnat_subprog) == Convention_Intrinsic
6017 && Present (Interface_Name (gnat_subprog)))
6019 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6020 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
6022 /* If we have a builtin DECL for that function, use it. Check if
6023 the profiles are compatible and warn if they are not. Note that
6024 the checker is expected to post diagnostics in this case. */
6025 if (gnu_builtin_decl)
6027 intrin_binding_t inb
6028 = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) };
6030 if (!intrin_profiles_compatible_p (&inb))
6031 post_error
6032 ("?profile of& doesn''t match the builtin it binds!",
6033 gnat_subprog);
6035 return gnu_builtin_decl;
6038 /* Inability to find the builtin DECL most often indicates a genuine
6039 mistake, but imports of unregistered intrinsics are sometimes used
6040 on purpose to allow hooking in alternate bodies; we post a warning
6041 conditioned on Wshadow in this case, to let developers be notified
6042 on demand without risking false positives with common default sets
6043 of options. */
6044 if (warn_shadow)
6045 post_error ("?gcc intrinsic not found for&!", gnat_subprog);
6049 return gnu_type;
6052 /* Return the external name for GNAT_SUBPROG given its entity name. */
6054 static tree
6055 gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name)
6057 tree gnu_ext_name = create_concat_name (gnat_subprog, NULL);
6059 /* If there was no specified Interface_Name and the external and
6060 internal names of the subprogram are the same, only use the
6061 internal name to allow disambiguation of nested subprograms. */
6062 if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name)
6063 gnu_ext_name = NULL_TREE;
6065 return gnu_ext_name;
6068 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6069 qualifiers on TYPE. */
6071 static tree
6072 change_qualified_type (tree type, int type_quals)
6074 /* Qualifiers must be put on the associated array type. */
6075 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
6076 return type;
6078 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6081 /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of
6082 build_nonshared_array_type. */
6084 static void
6085 set_nonaliased_component_on_array_type (tree type)
6087 TYPE_NONALIASED_COMPONENT (type) = 1;
6088 TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1;
6091 /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of
6092 build_nonshared_array_type. */
6094 static void
6095 set_reverse_storage_order_on_array_type (tree type)
6097 TYPE_REVERSE_STORAGE_ORDER (type) = 1;
6098 TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1;
6101 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6103 static bool
6104 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6106 while (Present (Corresponding_Discriminant (discr1)))
6107 discr1 = Corresponding_Discriminant (discr1);
6109 while (Present (Corresponding_Discriminant (discr2)))
6110 discr2 = Corresponding_Discriminant (discr2);
6112 return
6113 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6116 /* Return true if the array type GNU_TYPE, which represents a dimension of
6117 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6119 static bool
6120 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6122 /* If the array type is not the innermost dimension of the GNAT type,
6123 then it has a non-aliased component. */
6124 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6125 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6126 return true;
6128 /* If the array type has an aliased component in the front-end sense,
6129 then it also has an aliased component in the back-end sense. */
6130 if (Has_Aliased_Components (gnat_type))
6131 return false;
6133 /* If this is a derived type, then it has a non-aliased component if
6134 and only if its parent type also has one. */
6135 if (Is_Derived_Type (gnat_type))
6137 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6138 int index;
6139 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6140 gnu_parent_type
6141 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6142 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6143 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6144 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6147 /* Consider that an array of pointers has an aliased component, which is
6148 sort of logical and helps with Taft Amendment types in LTO mode. */
6149 if (POINTER_TYPE_P (TREE_TYPE (gnu_type)))
6150 return false;
6152 /* Otherwise, rely exclusively on properties of the element type. */
6153 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6156 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6158 static bool
6159 compile_time_known_address_p (Node_Id gnat_address)
6161 /* Handle reference to a constant. */
6162 if (Is_Entity_Name (gnat_address)
6163 && Ekind (Entity (gnat_address)) == E_Constant)
6165 gnat_address = Constant_Value (Entity (gnat_address));
6166 if (No (gnat_address))
6167 return false;
6170 /* Catch System'To_Address. */
6171 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6172 gnat_address = Expression (gnat_address);
6174 return Compile_Time_Known_Value (gnat_address);
6177 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6178 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6180 static bool
6181 cannot_be_superflat (Node_Id gnat_range)
6183 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6184 Node_Id scalar_range;
6185 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6187 /* If the low bound is not constant, try to find an upper bound. */
6188 while (Nkind (gnat_lb) != N_Integer_Literal
6189 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6190 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6191 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6192 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6193 || Nkind (scalar_range) == N_Range))
6194 gnat_lb = High_Bound (scalar_range);
6196 /* If the high bound is not constant, try to find a lower bound. */
6197 while (Nkind (gnat_hb) != N_Integer_Literal
6198 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6199 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6200 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6201 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6202 || Nkind (scalar_range) == N_Range))
6203 gnat_hb = Low_Bound (scalar_range);
6205 /* If we have failed to find constant bounds, punt. */
6206 if (Nkind (gnat_lb) != N_Integer_Literal
6207 || Nkind (gnat_hb) != N_Integer_Literal)
6208 return false;
6210 /* We need at least a signed 64-bit type to catch most cases. */
6211 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6212 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6213 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6214 return false;
6216 /* If the low bound is the smallest integer, nothing can be smaller. */
6217 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6218 if (TREE_OVERFLOW (gnu_lb_minus_one))
6219 return true;
6221 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6224 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6226 static bool
6227 constructor_address_p (tree gnu_expr)
6229 while (TREE_CODE (gnu_expr) == NOP_EXPR
6230 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6231 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6232 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6234 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6235 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6238 /* Return true if the size in units represented by GNU_SIZE can be handled by
6239 an allocation. If STATIC_P is true, consider only what can be done with a
6240 static allocation. */
6242 static bool
6243 allocatable_size_p (tree gnu_size, bool static_p)
6245 /* We can allocate a fixed size if it is a valid for the middle-end. */
6246 if (TREE_CODE (gnu_size) == INTEGER_CST)
6247 return valid_constant_size_p (gnu_size);
6249 /* We can allocate a variable size if this isn't a static allocation. */
6250 else
6251 return !static_p;
6254 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6255 initial value of an object of GNU_TYPE. */
6257 static bool
6258 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6260 /* Do not convert if the object's type is unconstrained because this would
6261 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6262 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6263 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6264 return false;
6266 /* Do not convert if the object's type is a padding record whose field is of
6267 self-referential size because we want to copy only the actual data. */
6268 if (type_is_padding_self_referential (gnu_type))
6269 return false;
6271 /* Do not convert a call to a function that returns with variable size since
6272 we want to use the return slot optimization in this case. */
6273 if (TREE_CODE (gnu_expr) == CALL_EXPR
6274 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6275 return false;
6277 /* Do not convert to a record type with a variant part from a record type
6278 without one, to keep the object simpler. */
6279 if (TREE_CODE (gnu_type) == RECORD_TYPE
6280 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6281 && get_variant_part (gnu_type)
6282 && !get_variant_part (TREE_TYPE (gnu_expr)))
6283 return false;
6285 /* In all the other cases, convert the expression to the object's type. */
6286 return true;
6289 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6290 be elaborated at the point of its definition, but do nothing else. */
6292 void
6293 elaborate_entity (Entity_Id gnat_entity)
6295 switch (Ekind (gnat_entity))
6297 case E_Signed_Integer_Subtype:
6298 case E_Modular_Integer_Subtype:
6299 case E_Enumeration_Subtype:
6300 case E_Ordinary_Fixed_Point_Subtype:
6301 case E_Decimal_Fixed_Point_Subtype:
6302 case E_Floating_Point_Subtype:
6304 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6305 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6307 /* ??? Tests to avoid Constraint_Error in static expressions
6308 are needed until after the front stops generating bogus
6309 conversions on bounds of real types. */
6310 if (!Raises_Constraint_Error (gnat_lb))
6311 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6312 Needs_Debug_Info (gnat_entity));
6313 if (!Raises_Constraint_Error (gnat_hb))
6314 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6315 Needs_Debug_Info (gnat_entity));
6316 break;
6319 case E_Record_Subtype:
6320 case E_Private_Subtype:
6321 case E_Limited_Private_Subtype:
6322 case E_Record_Subtype_With_Private:
6323 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6325 Node_Id gnat_discriminant_expr;
6326 Entity_Id gnat_field;
6328 for (gnat_field
6329 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6330 gnat_discriminant_expr
6331 = First_Elmt (Discriminant_Constraint (gnat_entity));
6332 Present (gnat_field);
6333 gnat_field = Next_Discriminant (gnat_field),
6334 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6335 /* Ignore access discriminants. */
6336 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6337 elaborate_expression (Node (gnat_discriminant_expr),
6338 gnat_entity, get_entity_char (gnat_field),
6339 true, false, false);
6341 break;
6346 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6347 NAME, ARGS and ERROR_POINT. */
6349 static void
6350 prepend_one_attribute (struct attrib **attr_list,
6351 enum attrib_type attrib_type,
6352 tree attr_name,
6353 tree attr_args,
6354 Node_Id attr_error_point)
6356 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6358 attr->type = attrib_type;
6359 attr->name = attr_name;
6360 attr->args = attr_args;
6361 attr->error_point = attr_error_point;
6363 attr->next = *attr_list;
6364 *attr_list = attr;
6367 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6369 static void
6370 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6372 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6373 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6374 enum attrib_type etype;
6376 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6377 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6379 case Pragma_Machine_Attribute:
6380 etype = ATTR_MACHINE_ATTRIBUTE;
6381 break;
6383 case Pragma_Linker_Alias:
6384 etype = ATTR_LINK_ALIAS;
6385 break;
6387 case Pragma_Linker_Section:
6388 etype = ATTR_LINK_SECTION;
6389 break;
6391 case Pragma_Linker_Constructor:
6392 etype = ATTR_LINK_CONSTRUCTOR;
6393 break;
6395 case Pragma_Linker_Destructor:
6396 etype = ATTR_LINK_DESTRUCTOR;
6397 break;
6399 case Pragma_Weak_External:
6400 etype = ATTR_WEAK_EXTERNAL;
6401 break;
6403 case Pragma_Thread_Local_Storage:
6404 etype = ATTR_THREAD_LOCAL_STORAGE;
6405 break;
6407 default:
6408 return;
6411 /* See what arguments we have and turn them into GCC trees for attribute
6412 handlers. These expect identifier for strings. We handle at most two
6413 arguments and static expressions only. */
6414 if (Present (gnat_arg) && Present (First (gnat_arg)))
6416 Node_Id gnat_arg0 = Next (First (gnat_arg));
6417 Node_Id gnat_arg1 = Empty;
6419 if (Present (gnat_arg0)
6420 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6422 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6424 if (TREE_CODE (gnu_arg0) == STRING_CST)
6426 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6427 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6428 return;
6431 gnat_arg1 = Next (gnat_arg0);
6434 if (Present (gnat_arg1)
6435 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6437 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6439 if (TREE_CODE (gnu_arg1) == STRING_CST)
6440 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6444 /* Prepend to the list. Make a list of the argument we might have, as GCC
6445 expects it. */
6446 prepend_one_attribute (attr_list, etype, gnu_arg0,
6447 gnu_arg1
6448 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6449 Present (Next (First (gnat_arg)))
6450 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6453 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6455 static void
6456 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6458 Node_Id gnat_temp;
6460 /* Attributes are stored as Representation Item pragmas. */
6461 for (gnat_temp = First_Rep_Item (gnat_entity);
6462 Present (gnat_temp);
6463 gnat_temp = Next_Rep_Item (gnat_temp))
6464 if (Nkind (gnat_temp) == N_Pragma)
6465 prepend_one_attribute_pragma (attr_list, gnat_temp);
6468 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6469 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6470 return the GCC tree to use for that expression. S is the suffix to use
6471 if a variable needs to be created and DEFINITION is true if this is done
6472 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6473 otherwise, we are just elaborating the expression for side-effects. If
6474 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6475 isn't needed for code generation. */
6477 static tree
6478 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6479 bool definition, bool need_value, bool need_debug)
6481 tree gnu_expr;
6483 /* If we already elaborated this expression (e.g. it was involved
6484 in the definition of a private type), use the old value. */
6485 if (present_gnu_tree (gnat_expr))
6486 return get_gnu_tree (gnat_expr);
6488 /* If we don't need a value and this is static or a discriminant,
6489 we don't need to do anything. */
6490 if (!need_value
6491 && (Is_OK_Static_Expression (gnat_expr)
6492 || (Nkind (gnat_expr) == N_Identifier
6493 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6494 return NULL_TREE;
6496 /* If it's a static expression, we don't need a variable for debugging. */
6497 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6498 need_debug = false;
6500 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6501 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6502 definition, need_debug);
6504 /* Save the expression in case we try to elaborate this entity again. Since
6505 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6506 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6507 save_gnu_tree (gnat_expr, gnu_expr, true);
6509 return need_value ? gnu_expr : error_mark_node;
6512 /* Similar, but take a GNU expression and always return a result. */
6514 static tree
6515 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6516 bool definition, bool need_debug)
6518 const bool expr_public_p = Is_Public (gnat_entity);
6519 const bool expr_global_p = expr_public_p || global_bindings_p ();
6520 bool expr_variable_p, use_variable;
6522 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6523 that an expression cannot contain both a discriminant and a variable. */
6524 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6525 return gnu_expr;
6527 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6528 a variable that is initialized to contain the expression when the package
6529 containing the definition is elaborated. If this entity is defined at top
6530 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6531 if this is necessary. */
6532 if (TREE_CONSTANT (gnu_expr))
6533 expr_variable_p = false;
6534 else
6536 /* Skip any conversions and simple constant arithmetics to see if the
6537 expression is based on a read-only variable. */
6538 tree inner = remove_conversions (gnu_expr, true);
6540 inner = skip_simple_constant_arithmetic (inner);
6542 if (handled_component_p (inner))
6543 inner = get_inner_constant_reference (inner);
6545 expr_variable_p
6546 = !(inner
6547 && TREE_CODE (inner) == VAR_DECL
6548 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6551 /* We only need to use the variable if we are in a global context since GCC
6552 can do the right thing in the local case. However, when not optimizing,
6553 use it for bounds of loop iteration scheme to avoid code duplication. */
6554 use_variable = expr_variable_p
6555 && (expr_global_p
6556 || (!optimize
6557 && definition
6558 && Is_Itype (gnat_entity)
6559 && Nkind (Associated_Node_For_Itype (gnat_entity))
6560 == N_Loop_Parameter_Specification));
6562 /* Now create it, possibly only for debugging purposes. */
6563 if (use_variable || need_debug)
6565 /* The following variable creation can happen when processing the body
6566 of subprograms that are defined out of the extended main unit and
6567 inlined. In this case, we are not at the global scope, and thus the
6568 new variable must not be tagged "external", as we used to do here as
6569 soon as DEFINITION was false. */
6570 tree gnu_decl
6571 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6572 TREE_TYPE (gnu_expr), gnu_expr, true,
6573 expr_public_p, !definition && expr_global_p,
6574 expr_global_p, false, true, need_debug,
6575 NULL, gnat_entity);
6577 /* Using this variable at debug time (if need_debug is true) requires a
6578 proper location. The back-end will compute a location for this
6579 variable only if the variable is used by the generated code.
6580 Returning the variable ensures the caller will use it in generated
6581 code. Note that there is no need for a location if the debug info
6582 contains an integer constant.
6583 TODO: when the encoding-based debug scheme is dropped, move this
6584 condition to the top-level IF block: we will not need to create a
6585 variable anymore in such cases, then. */
6586 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6587 return gnu_decl;
6590 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6593 /* Similar, but take an alignment factor and make it explicit in the tree. */
6595 static tree
6596 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6597 bool definition, bool need_debug, unsigned int align)
6599 tree unit_align = size_int (align / BITS_PER_UNIT);
6600 return
6601 size_binop (MULT_EXPR,
6602 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6603 gnu_expr,
6604 unit_align),
6605 gnat_entity, s, definition,
6606 need_debug),
6607 unit_align);
6610 /* Structure to hold internal data for elaborate_reference. */
6612 struct er_data
6614 Entity_Id entity;
6615 bool definition;
6616 unsigned int n;
6619 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6621 static tree
6622 elaborate_reference_1 (tree ref, void *data)
6624 struct er_data *er = (struct er_data *)data;
6625 char suffix[16];
6627 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6628 if (TREE_CONSTANT (ref))
6629 return ref;
6631 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6632 pointer. This may be more efficient, but will also allow us to more
6633 easily find the match for the PLACEHOLDER_EXPR. */
6634 if (TREE_CODE (ref) == COMPONENT_REF
6635 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6636 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6637 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6638 TREE_OPERAND (ref, 1), NULL_TREE);
6640 sprintf (suffix, "EXP%d", ++er->n);
6641 return
6642 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6645 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6646 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6647 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6649 static tree
6650 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6651 tree *init)
6653 struct er_data er = { gnat_entity, definition, 0 };
6654 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6657 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6658 the value passed against the list of choices. */
6660 static tree
6661 choices_to_gnu (tree operand, Node_Id choices)
6663 Node_Id choice;
6664 Node_Id gnat_temp;
6665 tree result = boolean_false_node;
6666 tree this_test, low = 0, high = 0, single = 0;
6668 for (choice = First (choices); Present (choice); choice = Next (choice))
6670 switch (Nkind (choice))
6672 case N_Range:
6673 low = gnat_to_gnu (Low_Bound (choice));
6674 high = gnat_to_gnu (High_Bound (choice));
6676 this_test
6677 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6678 build_binary_op (GE_EXPR, boolean_type_node,
6679 operand, low, true),
6680 build_binary_op (LE_EXPR, boolean_type_node,
6681 operand, high, true),
6682 true);
6684 break;
6686 case N_Subtype_Indication:
6687 gnat_temp = Range_Expression (Constraint (choice));
6688 low = gnat_to_gnu (Low_Bound (gnat_temp));
6689 high = gnat_to_gnu (High_Bound (gnat_temp));
6691 this_test
6692 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6693 build_binary_op (GE_EXPR, boolean_type_node,
6694 operand, low, true),
6695 build_binary_op (LE_EXPR, boolean_type_node,
6696 operand, high, true),
6697 true);
6698 break;
6700 case N_Identifier:
6701 case N_Expanded_Name:
6702 /* This represents either a subtype range, an enumeration
6703 literal, or a constant Ekind says which. If an enumeration
6704 literal or constant, fall through to the next case. */
6705 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6706 && Ekind (Entity (choice)) != E_Constant)
6708 tree type = gnat_to_gnu_type (Entity (choice));
6710 low = TYPE_MIN_VALUE (type);
6711 high = TYPE_MAX_VALUE (type);
6713 this_test
6714 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6715 build_binary_op (GE_EXPR, boolean_type_node,
6716 operand, low, true),
6717 build_binary_op (LE_EXPR, boolean_type_node,
6718 operand, high, true),
6719 true);
6720 break;
6723 /* ... fall through ... */
6725 case N_Character_Literal:
6726 case N_Integer_Literal:
6727 single = gnat_to_gnu (choice);
6728 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6729 single, true);
6730 break;
6732 case N_Others_Choice:
6733 this_test = boolean_true_node;
6734 break;
6736 default:
6737 gcc_unreachable ();
6740 if (result == boolean_false_node)
6741 result = this_test;
6742 else
6743 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6744 this_test, true);
6747 return result;
6750 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6751 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6753 static int
6754 adjust_packed (tree field_type, tree record_type, int packed)
6756 /* If the field contains an item of variable size, we cannot pack it
6757 because we cannot create temporaries of non-fixed size in case
6758 we need to take the address of the field. See addressable_p and
6759 the notes on the addressability issues for further details. */
6760 if (type_has_variable_size (field_type))
6761 return 0;
6763 /* In the other cases, we can honor the packing. */
6764 if (packed)
6765 return packed;
6767 /* If the alignment of the record is specified and the field type
6768 is over-aligned, request Storage_Unit alignment for the field. */
6769 if (TYPE_ALIGN (record_type)
6770 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6771 return -1;
6773 /* Likewise if the maximum alignment of the record is specified. */
6774 if (TYPE_MAX_ALIGN (record_type)
6775 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6776 return -1;
6778 return 0;
6781 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6782 placed in GNU_RECORD_TYPE.
6784 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6785 record has Component_Alignment of Storage_Unit.
6787 DEFINITION is true if this field is for a record being defined.
6789 DEBUG_INFO_P is true if we need to write debug information for types
6790 that we may create in the process. */
6792 static tree
6793 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6794 bool definition, bool debug_info_p)
6796 const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field));
6797 const Entity_Id gnat_field_type = Etype (gnat_field);
6798 const bool is_atomic
6799 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6800 const bool is_aliased = Is_Aliased (gnat_field);
6801 const bool is_independent
6802 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6803 const bool is_volatile
6804 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6805 const bool is_strict_alignment = Strict_Alignment (gnat_field_type);
6806 /* We used to consider that volatile fields also require strict alignment,
6807 but that was an interpolation and would cause us to reject a pragma
6808 volatile on a packed record type containing boolean components, while
6809 there is no basis to do so in the RM. In such cases, the writes will
6810 involve load-modify-store sequences, but that's OK for volatile. The
6811 only constraint is the implementation advice whereby only the bits of
6812 the components should be accessed if they both start and end on byte
6813 boundaries, but that should be guaranteed by the GCC memory model. */
6814 const bool needs_strict_alignment
6815 = (is_atomic || is_aliased || is_independent || is_strict_alignment);
6816 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6817 tree gnu_field_id = get_entity_name (gnat_field);
6818 tree gnu_field, gnu_size, gnu_pos;
6820 /* If this field requires strict alignment, we cannot pack it because
6821 it would very likely be under-aligned in the record. */
6822 if (needs_strict_alignment)
6823 packed = 0;
6824 else
6825 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6827 /* If a size is specified, use it. Otherwise, if the record type is packed,
6828 use the official RM size. See "Handling of Type'Size Values" in Einfo
6829 for further details. */
6830 if (Known_Esize (gnat_field))
6831 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6832 gnat_field, FIELD_DECL, false, true);
6833 else if (packed == 1)
6834 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6835 gnat_field, FIELD_DECL, false, true);
6836 else
6837 gnu_size = NULL_TREE;
6839 /* If we have a specified size that is smaller than that of the field's type,
6840 or a position is specified, and the field's type is a record that doesn't
6841 require strict alignment, see if we can get either an integral mode form
6842 of the type or a smaller form. If we can, show a size was specified for
6843 the field if there wasn't one already, so we know to make this a bitfield
6844 and avoid making things wider.
6846 Changing to an integral mode form is useful when the record is packed as
6847 we can then place the field at a non-byte-aligned position and so achieve
6848 tighter packing. This is in addition required if the field shares a byte
6849 with another field and the front-end lets the back-end handle the access
6850 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6852 Changing to a smaller form is required if the specified size is smaller
6853 than that of the field's type and the type contains sub-fields that are
6854 padded, in order to avoid generating accesses to these sub-fields that
6855 are wider than the field.
6857 We avoid the transformation if it is not required or potentially useful,
6858 as it might entail an increase of the field's alignment and have ripple
6859 effects on the outer record type. A typical case is a field known to be
6860 byte-aligned and not to share a byte with another field. */
6861 if (!needs_strict_alignment
6862 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6863 && !TYPE_FAT_POINTER_P (gnu_field_type)
6864 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6865 && (packed == 1
6866 || (gnu_size
6867 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6868 || (Present (Component_Clause (gnat_field))
6869 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6870 % BITS_PER_UNIT == 0
6871 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6873 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6874 if (gnu_packable_type != gnu_field_type)
6876 gnu_field_type = gnu_packable_type;
6877 if (!gnu_size)
6878 gnu_size = rm_size (gnu_field_type);
6882 if (Is_Atomic_Or_VFA (gnat_field))
6884 const unsigned int align
6885 = promote_object_alignment (gnu_field_type, gnat_field);
6886 if (align > 0)
6887 gnu_field_type
6888 = maybe_pad_type (gnu_field_type, NULL_TREE, align, gnat_field,
6889 false, false, definition, true);
6890 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6893 if (Present (Component_Clause (gnat_field)))
6895 Node_Id gnat_clause = Component_Clause (gnat_field);
6896 Entity_Id gnat_parent = Parent_Subtype (gnat_record_type);
6898 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6899 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6900 gnat_field, FIELD_DECL, false, true);
6902 /* Ensure the position does not overlap with the parent subtype, if there
6903 is one. This test is omitted if the parent of the tagged type has a
6904 full rep clause since, in this case, component clauses are allowed to
6905 overlay the space allocated for the parent type and the front-end has
6906 checked that there are no overlapping components. */
6907 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6909 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6911 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6912 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6913 post_error_ne_tree
6914 ("offset of& must be beyond parent{, minimum allowed is ^}",
6915 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6918 /* If this field needs strict alignment, make sure that the record is
6919 sufficiently aligned and that the position and size are consistent
6920 with the type. But don't do it if we are just annotating types and
6921 the field's type is tagged, since tagged types aren't fully laid out
6922 in this mode. Also, note that atomic implies volatile so the inner
6923 test sequences ordering is significant here. */
6924 if (needs_strict_alignment
6925 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6927 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6929 if (TYPE_ALIGN (gnu_record_type)
6930 && TYPE_ALIGN (gnu_record_type) < type_align)
6931 SET_TYPE_ALIGN (gnu_record_type, type_align);
6933 /* If the position is not a multiple of the alignment of the type,
6934 then error out and reset the position. */
6935 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6936 bitsize_int (type_align))))
6938 const char *s;
6940 if (is_atomic)
6941 s = "position of atomic field& must be multiple of ^ bits";
6942 else if (is_aliased)
6943 s = "position of aliased field& must be multiple of ^ bits";
6944 else if (is_independent)
6945 s = "position of independent field& must be multiple of ^ bits";
6946 else if (is_strict_alignment)
6947 s = "position of & with aliased or tagged part must be"
6948 " multiple of ^ bits";
6949 else
6950 gcc_unreachable ();
6952 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6953 type_align);
6954 gnu_pos = NULL_TREE;
6957 if (gnu_size)
6959 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6960 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6962 /* If the size is lower than that of the type, or greater for
6963 atomic and aliased, then error out and reset the size. */
6964 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6966 const char *s;
6968 if (is_atomic)
6969 s = "size of atomic field& must be ^ bits";
6970 else if (is_aliased)
6971 s = "size of aliased field& must be ^ bits";
6972 else if (is_independent)
6973 s = "size of independent field& must be at least ^ bits";
6974 else if (is_strict_alignment)
6975 s = "size of & with aliased or tagged part must be"
6976 " at least ^ bits";
6977 else
6978 gcc_unreachable ();
6980 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6981 gnu_type_size);
6982 gnu_size = NULL_TREE;
6985 /* Likewise if the size is not a multiple of a byte, */
6986 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6987 bitsize_unit_node)))
6989 const char *s;
6991 if (is_independent)
6992 s = "size of independent field& must be multiple of"
6993 " Storage_Unit";
6994 else if (is_strict_alignment)
6995 s = "size of & with aliased or tagged part must be"
6996 " multiple of Storage_Unit";
6997 else
6998 gcc_unreachable ();
7000 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
7001 gnu_size = NULL_TREE;
7007 /* If the record has rep clauses and this is the tag field, make a rep
7008 clause for it as well. */
7009 else if (Has_Specified_Layout (gnat_record_type)
7010 && Chars (gnat_field) == Name_uTag)
7012 gnu_pos = bitsize_zero_node;
7013 gnu_size = TYPE_SIZE (gnu_field_type);
7016 else
7018 gnu_pos = NULL_TREE;
7020 /* If we are packing the record and the field is BLKmode, round the
7021 size up to a byte boundary. */
7022 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7023 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7026 /* We need to make the size the maximum for the type if it is
7027 self-referential and an unconstrained type. In that case, we can't
7028 pack the field since we can't make a copy to align it. */
7029 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7030 && !gnu_size
7031 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7032 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7034 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7035 packed = 0;
7038 /* If a size is specified, adjust the field's type to it. */
7039 if (gnu_size)
7041 tree orig_field_type;
7043 /* If the field's type is justified modular, we would need to remove
7044 the wrapper to (better) meet the layout requirements. However we
7045 can do so only if the field is not aliased to preserve the unique
7046 layout, if it has the same storage order as the enclosing record
7047 and if the prescribed size is not greater than that of the packed
7048 array to preserve the justification. */
7049 if (!needs_strict_alignment
7050 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7051 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7052 && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type)
7053 == Reverse_Storage_Order (gnat_record_type)
7054 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7055 <= 0)
7056 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7058 /* Similarly if the field's type is a misaligned integral type, but
7059 there is no restriction on the size as there is no justification. */
7060 if (!needs_strict_alignment
7061 && TYPE_IS_PADDING_P (gnu_field_type)
7062 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7063 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7065 gnu_field_type
7066 = make_type_from_size (gnu_field_type, gnu_size,
7067 Has_Biased_Representation (gnat_field));
7069 orig_field_type = gnu_field_type;
7070 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7071 false, false, definition, true);
7073 /* If a padding record was made, declare it now since it will never be
7074 declared otherwise. This is necessary to ensure that its subtrees
7075 are properly marked. */
7076 if (gnu_field_type != orig_field_type
7077 && !DECL_P (TYPE_NAME (gnu_field_type)))
7078 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7079 debug_info_p, gnat_field);
7082 /* Otherwise (or if there was an error), don't specify a position. */
7083 else
7084 gnu_pos = NULL_TREE;
7086 /* If the field's type is a padded type made for a scalar field of a record
7087 type with reverse storage order, we need to propagate the reverse storage
7088 order to the padding type since it is the innermost enclosing aggregate
7089 type around the scalar. */
7090 if (TYPE_IS_PADDING_P (gnu_field_type)
7091 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7092 && Is_Scalar_Type (gnat_field_type))
7093 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7095 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7096 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7098 /* Now create the decl for the field. */
7099 gnu_field
7100 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7101 gnu_size, gnu_pos, packed, is_aliased);
7102 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7103 DECL_ALIASED_P (gnu_field) = is_aliased;
7104 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7106 if (Ekind (gnat_field) == E_Discriminant)
7108 DECL_INVARIANT_P (gnu_field)
7109 = No (Discriminant_Default_Value (gnat_field));
7110 DECL_DISCRIMINANT_NUMBER (gnu_field)
7111 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7114 return gnu_field;
7117 /* Return true if at least one member of COMPONENT_LIST needs strict
7118 alignment. */
7120 static bool
7121 components_need_strict_alignment (Node_Id component_list)
7123 Node_Id component_decl;
7125 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7126 Present (component_decl);
7127 component_decl = Next_Non_Pragma (component_decl))
7129 Entity_Id gnat_field = Defining_Entity (component_decl);
7131 if (Is_Aliased (gnat_field))
7132 return true;
7134 if (Strict_Alignment (Etype (gnat_field)))
7135 return true;
7138 return false;
7141 /* Return true if TYPE is a type with variable size or a padding type with a
7142 field of variable size or a record that has a field with such a type. */
7144 static bool
7145 type_has_variable_size (tree type)
7147 tree field;
7149 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7150 return true;
7152 if (TYPE_IS_PADDING_P (type)
7153 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7154 return true;
7156 if (!RECORD_OR_UNION_TYPE_P (type))
7157 return false;
7159 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7160 if (type_has_variable_size (TREE_TYPE (field)))
7161 return true;
7163 return false;
7166 /* Return true if FIELD is an artificial field. */
7168 static bool
7169 field_is_artificial (tree field)
7171 /* These fields are generated by the front-end proper. */
7172 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7173 return true;
7175 /* These fields are generated by gigi. */
7176 if (DECL_INTERNAL_P (field))
7177 return true;
7179 return false;
7182 /* Return true if FIELD is a non-artificial field with self-referential
7183 size. */
7185 static bool
7186 field_has_self_size (tree field)
7188 if (field_is_artificial (field))
7189 return false;
7191 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7192 return false;
7194 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7197 /* Return true if FIELD is a non-artificial field with variable size. */
7199 static bool
7200 field_has_variable_size (tree field)
7202 if (field_is_artificial (field))
7203 return false;
7205 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7206 return false;
7208 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7211 /* qsort comparer for the bit positions of two record components. */
7213 static int
7214 compare_field_bitpos (const PTR rt1, const PTR rt2)
7216 const_tree const field1 = * (const_tree const *) rt1;
7217 const_tree const field2 = * (const_tree const *) rt2;
7218 const int ret
7219 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7221 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7224 /* Sort the LIST of fields in reverse order of increasing position. */
7226 static tree
7227 reverse_sort_field_list (tree list)
7229 const int len = list_length (list);
7230 tree *field_arr = XALLOCAVEC (tree, len);
7232 for (int i = 0; list; list = DECL_CHAIN (list), i++)
7233 field_arr[i] = list;
7235 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
7237 for (int i = 0; i < len; i++)
7239 DECL_CHAIN (field_arr[i]) = list;
7240 list = field_arr[i];
7243 return list;
7246 /* Reverse function from gnat_to_gnu_field: return the GNAT field present in
7247 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and
7248 corresponding to the GNU tree GNU_FIELD. */
7250 static Entity_Id
7251 gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list,
7252 Entity_Id gnat_record_type)
7254 Entity_Id gnat_component_decl, gnat_field;
7256 if (Present (Component_Items (gnat_component_list)))
7257 for (gnat_component_decl
7258 = First_Non_Pragma (Component_Items (gnat_component_list));
7259 Present (gnat_component_decl);
7260 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7262 gnat_field = Defining_Entity (gnat_component_decl);
7263 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7264 return gnat_field;
7267 if (Has_Discriminants (gnat_record_type))
7268 for (gnat_field = First_Stored_Discriminant (gnat_record_type);
7269 Present (gnat_field);
7270 gnat_field = Next_Stored_Discriminant (gnat_field))
7271 if (gnat_to_gnu_field_decl (gnat_field) == gnu_field)
7272 return gnat_field;
7274 return Empty;
7277 /* Issue a warning for the problematic placement of GNU_FIELD present in
7278 either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE.
7279 IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant.
7280 DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */
7282 static void
7283 warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list,
7284 Entity_Id gnat_record_type, bool in_variant,
7285 bool do_reorder)
7287 if (!Comes_From_Source (gnat_record_type))
7288 return;
7290 Entity_Id gnat_field
7291 = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type);
7292 gcc_assert (Present (gnat_field));
7294 const char *msg1
7295 = in_variant
7296 ? "?variant layout may cause performance issues"
7297 : "?record layout may cause performance issues";
7298 const char *msg2
7299 = Ekind (gnat_field) == E_Discriminant
7300 ? "?discriminant & whose length is not multiple of a byte"
7301 : field_has_self_size (gnu_field)
7302 ? "?component & whose length depends on a discriminant"
7303 : field_has_variable_size (gnu_field)
7304 ? "?component & whose length is not fixed"
7305 : "?component & whose length is not multiple of a byte";
7306 const char *msg3
7307 = do_reorder
7308 ? "?comes too early and was moved down"
7309 : "?comes too early and ought to be moved down";
7311 post_error (msg1, gnat_field);
7312 post_error_ne (msg2, gnat_field, gnat_field);
7313 post_error (msg3, gnat_field);
7316 /* Likewise but for every field present on GNU_FIELD_LIST. */
7318 static void
7319 warn_on_list_placement (tree gnu_field_list, Node_Id gnat_component_list,
7320 Entity_Id gnat_record_type, bool in_variant,
7321 bool do_reorder)
7323 for (tree gnu_tmp = gnu_field_list; gnu_tmp; gnu_tmp = DECL_CHAIN (gnu_tmp))
7324 warn_on_field_placement (gnu_tmp, gnat_component_list, gnat_record_type,
7325 in_variant, do_reorder);
7328 /* Structure holding information for a given variant. */
7329 typedef struct vinfo
7331 /* The record type of the variant. */
7332 tree type;
7334 /* The name of the variant. */
7335 tree name;
7337 /* The qualifier of the variant. */
7338 tree qual;
7340 /* Whether the variant has a rep clause. */
7341 bool has_rep;
7343 /* Whether the variant is packed. */
7344 bool packed;
7346 } vinfo_t;
7348 /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to
7349 GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and
7350 finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects
7351 the layout (see below). When called from gnat_to_gnu_entity during the
7352 processing of a record definition, the GCC node for the parent, if any,
7353 will be the single field of GNU_RECORD_TYPE and the GCC nodes for the
7354 discriminants will be on GNU_FIELD_LIST. The other call to this function
7355 is a recursive call for the component list of a variant and, in this case,
7356 GNU_FIELD_LIST is empty.
7358 PACKED is 1 if this is for a packed record or -1 if this is for a record
7359 with Component_Alignment of Storage_Unit.
7361 DEFINITION is true if we are defining this record type.
7363 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7364 out the record. This means the alignment only serves to force fields to
7365 be bitfields, but not to require the record to be that aligned. This is
7366 used for variants.
7368 ALL_REP is true if a rep clause is present for all the fields.
7370 UNCHECKED_UNION is true if we are building this type for a record with a
7371 Pragma Unchecked_Union.
7373 ARTIFICIAL is true if this is a type that was generated by the compiler.
7375 DEBUG_INFO is true if we need to write debug information about the type.
7377 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7378 mean that its contents may be unused as well, only the container itself.
7380 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7381 the outer record type down to this variant level. It is nonzero only if
7382 all the fields down to this level have a rep clause and ALL_REP is false.
7384 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7385 with a rep clause is to be added; in this case, that is all that should
7386 be done with such fields and the return value will be false. */
7388 static bool
7389 components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type,
7390 tree gnu_field_list, tree gnu_record_type, int packed,
7391 bool definition, bool cancel_alignment, bool all_rep,
7392 bool unchecked_union, bool artificial, bool debug_info,
7393 bool maybe_unused, tree first_free_pos,
7394 tree *p_gnu_rep_list)
7396 const bool needs_xv_encodings
7397 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7398 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7399 bool variants_have_rep = all_rep;
7400 bool layout_with_rep = false;
7401 bool has_self_field = false;
7402 bool has_aliased_after_self_field = false;
7403 Entity_Id gnat_component_decl, gnat_variant_part;
7404 tree gnu_field, gnu_next, gnu_last;
7405 tree gnu_variant_part = NULL_TREE;
7406 tree gnu_rep_list = NULL_TREE;
7408 /* For each component referenced in a component declaration create a GCC
7409 field and add it to the list, skipping pragmas in the GNAT list. */
7410 gnu_last = tree_last (gnu_field_list);
7411 if (Present (Component_Items (gnat_component_list)))
7412 for (gnat_component_decl
7413 = First_Non_Pragma (Component_Items (gnat_component_list));
7414 Present (gnat_component_decl);
7415 gnat_component_decl = Next_Non_Pragma (gnat_component_decl))
7417 Entity_Id gnat_field = Defining_Entity (gnat_component_decl);
7418 Name_Id gnat_name = Chars (gnat_field);
7420 /* If present, the _Parent field must have been created as the single
7421 field of the record type. Put it before any other fields. */
7422 if (gnat_name == Name_uParent)
7424 gnu_field = TYPE_FIELDS (gnu_record_type);
7425 gnu_field_list = chainon (gnu_field_list, gnu_field);
7427 else
7429 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7430 definition, debug_info);
7432 /* If this is the _Tag field, put it before any other fields. */
7433 if (gnat_name == Name_uTag)
7434 gnu_field_list = chainon (gnu_field_list, gnu_field);
7436 /* If this is the _Controller field, put it before the other
7437 fields except for the _Tag or _Parent field. */
7438 else if (gnat_name == Name_uController && gnu_last)
7440 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7441 DECL_CHAIN (gnu_last) = gnu_field;
7444 /* If this is a regular field, put it after the other fields. */
7445 else
7447 DECL_CHAIN (gnu_field) = gnu_field_list;
7448 gnu_field_list = gnu_field;
7449 if (!gnu_last)
7450 gnu_last = gnu_field;
7452 /* And record information for the final layout. */
7453 if (field_has_self_size (gnu_field))
7454 has_self_field = true;
7455 else if (has_self_field && DECL_ALIASED_P (gnu_field))
7456 has_aliased_after_self_field = true;
7460 save_gnu_tree (gnat_field, gnu_field, false);
7463 /* At the end of the component list there may be a variant part. */
7464 gnat_variant_part = Variant_Part (gnat_component_list);
7466 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7467 mutually exclusive and should go in the same memory. To do this we need
7468 to treat each variant as a record whose elements are created from the
7469 component list for the variant. So here we create the records from the
7470 lists for the variants and put them all into the QUAL_UNION_TYPE.
7471 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7472 use GNU_RECORD_TYPE if there are no fields so far. */
7473 if (Present (gnat_variant_part))
7475 Node_Id gnat_discr = Name (gnat_variant_part), variant;
7476 tree gnu_discr = gnat_to_gnu (gnat_discr);
7477 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7478 tree gnu_var_name
7479 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7480 "XVN");
7481 tree gnu_union_type, gnu_union_name;
7482 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7483 bool union_field_needs_strict_alignment = false;
7484 auto_vec <vinfo_t, 16> variant_types;
7485 vinfo_t *gnu_variant;
7486 unsigned int variants_align = 0;
7487 unsigned int i;
7489 gnu_union_name
7490 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7492 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7493 are all in the variant part, to match the layout of C unions. There
7494 is an associated check below. */
7495 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7496 gnu_union_type = gnu_record_type;
7497 else
7499 gnu_union_type
7500 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7502 TYPE_NAME (gnu_union_type) = gnu_union_name;
7503 SET_TYPE_ALIGN (gnu_union_type, 0);
7504 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7505 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7506 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7509 /* If all the fields down to this level have a rep clause, find out
7510 whether all the fields at this level also have one. If so, then
7511 compute the new first free position to be passed downward. */
7512 this_first_free_pos = first_free_pos;
7513 if (this_first_free_pos)
7515 for (gnu_field = gnu_field_list;
7516 gnu_field;
7517 gnu_field = DECL_CHAIN (gnu_field))
7518 if (DECL_FIELD_OFFSET (gnu_field))
7520 tree pos = bit_position (gnu_field);
7521 if (!tree_int_cst_lt (pos, this_first_free_pos))
7522 this_first_free_pos
7523 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7525 else
7527 this_first_free_pos = NULL_TREE;
7528 break;
7532 /* We build the variants in two passes. The bulk of the work is done in
7533 the first pass, that is to say translating the GNAT nodes, building
7534 the container types and computing the associated properties. However
7535 we cannot finish up the container types during this pass because we
7536 don't know where the variant part will be placed until the end. */
7537 for (variant = First_Non_Pragma (Variants (gnat_variant_part));
7538 Present (variant);
7539 variant = Next_Non_Pragma (variant))
7541 tree gnu_variant_type = make_node (RECORD_TYPE);
7542 tree gnu_inner_name, gnu_qual;
7543 bool has_rep;
7544 int field_packed;
7545 vinfo_t vinfo;
7547 Get_Variant_Encoding (variant);
7548 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7549 TYPE_NAME (gnu_variant_type)
7550 = concat_name (gnu_union_name,
7551 IDENTIFIER_POINTER (gnu_inner_name));
7553 /* Set the alignment of the inner type in case we need to make
7554 inner objects into bitfields, but then clear it out so the
7555 record actually gets only the alignment required. */
7556 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7557 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7558 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7559 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7561 /* Similarly, if the outer record has a size specified and all
7562 the fields have a rep clause, we can propagate the size. */
7563 if (all_rep_and_size)
7565 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7566 TYPE_SIZE_UNIT (gnu_variant_type)
7567 = TYPE_SIZE_UNIT (gnu_record_type);
7570 /* Add the fields into the record type for the variant. Note that
7571 we aren't sure to really use it at this point, see below. */
7572 has_rep
7573 = components_to_record (Component_List (variant), gnat_record_type,
7574 NULL_TREE, gnu_variant_type, packed,
7575 definition, !all_rep_and_size, all_rep,
7576 unchecked_union, true, needs_xv_encodings,
7577 true, this_first_free_pos,
7578 all_rep || this_first_free_pos
7579 ? NULL : &gnu_rep_list);
7581 /* Translate the qualifier and annotate the GNAT node. */
7582 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7583 Set_Present_Expr (variant, annotate_value (gnu_qual));
7585 /* Deal with packedness like in gnat_to_gnu_field. */
7586 if (components_need_strict_alignment (Component_List (variant)))
7588 field_packed = 0;
7589 union_field_needs_strict_alignment = true;
7591 else
7592 field_packed
7593 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7595 /* Push this variant onto the stack for the second pass. */
7596 vinfo.type = gnu_variant_type;
7597 vinfo.name = gnu_inner_name;
7598 vinfo.qual = gnu_qual;
7599 vinfo.has_rep = has_rep;
7600 vinfo.packed = field_packed;
7601 variant_types.safe_push (vinfo);
7603 /* Compute the global properties that will determine the placement of
7604 the variant part. */
7605 variants_have_rep |= has_rep;
7606 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7607 variants_align = TYPE_ALIGN (gnu_variant_type);
7610 /* Round up the first free position to the alignment of the variant part
7611 for the variants without rep clause. This will guarantee a consistent
7612 layout independently of the placement of the variant part. */
7613 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7614 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7616 /* In the second pass, the container types are adjusted if necessary and
7617 finished up, then the corresponding fields of the variant part are
7618 built with their qualifier, unless this is an unchecked union. */
7619 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7621 tree gnu_variant_type = gnu_variant->type;
7622 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7624 /* If this is an Unchecked_Union whose fields are all in the variant
7625 part and we have a single field with no representation clause or
7626 placed at offset zero, use the field directly to match the layout
7627 of C unions. */
7628 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7629 && gnu_field_list
7630 && !DECL_CHAIN (gnu_field_list)
7631 && (!DECL_FIELD_OFFSET (gnu_field_list)
7632 || integer_zerop (bit_position (gnu_field_list))))
7634 gnu_field = gnu_field_list;
7635 DECL_CONTEXT (gnu_field) = gnu_record_type;
7637 else
7639 /* Finalize the variant type now. We used to throw away empty
7640 record types but we no longer do that because we need them to
7641 generate complete debug info for the variant; otherwise, the
7642 union type definition will be lacking the fields associated
7643 with these empty variants. */
7644 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7646 /* The variant part will be at offset 0 so we need to ensure
7647 that the fields are laid out starting from the first free
7648 position at this level. */
7649 tree gnu_rep_type = make_node (RECORD_TYPE);
7650 tree gnu_rep_part;
7651 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7652 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7653 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7654 gnu_rep_part
7655 = create_rep_part (gnu_rep_type, gnu_variant_type,
7656 this_first_free_pos);
7657 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7658 gnu_field_list = gnu_rep_part;
7659 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7660 false);
7663 if (debug_info)
7664 rest_of_record_type_compilation (gnu_variant_type);
7665 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7666 true, needs_xv_encodings, gnat_component_list);
7668 gnu_field
7669 = create_field_decl (gnu_variant->name, gnu_variant_type,
7670 gnu_union_type,
7671 all_rep_and_size
7672 ? TYPE_SIZE (gnu_variant_type) : 0,
7673 variants_have_rep ? bitsize_zero_node : 0,
7674 gnu_variant->packed, 0);
7676 DECL_INTERNAL_P (gnu_field) = 1;
7678 if (!unchecked_union)
7679 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7682 DECL_CHAIN (gnu_field) = gnu_variant_list;
7683 gnu_variant_list = gnu_field;
7686 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7687 if (gnu_variant_list)
7689 int union_field_packed;
7691 if (all_rep_and_size)
7693 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7694 TYPE_SIZE_UNIT (gnu_union_type)
7695 = TYPE_SIZE_UNIT (gnu_record_type);
7698 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7699 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7701 /* If GNU_UNION_TYPE is our record type, it means we must have an
7702 Unchecked_Union with no fields. Verify that and, if so, just
7703 return. */
7704 if (gnu_union_type == gnu_record_type)
7706 gcc_assert (unchecked_union
7707 && !gnu_field_list
7708 && !gnu_rep_list);
7709 return variants_have_rep;
7712 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7713 needs_xv_encodings, gnat_component_list);
7715 /* Deal with packedness like in gnat_to_gnu_field. */
7716 if (union_field_needs_strict_alignment)
7717 union_field_packed = 0;
7718 else
7719 union_field_packed
7720 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7722 gnu_variant_part
7723 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7724 all_rep_and_size
7725 ? TYPE_SIZE (gnu_union_type) : 0,
7726 variants_have_rep ? bitsize_zero_node : 0,
7727 union_field_packed, 0);
7729 DECL_INTERNAL_P (gnu_variant_part) = 1;
7733 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do,
7734 pull them out and put them onto the appropriate list.
7736 Similarly, pull out the fields with zero size and no rep clause, as they
7737 would otherwise modify the layout and thus very likely run afoul of the
7738 Ada semantics, which are different from those of C here.
7740 Finally, if there is an aliased field placed in the list after fields
7741 with self-referential size, pull out the latter in the same way.
7743 Optionally, if the reordering mechanism is enabled, pull out the fields
7744 with self-referential size, variable size and fixed size not a multiple
7745 of a byte, so that they don't cause the regular fields to be either at
7746 self-referential/variable offset or misaligned. Note, in the latter
7747 case, that this can only happen in packed record types so the alignment
7748 is effectively capped to the byte for the whole record. But we don't
7749 do it for non-packed record types if pragma Optimize_Alignment (Space)
7750 is specified because this can prevent alignment gaps from being filled.
7752 Optionally, if the layout warning is enabled, keep track of the above 4
7753 different kinds of fields and issue a warning if some of them would be
7754 (or are being) reordered by the reordering mechanism.
7756 ??? If we reorder fields, the debugging information will be affected and
7757 the debugger print fields in a different order from the source code. */
7758 const bool do_reorder
7759 = (Convention (gnat_record_type) == Convention_Ada
7760 && !No_Reordering (gnat_record_type)
7761 && (!Optimize_Alignment_Space (gnat_record_type)
7762 || Is_Packed (gnat_record_type))
7763 && !debug__debug_flag_dot_r);
7764 const bool w_reorder
7765 = (Convention (gnat_record_type) == Convention_Ada
7766 && Warn_On_Questionable_Layout
7767 && !(No_Reordering (gnat_record_type) && GNAT_Mode));
7768 const bool in_variant = (p_gnu_rep_list != NULL);
7769 tree gnu_zero_list = NULL_TREE;
7770 tree gnu_self_list = NULL_TREE;
7771 tree gnu_var_list = NULL_TREE;
7772 tree gnu_bitp_list = NULL_TREE;
7773 tree gnu_tmp_bitp_list = NULL_TREE;
7774 unsigned int tmp_bitp_size = 0;
7775 unsigned int last_reorder_field_type = -1;
7776 unsigned int tmp_last_reorder_field_type = -1;
7778 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7779 do { \
7780 if (gnu_last) \
7781 DECL_CHAIN (gnu_last) = gnu_next; \
7782 else \
7783 gnu_field_list = gnu_next; \
7785 DECL_CHAIN (gnu_field) = (LIST); \
7786 (LIST) = gnu_field; \
7787 } while (0)
7789 gnu_last = NULL_TREE;
7790 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7792 gnu_next = DECL_CHAIN (gnu_field);
7794 if (DECL_FIELD_OFFSET (gnu_field))
7796 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7797 continue;
7800 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7802 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7803 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7804 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7805 if (DECL_ALIASED_P (gnu_field))
7806 SET_TYPE_ALIGN (gnu_record_type,
7807 MAX (TYPE_ALIGN (gnu_record_type),
7808 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7809 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7810 continue;
7813 if (has_aliased_after_self_field && field_has_self_size (gnu_field))
7815 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7816 continue;
7819 /* We don't need further processing in default mode. */
7820 if (!w_reorder && !do_reorder)
7822 gnu_last = gnu_field;
7823 continue;
7826 if (field_has_self_size (gnu_field))
7828 if (w_reorder)
7830 if (last_reorder_field_type < 4)
7831 warn_on_field_placement (gnu_field, gnat_component_list,
7832 gnat_record_type, in_variant,
7833 do_reorder);
7834 else
7835 last_reorder_field_type = 4;
7838 if (do_reorder)
7840 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7841 continue;
7845 else if (field_has_variable_size (gnu_field))
7847 if (w_reorder)
7849 if (last_reorder_field_type < 3)
7850 warn_on_field_placement (gnu_field, gnat_component_list,
7851 gnat_record_type, in_variant,
7852 do_reorder);
7853 else
7854 last_reorder_field_type = 3;
7857 if (do_reorder)
7859 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7860 continue;
7864 else
7866 /* If the field has no size, then it cannot be bit-packed. */
7867 const unsigned int bitp_size
7868 = DECL_SIZE (gnu_field)
7869 ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT
7870 : 0;
7872 /* If the field is bit-packed, we move it to a temporary list that
7873 contains the contiguously preceding bit-packed fields, because
7874 we want to be able to put them back if the misalignment happens
7875 to cancel itself after several bit-packed fields. */
7876 if (bitp_size != 0)
7878 tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT;
7880 if (last_reorder_field_type != 2)
7882 tmp_last_reorder_field_type = last_reorder_field_type;
7883 last_reorder_field_type = 2;
7886 if (do_reorder)
7888 MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list);
7889 continue;
7893 /* No more bit-packed fields, move the existing ones to the end or
7894 put them back at their original location. */
7895 else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list)
7897 last_reorder_field_type = 1;
7899 if (tmp_bitp_size != 0)
7901 if (w_reorder && tmp_last_reorder_field_type < 2)
7903 if (gnu_tmp_bitp_list)
7904 warn_on_list_placement (gnu_tmp_bitp_list,
7905 gnat_component_list,
7906 gnat_record_type, in_variant,
7907 do_reorder);
7908 else
7909 warn_on_field_placement (gnu_last,
7910 gnat_component_list,
7911 gnat_record_type, in_variant,
7912 do_reorder);
7915 if (do_reorder)
7916 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7918 gnu_tmp_bitp_list = NULL_TREE;
7919 tmp_bitp_size = 0;
7921 else
7923 /* Rechain the temporary list in front of GNU_FIELD. */
7924 tree gnu_bitp_field = gnu_field;
7925 while (gnu_tmp_bitp_list)
7927 tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list);
7928 DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field;
7929 if (gnu_last)
7930 DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list;
7931 else
7932 gnu_field_list = gnu_tmp_bitp_list;
7933 gnu_bitp_field = gnu_tmp_bitp_list;
7934 gnu_tmp_bitp_list = gnu_bitp_next;
7939 else
7940 last_reorder_field_type = 1;
7943 gnu_last = gnu_field;
7946 #undef MOVE_FROM_FIELD_LIST_TO
7948 gnu_field_list = nreverse (gnu_field_list);
7950 /* If permitted, we reorder the fields as follows:
7952 1) all (groups of) fields whose length is fixed and multiple of a byte,
7953 2) the remaining fields whose length is fixed and not multiple of a byte,
7954 3) the remaining fields whose length doesn't depend on discriminants,
7955 4) all fields whose length depends on discriminants,
7956 5) the variant part,
7958 within the record and within each variant recursively. */
7960 if (w_reorder)
7962 /* If we have pending bit-packed fields, warn if they would be moved
7963 to after regular fields. */
7964 if (last_reorder_field_type == 2
7965 && tmp_bitp_size != 0
7966 && tmp_last_reorder_field_type < 2)
7968 if (gnu_tmp_bitp_list)
7969 warn_on_list_placement (gnu_tmp_bitp_list,
7970 gnat_component_list, gnat_record_type,
7971 in_variant, do_reorder);
7972 else
7973 warn_on_field_placement (gnu_field_list,
7974 gnat_component_list, gnat_record_type,
7975 in_variant, do_reorder);
7979 if (do_reorder)
7981 /* If we have pending bit-packed fields on the temporary list, we put
7982 them either on the bit-packed list or back on the regular list. */
7983 if (gnu_tmp_bitp_list)
7985 if (tmp_bitp_size != 0)
7986 gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list);
7987 else
7988 gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list);
7991 gnu_field_list
7992 = chainon (gnu_field_list,
7993 chainon (gnu_bitp_list,
7994 chainon (gnu_var_list, gnu_self_list)));
7997 /* Otherwise, if there is an aliased field placed after a field whose length
7998 depends on discriminants, we put all the fields of the latter sort, last.
7999 We need to do this in case an object of this record type is mutable. */
8000 else if (has_aliased_after_self_field)
8001 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
8003 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
8004 in our REP list to the previous level because this level needs them in
8005 order to do a correct layout, i.e. avoid having overlapping fields. */
8006 if (p_gnu_rep_list && gnu_rep_list)
8007 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
8009 /* Deal with the annoying case of an extension of a record with variable size
8010 and partial rep clause, for which the _Parent field is forced at offset 0
8011 and has variable size, which we do not support below. Note that we cannot
8012 do it if the field has fixed size because we rely on the presence of the
8013 REP part built below to trigger the reordering of the fields in a derived
8014 record type when all the fields have a fixed position. */
8015 else if (gnu_rep_list
8016 && !DECL_CHAIN (gnu_rep_list)
8017 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
8018 && !variants_have_rep
8019 && first_free_pos
8020 && integer_zerop (first_free_pos)
8021 && integer_zerop (bit_position (gnu_rep_list)))
8023 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
8024 gnu_field_list = gnu_rep_list;
8025 gnu_rep_list = NULL_TREE;
8028 /* Otherwise, sort the fields by bit position and put them into their own
8029 record, before the others, if we also have fields without rep clause. */
8030 else if (gnu_rep_list)
8032 tree gnu_rep_type, gnu_rep_part;
8033 int i, len = list_length (gnu_rep_list);
8034 tree *gnu_arr = XALLOCAVEC (tree, len);
8036 /* If all the fields have a rep clause, we can do a flat layout. */
8037 layout_with_rep = !gnu_field_list
8038 && (!gnu_variant_part || variants_have_rep);
8039 gnu_rep_type
8040 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
8042 for (gnu_field = gnu_rep_list, i = 0;
8043 gnu_field;
8044 gnu_field = DECL_CHAIN (gnu_field), i++)
8045 gnu_arr[i] = gnu_field;
8047 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
8049 /* Put the fields in the list in order of increasing position, which
8050 means we start from the end. */
8051 gnu_rep_list = NULL_TREE;
8052 for (i = len - 1; i >= 0; i--)
8054 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
8055 gnu_rep_list = gnu_arr[i];
8056 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
8059 if (layout_with_rep)
8060 gnu_field_list = gnu_rep_list;
8061 else
8063 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
8064 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
8065 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
8067 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
8068 without rep clause are laid out starting from this position.
8069 Therefore, we force it as a minimal size on the REP part. */
8070 gnu_rep_part
8071 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
8073 /* Chain the REP part at the beginning of the field list. */
8074 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
8075 gnu_field_list = gnu_rep_part;
8079 /* Chain the variant part at the end of the field list. */
8080 if (gnu_variant_part)
8082 /* We make an exception if the variant part is at offset 0, has a fixed
8083 size, and there is a single rep'ed field placed after it because, in
8084 this case, there is an obvious order of increasing position. */
8085 if (variants_have_rep
8086 && TREE_CODE (DECL_SIZE_UNIT (gnu_variant_part)) == INTEGER_CST
8087 && gnu_rep_list
8088 && gnu_field_list == gnu_rep_list
8089 && !tree_int_cst_lt (DECL_FIELD_OFFSET (gnu_rep_list),
8090 DECL_SIZE_UNIT (gnu_variant_part)))
8092 DECL_CHAIN (gnu_variant_part) = gnu_field_list;
8093 gnu_field_list = gnu_variant_part;
8095 else
8096 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
8099 if (cancel_alignment)
8100 SET_TYPE_ALIGN (gnu_record_type, 0);
8102 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
8104 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
8105 debug_info && !maybe_unused);
8107 /* Chain the fields with zero size at the beginning of the field list. */
8108 if (gnu_zero_list)
8109 TYPE_FIELDS (gnu_record_type)
8110 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
8112 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
8115 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
8116 placed into an Esize, Component_Bit_Offset, or Component_Size value
8117 in the GNAT tree. */
8119 static Uint
8120 annotate_value (tree gnu_size)
8122 static int var_count = 0;
8123 TCode tcode;
8124 Node_Ref_Or_Val ops[3] = { No_Uint, No_Uint, No_Uint };
8125 struct tree_int_map in;
8127 /* See if we've already saved the value for this node. */
8128 if (EXPR_P (gnu_size) || DECL_P (gnu_size))
8130 struct tree_int_map *e;
8132 in.base.from = gnu_size;
8133 e = annotate_value_cache->find (&in);
8135 if (e)
8136 return (Node_Ref_Or_Val) e->to;
8138 else
8139 in.base.from = NULL_TREE;
8141 /* If we do not return inside this switch, TCODE will be set to the
8142 code to be used in a call to Create_Node. */
8143 switch (TREE_CODE (gnu_size))
8145 case INTEGER_CST:
8146 /* For negative values, build NEGATE_EXPR of the opposite. Such values
8147 can appear for discriminants in expressions for variants. */
8148 if (tree_int_cst_sgn (gnu_size) < 0)
8150 tree t = wide_int_to_tree (sizetype, -wi::to_wide (gnu_size));
8151 tcode = Negate_Expr;
8152 ops[0] = UI_From_gnu (t);
8154 else
8155 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
8156 break;
8158 case COMPONENT_REF:
8159 /* The only case we handle here is a simple discriminant reference. */
8160 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
8162 tree ref = gnu_size;
8163 gnu_size = TREE_OPERAND (ref, 1);
8165 /* Climb up the chain of successive extensions, if any. */
8166 while (TREE_CODE (TREE_OPERAND (ref, 0)) == COMPONENT_REF
8167 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (ref, 0), 1))
8168 == parent_name_id)
8169 ref = TREE_OPERAND (ref, 0);
8171 if (TREE_CODE (TREE_OPERAND (ref, 0)) == PLACEHOLDER_EXPR)
8173 /* Fall through to common processing as a FIELD_DECL. */
8174 tcode = Discrim_Val;
8175 ops[0] = UI_From_gnu (DECL_DISCRIMINANT_NUMBER (gnu_size));
8177 else
8178 return No_Uint;
8180 else
8181 return No_Uint;
8182 break;
8184 case VAR_DECL:
8185 tcode = Dynamic_Val;
8186 ops[0] = UI_From_Int (++var_count);
8187 break;
8189 CASE_CONVERT:
8190 case NON_LVALUE_EXPR:
8191 return annotate_value (TREE_OPERAND (gnu_size, 0));
8193 /* Now just list the operations we handle. */
8194 case COND_EXPR: tcode = Cond_Expr; break;
8195 case MINUS_EXPR: tcode = Minus_Expr; break;
8196 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
8197 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
8198 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
8199 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
8200 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
8201 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
8202 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
8203 case NEGATE_EXPR: tcode = Negate_Expr; break;
8204 case MIN_EXPR: tcode = Min_Expr; break;
8205 case MAX_EXPR: tcode = Max_Expr; break;
8206 case ABS_EXPR: tcode = Abs_Expr; break;
8207 case TRUTH_ANDIF_EXPR:
8208 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
8209 case TRUTH_ORIF_EXPR:
8210 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
8211 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
8212 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
8213 case LT_EXPR: tcode = Lt_Expr; break;
8214 case LE_EXPR: tcode = Le_Expr; break;
8215 case GT_EXPR: tcode = Gt_Expr; break;
8216 case GE_EXPR: tcode = Ge_Expr; break;
8217 case EQ_EXPR: tcode = Eq_Expr; break;
8218 case NE_EXPR: tcode = Ne_Expr; break;
8220 case MULT_EXPR:
8221 case PLUS_EXPR:
8222 tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr);
8223 /* Fold conversions from bytes to bits into inner operations. */
8224 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST
8225 && CONVERT_EXPR_P (TREE_OPERAND (gnu_size, 0)))
8227 tree inner_op = TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 0);
8228 if (TREE_CODE (inner_op) == TREE_CODE (gnu_size)
8229 && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST)
8231 tree inner_op_op1 = TREE_OPERAND (inner_op, 1);
8232 tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1);
8233 widest_int op1;
8234 if (TREE_CODE (gnu_size) == MULT_EXPR)
8235 op1 = (wi::to_widest (inner_op_op1)
8236 * wi::to_widest (gnu_size_op1));
8237 else
8238 op1 = (wi::to_widest (inner_op_op1)
8239 + wi::to_widest (gnu_size_op1));
8240 ops[1] = UI_From_gnu (wide_int_to_tree (sizetype, op1));
8241 ops[0] = annotate_value (TREE_OPERAND (inner_op, 0));
8244 break;
8246 case BIT_AND_EXPR:
8247 tcode = Bit_And_Expr;
8248 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
8249 Such values appear in expressions with aligning patterns. Note that,
8250 since sizetype is unsigned, we have to jump through some hoops. */
8251 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
8253 tree op1 = TREE_OPERAND (gnu_size, 1);
8254 wide_int signed_op1 = wi::sext (wi::to_wide (op1),
8255 TYPE_PRECISION (sizetype));
8256 if (wi::neg_p (signed_op1))
8258 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
8259 ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
8262 break;
8264 case CALL_EXPR:
8265 /* In regular mode, inline back only if symbolic annotation is requested
8266 in order to avoid memory explosion on big discriminated record types.
8267 But not in ASIS mode, as symbolic annotation is required for DDA. */
8268 if (List_Representation_Info == 3 || type_annotate_only)
8270 tree t = maybe_inline_call_in_expr (gnu_size);
8271 return t ? annotate_value (t) : No_Uint;
8273 else
8274 return Uint_Minus_1;
8276 default:
8277 return No_Uint;
8280 /* Now get each of the operands that's relevant for this code. If any
8281 cannot be expressed as a repinfo node, say we can't. */
8282 for (int i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
8283 if (ops[i] == No_Uint)
8285 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
8286 if (ops[i] == No_Uint)
8287 return No_Uint;
8290 Node_Ref_Or_Val ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
8292 /* Save the result in the cache. */
8293 if (in.base.from)
8295 struct tree_int_map **h;
8296 /* We can't assume the hash table data hasn't moved since the initial
8297 look up, so we have to search again. Allocating and inserting an
8298 entry at that point would be an alternative, but then we'd better
8299 discard the entry if we decided not to cache it. */
8300 h = annotate_value_cache->find_slot (&in, INSERT);
8301 gcc_assert (!*h);
8302 *h = ggc_alloc<tree_int_map> ();
8303 (*h)->base.from = in.base.from;
8304 (*h)->to = ret;
8307 return ret;
8310 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
8311 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
8312 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
8313 BY_REF is true if the object is used by reference. */
8315 void
8316 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
8318 if (by_ref)
8320 if (TYPE_IS_FAT_POINTER_P (gnu_type))
8321 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
8322 else
8323 gnu_type = TREE_TYPE (gnu_type);
8326 if (Unknown_Esize (gnat_entity))
8328 if (TREE_CODE (gnu_type) == RECORD_TYPE
8329 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8330 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
8331 else if (!size)
8332 size = TYPE_SIZE (gnu_type);
8334 if (size)
8335 Set_Esize (gnat_entity, annotate_value (size));
8338 if (Unknown_Alignment (gnat_entity))
8339 Set_Alignment (gnat_entity,
8340 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
8343 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
8344 Return NULL_TREE if there is no such element in the list. */
8346 static tree
8347 purpose_member_field (const_tree elem, tree list)
8349 while (list)
8351 tree field = TREE_PURPOSE (list);
8352 if (SAME_FIELD_P (field, elem))
8353 return list;
8354 list = TREE_CHAIN (list);
8356 return NULL_TREE;
8359 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8360 set Component_Bit_Offset and Esize of the components to the position and
8361 size used by Gigi. */
8363 static void
8364 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8366 /* For an extension, the inherited components have not been translated because
8367 they are fetched from the _Parent component on the fly. */
8368 const bool is_extension
8369 = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity);
8371 /* We operate by first making a list of all fields and their position (we
8372 can get the size easily) and then update all the sizes in the tree. */
8373 tree gnu_list
8374 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8375 BIGGEST_ALIGNMENT, NULL_TREE);
8377 for (Entity_Id gnat_field = First_Entity (gnat_entity);
8378 Present (gnat_field);
8379 gnat_field = Next_Entity (gnat_field))
8380 if ((Ekind (gnat_field) == E_Component
8381 && (is_extension || present_gnu_tree (gnat_field)))
8382 || (Ekind (gnat_field) == E_Discriminant
8383 && !Is_Unchecked_Union (Scope (gnat_field))))
8385 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8386 gnu_list);
8387 if (t)
8389 tree offset = TREE_VEC_ELT (TREE_VALUE (t), 0);
8390 tree bit_offset = TREE_VEC_ELT (TREE_VALUE (t), 2);
8392 /* If we are just annotating types and the type is tagged, the tag
8393 and the parent components are not generated by the front-end so
8394 we need to add the appropriate offset to each component without
8395 representation clause. */
8396 if (type_annotate_only
8397 && Is_Tagged_Type (gnat_entity)
8398 && No (Component_Clause (gnat_field)))
8400 tree parent_bit_offset;
8402 /* For a component appearing in the current extension, the
8403 offset is the size of the parent. */
8404 if (Is_Derived_Type (gnat_entity)
8405 && Original_Record_Component (gnat_field) == gnat_field)
8406 parent_bit_offset
8407 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8408 bitsizetype);
8409 else
8410 parent_bit_offset = bitsize_int (POINTER_SIZE);
8412 if (TYPE_FIELDS (gnu_type))
8413 parent_bit_offset
8414 = round_up (parent_bit_offset,
8415 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8417 offset
8418 = size_binop (PLUS_EXPR, offset,
8419 fold_convert (sizetype,
8420 size_binop (TRUNC_DIV_EXPR,
8421 parent_bit_offset,
8422 bitsize_unit_node)));
8425 /* If the field has a variable offset, also compute the normalized
8426 position since it's easier to do on trees here than to deduce
8427 it from the annotated expression of Component_Bit_Offset. */
8428 if (TREE_CODE (offset) != INTEGER_CST)
8430 normalize_offset (&offset, &bit_offset, BITS_PER_UNIT);
8431 Set_Normalized_Position (gnat_field,
8432 annotate_value (offset));
8433 Set_Normalized_First_Bit (gnat_field,
8434 annotate_value (bit_offset));
8437 Set_Component_Bit_Offset
8438 (gnat_field,
8439 annotate_value (bit_from_pos (offset, bit_offset)));
8441 Set_Esize (gnat_field,
8442 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8444 else if (is_extension)
8446 /* If there is no entry, this is an inherited component whose
8447 position is the same as in the parent type. */
8448 Entity_Id gnat_orig = Original_Record_Component (gnat_field);
8450 /* If we are just annotating types, discriminants renaming those of
8451 the parent have no entry so deal with them specifically. */
8452 if (type_annotate_only
8453 && gnat_orig == gnat_field
8454 && Ekind (gnat_field) == E_Discriminant)
8455 gnat_orig = Corresponding_Discriminant (gnat_field);
8457 if (Known_Normalized_Position (gnat_orig))
8459 Set_Normalized_Position (gnat_field,
8460 Normalized_Position (gnat_orig));
8461 Set_Normalized_First_Bit (gnat_field,
8462 Normalized_First_Bit (gnat_orig));
8465 Set_Component_Bit_Offset (gnat_field,
8466 Component_Bit_Offset (gnat_orig));
8468 Set_Esize (gnat_field, Esize (gnat_orig));
8473 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8474 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8475 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8476 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8477 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8478 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8479 pre-existing list to be chained to the newly created entries. */
8481 static tree
8482 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8483 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8485 tree gnu_field;
8487 for (gnu_field = TYPE_FIELDS (gnu_type);
8488 gnu_field;
8489 gnu_field = DECL_CHAIN (gnu_field))
8491 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8492 DECL_FIELD_BIT_OFFSET (gnu_field));
8493 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8494 DECL_FIELD_OFFSET (gnu_field));
8495 unsigned int our_offset_align
8496 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8497 tree v = make_tree_vec (3);
8499 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8500 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8501 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8502 gnu_list = tree_cons (gnu_field, v, gnu_list);
8504 /* Recurse on internal fields, flattening the nested fields except for
8505 those in the variant part, if requested. */
8506 if (DECL_INTERNAL_P (gnu_field))
8508 tree gnu_field_type = TREE_TYPE (gnu_field);
8509 if (do_not_flatten_variant
8510 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8511 gnu_list
8512 = build_position_list (gnu_field_type, do_not_flatten_variant,
8513 size_zero_node, bitsize_zero_node,
8514 BIGGEST_ALIGNMENT, gnu_list);
8515 else
8516 gnu_list
8517 = build_position_list (gnu_field_type, do_not_flatten_variant,
8518 gnu_our_offset, gnu_our_bitpos,
8519 our_offset_align, gnu_list);
8523 return gnu_list;
8526 /* Return a list describing the substitutions needed to reflect the
8527 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8528 be in any order. The values in an element of the list are in the form
8529 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8530 a definition of GNAT_SUBTYPE. */
8532 static vec<subst_pair>
8533 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8535 vec<subst_pair> gnu_list = vNULL;
8536 Entity_Id gnat_discrim;
8537 Node_Id gnat_constr;
8539 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8540 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8541 Present (gnat_discrim);
8542 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8543 gnat_constr = Next_Elmt (gnat_constr))
8544 /* Ignore access discriminants. */
8545 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8547 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8548 tree replacement = convert (TREE_TYPE (gnu_field),
8549 elaborate_expression
8550 (Node (gnat_constr), gnat_subtype,
8551 get_entity_char (gnat_discrim),
8552 definition, true, false));
8553 subst_pair s = { gnu_field, replacement };
8554 gnu_list.safe_push (s);
8557 return gnu_list;
8560 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8561 variants of QUAL_UNION_TYPE that are still relevant after applying
8562 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8563 list to be prepended to the newly created entries. */
8565 static vec<variant_desc>
8566 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8567 vec<variant_desc> gnu_list)
8569 tree gnu_field;
8571 for (gnu_field = TYPE_FIELDS (qual_union_type);
8572 gnu_field;
8573 gnu_field = DECL_CHAIN (gnu_field))
8575 tree qual = DECL_QUALIFIER (gnu_field);
8576 unsigned int i;
8577 subst_pair *s;
8579 FOR_EACH_VEC_ELT (subst_list, i, s)
8580 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8582 /* If the new qualifier is not unconditionally false, its variant may
8583 still be accessed. */
8584 if (!integer_zerop (qual))
8586 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8587 variant_desc v
8588 = { variant_type, gnu_field, qual, NULL_TREE, NULL_TREE };
8590 gnu_list.safe_push (v);
8592 /* Recurse on the variant subpart of the variant, if any. */
8593 variant_subpart = get_variant_part (variant_type);
8594 if (variant_subpart)
8595 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8596 subst_list, gnu_list);
8598 /* If the new qualifier is unconditionally true, the subsequent
8599 variants cannot be accessed. */
8600 if (integer_onep (qual))
8601 break;
8605 return gnu_list;
8608 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8609 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8610 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8611 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8612 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8613 true if we are being called to process the Component_Size of GNAT_OBJECT;
8614 this is used only for error messages. ZERO_OK is true if a size of zero
8615 is permitted; if ZERO_OK is false, it means that a size of zero should be
8616 treated as an unspecified size. */
8618 static tree
8619 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8620 enum tree_code kind, bool component_p, bool zero_ok)
8622 Node_Id gnat_error_node;
8623 tree type_size, size;
8625 /* Return 0 if no size was specified. */
8626 if (uint_size == No_Uint)
8627 return NULL_TREE;
8629 /* Ignore a negative size since that corresponds to our back-annotation. */
8630 if (UI_Lt (uint_size, Uint_0))
8631 return NULL_TREE;
8633 /* Find the node to use for error messages. */
8634 if ((Ekind (gnat_object) == E_Component
8635 || Ekind (gnat_object) == E_Discriminant)
8636 && Present (Component_Clause (gnat_object)))
8637 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8638 else if (Present (Size_Clause (gnat_object)))
8639 gnat_error_node = Expression (Size_Clause (gnat_object));
8640 else
8641 gnat_error_node = gnat_object;
8643 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8644 but cannot be represented in bitsizetype. */
8645 size = UI_To_gnu (uint_size, bitsizetype);
8646 if (TREE_OVERFLOW (size))
8648 if (component_p)
8649 post_error_ne ("component size for& is too large", gnat_error_node,
8650 gnat_object);
8651 else
8652 post_error_ne ("size for& is too large", gnat_error_node,
8653 gnat_object);
8654 return NULL_TREE;
8657 /* Ignore a zero size if it is not permitted. */
8658 if (!zero_ok && integer_zerop (size))
8659 return NULL_TREE;
8661 /* The size of objects is always a multiple of a byte. */
8662 if (kind == VAR_DECL
8663 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8665 if (component_p)
8666 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8667 gnat_error_node, gnat_object);
8668 else
8669 post_error_ne ("size for& is not a multiple of Storage_Unit",
8670 gnat_error_node, gnat_object);
8671 return NULL_TREE;
8674 /* If this is an integral type or a packed array type, the front-end has
8675 already verified the size, so we need not do it here (which would mean
8676 checking against the bounds). However, if this is an aliased object,
8677 it may not be smaller than the type of the object. */
8678 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8679 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8680 return size;
8682 /* If the object is a record that contains a template, add the size of the
8683 template to the specified size. */
8684 if (TREE_CODE (gnu_type) == RECORD_TYPE
8685 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8686 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8688 if (kind == VAR_DECL
8689 /* If a type needs strict alignment, a component of this type in
8690 a packed record cannot be packed and thus uses the type size. */
8691 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8692 type_size = TYPE_SIZE (gnu_type);
8693 else
8694 type_size = rm_size (gnu_type);
8696 /* Modify the size of a discriminated type to be the maximum size. */
8697 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8698 type_size = max_size (type_size, true);
8700 /* If this is an access type or a fat pointer, the minimum size is that given
8701 by the smallest integral mode that's valid for pointers. */
8702 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8704 scalar_int_mode p_mode = NARROWEST_INT_MODE;
8705 while (!targetm.valid_pointer_mode (p_mode))
8706 p_mode = GET_MODE_WIDER_MODE (p_mode).require ();
8707 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8710 /* Issue an error either if the default size of the object isn't a constant
8711 or if the new size is smaller than it. */
8712 if (TREE_CODE (type_size) != INTEGER_CST
8713 || TREE_OVERFLOW (type_size)
8714 || tree_int_cst_lt (size, type_size))
8716 if (component_p)
8717 post_error_ne_tree
8718 ("component size for& too small{, minimum allowed is ^}",
8719 gnat_error_node, gnat_object, type_size);
8720 else
8721 post_error_ne_tree
8722 ("size for& too small{, minimum allowed is ^}",
8723 gnat_error_node, gnat_object, type_size);
8724 return NULL_TREE;
8727 return size;
8730 /* Similarly, but both validate and process a value of RM size. This routine
8731 is only called for types. */
8733 static void
8734 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8736 Node_Id gnat_attr_node;
8737 tree old_size, size;
8739 /* Do nothing if no size was specified. */
8740 if (uint_size == No_Uint)
8741 return;
8743 /* Ignore a negative size since that corresponds to our back-annotation. */
8744 if (UI_Lt (uint_size, Uint_0))
8745 return;
8747 /* Only issue an error if a Value_Size clause was explicitly given.
8748 Otherwise, we'd be duplicating an error on the Size clause. */
8749 gnat_attr_node
8750 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8752 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8753 but cannot be represented in bitsizetype. */
8754 size = UI_To_gnu (uint_size, bitsizetype);
8755 if (TREE_OVERFLOW (size))
8757 if (Present (gnat_attr_node))
8758 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8759 gnat_entity);
8760 return;
8763 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8764 exists, or this is an integer type, in which case the front-end will
8765 have always set it. */
8766 if (No (gnat_attr_node)
8767 && integer_zerop (size)
8768 && !Has_Size_Clause (gnat_entity)
8769 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8770 return;
8772 old_size = rm_size (gnu_type);
8774 /* If the old size is self-referential, get the maximum size. */
8775 if (CONTAINS_PLACEHOLDER_P (old_size))
8776 old_size = max_size (old_size, true);
8778 /* Issue an error either if the old size of the object isn't a constant or
8779 if the new size is smaller than it. The front-end has already verified
8780 this for scalar and packed array types. */
8781 if (TREE_CODE (old_size) != INTEGER_CST
8782 || TREE_OVERFLOW (old_size)
8783 || (AGGREGATE_TYPE_P (gnu_type)
8784 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8785 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8786 && !(TYPE_IS_PADDING_P (gnu_type)
8787 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8788 && TYPE_PACKED_ARRAY_TYPE_P
8789 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8790 && tree_int_cst_lt (size, old_size)))
8792 if (Present (gnat_attr_node))
8793 post_error_ne_tree
8794 ("Value_Size for& too small{, minimum allowed is ^}",
8795 gnat_attr_node, gnat_entity, old_size);
8796 return;
8799 /* Otherwise, set the RM size proper for integral types... */
8800 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8801 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8802 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8803 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8804 SET_TYPE_RM_SIZE (gnu_type, size);
8806 /* ...or the Ada size for record and union types. */
8807 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8808 && !TYPE_FAT_POINTER_P (gnu_type))
8809 SET_TYPE_ADA_SIZE (gnu_type, size);
8812 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8813 a type or object whose present alignment is ALIGN. If this alignment is
8814 valid, return it. Otherwise, give an error and return ALIGN. */
8816 static unsigned int
8817 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8819 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8820 unsigned int new_align;
8821 Node_Id gnat_error_node;
8823 /* Don't worry about checking alignment if alignment was not specified
8824 by the source program and we already posted an error for this entity. */
8825 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8826 return align;
8828 /* Post the error on the alignment clause if any. Note, for the implicit
8829 base type of an array type, the alignment clause is on the first
8830 subtype. */
8831 if (Present (Alignment_Clause (gnat_entity)))
8832 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8834 else if (Is_Itype (gnat_entity)
8835 && Is_Array_Type (gnat_entity)
8836 && Etype (gnat_entity) == gnat_entity
8837 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8838 gnat_error_node =
8839 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8841 else
8842 gnat_error_node = gnat_entity;
8844 /* Within GCC, an alignment is an integer, so we must make sure a value is
8845 specified that fits in that range. Also, there is an upper bound to
8846 alignments we can support/allow. */
8847 if (!UI_Is_In_Int_Range (alignment)
8848 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8849 post_error_ne_num ("largest supported alignment for& is ^",
8850 gnat_error_node, gnat_entity, max_allowed_alignment);
8851 else if (!(Present (Alignment_Clause (gnat_entity))
8852 && From_At_Mod (Alignment_Clause (gnat_entity)))
8853 && new_align * BITS_PER_UNIT < align)
8855 unsigned int double_align;
8856 bool is_capped_double, align_clause;
8858 /* If the default alignment of "double" or larger scalar types is
8859 specifically capped and the new alignment is above the cap, do
8860 not post an error and change the alignment only if there is an
8861 alignment clause; this makes it possible to have the associated
8862 GCC type overaligned by default for performance reasons. */
8863 if ((double_align = double_float_alignment) > 0)
8865 Entity_Id gnat_type
8866 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8867 is_capped_double
8868 = is_double_float_or_array (gnat_type, &align_clause);
8870 else if ((double_align = double_scalar_alignment) > 0)
8872 Entity_Id gnat_type
8873 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8874 is_capped_double
8875 = is_double_scalar_or_array (gnat_type, &align_clause);
8877 else
8878 is_capped_double = align_clause = false;
8880 if (is_capped_double && new_align >= double_align)
8882 if (align_clause)
8883 align = new_align * BITS_PER_UNIT;
8885 else
8887 if (is_capped_double)
8888 align = double_align * BITS_PER_UNIT;
8890 post_error_ne_num ("alignment for& must be at least ^",
8891 gnat_error_node, gnat_entity,
8892 align / BITS_PER_UNIT);
8895 else
8897 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8898 if (new_align > align)
8899 align = new_align;
8902 return align;
8905 /* Promote the alignment of GNU_TYPE corresponding to GNAT_ENTITY. Return
8906 a positive value on success or zero on failure. */
8908 static unsigned int
8909 promote_object_alignment (tree gnu_type, Entity_Id gnat_entity)
8911 unsigned int align, size_cap, align_cap;
8913 /* No point in promoting the alignment if this doesn't prevent BLKmode access
8914 to the object, in particular block copy, as this will for example disable
8915 the NRV optimization for it. No point in jumping through all the hoops
8916 needed in order to support BIGGEST_ALIGNMENT if we don't really have to.
8917 So we cap to the smallest alignment that corresponds to a known efficient
8918 memory access pattern, except for Atomic and Volatile_Full_Access. */
8919 if (Is_Atomic_Or_VFA (gnat_entity))
8921 size_cap = UINT_MAX;
8922 align_cap = BIGGEST_ALIGNMENT;
8924 else
8926 size_cap = MAX_FIXED_MODE_SIZE;
8927 align_cap = get_mode_alignment (ptr_mode);
8930 /* Do the promotion within the above limits. */
8931 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
8932 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
8933 align = 0;
8934 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
8935 align = align_cap;
8936 else
8937 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
8939 /* But make sure not to under-align the object. */
8940 if (align <= TYPE_ALIGN (gnu_type))
8941 align = 0;
8943 /* And honor the minimum valid atomic alignment, if any. */
8944 #ifdef MINIMUM_ATOMIC_ALIGNMENT
8945 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
8946 align = MINIMUM_ATOMIC_ALIGNMENT;
8947 #endif
8949 return align;
8952 /* Verify that TYPE is something we can implement atomically. If not, issue
8953 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8954 process a component type. */
8956 static void
8957 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8959 Node_Id gnat_error_point = gnat_entity;
8960 Node_Id gnat_node;
8961 machine_mode mode;
8962 enum mode_class mclass;
8963 unsigned int align;
8964 tree size;
8966 /* If this is an anonymous base type, nothing to check, the error will be
8967 reported on the source type if need be. */
8968 if (!Comes_From_Source (gnat_entity))
8969 return;
8971 mode = TYPE_MODE (type);
8972 mclass = GET_MODE_CLASS (mode);
8973 align = TYPE_ALIGN (type);
8974 size = TYPE_SIZE (type);
8976 /* Consider all aligned floating-point types atomic and any aligned types
8977 that are represented by integers no wider than a machine word. */
8978 scalar_int_mode int_mode;
8979 if ((mclass == MODE_FLOAT
8980 || (is_a <scalar_int_mode> (mode, &int_mode)
8981 && GET_MODE_BITSIZE (int_mode) <= BITS_PER_WORD))
8982 && align >= GET_MODE_ALIGNMENT (mode))
8983 return;
8985 /* For the moment, also allow anything that has an alignment equal to its
8986 size and which is smaller than a word. */
8987 if (size
8988 && TREE_CODE (size) == INTEGER_CST
8989 && compare_tree_int (size, align) == 0
8990 && align <= BITS_PER_WORD)
8991 return;
8993 for (gnat_node = First_Rep_Item (gnat_entity);
8994 Present (gnat_node);
8995 gnat_node = Next_Rep_Item (gnat_node))
8996 if (Nkind (gnat_node) == N_Pragma)
8998 unsigned char pragma_id
8999 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
9001 if ((pragma_id == Pragma_Atomic && !component_p)
9002 || (pragma_id == Pragma_Atomic_Components && component_p))
9004 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
9005 break;
9009 if (component_p)
9010 post_error_ne ("atomic access to component of & cannot be guaranteed",
9011 gnat_error_point, gnat_entity);
9012 else if (Is_Volatile_Full_Access (gnat_entity))
9013 post_error_ne ("volatile full access to & cannot be guaranteed",
9014 gnat_error_point, gnat_entity);
9015 else
9016 post_error_ne ("atomic access to & cannot be guaranteed",
9017 gnat_error_point, gnat_entity);
9021 /* Helper for the intrin compatibility checks family. Evaluate whether
9022 two types are definitely incompatible. */
9024 static bool
9025 intrin_types_incompatible_p (tree t1, tree t2)
9027 enum tree_code code;
9029 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
9030 return false;
9032 if (TYPE_MODE (t1) != TYPE_MODE (t2))
9033 return true;
9035 if (TREE_CODE (t1) != TREE_CODE (t2))
9036 return true;
9038 code = TREE_CODE (t1);
9040 switch (code)
9042 case INTEGER_TYPE:
9043 case REAL_TYPE:
9044 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
9046 case POINTER_TYPE:
9047 case REFERENCE_TYPE:
9048 /* Assume designated types are ok. We'd need to account for char * and
9049 void * variants to do better, which could rapidly get messy and isn't
9050 clearly worth the effort. */
9051 return false;
9053 default:
9054 break;
9057 return false;
9060 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
9061 on the Ada/builtin argument lists for the INB binding. */
9063 static bool
9064 intrin_arglists_compatible_p (intrin_binding_t * inb)
9066 function_args_iterator ada_iter, btin_iter;
9068 function_args_iter_init (&ada_iter, inb->ada_fntype);
9069 function_args_iter_init (&btin_iter, inb->btin_fntype);
9071 /* Sequence position of the last argument we checked. */
9072 int argpos = 0;
9074 while (true)
9076 tree ada_type = function_args_iter_cond (&ada_iter);
9077 tree btin_type = function_args_iter_cond (&btin_iter);
9079 /* If we've exhausted both lists simultaneously, we're done. */
9080 if (!ada_type && !btin_type)
9081 break;
9083 /* If one list is shorter than the other, they fail to match. */
9084 if (!ada_type || !btin_type)
9085 return false;
9087 /* If we're done with the Ada args and not with the internal builtin
9088 args, or the other way around, complain. */
9089 if (ada_type == void_type_node
9090 && btin_type != void_type_node)
9092 post_error ("?Ada arguments list too short!", inb->gnat_entity);
9093 return false;
9096 if (btin_type == void_type_node
9097 && ada_type != void_type_node)
9099 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
9100 inb->gnat_entity, inb->gnat_entity, argpos);
9101 return false;
9104 /* Otherwise, check that types match for the current argument. */
9105 argpos ++;
9106 if (intrin_types_incompatible_p (ada_type, btin_type))
9108 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
9109 inb->gnat_entity, inb->gnat_entity, argpos);
9110 return false;
9114 function_args_iter_next (&ada_iter);
9115 function_args_iter_next (&btin_iter);
9118 return true;
9121 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
9122 on the Ada/builtin return values for the INB binding. */
9124 static bool
9125 intrin_return_compatible_p (intrin_binding_t * inb)
9127 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
9128 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
9130 /* Accept function imported as procedure, common and convenient. */
9131 if (VOID_TYPE_P (ada_return_type)
9132 && !VOID_TYPE_P (btin_return_type))
9133 return true;
9135 /* Check return types compatibility otherwise. Note that this
9136 handles void/void as well. */
9137 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
9139 post_error ("?intrinsic binding type mismatch on return value!",
9140 inb->gnat_entity);
9141 return false;
9144 return true;
9147 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
9148 compatible. Issue relevant warnings when they are not.
9150 This is intended as a light check to diagnose the most obvious cases, not
9151 as a full fledged type compatibility predicate. It is the programmer's
9152 responsibility to ensure correctness of the Ada declarations in Imports,
9153 especially when binding straight to a compiler internal. */
9155 static bool
9156 intrin_profiles_compatible_p (intrin_binding_t * inb)
9158 /* Check compatibility on return values and argument lists, each responsible
9159 for posting warnings as appropriate. Ensure use of the proper sloc for
9160 this purpose. */
9162 bool arglists_compatible_p, return_compatible_p;
9163 location_t saved_location = input_location;
9165 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
9167 return_compatible_p = intrin_return_compatible_p (inb);
9168 arglists_compatible_p = intrin_arglists_compatible_p (inb);
9170 input_location = saved_location;
9172 return return_compatible_p && arglists_compatible_p;
9175 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
9176 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
9177 specified size for this field. POS_LIST is a position list describing
9178 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
9179 to this layout. */
9181 static tree
9182 create_field_decl_from (tree old_field, tree field_type, tree record_type,
9183 tree size, tree pos_list,
9184 vec<subst_pair> subst_list)
9186 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
9187 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
9188 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
9189 tree new_pos, new_field;
9190 unsigned int i;
9191 subst_pair *s;
9193 if (CONTAINS_PLACEHOLDER_P (pos))
9194 FOR_EACH_VEC_ELT (subst_list, i, s)
9195 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
9197 /* If the position is now a constant, we can set it as the position of the
9198 field when we make it. Otherwise, we need to deal with it specially. */
9199 if (TREE_CONSTANT (pos))
9200 new_pos = bit_from_pos (pos, bitpos);
9201 else
9202 new_pos = NULL_TREE;
9204 new_field
9205 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
9206 size, new_pos, DECL_PACKED (old_field),
9207 !DECL_NONADDRESSABLE_P (old_field));
9209 if (!new_pos)
9211 normalize_offset (&pos, &bitpos, offset_align);
9212 /* Finalize the position. */
9213 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
9214 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
9215 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
9216 DECL_SIZE (new_field) = size;
9217 DECL_SIZE_UNIT (new_field)
9218 = convert (sizetype,
9219 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
9220 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
9223 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
9224 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
9225 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
9226 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
9228 return new_field;
9231 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
9232 it is the minimal size the REP_PART must have. */
9234 static tree
9235 create_rep_part (tree rep_type, tree record_type, tree min_size)
9237 tree field;
9239 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
9240 min_size = NULL_TREE;
9242 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
9243 min_size, NULL_TREE, 0, 1);
9244 DECL_INTERNAL_P (field) = 1;
9246 return field;
9249 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
9251 static tree
9252 get_rep_part (tree record_type)
9254 tree field = TYPE_FIELDS (record_type);
9256 /* The REP part is the first field, internal, another record, and its name
9257 starts with an 'R'. */
9258 if (field
9259 && DECL_INTERNAL_P (field)
9260 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
9261 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
9262 return field;
9264 return NULL_TREE;
9267 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
9269 tree
9270 get_variant_part (tree record_type)
9272 tree field;
9274 /* The variant part is the only internal field that is a qualified union. */
9275 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9276 if (DECL_INTERNAL_P (field)
9277 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
9278 return field;
9280 return NULL_TREE;
9283 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
9284 the list of variants to be used and RECORD_TYPE is the type of the parent.
9285 POS_LIST is a position list describing the layout of fields present in
9286 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
9287 layout. DEBUG_INFO_P is true if we need to write debug information. */
9289 static tree
9290 create_variant_part_from (tree old_variant_part,
9291 vec<variant_desc> variant_list,
9292 tree record_type, tree pos_list,
9293 vec<subst_pair> subst_list,
9294 bool debug_info_p)
9296 tree offset = DECL_FIELD_OFFSET (old_variant_part);
9297 tree old_union_type = TREE_TYPE (old_variant_part);
9298 tree new_union_type, new_variant_part;
9299 tree union_field_list = NULL_TREE;
9300 variant_desc *v;
9301 unsigned int i;
9303 /* First create the type of the variant part from that of the old one. */
9304 new_union_type = make_node (QUAL_UNION_TYPE);
9305 TYPE_NAME (new_union_type)
9306 = concat_name (TYPE_NAME (record_type),
9307 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
9309 /* If the position of the variant part is constant, subtract it from the
9310 size of the type of the parent to get the new size. This manual CSE
9311 reduces the code size when not optimizing. */
9312 if (TREE_CODE (offset) == INTEGER_CST
9313 && TYPE_SIZE (record_type)
9314 && TYPE_SIZE_UNIT (record_type))
9316 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
9317 tree first_bit = bit_from_pos (offset, bitpos);
9318 TYPE_SIZE (new_union_type)
9319 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
9320 TYPE_SIZE_UNIT (new_union_type)
9321 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
9322 byte_from_pos (offset, bitpos));
9323 SET_TYPE_ADA_SIZE (new_union_type,
9324 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
9325 first_bit));
9326 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
9327 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
9329 else
9330 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
9332 /* Now finish up the new variants and populate the union type. */
9333 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
9335 tree old_field = v->field, new_field;
9336 tree old_variant, old_variant_subpart, new_variant, field_list;
9338 /* Skip variants that don't belong to this nesting level. */
9339 if (DECL_CONTEXT (old_field) != old_union_type)
9340 continue;
9342 /* Retrieve the list of fields already added to the new variant. */
9343 new_variant = v->new_type;
9344 field_list = TYPE_FIELDS (new_variant);
9346 /* If the old variant had a variant subpart, we need to create a new
9347 variant subpart and add it to the field list. */
9348 old_variant = v->type;
9349 old_variant_subpart = get_variant_part (old_variant);
9350 if (old_variant_subpart)
9352 tree new_variant_subpart
9353 = create_variant_part_from (old_variant_subpart, variant_list,
9354 new_variant, pos_list, subst_list,
9355 debug_info_p);
9356 DECL_CHAIN (new_variant_subpart) = field_list;
9357 field_list = new_variant_subpart;
9360 /* Finish up the new variant and create the field. */
9361 finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p);
9362 create_type_decl (TYPE_NAME (new_variant), new_variant, true,
9363 debug_info_p, Empty);
9365 new_field
9366 = create_field_decl_from (old_field, new_variant, new_union_type,
9367 TYPE_SIZE (new_variant),
9368 pos_list, subst_list);
9369 DECL_QUALIFIER (new_field) = v->qual;
9370 DECL_INTERNAL_P (new_field) = 1;
9371 DECL_CHAIN (new_field) = union_field_list;
9372 union_field_list = new_field;
9375 /* Finish up the union type and create the variant part. Note that we don't
9376 reverse the field list because VARIANT_LIST has been traversed in reverse
9377 order. */
9378 finish_record_type (new_union_type, union_field_list, 2, debug_info_p);
9379 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true,
9380 debug_info_p, Empty);
9382 new_variant_part
9383 = create_field_decl_from (old_variant_part, new_union_type, record_type,
9384 TYPE_SIZE (new_union_type),
9385 pos_list, subst_list);
9386 DECL_INTERNAL_P (new_variant_part) = 1;
9388 /* With multiple discriminants it is possible for an inner variant to be
9389 statically selected while outer ones are not; in this case, the list
9390 of fields of the inner variant is not flattened and we end up with a
9391 qualified union with a single member. Drop the useless container. */
9392 if (!DECL_CHAIN (union_field_list))
9394 DECL_CONTEXT (union_field_list) = record_type;
9395 DECL_FIELD_OFFSET (union_field_list)
9396 = DECL_FIELD_OFFSET (new_variant_part);
9397 DECL_FIELD_BIT_OFFSET (union_field_list)
9398 = DECL_FIELD_BIT_OFFSET (new_variant_part);
9399 SET_DECL_OFFSET_ALIGN (union_field_list,
9400 DECL_OFFSET_ALIGN (new_variant_part));
9401 new_variant_part = union_field_list;
9404 return new_variant_part;
9407 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
9408 which are both RECORD_TYPE, after applying the substitutions described
9409 in SUBST_LIST. */
9411 static void
9412 copy_and_substitute_in_size (tree new_type, tree old_type,
9413 vec<subst_pair> subst_list)
9415 unsigned int i;
9416 subst_pair *s;
9418 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
9419 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
9420 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9421 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9422 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9424 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9425 FOR_EACH_VEC_ELT (subst_list, i, s)
9426 TYPE_SIZE (new_type)
9427 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9428 s->discriminant, s->replacement);
9430 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9431 FOR_EACH_VEC_ELT (subst_list, i, s)
9432 TYPE_SIZE_UNIT (new_type)
9433 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9434 s->discriminant, s->replacement);
9436 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9437 FOR_EACH_VEC_ELT (subst_list, i, s)
9438 SET_TYPE_ADA_SIZE
9439 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9440 s->discriminant, s->replacement));
9442 /* Finalize the size. */
9443 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9444 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9447 /* Return true if DISC is a stored discriminant of RECORD_TYPE. */
9449 static inline bool
9450 is_stored_discriminant (Entity_Id discr, Entity_Id record_type)
9452 if (Is_Unchecked_Union (record_type))
9453 return false;
9454 else if (Is_Tagged_Type (record_type))
9455 return No (Corresponding_Discriminant (discr));
9456 else if (Ekind (record_type) == E_Record_Type)
9457 return Original_Record_Component (discr) == discr;
9458 else
9459 return true;
9462 /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are
9463 both record types, after applying the substitutions described in SUBST_LIST.
9464 DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */
9466 static void
9467 copy_and_substitute_in_layout (Entity_Id gnat_new_type,
9468 Entity_Id gnat_old_type,
9469 tree gnu_new_type,
9470 tree gnu_old_type,
9471 vec<subst_pair> gnu_subst_list,
9472 bool debug_info_p)
9474 const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype);
9475 tree gnu_field_list = NULL_TREE;
9476 tree gnu_variable_field_list = NULL_TREE;
9477 bool selected_variant;
9478 vec<variant_desc> gnu_variant_list;
9480 /* Look for REP and variant parts in the old type. */
9481 tree gnu_rep_part = get_rep_part (gnu_old_type);
9482 tree gnu_variant_part = get_variant_part (gnu_old_type);
9484 /* If there is a variant part, we must compute whether the constraints
9485 statically select a particular variant. If so, we simply drop the
9486 qualified union and flatten the list of fields. Otherwise we will
9487 build a new qualified union for the variants that are still relevant. */
9488 if (gnu_variant_part)
9490 variant_desc *v;
9491 unsigned int i;
9493 gnu_variant_list = build_variant_list (TREE_TYPE (gnu_variant_part),
9494 gnu_subst_list, vNULL);
9496 /* If all the qualifiers are unconditionally true, the innermost variant
9497 is statically selected. */
9498 selected_variant = true;
9499 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9500 if (!integer_onep (v->qual))
9502 selected_variant = false;
9503 break;
9506 /* Otherwise, create the new variants. */
9507 if (!selected_variant)
9508 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9510 tree old_variant = v->type;
9511 tree new_variant = make_node (RECORD_TYPE);
9512 tree suffix
9513 = concat_name (DECL_NAME (gnu_variant_part),
9514 IDENTIFIER_POINTER (DECL_NAME (v->field)));
9515 TYPE_NAME (new_variant)
9516 = concat_name (TYPE_NAME (gnu_new_type),
9517 IDENTIFIER_POINTER (suffix));
9518 TYPE_REVERSE_STORAGE_ORDER (new_variant)
9519 = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type);
9520 copy_and_substitute_in_size (new_variant, old_variant,
9521 gnu_subst_list);
9522 v->new_type = new_variant;
9525 else
9527 gnu_variant_list.create (0);
9528 selected_variant = false;
9531 /* Make a list of fields and their position in the old type. */
9532 tree gnu_pos_list
9533 = build_position_list (gnu_old_type,
9534 gnu_variant_list.exists () && !selected_variant,
9535 size_zero_node, bitsize_zero_node,
9536 BIGGEST_ALIGNMENT, NULL_TREE);
9538 /* Now go down every component in the new type and compute its size and
9539 position from those of the component in the old type and the stored
9540 constraints of the new type. */
9541 Entity_Id gnat_field, gnat_old_field;
9542 for (gnat_field = First_Entity (gnat_new_type);
9543 Present (gnat_field);
9544 gnat_field = Next_Entity (gnat_field))
9545 if ((Ekind (gnat_field) == E_Component
9546 || (Ekind (gnat_field) == E_Discriminant
9547 && is_stored_discriminant (gnat_field, gnat_new_type)))
9548 && (gnat_old_field = is_subtype
9549 ? Original_Record_Component (gnat_field)
9550 : Corresponding_Record_Component (gnat_field))
9551 && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type
9552 && present_gnu_tree (gnat_old_field))
9554 Name_Id gnat_name = Chars (gnat_field);
9555 tree gnu_old_field = get_gnu_tree (gnat_old_field);
9556 if (TREE_CODE (gnu_old_field) == COMPONENT_REF)
9557 gnu_old_field = TREE_OPERAND (gnu_old_field, 1);
9558 tree gnu_context = DECL_CONTEXT (gnu_old_field);
9559 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
9560 tree gnu_cont_type, gnu_last = NULL_TREE;
9561 variant_desc *v = NULL;
9563 /* If the type is the same, retrieve the GCC type from the
9564 old field to take into account possible adjustments. */
9565 if (Etype (gnat_field) == Etype (gnat_old_field))
9566 gnu_field_type = TREE_TYPE (gnu_old_field);
9567 else
9568 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
9570 /* If there was a component clause, the field types must be the same
9571 for the old and new types, so copy the data from the old field to
9572 avoid recomputation here. Also if the field is justified modular
9573 and the optimization in gnat_to_gnu_field was applied. */
9574 if (Present (Component_Clause (gnat_old_field))
9575 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
9576 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
9577 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
9578 == TREE_TYPE (gnu_old_field)))
9580 gnu_size = DECL_SIZE (gnu_old_field);
9581 gnu_field_type = TREE_TYPE (gnu_old_field);
9584 /* If the old field was packed and of constant size, we have to get the
9585 old size here as it might differ from what the Etype conveys and the
9586 latter might overlap with the following field. Try to arrange the
9587 type for possible better packing along the way. */
9588 else if (DECL_PACKED (gnu_old_field)
9589 && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST)
9591 gnu_size = DECL_SIZE (gnu_old_field);
9592 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
9593 && !TYPE_FAT_POINTER_P (gnu_field_type)
9594 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
9595 gnu_field_type = make_packable_type (gnu_field_type, true);
9598 else
9599 gnu_size = TYPE_SIZE (gnu_field_type);
9601 /* If the context of the old field is the old type or its REP part,
9602 put the field directly in the new type; otherwise look up the
9603 context in the variant list and put the field either in the new
9604 type if there is a selected variant or in one new variant. */
9605 if (gnu_context == gnu_old_type
9606 || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part)))
9607 gnu_cont_type = gnu_new_type;
9608 else
9610 unsigned int i;
9611 tree rep_part;
9613 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9614 if (gnu_context == v->type
9615 || ((rep_part = get_rep_part (v->type))
9616 && gnu_context == TREE_TYPE (rep_part)))
9617 break;
9619 if (v)
9620 gnu_cont_type = selected_variant ? gnu_new_type : v->new_type;
9621 else
9622 /* The front-end may pass us zombie components if it fails to
9623 recognize that a constrain statically selects a particular
9624 variant. Discard them. */
9625 continue;
9628 /* Now create the new field modeled on the old one. */
9629 gnu_field
9630 = create_field_decl_from (gnu_old_field, gnu_field_type,
9631 gnu_cont_type, gnu_size,
9632 gnu_pos_list, gnu_subst_list);
9633 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
9635 /* If the context is a variant, put it in the new variant directly. */
9636 if (gnu_cont_type != gnu_new_type)
9638 if (TREE_CODE (gnu_pos) == INTEGER_CST)
9640 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
9641 TYPE_FIELDS (gnu_cont_type) = gnu_field;
9643 else
9645 DECL_CHAIN (gnu_field) = v->aux;
9646 v->aux = gnu_field;
9650 /* To match the layout crafted in components_to_record, if this is
9651 the _Tag or _Parent field, put it before any other fields. */
9652 else if (gnat_name == Name_uTag || gnat_name == Name_uParent)
9653 gnu_field_list = chainon (gnu_field_list, gnu_field);
9655 /* Similarly, if this is the _Controller field, put it before the
9656 other fields except for the _Tag or _Parent field. */
9657 else if (gnat_name == Name_uController && gnu_last)
9659 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
9660 DECL_CHAIN (gnu_last) = gnu_field;
9663 /* Otherwise, put it after the other fields. */
9664 else
9666 if (TREE_CODE (gnu_pos) == INTEGER_CST)
9668 DECL_CHAIN (gnu_field) = gnu_field_list;
9669 gnu_field_list = gnu_field;
9670 if (!gnu_last)
9671 gnu_last = gnu_field;
9673 else
9675 DECL_CHAIN (gnu_field) = gnu_variable_field_list;
9676 gnu_variable_field_list = gnu_field;
9680 /* For a stored discriminant in a derived type, replace the field. */
9681 if (!is_subtype && Ekind (gnat_field) == E_Discriminant)
9683 tree gnu_ref = get_gnu_tree (gnat_field);
9684 TREE_OPERAND (gnu_ref, 1) = gnu_field;
9686 else
9687 save_gnu_tree (gnat_field, gnu_field, false);
9690 /* Put the fields with fixed position in order of increasing position. */
9691 if (gnu_field_list)
9692 gnu_field_list = reverse_sort_field_list (gnu_field_list);
9694 /* Put the fields with variable position at the end. */
9695 if (gnu_variable_field_list)
9696 gnu_field_list = chainon (gnu_variable_field_list, gnu_field_list);
9698 /* If there is a variant list and no selected variant, we need to create the
9699 nest of variant parts from the old nest. */
9700 if (gnu_variant_list.exists () && !selected_variant)
9702 variant_desc *v;
9703 unsigned int i;
9705 /* Same processing as above for the fields of each variant. */
9706 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
9708 if (TYPE_FIELDS (v->new_type))
9709 TYPE_FIELDS (v->new_type)
9710 = reverse_sort_field_list (TYPE_FIELDS (v->new_type));
9711 if (v->aux)
9712 TYPE_FIELDS (v->new_type)
9713 = chainon (v->aux, TYPE_FIELDS (v->new_type));
9716 tree new_variant_part
9717 = create_variant_part_from (gnu_variant_part, gnu_variant_list,
9718 gnu_new_type, gnu_pos_list,
9719 gnu_subst_list, debug_info_p);
9720 DECL_CHAIN (new_variant_part) = gnu_field_list;
9721 gnu_field_list = new_variant_part;
9724 gnu_variant_list.release ();
9725 gnu_subst_list.release ();
9727 /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE.
9728 Otherwise sizes and alignment must be computed independently. */
9729 finish_record_type (gnu_new_type, nreverse (gnu_field_list),
9730 is_subtype ? 2 : 1, debug_info_p);
9732 /* Now go through the entities again looking for Itypes that we have not yet
9733 elaborated (e.g. Etypes of fields that have Original_Components). */
9734 for (Entity_Id gnat_field = First_Entity (gnat_new_type);
9735 Present (gnat_field);
9736 gnat_field = Next_Entity (gnat_field))
9737 if ((Ekind (gnat_field) == E_Component
9738 || Ekind (gnat_field) == E_Discriminant)
9739 && Is_Itype (Etype (gnat_field))
9740 && !present_gnu_tree (Etype (gnat_field)))
9741 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
9744 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9745 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9746 the original array type if it has been translated. This association is a
9747 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9748 that for standard DWARF, we also want to get the original type name. */
9750 static void
9751 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9753 Entity_Id gnat_original_array_type
9754 = Underlying_Type (Original_Array_Type (gnat_entity));
9755 tree gnu_original_array_type;
9757 if (!present_gnu_tree (gnat_original_array_type))
9758 return;
9760 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9762 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9763 return;
9765 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9767 tree original_name = TYPE_NAME (gnu_original_array_type);
9769 if (TREE_CODE (original_name) == TYPE_DECL)
9770 original_name = DECL_NAME (original_name);
9772 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9773 TYPE_NAME (gnu_type) = original_name;
9775 else
9776 add_parallel_type (gnu_type, gnu_original_array_type);
9779 /* Given a type T, a FIELD_DECL F, and a replacement value R, return an
9780 equivalent type with adjusted size expressions where all occurrences
9781 of references to F in a PLACEHOLDER_EXPR have been replaced by R.
9783 The function doesn't update the layout of the type, i.e. it assumes
9784 that the substitution is purely formal. That's why the replacement
9785 value R must itself contain a PLACEHOLDER_EXPR. */
9787 tree
9788 substitute_in_type (tree t, tree f, tree r)
9790 tree nt;
9792 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9794 switch (TREE_CODE (t))
9796 case INTEGER_TYPE:
9797 case ENUMERAL_TYPE:
9798 case BOOLEAN_TYPE:
9799 case REAL_TYPE:
9801 /* First the domain types of arrays. */
9802 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9803 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9805 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9806 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9808 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9809 return t;
9811 nt = copy_type (t);
9812 TYPE_GCC_MIN_VALUE (nt) = low;
9813 TYPE_GCC_MAX_VALUE (nt) = high;
9815 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9816 SET_TYPE_INDEX_TYPE
9817 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9819 return nt;
9822 /* Then the subtypes. */
9823 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9824 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9826 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9827 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9829 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9830 return t;
9832 nt = copy_type (t);
9833 SET_TYPE_RM_MIN_VALUE (nt, low);
9834 SET_TYPE_RM_MAX_VALUE (nt, high);
9836 return nt;
9839 return t;
9841 case COMPLEX_TYPE:
9842 nt = substitute_in_type (TREE_TYPE (t), f, r);
9843 if (nt == TREE_TYPE (t))
9844 return t;
9846 return build_complex_type (nt);
9848 case FUNCTION_TYPE:
9849 /* These should never show up here. */
9850 gcc_unreachable ();
9852 case ARRAY_TYPE:
9854 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9855 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9857 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9858 return t;
9860 nt = build_nonshared_array_type (component, domain);
9861 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9862 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9863 SET_TYPE_MODE (nt, TYPE_MODE (t));
9864 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9865 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9866 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9867 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9868 if (TYPE_REVERSE_STORAGE_ORDER (t))
9869 set_reverse_storage_order_on_array_type (nt);
9870 if (TYPE_NONALIASED_COMPONENT (t))
9871 set_nonaliased_component_on_array_type (nt);
9872 return nt;
9875 case RECORD_TYPE:
9876 case UNION_TYPE:
9877 case QUAL_UNION_TYPE:
9879 bool changed_field = false;
9880 tree field;
9882 /* Start out with no fields, make new fields, and chain them
9883 in. If we haven't actually changed the type of any field,
9884 discard everything we've done and return the old type. */
9885 nt = copy_type (t);
9886 TYPE_FIELDS (nt) = NULL_TREE;
9888 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9890 tree new_field = copy_node (field), new_n;
9892 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9893 if (new_n != TREE_TYPE (field))
9895 TREE_TYPE (new_field) = new_n;
9896 changed_field = true;
9899 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9900 if (new_n != DECL_FIELD_OFFSET (field))
9902 DECL_FIELD_OFFSET (new_field) = new_n;
9903 changed_field = true;
9906 /* Do the substitution inside the qualifier, if any. */
9907 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9909 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9910 if (new_n != DECL_QUALIFIER (field))
9912 DECL_QUALIFIER (new_field) = new_n;
9913 changed_field = true;
9917 DECL_CONTEXT (new_field) = nt;
9918 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9920 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9921 TYPE_FIELDS (nt) = new_field;
9924 if (!changed_field)
9925 return t;
9927 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9928 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9929 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9930 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9931 return nt;
9934 default:
9935 return t;
9939 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9940 needed to represent the object. */
9942 tree
9943 rm_size (tree gnu_type)
9945 /* For integral types, we store the RM size explicitly. */
9946 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9947 return TYPE_RM_SIZE (gnu_type);
9949 /* Return the RM size of the actual data plus the size of the template. */
9950 if (TREE_CODE (gnu_type) == RECORD_TYPE
9951 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9952 return
9953 size_binop (PLUS_EXPR,
9954 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9955 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9957 /* For record or union types, we store the size explicitly. */
9958 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9959 && !TYPE_FAT_POINTER_P (gnu_type)
9960 && TYPE_ADA_SIZE (gnu_type))
9961 return TYPE_ADA_SIZE (gnu_type);
9963 /* For other types, this is just the size. */
9964 return TYPE_SIZE (gnu_type);
9967 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9968 fully-qualified name, possibly with type information encoding.
9969 Otherwise, return the name. */
9971 static const char *
9972 get_entity_char (Entity_Id gnat_entity)
9974 Get_Encoded_Name (gnat_entity);
9975 return ggc_strdup (Name_Buffer);
9978 tree
9979 get_entity_name (Entity_Id gnat_entity)
9981 Get_Encoded_Name (gnat_entity);
9982 return get_identifier_with_length (Name_Buffer, Name_Len);
9985 /* Return an identifier representing the external name to be used for
9986 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9987 and the specified suffix. */
9989 tree
9990 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9992 const Entity_Kind kind = Ekind (gnat_entity);
9993 const bool has_suffix = (suffix != NULL);
9994 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9995 String_Pointer sp = {suffix, &temp};
9997 Get_External_Name (gnat_entity, has_suffix, sp);
9999 /* A variable using the Stdcall convention lives in a DLL. We adjust
10000 its name to use the jump table, the _imp__NAME contains the address
10001 for the NAME variable. */
10002 if ((kind == E_Variable || kind == E_Constant)
10003 && Has_Stdcall_Convention (gnat_entity))
10005 const int len = strlen (STDCALL_PREFIX) + Name_Len;
10006 char *new_name = (char *) alloca (len + 1);
10007 strcpy (new_name, STDCALL_PREFIX);
10008 strcat (new_name, Name_Buffer);
10009 return get_identifier_with_length (new_name, len);
10012 return get_identifier_with_length (Name_Buffer, Name_Len);
10015 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
10016 string, return a new IDENTIFIER_NODE that is the concatenation of
10017 the name followed by "___" and the specified suffix. */
10019 tree
10020 concat_name (tree gnu_name, const char *suffix)
10022 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
10023 char *new_name = (char *) alloca (len + 1);
10024 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
10025 strcat (new_name, "___");
10026 strcat (new_name, suffix);
10027 return get_identifier_with_length (new_name, len);
10030 /* Initialize data structures of the decl.c module. */
10032 void
10033 init_gnat_decl (void)
10035 /* Initialize the cache of annotated values. */
10036 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
10038 /* Initialize the association of dummy types with subprograms. */
10039 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
10042 /* Destroy data structures of the decl.c module. */
10044 void
10045 destroy_gnat_decl (void)
10047 /* Destroy the cache of annotated values. */
10048 annotate_value_cache->empty ();
10049 annotate_value_cache = NULL;
10051 /* Destroy the association of dummy types with subprograms. */
10052 dummy_to_subprog_map->empty ();
10053 dummy_to_subprog_map = NULL;
10056 #include "gt-ada-decl.h"