tree-vrp.c (compare_values_warnv): Simplify handling of symbolic ranges by calling...
[gcc.git] / gcc / ada / gcc-interface / decl.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2016, 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 ****************************************************************************/
25
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
38 #include "ada.h"
39 #include "types.h"
40 #include "atree.h"
41 #include "elists.h"
42 #include "namet.h"
43 #include "nlists.h"
44 #include "repinfo.h"
45 #include "snames.h"
46 #include "uintp.h"
47 #include "urealp.h"
48 #include "fe.h"
49 #include "sinfo.h"
50 #include "einfo.h"
51 #include "ada-tree.h"
52 #include "gigi.h"
53
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
57
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
59 #ifdef TARGET_64BIT
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
64 #else
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
67 #endif
68 #else
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
71 #endif
72
73 #define STDCALL_PREFIX "_imp__"
74
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
78
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
83
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
87 #else
88 #define FOREIGN_FORCE_REALIGN_STACK 0
89 #endif
90
91 struct incomplete
92 {
93 struct incomplete *next;
94 tree old_type;
95 Entity_Id full_type;
96 };
97
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing a record, an array or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
102
103 /* This variable is used to delay expanding From_Limited_With types until the
104 end of the spec. */
105 static struct incomplete *defer_limited_with_list;
106
107 typedef struct subst_pair_d {
108 tree discriminant;
109 tree replacement;
110 } subst_pair;
111
112
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
115 tree type;
116
117 /* The associated field. */
118 tree field;
119
120 /* The value of the qualifier. */
121 tree qual;
122
123 /* The type of the variant after transformation. */
124 tree new_type;
125 } variant_desc;
126
127
128 /* A map used to cache the result of annotate_value. */
129 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
130 {
131 static inline hashval_t
132 hash (tree_int_map *m)
133 {
134 return htab_hash_pointer (m->base.from);
135 }
136
137 static inline bool
138 equal (tree_int_map *a, tree_int_map *b)
139 {
140 return a->base.from == b->base.from;
141 }
142
143 static int
144 keep_cache_entry (tree_int_map *&m)
145 {
146 return ggc_marked_p (m->base.from);
147 }
148 };
149
150 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
151
152 /* A map used to associate a dummy type with a list of subprogram entities. */
153 struct GTY((for_user)) tree_entity_vec_map
154 {
155 struct tree_map_base base;
156 vec<Entity_Id, va_gc_atomic> *to;
157 };
158
159 void
160 gt_pch_nx (Entity_Id &)
161 {
162 }
163
164 void
165 gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie)
166 {
167 op (x, cookie);
168 }
169
170 struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map>
171 {
172 static inline hashval_t
173 hash (tree_entity_vec_map *m)
174 {
175 return htab_hash_pointer (m->base.from);
176 }
177
178 static inline bool
179 equal (tree_entity_vec_map *a, tree_entity_vec_map *b)
180 {
181 return a->base.from == b->base.from;
182 }
183
184 static int
185 keep_cache_entry (tree_entity_vec_map *&m)
186 {
187 return ggc_marked_p (m->base.from);
188 }
189 };
190
191 static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map;
192
193 static void prepend_one_attribute (struct attrib **,
194 enum attrib_type, tree, tree, Node_Id);
195 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
196 static void prepend_attributes (struct attrib **, Entity_Id);
197 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
198 bool);
199 static bool type_has_variable_size (tree);
200 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
201 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
202 unsigned int);
203 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
204 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
205 static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *);
206 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
207 static tree change_qualified_type (tree, int);
208 static bool same_discriminant_p (Entity_Id, Entity_Id);
209 static bool array_type_has_nonaliased_component (tree, Entity_Id);
210 static bool compile_time_known_address_p (Node_Id);
211 static bool cannot_be_superflat (Node_Id);
212 static bool constructor_address_p (tree);
213 static bool allocatable_size_p (tree, bool);
214 static bool initial_value_needs_conversion (tree, tree);
215 static int compare_field_bitpos (const PTR, const PTR);
216 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
217 bool, bool, bool, bool, bool, tree, tree *);
218 static Uint annotate_value (tree);
219 static void annotate_rep (Entity_Id, tree);
220 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
221 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
222 static vec<variant_desc> build_variant_list (tree,
223 vec<subst_pair> ,
224 vec<variant_desc> );
225 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
226 static void set_rm_size (Uint, tree, Entity_Id);
227 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
228 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
229 static tree create_field_decl_from (tree, tree, tree, tree, tree,
230 vec<subst_pair> );
231 static tree create_rep_part (tree, tree, tree);
232 static tree get_rep_part (tree);
233 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
234 tree, vec<subst_pair> );
235 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
236 static void associate_original_type_to_packed_array (tree, Entity_Id);
237 static const char *get_entity_char (Entity_Id);
238
239 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
240 to pass around calls performing profile compatibility checks. */
241
242 typedef struct {
243 Entity_Id gnat_entity; /* The Ada subprogram entity. */
244 tree ada_fntype; /* The corresponding GCC type node. */
245 tree btin_fntype; /* The GCC builtin function type node. */
246 } intrin_binding_t;
247
248 static bool intrin_profiles_compatible_p (intrin_binding_t *);
249 \f
250 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
251 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
252 and associate the ..._DECL node with the input GNAT defining identifier.
253
254 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
255 initial value (in GCC tree form). This is optional for a variable. For
256 a renamed entity, GNU_EXPR gives the object being renamed.
257
258 DEFINITION is true if this call is intended for a definition. This is used
259 for separate compilation where it is necessary to know whether an external
260 declaration or a definition must be created if the GCC equivalent was not
261 created previously. */
262
263 tree
264 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
265 {
266 /* Contains the kind of the input GNAT node. */
267 const Entity_Kind kind = Ekind (gnat_entity);
268 /* True if this is a type. */
269 const bool is_type = IN (kind, Type_Kind);
270 /* True if this is an artificial entity. */
271 const bool artificial_p = !Comes_From_Source (gnat_entity);
272 /* True if debug info is requested for this entity. */
273 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
274 /* True if this entity is to be considered as imported. */
275 const bool imported_p
276 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
277 /* For a type, contains the equivalent GNAT node to be used in gigi. */
278 Entity_Id gnat_equiv_type = Empty;
279 /* Temporary used to walk the GNAT tree. */
280 Entity_Id gnat_temp;
281 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
282 This node will be associated with the GNAT node by calling at the end
283 of the `switch' statement. */
284 tree gnu_decl = NULL_TREE;
285 /* Contains the GCC type to be used for the GCC node. */
286 tree gnu_type = NULL_TREE;
287 /* Contains the GCC size tree to be used for the GCC node. */
288 tree gnu_size = NULL_TREE;
289 /* Contains the GCC name to be used for the GCC node. */
290 tree gnu_entity_name;
291 /* True if we have already saved gnu_decl as a GNAT association. */
292 bool saved = false;
293 /* True if we incremented defer_incomplete_level. */
294 bool this_deferred = false;
295 /* True if we incremented force_global. */
296 bool this_global = false;
297 /* True if we should check to see if elaborated during processing. */
298 bool maybe_present = false;
299 /* True if we made GNU_DECL and its type here. */
300 bool this_made_decl = false;
301 /* Size and alignment of the GCC node, if meaningful. */
302 unsigned int esize = 0, align = 0;
303 /* Contains the list of attributes directly attached to the entity. */
304 struct attrib *attr_list = NULL;
305
306 /* Since a use of an Itype is a definition, process it as such if it
307 is not in a with'ed unit. */
308 if (!definition
309 && is_type
310 && Is_Itype (gnat_entity)
311 && !present_gnu_tree (gnat_entity)
312 && In_Extended_Main_Code_Unit (gnat_entity))
313 {
314 /* Ensure that we are in a subprogram mentioned in the Scope chain of
315 this entity, our current scope is global, or we encountered a task
316 or entry (where we can't currently accurately check scoping). */
317 if (!current_function_decl
318 || DECL_ELABORATION_PROC_P (current_function_decl))
319 {
320 process_type (gnat_entity);
321 return get_gnu_tree (gnat_entity);
322 }
323
324 for (gnat_temp = Scope (gnat_entity);
325 Present (gnat_temp);
326 gnat_temp = Scope (gnat_temp))
327 {
328 if (Is_Type (gnat_temp))
329 gnat_temp = Underlying_Type (gnat_temp);
330
331 if (Ekind (gnat_temp) == E_Subprogram_Body)
332 gnat_temp
333 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
334
335 if (IN (Ekind (gnat_temp), Subprogram_Kind)
336 && Present (Protected_Body_Subprogram (gnat_temp)))
337 gnat_temp = Protected_Body_Subprogram (gnat_temp);
338
339 if (Ekind (gnat_temp) == E_Entry
340 || Ekind (gnat_temp) == E_Entry_Family
341 || Ekind (gnat_temp) == E_Task_Type
342 || (IN (Ekind (gnat_temp), Subprogram_Kind)
343 && present_gnu_tree (gnat_temp)
344 && (current_function_decl
345 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))))
346 {
347 process_type (gnat_entity);
348 return get_gnu_tree (gnat_entity);
349 }
350 }
351
352 /* This abort means the Itype has an incorrect scope, i.e. that its
353 scope does not correspond to the subprogram it is declared in. */
354 gcc_unreachable ();
355 }
356
357 /* If we've already processed this entity, return what we got last time.
358 If we are defining the node, we should not have already processed it.
359 In that case, we will abort below when we try to save a new GCC tree
360 for this object. We also need to handle the case of getting a dummy
361 type when a Full_View exists but be careful so as not to trigger its
362 premature elaboration. */
363 if ((!definition || (is_type && imported_p))
364 && present_gnu_tree (gnat_entity))
365 {
366 gnu_decl = get_gnu_tree (gnat_entity);
367
368 if (TREE_CODE (gnu_decl) == TYPE_DECL
369 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
370 && IN (kind, Incomplete_Or_Private_Kind)
371 && Present (Full_View (gnat_entity))
372 && (present_gnu_tree (Full_View (gnat_entity))
373 || No (Freeze_Node (Full_View (gnat_entity)))))
374 {
375 gnu_decl
376 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, false);
377 save_gnu_tree (gnat_entity, NULL_TREE, false);
378 save_gnu_tree (gnat_entity, gnu_decl, false);
379 }
380
381 return gnu_decl;
382 }
383
384 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
385 must be specified unless it was specified by the programmer. Exceptions
386 are for access-to-protected-subprogram types and all access subtypes, as
387 another GNAT type is used to lay out the GCC type for them. */
388 gcc_assert (!Unknown_Esize (gnat_entity)
389 || Has_Size_Clause (gnat_entity)
390 || (!IN (kind, Numeric_Kind)
391 && !IN (kind, Enumeration_Kind)
392 && (!IN (kind, Access_Kind)
393 || kind == E_Access_Protected_Subprogram_Type
394 || kind == E_Anonymous_Access_Protected_Subprogram_Type
395 || kind == E_Access_Subtype
396 || type_annotate_only)));
397
398 /* The RM size must be specified for all discrete and fixed-point types. */
399 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
400 && Unknown_RM_Size (gnat_entity)));
401
402 /* If we get here, it means we have not yet done anything with this entity.
403 If we are not defining it, it must be a type or an entity that is defined
404 elsewhere or externally, otherwise we should have defined it already. */
405 gcc_assert (definition
406 || type_annotate_only
407 || is_type
408 || kind == E_Discriminant
409 || kind == E_Component
410 || kind == E_Label
411 || (kind == E_Constant && Present (Full_View (gnat_entity)))
412 || Is_Public (gnat_entity));
413
414 /* Get the name of the entity and set up the line number and filename of
415 the original definition for use in any decl we make. Make sure we do not
416 inherit another source location. */
417 gnu_entity_name = get_entity_name (gnat_entity);
418 if (Sloc (gnat_entity) != No_Location
419 && !renaming_from_generic_instantiation_p (gnat_entity))
420 Sloc_to_locus (Sloc (gnat_entity), &input_location);
421
422 /* For cases when we are not defining (i.e., we are referencing from
423 another compilation unit) public entities, show we are at global level
424 for the purpose of computing scopes. Don't do this for components or
425 discriminants since the relevant test is whether or not the record is
426 being defined. */
427 if (!definition
428 && kind != E_Component
429 && kind != E_Discriminant
430 && Is_Public (gnat_entity)
431 && !Is_Statically_Allocated (gnat_entity))
432 force_global++, this_global = true;
433
434 /* Handle any attributes directly attached to the entity. */
435 if (Has_Gigi_Rep_Item (gnat_entity))
436 prepend_attributes (&attr_list, gnat_entity);
437
438 /* Do some common processing for types. */
439 if (is_type)
440 {
441 /* Compute the equivalent type to be used in gigi. */
442 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
443
444 /* Machine_Attributes on types are expected to be propagated to
445 subtypes. The corresponding Gigi_Rep_Items are only attached
446 to the first subtype though, so we handle the propagation here. */
447 if (Base_Type (gnat_entity) != gnat_entity
448 && !Is_First_Subtype (gnat_entity)
449 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
450 prepend_attributes (&attr_list,
451 First_Subtype (Base_Type (gnat_entity)));
452
453 /* Compute a default value for the size of an elementary type. */
454 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
455 {
456 unsigned int max_esize;
457
458 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
459 esize = UI_To_Int (Esize (gnat_entity));
460
461 if (IN (kind, Float_Kind))
462 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
463 else if (IN (kind, Access_Kind))
464 max_esize = POINTER_SIZE * 2;
465 else
466 max_esize = LONG_LONG_TYPE_SIZE;
467
468 if (esize > max_esize)
469 esize = max_esize;
470 }
471 }
472
473 switch (kind)
474 {
475 case E_Component:
476 case E_Discriminant:
477 {
478 /* The GNAT record where the component was defined. */
479 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
480
481 /* If the entity is a discriminant of an extended tagged type used to
482 rename a discriminant of the parent type, return the latter. */
483 if (Is_Tagged_Type (gnat_record)
484 && Present (Corresponding_Discriminant (gnat_entity)))
485 {
486 gnu_decl
487 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
488 gnu_expr, definition);
489 saved = true;
490 break;
491 }
492
493 /* If the entity is an inherited component (in the case of extended
494 tagged record types), just return the original entity, which must
495 be a FIELD_DECL. Likewise for discriminants. If the entity is a
496 non-girder discriminant (in the case of derived untagged record
497 types), return the stored discriminant it renames. */
498 else if (Present (Original_Record_Component (gnat_entity))
499 && Original_Record_Component (gnat_entity) != gnat_entity)
500 {
501 gnu_decl
502 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
503 gnu_expr, definition);
504 saved = true;
505 break;
506 }
507
508 /* Otherwise, if we are not defining this and we have no GCC type
509 for the containing record, make one for it. Then we should
510 have made our own equivalent. */
511 else if (!definition && !present_gnu_tree (gnat_record))
512 {
513 /* ??? If this is in a record whose scope is a protected
514 type and we have an Original_Record_Component, use it.
515 This is a workaround for major problems in protected type
516 handling. */
517 Entity_Id Scop = Scope (Scope (gnat_entity));
518 if (Is_Protected_Type (Underlying_Type (Scop))
519 && Present (Original_Record_Component (gnat_entity)))
520 {
521 gnu_decl
522 = gnat_to_gnu_entity (Original_Record_Component
523 (gnat_entity),
524 gnu_expr, false);
525 saved = true;
526 break;
527 }
528
529 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false);
530 gnu_decl = get_gnu_tree (gnat_entity);
531 saved = true;
532 break;
533 }
534
535 else
536 /* Here we have no GCC type and this is a reference rather than a
537 definition. This should never happen. Most likely the cause is
538 reference before declaration in the GNAT tree for gnat_entity. */
539 gcc_unreachable ();
540 }
541
542 case E_Constant:
543 /* Ignore constant definitions already marked with the error node. See
544 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
545 if (definition
546 && present_gnu_tree (gnat_entity)
547 && get_gnu_tree (gnat_entity) == error_mark_node)
548 {
549 maybe_present = true;
550 break;
551 }
552
553 /* Ignore deferred constant definitions without address clause since
554 they are processed fully in the front-end. If No_Initialization
555 is set, this is not a deferred constant but a constant whose value
556 is built manually. And constants that are renamings are handled
557 like variables. */
558 if (definition
559 && !gnu_expr
560 && No (Address_Clause (gnat_entity))
561 && !No_Initialization (Declaration_Node (gnat_entity))
562 && No (Renamed_Object (gnat_entity)))
563 {
564 gnu_decl = error_mark_node;
565 saved = true;
566 break;
567 }
568
569 /* If this is a use of a deferred constant without address clause,
570 get its full definition. */
571 if (!definition
572 && No (Address_Clause (gnat_entity))
573 && Present (Full_View (gnat_entity)))
574 {
575 gnu_decl
576 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false);
577 saved = true;
578 break;
579 }
580
581 /* If we have a constant that we are not defining, get the expression it
582 was defined to represent. This is necessary to avoid generating dumb
583 elaboration code in simple cases, but we may throw it away later if it
584 is not a constant. But do not retrieve it if it is an allocator since
585 the designated type might still be dummy at this point. */
586 if (!definition
587 && !No_Initialization (Declaration_Node (gnat_entity))
588 && Present (Expression (Declaration_Node (gnat_entity)))
589 && Nkind (Expression (Declaration_Node (gnat_entity)))
590 != N_Allocator)
591 /* The expression may contain N_Expression_With_Actions nodes and
592 thus object declarations from other units. Discard them. */
593 gnu_expr
594 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
595
596 /* ... fall through ... */
597
598 case E_Exception:
599 case E_Loop_Parameter:
600 case E_Out_Parameter:
601 case E_Variable:
602 {
603 /* Always create a variable for volatile objects and variables seen
604 constant but with a Linker_Section pragma. */
605 bool const_flag
606 = ((kind == E_Constant || kind == E_Variable)
607 && Is_True_Constant (gnat_entity)
608 && !(kind == E_Variable
609 && Present (Linker_Section_Pragma (gnat_entity)))
610 && !Treat_As_Volatile (gnat_entity)
611 && (((Nkind (Declaration_Node (gnat_entity))
612 == N_Object_Declaration)
613 && Present (Expression (Declaration_Node (gnat_entity))))
614 || Present (Renamed_Object (gnat_entity))
615 || imported_p));
616 bool inner_const_flag = const_flag;
617 bool static_flag = Is_Statically_Allocated (gnat_entity);
618 /* We implement RM 13.3(19) for exported and imported (non-constant)
619 objects by making them volatile. */
620 bool volatile_flag
621 = (Treat_As_Volatile (gnat_entity)
622 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
623 bool mutable_p = false;
624 bool used_by_ref = false;
625 tree gnu_ext_name = NULL_TREE;
626 tree renamed_obj = NULL_TREE;
627 tree gnu_object_size;
628
629 /* We need to translate the renamed object even though we are only
630 referencing the renaming. But it may contain a call for which
631 we'll generate a temporary to hold the return value and which
632 is part of the definition of the renaming, so discard it. */
633 if (Present (Renamed_Object (gnat_entity)) && !definition)
634 {
635 if (kind == E_Exception)
636 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
637 NULL_TREE, false);
638 else
639 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
640 }
641
642 /* Get the type after elaborating the renamed object. */
643 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
644
645 /* If this is a standard exception definition, then use the standard
646 exception type. This is necessary to make sure that imported and
647 exported views of exceptions are properly merged in LTO mode. */
648 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
649 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
650 gnu_type = except_type_node;
651
652 /* For a debug renaming declaration, build a debug-only entity. */
653 if (Present (Debug_Renaming_Link (gnat_entity)))
654 {
655 /* Force a non-null value to make sure the symbol is retained. */
656 tree value = build1 (INDIRECT_REF, gnu_type,
657 build1 (NOP_EXPR,
658 build_pointer_type (gnu_type),
659 integer_minus_one_node));
660 gnu_decl = build_decl (input_location,
661 VAR_DECL, gnu_entity_name, gnu_type);
662 SET_DECL_VALUE_EXPR (gnu_decl, value);
663 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
664 gnat_pushdecl (gnu_decl, gnat_entity);
665 break;
666 }
667
668 /* If this is a loop variable, its type should be the base type.
669 This is because the code for processing a loop determines whether
670 a normal loop end test can be done by comparing the bounds of the
671 loop against those of the base type, which is presumed to be the
672 size used for computation. But this is not correct when the size
673 of the subtype is smaller than the type. */
674 if (kind == E_Loop_Parameter)
675 gnu_type = get_base_type (gnu_type);
676
677 /* Reject non-renamed objects whose type is an unconstrained array or
678 any object whose type is a dummy type or void. */
679 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
680 && No (Renamed_Object (gnat_entity)))
681 || TYPE_IS_DUMMY_P (gnu_type)
682 || TREE_CODE (gnu_type) == VOID_TYPE)
683 {
684 gcc_assert (type_annotate_only);
685 if (this_global)
686 force_global--;
687 return error_mark_node;
688 }
689
690 /* If an alignment is specified, use it if valid. Note that exceptions
691 are objects but don't have an alignment. We must do this before we
692 validate the size, since the alignment can affect the size. */
693 if (kind != E_Exception && Known_Alignment (gnat_entity))
694 {
695 gcc_assert (Present (Alignment (gnat_entity)));
696
697 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
698 TYPE_ALIGN (gnu_type));
699
700 /* No point in changing the type if there is an address clause
701 as the final type of the object will be a reference type. */
702 if (Present (Address_Clause (gnat_entity)))
703 align = 0;
704 else
705 {
706 tree orig_type = gnu_type;
707
708 gnu_type
709 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
710 false, false, definition, true);
711
712 /* If a padding record was made, declare it now since it will
713 never be declared otherwise. This is necessary to ensure
714 that its subtrees are properly marked. */
715 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
716 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
717 debug_info_p, gnat_entity);
718 }
719 }
720
721 /* If we are defining the object, see if it has a Size and validate it
722 if so. If we are not defining the object and a Size clause applies,
723 simply retrieve the value. We don't want to ignore the clause and
724 it is expected to have been validated already. Then get the new
725 type, if any. */
726 if (definition)
727 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
728 gnat_entity, VAR_DECL, false,
729 Has_Size_Clause (gnat_entity));
730 else if (Has_Size_Clause (gnat_entity))
731 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
732
733 if (gnu_size)
734 {
735 gnu_type
736 = make_type_from_size (gnu_type, gnu_size,
737 Has_Biased_Representation (gnat_entity));
738
739 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
740 gnu_size = NULL_TREE;
741 }
742
743 /* If this object has self-referential size, it must be a record with
744 a default discriminant. We are supposed to allocate an object of
745 the maximum size in this case, unless it is a constant with an
746 initializing expression, in which case we can get the size from
747 that. Note that the resulting size may still be a variable, so
748 this may end up with an indirect allocation. */
749 if (No (Renamed_Object (gnat_entity))
750 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
751 {
752 if (gnu_expr && kind == E_Constant)
753 {
754 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
755 if (CONTAINS_PLACEHOLDER_P (size))
756 {
757 /* If the initializing expression is itself a constant,
758 despite having a nominal type with self-referential
759 size, we can get the size directly from it. */
760 if (TREE_CODE (gnu_expr) == COMPONENT_REF
761 && TYPE_IS_PADDING_P
762 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
763 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
764 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
765 || DECL_READONLY_ONCE_ELAB
766 (TREE_OPERAND (gnu_expr, 0))))
767 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
768 else
769 gnu_size
770 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
771 }
772 else
773 gnu_size = size;
774 }
775 /* We may have no GNU_EXPR because No_Initialization is
776 set even though there's an Expression. */
777 else if (kind == E_Constant
778 && (Nkind (Declaration_Node (gnat_entity))
779 == N_Object_Declaration)
780 && Present (Expression (Declaration_Node (gnat_entity))))
781 gnu_size
782 = TYPE_SIZE (gnat_to_gnu_type
783 (Etype
784 (Expression (Declaration_Node (gnat_entity)))));
785 else
786 {
787 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
788 mutable_p = true;
789 }
790
791 /* If we are at global level and the size isn't constant, call
792 elaborate_expression_1 to make a variable for it rather than
793 calculating it each time. */
794 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
795 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
796 "SIZE", definition, false);
797 }
798
799 /* If the size is zero byte, make it one byte since some linkers have
800 troubles with zero-sized objects. If the object will have a
801 template, that will make it nonzero so don't bother. Also avoid
802 doing that for an object renaming or an object with an address
803 clause, as we would lose useful information on the view size
804 (e.g. for null array slices) and we are not allocating the object
805 here anyway. */
806 if (((gnu_size
807 && integer_zerop (gnu_size)
808 && !TREE_OVERFLOW (gnu_size))
809 || (TYPE_SIZE (gnu_type)
810 && integer_zerop (TYPE_SIZE (gnu_type))
811 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
812 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
813 && No (Renamed_Object (gnat_entity))
814 && No (Address_Clause (gnat_entity)))
815 gnu_size = bitsize_unit_node;
816
817 /* If this is an object with no specified size and alignment, and
818 if either it is atomic or we are not optimizing alignment for
819 space and it is composite and not an exception, an Out parameter
820 or a reference to another object, and the size of its type is a
821 constant, set the alignment to the smallest one which is not
822 smaller than the size, with an appropriate cap. */
823 if (!gnu_size && align == 0
824 && (Is_Atomic_Or_VFA (gnat_entity)
825 || (!Optimize_Alignment_Space (gnat_entity)
826 && kind != E_Exception
827 && kind != E_Out_Parameter
828 && Is_Composite_Type (Etype (gnat_entity))
829 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
830 && !Is_Exported (gnat_entity)
831 && !imported_p
832 && No (Renamed_Object (gnat_entity))
833 && No (Address_Clause (gnat_entity))))
834 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
835 {
836 unsigned int size_cap, align_cap;
837
838 /* No point in promoting the alignment if this doesn't prevent
839 BLKmode access to the object, in particular block copy, as
840 this will for example disable the NRV optimization for it.
841 No point in jumping through all the hoops needed in order
842 to support BIGGEST_ALIGNMENT if we don't really have to.
843 So we cap to the smallest alignment that corresponds to
844 a known efficient memory access pattern of the target. */
845 if (Is_Atomic_Or_VFA (gnat_entity))
846 {
847 size_cap = UINT_MAX;
848 align_cap = BIGGEST_ALIGNMENT;
849 }
850 else
851 {
852 size_cap = MAX_FIXED_MODE_SIZE;
853 align_cap = get_mode_alignment (ptr_mode);
854 }
855
856 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
857 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
858 align = 0;
859 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
860 align = align_cap;
861 else
862 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
863
864 /* But make sure not to under-align the object. */
865 if (align <= TYPE_ALIGN (gnu_type))
866 align = 0;
867
868 /* And honor the minimum valid atomic alignment, if any. */
869 #ifdef MINIMUM_ATOMIC_ALIGNMENT
870 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
871 align = MINIMUM_ATOMIC_ALIGNMENT;
872 #endif
873 }
874
875 /* If the object is set to have atomic components, find the component
876 type and validate it.
877
878 ??? Note that we ignore Has_Volatile_Components on objects; it's
879 not at all clear what to do in that case. */
880 if (Has_Atomic_Components (gnat_entity))
881 {
882 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
883 ? TREE_TYPE (gnu_type) : gnu_type);
884
885 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
886 && TYPE_MULTI_ARRAY_P (gnu_inner))
887 gnu_inner = TREE_TYPE (gnu_inner);
888
889 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
890 }
891
892 /* If this is an aliased object with an unconstrained array nominal
893 subtype, make a type that includes the template. We will either
894 allocate or create a variable of that type, see below. */
895 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
896 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
897 && !type_annotate_only)
898 {
899 tree gnu_array
900 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
901 gnu_type
902 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
903 gnu_type,
904 concat_name (gnu_entity_name,
905 "UNC"),
906 debug_info_p);
907 }
908
909 /* ??? If this is an object of CW type initialized to a value, try to
910 ensure that the object is sufficient aligned for this value, but
911 without pessimizing the allocation. This is a kludge necessary
912 because we don't support dynamic alignment. */
913 if (align == 0
914 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
915 && No (Renamed_Object (gnat_entity))
916 && No (Address_Clause (gnat_entity)))
917 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
918
919 #ifdef MINIMUM_ATOMIC_ALIGNMENT
920 /* If the size is a constant and no alignment is specified, force
921 the alignment to be the minimum valid atomic alignment. The
922 restriction on constant size avoids problems with variable-size
923 temporaries; if the size is variable, there's no issue with
924 atomic access. Also don't do this for a constant, since it isn't
925 necessary and can interfere with constant replacement. Finally,
926 do not do it for Out parameters since that creates an
927 size inconsistency with In parameters. */
928 if (align == 0
929 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
930 && !FLOAT_TYPE_P (gnu_type)
931 && !const_flag && No (Renamed_Object (gnat_entity))
932 && !imported_p && No (Address_Clause (gnat_entity))
933 && kind != E_Out_Parameter
934 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
935 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
936 align = MINIMUM_ATOMIC_ALIGNMENT;
937 #endif
938
939 /* Make a new type with the desired size and alignment, if needed.
940 But do not take into account alignment promotions to compute the
941 size of the object. */
942 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
943 if (gnu_size || align > 0)
944 {
945 tree orig_type = gnu_type;
946
947 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
948 false, false, definition, true);
949
950 /* If a padding record was made, declare it now since it will
951 never be declared otherwise. This is necessary to ensure
952 that its subtrees are properly marked. */
953 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
954 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
955 debug_info_p, gnat_entity);
956 }
957
958 /* Now check if the type of the object allows atomic access. */
959 if (Is_Atomic_Or_VFA (gnat_entity))
960 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
961
962 /* If this is a renaming, avoid as much as possible to create a new
963 object. However, in some cases, creating it is required because
964 renaming can be applied to objects that are not names in Ada.
965 This processing needs to be applied to the raw expression so as
966 to make it more likely to rename the underlying object. */
967 if (Present (Renamed_Object (gnat_entity)))
968 {
969 /* If the renamed object had padding, strip off the reference to
970 the inner object and reset our type. */
971 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
972 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
973 /* Strip useless conversions around the object. */
974 || gnat_useless_type_conversion (gnu_expr))
975 {
976 gnu_expr = TREE_OPERAND (gnu_expr, 0);
977 gnu_type = TREE_TYPE (gnu_expr);
978 }
979
980 /* Or else, if the renamed object has an unconstrained type with
981 default discriminant, use the padded type. */
982 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
983 gnu_type = TREE_TYPE (gnu_expr);
984
985 /* Case 1: if this is a constant renaming stemming from a function
986 call, treat it as a normal object whose initial value is what
987 is being renamed. RM 3.3 says that the result of evaluating a
988 function call is a constant object. Therefore, it can be the
989 inner object of a constant renaming and the renaming must be
990 fully instantiated, i.e. it cannot be a reference to (part of)
991 an existing object. And treat other rvalues (addresses, null
992 expressions, constructors and literals) the same way. */
993 tree inner = gnu_expr;
994 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
995 inner = TREE_OPERAND (inner, 0);
996 /* Expand_Dispatching_Call can prepend a comparison of the tags
997 before the call to "=". */
998 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
999 || TREE_CODE (inner) == COMPOUND_EXPR)
1000 inner = TREE_OPERAND (inner, 1);
1001 if ((TREE_CODE (inner) == CALL_EXPR
1002 && !call_is_atomic_load (inner))
1003 || TREE_CODE (inner) == ADDR_EXPR
1004 || TREE_CODE (inner) == NULL_EXPR
1005 || TREE_CODE (inner) == CONSTRUCTOR
1006 || CONSTANT_CLASS_P (inner)
1007 /* We need to detect the case where a temporary is created to
1008 hold the return value, since we cannot safely rename it at
1009 top level as it lives only in the elaboration routine. */
1010 || (TREE_CODE (inner) == VAR_DECL
1011 && DECL_RETURN_VALUE_P (inner))
1012 /* We also need to detect the case where the front-end creates
1013 a dangling 'reference to a function call at top level and
1014 substitutes it in the renaming, for example:
1015
1016 q__b : boolean renames r__f.e (1);
1017
1018 can be rewritten into:
1019
1020 q__R1s : constant q__A2s := r__f'reference;
1021 [...]
1022 q__b : boolean renames q__R1s.all.e (1);
1023
1024 We cannot safely rename the rewritten expression since the
1025 underlying object lives only in the elaboration routine. */
1026 || (TREE_CODE (inner) == INDIRECT_REF
1027 && (inner
1028 = remove_conversions (TREE_OPERAND (inner, 0), true))
1029 && TREE_CODE (inner) == VAR_DECL
1030 && DECL_RETURN_VALUE_P (inner)))
1031 ;
1032
1033 /* Case 2: if the renaming entity need not be materialized, use
1034 the elaborated renamed expression for the renaming. But this
1035 means that the caller is responsible for evaluating the address
1036 of the renaming in the correct place for the definition case to
1037 instantiate the SAVE_EXPRs. */
1038 else if (!Materialize_Entity (gnat_entity))
1039 {
1040 tree init = NULL_TREE;
1041
1042 gnu_decl
1043 = elaborate_reference (gnu_expr, gnat_entity, definition,
1044 &init);
1045
1046 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1047 correct place for this case. */
1048 gcc_assert (!init);
1049
1050 /* No DECL_EXPR will be created so the expression needs to be
1051 marked manually because it will likely be shared. */
1052 if (global_bindings_p ())
1053 MARK_VISITED (gnu_decl);
1054
1055 /* This assertion will fail if the renamed object isn't aligned
1056 enough as to make it possible to honor the alignment set on
1057 the renaming. */
1058 if (align)
1059 {
1060 unsigned int ralign = DECL_P (gnu_decl)
1061 ? DECL_ALIGN (gnu_decl)
1062 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1063 gcc_assert (ralign >= align);
1064 }
1065
1066 save_gnu_tree (gnat_entity, gnu_decl, true);
1067 saved = true;
1068 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1069 break;
1070 }
1071
1072 /* Case 3: otherwise, make a constant pointer to the object we
1073 are renaming and attach the object to the pointer after it is
1074 elaborated. The object will be referenced directly instead
1075 of indirectly via the pointer to avoid aliasing problems with
1076 non-addressable entities. The pointer is called a "renaming"
1077 pointer in this case. Note that we also need to preserve the
1078 volatility of the renamed object through the indirection. */
1079 else
1080 {
1081 tree init = NULL_TREE;
1082
1083 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1084 gnu_type
1085 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1086 gnu_type = build_reference_type (gnu_type);
1087 used_by_ref = true;
1088 const_flag = true;
1089 volatile_flag = false;
1090 inner_const_flag = TREE_READONLY (gnu_expr);
1091 gnu_size = NULL_TREE;
1092
1093 renamed_obj
1094 = elaborate_reference (gnu_expr, gnat_entity, definition,
1095 &init);
1096
1097 /* The expression needs to be marked manually because it will
1098 likely be shared, even for a definition since the ADDR_EXPR
1099 built below can cause the first few nodes to be folded. */
1100 if (global_bindings_p ())
1101 MARK_VISITED (renamed_obj);
1102
1103 if (type_annotate_only
1104 && TREE_CODE (renamed_obj) == ERROR_MARK)
1105 gnu_expr = NULL_TREE;
1106 else
1107 {
1108 gnu_expr
1109 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1110 if (init)
1111 gnu_expr
1112 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1113 gnu_expr);
1114 }
1115 }
1116 }
1117
1118 /* If we are defining an aliased object whose nominal subtype is
1119 unconstrained, the object is a record that contains both the
1120 template and the object. If there is an initializer, it will
1121 have already been converted to the right type, but we need to
1122 create the template if there is no initializer. */
1123 if (definition
1124 && !gnu_expr
1125 && TREE_CODE (gnu_type) == RECORD_TYPE
1126 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1127 /* Beware that padding might have been introduced above. */
1128 || (TYPE_PADDING_P (gnu_type)
1129 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1130 == RECORD_TYPE
1131 && TYPE_CONTAINS_TEMPLATE_P
1132 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1133 {
1134 tree template_field
1135 = TYPE_PADDING_P (gnu_type)
1136 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1137 : TYPE_FIELDS (gnu_type);
1138 vec<constructor_elt, va_gc> *v;
1139 vec_alloc (v, 1);
1140 tree t = build_template (TREE_TYPE (template_field),
1141 TREE_TYPE (DECL_CHAIN (template_field)),
1142 NULL_TREE);
1143 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1144 gnu_expr = gnat_build_constructor (gnu_type, v);
1145 }
1146
1147 /* Convert the expression to the type of the object if need be. */
1148 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1149 gnu_expr = convert (gnu_type, gnu_expr);
1150
1151 /* If this is a pointer that doesn't have an initializing expression,
1152 initialize it to NULL, unless the object is declared imported as
1153 per RM B.1(24). */
1154 if (definition
1155 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1156 && !gnu_expr
1157 && !Is_Imported (gnat_entity))
1158 gnu_expr = integer_zero_node;
1159
1160 /* If we are defining the object and it has an Address clause, we must
1161 either get the address expression from the saved GCC tree for the
1162 object if it has a Freeze node, or elaborate the address expression
1163 here since the front-end has guaranteed that the elaboration has no
1164 effects in this case. */
1165 if (definition && Present (Address_Clause (gnat_entity)))
1166 {
1167 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1168 Node_Id gnat_address = Expression (gnat_clause);
1169 tree gnu_address
1170 = present_gnu_tree (gnat_entity)
1171 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_address);
1172
1173 save_gnu_tree (gnat_entity, NULL_TREE, false);
1174
1175 /* Convert the type of the object to a reference type that can
1176 alias everything as per RM 13.3(19). */
1177 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1178 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1179 gnu_type
1180 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1181 gnu_address = convert (gnu_type, gnu_address);
1182 used_by_ref = true;
1183 const_flag
1184 = (!Is_Public (gnat_entity)
1185 || compile_time_known_address_p (gnat_address));
1186 volatile_flag = false;
1187 gnu_size = NULL_TREE;
1188
1189 /* If this is an aliased object with an unconstrained array nominal
1190 subtype, then it can overlay only another aliased object with an
1191 unconstrained array nominal subtype and compatible template. */
1192 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1193 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1194 && !type_annotate_only)
1195 {
1196 tree rec_type = TREE_TYPE (gnu_type);
1197 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1198
1199 /* This is the pattern built for a regular object. */
1200 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1201 && TREE_OPERAND (gnu_address, 1) == off)
1202 gnu_address = TREE_OPERAND (gnu_address, 0);
1203 /* This is the pattern built for an overaligned object. */
1204 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1205 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1206 == PLUS_EXPR
1207 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1208 == off)
1209 gnu_address
1210 = build2 (POINTER_PLUS_EXPR, gnu_type,
1211 TREE_OPERAND (gnu_address, 0),
1212 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1213 else
1214 {
1215 post_error_ne ("aliased object& with unconstrained array "
1216 "nominal subtype", gnat_clause,
1217 gnat_entity);
1218 post_error ("\\can overlay only aliased object with "
1219 "compatible subtype", gnat_clause);
1220 }
1221 }
1222
1223 /* If we don't have an initializing expression for the underlying
1224 variable, the initializing expression for the pointer is the
1225 specified address. Otherwise, we have to make a COMPOUND_EXPR
1226 to assign both the address and the initial value. */
1227 if (!gnu_expr)
1228 gnu_expr = gnu_address;
1229 else
1230 gnu_expr
1231 = build2 (COMPOUND_EXPR, gnu_type,
1232 build_binary_op (INIT_EXPR, NULL_TREE,
1233 build_unary_op (INDIRECT_REF,
1234 NULL_TREE,
1235 gnu_address),
1236 gnu_expr),
1237 gnu_address);
1238 }
1239
1240 /* If it has an address clause and we are not defining it, mark it
1241 as an indirect object. Likewise for Stdcall objects that are
1242 imported. */
1243 if ((!definition && Present (Address_Clause (gnat_entity)))
1244 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1245 {
1246 /* Convert the type of the object to a reference type that can
1247 alias everything as per RM 13.3(19). */
1248 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1249 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1250 gnu_type
1251 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1252 used_by_ref = true;
1253 const_flag = false;
1254 volatile_flag = false;
1255 gnu_size = NULL_TREE;
1256
1257 /* No point in taking the address of an initializing expression
1258 that isn't going to be used. */
1259 gnu_expr = NULL_TREE;
1260
1261 /* If it has an address clause whose value is known at compile
1262 time, make the object a CONST_DECL. This will avoid a
1263 useless dereference. */
1264 if (Present (Address_Clause (gnat_entity)))
1265 {
1266 Node_Id gnat_address
1267 = Expression (Address_Clause (gnat_entity));
1268
1269 if (compile_time_known_address_p (gnat_address))
1270 {
1271 gnu_expr = gnat_to_gnu (gnat_address);
1272 const_flag = true;
1273 }
1274 }
1275 }
1276
1277 /* If we are at top level and this object is of variable size,
1278 make the actual type a hidden pointer to the real type and
1279 make the initializer be a memory allocation and initialization.
1280 Likewise for objects we aren't defining (presumed to be
1281 external references from other packages), but there we do
1282 not set up an initialization.
1283
1284 If the object's size overflows, make an allocator too, so that
1285 Storage_Error gets raised. Note that we will never free
1286 such memory, so we presume it never will get allocated. */
1287 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1288 global_bindings_p ()
1289 || !definition
1290 || static_flag)
1291 || (gnu_size
1292 && !allocatable_size_p (convert (sizetype,
1293 size_binop
1294 (CEIL_DIV_EXPR, gnu_size,
1295 bitsize_unit_node)),
1296 global_bindings_p ()
1297 || !definition
1298 || static_flag)))
1299 {
1300 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1301 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1302 gnu_type = build_reference_type (gnu_type);
1303 used_by_ref = true;
1304 const_flag = true;
1305 volatile_flag = false;
1306 gnu_size = NULL_TREE;
1307
1308 /* In case this was a aliased object whose nominal subtype is
1309 unconstrained, the pointer above will be a thin pointer and
1310 build_allocator will automatically make the template.
1311
1312 If we have a template initializer only (that we made above),
1313 pretend there is none and rely on what build_allocator creates
1314 again anyway. Otherwise (if we have a full initializer), get
1315 the data part and feed that to build_allocator.
1316
1317 If we are elaborating a mutable object, tell build_allocator to
1318 ignore a possibly simpler size from the initializer, if any, as
1319 we must allocate the maximum possible size in this case. */
1320 if (definition && !imported_p)
1321 {
1322 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1323
1324 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1325 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1326 {
1327 gnu_alloc_type
1328 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1329
1330 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1331 && vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)) == 1)
1332 gnu_expr = NULL_TREE;
1333 else
1334 gnu_expr
1335 = build_component_ref
1336 (gnu_expr,
1337 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1338 false);
1339 }
1340
1341 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1342 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1343 post_error ("?`Storage_Error` will be raised at run time!",
1344 gnat_entity);
1345
1346 gnu_expr
1347 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1348 Empty, Empty, gnat_entity, mutable_p);
1349 }
1350 else
1351 gnu_expr = NULL_TREE;
1352 }
1353
1354 /* If this object would go into the stack and has an alignment larger
1355 than the largest stack alignment the back-end can honor, resort to
1356 a variable of "aligning type". */
1357 if (definition
1358 && !global_bindings_p ()
1359 && !static_flag
1360 && !imported_p
1361 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1362 {
1363 /* Create the new variable. No need for extra room before the
1364 aligned field as this is in automatic storage. */
1365 tree gnu_new_type
1366 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1367 TYPE_SIZE_UNIT (gnu_type),
1368 BIGGEST_ALIGNMENT, 0, gnat_entity);
1369 tree gnu_new_var
1370 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1371 NULL_TREE, gnu_new_type, NULL_TREE,
1372 false, false, false, false, false,
1373 true, debug_info_p, NULL, gnat_entity);
1374
1375 /* Initialize the aligned field if we have an initializer. */
1376 if (gnu_expr)
1377 add_stmt_with_node
1378 (build_binary_op (INIT_EXPR, NULL_TREE,
1379 build_component_ref
1380 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1381 false),
1382 gnu_expr),
1383 gnat_entity);
1384
1385 /* And setup this entity as a reference to the aligned field. */
1386 gnu_type = build_reference_type (gnu_type);
1387 gnu_expr
1388 = build_unary_op
1389 (ADDR_EXPR, NULL_TREE,
1390 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1391 false));
1392 TREE_CONSTANT (gnu_expr) = 1;
1393
1394 used_by_ref = true;
1395 const_flag = true;
1396 volatile_flag = false;
1397 gnu_size = NULL_TREE;
1398 }
1399
1400 /* If this is an aliased object with an unconstrained array nominal
1401 subtype, we make its type a thin reference, i.e. the reference
1402 counterpart of a thin pointer, so it points to the array part.
1403 This is aimed to make it easier for the debugger to decode the
1404 object. Note that we have to do it this late because of the
1405 couple of allocation adjustments that might be made above. */
1406 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1407 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1408 && !type_annotate_only)
1409 {
1410 /* In case the object with the template has already been allocated
1411 just above, we have nothing to do here. */
1412 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1413 {
1414 /* This variable is a GNAT encoding used by Workbench: let it
1415 go through the debugging information but mark it as
1416 artificial: users are not interested in it. */
1417 tree gnu_unc_var
1418 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1419 NULL_TREE, gnu_type, gnu_expr,
1420 const_flag, Is_Public (gnat_entity),
1421 imported_p || !definition, static_flag,
1422 volatile_flag, true, debug_info_p,
1423 NULL, gnat_entity);
1424 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1425 TREE_CONSTANT (gnu_expr) = 1;
1426
1427 used_by_ref = true;
1428 const_flag = true;
1429 volatile_flag = false;
1430 inner_const_flag = TREE_READONLY (gnu_unc_var);
1431 gnu_size = NULL_TREE;
1432 }
1433
1434 tree gnu_array
1435 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1436 gnu_type
1437 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1438 }
1439
1440 /* Convert the expression to the type of the object if need be. */
1441 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1442 gnu_expr = convert (gnu_type, gnu_expr);
1443
1444 /* If this name is external or a name was specified, use it, but don't
1445 use the Interface_Name with an address clause (see cd30005). */
1446 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1447 || (Present (Interface_Name (gnat_entity))
1448 && No (Address_Clause (gnat_entity))))
1449 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1450
1451 /* If this is an aggregate constant initialized to a constant, force it
1452 to be statically allocated. This saves an initialization copy. */
1453 if (!static_flag
1454 && const_flag
1455 && gnu_expr && TREE_CONSTANT (gnu_expr)
1456 && AGGREGATE_TYPE_P (gnu_type)
1457 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1458 && !(TYPE_IS_PADDING_P (gnu_type)
1459 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1460 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1461 static_flag = true;
1462
1463 /* Deal with a pragma Linker_Section on a constant or variable. */
1464 if ((kind == E_Constant || kind == E_Variable)
1465 && Present (Linker_Section_Pragma (gnat_entity)))
1466 prepend_one_attribute_pragma (&attr_list,
1467 Linker_Section_Pragma (gnat_entity));
1468
1469 /* Now create the variable or the constant and set various flags. */
1470 gnu_decl
1471 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1472 gnu_expr, const_flag, Is_Public (gnat_entity),
1473 imported_p || !definition, static_flag,
1474 volatile_flag, artificial_p, debug_info_p,
1475 attr_list, gnat_entity, !renamed_obj);
1476 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1477 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1478 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1479
1480 /* If we are defining an Out parameter and optimization isn't enabled,
1481 create a fake PARM_DECL for debugging purposes and make it point to
1482 the VAR_DECL. Suppress debug info for the latter but make sure it
1483 will live in memory so that it can be accessed from within the
1484 debugger through the PARM_DECL. */
1485 if (kind == E_Out_Parameter
1486 && definition
1487 && debug_info_p
1488 && !optimize
1489 && !flag_generate_lto)
1490 {
1491 tree param = create_param_decl (gnu_entity_name, gnu_type);
1492 gnat_pushdecl (param, gnat_entity);
1493 SET_DECL_VALUE_EXPR (param, gnu_decl);
1494 DECL_HAS_VALUE_EXPR_P (param) = 1;
1495 DECL_IGNORED_P (gnu_decl) = 1;
1496 TREE_ADDRESSABLE (gnu_decl) = 1;
1497 }
1498
1499 /* If this is a loop parameter, set the corresponding flag. */
1500 else if (kind == E_Loop_Parameter)
1501 DECL_LOOP_PARM_P (gnu_decl) = 1;
1502
1503 /* If this is a renaming pointer, attach the renamed object to it. */
1504 if (renamed_obj)
1505 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1506
1507 /* If this is a constant and we are defining it or it generates a real
1508 symbol at the object level and we are referencing it, we may want
1509 or need to have a true variable to represent it:
1510 - if optimization isn't enabled, for debugging purposes,
1511 - if the constant is public and not overlaid on something else,
1512 - if its address is taken,
1513 - if either itself or its type is aliased. */
1514 if (TREE_CODE (gnu_decl) == CONST_DECL
1515 && (definition || Sloc (gnat_entity) > Standard_Location)
1516 && ((!optimize && debug_info_p)
1517 || (Is_Public (gnat_entity)
1518 && No (Address_Clause (gnat_entity)))
1519 || Address_Taken (gnat_entity)
1520 || Is_Aliased (gnat_entity)
1521 || Is_Aliased (Etype (gnat_entity))))
1522 {
1523 tree gnu_corr_var
1524 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1525 gnu_expr, true, Is_Public (gnat_entity),
1526 !definition, static_flag, volatile_flag,
1527 artificial_p, debug_info_p, attr_list,
1528 gnat_entity, false);
1529
1530 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1531 }
1532
1533 /* If this is a constant, even if we don't need a true variable, we
1534 may need to avoid returning the initializer in every case. That
1535 can happen for the address of a (constant) constructor because,
1536 upon dereferencing it, the constructor will be reinjected in the
1537 tree, which may not be valid in every case; see lvalue_required_p
1538 for more details. */
1539 if (TREE_CODE (gnu_decl) == CONST_DECL)
1540 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1541
1542 /* If this object is declared in a block that contains a block with an
1543 exception handler, and we aren't using the GCC exception mechanism,
1544 we must force this variable in memory in order to avoid an invalid
1545 optimization. */
1546 if (Front_End_Exceptions ()
1547 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1548 TREE_ADDRESSABLE (gnu_decl) = 1;
1549
1550 /* If this is a local variable with non-BLKmode and aggregate type,
1551 and optimization isn't enabled, then force it in memory so that
1552 a register won't be allocated to it with possible subparts left
1553 uninitialized and reaching the register allocator. */
1554 else if (TREE_CODE (gnu_decl) == VAR_DECL
1555 && !DECL_EXTERNAL (gnu_decl)
1556 && !TREE_STATIC (gnu_decl)
1557 && DECL_MODE (gnu_decl) != BLKmode
1558 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1559 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1560 && !optimize)
1561 TREE_ADDRESSABLE (gnu_decl) = 1;
1562
1563 /* If we are defining an object with variable size or an object with
1564 fixed size that will be dynamically allocated, and we are using the
1565 front-end setjmp/longjmp exception mechanism, update the setjmp
1566 buffer. */
1567 if (definition
1568 && Exception_Mechanism == Front_End_SJLJ
1569 && get_block_jmpbuf_decl ()
1570 && DECL_SIZE_UNIT (gnu_decl)
1571 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1572 || (flag_stack_check == GENERIC_STACK_CHECK
1573 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1574 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1575 add_stmt_with_node (build_call_n_expr
1576 (update_setjmp_buf_decl, 1,
1577 build_unary_op (ADDR_EXPR, NULL_TREE,
1578 get_block_jmpbuf_decl ())),
1579 gnat_entity);
1580
1581 /* Back-annotate Esize and Alignment of the object if not already
1582 known. Note that we pick the values of the type, not those of
1583 the object, to shield ourselves from low-level platform-dependent
1584 adjustments like alignment promotion. This is both consistent with
1585 all the treatment above, where alignment and size are set on the
1586 type of the object and not on the object directly, and makes it
1587 possible to support all confirming representation clauses. */
1588 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1589 used_by_ref);
1590 }
1591 break;
1592
1593 case E_Void:
1594 /* Return a TYPE_DECL for "void" that we previously made. */
1595 gnu_decl = TYPE_NAME (void_type_node);
1596 break;
1597
1598 case E_Enumeration_Type:
1599 /* A special case: for the types Character and Wide_Character in
1600 Standard, we do not list all the literals. So if the literals
1601 are not specified, make this an integer type. */
1602 if (No (First_Literal (gnat_entity)))
1603 {
1604 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1605 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1606 else
1607 gnu_type = make_unsigned_type (esize);
1608 TYPE_NAME (gnu_type) = gnu_entity_name;
1609
1610 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1611 This is needed by the DWARF-2 back-end to distinguish between
1612 unsigned integer types and character types. */
1613 TYPE_STRING_FLAG (gnu_type) = 1;
1614
1615 /* This flag is needed by the call just below. */
1616 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1617
1618 finish_character_type (gnu_type);
1619 }
1620 else
1621 {
1622 /* We have a list of enumeral constants in First_Literal. We make a
1623 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1624 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1625 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1626 value of the literal. But when we have a regular boolean type, we
1627 simplify this a little by using a BOOLEAN_TYPE. */
1628 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1629 && !Has_Non_Standard_Rep (gnat_entity);
1630 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1631 tree gnu_list = NULL_TREE;
1632 Entity_Id gnat_literal;
1633
1634 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1635 TYPE_PRECISION (gnu_type) = esize;
1636 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1637 set_min_and_max_values_for_integral_type (gnu_type, esize,
1638 TYPE_SIGN (gnu_type));
1639 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1640 layout_type (gnu_type);
1641
1642 for (gnat_literal = First_Literal (gnat_entity);
1643 Present (gnat_literal);
1644 gnat_literal = Next_Literal (gnat_literal))
1645 {
1646 tree gnu_value
1647 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1648 /* Do not generate debug info for individual enumerators. */
1649 tree gnu_literal
1650 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1651 gnu_type, gnu_value, true, false, false,
1652 false, false, artificial_p, false,
1653 NULL, gnat_literal);
1654 save_gnu_tree (gnat_literal, gnu_literal, false);
1655 gnu_list
1656 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1657 }
1658
1659 if (!is_boolean)
1660 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1661
1662 /* Note that the bounds are updated at the end of this function
1663 to avoid an infinite recursion since they refer to the type. */
1664 goto discrete_type;
1665 }
1666 break;
1667
1668 case E_Signed_Integer_Type:
1669 /* For integer types, just make a signed type the appropriate number
1670 of bits. */
1671 gnu_type = make_signed_type (esize);
1672 goto discrete_type;
1673
1674 case E_Ordinary_Fixed_Point_Type:
1675 case E_Decimal_Fixed_Point_Type:
1676 {
1677 /* Small_Value is the scale factor. */
1678 const Ureal gnat_small_value = Small_Value (gnat_entity);
1679 tree scale_factor = NULL_TREE;
1680
1681 gnu_type = make_signed_type (esize);
1682
1683 /* Try to decode the scale factor and to save it for the fixed-point
1684 types debug hook. */
1685
1686 /* There are various ways to describe the scale factor, however there
1687 are cases where back-end internals cannot hold it. In such cases,
1688 we output invalid scale factor for such cases (i.e. the 0/0
1689 rational constant) but we expect GNAT to output GNAT encodings,
1690 then. Thus, keep this in sync with
1691 Exp_Dbug.Is_Handled_Scale_Factor. */
1692
1693 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1694 binary or decimal scale: it is easier to read for humans. */
1695 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1696 && (Rbase (gnat_small_value) == 2
1697 || Rbase (gnat_small_value) == 10))
1698 {
1699 /* Given RM restrictions on 'Small values, we assume here that
1700 the denominator fits in an int. */
1701 const tree base = build_int_cst (integer_type_node,
1702 Rbase (gnat_small_value));
1703 const tree exponent
1704 = build_int_cst (integer_type_node,
1705 UI_To_Int (Denominator (gnat_small_value)));
1706 scale_factor
1707 = build2 (RDIV_EXPR, integer_type_node,
1708 integer_one_node,
1709 build2 (POWER_EXPR, integer_type_node,
1710 base, exponent));
1711 }
1712
1713 /* Default to arbitrary scale factors descriptions. */
1714 else
1715 {
1716 const Uint num = Norm_Num (gnat_small_value);
1717 const Uint den = Norm_Den (gnat_small_value);
1718
1719 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1720 {
1721 const tree gnu_num
1722 = build_int_cst (integer_type_node,
1723 UI_To_Int (Norm_Num (gnat_small_value)));
1724 const tree gnu_den
1725 = build_int_cst (integer_type_node,
1726 UI_To_Int (Norm_Den (gnat_small_value)));
1727 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1728 gnu_num, gnu_den);
1729 }
1730 else
1731 /* If compiler internals cannot represent arbitrary scale
1732 factors, output an invalid scale factor so that debugger
1733 don't try to handle them but so that we still have a type
1734 in the output. Note that GNAT */
1735 scale_factor = integer_zero_node;
1736 }
1737
1738 TYPE_FIXED_POINT_P (gnu_type) = 1;
1739 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1740 }
1741 goto discrete_type;
1742
1743 case E_Modular_Integer_Type:
1744 {
1745 /* For modular types, make the unsigned type of the proper number
1746 of bits and then set up the modulus, if required. */
1747 tree gnu_modulus, gnu_high = NULL_TREE;
1748
1749 /* Packed Array Impl. Types are supposed to be subtypes only. */
1750 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1751
1752 gnu_type = make_unsigned_type (esize);
1753
1754 /* Get the modulus in this type. If it overflows, assume it is because
1755 it is equal to 2**Esize. Note that there is no overflow checking
1756 done on unsigned type, so we detect the overflow by looking for
1757 a modulus of zero, which is otherwise invalid. */
1758 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1759
1760 if (!integer_zerop (gnu_modulus))
1761 {
1762 TYPE_MODULAR_P (gnu_type) = 1;
1763 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1764 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1765 build_int_cst (gnu_type, 1));
1766 }
1767
1768 /* If the upper bound is not maximal, make an extra subtype. */
1769 if (gnu_high
1770 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1771 {
1772 tree gnu_subtype = make_unsigned_type (esize);
1773 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1774 TREE_TYPE (gnu_subtype) = gnu_type;
1775 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1776 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1777 gnu_type = gnu_subtype;
1778 }
1779 }
1780 goto discrete_type;
1781
1782 case E_Signed_Integer_Subtype:
1783 case E_Enumeration_Subtype:
1784 case E_Modular_Integer_Subtype:
1785 case E_Ordinary_Fixed_Point_Subtype:
1786 case E_Decimal_Fixed_Point_Subtype:
1787
1788 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1789 not want to call create_range_type since we would like each subtype
1790 node to be distinct. ??? Historically this was in preparation for
1791 when memory aliasing is implemented, but that's obsolete now given
1792 the call to relate_alias_sets below.
1793
1794 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1795 this fact is used by the arithmetic conversion functions.
1796
1797 We elaborate the Ancestor_Subtype if it is not in the current unit
1798 and one of our bounds is non-static. We do this to ensure consistent
1799 naming in the case where several subtypes share the same bounds, by
1800 elaborating the first such subtype first, thus using its name. */
1801
1802 if (!definition
1803 && Present (Ancestor_Subtype (gnat_entity))
1804 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1805 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1806 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1807 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
1808
1809 /* Set the precision to the Esize except for bit-packed arrays. */
1810 if (Is_Packed_Array_Impl_Type (gnat_entity)
1811 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1812 esize = UI_To_Int (RM_Size (gnat_entity));
1813
1814 /* First subtypes of Character are treated as Character; otherwise
1815 this should be an unsigned type if the base type is unsigned or
1816 if the lower bound is constant and non-negative or if the type
1817 is biased. However, even if the lower bound is constant and
1818 non-negative, we use a signed type for a subtype with the same
1819 size as its signed base type, because this eliminates useless
1820 conversions to it and gives more leeway to the optimizer; but
1821 this means that we will need to explicitly test for this case
1822 when we change the representation based on the RM size. */
1823 if (kind == E_Enumeration_Subtype
1824 && No (First_Literal (Etype (gnat_entity)))
1825 && Esize (gnat_entity) == RM_Size (gnat_entity)
1826 && esize == CHAR_TYPE_SIZE
1827 && flag_signed_char)
1828 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1829 else if (Is_Unsigned_Type (Etype (gnat_entity))
1830 || (Esize (Etype (gnat_entity)) != Esize (gnat_entity)
1831 && Is_Unsigned_Type (gnat_entity))
1832 || Has_Biased_Representation (gnat_entity))
1833 gnu_type = make_unsigned_type (esize);
1834 else
1835 gnu_type = make_signed_type (esize);
1836 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1837
1838 SET_TYPE_RM_MIN_VALUE
1839 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1840 gnat_entity, "L", definition, true,
1841 debug_info_p));
1842
1843 SET_TYPE_RM_MAX_VALUE
1844 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1845 gnat_entity, "U", definition, true,
1846 debug_info_p));
1847
1848 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1849 = Has_Biased_Representation (gnat_entity);
1850
1851 /* Set TYPE_STRING_FLAG for Character and Wide_Character subtypes. */
1852 TYPE_STRING_FLAG (gnu_type) = TYPE_STRING_FLAG (TREE_TYPE (gnu_type));
1853
1854 /* Inherit our alias set from what we're a subtype of. Subtypes
1855 are not different types and a pointer can designate any instance
1856 within a subtype hierarchy. */
1857 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1858
1859 /* One of the above calls might have caused us to be elaborated,
1860 so don't blow up if so. */
1861 if (present_gnu_tree (gnat_entity))
1862 {
1863 maybe_present = true;
1864 break;
1865 }
1866
1867 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1868 TYPE_STUB_DECL (gnu_type)
1869 = create_type_stub_decl (gnu_entity_name, gnu_type);
1870
1871 /* For a packed array, make the original array type a parallel/debug
1872 type. */
1873 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1874 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1875
1876 discrete_type:
1877
1878 /* We have to handle clauses that under-align the type specially. */
1879 if ((Present (Alignment_Clause (gnat_entity))
1880 || (Is_Packed_Array_Impl_Type (gnat_entity)
1881 && Present
1882 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1883 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1884 {
1885 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1886 if (align >= TYPE_ALIGN (gnu_type))
1887 align = 0;
1888 }
1889
1890 /* If the type we are dealing with represents a bit-packed array,
1891 we need to have the bits left justified on big-endian targets
1892 and right justified on little-endian targets. We also need to
1893 ensure that when the value is read (e.g. for comparison of two
1894 such values), we only get the good bits, since the unused bits
1895 are uninitialized. Both goals are accomplished by wrapping up
1896 the modular type in an enclosing record type. */
1897 if (Is_Packed_Array_Impl_Type (gnat_entity)
1898 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1899 {
1900 tree gnu_field_type, gnu_field;
1901
1902 /* Set the RM size before wrapping up the original type. */
1903 SET_TYPE_RM_SIZE (gnu_type,
1904 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1905 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1906
1907 /* Strip the ___XP suffix for standard DWARF. */
1908 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1909 gnu_entity_name = TYPE_NAME (gnu_type);
1910
1911 /* Create a stripped-down declaration, mainly for debugging. */
1912 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1913 gnat_entity);
1914
1915 /* Now save it and build the enclosing record type. */
1916 gnu_field_type = gnu_type;
1917
1918 gnu_type = make_node (RECORD_TYPE);
1919 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1920 TYPE_PACKED (gnu_type) = 1;
1921 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1922 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1923 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1924
1925 /* Propagate the alignment of the modular type to the record type,
1926 unless there is an alignment clause that under-aligns the type.
1927 This means that bit-packed arrays are given "ceil" alignment for
1928 their size by default, which may seem counter-intuitive but makes
1929 it possible to overlay them on modular types easily. */
1930 SET_TYPE_ALIGN (gnu_type,
1931 align > 0 ? align : TYPE_ALIGN (gnu_field_type));
1932
1933 /* Propagate the reverse storage order flag to the record type so
1934 that the required byte swapping is performed when retrieving the
1935 enclosed modular value. */
1936 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1937 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1938
1939 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1940
1941 /* Don't declare the field as addressable since we won't be taking
1942 its address and this would prevent create_field_decl from making
1943 a bitfield. */
1944 gnu_field
1945 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1946 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1947
1948 /* We will output additional debug info manually below. */
1949 finish_record_type (gnu_type, gnu_field, 2, false);
1950 compute_record_mode (gnu_type);
1951 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1952
1953 if (debug_info_p)
1954 {
1955 /* Make the original array type a parallel/debug type. */
1956 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1957
1958 /* Since GNU_TYPE is a padding type around the packed array
1959 implementation type, the padded type is its debug type. */
1960 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1961 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1962 }
1963 }
1964
1965 /* If the type we are dealing with has got a smaller alignment than the
1966 natural one, we need to wrap it up in a record type and misalign the
1967 latter; we reuse the padding machinery for this purpose. */
1968 else if (align > 0)
1969 {
1970 tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1971
1972 /* Set the RM size before wrapping the type. */
1973 SET_TYPE_RM_SIZE (gnu_type, gnu_size);
1974
1975 gnu_type
1976 = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align,
1977 gnat_entity, false, true, definition, false);
1978
1979 TYPE_PACKED (gnu_type) = 1;
1980 SET_TYPE_ADA_SIZE (gnu_type, gnu_size);
1981 }
1982
1983 break;
1984
1985 case E_Floating_Point_Type:
1986 /* The type of the Low and High bounds can be our type if this is
1987 a type from Standard, so set them at the end of the function. */
1988 gnu_type = make_node (REAL_TYPE);
1989 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1990 layout_type (gnu_type);
1991 break;
1992
1993 case E_Floating_Point_Subtype:
1994 /* See the E_Signed_Integer_Subtype case for the rationale. */
1995 if (!definition
1996 && Present (Ancestor_Subtype (gnat_entity))
1997 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1998 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1999 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
2000 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false);
2001
2002 gnu_type = make_node (REAL_TYPE);
2003 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
2004 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
2005 TYPE_GCC_MIN_VALUE (gnu_type)
2006 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
2007 TYPE_GCC_MAX_VALUE (gnu_type)
2008 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
2009 layout_type (gnu_type);
2010
2011 SET_TYPE_RM_MIN_VALUE
2012 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2013 gnat_entity, "L", definition, true,
2014 debug_info_p));
2015
2016 SET_TYPE_RM_MAX_VALUE
2017 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2018 gnat_entity, "U", definition, true,
2019 debug_info_p));
2020
2021 /* Inherit our alias set from what we're a subtype of, as for
2022 integer subtypes. */
2023 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2024
2025 /* One of the above calls might have caused us to be elaborated,
2026 so don't blow up if so. */
2027 maybe_present = true;
2028 break;
2029
2030 /* Array Types and Subtypes
2031
2032 Unconstrained array types are represented by E_Array_Type and
2033 constrained array types are represented by E_Array_Subtype. There
2034 are no actual objects of an unconstrained array type; all we have
2035 are pointers to that type.
2036
2037 The following fields are defined on array types and subtypes:
2038
2039 Component_Type Component type of the array.
2040 Number_Dimensions Number of dimensions (an int).
2041 First_Index Type of first index. */
2042
2043 case E_Array_Type:
2044 {
2045 const bool convention_fortran_p
2046 = (Convention (gnat_entity) == Convention_Fortran);
2047 const int ndim = Number_Dimensions (gnat_entity);
2048 tree gnu_template_type;
2049 tree gnu_ptr_template;
2050 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2051 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2052 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2053 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2054 Entity_Id gnat_index, gnat_name;
2055 int index;
2056 tree comp_type;
2057
2058 /* Create the type for the component now, as it simplifies breaking
2059 type reference loops. */
2060 comp_type
2061 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2062 if (present_gnu_tree (gnat_entity))
2063 {
2064 /* As a side effect, the type may have been translated. */
2065 maybe_present = true;
2066 break;
2067 }
2068
2069 /* We complete an existing dummy fat pointer type in place. This both
2070 avoids further complex adjustments in update_pointer_to and yields
2071 better debugging information in DWARF by leveraging the support for
2072 incomplete declarations of "tagged" types in the DWARF back-end. */
2073 gnu_type = get_dummy_type (gnat_entity);
2074 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2075 {
2076 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2077 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2078 /* Save the contents of the dummy type for update_pointer_to. */
2079 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2080 gnu_ptr_template =
2081 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2082 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2083 }
2084 else
2085 {
2086 gnu_fat_type = make_node (RECORD_TYPE);
2087 gnu_template_type = make_node (RECORD_TYPE);
2088 gnu_ptr_template = build_pointer_type (gnu_template_type);
2089 }
2090
2091 /* Make a node for the array. If we are not defining the array
2092 suppress expanding incomplete types. */
2093 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2094
2095 if (!definition)
2096 {
2097 defer_incomplete_level++;
2098 this_deferred = true;
2099 }
2100
2101 /* Build the fat pointer type. Use a "void *" object instead of
2102 a pointer to the array type since we don't have the array type
2103 yet (it will reference the fat pointer via the bounds). */
2104 tem
2105 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2106 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2107 DECL_CHAIN (tem)
2108 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2109 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2110
2111 if (COMPLETE_TYPE_P (gnu_fat_type))
2112 {
2113 /* We are going to lay it out again so reset the alias set. */
2114 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2115 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2116 finish_fat_pointer_type (gnu_fat_type, tem);
2117 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2118 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2119 {
2120 TYPE_FIELDS (t) = tem;
2121 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2122 }
2123 }
2124 else
2125 {
2126 finish_fat_pointer_type (gnu_fat_type, tem);
2127 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2128 }
2129
2130 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2131 is the fat pointer. This will be used to access the individual
2132 fields once we build them. */
2133 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2134 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2135 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2136 gnu_template_reference
2137 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2138 TREE_READONLY (gnu_template_reference) = 1;
2139 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2140
2141 /* Now create the GCC type for each index and add the fields for that
2142 index to the template. */
2143 for (index = (convention_fortran_p ? ndim - 1 : 0),
2144 gnat_index = First_Index (gnat_entity);
2145 0 <= index && index < ndim;
2146 index += (convention_fortran_p ? - 1 : 1),
2147 gnat_index = Next_Index (gnat_index))
2148 {
2149 char field_name[16];
2150 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2151 tree gnu_index_base_type
2152 = maybe_character_type (get_base_type (gnu_index_type));
2153 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2154 tree gnu_min, gnu_max, gnu_high;
2155
2156 /* Make the FIELD_DECLs for the low and high bounds of this
2157 type and then make extractions of these fields from the
2158 template. */
2159 sprintf (field_name, "LB%d", index);
2160 gnu_lb_field = create_field_decl (get_identifier (field_name),
2161 gnu_index_base_type,
2162 gnu_template_type, NULL_TREE,
2163 NULL_TREE, 0, 0);
2164 Sloc_to_locus (Sloc (gnat_entity),
2165 &DECL_SOURCE_LOCATION (gnu_lb_field));
2166
2167 field_name[0] = 'U';
2168 gnu_hb_field = create_field_decl (get_identifier (field_name),
2169 gnu_index_base_type,
2170 gnu_template_type, NULL_TREE,
2171 NULL_TREE, 0, 0);
2172 Sloc_to_locus (Sloc (gnat_entity),
2173 &DECL_SOURCE_LOCATION (gnu_hb_field));
2174
2175 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2176
2177 /* We can't use build_component_ref here since the template type
2178 isn't complete yet. */
2179 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2180 gnu_template_reference, gnu_lb_field,
2181 NULL_TREE);
2182 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2183 gnu_template_reference, gnu_hb_field,
2184 NULL_TREE);
2185 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2186
2187 gnu_min = convert (sizetype, gnu_orig_min);
2188 gnu_max = convert (sizetype, gnu_orig_max);
2189
2190 /* Compute the size of this dimension. See the E_Array_Subtype
2191 case below for the rationale. */
2192 gnu_high
2193 = build3 (COND_EXPR, sizetype,
2194 build2 (GE_EXPR, boolean_type_node,
2195 gnu_orig_max, gnu_orig_min),
2196 gnu_max,
2197 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2198
2199 /* Make a range type with the new range in the Ada base type.
2200 Then make an index type with the size range in sizetype. */
2201 gnu_index_types[index]
2202 = create_index_type (gnu_min, gnu_high,
2203 create_range_type (gnu_index_base_type,
2204 gnu_orig_min,
2205 gnu_orig_max),
2206 gnat_entity);
2207
2208 /* Update the maximum size of the array in elements. */
2209 if (gnu_max_size)
2210 {
2211 tree gnu_min
2212 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2213 tree gnu_max
2214 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2215 tree gnu_this_max
2216 = size_binop (PLUS_EXPR, size_one_node,
2217 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2218
2219 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2220 && TREE_OVERFLOW (gnu_this_max))
2221 gnu_max_size = NULL_TREE;
2222 else
2223 gnu_max_size
2224 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2225 }
2226
2227 TYPE_NAME (gnu_index_types[index])
2228 = create_concat_name (gnat_entity, field_name);
2229 }
2230
2231 /* Install all the fields into the template. */
2232 TYPE_NAME (gnu_template_type)
2233 = create_concat_name (gnat_entity, "XUB");
2234 gnu_template_fields = NULL_TREE;
2235 for (index = 0; index < ndim; index++)
2236 gnu_template_fields
2237 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2238 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2239 debug_info_p);
2240 TYPE_READONLY (gnu_template_type) = 1;
2241
2242 /* If Component_Size is not already specified, annotate it with the
2243 size of the component. */
2244 if (Unknown_Component_Size (gnat_entity))
2245 Set_Component_Size (gnat_entity,
2246 annotate_value (TYPE_SIZE (comp_type)));
2247
2248 /* Compute the maximum size of the array in units and bits. */
2249 if (gnu_max_size)
2250 {
2251 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2252 TYPE_SIZE_UNIT (comp_type));
2253 gnu_max_size = size_binop (MULT_EXPR,
2254 convert (bitsizetype, gnu_max_size),
2255 TYPE_SIZE (comp_type));
2256 }
2257 else
2258 gnu_max_size_unit = NULL_TREE;
2259
2260 /* Now build the array type. */
2261 tem = comp_type;
2262 for (index = ndim - 1; index >= 0; index--)
2263 {
2264 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2265 if (index == ndim - 1)
2266 TYPE_REVERSE_STORAGE_ORDER (tem)
2267 = Reverse_Storage_Order (gnat_entity);
2268 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2269 if (array_type_has_nonaliased_component (tem, gnat_entity))
2270 TYPE_NONALIASED_COMPONENT (tem) = 1;
2271 }
2272
2273 /* If an alignment is specified, use it if valid. But ignore it
2274 for the original type of packed array types. If the alignment
2275 was requested with an explicit alignment clause, state so. */
2276 if (No (Packed_Array_Impl_Type (gnat_entity))
2277 && Known_Alignment (gnat_entity))
2278 {
2279 SET_TYPE_ALIGN (tem,
2280 validate_alignment (Alignment (gnat_entity),
2281 gnat_entity,
2282 TYPE_ALIGN (tem)));
2283 if (Present (Alignment_Clause (gnat_entity)))
2284 TYPE_USER_ALIGN (tem) = 1;
2285 }
2286
2287 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2288
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));
2295
2296 if (Treat_As_Volatile (gnat_entity))
2297 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2298
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);
2304
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));
2312
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))
2319 {
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));
2324 }
2325
2326 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2327 artificial_p, debug_info_p, gnat_entity);
2328
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 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2337 gnu_entity_name = create_concat_name (gnat_name, "XUP");
2338 create_type_decl (gnu_entity_name, gnu_fat_type, artificial_p,
2339 debug_info_p, gnat_entity);
2340
2341 /* Create the type to be designated by thin pointers: a record type for
2342 the array and its template. We used to shift the fields to have the
2343 template at a negative offset, but this was somewhat of a kludge; we
2344 now shift thin pointer values explicitly but only those which have a
2345 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2346 Note that GDB can handle standard DWARF information for them, so we
2347 don't have to name them as a GNAT encoding, except if specifically
2348 asked to. */
2349 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2350 gnu_entity_name = create_concat_name (gnat_name, "XUT");
2351 else
2352 gnu_entity_name = get_entity_name (gnat_name);
2353 tem = build_unc_object_type (gnu_template_type, tem, gnu_entity_name,
2354 debug_info_p);
2355
2356 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2357 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2358 }
2359 break;
2360
2361 case E_Array_Subtype:
2362
2363 /* This is the actual data type for array variables. Multidimensional
2364 arrays are implemented as arrays of arrays. Note that arrays which
2365 have sparse enumeration subtypes as index components create sparse
2366 arrays, which is obviously space inefficient but so much easier to
2367 code for now.
2368
2369 Also note that the subtype never refers to the unconstrained array
2370 type, which is somewhat at variance with Ada semantics.
2371
2372 First check to see if this is simply a renaming of the array type.
2373 If so, the result is the array type. */
2374
2375 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2376 if (!Is_Constrained (gnat_entity))
2377 ;
2378 else
2379 {
2380 Entity_Id gnat_index, gnat_base_index;
2381 const bool convention_fortran_p
2382 = (Convention (gnat_entity) == Convention_Fortran);
2383 const int ndim = Number_Dimensions (gnat_entity);
2384 tree gnu_base_type = gnu_type;
2385 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2386 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2387 bool need_index_type_struct = false;
2388 int index;
2389
2390 /* First create the GCC type for each index and find out whether
2391 special types are needed for debugging information. */
2392 for (index = (convention_fortran_p ? ndim - 1 : 0),
2393 gnat_index = First_Index (gnat_entity),
2394 gnat_base_index
2395 = First_Index (Implementation_Base_Type (gnat_entity));
2396 0 <= index && index < ndim;
2397 index += (convention_fortran_p ? - 1 : 1),
2398 gnat_index = Next_Index (gnat_index),
2399 gnat_base_index = Next_Index (gnat_base_index))
2400 {
2401 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2402 tree gnu_index_base_type
2403 = maybe_character_type (get_base_type (gnu_index_type));
2404 tree gnu_orig_min
2405 = convert (gnu_index_base_type,
2406 TYPE_MIN_VALUE (gnu_index_type));
2407 tree gnu_orig_max
2408 = convert (gnu_index_base_type,
2409 TYPE_MAX_VALUE (gnu_index_type));
2410 tree gnu_min = convert (sizetype, gnu_orig_min);
2411 tree gnu_max = convert (sizetype, gnu_orig_max);
2412 tree gnu_base_index_type
2413 = get_unpadded_type (Etype (gnat_base_index));
2414 tree gnu_base_index_base_type
2415 = maybe_character_type (get_base_type (gnu_base_index_type));
2416 tree gnu_base_orig_min
2417 = convert (gnu_base_index_base_type,
2418 TYPE_MIN_VALUE (gnu_base_index_type));
2419 tree gnu_base_orig_max
2420 = convert (gnu_base_index_base_type,
2421 TYPE_MAX_VALUE (gnu_base_index_type));
2422 tree gnu_high;
2423
2424 /* See if the base array type is already flat. If it is, we
2425 are probably compiling an ACATS test but it will cause the
2426 code below to malfunction if we don't handle it specially. */
2427 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2428 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2429 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2430 {
2431 gnu_min = size_one_node;
2432 gnu_max = size_zero_node;
2433 gnu_high = gnu_max;
2434 }
2435
2436 /* Similarly, if one of the values overflows in sizetype and the
2437 range is null, use 1..0 for the sizetype bounds. */
2438 else if (TREE_CODE (gnu_min) == INTEGER_CST
2439 && TREE_CODE (gnu_max) == INTEGER_CST
2440 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2441 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2442 {
2443 gnu_min = size_one_node;
2444 gnu_max = size_zero_node;
2445 gnu_high = gnu_max;
2446 }
2447
2448 /* If the minimum and maximum values both overflow in sizetype,
2449 but the difference in the original type does not overflow in
2450 sizetype, ignore the overflow indication. */
2451 else if (TREE_CODE (gnu_min) == INTEGER_CST
2452 && TREE_CODE (gnu_max) == INTEGER_CST
2453 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2454 && !TREE_OVERFLOW
2455 (convert (sizetype,
2456 fold_build2 (MINUS_EXPR, gnu_index_type,
2457 gnu_orig_max,
2458 gnu_orig_min))))
2459 {
2460 TREE_OVERFLOW (gnu_min) = 0;
2461 TREE_OVERFLOW (gnu_max) = 0;
2462 gnu_high = gnu_max;
2463 }
2464
2465 /* Compute the size of this dimension in the general case. We
2466 need to provide GCC with an upper bound to use but have to
2467 deal with the "superflat" case. There are three ways to do
2468 this. If we can prove that the array can never be superflat,
2469 we can just use the high bound of the index type. */
2470 else if ((Nkind (gnat_index) == N_Range
2471 && cannot_be_superflat (gnat_index))
2472 /* Bit-Packed Array Impl. Types are never superflat. */
2473 || (Is_Packed_Array_Impl_Type (gnat_entity)
2474 && Is_Bit_Packed_Array
2475 (Original_Array_Type (gnat_entity))))
2476 gnu_high = gnu_max;
2477
2478 /* Otherwise, if the high bound is constant but the low bound is
2479 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2480 lower bound. Note that the comparison must be done in the
2481 original type to avoid any overflow during the conversion. */
2482 else if (TREE_CODE (gnu_max) == INTEGER_CST
2483 && TREE_CODE (gnu_min) != INTEGER_CST)
2484 {
2485 gnu_high = gnu_max;
2486 gnu_min
2487 = build_cond_expr (sizetype,
2488 build_binary_op (GE_EXPR,
2489 boolean_type_node,
2490 gnu_orig_max,
2491 gnu_orig_min),
2492 gnu_min,
2493 int_const_binop (PLUS_EXPR, gnu_max,
2494 size_one_node));
2495 }
2496
2497 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2498 in all the other cases. Note that, here as well as above,
2499 the condition used in the comparison must be equivalent to
2500 the condition (length != 0). This is relied upon in order
2501 to optimize array comparisons in compare_arrays. Moreover
2502 we use int_const_binop for the shift by 1 if the bound is
2503 constant to avoid any unwanted overflow. */
2504 else
2505 gnu_high
2506 = build_cond_expr (sizetype,
2507 build_binary_op (GE_EXPR,
2508 boolean_type_node,
2509 gnu_orig_max,
2510 gnu_orig_min),
2511 gnu_max,
2512 TREE_CODE (gnu_min) == INTEGER_CST
2513 ? int_const_binop (MINUS_EXPR, gnu_min,
2514 size_one_node)
2515 : size_binop (MINUS_EXPR, gnu_min,
2516 size_one_node));
2517
2518 /* Reuse the index type for the range type. Then make an index
2519 type with the size range in sizetype. */
2520 gnu_index_types[index]
2521 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2522 gnat_entity);
2523
2524 /* Update the maximum size of the array in elements. Here we
2525 see if any constraint on the index type of the base type
2526 can be used in the case of self-referential bound on the
2527 index type of the subtype. We look for a non-"infinite"
2528 and non-self-referential bound from any type involved and
2529 handle each bound separately. */
2530 if (gnu_max_size)
2531 {
2532 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2533 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2534 tree gnu_base_base_min
2535 = convert (sizetype,
2536 TYPE_MIN_VALUE (gnu_base_index_base_type));
2537 tree gnu_base_base_max
2538 = convert (sizetype,
2539 TYPE_MAX_VALUE (gnu_base_index_base_type));
2540
2541 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2542 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2543 && !TREE_OVERFLOW (gnu_base_min)))
2544 gnu_base_min = gnu_min;
2545
2546 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2547 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2548 && !TREE_OVERFLOW (gnu_base_max)))
2549 gnu_base_max = gnu_max;
2550
2551 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2552 && TREE_OVERFLOW (gnu_base_min))
2553 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2554 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2555 && TREE_OVERFLOW (gnu_base_max))
2556 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2557 gnu_max_size = NULL_TREE;
2558 else
2559 {
2560 tree gnu_this_max;
2561
2562 /* Use int_const_binop if the bounds are constant to
2563 avoid any unwanted overflow. */
2564 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2565 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2566 gnu_this_max
2567 = int_const_binop (PLUS_EXPR, size_one_node,
2568 int_const_binop (MINUS_EXPR,
2569 gnu_base_max,
2570 gnu_base_min));
2571 else
2572 gnu_this_max
2573 = size_binop (PLUS_EXPR, size_one_node,
2574 size_binop (MINUS_EXPR,
2575 gnu_base_max,
2576 gnu_base_min));
2577
2578 gnu_max_size
2579 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2580 }
2581 }
2582
2583 /* We need special types for debugging information to point to
2584 the index types if they have variable bounds, are not integer
2585 types, are biased or are wider than sizetype. These are GNAT
2586 encodings, so we have to include them only when all encodings
2587 are requested. */
2588 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2589 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2590 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2591 || (TREE_TYPE (gnu_index_type)
2592 && TREE_CODE (TREE_TYPE (gnu_index_type))
2593 != INTEGER_TYPE)
2594 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2595 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2596 need_index_type_struct = true;
2597 }
2598
2599 /* Then flatten: create the array of arrays. For an array type
2600 used to implement a packed array, get the component type from
2601 the original array type since the representation clauses that
2602 can affect it are on the latter. */
2603 if (Is_Packed_Array_Impl_Type (gnat_entity)
2604 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2605 {
2606 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2607 for (index = ndim - 1; index >= 0; index--)
2608 gnu_type = TREE_TYPE (gnu_type);
2609
2610 /* One of the above calls might have caused us to be elaborated,
2611 so don't blow up if so. */
2612 if (present_gnu_tree (gnat_entity))
2613 {
2614 maybe_present = true;
2615 break;
2616 }
2617 }
2618 else
2619 {
2620 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2621 debug_info_p);
2622
2623 /* One of the above calls might have caused us to be elaborated,
2624 so don't blow up if so. */
2625 if (present_gnu_tree (gnat_entity))
2626 {
2627 maybe_present = true;
2628 break;
2629 }
2630 }
2631
2632 /* Compute the maximum size of the array in units and bits. */
2633 if (gnu_max_size)
2634 {
2635 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2636 TYPE_SIZE_UNIT (gnu_type));
2637 gnu_max_size = size_binop (MULT_EXPR,
2638 convert (bitsizetype, gnu_max_size),
2639 TYPE_SIZE (gnu_type));
2640 }
2641 else
2642 gnu_max_size_unit = NULL_TREE;
2643
2644 /* Now build the array type. */
2645 for (index = ndim - 1; index >= 0; index --)
2646 {
2647 gnu_type = build_nonshared_array_type (gnu_type,
2648 gnu_index_types[index]);
2649 if (index == ndim - 1)
2650 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2651 = Reverse_Storage_Order (gnat_entity);
2652 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2653 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2654 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2655 }
2656
2657 /* Strip the ___XP suffix for standard DWARF. */
2658 if (Is_Packed_Array_Impl_Type (gnat_entity)
2659 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2660 {
2661 Entity_Id gnat_original_array_type
2662 = Underlying_Type (Original_Array_Type (gnat_entity));
2663
2664 gnu_entity_name
2665 = get_entity_name (gnat_original_array_type);
2666 }
2667
2668 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2669 TYPE_STUB_DECL (gnu_type)
2670 = create_type_stub_decl (gnu_entity_name, gnu_type);
2671
2672 /* If we are at file level and this is a multi-dimensional array,
2673 we need to make a variable corresponding to the stride of the
2674 inner dimensions. */
2675 if (global_bindings_p () && ndim > 1)
2676 {
2677 tree gnu_arr_type;
2678
2679 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2680 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2681 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2682 {
2683 tree eltype = TREE_TYPE (gnu_arr_type);
2684 char stride_name[32];
2685
2686 sprintf (stride_name, "ST%d", index);
2687 TYPE_SIZE (gnu_arr_type)
2688 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2689 gnat_entity, stride_name,
2690 definition, false);
2691
2692 /* ??? For now, store the size as a multiple of the
2693 alignment of the element type in bytes so that we
2694 can see the alignment from the tree. */
2695 sprintf (stride_name, "ST%d_A_UNIT", index);
2696 TYPE_SIZE_UNIT (gnu_arr_type)
2697 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2698 gnat_entity, stride_name,
2699 definition, false,
2700 TYPE_ALIGN (eltype));
2701
2702 /* ??? create_type_decl is not invoked on the inner types so
2703 the MULT_EXPR node built above will never be marked. */
2704 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2705 }
2706 }
2707
2708 /* If we need to write out a record type giving the names of the
2709 bounds for debugging purposes, do it now and make the record
2710 type a parallel type. This is not needed for a packed array
2711 since the bounds are conveyed by the original array type. */
2712 if (need_index_type_struct
2713 && debug_info_p
2714 && !Is_Packed_Array_Impl_Type (gnat_entity))
2715 {
2716 tree gnu_bound_rec = make_node (RECORD_TYPE);
2717 tree gnu_field_list = NULL_TREE;
2718 tree gnu_field;
2719
2720 TYPE_NAME (gnu_bound_rec)
2721 = create_concat_name (gnat_entity, "XA");
2722
2723 for (index = ndim - 1; index >= 0; index--)
2724 {
2725 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2726 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2727
2728 /* Make sure to reference the types themselves, and not just
2729 their names, as the debugger may fall back on them. */
2730 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2731 gnu_bound_rec, NULL_TREE,
2732 NULL_TREE, 0, 0);
2733 DECL_CHAIN (gnu_field) = gnu_field_list;
2734 gnu_field_list = gnu_field;
2735 }
2736
2737 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2738 add_parallel_type (gnu_type, gnu_bound_rec);
2739 }
2740
2741 /* If this is a packed array type, make the original array type a
2742 parallel/debug type. Otherwise, if such GNAT encodings are
2743 required, do it for the base array type if it isn't artificial to
2744 make sure it is kept in the debug info. */
2745 if (debug_info_p)
2746 {
2747 if (Is_Packed_Array_Impl_Type (gnat_entity))
2748 associate_original_type_to_packed_array (gnu_type,
2749 gnat_entity);
2750 else
2751 {
2752 tree gnu_base_decl
2753 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE,
2754 false);
2755 if (!DECL_ARTIFICIAL (gnu_base_decl)
2756 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2757 add_parallel_type (gnu_type,
2758 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2759 }
2760 }
2761
2762 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2763 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2764 = (Is_Packed_Array_Impl_Type (gnat_entity)
2765 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2766
2767 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2768 implementation types as such so that the debug information back-end
2769 can output the appropriate description for them. */
2770 TYPE_PACKED (gnu_type)
2771 = (Is_Packed (gnat_entity)
2772 || Is_Packed_Array_Impl_Type (gnat_entity));
2773
2774 /* If the size is self-referential and the maximum size doesn't
2775 overflow, use it. */
2776 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2777 && gnu_max_size
2778 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2779 && TREE_OVERFLOW (gnu_max_size))
2780 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2781 && TREE_OVERFLOW (gnu_max_size_unit)))
2782 {
2783 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2784 TYPE_SIZE (gnu_type));
2785 TYPE_SIZE_UNIT (gnu_type)
2786 = size_binop (MIN_EXPR, gnu_max_size_unit,
2787 TYPE_SIZE_UNIT (gnu_type));
2788 }
2789
2790 /* Set our alias set to that of our base type. This gives all
2791 array subtypes the same alias set. */
2792 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2793
2794 /* If this is a packed type, make this type the same as the packed
2795 array type, but do some adjusting in the type first. */
2796 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2797 {
2798 Entity_Id gnat_index;
2799 tree gnu_inner;
2800
2801 /* First finish the type we had been making so that we output
2802 debugging information for it. */
2803 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2804 if (Treat_As_Volatile (gnat_entity))
2805 {
2806 const int quals
2807 = TYPE_QUAL_VOLATILE
2808 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2809 gnu_type = change_qualified_type (gnu_type, quals);
2810 }
2811 /* Make it artificial only if the base type was artificial too.
2812 That's sort of "morally" true and will make it possible for
2813 the debugger to look it up by name in DWARF, which is needed
2814 in order to decode the packed array type. */
2815 gnu_decl
2816 = create_type_decl (gnu_entity_name, gnu_type,
2817 !Comes_From_Source (Etype (gnat_entity))
2818 && artificial_p, debug_info_p,
2819 gnat_entity);
2820
2821 /* Save it as our equivalent in case the call below elaborates
2822 this type again. */
2823 save_gnu_tree (gnat_entity, gnu_decl, false);
2824
2825 gnu_decl
2826 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2827 NULL_TREE, false);
2828 this_made_decl = true;
2829 gnu_type = TREE_TYPE (gnu_decl);
2830
2831 save_gnu_tree (gnat_entity, NULL_TREE, false);
2832
2833 gnu_inner = gnu_type;
2834 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2835 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2836 || TYPE_PADDING_P (gnu_inner)))
2837 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2838
2839 /* We need to attach the index type to the type we just made so
2840 that the actual bounds can later be put into a template. */
2841 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2842 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2843 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2844 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2845 {
2846 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2847 {
2848 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2849 TYPE_MODULUS for modular types so we make an extra
2850 subtype if necessary. */
2851 if (TYPE_MODULAR_P (gnu_inner))
2852 {
2853 tree gnu_subtype
2854 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2855 TREE_TYPE (gnu_subtype) = gnu_inner;
2856 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2857 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2858 TYPE_MIN_VALUE (gnu_inner));
2859 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2860 TYPE_MAX_VALUE (gnu_inner));
2861 gnu_inner = gnu_subtype;
2862 }
2863
2864 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2865
2866 /* Check for other cases of overloading. */
2867 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2868 }
2869
2870 for (gnat_index = First_Index (gnat_entity);
2871 Present (gnat_index);
2872 gnat_index = Next_Index (gnat_index))
2873 SET_TYPE_ACTUAL_BOUNDS
2874 (gnu_inner,
2875 tree_cons (NULL_TREE,
2876 get_unpadded_type (Etype (gnat_index)),
2877 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2878
2879 if (Convention (gnat_entity) != Convention_Fortran)
2880 SET_TYPE_ACTUAL_BOUNDS
2881 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2882
2883 if (TREE_CODE (gnu_type) == RECORD_TYPE
2884 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2885 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2886 }
2887 }
2888 }
2889 break;
2890
2891 case E_String_Literal_Subtype:
2892 /* Create the type for a string literal. */
2893 {
2894 Entity_Id gnat_full_type
2895 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2896 && Present (Full_View (Etype (gnat_entity)))
2897 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2898 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2899 tree gnu_string_array_type
2900 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2901 tree gnu_string_index_type
2902 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2903 (TYPE_DOMAIN (gnu_string_array_type))));
2904 tree gnu_lower_bound
2905 = convert (gnu_string_index_type,
2906 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2907 tree gnu_length
2908 = UI_To_gnu (String_Literal_Length (gnat_entity),
2909 gnu_string_index_type);
2910 tree gnu_upper_bound
2911 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2912 gnu_lower_bound,
2913 int_const_binop (MINUS_EXPR, gnu_length,
2914 convert (gnu_string_index_type,
2915 integer_one_node)));
2916 tree gnu_index_type
2917 = create_index_type (convert (sizetype, gnu_lower_bound),
2918 convert (sizetype, gnu_upper_bound),
2919 create_range_type (gnu_string_index_type,
2920 gnu_lower_bound,
2921 gnu_upper_bound),
2922 gnat_entity);
2923
2924 gnu_type
2925 = build_nonshared_array_type (gnat_to_gnu_type
2926 (Component_Type (gnat_entity)),
2927 gnu_index_type);
2928 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2929 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2930 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2931 }
2932 break;
2933
2934 /* Record Types and Subtypes
2935
2936 The following fields are defined on record types:
2937
2938 Has_Discriminants True if the record has discriminants
2939 First_Discriminant Points to head of list of discriminants
2940 First_Entity Points to head of list of fields
2941 Is_Tagged_Type True if the record is tagged
2942
2943 Implementation of Ada records and discriminated records:
2944
2945 A record type definition is transformed into the equivalent of a C
2946 struct definition. The fields that are the discriminants which are
2947 found in the Full_Type_Declaration node and the elements of the
2948 Component_List found in the Record_Type_Definition node. The
2949 Component_List can be a recursive structure since each Variant of
2950 the Variant_Part of the Component_List has a Component_List.
2951
2952 Processing of a record type definition comprises starting the list of
2953 field declarations here from the discriminants and the calling the
2954 function components_to_record to add the rest of the fields from the
2955 component list and return the gnu type node. The function
2956 components_to_record will call itself recursively as it traverses
2957 the tree. */
2958
2959 case E_Record_Type:
2960 if (Has_Complex_Representation (gnat_entity))
2961 {
2962 gnu_type
2963 = build_complex_type
2964 (get_unpadded_type
2965 (Etype (Defining_Entity
2966 (First (Component_Items
2967 (Component_List
2968 (Type_Definition
2969 (Declaration_Node (gnat_entity)))))))));
2970
2971 break;
2972 }
2973
2974 {
2975 Node_Id full_definition = Declaration_Node (gnat_entity);
2976 Node_Id record_definition = Type_Definition (full_definition);
2977 Node_Id gnat_constr;
2978 Entity_Id gnat_field;
2979 tree gnu_field, gnu_field_list = NULL_TREE;
2980 tree gnu_get_parent;
2981 /* Set PACKED in keeping with gnat_to_gnu_field. */
2982 const int packed
2983 = Is_Packed (gnat_entity)
2984 ? 1
2985 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2986 ? -1
2987 : 0;
2988 const bool has_align = Known_Alignment (gnat_entity);
2989 const bool has_discr = Has_Discriminants (gnat_entity);
2990 const bool has_rep = Has_Specified_Layout (gnat_entity);
2991 const bool is_extension
2992 = (Is_Tagged_Type (gnat_entity)
2993 && Nkind (record_definition) == N_Derived_Type_Definition);
2994 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2995 bool all_rep = has_rep;
2996
2997 /* See if all fields have a rep clause. Stop when we find one
2998 that doesn't. */
2999 if (all_rep)
3000 for (gnat_field = First_Entity (gnat_entity);
3001 Present (gnat_field);
3002 gnat_field = Next_Entity (gnat_field))
3003 if ((Ekind (gnat_field) == E_Component
3004 || Ekind (gnat_field) == E_Discriminant)
3005 && No (Component_Clause (gnat_field)))
3006 {
3007 all_rep = false;
3008 break;
3009 }
3010
3011 /* If this is a record extension, go a level further to find the
3012 record definition. Also, verify we have a Parent_Subtype. */
3013 if (is_extension)
3014 {
3015 if (!type_annotate_only
3016 || Present (Record_Extension_Part (record_definition)))
3017 record_definition = Record_Extension_Part (record_definition);
3018
3019 gcc_assert (type_annotate_only
3020 || Present (Parent_Subtype (gnat_entity)));
3021 }
3022
3023 /* Make a node for the record. If we are not defining the record,
3024 suppress expanding incomplete types. */
3025 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3026 TYPE_NAME (gnu_type) = gnu_entity_name;
3027 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3028 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3029 = Reverse_Storage_Order (gnat_entity);
3030 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3031
3032 if (!definition)
3033 {
3034 defer_incomplete_level++;
3035 this_deferred = true;
3036 }
3037
3038 /* If both a size and rep clause were specified, put the size on
3039 the record type now so that it can get the proper layout. */
3040 if (has_rep && Known_RM_Size (gnat_entity))
3041 TYPE_SIZE (gnu_type)
3042 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3043
3044 /* Always set the alignment on the record type here so that it can
3045 get the proper layout. */
3046 if (has_align)
3047 SET_TYPE_ALIGN (gnu_type,
3048 validate_alignment (Alignment (gnat_entity),
3049 gnat_entity, 0));
3050 else
3051 {
3052 SET_TYPE_ALIGN (gnu_type, 0);
3053
3054 /* If a type needs strict alignment, the minimum size will be the
3055 type size instead of the RM size (see validate_size). Cap the
3056 alignment lest it causes this type size to become too large. */
3057 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3058 {
3059 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3060 unsigned int max_align = max_size & -max_size;
3061 if (max_align < BIGGEST_ALIGNMENT)
3062 TYPE_MAX_ALIGN (gnu_type) = max_align;
3063 }
3064 }
3065
3066 /* If we have a Parent_Subtype, make a field for the parent. If
3067 this record has rep clauses, force the position to zero. */
3068 if (Present (Parent_Subtype (gnat_entity)))
3069 {
3070 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3071 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3072 tree gnu_parent;
3073
3074 /* A major complexity here is that the parent subtype will
3075 reference our discriminants in its Stored_Constraint list.
3076 But those must reference the parent component of this record
3077 which is precisely of the parent subtype we have not built yet!
3078 To break the circle we first build a dummy COMPONENT_REF which
3079 represents the "get to the parent" operation and initialize
3080 each of those discriminants to a COMPONENT_REF of the above
3081 dummy parent referencing the corresponding discriminant of the
3082 base type of the parent subtype. */
3083 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3084 build0 (PLACEHOLDER_EXPR, gnu_type),
3085 build_decl (input_location,
3086 FIELD_DECL, NULL_TREE,
3087 gnu_dummy_parent_type),
3088 NULL_TREE);
3089
3090 if (has_discr)
3091 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3092 Present (gnat_field);
3093 gnat_field = Next_Stored_Discriminant (gnat_field))
3094 if (Present (Corresponding_Discriminant (gnat_field)))
3095 {
3096 tree gnu_field
3097 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3098 (gnat_field));
3099 save_gnu_tree
3100 (gnat_field,
3101 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3102 gnu_get_parent, gnu_field, NULL_TREE),
3103 true);
3104 }
3105
3106 /* Then we build the parent subtype. If it has discriminants but
3107 the type itself has unknown discriminants, this means that it
3108 doesn't contain information about how the discriminants are
3109 derived from those of the ancestor type, so it cannot be used
3110 directly. Instead it is built by cloning the parent subtype
3111 of the underlying record view of the type, for which the above
3112 derivation of discriminants has been made explicit. */
3113 if (Has_Discriminants (gnat_parent)
3114 && Has_Unknown_Discriminants (gnat_entity))
3115 {
3116 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3117
3118 /* If we are defining the type, the underlying record
3119 view must already have been elaborated at this point.
3120 Otherwise do it now as its parent subtype cannot be
3121 technically elaborated on its own. */
3122 if (definition)
3123 gcc_assert (present_gnu_tree (gnat_uview));
3124 else
3125 gnat_to_gnu_entity (gnat_uview, NULL_TREE, false);
3126
3127 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3128
3129 /* Substitute the "get to the parent" of the type for that
3130 of its underlying record view in the cloned type. */
3131 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3132 Present (gnat_field);
3133 gnat_field = Next_Stored_Discriminant (gnat_field))
3134 if (Present (Corresponding_Discriminant (gnat_field)))
3135 {
3136 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3137 tree gnu_ref
3138 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3139 gnu_get_parent, gnu_field, NULL_TREE);
3140 gnu_parent
3141 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3142 }
3143 }
3144 else
3145 gnu_parent = gnat_to_gnu_type (gnat_parent);
3146
3147 /* The parent field needs strict alignment so, if it is to
3148 be created with a component clause below, then we need
3149 to apply the same adjustment as in gnat_to_gnu_field. */
3150 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3151 SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent));
3152
3153 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3154 initially built. The discriminants must reference the fields
3155 of the parent subtype and not those of its base type for the
3156 placeholder machinery to properly work. */
3157 if (has_discr)
3158 {
3159 /* The actual parent subtype is the full view. */
3160 if (IN (Ekind (gnat_parent), Private_Kind))
3161 {
3162 if (Present (Full_View (gnat_parent)))
3163 gnat_parent = Full_View (gnat_parent);
3164 else
3165 gnat_parent = Underlying_Full_View (gnat_parent);
3166 }
3167
3168 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3169 Present (gnat_field);
3170 gnat_field = Next_Stored_Discriminant (gnat_field))
3171 if (Present (Corresponding_Discriminant (gnat_field)))
3172 {
3173 Entity_Id field;
3174 for (field = First_Stored_Discriminant (gnat_parent);
3175 Present (field);
3176 field = Next_Stored_Discriminant (field))
3177 if (same_discriminant_p (gnat_field, field))
3178 break;
3179 gcc_assert (Present (field));
3180 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3181 = gnat_to_gnu_field_decl (field);
3182 }
3183 }
3184
3185 /* The "get to the parent" COMPONENT_REF must be given its
3186 proper type... */
3187 TREE_TYPE (gnu_get_parent) = gnu_parent;
3188
3189 /* ...and reference the _Parent field of this record. */
3190 gnu_field
3191 = create_field_decl (parent_name_id,
3192 gnu_parent, gnu_type,
3193 has_rep
3194 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3195 has_rep
3196 ? bitsize_zero_node : NULL_TREE,
3197 0, 1);
3198 DECL_INTERNAL_P (gnu_field) = 1;
3199 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3200 TYPE_FIELDS (gnu_type) = gnu_field;
3201 }
3202
3203 /* Make the fields for the discriminants and put them into the record
3204 unless it's an Unchecked_Union. */
3205 if (has_discr)
3206 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3207 Present (gnat_field);
3208 gnat_field = Next_Stored_Discriminant (gnat_field))
3209 {
3210 /* If this is a record extension and this discriminant is the
3211 renaming of another discriminant, we've handled it above. */
3212 if (Present (Parent_Subtype (gnat_entity))
3213 && Present (Corresponding_Discriminant (gnat_field)))
3214 continue;
3215
3216 /* However, if we are just annotating types, the Parent_Subtype
3217 doesn't exist so we need skip the discriminant altogether. */
3218 if (type_annotate_only
3219 && Is_Tagged_Type (gnat_entity)
3220 && Is_Derived_Type (gnat_entity)
3221 && Present (Corresponding_Discriminant (gnat_field)))
3222 continue;
3223
3224 gnu_field
3225 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3226 debug_info_p);
3227
3228 /* Make an expression using a PLACEHOLDER_EXPR from the
3229 FIELD_DECL node just created and link that with the
3230 corresponding GNAT defining identifier. */
3231 save_gnu_tree (gnat_field,
3232 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3233 build0 (PLACEHOLDER_EXPR, gnu_type),
3234 gnu_field, NULL_TREE),
3235 true);
3236
3237 if (!is_unchecked_union)
3238 {
3239 DECL_CHAIN (gnu_field) = gnu_field_list;
3240 gnu_field_list = gnu_field;
3241 }
3242 }
3243
3244 /* If we have a derived untagged type that renames discriminants in
3245 the root type, the (stored) discriminants are a just copy of the
3246 discriminants of the root type. This means that any constraints
3247 added by the renaming in the derivation are disregarded as far
3248 as the layout of the derived type is concerned. To rescue them,
3249 we change the type of the (stored) discriminants to a subtype
3250 with the bounds of the type of the visible discriminants. */
3251 if (has_discr
3252 && !is_extension
3253 && Stored_Constraint (gnat_entity) != No_Elist)
3254 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3255 gnat_constr != No_Elmt;
3256 gnat_constr = Next_Elmt (gnat_constr))
3257 if (Nkind (Node (gnat_constr)) == N_Identifier
3258 /* Ignore access discriminants. */
3259 && !Is_Access_Type (Etype (Node (gnat_constr)))
3260 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3261 {
3262 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3263 tree gnu_discr_type, gnu_ref;
3264
3265 /* If the scope of the discriminant is not the record type,
3266 this means that we're processing the implicit full view
3267 of a type derived from a private discriminated type: in
3268 this case, the Stored_Constraint list is simply copied
3269 from the partial view, see Build_Derived_Private_Type.
3270 So we need to retrieve the corresponding discriminant
3271 of the implicit full view, otherwise we will abort. */
3272 if (Scope (gnat_discr) != gnat_entity)
3273 {
3274 Entity_Id field;
3275 for (field = First_Entity (gnat_entity);
3276 Present (field);
3277 field = Next_Entity (field))
3278 if (Ekind (field) == E_Discriminant
3279 && same_discriminant_p (gnat_discr, field))
3280 break;
3281 gcc_assert (Present (field));
3282 gnat_discr = field;
3283 }
3284
3285 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3286 gnu_ref
3287 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3288 NULL_TREE, false);
3289
3290 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3291 just above for one of the stored discriminants. */
3292 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3293
3294 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3295 {
3296 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3297 tree gnu_subtype
3298 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3299 ? make_unsigned_type (prec) : make_signed_type (prec);
3300 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3301 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3302 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3303 TYPE_MIN_VALUE (gnu_discr_type));
3304 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3305 TYPE_MAX_VALUE (gnu_discr_type));
3306 TREE_TYPE (gnu_ref)
3307 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3308 }
3309 }
3310
3311 /* Add the fields into the record type and finish it up. */
3312 components_to_record (gnu_type, Component_List (record_definition),
3313 gnu_field_list, packed, definition, false,
3314 all_rep, is_unchecked_union,
3315 artificial_p, debug_info_p,
3316 false, OK_To_Reorder_Components (gnat_entity),
3317 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3318
3319 /* Fill in locations of fields. */
3320 annotate_rep (gnat_entity, gnu_type);
3321
3322 /* If there are any entities in the chain corresponding to components
3323 that we did not elaborate, ensure we elaborate their types if they
3324 are Itypes. */
3325 for (gnat_temp = First_Entity (gnat_entity);
3326 Present (gnat_temp);
3327 gnat_temp = Next_Entity (gnat_temp))
3328 if ((Ekind (gnat_temp) == E_Component
3329 || Ekind (gnat_temp) == E_Discriminant)
3330 && Is_Itype (Etype (gnat_temp))
3331 && !present_gnu_tree (gnat_temp))
3332 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
3333
3334 /* If this is a record type associated with an exception definition,
3335 equate its fields to those of the standard exception type. This
3336 will make it possible to convert between them. */
3337 if (gnu_entity_name == exception_data_name_id)
3338 {
3339 tree gnu_std_field;
3340 for (gnu_field = TYPE_FIELDS (gnu_type),
3341 gnu_std_field = TYPE_FIELDS (except_type_node);
3342 gnu_field;
3343 gnu_field = DECL_CHAIN (gnu_field),
3344 gnu_std_field = DECL_CHAIN (gnu_std_field))
3345 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3346 gcc_assert (!gnu_std_field);
3347 }
3348 }
3349 break;
3350
3351 case E_Class_Wide_Subtype:
3352 /* If an equivalent type is present, that is what we should use.
3353 Otherwise, fall through to handle this like a record subtype
3354 since it may have constraints. */
3355 if (gnat_equiv_type != gnat_entity)
3356 {
3357 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
3358 maybe_present = true;
3359 break;
3360 }
3361
3362 /* ... fall through ... */
3363
3364 case E_Record_Subtype:
3365 /* If Cloned_Subtype is Present it means this record subtype has
3366 identical layout to that type or subtype and we should use
3367 that GCC type for this one. The front end guarantees that
3368 the component list is shared. */
3369 if (Present (Cloned_Subtype (gnat_entity)))
3370 {
3371 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3372 NULL_TREE, false);
3373 maybe_present = true;
3374 break;
3375 }
3376
3377 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3378 changing the type, make a new type with each field having the type of
3379 the field in the new subtype but the position computed by transforming
3380 every discriminant reference according to the constraints. We don't
3381 see any difference between private and non-private type here since
3382 derivations from types should have been deferred until the completion
3383 of the private type. */
3384 else
3385 {
3386 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3387 tree gnu_base_type;
3388
3389 if (!definition)
3390 {
3391 defer_incomplete_level++;
3392 this_deferred = true;
3393 }
3394
3395 gnu_base_type
3396 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3397
3398 if (present_gnu_tree (gnat_entity))
3399 {
3400 maybe_present = true;
3401 break;
3402 }
3403
3404 /* If this is a record subtype associated with a dispatch table,
3405 strip the suffix. This is necessary to make sure 2 different
3406 subtypes associated with the imported and exported views of a
3407 dispatch table are properly merged in LTO mode. */
3408 if (Is_Dispatch_Table_Entity (gnat_entity))
3409 {
3410 char *p;
3411 Get_Encoded_Name (gnat_entity);
3412 p = strchr (Name_Buffer, '_');
3413 gcc_assert (p);
3414 strcpy (p+2, "dtS");
3415 gnu_entity_name = get_identifier (Name_Buffer);
3416 }
3417
3418 /* When the subtype has discriminants and these discriminants affect
3419 the initial shape it has inherited, factor them in. But for an
3420 Unchecked_Union (it must be an Itype), just return the type.
3421 We can't just test Is_Constrained because private subtypes without
3422 discriminants of types with discriminants with default expressions
3423 are Is_Constrained but aren't constrained! */
3424 if (IN (Ekind (gnat_base_type), Record_Kind)
3425 && !Is_Unchecked_Union (gnat_base_type)
3426 && !Is_For_Access_Subtype (gnat_entity)
3427 && Has_Discriminants (gnat_entity)
3428 && Is_Constrained (gnat_entity)
3429 && Stored_Constraint (gnat_entity) != No_Elist)
3430 {
3431 vec<subst_pair> gnu_subst_list
3432 = build_subst_list (gnat_entity, gnat_base_type, definition);
3433 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3434 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3435 bool selected_variant = false, all_constant_pos = true;
3436 Entity_Id gnat_field;
3437 vec<variant_desc> gnu_variant_list;
3438
3439 gnu_type = make_node (RECORD_TYPE);
3440 TYPE_NAME (gnu_type) = gnu_entity_name;
3441 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3442 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3443 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3444 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3445 = Reverse_Storage_Order (gnat_entity);
3446 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3447
3448 /* Set the size, alignment and alias set of the new type to
3449 match that of the old one, doing required substitutions. */
3450 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3451 gnu_subst_list);
3452
3453 if (TYPE_IS_PADDING_P (gnu_base_type))
3454 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3455 else
3456 gnu_unpad_base_type = gnu_base_type;
3457
3458 /* Look for REP and variant parts in the base type. */
3459 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3460 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3461
3462 /* If there is a variant part, we must compute whether the
3463 constraints statically select a particular variant. If
3464 so, we simply drop the qualified union and flatten the
3465 list of fields. Otherwise we'll build a new qualified
3466 union for the variants that are still relevant. */
3467 if (gnu_variant_part)
3468 {
3469 variant_desc *v;
3470 unsigned int i;
3471
3472 gnu_variant_list
3473 = build_variant_list (TREE_TYPE (gnu_variant_part),
3474 gnu_subst_list,
3475 vNULL);
3476
3477 /* If all the qualifiers are unconditionally true, the
3478 innermost variant is statically selected. */
3479 selected_variant = true;
3480 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3481 if (!integer_onep (v->qual))
3482 {
3483 selected_variant = false;
3484 break;
3485 }
3486
3487 /* Otherwise, create the new variants. */
3488 if (!selected_variant)
3489 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3490 {
3491 tree old_variant = v->type;
3492 tree new_variant = make_node (RECORD_TYPE);
3493 tree suffix
3494 = concat_name (DECL_NAME (gnu_variant_part),
3495 IDENTIFIER_POINTER
3496 (DECL_NAME (v->field)));
3497 TYPE_NAME (new_variant)
3498 = concat_name (TYPE_NAME (gnu_type),
3499 IDENTIFIER_POINTER (suffix));
3500 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3501 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3502 copy_and_substitute_in_size (new_variant, old_variant,
3503 gnu_subst_list);
3504 v->new_type = new_variant;
3505 }
3506 }
3507 else
3508 {
3509 gnu_variant_list.create (0);
3510 selected_variant = false;
3511 }
3512
3513 /* Make a list of fields and their position in the base type. */
3514 gnu_pos_list
3515 = build_position_list (gnu_unpad_base_type,
3516 gnu_variant_list.exists ()
3517 && !selected_variant,
3518 size_zero_node, bitsize_zero_node,
3519 BIGGEST_ALIGNMENT, NULL_TREE);
3520
3521 /* Now go down every component in the subtype and compute its
3522 size and position from those of the component in the base
3523 type and from the constraints of the subtype. */
3524 for (gnat_field = First_Entity (gnat_entity);
3525 Present (gnat_field);
3526 gnat_field = Next_Entity (gnat_field))
3527 if ((Ekind (gnat_field) == E_Component
3528 || Ekind (gnat_field) == E_Discriminant)
3529 && !(Present (Corresponding_Discriminant (gnat_field))
3530 && Is_Tagged_Type (gnat_base_type))
3531 && Underlying_Type
3532 (Scope (Original_Record_Component (gnat_field)))
3533 == gnat_base_type)
3534 {
3535 Name_Id gnat_name = Chars (gnat_field);
3536 Entity_Id gnat_old_field
3537 = Original_Record_Component (gnat_field);
3538 tree gnu_old_field
3539 = gnat_to_gnu_field_decl (gnat_old_field);
3540 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3541 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3542 tree gnu_cont_type, gnu_last = NULL_TREE;
3543
3544 /* If the type is the same, retrieve the GCC type from the
3545 old field to take into account possible adjustments. */
3546 if (Etype (gnat_field) == Etype (gnat_old_field))
3547 gnu_field_type = TREE_TYPE (gnu_old_field);
3548 else
3549 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3550
3551 /* If there was a component clause, the field types must be
3552 the same for the type and subtype, so copy the data from
3553 the old field to avoid recomputation here. Also if the
3554 field is justified modular and the optimization in
3555 gnat_to_gnu_field was applied. */
3556 if (Present (Component_Clause (gnat_old_field))
3557 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3558 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3559 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3560 == TREE_TYPE (gnu_old_field)))
3561 {
3562 gnu_size = DECL_SIZE (gnu_old_field);
3563 gnu_field_type = TREE_TYPE (gnu_old_field);
3564 }
3565
3566 /* If the old field was packed and of constant size, we
3567 have to get the old size here, as it might differ from
3568 what the Etype conveys and the latter might overlap
3569 onto the following field. Try to arrange the type for
3570 possible better packing along the way. */
3571 else if (DECL_PACKED (gnu_old_field)
3572 && TREE_CODE (DECL_SIZE (gnu_old_field))
3573 == INTEGER_CST)
3574 {
3575 gnu_size = DECL_SIZE (gnu_old_field);
3576 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3577 && !TYPE_FAT_POINTER_P (gnu_field_type)
3578 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3579 gnu_field_type
3580 = make_packable_type (gnu_field_type, true);
3581 }
3582
3583 else
3584 gnu_size = TYPE_SIZE (gnu_field_type);
3585
3586 /* If the context of the old field is the base type or its
3587 REP part (if any), put the field directly in the new
3588 type; otherwise look up the context in the variant list
3589 and put the field either in the new type if there is a
3590 selected variant or in one of the new variants. */
3591 if (gnu_context == gnu_unpad_base_type
3592 || (gnu_rep_part
3593 && gnu_context == TREE_TYPE (gnu_rep_part)))
3594 gnu_cont_type = gnu_type;
3595 else
3596 {
3597 variant_desc *v;
3598 unsigned int i;
3599 tree rep_part;
3600
3601 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3602 if (gnu_context == v->type
3603 || ((rep_part = get_rep_part (v->type))
3604 && gnu_context == TREE_TYPE (rep_part)))
3605 break;
3606 if (v)
3607 {
3608 if (selected_variant)
3609 gnu_cont_type = gnu_type;
3610 else
3611 gnu_cont_type = v->new_type;
3612 }
3613 else
3614 /* The front-end may pass us "ghost" components if
3615 it fails to recognize that a constrained subtype
3616 is statically constrained. Discard them. */
3617 continue;
3618 }
3619
3620 /* Now create the new field modeled on the old one. */
3621 gnu_field
3622 = create_field_decl_from (gnu_old_field, gnu_field_type,
3623 gnu_cont_type, gnu_size,
3624 gnu_pos_list, gnu_subst_list);
3625 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3626
3627 /* Put it in one of the new variants directly. */
3628 if (gnu_cont_type != gnu_type)
3629 {
3630 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3631 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3632 }
3633
3634 /* To match the layout crafted in components_to_record,
3635 if this is the _Tag or _Parent field, put it before
3636 any other fields. */
3637 else if (gnat_name == Name_uTag
3638 || gnat_name == Name_uParent)
3639 gnu_field_list = chainon (gnu_field_list, gnu_field);
3640
3641 /* Similarly, if this is the _Controller field, put
3642 it before the other fields except for the _Tag or
3643 _Parent field. */
3644 else if (gnat_name == Name_uController && gnu_last)
3645 {
3646 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3647 DECL_CHAIN (gnu_last) = gnu_field;
3648 }
3649
3650 /* Otherwise, if this is a regular field, put it after
3651 the other fields. */
3652 else
3653 {
3654 DECL_CHAIN (gnu_field) = gnu_field_list;
3655 gnu_field_list = gnu_field;
3656 if (!gnu_last)
3657 gnu_last = gnu_field;
3658 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3659 all_constant_pos = false;
3660 }
3661
3662 save_gnu_tree (gnat_field, gnu_field, false);
3663 }
3664
3665 /* If there is a variant list, a selected variant and the fields
3666 all have a constant position, put them in order of increasing
3667 position to match that of constant CONSTRUCTORs. Likewise if
3668 there is no variant list but a REP part, since the latter has
3669 been flattened in the process. */
3670 if (((gnu_variant_list.exists () && selected_variant)
3671 || (!gnu_variant_list.exists () && gnu_rep_part))
3672 && all_constant_pos)
3673 {
3674 const int len = list_length (gnu_field_list);
3675 tree *field_arr = XALLOCAVEC (tree, len), t;
3676 int i;
3677
3678 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3679 field_arr[i] = t;
3680
3681 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3682
3683 gnu_field_list = NULL_TREE;
3684 for (i = 0; i < len; i++)
3685 {
3686 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3687 gnu_field_list = field_arr[i];
3688 }
3689 }
3690
3691 /* If there is a variant list and no selected variant, we need
3692 to create the nest of variant parts from the old nest. */
3693 else if (gnu_variant_list.exists () && !selected_variant)
3694 {
3695 tree new_variant_part
3696 = create_variant_part_from (gnu_variant_part,
3697 gnu_variant_list, gnu_type,
3698 gnu_pos_list, gnu_subst_list);
3699 DECL_CHAIN (new_variant_part) = gnu_field_list;
3700 gnu_field_list = new_variant_part;
3701 }
3702
3703 /* Now go through the entities again looking for Itypes that
3704 we have not elaborated but should (e.g., Etypes of fields
3705 that have Original_Components). */
3706 for (gnat_field = First_Entity (gnat_entity);
3707 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3708 if ((Ekind (gnat_field) == E_Discriminant
3709 || Ekind (gnat_field) == E_Component)
3710 && !present_gnu_tree (Etype (gnat_field)))
3711 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false);
3712
3713 /* We will output additional debug info manually below. */
3714 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3715 false);
3716 compute_record_mode (gnu_type);
3717
3718 /* Fill in locations of fields. */
3719 annotate_rep (gnat_entity, gnu_type);
3720
3721 /* If debugging information is being written for the type and if
3722 we are asked to output such encodings, write a record that
3723 shows what we are a subtype of and also make a variable that
3724 indicates our size, if still variable. */
3725 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3726 {
3727 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3728 tree gnu_unpad_base_name
3729 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3730 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3731
3732 TYPE_NAME (gnu_subtype_marker)
3733 = create_concat_name (gnat_entity, "XVS");
3734 finish_record_type (gnu_subtype_marker,
3735 create_field_decl (gnu_unpad_base_name,
3736 build_reference_type
3737 (gnu_unpad_base_type),
3738 gnu_subtype_marker,
3739 NULL_TREE, NULL_TREE,
3740 0, 0),
3741 0, true);
3742
3743 add_parallel_type (gnu_type, gnu_subtype_marker);
3744
3745 if (definition
3746 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3747 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3748 TYPE_SIZE_UNIT (gnu_subtype_marker)
3749 = create_var_decl (create_concat_name (gnat_entity,
3750 "XVZ"),
3751 NULL_TREE, sizetype, gnu_size_unit,
3752 false, false, false, false, false,
3753 true, debug_info_p,
3754 NULL, gnat_entity);
3755 }
3756
3757 gnu_variant_list.release ();
3758 gnu_subst_list.release ();
3759 }
3760
3761 /* Otherwise, go down all the components in the new type and make
3762 them equivalent to those in the base type. */
3763 else
3764 {
3765 gnu_type = gnu_base_type;
3766
3767 for (gnat_temp = First_Entity (gnat_entity);
3768 Present (gnat_temp);
3769 gnat_temp = Next_Entity (gnat_temp))
3770 if ((Ekind (gnat_temp) == E_Discriminant
3771 && !Is_Unchecked_Union (gnat_base_type))
3772 || Ekind (gnat_temp) == E_Component)
3773 save_gnu_tree (gnat_temp,
3774 gnat_to_gnu_field_decl
3775 (Original_Record_Component (gnat_temp)),
3776 false);
3777 }
3778 }
3779 break;
3780
3781 case E_Access_Subprogram_Type:
3782 case E_Anonymous_Access_Subprogram_Type:
3783 /* Use the special descriptor type for dispatch tables if needed,
3784 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3785 Note that we are only required to do so for static tables in
3786 order to be compatible with the C++ ABI, but Ada 2005 allows
3787 to extend library level tagged types at the local level so
3788 we do it in the non-static case as well. */
3789 if (TARGET_VTABLE_USES_DESCRIPTORS
3790 && Is_Dispatch_Table_Entity (gnat_entity))
3791 {
3792 gnu_type = fdesc_type_node;
3793 gnu_size = TYPE_SIZE (gnu_type);
3794 break;
3795 }
3796
3797 /* ... fall through ... */
3798
3799 case E_Allocator_Type:
3800 case E_Access_Type:
3801 case E_Access_Attribute_Type:
3802 case E_Anonymous_Access_Type:
3803 case E_General_Access_Type:
3804 {
3805 /* The designated type and its equivalent type for gigi. */
3806 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3807 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3808 /* Whether it comes from a limited with. */
3809 const bool is_from_limited_with
3810 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3811 && From_Limited_With (gnat_desig_equiv));
3812 /* The "full view" of the designated type. If this is an incomplete
3813 entity from a limited with, treat its non-limited view as the full
3814 view. Otherwise, if this is an incomplete or private type, use the
3815 full view. In the former case, we might point to a private type,
3816 in which case, we need its full view. Also, we want to look at the
3817 actual type used for the representation, so this takes a total of
3818 three steps. */
3819 Entity_Id gnat_desig_full_direct_first
3820 = (is_from_limited_with
3821 ? Non_Limited_View (gnat_desig_equiv)
3822 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3823 ? Full_View (gnat_desig_equiv) : Empty));
3824 Entity_Id gnat_desig_full_direct
3825 = ((is_from_limited_with
3826 && Present (gnat_desig_full_direct_first)
3827 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3828 ? Full_View (gnat_desig_full_direct_first)
3829 : gnat_desig_full_direct_first);
3830 Entity_Id gnat_desig_full
3831 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3832 /* The type actually used to represent the designated type, either
3833 gnat_desig_full or gnat_desig_equiv. */
3834 Entity_Id gnat_desig_rep;
3835 /* We want to know if we'll be seeing the freeze node for any
3836 incomplete type we may be pointing to. */
3837 const bool in_main_unit
3838 = (Present (gnat_desig_full)
3839 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3840 : In_Extended_Main_Code_Unit (gnat_desig_type));
3841 /* True if we make a dummy type here. */
3842 bool made_dummy = false;
3843 /* The mode to be used for the pointer type. */
3844 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3845 /* The GCC type used for the designated type. */
3846 tree gnu_desig_type = NULL_TREE;
3847
3848 if (!targetm.valid_pointer_mode (p_mode))
3849 p_mode = ptr_mode;
3850
3851 /* If either the designated type or its full view is an unconstrained
3852 array subtype, replace it with the type it's a subtype of. This
3853 avoids problems with multiple copies of unconstrained array types.
3854 Likewise, if the designated type is a subtype of an incomplete
3855 record type, use the parent type to avoid order of elaboration
3856 issues. This can lose some code efficiency, but there is no
3857 alternative. */
3858 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3859 && !Is_Constrained (gnat_desig_equiv))
3860 gnat_desig_equiv = Etype (gnat_desig_equiv);
3861 if (Present (gnat_desig_full)
3862 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3863 && !Is_Constrained (gnat_desig_full))
3864 || (Ekind (gnat_desig_full) == E_Record_Subtype
3865 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3866 gnat_desig_full = Etype (gnat_desig_full);
3867
3868 /* Set the type that's the representation of the designated type. */
3869 gnat_desig_rep
3870 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3871
3872 /* If we already know what the full type is, use it. */
3873 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3874 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3875
3876 /* Get the type of the thing we are to point to and build a pointer to
3877 it. If it is a reference to an incomplete or private type with a
3878 full view that is a record or an array, make a dummy type node and
3879 get the actual type later when we have verified it is safe. */
3880 else if ((!in_main_unit
3881 && !present_gnu_tree (gnat_desig_equiv)
3882 && Present (gnat_desig_full)
3883 && (Is_Record_Type (gnat_desig_full)
3884 || Is_Array_Type (gnat_desig_full)))
3885 /* Likewise if this is a reference to a record, an array or a
3886 subprogram type and we are to defer elaborating incomplete
3887 types. We do this because this access type may be the full
3888 view of a private type. */
3889 || ((!in_main_unit || imported_p)
3890 && defer_incomplete_level != 0
3891 && !present_gnu_tree (gnat_desig_equiv)
3892 && (Is_Record_Type (gnat_desig_rep)
3893 || Is_Array_Type (gnat_desig_rep)
3894 || Ekind (gnat_desig_rep) == E_Subprogram_Type))
3895 /* If this is a reference from a limited_with type back to our
3896 main unit and there's a freeze node for it, either we have
3897 already processed the declaration and made the dummy type,
3898 in which case we just reuse the latter, or we have not yet,
3899 in which case we make the dummy type and it will be reused
3900 when the declaration is finally processed. In both cases,
3901 the pointer eventually created below will be automatically
3902 adjusted when the freeze node is processed. */
3903 || (in_main_unit
3904 && is_from_limited_with
3905 && Present (Freeze_Node (gnat_desig_rep))))
3906 {
3907 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3908 made_dummy = true;
3909 }
3910
3911 /* Otherwise handle the case of a pointer to itself. */
3912 else if (gnat_desig_equiv == gnat_entity)
3913 {
3914 gnu_type
3915 = build_pointer_type_for_mode (void_type_node, p_mode,
3916 No_Strict_Aliasing (gnat_entity));
3917 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3918 }
3919
3920 /* If expansion is disabled, the equivalent type of a concurrent type
3921 is absent, so build a dummy pointer type. */
3922 else if (type_annotate_only && No (gnat_desig_equiv))
3923 gnu_type = ptr_type_node;
3924
3925 /* Finally, handle the default case where we can just elaborate our
3926 designated type. */
3927 else
3928 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3929
3930 /* It is possible that a call to gnat_to_gnu_type above resolved our
3931 type. If so, just return it. */
3932 if (present_gnu_tree (gnat_entity))
3933 {
3934 maybe_present = true;
3935 break;
3936 }
3937
3938 /* Access-to-unconstrained-array types need a special treatment. */
3939 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3940 {
3941 /* If the processing above got something that has a pointer, then
3942 we are done. This could have happened either because the type
3943 was elaborated or because somebody else executed the code. */
3944 if (!TYPE_POINTER_TO (gnu_desig_type))
3945 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3946
3947 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3948 }
3949
3950 /* If we haven't done it yet, build the pointer type the usual way. */
3951 else if (!gnu_type)
3952 {
3953 /* Modify the designated type if we are pointing only to constant
3954 objects, but don't do it for a dummy type. */
3955 if (Is_Access_Constant (gnat_entity)
3956 && !TYPE_IS_DUMMY_P (gnu_desig_type))
3957 gnu_desig_type
3958 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3959
3960 gnu_type
3961 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3962 No_Strict_Aliasing (gnat_entity));
3963 }
3964
3965 /* If the designated type is not declared in the main unit and we made
3966 a dummy node for it, save our definition, elaborate the actual type
3967 and replace the dummy type we made with the actual one. But if we
3968 are to defer actually looking up the actual type, make an entry in
3969 the deferred list instead. If this is from a limited with, we may
3970 have to defer until the end of the current unit. */
3971 if (!in_main_unit && made_dummy)
3972 {
3973 if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE)
3974 gnu_type
3975 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type));
3976
3977 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3978 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3979 artificial_p, debug_info_p,
3980 gnat_entity);
3981 this_made_decl = true;
3982 gnu_type = TREE_TYPE (gnu_decl);
3983 save_gnu_tree (gnat_entity, gnu_decl, false);
3984 saved = true;
3985
3986 if (defer_incomplete_level == 0 && !is_from_limited_with)
3987 {
3988 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type),
3989 gnat_to_gnu_type (gnat_desig_equiv));
3990 }
3991 else
3992 {
3993 struct incomplete *p = XNEW (struct incomplete);
3994 struct incomplete **head
3995 = (is_from_limited_with
3996 ? &defer_limited_with_list : &defer_incomplete_list);
3997
3998 p->old_type = gnu_desig_type;
3999 p->full_type = gnat_desig_equiv;
4000 p->next = *head;
4001 *head = p;
4002 }
4003 }
4004 }
4005 break;
4006
4007 case E_Access_Protected_Subprogram_Type:
4008 case E_Anonymous_Access_Protected_Subprogram_Type:
4009 /* The run-time representation is the equivalent type. */
4010 if (type_annotate_only && No (gnat_equiv_type))
4011 gnu_type = ptr_type_node;
4012 else
4013 {
4014 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4015 maybe_present = true;
4016 }
4017
4018 /* The designated subtype must be elaborated as well, if it does
4019 not have its own freeze node. */
4020 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4021 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4022 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4023 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4024 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4025 NULL_TREE, false);
4026
4027 break;
4028
4029 case E_Access_Subtype:
4030 /* We treat this as identical to its base type; any constraint is
4031 meaningful only to the front-end. */
4032 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4033
4034 /* The designated subtype must be elaborated as well, if it does
4035 not have its own freeze node. But designated subtypes created
4036 for constrained components of records with discriminants are
4037 not frozen by the front-end and not elaborated here, because
4038 their use may appear before the base type is frozen and it is
4039 not clear that they are needed in gigi. With the current model,
4040 there is no correct place where they could be elaborated. */
4041 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4042 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4043 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4044 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4045 {
4046 /* If we are to defer elaborating incomplete types, make a dummy
4047 type node and elaborate it later. */
4048 if (defer_incomplete_level != 0)
4049 {
4050 struct incomplete *p = XNEW (struct incomplete);
4051
4052 p->old_type
4053 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4054 p->full_type = Directly_Designated_Type (gnat_entity);
4055 p->next = defer_incomplete_list;
4056 defer_incomplete_list = p;
4057 }
4058 else if (!IN (Ekind (Base_Type
4059 (Directly_Designated_Type (gnat_entity))),
4060 Incomplete_Or_Private_Kind))
4061 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4062 NULL_TREE, false);
4063 }
4064
4065 maybe_present = true;
4066 break;
4067
4068 /* Subprogram Entities
4069
4070 The following access functions are defined for subprograms:
4071
4072 Etype Return type or Standard_Void_Type.
4073 First_Formal The first formal parameter.
4074 Is_Imported Indicates that the subprogram has appeared in
4075 an INTERFACE or IMPORT pragma. For now we
4076 assume that the external language is C.
4077 Is_Exported Likewise but for an EXPORT pragma.
4078 Is_Inlined True if the subprogram is to be inlined.
4079
4080 Each parameter is first checked by calling must_pass_by_ref on its
4081 type to determine if it is passed by reference. For parameters which
4082 are copied in, if they are Ada In Out or Out parameters, their return
4083 value becomes part of a record which becomes the return type of the
4084 function (C function - note that this applies only to Ada procedures
4085 so there is no Ada return type). Additional code to store back the
4086 parameters will be generated on the caller side. This transformation
4087 is done here, not in the front-end.
4088
4089 The intended result of the transformation can be seen from the
4090 equivalent source rewritings that follow:
4091
4092 struct temp {int a,b};
4093 procedure P (A,B: In Out ...) is temp P (int A,B)
4094 begin {
4095 .. ..
4096 end P; return {A,B};
4097 }
4098
4099 temp t;
4100 P(X,Y); t = P(X,Y);
4101 X = t.a , Y = t.b;
4102
4103 For subprogram types we need to perform mainly the same conversions to
4104 GCC form that are needed for procedures and function declarations. The
4105 only difference is that at the end, we make a type declaration instead
4106 of a function declaration. */
4107
4108 case E_Subprogram_Type:
4109 case E_Function:
4110 case E_Procedure:
4111 {
4112 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4113 enum inline_status_t inline_status
4114 = Has_Pragma_No_Inline (gnat_entity)
4115 ? is_suppressed
4116 : Has_Pragma_Inline_Always (gnat_entity)
4117 ? is_required
4118 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4119 bool public_flag = Is_Public (gnat_entity) || imported_p;
4120 /* Subprograms marked both Intrinsic and Always_Inline need not
4121 have a body of their own. */
4122 bool extern_flag
4123 = ((Is_Public (gnat_entity) && !definition)
4124 || imported_p
4125 || (Convention (gnat_entity) == Convention_Intrinsic
4126 && Has_Pragma_Inline_Always (gnat_entity)));
4127 tree gnu_param_list;
4128
4129 /* A parameter may refer to this type, so defer completion of any
4130 incomplete types. */
4131 if (kind == E_Subprogram_Type && !definition)
4132 {
4133 defer_incomplete_level++;
4134 this_deferred = true;
4135 }
4136
4137 /* If the subprogram has an alias, it is probably inherited, so
4138 we can use the original one. If the original "subprogram"
4139 is actually an enumeration literal, it may be the first use
4140 of its type, so we must elaborate that type now. */
4141 if (Present (Alias (gnat_entity)))
4142 {
4143 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
4144
4145 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4146 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE,
4147 false);
4148
4149 gnu_decl
4150 = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, false);
4151
4152 /* Elaborate any Itypes in the parameters of this entity. */
4153 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4154 Present (gnat_temp);
4155 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4156 if (Is_Itype (Etype (gnat_temp)))
4157 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
4158
4159 /* Materialize renamed subprograms in the debugging information
4160 when the renamed object is compile time known. We can consider
4161 such renamings as imported declarations.
4162
4163 Because the parameters in generics instantiation are generally
4164 materialized as renamings, we ofter end up having both the
4165 renamed subprogram and the renaming in the same context and with
4166 the same name: in this case, renaming is both useless debug-wise
4167 and potentially harmful as name resolution in the debugger could
4168 return twice the same entity! So avoid this case. */
4169 if (debug_info_p && !artificial_p
4170 && !(get_debug_scope (gnat_entity, NULL)
4171 == get_debug_scope (gnat_renamed, NULL)
4172 && Name_Equals (Chars (gnat_entity),
4173 Chars (gnat_renamed)))
4174 && Present (gnat_renamed)
4175 && (Ekind (gnat_renamed) == E_Function
4176 || Ekind (gnat_renamed) == E_Procedure)
4177 && gnu_decl
4178 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
4179 {
4180 tree decl = build_decl (input_location, IMPORTED_DECL,
4181 gnu_entity_name, void_type_node);
4182 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
4183 gnat_pushdecl (decl, gnat_entity);
4184 }
4185
4186 break;
4187 }
4188
4189 /* Get the GCC tree for the (underlying) subprogram type. If the
4190 entity is an actual subprogram, also get the parameter list. */
4191 gnu_type
4192 = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p,
4193 &gnu_param_list);
4194
4195 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4196 corresponding DECL node and check the parameter association. */
4197 if (Convention (gnat_entity) == Convention_Intrinsic
4198 && Present (Interface_Name (gnat_entity)))
4199 {
4200 tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4201
4202 /* If we have a builtin DECL for that function, use it. Check if
4203 the profiles are compatible and warn if they are not. Note that
4204 the checker is expected to post diagnostics in this case. */
4205 if (gnu_builtin_decl)
4206 {
4207 intrin_binding_t inb
4208 = { gnat_entity, gnu_type, TREE_TYPE (gnu_builtin_decl) };
4209
4210 if (!intrin_profiles_compatible_p (&inb))
4211 post_error
4212 ("?profile of& doesn''t match the builtin it binds!",
4213 gnat_entity);
4214
4215 gnu_decl = gnu_builtin_decl;
4216 gnu_type = TREE_TYPE (gnu_builtin_decl);
4217 break;
4218 }
4219
4220 /* Inability to find the builtin DECL most often indicates a
4221 genuine mistake, but imports of unregistered intrinsics are
4222 sometimes issued on purpose to allow hooking in alternate
4223 bodies. We post a warning conditioned on Wshadow in this case,
4224 to let developers be notified on demand without risking false
4225 positives with common default sets of options. */
4226 else if (warn_shadow)
4227 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4228 }
4229
4230 /* If there was no specified Interface_Name and the external and
4231 internal names of the subprogram are the same, only use the
4232 internal name to allow disambiguation of nested subprograms. */
4233 if (No (Interface_Name (gnat_entity))
4234 && gnu_ext_name == gnu_entity_name)
4235 gnu_ext_name = NULL_TREE;
4236
4237 /* Deal with platform-specific calling conventions. */
4238 if (Has_Stdcall_Convention (gnat_entity))
4239 prepend_one_attribute
4240 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4241 get_identifier ("stdcall"), NULL_TREE,
4242 gnat_entity);
4243 else if (Has_Thiscall_Convention (gnat_entity))
4244 prepend_one_attribute
4245 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4246 get_identifier ("thiscall"), NULL_TREE,
4247 gnat_entity);
4248
4249 /* If we should request stack realignment for a foreign convention
4250 subprogram, do so. Note that this applies to task entry points
4251 in particular. */
4252 if (FOREIGN_FORCE_REALIGN_STACK
4253 && Has_Foreign_Convention (gnat_entity))
4254 prepend_one_attribute
4255 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4256 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4257 gnat_entity);
4258
4259 /* Deal with a pragma Linker_Section on a subprogram. */
4260 if ((kind == E_Function || kind == E_Procedure)
4261 && Present (Linker_Section_Pragma (gnat_entity)))
4262 prepend_one_attribute_pragma (&attr_list,
4263 Linker_Section_Pragma (gnat_entity));
4264
4265 /* If we are defining the subprogram and it has an Address clause
4266 we must get the address expression from the saved GCC tree for the
4267 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4268 the address expression here since the front-end has guaranteed
4269 in that case that the elaboration has no effects. If there is
4270 an Address clause and we are not defining the object, just
4271 make it a constant. */
4272 if (Present (Address_Clause (gnat_entity)))
4273 {
4274 tree gnu_address = NULL_TREE;
4275
4276 if (definition)
4277 gnu_address
4278 = (present_gnu_tree (gnat_entity)
4279 ? get_gnu_tree (gnat_entity)
4280 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4281
4282 save_gnu_tree (gnat_entity, NULL_TREE, false);
4283
4284 /* Convert the type of the object to a reference type that can
4285 alias everything as per RM 13.3(19). */
4286 gnu_type
4287 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4288 if (gnu_address)
4289 gnu_address = convert (gnu_type, gnu_address);
4290
4291 gnu_decl
4292 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4293 gnu_address, false, Is_Public (gnat_entity),
4294 extern_flag, false, false, artificial_p,
4295 debug_info_p, NULL, gnat_entity);
4296 DECL_BY_REF_P (gnu_decl) = 1;
4297 }
4298
4299 else if (kind == E_Subprogram_Type)
4300 {
4301 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4302
4303 gnu_decl
4304 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4305 debug_info_p, gnat_entity);
4306 }
4307
4308 else
4309 {
4310 gnu_decl
4311 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4312 gnu_param_list, inline_status,
4313 public_flag, extern_flag,
4314 artificial_p, debug_info_p,
4315 attr_list, gnat_entity);
4316
4317 DECL_STUBBED_P (gnu_decl)
4318 = (Convention (gnat_entity) == Convention_Stubbed);
4319 }
4320 }
4321 break;
4322
4323 case E_Incomplete_Type:
4324 case E_Incomplete_Subtype:
4325 case E_Private_Type:
4326 case E_Private_Subtype:
4327 case E_Limited_Private_Type:
4328 case E_Limited_Private_Subtype:
4329 case E_Record_Type_With_Private:
4330 case E_Record_Subtype_With_Private:
4331 {
4332 const bool is_from_limited_with
4333 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4334 /* Get the "full view" of this entity. If this is an incomplete
4335 entity from a limited with, treat its non-limited view as the
4336 full view. Otherwise, use either the full view or the underlying
4337 full view, whichever is present. This is used in all the tests
4338 below. */
4339 const Entity_Id full_view
4340 = is_from_limited_with
4341 ? Non_Limited_View (gnat_entity)
4342 : Present (Full_View (gnat_entity))
4343 ? Full_View (gnat_entity)
4344 : IN (kind, Private_Kind)
4345 ? Underlying_Full_View (gnat_entity)
4346 : Empty;
4347
4348 /* If this is an incomplete type with no full view, it must be a Taft
4349 Amendment type, in which case we return a dummy type. Otherwise,
4350 just get the type from its Etype. */
4351 if (No (full_view))
4352 {
4353 if (kind == E_Incomplete_Type)
4354 {
4355 gnu_type = make_dummy_type (gnat_entity);
4356 gnu_decl = TYPE_STUB_DECL (gnu_type);
4357 }
4358 else
4359 {
4360 gnu_decl
4361 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false);
4362 maybe_present = true;
4363 }
4364 }
4365
4366 /* Or else, if we already made a type for the full view, reuse it. */
4367 else if (present_gnu_tree (full_view))
4368 gnu_decl = get_gnu_tree (full_view);
4369
4370 /* Or else, if we are not defining the type or there is no freeze
4371 node on it, get the type for the full view. Likewise if this is
4372 a limited_with'ed type not declared in the main unit, which can
4373 happen for incomplete formal types instantiated on a type coming
4374 from a limited_with clause. */
4375 else if (!definition
4376 || No (Freeze_Node (full_view))
4377 || (is_from_limited_with
4378 && !In_Extended_Main_Code_Unit (full_view)))
4379 {
4380 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false);
4381 maybe_present = true;
4382 }
4383
4384 /* Otherwise, make a dummy type entry which will be replaced later.
4385 Save it as the full declaration's type so we can do any needed
4386 updates when we see it. */
4387 else
4388 {
4389 gnu_type = make_dummy_type (gnat_entity);
4390 gnu_decl = TYPE_STUB_DECL (gnu_type);
4391 if (Has_Completion_In_Body (gnat_entity))
4392 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4393 save_gnu_tree (full_view, gnu_decl, 0);
4394 }
4395 }
4396 break;
4397
4398 case E_Class_Wide_Type:
4399 /* Class-wide types are always transformed into their root type. */
4400 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4401 maybe_present = true;
4402 break;
4403
4404 case E_Protected_Type:
4405 case E_Protected_Subtype:
4406 case E_Task_Type:
4407 case E_Task_Subtype:
4408 /* If we are just annotating types and have no equivalent record type,
4409 just return void_type, except for root types that have discriminants
4410 because the discriminants will very likely be used in the declarative
4411 part of the associated body so they need to be translated. */
4412 if (type_annotate_only && No (gnat_equiv_type))
4413 {
4414 if (Has_Discriminants (gnat_entity)
4415 && Root_Type (gnat_entity) == gnat_entity)
4416 {
4417 tree gnu_field_list = NULL_TREE;
4418 Entity_Id gnat_field;
4419
4420 /* This is a minimal version of the E_Record_Type handling. */
4421 gnu_type = make_node (RECORD_TYPE);
4422 TYPE_NAME (gnu_type) = gnu_entity_name;
4423
4424 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4425 Present (gnat_field);
4426 gnat_field = Next_Stored_Discriminant (gnat_field))
4427 {
4428 tree gnu_field
4429 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4430 definition, debug_info_p);
4431
4432 save_gnu_tree (gnat_field,
4433 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4434 build0 (PLACEHOLDER_EXPR, gnu_type),
4435 gnu_field, NULL_TREE),
4436 true);
4437
4438 DECL_CHAIN (gnu_field) = gnu_field_list;
4439 gnu_field_list = gnu_field;
4440 }
4441
4442 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4443 false);
4444 }
4445 else
4446 gnu_type = void_type_node;
4447 }
4448
4449 /* Concurrent types are always transformed into their record type. */
4450 else
4451 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false);
4452 maybe_present = true;
4453 break;
4454
4455 case E_Label:
4456 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4457 break;
4458
4459 case E_Block:
4460 case E_Loop:
4461 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4462 we've already saved it, so we don't try to. */
4463 gnu_decl = error_mark_node;
4464 saved = true;
4465 break;
4466
4467 case E_Abstract_State:
4468 /* This is a SPARK annotation that only reaches here when compiling in
4469 ASIS mode. */
4470 gcc_assert (type_annotate_only);
4471 gnu_decl = error_mark_node;
4472 saved = true;
4473 break;
4474
4475 default:
4476 gcc_unreachable ();
4477 }
4478
4479 /* If we had a case where we evaluated another type and it might have
4480 defined this one, handle it here. */
4481 if (maybe_present && present_gnu_tree (gnat_entity))
4482 {
4483 gnu_decl = get_gnu_tree (gnat_entity);
4484 saved = true;
4485 }
4486
4487 /* If we are processing a type and there is either no decl for it or
4488 we just made one, do some common processing for the type, such as
4489 handling alignment and possible padding. */
4490 if (is_type && (!gnu_decl || this_made_decl))
4491 {
4492 /* Process the attributes, if not already done. Note that the type is
4493 already defined so we cannot pass true for IN_PLACE here. */
4494 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4495
4496 /* Tell the middle-end that objects of tagged types are guaranteed to
4497 be properly aligned. This is necessary because conversions to the
4498 class-wide type are translated into conversions to the root type,
4499 which can be less aligned than some of its derived types. */
4500 if (Is_Tagged_Type (gnat_entity)
4501 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4502 TYPE_ALIGN_OK (gnu_type) = 1;
4503
4504 /* Record whether the type is passed by reference. */
4505 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4506 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4507
4508 /* ??? Don't set the size for a String_Literal since it is either
4509 confirming or we don't handle it properly (if the low bound is
4510 non-constant). */
4511 if (!gnu_size && kind != E_String_Literal_Subtype)
4512 {
4513 Uint gnat_size = Known_Esize (gnat_entity)
4514 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4515 gnu_size
4516 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4517 false, Has_Size_Clause (gnat_entity));
4518 }
4519
4520 /* If a size was specified, see if we can make a new type of that size
4521 by rearranging the type, for example from a fat to a thin pointer. */
4522 if (gnu_size)
4523 {
4524 gnu_type
4525 = make_type_from_size (gnu_type, gnu_size,
4526 Has_Biased_Representation (gnat_entity));
4527
4528 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4529 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4530 gnu_size = NULL_TREE;
4531 }
4532
4533 /* If the alignment has not already been processed and this is not
4534 an unconstrained array type, see if an alignment is specified.
4535 If not, we pick a default alignment for atomic objects. */
4536 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4537 ;
4538 else if (Known_Alignment (gnat_entity))
4539 {
4540 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4541 TYPE_ALIGN (gnu_type));
4542
4543 /* Warn on suspiciously large alignments. This should catch
4544 errors about the (alignment,byte)/(size,bit) discrepancy. */
4545 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4546 {
4547 tree size;
4548
4549 /* If a size was specified, take it into account. Otherwise
4550 use the RM size for records or unions as the type size has
4551 already been adjusted to the alignment. */
4552 if (gnu_size)
4553 size = gnu_size;
4554 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4555 && !TYPE_FAT_POINTER_P (gnu_type))
4556 size = rm_size (gnu_type);
4557 else
4558 size = TYPE_SIZE (gnu_type);
4559
4560 /* Consider an alignment as suspicious if the alignment/size
4561 ratio is greater or equal to the byte/bit ratio. */
4562 if (tree_fits_uhwi_p (size)
4563 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4564 post_error_ne ("?suspiciously large alignment specified for&",
4565 Expression (Alignment_Clause (gnat_entity)),
4566 gnat_entity);
4567 }
4568 }
4569 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
4570 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4571 && integer_pow2p (TYPE_SIZE (gnu_type)))
4572 align = MIN (BIGGEST_ALIGNMENT,
4573 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4574 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
4575 && tree_fits_uhwi_p (gnu_size)
4576 && integer_pow2p (gnu_size))
4577 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4578
4579 /* See if we need to pad the type. If we did, and made a record,
4580 the name of the new type may be changed. So get it back for
4581 us when we make the new TYPE_DECL below. */
4582 if (gnu_size || align > 0)
4583 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4584 false, !gnu_decl, definition, false);
4585
4586 if (TYPE_IS_PADDING_P (gnu_type))
4587 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4588
4589 /* Now set the RM size of the type. We cannot do it before padding
4590 because we need to accept arbitrary RM sizes on integral types. */
4591 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4592
4593 /* If we are at global level, GCC will have applied variable_size to
4594 the type, but that won't have done anything. So, if it's not
4595 a constant or self-referential, call elaborate_expression_1 to
4596 make a variable for the size rather than calculating it each time.
4597 Handle both the RM size and the actual size. */
4598 if (global_bindings_p ()
4599 && TYPE_SIZE (gnu_type)
4600 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4601 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4602 {
4603 tree size = TYPE_SIZE (gnu_type);
4604
4605 TYPE_SIZE (gnu_type)
4606 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
4607 false);
4608
4609 /* ??? For now, store the size as a multiple of the alignment in
4610 bytes so that we can see the alignment from the tree. */
4611 TYPE_SIZE_UNIT (gnu_type)
4612 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4613 "SIZE_A_UNIT", definition, false,
4614 TYPE_ALIGN (gnu_type));
4615
4616 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4617 may not be marked by the call to create_type_decl below. */
4618 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4619
4620 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4621 {
4622 tree variant_part = get_variant_part (gnu_type);
4623 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4624
4625 if (variant_part)
4626 {
4627 tree union_type = TREE_TYPE (variant_part);
4628 tree offset = DECL_FIELD_OFFSET (variant_part);
4629
4630 /* If the position of the variant part is constant, subtract
4631 it from the size of the type of the parent to get the new
4632 size. This manual CSE reduces the data size. */
4633 if (TREE_CODE (offset) == INTEGER_CST)
4634 {
4635 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4636 TYPE_SIZE (union_type)
4637 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4638 bit_from_pos (offset, bitpos));
4639 TYPE_SIZE_UNIT (union_type)
4640 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4641 byte_from_pos (offset, bitpos));
4642 }
4643 else
4644 {
4645 TYPE_SIZE (union_type)
4646 = elaborate_expression_1 (TYPE_SIZE (union_type),
4647 gnat_entity, "VSIZE",
4648 definition, false);
4649
4650 /* ??? For now, store the size as a multiple of the
4651 alignment in bytes so that we can see the alignment
4652 from the tree. */
4653 TYPE_SIZE_UNIT (union_type)
4654 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4655 gnat_entity, "VSIZE_A_UNIT",
4656 definition, false,
4657 TYPE_ALIGN (union_type));
4658
4659 /* ??? For now, store the offset as a multiple of the
4660 alignment in bytes so that we can see the alignment
4661 from the tree. */
4662 DECL_FIELD_OFFSET (variant_part)
4663 = elaborate_expression_2 (offset, gnat_entity,
4664 "VOFFSET", definition, false,
4665 DECL_OFFSET_ALIGN
4666 (variant_part));
4667 }
4668
4669 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4670 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4671 }
4672
4673 if (operand_equal_p (ada_size, size, 0))
4674 ada_size = TYPE_SIZE (gnu_type);
4675 else
4676 ada_size
4677 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
4678 definition, false);
4679 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4680 }
4681 }
4682
4683 /* If this is a record type or subtype, call elaborate_expression_2 on
4684 any field position. Do this for both global and local types.
4685 Skip any fields that we haven't made trees for to avoid problems with
4686 class wide types. */
4687 if (IN (kind, Record_Kind))
4688 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4689 gnat_temp = Next_Entity (gnat_temp))
4690 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4691 {
4692 tree gnu_field = get_gnu_tree (gnat_temp);
4693
4694 /* ??? For now, store the offset as a multiple of the alignment
4695 in bytes so that we can see the alignment from the tree. */
4696 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4697 {
4698 DECL_FIELD_OFFSET (gnu_field)
4699 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4700 gnat_temp, "OFFSET", definition,
4701 false,
4702 DECL_OFFSET_ALIGN (gnu_field));
4703
4704 /* ??? The context of gnu_field is not necessarily gnu_type
4705 so the MULT_EXPR node built above may not be marked by
4706 the call to create_type_decl below. */
4707 if (global_bindings_p ())
4708 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4709 }
4710 }
4711
4712 if (Is_Atomic_Or_VFA (gnat_entity))
4713 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
4714
4715 /* If this is not an unconstrained array type, set some flags. */
4716 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4717 {
4718 if (Present (Alignment_Clause (gnat_entity)))
4719 TYPE_USER_ALIGN (gnu_type) = 1;
4720
4721 if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type))
4722 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
4723
4724 /* If it is passed by reference, force BLKmode to ensure that
4725 objects of this type will always be put in memory. */
4726 if (TYPE_MODE (gnu_type) != BLKmode
4727 && AGGREGATE_TYPE_P (gnu_type)
4728 && TYPE_BY_REFERENCE_P (gnu_type))
4729 SET_TYPE_MODE (gnu_type, BLKmode);
4730
4731 if (Treat_As_Volatile (gnat_entity))
4732 {
4733 const int quals
4734 = TYPE_QUAL_VOLATILE
4735 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
4736 gnu_type = change_qualified_type (gnu_type, quals);
4737 }
4738 }
4739
4740 if (!gnu_decl)
4741 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4742 artificial_p, debug_info_p,
4743 gnat_entity);
4744 else
4745 {
4746 TREE_TYPE (gnu_decl) = gnu_type;
4747 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4748 }
4749 }
4750
4751 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4752 {
4753 gnu_type = TREE_TYPE (gnu_decl);
4754
4755 /* If this is a derived type, relate its alias set to that of its parent
4756 to avoid troubles when a call to an inherited primitive is inlined in
4757 a context where a derived object is accessed. The inlined code works
4758 on the parent view so the resulting code may access the same object
4759 using both the parent and the derived alias sets, which thus have to
4760 conflict. As the same issue arises with component references, the
4761 parent alias set also has to conflict with composite types enclosing
4762 derived components. For instance, if we have:
4763
4764 type D is new T;
4765 type R is record
4766 Component : D;
4767 end record;
4768
4769 we want T to conflict with both D and R, in addition to R being a
4770 superset of D by record/component construction.
4771
4772 One way to achieve this is to perform an alias set copy from the
4773 parent to the derived type. This is not quite appropriate, though,
4774 as we don't want separate derived types to conflict with each other:
4775
4776 type I1 is new Integer;
4777 type I2 is new Integer;
4778
4779 We want I1 and I2 to both conflict with Integer but we do not want
4780 I1 to conflict with I2, and an alias set copy on derivation would
4781 have that effect.
4782
4783 The option chosen is to make the alias set of the derived type a
4784 superset of that of its parent type. It trivially fulfills the
4785 simple requirement for the Integer derivation example above, and
4786 the component case as well by superset transitivity:
4787
4788 superset superset
4789 R ----------> D ----------> T
4790
4791 However, for composite types, conversions between derived types are
4792 translated into VIEW_CONVERT_EXPRs so a sequence like:
4793
4794 type Comp1 is new Comp;
4795 type Comp2 is new Comp;
4796 procedure Proc (C : Comp1);
4797
4798 C : Comp2;
4799 Proc (Comp1 (C));
4800
4801 is translated into:
4802
4803 C : Comp2;
4804 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4805
4806 and gimplified into:
4807
4808 C : Comp2;
4809 Comp1 *C.0;
4810 C.0 = (Comp1 *) &C;
4811 Proc (C.0);
4812
4813 i.e. generates code involving type punning. Therefore, Comp1 needs
4814 to conflict with Comp2 and an alias set copy is required.
4815
4816 The language rules ensure the parent type is already frozen here. */
4817 if (kind != E_Subprogram_Type
4818 && Is_Derived_Type (gnat_entity)
4819 && !type_annotate_only)
4820 {
4821 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
4822 /* For constrained packed array subtypes, the implementation type is
4823 used instead of the nominal type. */
4824 if (kind == E_Array_Subtype
4825 && Is_Constrained (gnat_entity)
4826 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
4827 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
4828 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
4829 Is_Composite_Type (gnat_entity)
4830 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4831 }
4832
4833 /* Back-annotate the Alignment of the type if not already in the
4834 tree. Likewise for sizes. */
4835 if (Unknown_Alignment (gnat_entity))
4836 {
4837 unsigned int double_align, align;
4838 bool is_capped_double, align_clause;
4839
4840 /* If the default alignment of "double" or larger scalar types is
4841 specifically capped and this is not an array with an alignment
4842 clause on the component type, return the cap. */
4843 if ((double_align = double_float_alignment) > 0)
4844 is_capped_double
4845 = is_double_float_or_array (gnat_entity, &align_clause);
4846 else if ((double_align = double_scalar_alignment) > 0)
4847 is_capped_double
4848 = is_double_scalar_or_array (gnat_entity, &align_clause);
4849 else
4850 is_capped_double = align_clause = false;
4851
4852 if (is_capped_double && !align_clause)
4853 align = double_align;
4854 else
4855 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4856
4857 Set_Alignment (gnat_entity, UI_From_Int (align));
4858 }
4859
4860 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4861 {
4862 tree gnu_size = TYPE_SIZE (gnu_type);
4863
4864 /* If the size is self-referential, annotate the maximum value. */
4865 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4866 gnu_size = max_size (gnu_size, true);
4867
4868 /* If we are just annotating types and the type is tagged, the tag
4869 and the parent components are not generated by the front-end so
4870 alignment and sizes must be adjusted if there is no rep clause. */
4871 if (type_annotate_only
4872 && Is_Tagged_Type (gnat_entity)
4873 && Unknown_RM_Size (gnat_entity)
4874 && !VOID_TYPE_P (gnu_type)
4875 && (!TYPE_FIELDS (gnu_type)
4876 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
4877 {
4878 tree offset;
4879
4880 if (Is_Derived_Type (gnat_entity))
4881 {
4882 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
4883 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
4884 Set_Alignment (gnat_entity, Alignment (gnat_parent));
4885 }
4886 else
4887 {
4888 unsigned int align
4889 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4890 offset = bitsize_int (POINTER_SIZE);
4891 Set_Alignment (gnat_entity, UI_From_Int (align));
4892 }
4893
4894 if (TYPE_FIELDS (gnu_type))
4895 offset
4896 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
4897
4898 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4899 gnu_size = round_up (gnu_size, POINTER_SIZE);
4900 Uint uint_size = annotate_value (gnu_size);
4901 Set_RM_Size (gnat_entity, uint_size);
4902 Set_Esize (gnat_entity, uint_size);
4903 }
4904
4905 /* If there is a rep clause, only adjust alignment and Esize. */
4906 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4907 {
4908 unsigned int align
4909 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
4910 Set_Alignment (gnat_entity, UI_From_Int (align));
4911 gnu_size = round_up (gnu_size, POINTER_SIZE);
4912 Set_Esize (gnat_entity, annotate_value (gnu_size));
4913 }
4914
4915 /* Otherwise no adjustment is needed. */
4916 else
4917 Set_Esize (gnat_entity, annotate_value (gnu_size));
4918 }
4919
4920 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4921 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4922 }
4923
4924 /* If we haven't already, associate the ..._DECL node that we just made with
4925 the input GNAT entity node. */
4926 if (!saved)
4927 save_gnu_tree (gnat_entity, gnu_decl, false);
4928
4929 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4930 eliminate as many deferred computations as possible. */
4931 process_deferred_decl_context (false);
4932
4933 /* If this is an enumeration or floating-point type, we were not able to set
4934 the bounds since they refer to the type. These are always static. */
4935 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4936 || (kind == E_Floating_Point_Type))
4937 {
4938 tree gnu_scalar_type = gnu_type;
4939 tree gnu_low_bound, gnu_high_bound;
4940
4941 /* If this is a padded type, we need to use the underlying type. */
4942 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4943 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4944
4945 /* If this is a floating point type and we haven't set a floating
4946 point type yet, use this in the evaluation of the bounds. */
4947 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4948 longest_float_type_node = gnu_scalar_type;
4949
4950 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4951 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4952
4953 if (kind == E_Enumeration_Type)
4954 {
4955 /* Enumeration types have specific RM bounds. */
4956 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4957 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4958 }
4959 else
4960 {
4961 /* Floating-point types don't have specific RM bounds. */
4962 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4963 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4964 }
4965 }
4966
4967 /* If we deferred processing of incomplete types, re-enable it. If there
4968 were no other disables and we have deferred types to process, do so. */
4969 if (this_deferred
4970 && --defer_incomplete_level == 0
4971 && defer_incomplete_list)
4972 {
4973 struct incomplete *p, *next;
4974
4975 /* We are back to level 0 for the deferring of incomplete types.
4976 But processing these incomplete types below may itself require
4977 deferring, so preserve what we have and restart from scratch. */
4978 p = defer_incomplete_list;
4979 defer_incomplete_list = NULL;
4980
4981 for (; p; p = next)
4982 {
4983 next = p->next;
4984
4985 if (p->old_type)
4986 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
4987 gnat_to_gnu_type (p->full_type));
4988 free (p);
4989 }
4990 }
4991
4992 /* If we are not defining this type, see if it's on one of the lists of
4993 incomplete types. If so, handle the list entry now. */
4994 if (is_type && !definition)
4995 {
4996 struct incomplete *p;
4997
4998 for (p = defer_incomplete_list; p; p = p->next)
4999 if (p->old_type && p->full_type == gnat_entity)
5000 {
5001 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5002 TREE_TYPE (gnu_decl));
5003 p->old_type = NULL_TREE;
5004 }
5005
5006 for (p = defer_limited_with_list; p; p = p->next)
5007 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5008 {
5009 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5010 TREE_TYPE (gnu_decl));
5011 p->old_type = NULL_TREE;
5012 }
5013 }
5014
5015 if (this_global)
5016 force_global--;
5017
5018 /* If this is a packed array type whose original array type is itself
5019 an Itype without freeze node, make sure the latter is processed. */
5020 if (Is_Packed_Array_Impl_Type (gnat_entity)
5021 && Is_Itype (Original_Array_Type (gnat_entity))
5022 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5023 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5024 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false);
5025
5026 return gnu_decl;
5027 }
5028
5029 /* Similar, but if the returned value is a COMPONENT_REF, return the
5030 FIELD_DECL. */
5031
5032 tree
5033 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5034 {
5035 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5036
5037 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5038 gnu_field = TREE_OPERAND (gnu_field, 1);
5039
5040 return gnu_field;
5041 }
5042
5043 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5044 the GCC type corresponding to that entity. */
5045
5046 tree
5047 gnat_to_gnu_type (Entity_Id gnat_entity)
5048 {
5049 tree gnu_decl;
5050
5051 /* The back end never attempts to annotate generic types. */
5052 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5053 return void_type_node;
5054
5055 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5056 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5057
5058 return TREE_TYPE (gnu_decl);
5059 }
5060
5061 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5062 the unpadded version of the GCC type corresponding to that entity. */
5063
5064 tree
5065 get_unpadded_type (Entity_Id gnat_entity)
5066 {
5067 tree type = gnat_to_gnu_type (gnat_entity);
5068
5069 if (TYPE_IS_PADDING_P (type))
5070 type = TREE_TYPE (TYPE_FIELDS (type));
5071
5072 return type;
5073 }
5074
5075 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5076 a C++ imported method or equivalent.
5077
5078 We use the predicate on 32-bit x86/Windows to find out whether we need to
5079 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5080 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5081
5082 bool
5083 is_cplusplus_method (Entity_Id gnat_entity)
5084 {
5085 /* Check that the subprogram has C++ convention. */
5086 if (Convention (gnat_entity) != Convention_CPP)
5087 return false;
5088
5089 /* A constructor is a method on the C++ side. We deal with it now because
5090 it is declared without the 'this' parameter in the sources and, although
5091 the front-end will create a version with the 'this' parameter for code
5092 generation purposes, we want to return true for both versions. */
5093 if (Is_Constructor (gnat_entity))
5094 return true;
5095
5096 /* And that the type of the first parameter (indirectly) has it too. */
5097 Entity_Id gnat_first = First_Formal (gnat_entity);
5098 if (No (gnat_first))
5099 return false;
5100
5101 Entity_Id gnat_type = Etype (gnat_first);
5102 if (Is_Access_Type (gnat_type))
5103 gnat_type = Directly_Designated_Type (gnat_type);
5104 if (Convention (gnat_type) != Convention_CPP)
5105 return false;
5106
5107 /* This is the main case: C++ method imported as a primitive operation.
5108 Note that a C++ class with no virtual functions can be imported as a
5109 limited record type so the operation is not necessarily dispatching. */
5110 if (Is_Primitive (gnat_entity))
5111 return true;
5112
5113 /* A thunk needs to be handled like its associated primitive operation. */
5114 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5115 return true;
5116
5117 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5118 if (Is_Dispatch_Table_Entity (gnat_entity))
5119 return true;
5120
5121 return false;
5122 }
5123
5124 /* Finalize the processing of From_Limited_With incomplete types. */
5125
5126 void
5127 finalize_from_limited_with (void)
5128 {
5129 struct incomplete *p, *next;
5130
5131 p = defer_limited_with_list;
5132 defer_limited_with_list = NULL;
5133
5134 for (; p; p = next)
5135 {
5136 next = p->next;
5137
5138 if (p->old_type)
5139 {
5140 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5141 gnat_to_gnu_type (p->full_type));
5142 if (TYPE_DUMMY_IN_PROFILE_P (p->old_type))
5143 update_profiles_with (p->old_type);
5144 }
5145
5146 free (p);
5147 }
5148 }
5149
5150 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5151 kind of type (such E_Task_Type) that has a different type which Gigi
5152 uses for its representation. If the type does not have a special type
5153 for its representation, return GNAT_ENTITY. If a type is supposed to
5154 exist, but does not, abort unless annotating types, in which case
5155 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5156
5157 Entity_Id
5158 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5159 {
5160 Entity_Id gnat_equiv = gnat_entity;
5161
5162 if (No (gnat_entity))
5163 return gnat_entity;
5164
5165 switch (Ekind (gnat_entity))
5166 {
5167 case E_Class_Wide_Subtype:
5168 if (Present (Equivalent_Type (gnat_entity)))
5169 gnat_equiv = Equivalent_Type (gnat_entity);
5170 break;
5171
5172 case E_Access_Protected_Subprogram_Type:
5173 case E_Anonymous_Access_Protected_Subprogram_Type:
5174 gnat_equiv = Equivalent_Type (gnat_entity);
5175 break;
5176
5177 case E_Class_Wide_Type:
5178 gnat_equiv = Root_Type (gnat_entity);
5179 break;
5180
5181 case E_Task_Type:
5182 case E_Task_Subtype:
5183 case E_Protected_Type:
5184 case E_Protected_Subtype:
5185 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5186 break;
5187
5188 default:
5189 break;
5190 }
5191
5192 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5193
5194 return gnat_equiv;
5195 }
5196
5197 /* Return a GCC tree for a type corresponding to the component type of the
5198 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5199 is for an array being defined. DEBUG_INFO_P is true if we need to write
5200 debug information for other types that we may create in the process. */
5201
5202 static tree
5203 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5204 bool debug_info_p)
5205 {
5206 const Entity_Id gnat_type = Component_Type (gnat_array);
5207 tree gnu_type = gnat_to_gnu_type (gnat_type);
5208 tree gnu_comp_size;
5209 unsigned int max_align;
5210
5211 /* If an alignment is specified, use it as a cap on the component type
5212 so that it can be honored for the whole type. But ignore it for the
5213 original type of packed array types. */
5214 if (No (Packed_Array_Impl_Type (gnat_array))
5215 && Known_Alignment (gnat_array))
5216 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5217 else
5218 max_align = 0;
5219
5220 /* Try to get a smaller form of the component if needed. */
5221 if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array))
5222 && !Is_Bit_Packed_Array (gnat_array)
5223 && !Has_Aliased_Components (gnat_array)
5224 && !Strict_Alignment (gnat_type)
5225 && RECORD_OR_UNION_TYPE_P (gnu_type)
5226 && !TYPE_FAT_POINTER_P (gnu_type)
5227 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5228 gnu_type = make_packable_type (gnu_type, false, max_align);
5229
5230 if (Has_Atomic_Components (gnat_array))
5231 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5232
5233 /* Get and validate any specified Component_Size. */
5234 gnu_comp_size
5235 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5236 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5237 true, Has_Component_Size_Clause (gnat_array));
5238
5239 /* If the array has aliased components and the component size can be zero,
5240 force at least unit size to ensure that the components have distinct
5241 addresses. */
5242 if (!gnu_comp_size
5243 && Has_Aliased_Components (gnat_array)
5244 && (integer_zerop (TYPE_SIZE (gnu_type))
5245 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5246 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5247 gnu_comp_size
5248 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5249
5250 /* If the component type is a RECORD_TYPE that has a self-referential size,
5251 then use the maximum size for the component size. */
5252 if (!gnu_comp_size
5253 && TREE_CODE (gnu_type) == RECORD_TYPE
5254 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5255 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5256
5257 /* Honor the component size. This is not needed for bit-packed arrays. */
5258 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5259 {
5260 tree orig_type = gnu_type;
5261
5262 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5263 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5264 gnu_type = orig_type;
5265 else
5266 orig_type = gnu_type;
5267
5268 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5269 true, false, definition, true);
5270
5271 /* If a padding record was made, declare it now since it will never be
5272 declared otherwise. This is necessary to ensure that its subtrees
5273 are properly marked. */
5274 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5275 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5276 gnat_array);
5277 }
5278
5279 /* If the component type is a padded type made for a non-bit-packed array
5280 of scalars with reverse storage order, we need to propagate the reverse
5281 storage order to the padding type since it is the innermost enclosing
5282 aggregate type around the scalar. */
5283 if (TYPE_IS_PADDING_P (gnu_type)
5284 && Reverse_Storage_Order (gnat_array)
5285 && !Is_Bit_Packed_Array (gnat_array)
5286 && Is_Scalar_Type (gnat_type))
5287 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5288
5289 if (Has_Volatile_Components (gnat_array))
5290 {
5291 const int quals
5292 = TYPE_QUAL_VOLATILE
5293 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5294 gnu_type = change_qualified_type (gnu_type, quals);
5295 }
5296
5297 return gnu_type;
5298 }
5299
5300 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5301 in the parameter list built for GNAT_SUBPROG. FIRST is true if GNAT_PARAM
5302 is the first parameter in the list. Also set CICO to true if the parameter
5303 must use the copy-in copy-out implementation mechanism.
5304
5305 The returned tree is a PARM_DECL, except for those cases where no
5306 parameter needs to be actually passed to the subprogram; the type
5307 of this "shadow" parameter is then returned instead. */
5308
5309 static tree
5310 gnat_to_gnu_param (Entity_Id gnat_param, bool first, Entity_Id gnat_subprog,
5311 bool *cico)
5312 {
5313 Entity_Id gnat_param_type = Etype (gnat_param);
5314 Mechanism_Type mech = Mechanism (gnat_param);
5315 tree gnu_param_name = get_entity_name (gnat_param);
5316 tree gnu_param_type = gnat_to_gnu_type (gnat_param_type);
5317 bool foreign = Has_Foreign_Convention (gnat_subprog);
5318 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5319 /* The parameter can be indirectly modified if its address is taken. */
5320 bool ro_param = in_param && !Address_Taken (gnat_param);
5321 bool by_return = false, by_component_ptr = false;
5322 bool by_ref = false;
5323 bool restricted_aliasing_p = false;
5324 tree gnu_param;
5325
5326 /* Builtins are expanded inline and there is no real call sequence involved.
5327 So the type expected by the underlying expander is always the type of the
5328 argument "as is". */
5329 if (Convention (gnat_subprog) == Convention_Intrinsic
5330 && Present (Interface_Name (gnat_subprog)))
5331 mech = By_Copy;
5332
5333 /* Handle the first parameter of a valued procedure specially: it's a copy
5334 mechanism for which the parameter is never allocated. */
5335 else if (first && Is_Valued_Procedure (gnat_subprog))
5336 {
5337 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5338 mech = By_Copy;
5339 by_return = true;
5340 }
5341
5342 /* Or else, see if a Mechanism was supplied that forced this parameter
5343 to be passed one way or another. */
5344 else if (mech == Default || mech == By_Copy || mech == By_Reference)
5345 ;
5346
5347 /* Positive mechanism means by copy for sufficiently small parameters. */
5348 else if (mech > 0)
5349 {
5350 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
5351 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
5352 || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0)
5353 mech = By_Reference;
5354 else
5355 mech = By_Copy;
5356 }
5357
5358 /* Otherwise, it's an unsupported mechanism so error out. */
5359 else
5360 {
5361 post_error ("unsupported mechanism for&", gnat_param);
5362 mech = Default;
5363 }
5364
5365 /* If this is either a foreign function or if the underlying type won't
5366 be passed by reference and is as aligned as the original type, strip
5367 off possible padding type. */
5368 if (TYPE_IS_PADDING_P (gnu_param_type))
5369 {
5370 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5371
5372 if (foreign
5373 || (!must_pass_by_ref (unpadded_type)
5374 && mech != By_Reference
5375 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5376 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5377 gnu_param_type = unpadded_type;
5378 }
5379
5380 /* If this is a read-only parameter, make a variant of the type that is
5381 read-only. ??? However, if this is an unconstrained array, that type
5382 can be very complex, so skip it for now. Likewise for any other
5383 self-referential type. */
5384 if (ro_param
5385 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5386 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5387 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5388
5389 /* For foreign conventions, pass arrays as pointers to the element type.
5390 First check for unconstrained array and get the underlying array. */
5391 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5392 gnu_param_type
5393 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5394
5395 /* For GCC builtins, pass Address integer types as (void *) */
5396 if (Convention (gnat_subprog) == Convention_Intrinsic
5397 && Present (Interface_Name (gnat_subprog))
5398 && Is_Descendant_Of_Address (gnat_param_type))
5399 gnu_param_type = ptr_type_node;
5400
5401 /* Arrays are passed as pointers to element type for foreign conventions. */
5402 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5403 {
5404 /* Strip off any multi-dimensional entries, then strip
5405 off the last array to get the component type. */
5406 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5407 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5408 gnu_param_type = TREE_TYPE (gnu_param_type);
5409
5410 by_component_ptr = true;
5411 gnu_param_type = TREE_TYPE (gnu_param_type);
5412
5413 if (ro_param)
5414 gnu_param_type
5415 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5416
5417 gnu_param_type = build_pointer_type (gnu_param_type);
5418 }
5419
5420 /* Fat pointers are passed as thin pointers for foreign conventions. */
5421 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5422 gnu_param_type
5423 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5424
5425 /* If we were requested or muss pass by reference, do so.
5426 If we were requested to pass by copy, do so.
5427 Otherwise, for foreign conventions, pass In Out or Out parameters
5428 or aggregates by reference. For COBOL and Fortran, pass all
5429 integer and FP types that way too. For Convention Ada, use
5430 the standard Ada default. */
5431 else if (mech == By_Reference
5432 || must_pass_by_ref (gnu_param_type)
5433 || (mech != By_Copy
5434 && ((foreign
5435 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5436 || (foreign
5437 && (Convention (gnat_subprog) == Convention_Fortran
5438 || Convention (gnat_subprog) == Convention_COBOL)
5439 && (INTEGRAL_TYPE_P (gnu_param_type)
5440 || FLOAT_TYPE_P (gnu_param_type)))
5441 || (!foreign
5442 && default_pass_by_ref (gnu_param_type)))))
5443 {
5444 /* We take advantage of 6.2(12) by considering that references built for
5445 parameters whose type isn't by-ref and for which the mechanism hasn't
5446 been forced to by-ref allow only a restricted form of aliasing. */
5447 restricted_aliasing_p
5448 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5449 gnu_param_type = build_reference_type (gnu_param_type);
5450 by_ref = true;
5451 }
5452
5453 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5454 else if (!in_param)
5455 *cico = true;
5456
5457 if (mech == By_Copy && (by_ref || by_component_ptr))
5458 post_error ("?cannot pass & by copy", gnat_param);
5459
5460 /* If this is an Out parameter that isn't passed by reference and isn't
5461 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5462 it will be a VAR_DECL created when we process the procedure, so just
5463 return its type. For the special parameter of a valued procedure,
5464 never pass it in.
5465
5466 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5467 Out parameters with discriminants or implicit initial values to be
5468 handled like In Out parameters. These type are normally built as
5469 aggregates, hence passed by reference, except for some packed arrays
5470 which end up encoded in special integer types. Note that scalars can
5471 be given implicit initial values using the Default_Value aspect.
5472
5473 The exception we need to make is then for packed arrays of records
5474 with discriminants or implicit initial values. We have no light/easy
5475 way to check for the latter case, so we merely check for packed arrays
5476 of records. This may lead to useless copy-in operations, but in very
5477 rare cases only, as these would be exceptions in a set of already
5478 exceptional situations. */
5479 if (Ekind (gnat_param) == E_Out_Parameter
5480 && !by_ref
5481 && (by_return
5482 || (!POINTER_TYPE_P (gnu_param_type)
5483 && !AGGREGATE_TYPE_P (gnu_param_type)
5484 && !Has_Default_Aspect (gnat_param_type)))
5485 && !(Is_Array_Type (gnat_param_type)
5486 && Is_Packed (gnat_param_type)
5487 && Is_Composite_Type (Component_Type (gnat_param_type))))
5488 return gnu_param_type;
5489
5490 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5491 TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr;
5492 DECL_BY_REF_P (gnu_param) = by_ref;
5493 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5494 DECL_POINTS_TO_READONLY_P (gnu_param)
5495 = (ro_param && (by_ref || by_component_ptr));
5496 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5497 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5498 Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
5499
5500 /* If no Mechanism was specified, indicate what we're using, then
5501 back-annotate it. */
5502 if (mech == Default)
5503 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5504
5505 Set_Mechanism (gnat_param, mech);
5506 return gnu_param;
5507 }
5508
5509 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5510 GNAT_SUBPROG is updated when TYPE is completed. */
5511
5512 static void
5513 associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type)
5514 {
5515 gcc_assert (TYPE_IS_DUMMY_P (gnu_type));
5516
5517 struct tree_entity_vec_map in;
5518 in.base.from = gnu_type;
5519 struct tree_entity_vec_map **slot
5520 = dummy_to_subprog_map->find_slot (&in, INSERT);
5521 if (!*slot)
5522 {
5523 tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> ();
5524 e->base.from = gnu_type;
5525 e->to = NULL;
5526 *slot = e;
5527 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1;
5528 }
5529 vec<Entity_Id, va_gc_atomic> *v = (*slot)->to;
5530
5531 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5532 since this would mean updating twice its profile. */
5533 if (v)
5534 {
5535 const unsigned len = v->length ();
5536 unsigned int l = 0, u = len;
5537
5538 /* Entity_Id is a simple integer so we can implement a stable order on
5539 the vector with an ordered insertion scheme and binary search. */
5540 while (l < u)
5541 {
5542 unsigned int m = (l + u) / 2;
5543 int diff = (int) (*v)[m] - (int) gnat_subprog;
5544 if (diff > 0)
5545 u = m;
5546 else if (diff < 0)
5547 l = m + 1;
5548 else
5549 return;
5550 }
5551
5552 /* l == u and therefore is the insertion point. */
5553 vec_safe_insert (v, l, gnat_subprog);
5554 }
5555 else
5556 vec_safe_push (v, gnat_subprog);
5557
5558 (*slot)->to = v;
5559 }
5560
5561 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5562
5563 static void
5564 update_profile (Entity_Id gnat_subprog)
5565 {
5566 tree gnu_param_list;
5567 tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true,
5568 Needs_Debug_Info (gnat_subprog),
5569 &gnu_param_list);
5570 tree gnu_subprog = get_gnu_tree (gnat_subprog);
5571
5572 TREE_TYPE (gnu_subprog) = gnu_type;
5573
5574 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5575 and needs to be adjusted too. */
5576 if (Ekind (gnat_subprog) != E_Subprogram_Type)
5577 {
5578 DECL_ARGUMENTS (gnu_subprog) = gnu_param_list;
5579 finish_subprog_decl (gnu_subprog, gnu_type);
5580 }
5581 }
5582
5583 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5584 a dummy type which appears in profiles. */
5585
5586 void
5587 update_profiles_with (tree gnu_type)
5588 {
5589 struct tree_entity_vec_map in;
5590 in.base.from = gnu_type;
5591 struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in);
5592 gcc_assert (e);
5593 vec<Entity_Id, va_gc_atomic> *v = e->to;
5594 e->to = NULL;
5595 TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0;
5596
5597 unsigned int i;
5598 Entity_Id *iter;
5599 FOR_EACH_VEC_ELT (*v, i, iter)
5600 update_profile (*iter);
5601
5602 vec_free (v);
5603 }
5604
5605 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5606
5607 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5608 context may now appear as parameter and result types. As a consequence,
5609 we may need to defer their translation until after a freeze node is seen
5610 or to the end of the current unit. We also aim at handling temporarily
5611 incomplete types created by the usual delayed elaboration scheme. */
5612
5613 static tree
5614 gnat_to_gnu_profile_type (Entity_Id gnat_type)
5615 {
5616 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5617 so the rationale is exposed in that place. These processings probably
5618 ought to be merged at some point. */
5619 Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type);
5620 const bool is_from_limited_with
5621 = (IN (Ekind (gnat_equiv), Incomplete_Kind)
5622 && From_Limited_With (gnat_equiv));
5623 Entity_Id gnat_full_direct_first
5624 = (is_from_limited_with
5625 ? Non_Limited_View (gnat_equiv)
5626 : (IN (Ekind (gnat_equiv), Incomplete_Or_Private_Kind)
5627 ? Full_View (gnat_equiv) : Empty));
5628 Entity_Id gnat_full_direct
5629 = ((is_from_limited_with
5630 && Present (gnat_full_direct_first)
5631 && IN (Ekind (gnat_full_direct_first), Private_Kind))
5632 ? Full_View (gnat_full_direct_first)
5633 : gnat_full_direct_first);
5634 Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct);
5635 Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv;
5636 const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep);
5637 tree gnu_type;
5638
5639 if (Present (gnat_full) && present_gnu_tree (gnat_full))
5640 gnu_type = TREE_TYPE (get_gnu_tree (gnat_full));
5641
5642 else if (is_from_limited_with
5643 && ((!in_main_unit
5644 && !present_gnu_tree (gnat_equiv)
5645 && Present (gnat_full)
5646 && (Is_Record_Type (gnat_full) || Is_Array_Type (gnat_full)))
5647 || (in_main_unit && Present (Freeze_Node (gnat_rep)))))
5648 {
5649 gnu_type = make_dummy_type (gnat_equiv);
5650
5651 if (!in_main_unit)
5652 {
5653 struct incomplete *p = XNEW (struct incomplete);
5654
5655 p->old_type = gnu_type;
5656 p->full_type = gnat_equiv;
5657 p->next = defer_limited_with_list;
5658 defer_limited_with_list = p;
5659 }
5660 }
5661
5662 else if (type_annotate_only && No (gnat_equiv))
5663 gnu_type = void_type_node;
5664
5665 else
5666 gnu_type = gnat_to_gnu_type (gnat_equiv);
5667
5668 /* Access-to-unconstrained-array types need a special treatment. */
5669 if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep))
5670 {
5671 if (!TYPE_POINTER_TO (gnu_type))
5672 build_dummy_unc_pointer_types (gnat_equiv, gnu_type);
5673 }
5674
5675 return gnu_type;
5676 }
5677
5678 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5679 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5680 is true if we need to write debug information for other types that we may
5681 create in the process. Also set PARAM_LIST to the list of parameters. */
5682
5683 static tree
5684 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
5685 bool debug_info_p, tree *param_list)
5686 {
5687 const Entity_Kind kind = Ekind (gnat_subprog);
5688 Entity_Id gnat_return_type = Etype (gnat_subprog);
5689 Entity_Id gnat_param;
5690 tree gnu_return_type;
5691 tree gnu_param_type_list = NULL_TREE;
5692 tree gnu_param_list = NULL_TREE;
5693 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5694 (In Out or Out parameters not passed by reference), in which case it is
5695 the list of nodes used to specify the values of the In Out/Out parameters
5696 that are returned as a record upon procedure return. The TREE_PURPOSE of
5697 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5698 is the PARM_DECL corresponding to that field. This list will be saved in
5699 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5700 tree gnu_cico_list = NULL_TREE;
5701 /* Fields in return type of procedure with copy-in copy-out parameters. */
5702 tree gnu_field_list = NULL_TREE;
5703 /* The semantics of "pure" in Ada essentially matches that of "const"
5704 in the back-end. In particular, both properties are orthogonal to
5705 the "nothrow" property if the EH circuitry is explicit in the
5706 internal representation of the back-end. If we are to completely
5707 hide the EH circuitry from it, we need to declare that calls to pure
5708 Ada subprograms that can throw have side effects since they can
5709 trigger an "abnormal" transfer of control flow; thus they can be
5710 neither "const" nor "pure" in the back-end sense. */
5711 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_subprog));
5712 bool return_by_direct_ref_p = false;
5713 bool return_by_invisi_ref_p = false;
5714 bool return_unconstrained_p = false;
5715 bool incomplete_profile_p = false;
5716 unsigned int num;
5717
5718 /* Look into the return type and get its associated GCC tree. If it is not
5719 void, compute various flags for the subprogram type. */
5720 if (Ekind (gnat_return_type) == E_Void)
5721 gnu_return_type = void_type_node;
5722 else
5723 {
5724 gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type);
5725
5726 /* If this function returns by reference, make the actual return type
5727 the reference type and make a note of that. */
5728 if (Returns_By_Ref (gnat_subprog))
5729 {
5730 gnu_return_type = build_reference_type (gnu_return_type);
5731 return_by_direct_ref_p = true;
5732 }
5733
5734 /* If the return type is an unconstrained array type, the return value
5735 will be allocated on the secondary stack so the actual return type
5736 is the fat pointer type. */
5737 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
5738 {
5739 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5740 return_unconstrained_p = true;
5741 }
5742
5743 /* This is the same unconstrained array case, but for a dummy type. */
5744 else if (TYPE_REFERENCE_TO (gnu_return_type)
5745 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type)))
5746 {
5747 gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type);
5748 return_unconstrained_p = true;
5749 }
5750
5751 /* Likewise, if the return type requires a transient scope, the return
5752 value will also be allocated on the secondary stack so the actual
5753 return type is the reference type. */
5754 else if (Requires_Transient_Scope (gnat_return_type))
5755 {
5756 gnu_return_type = build_reference_type (gnu_return_type);
5757 return_unconstrained_p = true;
5758 }
5759
5760 /* If the Mechanism is By_Reference, ensure this function uses the
5761 target's by-invisible-reference mechanism, which may not be the
5762 same as above (e.g. it might be passing an extra parameter). */
5763 else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference)
5764 return_by_invisi_ref_p = true;
5765
5766 /* Likewise, if the return type is itself By_Reference. */
5767 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
5768 return_by_invisi_ref_p = true;
5769
5770 /* If the type is a padded type and the underlying type would not be
5771 passed by reference or the function has a foreign convention, return
5772 the underlying type. */
5773 else if (TYPE_IS_PADDING_P (gnu_return_type)
5774 && (!default_pass_by_ref
5775 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
5776 || Has_Foreign_Convention (gnat_subprog)))
5777 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
5778
5779 /* If the return type is unconstrained, it must have a maximum size.
5780 Use the padded type as the effective return type. And ensure the
5781 function uses the target's by-invisible-reference mechanism to
5782 avoid copying too much data when it returns. */
5783 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
5784 {
5785 tree orig_type = gnu_return_type;
5786 tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true);
5787
5788 /* If the size overflows to 0, set it to an arbitrary positive
5789 value so that assignments in the type are preserved. Their
5790 actual size is independent of this positive value. */
5791 if (TREE_CODE (max_return_size) == INTEGER_CST
5792 && TREE_OVERFLOW (max_return_size)
5793 && integer_zerop (max_return_size))
5794 {
5795 max_return_size = copy_node (bitsize_unit_node);
5796 TREE_OVERFLOW (max_return_size) = 1;
5797 }
5798
5799 gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size,
5800 0, gnat_subprog, false, false,
5801 definition, true);
5802
5803 /* Declare it now since it will never be declared otherwise. This
5804 is necessary to ensure that its subtrees are properly marked. */
5805 if (gnu_return_type != orig_type
5806 && !DECL_P (TYPE_NAME (gnu_return_type)))
5807 create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
5808 true, debug_info_p, gnat_subprog);
5809
5810 return_by_invisi_ref_p = true;
5811 }
5812
5813 /* If the return type has a size that overflows, we usually cannot have
5814 a function that returns that type. This usage doesn't really make
5815 sense anyway, so issue an error here. */
5816 if (!return_by_invisi_ref_p
5817 && TYPE_SIZE_UNIT (gnu_return_type)
5818 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
5819 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
5820 {
5821 post_error ("cannot return type whose size overflows", gnat_subprog);
5822 gnu_return_type = copy_type (gnu_return_type);
5823 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
5824 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
5825 }
5826
5827 /* If the return type is incomplete, there are 2 cases: if the function
5828 returns by reference, then the return type is only linked indirectly
5829 in the profile, so the profile can be seen as complete since it need
5830 not be further modified, only the reference types need be adjusted;
5831 otherwise the profile itself is incomplete and need be adjusted. */
5832 if (TYPE_IS_DUMMY_P (gnu_return_type))
5833 {
5834 associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
5835 incomplete_profile_p = true;
5836 }
5837
5838 if (kind == E_Function)
5839 Set_Mechanism (gnat_subprog, return_unconstrained_p
5840 || return_by_direct_ref_p
5841 || return_by_invisi_ref_p
5842 ? By_Reference : By_Copy);
5843 }
5844
5845 /* A procedure (something that doesn't return anything) shouldn't be
5846 considered const since there would be no reason for calling such a
5847 subprogram. Note that procedures with Out (or In Out) parameters
5848 have already been converted into a function with a return type.
5849 Similarly, if the function returns an unconstrained type, then the
5850 function will allocate the return value on the secondary stack and
5851 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5852 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
5853 const_flag = false;
5854
5855 /* Loop over the parameters and get their associated GCC tree. While doing
5856 this, build a copy-in copy-out structure if we need one. */
5857 for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0;
5858 Present (gnat_param);
5859 gnat_param = Next_Formal_With_Extras (gnat_param), num++)
5860 {
5861 Entity_Id gnat_param_type = Etype (gnat_param);
5862 tree gnu_param_name = get_entity_name (gnat_param);
5863 tree gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type);
5864 tree gnu_param, gnu_field;
5865 bool cico = false;
5866
5867 /* If the parameter type is incomplete, there are 2 cases: if it is
5868 passed by reference, then the type is only linked indirectly in
5869 the profile, so the profile can be seen as complete since it need
5870 not be further modified, only the reference types need be adjusted;
5871 otherwise the profile itself is incomplete and need be adjusted. */
5872 if (TYPE_IS_DUMMY_P (gnu_param_type))
5873 {
5874 Node_Id gnat_decl;
5875
5876 if (Mechanism (gnat_param) == By_Reference
5877 || (TYPE_REFERENCE_TO (gnu_param_type)
5878 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_param_type)))
5879 || TYPE_IS_BY_REFERENCE_P (gnu_param_type))
5880 {
5881 gnu_param_type = build_reference_type (gnu_param_type);
5882 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5883 TREE_READONLY (gnu_param) = 1;
5884 DECL_BY_REF_P (gnu_param) = 1;
5885 DECL_POINTS_TO_READONLY_P (gnu_param)
5886 = (Ekind (gnat_param) == E_In_Parameter
5887 && !Address_Taken (gnat_param));
5888 Set_Mechanism (gnat_param, By_Reference);
5889 Sloc_to_locus (Sloc (gnat_param),
5890 &DECL_SOURCE_LOCATION (gnu_param));
5891 }
5892
5893 /* ??? This is a kludge to support null procedures in spec taking a
5894 parameter with an untagged incomplete type coming from a limited
5895 context. The front-end creates a body without knowing anything
5896 about the non-limited view, which is illegal Ada and cannot be
5897 reasonably supported. Create a parameter with a fake type. */
5898 else if (kind == E_Procedure
5899 && (gnat_decl = Parent (gnat_subprog))
5900 && Nkind (gnat_decl) == N_Procedure_Specification
5901 && Null_Present (gnat_decl)
5902 && IN (Ekind (gnat_param_type), Incomplete_Kind))
5903 gnu_param = create_param_decl (gnu_param_name, ptr_type_node);
5904
5905 else
5906 {
5907 gnu_param = create_param_decl (gnu_param_name, gnu_param_type);
5908 associate_subprog_with_dummy_type (gnat_subprog, gnu_param_type);
5909 incomplete_profile_p = true;
5910 }
5911 }
5912
5913 else
5914 {
5915 gnu_param
5916 = gnat_to_gnu_param (gnat_param, num == 0, gnat_subprog, &cico);
5917
5918 /* We are returned either a PARM_DECL or a type if no parameter
5919 needs to be passed; in either case, adjust the type. */
5920 if (DECL_P (gnu_param))
5921 gnu_param_type = TREE_TYPE (gnu_param);
5922 else
5923 {
5924 gnu_param_type = gnu_param;
5925 gnu_param = NULL_TREE;
5926 }
5927 }
5928
5929 /* If we built a GCC tree for the parameter, register it. */
5930 if (gnu_param)
5931 {
5932 gnu_param_type_list
5933 = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list);
5934 gnu_param_list = chainon (gnu_param, gnu_param_list);
5935 save_gnu_tree (gnat_param, NULL_TREE, false);
5936 save_gnu_tree (gnat_param, gnu_param, false);
5937
5938 /* If a parameter is a pointer, a function may modify memory through
5939 it and thus shouldn't be considered a const function. Also, the
5940 memory may be modified between two calls, so they can't be CSE'ed.
5941 The latter case also handles by-ref parameters. */
5942 if (POINTER_TYPE_P (gnu_param_type)
5943 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
5944 const_flag = false;
5945 }
5946
5947 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5948 for it in the return type and register the association. */
5949 if (cico && !incomplete_profile_p)
5950 {
5951 if (!gnu_cico_list)
5952 {
5953 tree gnu_new_ret_type = make_node (RECORD_TYPE);
5954
5955 /* If this is a function, we also need a field for the
5956 return value to be placed. */
5957 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
5958 {
5959 gnu_field
5960 = create_field_decl (get_identifier ("RETVAL"),
5961 gnu_return_type,
5962 gnu_new_ret_type, NULL_TREE,
5963 NULL_TREE, 0, 0);
5964 Sloc_to_locus (Sloc (gnat_subprog),
5965 &DECL_SOURCE_LOCATION (gnu_field));
5966 gnu_field_list = gnu_field;
5967 gnu_cico_list
5968 = tree_cons (gnu_field, void_type_node, NULL_TREE);
5969 }
5970
5971 gnu_return_type = gnu_new_ret_type;
5972 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
5973 /* Set a default alignment to speed up accesses. But we should
5974 not increase the size of the structure too much, lest it does
5975 not fit in return registers anymore. */
5976 SET_TYPE_ALIGN (gnu_return_type, get_mode_alignment (ptr_mode));
5977 }
5978
5979 gnu_field
5980 = create_field_decl (gnu_param_name, gnu_param_type,
5981 gnu_return_type, NULL_TREE, NULL_TREE, 0, 0);
5982 Sloc_to_locus (Sloc (gnat_param),
5983 &DECL_SOURCE_LOCATION (gnu_field));
5984 DECL_CHAIN (gnu_field) = gnu_field_list;
5985 gnu_field_list = gnu_field;
5986 gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list);
5987 }
5988 }
5989
5990 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5991 and finish up the return type. */
5992 if (gnu_cico_list && !incomplete_profile_p)
5993 {
5994 /* If we have a CICO list but it has only one entry, we convert
5995 this function into a function that returns this object. */
5996 if (list_length (gnu_cico_list) == 1)
5997 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
5998
5999 /* Do not finalize the return type if the subprogram is stubbed
6000 since structures are incomplete for the back-end. */
6001 else if (Convention (gnat_subprog) != Convention_Stubbed)
6002 {
6003 finish_record_type (gnu_return_type, nreverse (gnu_field_list), 0,
6004 false);
6005
6006 /* Try to promote the mode of the return type if it is passed
6007 in registers, again to speed up accesses. */
6008 if (TYPE_MODE (gnu_return_type) == BLKmode
6009 && !targetm.calls.return_in_memory (gnu_return_type, NULL_TREE))
6010 {
6011 unsigned int size
6012 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
6013 unsigned int i = BITS_PER_UNIT;
6014 machine_mode mode;
6015
6016 while (i < size)
6017 i <<= 1;
6018 mode = mode_for_size (i, MODE_INT, 0);
6019 if (mode != BLKmode)
6020 {
6021 SET_TYPE_MODE (gnu_return_type, mode);
6022 SET_TYPE_ALIGN (gnu_return_type, GET_MODE_ALIGNMENT (mode));
6023 TYPE_SIZE (gnu_return_type)
6024 = bitsize_int (GET_MODE_BITSIZE (mode));
6025 TYPE_SIZE_UNIT (gnu_return_type)
6026 = size_int (GET_MODE_SIZE (mode));
6027 }
6028 }
6029
6030 if (debug_info_p)
6031 rest_of_record_type_compilation (gnu_return_type);
6032 }
6033 }
6034
6035 /* The lists have been built in reverse. */
6036 gnu_param_type_list = nreverse (gnu_param_type_list);
6037 gnu_param_type_list = chainon (gnu_param_type_list, void_list_node);
6038 *param_list = nreverse (gnu_param_list);
6039 gnu_cico_list = nreverse (gnu_cico_list);
6040
6041 /* If the profile is incomplete, we only set the (temporary) return and
6042 parameter types; otherwise, we build the full type. In either case,
6043 we reuse an already existing GCC tree that we built previously here. */
6044 tree gnu_type = present_gnu_tree (gnat_subprog)
6045 ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE;
6046
6047 if (incomplete_profile_p)
6048 {
6049 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
6050 ;
6051 else
6052 gnu_type = make_node (FUNCTION_TYPE);
6053 TREE_TYPE (gnu_type) = gnu_return_type;
6054 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6055 }
6056 else
6057 {
6058 if (gnu_type && TREE_CODE (gnu_type) == FUNCTION_TYPE)
6059 {
6060 TREE_TYPE (gnu_type) = gnu_return_type;
6061 TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list;
6062 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6063 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6064 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6065 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6066 TYPE_CANONICAL (gnu_type) = gnu_type;
6067 layout_type (gnu_type);
6068 }
6069 else
6070 {
6071 gnu_type
6072 = build_function_type (gnu_return_type, gnu_param_type_list);
6073
6074 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
6075 has a different TYPE_CI_CO_LIST or flags. */
6076 if (!fntype_same_flags_p (gnu_type, gnu_cico_list,
6077 return_unconstrained_p,
6078 return_by_direct_ref_p,
6079 return_by_invisi_ref_p))
6080 {
6081 gnu_type = copy_type (gnu_type);
6082 TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list;
6083 TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p;
6084 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p;
6085 TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p;
6086 }
6087 }
6088
6089 if (const_flag)
6090 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
6091
6092 if (No_Return (gnat_subprog))
6093 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
6094 }
6095
6096 return gnu_type;
6097 }
6098
6099 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6100 qualifiers on TYPE. */
6101
6102 static tree
6103 change_qualified_type (tree type, int type_quals)
6104 {
6105 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6106 }
6107
6108 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6109
6110 static bool
6111 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6112 {
6113 while (Present (Corresponding_Discriminant (discr1)))
6114 discr1 = Corresponding_Discriminant (discr1);
6115
6116 while (Present (Corresponding_Discriminant (discr2)))
6117 discr2 = Corresponding_Discriminant (discr2);
6118
6119 return
6120 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6121 }
6122
6123 /* Return true if the array type GNU_TYPE, which represents a dimension of
6124 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6125
6126 static bool
6127 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6128 {
6129 /* If the array type is not the innermost dimension of the GNAT type,
6130 then it has a non-aliased component. */
6131 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6132 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6133 return true;
6134
6135 /* If the array type has an aliased component in the front-end sense,
6136 then it also has an aliased component in the back-end sense. */
6137 if (Has_Aliased_Components (gnat_type))
6138 return false;
6139
6140 /* If this is a derived type, then it has a non-aliased component if
6141 and only if its parent type also has one. */
6142 if (Is_Derived_Type (gnat_type))
6143 {
6144 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6145 int index;
6146 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6147 gnu_parent_type
6148 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6149 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6150 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6151 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6152 }
6153
6154 /* Otherwise, rely exclusively on properties of the element type. */
6155 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6156 }
6157
6158 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6159
6160 static bool
6161 compile_time_known_address_p (Node_Id gnat_address)
6162 {
6163 /* Catch System'To_Address. */
6164 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6165 gnat_address = Expression (gnat_address);
6166
6167 return Compile_Time_Known_Value (gnat_address);
6168 }
6169
6170 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6171 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6172
6173 static bool
6174 cannot_be_superflat (Node_Id gnat_range)
6175 {
6176 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6177 Node_Id scalar_range;
6178 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6179
6180 /* If the low bound is not constant, try to find an upper bound. */
6181 while (Nkind (gnat_lb) != N_Integer_Literal
6182 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6183 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6184 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6185 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6186 || Nkind (scalar_range) == N_Range))
6187 gnat_lb = High_Bound (scalar_range);
6188
6189 /* If the high bound is not constant, try to find a lower bound. */
6190 while (Nkind (gnat_hb) != N_Integer_Literal
6191 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6192 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6193 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6194 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6195 || Nkind (scalar_range) == N_Range))
6196 gnat_hb = Low_Bound (scalar_range);
6197
6198 /* If we have failed to find constant bounds, punt. */
6199 if (Nkind (gnat_lb) != N_Integer_Literal
6200 || Nkind (gnat_hb) != N_Integer_Literal)
6201 return false;
6202
6203 /* We need at least a signed 64-bit type to catch most cases. */
6204 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6205 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6206 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6207 return false;
6208
6209 /* If the low bound is the smallest integer, nothing can be smaller. */
6210 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6211 if (TREE_OVERFLOW (gnu_lb_minus_one))
6212 return true;
6213
6214 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6215 }
6216
6217 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6218
6219 static bool
6220 constructor_address_p (tree gnu_expr)
6221 {
6222 while (TREE_CODE (gnu_expr) == NOP_EXPR
6223 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6224 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6225 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6226
6227 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6228 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6229 }
6230
6231 /* Return true if the size in units represented by GNU_SIZE can be handled by
6232 an allocation. If STATIC_P is true, consider only what can be done with a
6233 static allocation. */
6234
6235 static bool
6236 allocatable_size_p (tree gnu_size, bool static_p)
6237 {
6238 /* We can allocate a fixed size if it is a valid for the middle-end. */
6239 if (TREE_CODE (gnu_size) == INTEGER_CST)
6240 return valid_constant_size_p (gnu_size);
6241
6242 /* We can allocate a variable size if this isn't a static allocation. */
6243 else
6244 return !static_p;
6245 }
6246
6247 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6248 initial value of an object of GNU_TYPE. */
6249
6250 static bool
6251 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6252 {
6253 /* Do not convert if the object's type is unconstrained because this would
6254 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6255 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6256 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6257 return false;
6258
6259 /* Do not convert if the object's type is a padding record whose field is of
6260 self-referential size because we want to copy only the actual data. */
6261 if (type_is_padding_self_referential (gnu_type))
6262 return false;
6263
6264 /* Do not convert a call to a function that returns with variable size since
6265 we want to use the return slot optimization in this case. */
6266 if (TREE_CODE (gnu_expr) == CALL_EXPR
6267 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6268 return false;
6269
6270 /* Do not convert to a record type with a variant part from a record type
6271 without one, to keep the object simpler. */
6272 if (TREE_CODE (gnu_type) == RECORD_TYPE
6273 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6274 && get_variant_part (gnu_type)
6275 && !get_variant_part (TREE_TYPE (gnu_expr)))
6276 return false;
6277
6278 /* In all the other cases, convert the expression to the object's type. */
6279 return true;
6280 }
6281 \f
6282 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6283 be elaborated at the point of its definition, but do nothing else. */
6284
6285 void
6286 elaborate_entity (Entity_Id gnat_entity)
6287 {
6288 switch (Ekind (gnat_entity))
6289 {
6290 case E_Signed_Integer_Subtype:
6291 case E_Modular_Integer_Subtype:
6292 case E_Enumeration_Subtype:
6293 case E_Ordinary_Fixed_Point_Subtype:
6294 case E_Decimal_Fixed_Point_Subtype:
6295 case E_Floating_Point_Subtype:
6296 {
6297 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6298 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6299
6300 /* ??? Tests to avoid Constraint_Error in static expressions
6301 are needed until after the front stops generating bogus
6302 conversions on bounds of real types. */
6303 if (!Raises_Constraint_Error (gnat_lb))
6304 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6305 Needs_Debug_Info (gnat_entity));
6306 if (!Raises_Constraint_Error (gnat_hb))
6307 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6308 Needs_Debug_Info (gnat_entity));
6309 break;
6310 }
6311
6312 case E_Record_Subtype:
6313 case E_Private_Subtype:
6314 case E_Limited_Private_Subtype:
6315 case E_Record_Subtype_With_Private:
6316 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6317 {
6318 Node_Id gnat_discriminant_expr;
6319 Entity_Id gnat_field;
6320
6321 for (gnat_field
6322 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6323 gnat_discriminant_expr
6324 = First_Elmt (Discriminant_Constraint (gnat_entity));
6325 Present (gnat_field);
6326 gnat_field = Next_Discriminant (gnat_field),
6327 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6328 /* Ignore access discriminants. */
6329 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6330 elaborate_expression (Node (gnat_discriminant_expr),
6331 gnat_entity, get_entity_char (gnat_field),
6332 true, false, false);
6333 }
6334 break;
6335
6336 }
6337 }
6338 \f
6339 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6340 NAME, ARGS and ERROR_POINT. */
6341
6342 static void
6343 prepend_one_attribute (struct attrib **attr_list,
6344 enum attrib_type attrib_type,
6345 tree attr_name,
6346 tree attr_args,
6347 Node_Id attr_error_point)
6348 {
6349 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6350
6351 attr->type = attrib_type;
6352 attr->name = attr_name;
6353 attr->args = attr_args;
6354 attr->error_point = attr_error_point;
6355
6356 attr->next = *attr_list;
6357 *attr_list = attr;
6358 }
6359
6360 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6361
6362 static void
6363 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6364 {
6365 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6366 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6367 enum attrib_type etype;
6368
6369 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6370 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6371 {
6372 case Pragma_Machine_Attribute:
6373 etype = ATTR_MACHINE_ATTRIBUTE;
6374 break;
6375
6376 case Pragma_Linker_Alias:
6377 etype = ATTR_LINK_ALIAS;
6378 break;
6379
6380 case Pragma_Linker_Section:
6381 etype = ATTR_LINK_SECTION;
6382 break;
6383
6384 case Pragma_Linker_Constructor:
6385 etype = ATTR_LINK_CONSTRUCTOR;
6386 break;
6387
6388 case Pragma_Linker_Destructor:
6389 etype = ATTR_LINK_DESTRUCTOR;
6390 break;
6391
6392 case Pragma_Weak_External:
6393 etype = ATTR_WEAK_EXTERNAL;
6394 break;
6395
6396 case Pragma_Thread_Local_Storage:
6397 etype = ATTR_THREAD_LOCAL_STORAGE;
6398 break;
6399
6400 default:
6401 return;
6402 }
6403
6404 /* See what arguments we have and turn them into GCC trees for attribute
6405 handlers. These expect identifier for strings. We handle at most two
6406 arguments and static expressions only. */
6407 if (Present (gnat_arg) && Present (First (gnat_arg)))
6408 {
6409 Node_Id gnat_arg0 = Next (First (gnat_arg));
6410 Node_Id gnat_arg1 = Empty;
6411
6412 if (Present (gnat_arg0)
6413 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6414 {
6415 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6416
6417 if (TREE_CODE (gnu_arg0) == STRING_CST)
6418 {
6419 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6420 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6421 return;
6422 }
6423
6424 gnat_arg1 = Next (gnat_arg0);
6425 }
6426
6427 if (Present (gnat_arg1)
6428 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6429 {
6430 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6431
6432 if (TREE_CODE (gnu_arg1) == STRING_CST)
6433 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6434 }
6435 }
6436
6437 /* Prepend to the list. Make a list of the argument we might have, as GCC
6438 expects it. */
6439 prepend_one_attribute (attr_list, etype, gnu_arg0,
6440 gnu_arg1
6441 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6442 Present (Next (First (gnat_arg)))
6443 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6444 }
6445
6446 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6447
6448 static void
6449 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6450 {
6451 Node_Id gnat_temp;
6452
6453 /* Attributes are stored as Representation Item pragmas. */
6454 for (gnat_temp = First_Rep_Item (gnat_entity);
6455 Present (gnat_temp);
6456 gnat_temp = Next_Rep_Item (gnat_temp))
6457 if (Nkind (gnat_temp) == N_Pragma)
6458 prepend_one_attribute_pragma (attr_list, gnat_temp);
6459 }
6460 \f
6461 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6462 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6463 return the GCC tree to use for that expression. S is the suffix to use
6464 if a variable needs to be created and DEFINITION is true if this is done
6465 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6466 otherwise, we are just elaborating the expression for side-effects. If
6467 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6468 isn't needed for code generation. */
6469
6470 static tree
6471 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6472 bool definition, bool need_value, bool need_debug)
6473 {
6474 tree gnu_expr;
6475
6476 /* If we already elaborated this expression (e.g. it was involved
6477 in the definition of a private type), use the old value. */
6478 if (present_gnu_tree (gnat_expr))
6479 return get_gnu_tree (gnat_expr);
6480
6481 /* If we don't need a value and this is static or a discriminant,
6482 we don't need to do anything. */
6483 if (!need_value
6484 && (Is_OK_Static_Expression (gnat_expr)
6485 || (Nkind (gnat_expr) == N_Identifier
6486 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6487 return NULL_TREE;
6488
6489 /* If it's a static expression, we don't need a variable for debugging. */
6490 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6491 need_debug = false;
6492
6493 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6494 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6495 definition, need_debug);
6496
6497 /* Save the expression in case we try to elaborate this entity again. Since
6498 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6499 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6500 save_gnu_tree (gnat_expr, gnu_expr, true);
6501
6502 return need_value ? gnu_expr : error_mark_node;
6503 }
6504
6505 /* Similar, but take a GNU expression and always return a result. */
6506
6507 static tree
6508 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6509 bool definition, bool need_debug)
6510 {
6511 const bool expr_public_p = Is_Public (gnat_entity);
6512 const bool expr_global_p = expr_public_p || global_bindings_p ();
6513 bool expr_variable_p, use_variable;
6514
6515 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6516 that an expression cannot contain both a discriminant and a variable. */
6517 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6518 return gnu_expr;
6519
6520 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6521 a variable that is initialized to contain the expression when the package
6522 containing the definition is elaborated. If this entity is defined at top
6523 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6524 if this is necessary. */
6525 if (TREE_CONSTANT (gnu_expr))
6526 expr_variable_p = false;
6527 else
6528 {
6529 /* Skip any conversions and simple constant arithmetics to see if the
6530 expression is based on a read-only variable. */
6531 tree inner = remove_conversions (gnu_expr, true);
6532
6533 inner = skip_simple_constant_arithmetic (inner);
6534
6535 if (handled_component_p (inner))
6536 inner = get_inner_constant_reference (inner);
6537
6538 expr_variable_p
6539 = !(inner
6540 && TREE_CODE (inner) == VAR_DECL
6541 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6542 }
6543
6544 /* We only need to use the variable if we are in a global context since GCC
6545 can do the right thing in the local case. However, when not optimizing,
6546 use it for bounds of loop iteration scheme to avoid code duplication. */
6547 use_variable = expr_variable_p
6548 && (expr_global_p
6549 || (!optimize
6550 && definition
6551 && Is_Itype (gnat_entity)
6552 && Nkind (Associated_Node_For_Itype (gnat_entity))
6553 == N_Loop_Parameter_Specification));
6554
6555 /* Now create it, possibly only for debugging purposes. */
6556 if (use_variable || need_debug)
6557 {
6558 /* The following variable creation can happen when processing the body
6559 of subprograms that are defined out of the extended main unit and
6560 inlined. In this case, we are not at the global scope, and thus the
6561 new variable must not be tagged "external", as we used to do here as
6562 soon as DEFINITION was false. */
6563 tree gnu_decl
6564 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6565 TREE_TYPE (gnu_expr), gnu_expr, true,
6566 expr_public_p, !definition && expr_global_p,
6567 expr_global_p, false, true, need_debug,
6568 NULL, gnat_entity);
6569
6570 /* Using this variable at debug time (if need_debug is true) requires a
6571 proper location. The back-end will compute a location for this
6572 variable only if the variable is used by the generated code.
6573 Returning the variable ensures the caller will use it in generated
6574 code. Note that there is no need for a location if the debug info
6575 contains an integer constant.
6576 TODO: when the encoding-based debug scheme is dropped, move this
6577 condition to the top-level IF block: we will not need to create a
6578 variable anymore in such cases, then. */
6579 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6580 return gnu_decl;
6581 }
6582
6583 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6584 }
6585
6586 /* Similar, but take an alignment factor and make it explicit in the tree. */
6587
6588 static tree
6589 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6590 bool definition, bool need_debug, unsigned int align)
6591 {
6592 tree unit_align = size_int (align / BITS_PER_UNIT);
6593 return
6594 size_binop (MULT_EXPR,
6595 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6596 gnu_expr,
6597 unit_align),
6598 gnat_entity, s, definition,
6599 need_debug),
6600 unit_align);
6601 }
6602
6603 /* Structure to hold internal data for elaborate_reference. */
6604
6605 struct er_data
6606 {
6607 Entity_Id entity;
6608 bool definition;
6609 unsigned int n;
6610 };
6611
6612 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6613
6614 static tree
6615 elaborate_reference_1 (tree ref, void *data)
6616 {
6617 struct er_data *er = (struct er_data *)data;
6618 char suffix[16];
6619
6620 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6621 if (TREE_CONSTANT (ref))
6622 return ref;
6623
6624 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6625 pointer. This may be more efficient, but will also allow us to more
6626 easily find the match for the PLACEHOLDER_EXPR. */
6627 if (TREE_CODE (ref) == COMPONENT_REF
6628 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6629 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6630 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6631 TREE_OPERAND (ref, 1), NULL_TREE);
6632
6633 sprintf (suffix, "EXP%d", ++er->n);
6634 return
6635 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6636 }
6637
6638 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6639 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6640 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6641
6642 static tree
6643 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6644 tree *init)
6645 {
6646 struct er_data er = { gnat_entity, definition, 0 };
6647 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6648 }
6649 \f
6650 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6651 the value passed against the list of choices. */
6652
6653 tree
6654 choices_to_gnu (tree operand, Node_Id choices)
6655 {
6656 Node_Id choice;
6657 Node_Id gnat_temp;
6658 tree result = boolean_false_node;
6659 tree this_test, low = 0, high = 0, single = 0;
6660
6661 for (choice = First (choices); Present (choice); choice = Next (choice))
6662 {
6663 switch (Nkind (choice))
6664 {
6665 case N_Range:
6666 low = gnat_to_gnu (Low_Bound (choice));
6667 high = gnat_to_gnu (High_Bound (choice));
6668
6669 this_test
6670 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6671 build_binary_op (GE_EXPR, boolean_type_node,
6672 operand, low),
6673 build_binary_op (LE_EXPR, boolean_type_node,
6674 operand, high));
6675
6676 break;
6677
6678 case N_Subtype_Indication:
6679 gnat_temp = Range_Expression (Constraint (choice));
6680 low = gnat_to_gnu (Low_Bound (gnat_temp));
6681 high = gnat_to_gnu (High_Bound (gnat_temp));
6682
6683 this_test
6684 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6685 build_binary_op (GE_EXPR, boolean_type_node,
6686 operand, low),
6687 build_binary_op (LE_EXPR, boolean_type_node,
6688 operand, high));
6689 break;
6690
6691 case N_Identifier:
6692 case N_Expanded_Name:
6693 /* This represents either a subtype range, an enumeration
6694 literal, or a constant Ekind says which. If an enumeration
6695 literal or constant, fall through to the next case. */
6696 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6697 && Ekind (Entity (choice)) != E_Constant)
6698 {
6699 tree type = gnat_to_gnu_type (Entity (choice));
6700
6701 low = TYPE_MIN_VALUE (type);
6702 high = TYPE_MAX_VALUE (type);
6703
6704 this_test
6705 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6706 build_binary_op (GE_EXPR, boolean_type_node,
6707 operand, low),
6708 build_binary_op (LE_EXPR, boolean_type_node,
6709 operand, high));
6710 break;
6711 }
6712
6713 /* ... fall through ... */
6714
6715 case N_Character_Literal:
6716 case N_Integer_Literal:
6717 single = gnat_to_gnu (choice);
6718 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6719 single);
6720 break;
6721
6722 case N_Others_Choice:
6723 this_test = boolean_true_node;
6724 break;
6725
6726 default:
6727 gcc_unreachable ();
6728 }
6729
6730 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6731 this_test);
6732 }
6733
6734 return result;
6735 }
6736 \f
6737 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6738 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6739
6740 static int
6741 adjust_packed (tree field_type, tree record_type, int packed)
6742 {
6743 /* If the field contains an item of variable size, we cannot pack it
6744 because we cannot create temporaries of non-fixed size in case
6745 we need to take the address of the field. See addressable_p and
6746 the notes on the addressability issues for further details. */
6747 if (type_has_variable_size (field_type))
6748 return 0;
6749
6750 /* In the other cases, we can honor the packing. */
6751 if (packed)
6752 return packed;
6753
6754 /* If the alignment of the record is specified and the field type
6755 is over-aligned, request Storage_Unit alignment for the field. */
6756 if (TYPE_ALIGN (record_type)
6757 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6758 return -1;
6759
6760 /* Likewise if the maximum alignment of the record is specified. */
6761 if (TYPE_MAX_ALIGN (record_type)
6762 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6763 return -1;
6764
6765 return 0;
6766 }
6767
6768 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6769 placed in GNU_RECORD_TYPE.
6770
6771 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6772 record has Component_Alignment of Storage_Unit.
6773
6774 DEFINITION is true if this field is for a record being defined.
6775
6776 DEBUG_INFO_P is true if we need to write debug information for types
6777 that we may create in the process. */
6778
6779 static tree
6780 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6781 bool definition, bool debug_info_p)
6782 {
6783 const Entity_Id gnat_field_type = Etype (gnat_field);
6784 const bool is_aliased
6785 = Is_Aliased (gnat_field);
6786 const bool is_atomic
6787 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6788 const bool is_independent
6789 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6790 const bool is_volatile
6791 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6792 const bool needs_strict_alignment
6793 = (is_aliased
6794 || is_independent
6795 || is_volatile
6796 || Strict_Alignment (gnat_field_type));
6797 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6798 tree gnu_field_id = get_entity_name (gnat_field);
6799 tree gnu_field, gnu_size, gnu_pos;
6800
6801 /* If this field requires strict alignment, we cannot pack it because
6802 it would very likely be under-aligned in the record. */
6803 if (needs_strict_alignment)
6804 packed = 0;
6805 else
6806 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6807
6808 /* If a size is specified, use it. Otherwise, if the record type is packed,
6809 use the official RM size. See "Handling of Type'Size Values" in Einfo
6810 for further details. */
6811 if (Known_Esize (gnat_field))
6812 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6813 gnat_field, FIELD_DECL, false, true);
6814 else if (packed == 1)
6815 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6816 gnat_field, FIELD_DECL, false, true);
6817 else
6818 gnu_size = NULL_TREE;
6819
6820 /* If we have a specified size that is smaller than that of the field's type,
6821 or a position is specified, and the field's type is a record that doesn't
6822 require strict alignment, see if we can get either an integral mode form
6823 of the type or a smaller form. If we can, show a size was specified for
6824 the field if there wasn't one already, so we know to make this a bitfield
6825 and avoid making things wider.
6826
6827 Changing to an integral mode form is useful when the record is packed as
6828 we can then place the field at a non-byte-aligned position and so achieve
6829 tighter packing. This is in addition required if the field shares a byte
6830 with another field and the front-end lets the back-end handle the access
6831 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6832
6833 Changing to a smaller form is required if the specified size is smaller
6834 than that of the field's type and the type contains sub-fields that are
6835 padded, in order to avoid generating accesses to these sub-fields that
6836 are wider than the field.
6837
6838 We avoid the transformation if it is not required or potentially useful,
6839 as it might entail an increase of the field's alignment and have ripple
6840 effects on the outer record type. A typical case is a field known to be
6841 byte-aligned and not to share a byte with another field. */
6842 if (!needs_strict_alignment
6843 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6844 && !TYPE_FAT_POINTER_P (gnu_field_type)
6845 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6846 && (packed == 1
6847 || (gnu_size
6848 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6849 || (Present (Component_Clause (gnat_field))
6850 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6851 % BITS_PER_UNIT == 0
6852 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6853 {
6854 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6855 if (gnu_packable_type != gnu_field_type)
6856 {
6857 gnu_field_type = gnu_packable_type;
6858 if (!gnu_size)
6859 gnu_size = rm_size (gnu_field_type);
6860 }
6861 }
6862
6863 if (Is_Atomic_Or_VFA (gnat_field))
6864 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6865
6866 if (Present (Component_Clause (gnat_field)))
6867 {
6868 Node_Id gnat_clause = Component_Clause (gnat_field);
6869 Entity_Id gnat_parent
6870 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6871
6872 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6873 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6874 gnat_field, FIELD_DECL, false, true);
6875
6876 /* Ensure the position does not overlap with the parent subtype, if there
6877 is one. This test is omitted if the parent of the tagged type has a
6878 full rep clause since, in this case, component clauses are allowed to
6879 overlay the space allocated for the parent type and the front-end has
6880 checked that there are no overlapping components. */
6881 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6882 {
6883 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6884
6885 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6886 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6887 post_error_ne_tree
6888 ("offset of& must be beyond parent{, minimum allowed is ^}",
6889 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6890 }
6891
6892 /* If this field needs strict alignment, make sure that the record is
6893 sufficiently aligned and that the position and size are consistent
6894 with the type. But don't do it if we are just annotating types and
6895 the field's type is tagged, since tagged types aren't fully laid out
6896 in this mode. Also, note that atomic implies volatile so the inner
6897 test sequences ordering is significant here. */
6898 if (needs_strict_alignment
6899 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6900 {
6901 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6902
6903 if (TYPE_ALIGN (gnu_record_type) < type_align)
6904 SET_TYPE_ALIGN (gnu_record_type, type_align);
6905
6906 /* If the position is not a multiple of the alignment of the type,
6907 then error out and reset the position. */
6908 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6909 bitsize_int (type_align))))
6910 {
6911 const char *s;
6912
6913 if (is_atomic)
6914 s = "position of atomic field& must be multiple of ^ bits";
6915 else if (is_aliased)
6916 s = "position of aliased field& must be multiple of ^ bits";
6917 else if (is_independent)
6918 s = "position of independent field& must be multiple of ^ bits";
6919 else if (is_volatile)
6920 s = "position of volatile field& must be multiple of ^ bits";
6921 else if (Strict_Alignment (gnat_field_type))
6922 s = "position of & with aliased or tagged part must be"
6923 " multiple of ^ bits";
6924 else
6925 gcc_unreachable ();
6926
6927 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6928 type_align);
6929 gnu_pos = NULL_TREE;
6930 }
6931
6932 if (gnu_size)
6933 {
6934 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6935 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6936
6937 /* If the size is lower than that of the type, or greater for
6938 atomic and aliased, then error out and reset the size. */
6939 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6940 {
6941 const char *s;
6942
6943 if (is_atomic)
6944 s = "size of atomic field& must be ^ bits";
6945 else if (is_aliased)
6946 s = "size of aliased field& must be ^ bits";
6947 else if (is_independent)
6948 s = "size of independent field& must be at least ^ bits";
6949 else if (is_volatile)
6950 s = "size of volatile field& must be at least ^ bits";
6951 else if (Strict_Alignment (gnat_field_type))
6952 s = "size of & with aliased or tagged part must be"
6953 " at least ^ bits";
6954 else
6955 gcc_unreachable ();
6956
6957 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6958 gnu_type_size);
6959 gnu_size = NULL_TREE;
6960 }
6961
6962 /* Likewise if the size is not a multiple of a byte, */
6963 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6964 bitsize_unit_node)))
6965 {
6966 const char *s;
6967
6968 if (is_independent)
6969 s = "size of independent field& must be multiple of"
6970 " Storage_Unit";
6971 else if (is_volatile)
6972 s = "size of volatile field& must be multiple of"
6973 " Storage_Unit";
6974 else if (Strict_Alignment (gnat_field_type))
6975 s = "size of & with aliased or tagged part must be"
6976 " multiple of Storage_Unit";
6977 else
6978 gcc_unreachable ();
6979
6980 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6981 gnu_size = NULL_TREE;
6982 }
6983 }
6984 }
6985 }
6986
6987 /* If the record has rep clauses and this is the tag field, make a rep
6988 clause for it as well. */
6989 else if (Has_Specified_Layout (Scope (gnat_field))
6990 && Chars (gnat_field) == Name_uTag)
6991 {
6992 gnu_pos = bitsize_zero_node;
6993 gnu_size = TYPE_SIZE (gnu_field_type);
6994 }
6995
6996 else
6997 {
6998 gnu_pos = NULL_TREE;
6999
7000 /* If we are packing the record and the field is BLKmode, round the
7001 size up to a byte boundary. */
7002 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7003 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7004 }
7005
7006 /* We need to make the size the maximum for the type if it is
7007 self-referential and an unconstrained type. In that case, we can't
7008 pack the field since we can't make a copy to align it. */
7009 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7010 && !gnu_size
7011 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7012 && !Is_Constrained (Underlying_Type (gnat_field_type)))
7013 {
7014 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7015 packed = 0;
7016 }
7017
7018 /* If a size is specified, adjust the field's type to it. */
7019 if (gnu_size)
7020 {
7021 tree orig_field_type;
7022
7023 /* If the field's type is justified modular, we would need to remove
7024 the wrapper to (better) meet the layout requirements. However we
7025 can do so only if the field is not aliased to preserve the unique
7026 layout and if the prescribed size is not greater than that of the
7027 packed array to preserve the justification. */
7028 if (!needs_strict_alignment
7029 && TREE_CODE (gnu_field_type) == RECORD_TYPE
7030 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7031 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7032 <= 0)
7033 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7034
7035 /* Similarly if the field's type is a misaligned integral type, but
7036 there is no restriction on the size as there is no justification. */
7037 if (!needs_strict_alignment
7038 && TYPE_IS_PADDING_P (gnu_field_type)
7039 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
7040 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7041
7042 gnu_field_type
7043 = make_type_from_size (gnu_field_type, gnu_size,
7044 Has_Biased_Representation (gnat_field));
7045
7046 orig_field_type = gnu_field_type;
7047 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7048 false, false, definition, true);
7049
7050 /* If a padding record was made, declare it now since it will never be
7051 declared otherwise. This is necessary to ensure that its subtrees
7052 are properly marked. */
7053 if (gnu_field_type != orig_field_type
7054 && !DECL_P (TYPE_NAME (gnu_field_type)))
7055 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
7056 debug_info_p, gnat_field);
7057 }
7058
7059 /* Otherwise (or if there was an error), don't specify a position. */
7060 else
7061 gnu_pos = NULL_TREE;
7062
7063 /* If the field's type is a padded type made for a scalar field of a record
7064 type with reverse storage order, we need to propagate the reverse storage
7065 order to the padding type since it is the innermost enclosing aggregate
7066 type around the scalar. */
7067 if (TYPE_IS_PADDING_P (gnu_field_type)
7068 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
7069 && Is_Scalar_Type (gnat_field_type))
7070 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
7071
7072 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7073 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7074
7075 /* Now create the decl for the field. */
7076 gnu_field
7077 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7078 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
7079 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7080 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
7081 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
7082
7083 if (Ekind (gnat_field) == E_Discriminant)
7084 {
7085 DECL_INVARIANT_P (gnu_field)
7086 = No (Discriminant_Default_Value (gnat_field));
7087 DECL_DISCRIMINANT_NUMBER (gnu_field)
7088 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7089 }
7090
7091 return gnu_field;
7092 }
7093 \f
7094 /* Return true if at least one member of COMPONENT_LIST needs strict
7095 alignment. */
7096
7097 static bool
7098 components_need_strict_alignment (Node_Id component_list)
7099 {
7100 Node_Id component_decl;
7101
7102 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7103 Present (component_decl);
7104 component_decl = Next_Non_Pragma (component_decl))
7105 {
7106 Entity_Id gnat_field = Defining_Entity (component_decl);
7107
7108 if (Is_Aliased (gnat_field))
7109 return true;
7110
7111 if (Strict_Alignment (Etype (gnat_field)))
7112 return true;
7113 }
7114
7115 return false;
7116 }
7117
7118 /* Return true if TYPE is a type with variable size or a padding type with a
7119 field of variable size or a record that has a field with such a type. */
7120
7121 static bool
7122 type_has_variable_size (tree type)
7123 {
7124 tree field;
7125
7126 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7127 return true;
7128
7129 if (TYPE_IS_PADDING_P (type)
7130 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7131 return true;
7132
7133 if (!RECORD_OR_UNION_TYPE_P (type))
7134 return false;
7135
7136 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7137 if (type_has_variable_size (TREE_TYPE (field)))
7138 return true;
7139
7140 return false;
7141 }
7142 \f
7143 /* Return true if FIELD is an artificial field. */
7144
7145 static bool
7146 field_is_artificial (tree field)
7147 {
7148 /* These fields are generated by the front-end proper. */
7149 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7150 return true;
7151
7152 /* These fields are generated by gigi. */
7153 if (DECL_INTERNAL_P (field))
7154 return true;
7155
7156 return false;
7157 }
7158
7159 /* Return true if FIELD is a non-artificial aliased field. */
7160
7161 static bool
7162 field_is_aliased (tree field)
7163 {
7164 if (field_is_artificial (field))
7165 return false;
7166
7167 return DECL_ALIASED_P (field);
7168 }
7169
7170 /* Return true if FIELD is a non-artificial field with self-referential
7171 size. */
7172
7173 static bool
7174 field_has_self_size (tree field)
7175 {
7176 if (field_is_artificial (field))
7177 return false;
7178
7179 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7180 return false;
7181
7182 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7183 }
7184
7185 /* Return true if FIELD is a non-artificial field with variable size. */
7186
7187 static bool
7188 field_has_variable_size (tree field)
7189 {
7190 if (field_is_artificial (field))
7191 return false;
7192
7193 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7194 return false;
7195
7196 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7197 }
7198
7199 /* qsort comparer for the bit positions of two record components. */
7200
7201 static int
7202 compare_field_bitpos (const PTR rt1, const PTR rt2)
7203 {
7204 const_tree const field1 = * (const_tree const *) rt1;
7205 const_tree const field2 = * (const_tree const *) rt2;
7206 const int ret
7207 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7208
7209 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7210 }
7211
7212 /* Structure holding information for a given variant. */
7213 typedef struct vinfo
7214 {
7215 /* The record type of the variant. */
7216 tree type;
7217
7218 /* The name of the variant. */
7219 tree name;
7220
7221 /* The qualifier of the variant. */
7222 tree qual;
7223
7224 /* Whether the variant has a rep clause. */
7225 bool has_rep;
7226
7227 /* Whether the variant is packed. */
7228 bool packed;
7229
7230 } vinfo_t;
7231
7232 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
7233 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
7234 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
7235 When called from gnat_to_gnu_entity during the processing of a record type
7236 definition, the GCC node for the parent, if any, will be the single field
7237 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7238 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7239 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7240
7241 PACKED is 1 if this is for a packed record or -1 if this is for a record
7242 with Component_Alignment of Storage_Unit.
7243
7244 DEFINITION is true if we are defining this record type.
7245
7246 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7247 out the record. This means the alignment only serves to force fields to
7248 be bitfields, but not to require the record to be that aligned. This is
7249 used for variants.
7250
7251 ALL_REP is true if a rep clause is present for all the fields.
7252
7253 UNCHECKED_UNION is true if we are building this type for a record with a
7254 Pragma Unchecked_Union.
7255
7256 ARTIFICIAL is true if this is a type that was generated by the compiler.
7257
7258 DEBUG_INFO is true if we need to write debug information about the type.
7259
7260 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7261 mean that its contents may be unused as well, only the container itself.
7262
7263 REORDER is true if we are permitted to reorder components of this type.
7264
7265 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7266 the outer record type down to this variant level. It is nonzero only if
7267 all the fields down to this level have a rep clause and ALL_REP is false.
7268
7269 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7270 with a rep clause is to be added; in this case, that is all that should
7271 be done with such fields and the return value will be false. */
7272
7273 static bool
7274 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7275 tree gnu_field_list, int packed, bool definition,
7276 bool cancel_alignment, bool all_rep,
7277 bool unchecked_union, bool artificial,
7278 bool debug_info, bool maybe_unused, bool reorder,
7279 tree first_free_pos, tree *p_gnu_rep_list)
7280 {
7281 const bool needs_xv_encodings
7282 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7283 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7284 bool variants_have_rep = all_rep;
7285 bool layout_with_rep = false;
7286 bool has_self_field = false;
7287 bool has_aliased_after_self_field = false;
7288 Node_Id component_decl, variant_part;
7289 tree gnu_field, gnu_next, gnu_last;
7290 tree gnu_variant_part = NULL_TREE;
7291 tree gnu_rep_list = NULL_TREE;
7292 tree gnu_var_list = NULL_TREE;
7293 tree gnu_self_list = NULL_TREE;
7294 tree gnu_zero_list = NULL_TREE;
7295
7296 /* For each component referenced in a component declaration create a GCC
7297 field and add it to the list, skipping pragmas in the GNAT list. */
7298 gnu_last = tree_last (gnu_field_list);
7299 if (Present (Component_Items (gnat_component_list)))
7300 for (component_decl
7301 = First_Non_Pragma (Component_Items (gnat_component_list));
7302 Present (component_decl);
7303 component_decl = Next_Non_Pragma (component_decl))
7304 {
7305 Entity_Id gnat_field = Defining_Entity (component_decl);
7306 Name_Id gnat_name = Chars (gnat_field);
7307
7308 /* If present, the _Parent field must have been created as the single
7309 field of the record type. Put it before any other fields. */
7310 if (gnat_name == Name_uParent)
7311 {
7312 gnu_field = TYPE_FIELDS (gnu_record_type);
7313 gnu_field_list = chainon (gnu_field_list, gnu_field);
7314 }
7315 else
7316 {
7317 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7318 definition, debug_info);
7319
7320 /* If this is the _Tag field, put it before any other fields. */
7321 if (gnat_name == Name_uTag)
7322 gnu_field_list = chainon (gnu_field_list, gnu_field);
7323
7324 /* If this is the _Controller field, put it before the other
7325 fields except for the _Tag or _Parent field. */
7326 else if (gnat_name == Name_uController && gnu_last)
7327 {
7328 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7329 DECL_CHAIN (gnu_last) = gnu_field;
7330 }
7331
7332 /* If this is a regular field, put it after the other fields. */
7333 else
7334 {
7335 DECL_CHAIN (gnu_field) = gnu_field_list;
7336 gnu_field_list = gnu_field;
7337 if (!gnu_last)
7338 gnu_last = gnu_field;
7339
7340 /* And record information for the final layout. */
7341 if (field_has_self_size (gnu_field))
7342 has_self_field = true;
7343 else if (has_self_field && field_is_aliased (gnu_field))
7344 has_aliased_after_self_field = true;
7345 }
7346 }
7347
7348 save_gnu_tree (gnat_field, gnu_field, false);
7349 }
7350
7351 /* At the end of the component list there may be a variant part. */
7352 variant_part = Variant_Part (gnat_component_list);
7353
7354 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7355 mutually exclusive and should go in the same memory. To do this we need
7356 to treat each variant as a record whose elements are created from the
7357 component list for the variant. So here we create the records from the
7358 lists for the variants and put them all into the QUAL_UNION_TYPE.
7359 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7360 use GNU_RECORD_TYPE if there are no fields so far. */
7361 if (Present (variant_part))
7362 {
7363 Node_Id gnat_discr = Name (variant_part), variant;
7364 tree gnu_discr = gnat_to_gnu (gnat_discr);
7365 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7366 tree gnu_var_name
7367 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7368 "XVN");
7369 tree gnu_union_type, gnu_union_name;
7370 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7371 bool union_field_needs_strict_alignment = false;
7372 auto_vec <vinfo_t, 16> variant_types;
7373 vinfo_t *gnu_variant;
7374 unsigned int variants_align = 0;
7375 unsigned int i;
7376
7377 gnu_union_name
7378 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7379
7380 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7381 are all in the variant part, to match the layout of C unions. There
7382 is an associated check below. */
7383 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7384 gnu_union_type = gnu_record_type;
7385 else
7386 {
7387 gnu_union_type
7388 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7389
7390 TYPE_NAME (gnu_union_type) = gnu_union_name;
7391 SET_TYPE_ALIGN (gnu_union_type, 0);
7392 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7393 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7394 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7395 }
7396
7397 /* If all the fields down to this level have a rep clause, find out
7398 whether all the fields at this level also have one. If so, then
7399 compute the new first free position to be passed downward. */
7400 this_first_free_pos = first_free_pos;
7401 if (this_first_free_pos)
7402 {
7403 for (gnu_field = gnu_field_list;
7404 gnu_field;
7405 gnu_field = DECL_CHAIN (gnu_field))
7406 if (DECL_FIELD_OFFSET (gnu_field))
7407 {
7408 tree pos = bit_position (gnu_field);
7409 if (!tree_int_cst_lt (pos, this_first_free_pos))
7410 this_first_free_pos
7411 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7412 }
7413 else
7414 {
7415 this_first_free_pos = NULL_TREE;
7416 break;
7417 }
7418 }
7419
7420 /* We build the variants in two passes. The bulk of the work is done in
7421 the first pass, that is to say translating the GNAT nodes, building
7422 the container types and computing the associated properties. However
7423 we cannot finish up the container types during this pass because we
7424 don't know where the variant part will be placed until the end. */
7425 for (variant = First_Non_Pragma (Variants (variant_part));
7426 Present (variant);
7427 variant = Next_Non_Pragma (variant))
7428 {
7429 tree gnu_variant_type = make_node (RECORD_TYPE);
7430 tree gnu_inner_name, gnu_qual;
7431 bool has_rep;
7432 int field_packed;
7433 vinfo_t vinfo;
7434
7435 Get_Variant_Encoding (variant);
7436 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7437 TYPE_NAME (gnu_variant_type)
7438 = concat_name (gnu_union_name,
7439 IDENTIFIER_POINTER (gnu_inner_name));
7440
7441 /* Set the alignment of the inner type in case we need to make
7442 inner objects into bitfields, but then clear it out so the
7443 record actually gets only the alignment required. */
7444 SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type));
7445 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7446 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7447 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7448
7449 /* Similarly, if the outer record has a size specified and all
7450 the fields have a rep clause, we can propagate the size. */
7451 if (all_rep_and_size)
7452 {
7453 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7454 TYPE_SIZE_UNIT (gnu_variant_type)
7455 = TYPE_SIZE_UNIT (gnu_record_type);
7456 }
7457
7458 /* Add the fields into the record type for the variant. Note that
7459 we aren't sure to really use it at this point, see below. */
7460 has_rep
7461 = components_to_record (gnu_variant_type, Component_List (variant),
7462 NULL_TREE, packed, definition,
7463 !all_rep_and_size, all_rep,
7464 unchecked_union,
7465 true, needs_xv_encodings, true, reorder,
7466 this_first_free_pos,
7467 all_rep || this_first_free_pos
7468 ? NULL : &gnu_rep_list);
7469
7470 /* Translate the qualifier and annotate the GNAT node. */
7471 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7472 Set_Present_Expr (variant, annotate_value (gnu_qual));
7473
7474 /* Deal with packedness like in gnat_to_gnu_field. */
7475 if (components_need_strict_alignment (Component_List (variant)))
7476 {
7477 field_packed = 0;
7478 union_field_needs_strict_alignment = true;
7479 }
7480 else
7481 field_packed
7482 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7483
7484 /* Push this variant onto the stack for the second pass. */
7485 vinfo.type = gnu_variant_type;
7486 vinfo.name = gnu_inner_name;
7487 vinfo.qual = gnu_qual;
7488 vinfo.has_rep = has_rep;
7489 vinfo.packed = field_packed;
7490 variant_types.safe_push (vinfo);
7491
7492 /* Compute the global properties that will determine the placement of
7493 the variant part. */
7494 variants_have_rep |= has_rep;
7495 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7496 variants_align = TYPE_ALIGN (gnu_variant_type);
7497 }
7498
7499 /* Round up the first free position to the alignment of the variant part
7500 for the variants without rep clause. This will guarantee a consistent
7501 layout independently of the placement of the variant part. */
7502 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7503 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7504
7505 /* In the second pass, the container types are adjusted if necessary and
7506 finished up, then the corresponding fields of the variant part are
7507 built with their qualifier, unless this is an unchecked union. */
7508 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7509 {
7510 tree gnu_variant_type = gnu_variant->type;
7511 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7512
7513 /* If this is an Unchecked_Union whose fields are all in the variant
7514 part and we have a single field with no representation clause or
7515 placed at offset zero, use the field directly to match the layout
7516 of C unions. */
7517 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7518 && gnu_field_list
7519 && !DECL_CHAIN (gnu_field_list)
7520 && (!DECL_FIELD_OFFSET (gnu_field_list)
7521 || integer_zerop (bit_position (gnu_field_list))))
7522 {
7523 gnu_field = gnu_field_list;
7524 DECL_CONTEXT (gnu_field) = gnu_record_type;
7525 }
7526 else
7527 {
7528 /* Finalize the variant type now. We used to throw away empty
7529 record types but we no longer do that because we need them to
7530 generate complete debug info for the variant; otherwise, the
7531 union type definition will be lacking the fields associated
7532 with these empty variants. */
7533 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7534 {
7535 /* The variant part will be at offset 0 so we need to ensure
7536 that the fields are laid out starting from the first free
7537 position at this level. */
7538 tree gnu_rep_type = make_node (RECORD_TYPE);
7539 tree gnu_rep_part;
7540 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7541 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7542 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7543 gnu_rep_part
7544 = create_rep_part (gnu_rep_type, gnu_variant_type,
7545 this_first_free_pos);
7546 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7547 gnu_field_list = gnu_rep_part;
7548 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7549 false);
7550 }
7551
7552 if (debug_info)
7553 rest_of_record_type_compilation (gnu_variant_type);
7554 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7555 true, needs_xv_encodings, gnat_component_list);
7556
7557 gnu_field
7558 = create_field_decl (gnu_variant->name, gnu_variant_type,
7559 gnu_union_type,
7560 all_rep_and_size
7561 ? TYPE_SIZE (gnu_variant_type) : 0,
7562 variants_have_rep ? bitsize_zero_node : 0,
7563 gnu_variant->packed, 0);
7564
7565 DECL_INTERNAL_P (gnu_field) = 1;
7566
7567 if (!unchecked_union)
7568 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7569 }
7570
7571 DECL_CHAIN (gnu_field) = gnu_variant_list;
7572 gnu_variant_list = gnu_field;
7573 }
7574
7575 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7576 if (gnu_variant_list)
7577 {
7578 int union_field_packed;
7579
7580 if (all_rep_and_size)
7581 {
7582 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7583 TYPE_SIZE_UNIT (gnu_union_type)
7584 = TYPE_SIZE_UNIT (gnu_record_type);
7585 }
7586
7587 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7588 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7589
7590 /* If GNU_UNION_TYPE is our record type, it means we must have an
7591 Unchecked_Union with no fields. Verify that and, if so, just
7592 return. */
7593 if (gnu_union_type == gnu_record_type)
7594 {
7595 gcc_assert (unchecked_union
7596 && !gnu_field_list
7597 && !gnu_rep_list);
7598 return variants_have_rep;
7599 }
7600
7601 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7602 needs_xv_encodings, gnat_component_list);
7603
7604 /* Deal with packedness like in gnat_to_gnu_field. */
7605 if (union_field_needs_strict_alignment)
7606 union_field_packed = 0;
7607 else
7608 union_field_packed
7609 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7610
7611 gnu_variant_part
7612 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7613 all_rep_and_size
7614 ? TYPE_SIZE (gnu_union_type) : 0,
7615 variants_have_rep ? bitsize_zero_node : 0,
7616 union_field_packed, 0);
7617
7618 DECL_INTERNAL_P (gnu_variant_part) = 1;
7619 }
7620 }
7621
7622 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7623 permitted to reorder components, self-referential sizes or variable sizes.
7624 If they do, pull them out and put them onto the appropriate list. We have
7625 to do this in a separate pass since we want to handle the discriminants
7626 but can't play with them until we've used them in debugging data above.
7627
7628 Similarly, pull out the fields with zero size and no rep clause, as they
7629 would otherwise modify the layout and thus very likely run afoul of the
7630 Ada semantics, which are different from those of C here.
7631
7632 ??? If we reorder them, debugging information will be wrong but there is
7633 nothing that can be done about this at the moment. */
7634 gnu_last = NULL_TREE;
7635
7636 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7637 do { \
7638 if (gnu_last) \
7639 DECL_CHAIN (gnu_last) = gnu_next; \
7640 else \
7641 gnu_field_list = gnu_next; \
7642 \
7643 DECL_CHAIN (gnu_field) = (LIST); \
7644 (LIST) = gnu_field; \
7645 } while (0)
7646
7647 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7648 {
7649 gnu_next = DECL_CHAIN (gnu_field);
7650
7651 if (DECL_FIELD_OFFSET (gnu_field))
7652 {
7653 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7654 continue;
7655 }
7656
7657 if ((reorder || has_aliased_after_self_field)
7658 && field_has_self_size (gnu_field))
7659 {
7660 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7661 continue;
7662 }
7663
7664 if (reorder && field_has_variable_size (gnu_field))
7665 {
7666 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7667 continue;
7668 }
7669
7670 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7671 {
7672 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7673 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7674 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7675 if (field_is_aliased (gnu_field))
7676 SET_TYPE_ALIGN (gnu_record_type,
7677 MAX (TYPE_ALIGN (gnu_record_type),
7678 TYPE_ALIGN (TREE_TYPE (gnu_field))));
7679 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7680 continue;
7681 }
7682
7683 gnu_last = gnu_field;
7684 }
7685
7686 #undef MOVE_FROM_FIELD_LIST_TO
7687
7688 gnu_field_list = nreverse (gnu_field_list);
7689
7690 /* If permitted, we reorder the fields as follows:
7691
7692 1) all fixed length fields,
7693 2) all fields whose length doesn't depend on discriminants,
7694 3) all fields whose length depends on discriminants,
7695 4) the variant part,
7696
7697 within the record and within each variant recursively. */
7698 if (reorder)
7699 gnu_field_list
7700 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7701
7702 /* Otherwise, if there is an aliased field placed after a field whose length
7703 depends on discriminants, we put all the fields of the latter sort, last.
7704 We need to do this in case an object of this record type is mutable. */
7705 else if (has_aliased_after_self_field)
7706 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7707
7708 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7709 in our REP list to the previous level because this level needs them in
7710 order to do a correct layout, i.e. avoid having overlapping fields. */
7711 if (p_gnu_rep_list && gnu_rep_list)
7712 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7713
7714 /* Deal with the annoying case of an extension of a record with variable size
7715 and partial rep clause, for which the _Parent field is forced at offset 0
7716 and has variable size, which we do not support below. Note that we cannot
7717 do it if the field has fixed size because we rely on the presence of the
7718 REP part built below to trigger the reordering of the fields in a derived
7719 record type when all the fields have a fixed position. */
7720 else if (gnu_rep_list
7721 && !DECL_CHAIN (gnu_rep_list)
7722 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7723 && !variants_have_rep
7724 && first_free_pos
7725 && integer_zerop (first_free_pos)
7726 && integer_zerop (bit_position (gnu_rep_list)))
7727 {
7728 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7729 gnu_field_list = gnu_rep_list;
7730 gnu_rep_list = NULL_TREE;
7731 }
7732
7733 /* Otherwise, sort the fields by bit position and put them into their own
7734 record, before the others, if we also have fields without rep clause. */
7735 else if (gnu_rep_list)
7736 {
7737 tree gnu_rep_type, gnu_rep_part;
7738 int i, len = list_length (gnu_rep_list);
7739 tree *gnu_arr = XALLOCAVEC (tree, len);
7740
7741 /* If all the fields have a rep clause, we can do a flat layout. */
7742 layout_with_rep = !gnu_field_list
7743 && (!gnu_variant_part || variants_have_rep);
7744 gnu_rep_type
7745 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7746
7747 for (gnu_field = gnu_rep_list, i = 0;
7748 gnu_field;
7749 gnu_field = DECL_CHAIN (gnu_field), i++)
7750 gnu_arr[i] = gnu_field;
7751
7752 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7753
7754 /* Put the fields in the list in order of increasing position, which
7755 means we start from the end. */
7756 gnu_rep_list = NULL_TREE;
7757 for (i = len - 1; i >= 0; i--)
7758 {
7759 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7760 gnu_rep_list = gnu_arr[i];
7761 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7762 }
7763
7764 if (layout_with_rep)
7765 gnu_field_list = gnu_rep_list;
7766 else
7767 {
7768 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7769 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7770 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7771
7772 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7773 without rep clause are laid out starting from this position.
7774 Therefore, we force it as a minimal size on the REP part. */
7775 gnu_rep_part
7776 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7777
7778 /* Chain the REP part at the beginning of the field list. */
7779 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7780 gnu_field_list = gnu_rep_part;
7781 }
7782 }
7783
7784 /* Chain the variant part at the end of the field list. */
7785 if (gnu_variant_part)
7786 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7787
7788 if (cancel_alignment)
7789 SET_TYPE_ALIGN (gnu_record_type, 0);
7790
7791 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7792
7793 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7794 debug_info && !maybe_unused);
7795
7796 /* Chain the fields with zero size at the beginning of the field list. */
7797 if (gnu_zero_list)
7798 TYPE_FIELDS (gnu_record_type)
7799 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7800
7801 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7802 }
7803 \f
7804 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7805 placed into an Esize, Component_Bit_Offset, or Component_Size value
7806 in the GNAT tree. */
7807
7808 static Uint
7809 annotate_value (tree gnu_size)
7810 {
7811 TCode tcode;
7812 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7813 struct tree_int_map in;
7814 int i;
7815
7816 /* See if we've already saved the value for this node. */
7817 if (EXPR_P (gnu_size))
7818 {
7819 struct tree_int_map *e;
7820
7821 in.base.from = gnu_size;
7822 e = annotate_value_cache->find (&in);
7823
7824 if (e)
7825 return (Node_Ref_Or_Val) e->to;
7826 }
7827 else
7828 in.base.from = NULL_TREE;
7829
7830 /* If we do not return inside this switch, TCODE will be set to the
7831 code to use for a Create_Node operand and LEN (set above) will be
7832 the number of recursive calls for us to make. */
7833
7834 switch (TREE_CODE (gnu_size))
7835 {
7836 case INTEGER_CST:
7837 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7838
7839 case COMPONENT_REF:
7840 /* The only case we handle here is a simple discriminant reference. */
7841 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7842 {
7843 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7844
7845 /* Climb up the chain of successive extensions, if any. */
7846 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7847 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7848 == parent_name_id)
7849 gnu_size = TREE_OPERAND (gnu_size, 0);
7850
7851 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7852 return
7853 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7854 }
7855
7856 return No_Uint;
7857
7858 CASE_CONVERT: case NON_LVALUE_EXPR:
7859 return annotate_value (TREE_OPERAND (gnu_size, 0));
7860
7861 /* Now just list the operations we handle. */
7862 case COND_EXPR: tcode = Cond_Expr; break;
7863 case PLUS_EXPR: tcode = Plus_Expr; break;
7864 case MINUS_EXPR: tcode = Minus_Expr; break;
7865 case MULT_EXPR: tcode = Mult_Expr; break;
7866 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7867 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7868 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7869 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7870 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7871 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7872 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7873 case NEGATE_EXPR: tcode = Negate_Expr; break;
7874 case MIN_EXPR: tcode = Min_Expr; break;
7875 case MAX_EXPR: tcode = Max_Expr; break;
7876 case ABS_EXPR: tcode = Abs_Expr; break;
7877 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7878 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7879 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7880 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7881 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7882 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7883 case LT_EXPR: tcode = Lt_Expr; break;
7884 case LE_EXPR: tcode = Le_Expr; break;
7885 case GT_EXPR: tcode = Gt_Expr; break;
7886 case GE_EXPR: tcode = Ge_Expr; break;
7887 case EQ_EXPR: tcode = Eq_Expr; break;
7888 case NE_EXPR: tcode = Ne_Expr; break;
7889
7890 case BIT_AND_EXPR:
7891 tcode = Bit_And_Expr;
7892 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7893 Such values appear in expressions with aligning patterns. Note that,
7894 since sizetype is unsigned, we have to jump through some hoops. */
7895 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7896 {
7897 tree op1 = TREE_OPERAND (gnu_size, 1);
7898 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7899 if (wi::neg_p (signed_op1))
7900 {
7901 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7902 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7903 }
7904 }
7905 break;
7906
7907 case CALL_EXPR:
7908 /* In regular mode, inline back only if symbolic annotation is requested
7909 in order to avoid memory explosion on big discriminated record types.
7910 But not in ASIS mode, as symbolic annotation is required for DDA. */
7911 if (List_Representation_Info == 3 || type_annotate_only)
7912 {
7913 tree t = maybe_inline_call_in_expr (gnu_size);
7914 if (t)
7915 return annotate_value (t);
7916 }
7917 else
7918 return Uint_Minus_1;
7919
7920 /* Fall through... */
7921
7922 default:
7923 return No_Uint;
7924 }
7925
7926 /* Now get each of the operands that's relevant for this code. If any
7927 cannot be expressed as a repinfo node, say we can't. */
7928 for (i = 0; i < 3; i++)
7929 ops[i] = No_Uint;
7930
7931 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7932 {
7933 if (i == 1 && pre_op1 != No_Uint)
7934 ops[i] = pre_op1;
7935 else
7936 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7937 if (ops[i] == No_Uint)
7938 return No_Uint;
7939 }
7940
7941 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7942
7943 /* Save the result in the cache. */
7944 if (in.base.from)
7945 {
7946 struct tree_int_map **h;
7947 /* We can't assume the hash table data hasn't moved since the initial
7948 look up, so we have to search again. Allocating and inserting an
7949 entry at that point would be an alternative, but then we'd better
7950 discard the entry if we decided not to cache it. */
7951 h = annotate_value_cache->find_slot (&in, INSERT);
7952 gcc_assert (!*h);
7953 *h = ggc_alloc<tree_int_map> ();
7954 (*h)->base.from = gnu_size;
7955 (*h)->to = ret;
7956 }
7957
7958 return ret;
7959 }
7960
7961 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7962 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7963 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7964 BY_REF is true if the object is used by reference. */
7965
7966 void
7967 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7968 {
7969 if (by_ref)
7970 {
7971 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7972 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7973 else
7974 gnu_type = TREE_TYPE (gnu_type);
7975 }
7976
7977 if (Unknown_Esize (gnat_entity))
7978 {
7979 if (TREE_CODE (gnu_type) == RECORD_TYPE
7980 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7981 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7982 else if (!size)
7983 size = TYPE_SIZE (gnu_type);
7984
7985 if (size)
7986 Set_Esize (gnat_entity, annotate_value (size));
7987 }
7988
7989 if (Unknown_Alignment (gnat_entity))
7990 Set_Alignment (gnat_entity,
7991 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7992 }
7993
7994 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7995 Return NULL_TREE if there is no such element in the list. */
7996
7997 static tree
7998 purpose_member_field (const_tree elem, tree list)
7999 {
8000 while (list)
8001 {
8002 tree field = TREE_PURPOSE (list);
8003 if (SAME_FIELD_P (field, elem))
8004 return list;
8005 list = TREE_CHAIN (list);
8006 }
8007 return NULL_TREE;
8008 }
8009
8010 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8011 set Component_Bit_Offset and Esize of the components to the position and
8012 size used by Gigi. */
8013
8014 static void
8015 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
8016 {
8017 Entity_Id gnat_field;
8018 tree gnu_list;
8019
8020 /* We operate by first making a list of all fields and their position (we
8021 can get the size easily) and then update all the sizes in the tree. */
8022 gnu_list
8023 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
8024 BIGGEST_ALIGNMENT, NULL_TREE);
8025
8026 for (gnat_field = First_Entity (gnat_entity);
8027 Present (gnat_field);
8028 gnat_field = Next_Entity (gnat_field))
8029 if (Ekind (gnat_field) == E_Component
8030 || (Ekind (gnat_field) == E_Discriminant
8031 && !Is_Unchecked_Union (Scope (gnat_field))))
8032 {
8033 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
8034 gnu_list);
8035 if (t)
8036 {
8037 tree parent_offset;
8038
8039 /* If we are just annotating types and the type is tagged, the tag
8040 and the parent components are not generated by the front-end so
8041 we need to add the appropriate offset to each component without
8042 representation clause. */
8043 if (type_annotate_only
8044 && Is_Tagged_Type (gnat_entity)
8045 && No (Component_Clause (gnat_field)))
8046 {
8047 /* For a component appearing in the current extension, the
8048 offset is the size of the parent. */
8049 if (Is_Derived_Type (gnat_entity)
8050 && Original_Record_Component (gnat_field) == gnat_field)
8051 parent_offset
8052 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
8053 bitsizetype);
8054 else
8055 parent_offset = bitsize_int (POINTER_SIZE);
8056
8057 if (TYPE_FIELDS (gnu_type))
8058 parent_offset
8059 = round_up (parent_offset,
8060 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
8061 }
8062 else
8063 parent_offset = bitsize_zero_node;
8064
8065 Set_Component_Bit_Offset
8066 (gnat_field,
8067 annotate_value
8068 (size_binop (PLUS_EXPR,
8069 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
8070 TREE_VEC_ELT (TREE_VALUE (t), 2)),
8071 parent_offset)));
8072
8073 Set_Esize (gnat_field,
8074 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
8075 }
8076 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
8077 {
8078 /* If there is no entry, this is an inherited component whose
8079 position is the same as in the parent type. */
8080 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
8081
8082 /* If we are just annotating types, discriminants renaming those of
8083 the parent have no entry so deal with them specifically. */
8084 if (type_annotate_only
8085 && gnat_orig_field == gnat_field
8086 && Ekind (gnat_field) == E_Discriminant)
8087 gnat_orig_field = Corresponding_Discriminant (gnat_field);
8088
8089 Set_Component_Bit_Offset (gnat_field,
8090 Component_Bit_Offset (gnat_orig_field));
8091
8092 Set_Esize (gnat_field, Esize (gnat_orig_field));
8093 }
8094 }
8095 }
8096 \f
8097 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8098 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8099 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8100 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8101 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8102 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8103 pre-existing list to be chained to the newly created entries. */
8104
8105 static tree
8106 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8107 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8108 {
8109 tree gnu_field;
8110
8111 for (gnu_field = TYPE_FIELDS (gnu_type);
8112 gnu_field;
8113 gnu_field = DECL_CHAIN (gnu_field))
8114 {
8115 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8116 DECL_FIELD_BIT_OFFSET (gnu_field));
8117 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8118 DECL_FIELD_OFFSET (gnu_field));
8119 unsigned int our_offset_align
8120 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8121 tree v = make_tree_vec (3);
8122
8123 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8124 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8125 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8126 gnu_list = tree_cons (gnu_field, v, gnu_list);
8127
8128 /* Recurse on internal fields, flattening the nested fields except for
8129 those in the variant part, if requested. */
8130 if (DECL_INTERNAL_P (gnu_field))
8131 {
8132 tree gnu_field_type = TREE_TYPE (gnu_field);
8133 if (do_not_flatten_variant
8134 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8135 gnu_list
8136 = build_position_list (gnu_field_type, do_not_flatten_variant,
8137 size_zero_node, bitsize_zero_node,
8138 BIGGEST_ALIGNMENT, gnu_list);
8139 else
8140 gnu_list
8141 = build_position_list (gnu_field_type, do_not_flatten_variant,
8142 gnu_our_offset, gnu_our_bitpos,
8143 our_offset_align, gnu_list);
8144 }
8145 }
8146
8147 return gnu_list;
8148 }
8149
8150 /* Return a list describing the substitutions needed to reflect the
8151 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8152 be in any order. The values in an element of the list are in the form
8153 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8154 a definition of GNAT_SUBTYPE. */
8155
8156 static vec<subst_pair>
8157 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8158 {
8159 vec<subst_pair> gnu_list = vNULL;
8160 Entity_Id gnat_discrim;
8161 Node_Id gnat_constr;
8162
8163 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8164 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8165 Present (gnat_discrim);
8166 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8167 gnat_constr = Next_Elmt (gnat_constr))
8168 /* Ignore access discriminants. */
8169 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8170 {
8171 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8172 tree replacement = convert (TREE_TYPE (gnu_field),
8173 elaborate_expression
8174 (Node (gnat_constr), gnat_subtype,
8175 get_entity_char (gnat_discrim),
8176 definition, true, false));
8177 subst_pair s = {gnu_field, replacement};
8178 gnu_list.safe_push (s);
8179 }
8180
8181 return gnu_list;
8182 }
8183
8184 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8185 variants of QUAL_UNION_TYPE that are still relevant after applying
8186 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8187 list to be prepended to the newly created entries. */
8188
8189 static vec<variant_desc>
8190 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8191 vec<variant_desc> gnu_list)
8192 {
8193 tree gnu_field;
8194
8195 for (gnu_field = TYPE_FIELDS (qual_union_type);
8196 gnu_field;
8197 gnu_field = DECL_CHAIN (gnu_field))
8198 {
8199 tree qual = DECL_QUALIFIER (gnu_field);
8200 unsigned int i;
8201 subst_pair *s;
8202
8203 FOR_EACH_VEC_ELT (subst_list, i, s)
8204 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8205
8206 /* If the new qualifier is not unconditionally false, its variant may
8207 still be accessed. */
8208 if (!integer_zerop (qual))
8209 {
8210 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8211 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
8212
8213 gnu_list.safe_push (v);
8214
8215 /* Recurse on the variant subpart of the variant, if any. */
8216 variant_subpart = get_variant_part (variant_type);
8217 if (variant_subpart)
8218 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8219 subst_list, gnu_list);
8220
8221 /* If the new qualifier is unconditionally true, the subsequent
8222 variants cannot be accessed. */
8223 if (integer_onep (qual))
8224 break;
8225 }
8226 }
8227
8228 return gnu_list;
8229 }
8230 \f
8231 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8232 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8233 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8234 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8235 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8236 true if we are being called to process the Component_Size of GNAT_OBJECT;
8237 this is used only for error messages. ZERO_OK is true if a size of zero
8238 is permitted; if ZERO_OK is false, it means that a size of zero should be
8239 treated as an unspecified size. */
8240
8241 static tree
8242 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8243 enum tree_code kind, bool component_p, bool zero_ok)
8244 {
8245 Node_Id gnat_error_node;
8246 tree type_size, size;
8247
8248 /* Return 0 if no size was specified. */
8249 if (uint_size == No_Uint)
8250 return NULL_TREE;
8251
8252 /* Ignore a negative size since that corresponds to our back-annotation. */
8253 if (UI_Lt (uint_size, Uint_0))
8254 return NULL_TREE;
8255
8256 /* Find the node to use for error messages. */
8257 if ((Ekind (gnat_object) == E_Component
8258 || Ekind (gnat_object) == E_Discriminant)
8259 && Present (Component_Clause (gnat_object)))
8260 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8261 else if (Present (Size_Clause (gnat_object)))
8262 gnat_error_node = Expression (Size_Clause (gnat_object));
8263 else
8264 gnat_error_node = gnat_object;
8265
8266 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8267 but cannot be represented in bitsizetype. */
8268 size = UI_To_gnu (uint_size, bitsizetype);
8269 if (TREE_OVERFLOW (size))
8270 {
8271 if (component_p)
8272 post_error_ne ("component size for& is too large", gnat_error_node,
8273 gnat_object);
8274 else
8275 post_error_ne ("size for& is too large", gnat_error_node,
8276 gnat_object);
8277 return NULL_TREE;
8278 }
8279
8280 /* Ignore a zero size if it is not permitted. */
8281 if (!zero_ok && integer_zerop (size))
8282 return NULL_TREE;
8283
8284 /* The size of objects is always a multiple of a byte. */
8285 if (kind == VAR_DECL
8286 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8287 {
8288 if (component_p)
8289 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8290 gnat_error_node, gnat_object);
8291 else
8292 post_error_ne ("size for& is not a multiple of Storage_Unit",
8293 gnat_error_node, gnat_object);
8294 return NULL_TREE;
8295 }
8296
8297 /* If this is an integral type or a packed array type, the front-end has
8298 already verified the size, so we need not do it here (which would mean
8299 checking against the bounds). However, if this is an aliased object,
8300 it may not be smaller than the type of the object. */
8301 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8302 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8303 return size;
8304
8305 /* If the object is a record that contains a template, add the size of the
8306 template to the specified size. */
8307 if (TREE_CODE (gnu_type) == RECORD_TYPE
8308 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8309 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8310
8311 if (kind == VAR_DECL
8312 /* If a type needs strict alignment, a component of this type in
8313 a packed record cannot be packed and thus uses the type size. */
8314 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8315 type_size = TYPE_SIZE (gnu_type);
8316 else
8317 type_size = rm_size (gnu_type);
8318
8319 /* Modify the size of a discriminated type to be the maximum size. */
8320 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8321 type_size = max_size (type_size, true);
8322
8323 /* If this is an access type or a fat pointer, the minimum size is that given
8324 by the smallest integral mode that's valid for pointers. */
8325 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8326 {
8327 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8328 while (!targetm.valid_pointer_mode (p_mode))
8329 p_mode = GET_MODE_WIDER_MODE (p_mode);
8330 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8331 }
8332
8333 /* Issue an error either if the default size of the object isn't a constant
8334 or if the new size is smaller than it. */
8335 if (TREE_CODE (type_size) != INTEGER_CST
8336 || TREE_OVERFLOW (type_size)
8337 || tree_int_cst_lt (size, type_size))
8338 {
8339 if (component_p)
8340 post_error_ne_tree
8341 ("component size for& too small{, minimum allowed is ^}",
8342 gnat_error_node, gnat_object, type_size);
8343 else
8344 post_error_ne_tree
8345 ("size for& too small{, minimum allowed is ^}",
8346 gnat_error_node, gnat_object, type_size);
8347 return NULL_TREE;
8348 }
8349
8350 return size;
8351 }
8352 \f
8353 /* Similarly, but both validate and process a value of RM size. This routine
8354 is only called for types. */
8355
8356 static void
8357 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8358 {
8359 Node_Id gnat_attr_node;
8360 tree old_size, size;
8361
8362 /* Do nothing if no size was specified. */
8363 if (uint_size == No_Uint)
8364 return;
8365
8366 /* Ignore a negative size since that corresponds to our back-annotation. */
8367 if (UI_Lt (uint_size, Uint_0))
8368 return;
8369
8370 /* Only issue an error if a Value_Size clause was explicitly given.
8371 Otherwise, we'd be duplicating an error on the Size clause. */
8372 gnat_attr_node
8373 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8374
8375 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8376 but cannot be represented in bitsizetype. */
8377 size = UI_To_gnu (uint_size, bitsizetype);
8378 if (TREE_OVERFLOW (size))
8379 {
8380 if (Present (gnat_attr_node))
8381 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8382 gnat_entity);
8383 return;
8384 }
8385
8386 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8387 exists, or this is an integer type, in which case the front-end will
8388 have always set it. */
8389 if (No (gnat_attr_node)
8390 && integer_zerop (size)
8391 && !Has_Size_Clause (gnat_entity)
8392 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8393 return;
8394
8395 old_size = rm_size (gnu_type);
8396
8397 /* If the old size is self-referential, get the maximum size. */
8398 if (CONTAINS_PLACEHOLDER_P (old_size))
8399 old_size = max_size (old_size, true);
8400
8401 /* Issue an error either if the old size of the object isn't a constant or
8402 if the new size is smaller than it. The front-end has already verified
8403 this for scalar and packed array types. */
8404 if (TREE_CODE (old_size) != INTEGER_CST
8405 || TREE_OVERFLOW (old_size)
8406 || (AGGREGATE_TYPE_P (gnu_type)
8407 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8408 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8409 && !(TYPE_IS_PADDING_P (gnu_type)
8410 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8411 && TYPE_PACKED_ARRAY_TYPE_P
8412 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8413 && tree_int_cst_lt (size, old_size)))
8414 {
8415 if (Present (gnat_attr_node))
8416 post_error_ne_tree
8417 ("Value_Size for& too small{, minimum allowed is ^}",
8418 gnat_attr_node, gnat_entity, old_size);
8419 return;
8420 }
8421
8422 /* Otherwise, set the RM size proper for integral types... */
8423 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8424 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8425 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8426 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8427 SET_TYPE_RM_SIZE (gnu_type, size);
8428
8429 /* ...or the Ada size for record and union types. */
8430 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8431 && !TYPE_FAT_POINTER_P (gnu_type))
8432 SET_TYPE_ADA_SIZE (gnu_type, size);
8433 }
8434 \f
8435 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8436 a type or object whose present alignment is ALIGN. If this alignment is
8437 valid, return it. Otherwise, give an error and return ALIGN. */
8438
8439 static unsigned int
8440 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8441 {
8442 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8443 unsigned int new_align;
8444 Node_Id gnat_error_node;
8445
8446 /* Don't worry about checking alignment if alignment was not specified
8447 by the source program and we already posted an error for this entity. */
8448 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8449 return align;
8450
8451 /* Post the error on the alignment clause if any. Note, for the implicit
8452 base type of an array type, the alignment clause is on the first
8453 subtype. */
8454 if (Present (Alignment_Clause (gnat_entity)))
8455 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8456
8457 else if (Is_Itype (gnat_entity)
8458 && Is_Array_Type (gnat_entity)
8459 && Etype (gnat_entity) == gnat_entity
8460 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8461 gnat_error_node =
8462 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8463
8464 else
8465 gnat_error_node = gnat_entity;
8466
8467 /* Within GCC, an alignment is an integer, so we must make sure a value is
8468 specified that fits in that range. Also, there is an upper bound to
8469 alignments we can support/allow. */
8470 if (!UI_Is_In_Int_Range (alignment)
8471 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8472 post_error_ne_num ("largest supported alignment for& is ^",
8473 gnat_error_node, gnat_entity, max_allowed_alignment);
8474 else if (!(Present (Alignment_Clause (gnat_entity))
8475 && From_At_Mod (Alignment_Clause (gnat_entity)))
8476 && new_align * BITS_PER_UNIT < align)
8477 {
8478 unsigned int double_align;
8479 bool is_capped_double, align_clause;
8480
8481 /* If the default alignment of "double" or larger scalar types is
8482 specifically capped and the new alignment is above the cap, do
8483 not post an error and change the alignment only if there is an
8484 alignment clause; this makes it possible to have the associated
8485 GCC type overaligned by default for performance reasons. */
8486 if ((double_align = double_float_alignment) > 0)
8487 {
8488 Entity_Id gnat_type
8489 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8490 is_capped_double
8491 = is_double_float_or_array (gnat_type, &align_clause);
8492 }
8493 else if ((double_align = double_scalar_alignment) > 0)
8494 {
8495 Entity_Id gnat_type
8496 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8497 is_capped_double
8498 = is_double_scalar_or_array (gnat_type, &align_clause);
8499 }
8500 else
8501 is_capped_double = align_clause = false;
8502
8503 if (is_capped_double && new_align >= double_align)
8504 {
8505 if (align_clause)
8506 align = new_align * BITS_PER_UNIT;
8507 }
8508 else
8509 {
8510 if (is_capped_double)
8511 align = double_align * BITS_PER_UNIT;
8512
8513 post_error_ne_num ("alignment for& must be at least ^",
8514 gnat_error_node, gnat_entity,
8515 align / BITS_PER_UNIT);
8516 }
8517 }
8518 else
8519 {
8520 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8521 if (new_align > align)
8522 align = new_align;
8523 }
8524
8525 return align;
8526 }
8527 \f
8528 /* Verify that TYPE is something we can implement atomically. If not, issue
8529 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8530 process a component type. */
8531
8532 static void
8533 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8534 {
8535 Node_Id gnat_error_point = gnat_entity;
8536 Node_Id gnat_node;
8537 machine_mode mode;
8538 enum mode_class mclass;
8539 unsigned int align;
8540 tree size;
8541
8542 /* If this is an anonymous base type, nothing to check, the error will be
8543 reported on the source type if need be. */
8544 if (!Comes_From_Source (gnat_entity))
8545 return;
8546
8547 mode = TYPE_MODE (type);
8548 mclass = GET_MODE_CLASS (mode);
8549 align = TYPE_ALIGN (type);
8550 size = TYPE_SIZE (type);
8551
8552 /* Consider all aligned floating-point types atomic and any aligned types
8553 that are represented by integers no wider than a machine word. */
8554 if ((mclass == MODE_FLOAT
8555 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8556 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8557 && align >= GET_MODE_ALIGNMENT (mode))
8558 return;
8559
8560 /* For the moment, also allow anything that has an alignment equal to its
8561 size and which is smaller than a word. */
8562 if (size
8563 && TREE_CODE (size) == INTEGER_CST
8564 && compare_tree_int (size, align) == 0
8565 && align <= BITS_PER_WORD)
8566 return;
8567
8568 for (gnat_node = First_Rep_Item (gnat_entity);
8569 Present (gnat_node);
8570 gnat_node = Next_Rep_Item (gnat_node))
8571 if (Nkind (gnat_node) == N_Pragma)
8572 {
8573 unsigned char pragma_id
8574 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8575
8576 if ((pragma_id == Pragma_Atomic && !component_p)
8577 || (pragma_id == Pragma_Atomic_Components && component_p))
8578 {
8579 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8580 break;
8581 }
8582 }
8583
8584 if (component_p)
8585 post_error_ne ("atomic access to component of & cannot be guaranteed",
8586 gnat_error_point, gnat_entity);
8587 else if (Is_Volatile_Full_Access (gnat_entity))
8588 post_error_ne ("volatile full access to & cannot be guaranteed",
8589 gnat_error_point, gnat_entity);
8590 else
8591 post_error_ne ("atomic access to & cannot be guaranteed",
8592 gnat_error_point, gnat_entity);
8593 }
8594 \f
8595
8596 /* Helper for the intrin compatibility checks family. Evaluate whether
8597 two types are definitely incompatible. */
8598
8599 static bool
8600 intrin_types_incompatible_p (tree t1, tree t2)
8601 {
8602 enum tree_code code;
8603
8604 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8605 return false;
8606
8607 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8608 return true;
8609
8610 if (TREE_CODE (t1) != TREE_CODE (t2))
8611 return true;
8612
8613 code = TREE_CODE (t1);
8614
8615 switch (code)
8616 {
8617 case INTEGER_TYPE:
8618 case REAL_TYPE:
8619 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8620
8621 case POINTER_TYPE:
8622 case REFERENCE_TYPE:
8623 /* Assume designated types are ok. We'd need to account for char * and
8624 void * variants to do better, which could rapidly get messy and isn't
8625 clearly worth the effort. */
8626 return false;
8627
8628 default:
8629 break;
8630 }
8631
8632 return false;
8633 }
8634
8635 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8636 on the Ada/builtin argument lists for the INB binding. */
8637
8638 static bool
8639 intrin_arglists_compatible_p (intrin_binding_t * inb)
8640 {
8641 function_args_iterator ada_iter, btin_iter;
8642
8643 function_args_iter_init (&ada_iter, inb->ada_fntype);
8644 function_args_iter_init (&btin_iter, inb->btin_fntype);
8645
8646 /* Sequence position of the last argument we checked. */
8647 int argpos = 0;
8648
8649 while (true)
8650 {
8651 tree ada_type = function_args_iter_cond (&ada_iter);
8652 tree btin_type = function_args_iter_cond (&btin_iter);
8653
8654 /* If we've exhausted both lists simultaneously, we're done. */
8655 if (!ada_type && !btin_type)
8656 break;
8657
8658 /* If one list is shorter than the other, they fail to match. */
8659 if (!ada_type || !btin_type)
8660 return false;
8661
8662 /* If we're done with the Ada args and not with the internal builtin
8663 args, or the other way around, complain. */
8664 if (ada_type == void_type_node
8665 && btin_type != void_type_node)
8666 {
8667 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8668 return false;
8669 }
8670
8671 if (btin_type == void_type_node
8672 && ada_type != void_type_node)
8673 {
8674 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8675 inb->gnat_entity, inb->gnat_entity, argpos);
8676 return false;
8677 }
8678
8679 /* Otherwise, check that types match for the current argument. */
8680 argpos ++;
8681 if (intrin_types_incompatible_p (ada_type, btin_type))
8682 {
8683 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8684 inb->gnat_entity, inb->gnat_entity, argpos);
8685 return false;
8686 }
8687
8688
8689 function_args_iter_next (&ada_iter);
8690 function_args_iter_next (&btin_iter);
8691 }
8692
8693 return true;
8694 }
8695
8696 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8697 on the Ada/builtin return values for the INB binding. */
8698
8699 static bool
8700 intrin_return_compatible_p (intrin_binding_t * inb)
8701 {
8702 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8703 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8704
8705 /* Accept function imported as procedure, common and convenient. */
8706 if (VOID_TYPE_P (ada_return_type)
8707 && !VOID_TYPE_P (btin_return_type))
8708 return true;
8709
8710 /* If return type is Address (integer type), map it to void *. */
8711 if (Is_Descendant_Of_Address (Etype (inb->gnat_entity)))
8712 ada_return_type = ptr_type_node;
8713
8714 /* Check return types compatibility otherwise. Note that this
8715 handles void/void as well. */
8716 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8717 {
8718 post_error ("?intrinsic binding type mismatch on return value!",
8719 inb->gnat_entity);
8720 return false;
8721 }
8722
8723 return true;
8724 }
8725
8726 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8727 compatible. Issue relevant warnings when they are not.
8728
8729 This is intended as a light check to diagnose the most obvious cases, not
8730 as a full fledged type compatibility predicate. It is the programmer's
8731 responsibility to ensure correctness of the Ada declarations in Imports,
8732 especially when binding straight to a compiler internal. */
8733
8734 static bool
8735 intrin_profiles_compatible_p (intrin_binding_t * inb)
8736 {
8737 /* Check compatibility on return values and argument lists, each responsible
8738 for posting warnings as appropriate. Ensure use of the proper sloc for
8739 this purpose. */
8740
8741 bool arglists_compatible_p, return_compatible_p;
8742 location_t saved_location = input_location;
8743
8744 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8745
8746 return_compatible_p = intrin_return_compatible_p (inb);
8747 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8748
8749 input_location = saved_location;
8750
8751 return return_compatible_p && arglists_compatible_p;
8752 }
8753 \f
8754 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8755 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8756 specified size for this field. POS_LIST is a position list describing
8757 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8758 to this layout. */
8759
8760 static tree
8761 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8762 tree size, tree pos_list,
8763 vec<subst_pair> subst_list)
8764 {
8765 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8766 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8767 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8768 tree new_pos, new_field;
8769 unsigned int i;
8770 subst_pair *s;
8771
8772 if (CONTAINS_PLACEHOLDER_P (pos))
8773 FOR_EACH_VEC_ELT (subst_list, i, s)
8774 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8775
8776 /* If the position is now a constant, we can set it as the position of the
8777 field when we make it. Otherwise, we need to deal with it specially. */
8778 if (TREE_CONSTANT (pos))
8779 new_pos = bit_from_pos (pos, bitpos);
8780 else
8781 new_pos = NULL_TREE;
8782
8783 new_field
8784 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8785 size, new_pos, DECL_PACKED (old_field),
8786 !DECL_NONADDRESSABLE_P (old_field));
8787
8788 if (!new_pos)
8789 {
8790 normalize_offset (&pos, &bitpos, offset_align);
8791 /* Finalize the position. */
8792 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8793 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8794 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8795 DECL_SIZE (new_field) = size;
8796 DECL_SIZE_UNIT (new_field)
8797 = convert (sizetype,
8798 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8799 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8800 }
8801
8802 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8803 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8804 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8805 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8806
8807 return new_field;
8808 }
8809
8810 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8811 it is the minimal size the REP_PART must have. */
8812
8813 static tree
8814 create_rep_part (tree rep_type, tree record_type, tree min_size)
8815 {
8816 tree field;
8817
8818 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8819 min_size = NULL_TREE;
8820
8821 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8822 min_size, NULL_TREE, 0, 1);
8823 DECL_INTERNAL_P (field) = 1;
8824
8825 return field;
8826 }
8827
8828 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8829
8830 static tree
8831 get_rep_part (tree record_type)
8832 {
8833 tree field = TYPE_FIELDS (record_type);
8834
8835 /* The REP part is the first field, internal, another record, and its name
8836 starts with an 'R'. */
8837 if (field
8838 && DECL_INTERNAL_P (field)
8839 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8840 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8841 return field;
8842
8843 return NULL_TREE;
8844 }
8845
8846 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8847
8848 tree
8849 get_variant_part (tree record_type)
8850 {
8851 tree field;
8852
8853 /* The variant part is the only internal field that is a qualified union. */
8854 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8855 if (DECL_INTERNAL_P (field)
8856 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8857 return field;
8858
8859 return NULL_TREE;
8860 }
8861
8862 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8863 the list of variants to be used and RECORD_TYPE is the type of the parent.
8864 POS_LIST is a position list describing the layout of fields present in
8865 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8866 layout. */
8867
8868 static tree
8869 create_variant_part_from (tree old_variant_part,
8870 vec<variant_desc> variant_list,
8871 tree record_type, tree pos_list,
8872 vec<subst_pair> subst_list)
8873 {
8874 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8875 tree old_union_type = TREE_TYPE (old_variant_part);
8876 tree new_union_type, new_variant_part;
8877 tree union_field_list = NULL_TREE;
8878 variant_desc *v;
8879 unsigned int i;
8880
8881 /* First create the type of the variant part from that of the old one. */
8882 new_union_type = make_node (QUAL_UNION_TYPE);
8883 TYPE_NAME (new_union_type)
8884 = concat_name (TYPE_NAME (record_type),
8885 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8886
8887 /* If the position of the variant part is constant, subtract it from the
8888 size of the type of the parent to get the new size. This manual CSE
8889 reduces the code size when not optimizing. */
8890 if (TREE_CODE (offset) == INTEGER_CST)
8891 {
8892 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8893 tree first_bit = bit_from_pos (offset, bitpos);
8894 TYPE_SIZE (new_union_type)
8895 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8896 TYPE_SIZE_UNIT (new_union_type)
8897 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8898 byte_from_pos (offset, bitpos));
8899 SET_TYPE_ADA_SIZE (new_union_type,
8900 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8901 first_bit));
8902 SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type));
8903 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8904 }
8905 else
8906 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8907
8908 /* Now finish up the new variants and populate the union type. */
8909 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8910 {
8911 tree old_field = v->field, new_field;
8912 tree old_variant, old_variant_subpart, new_variant, field_list;
8913
8914 /* Skip variants that don't belong to this nesting level. */
8915 if (DECL_CONTEXT (old_field) != old_union_type)
8916 continue;
8917
8918 /* Retrieve the list of fields already added to the new variant. */
8919 new_variant = v->new_type;
8920 field_list = TYPE_FIELDS (new_variant);
8921
8922 /* If the old variant had a variant subpart, we need to create a new
8923 variant subpart and add it to the field list. */
8924 old_variant = v->type;
8925 old_variant_subpart = get_variant_part (old_variant);
8926 if (old_variant_subpart)
8927 {
8928 tree new_variant_subpart
8929 = create_variant_part_from (old_variant_subpart, variant_list,
8930 new_variant, pos_list, subst_list);
8931 DECL_CHAIN (new_variant_subpart) = field_list;
8932 field_list = new_variant_subpart;
8933 }
8934
8935 /* Finish up the new variant and create the field. No need for debug
8936 info thanks to the XVS type. */
8937 finish_record_type (new_variant, nreverse (field_list), 2, false);
8938 compute_record_mode (new_variant);
8939 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8940 Empty);
8941
8942 new_field
8943 = create_field_decl_from (old_field, new_variant, new_union_type,
8944 TYPE_SIZE (new_variant),
8945 pos_list, subst_list);
8946 DECL_QUALIFIER (new_field) = v->qual;
8947 DECL_INTERNAL_P (new_field) = 1;
8948 DECL_CHAIN (new_field) = union_field_list;
8949 union_field_list = new_field;
8950 }
8951
8952 /* Finish up the union type and create the variant part. No need for debug
8953 info thanks to the XVS type. Note that we don't reverse the field list
8954 because VARIANT_LIST has been traversed in reverse order. */
8955 finish_record_type (new_union_type, union_field_list, 2, false);
8956 compute_record_mode (new_union_type);
8957 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8958 Empty);
8959
8960 new_variant_part
8961 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8962 TYPE_SIZE (new_union_type),
8963 pos_list, subst_list);
8964 DECL_INTERNAL_P (new_variant_part) = 1;
8965
8966 /* With multiple discriminants it is possible for an inner variant to be
8967 statically selected while outer ones are not; in this case, the list
8968 of fields of the inner variant is not flattened and we end up with a
8969 qualified union with a single member. Drop the useless container. */
8970 if (!DECL_CHAIN (union_field_list))
8971 {
8972 DECL_CONTEXT (union_field_list) = record_type;
8973 DECL_FIELD_OFFSET (union_field_list)
8974 = DECL_FIELD_OFFSET (new_variant_part);
8975 DECL_FIELD_BIT_OFFSET (union_field_list)
8976 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8977 SET_DECL_OFFSET_ALIGN (union_field_list,
8978 DECL_OFFSET_ALIGN (new_variant_part));
8979 new_variant_part = union_field_list;
8980 }
8981
8982 return new_variant_part;
8983 }
8984
8985 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8986 which are both RECORD_TYPE, after applying the substitutions described
8987 in SUBST_LIST. */
8988
8989 static void
8990 copy_and_substitute_in_size (tree new_type, tree old_type,
8991 vec<subst_pair> subst_list)
8992 {
8993 unsigned int i;
8994 subst_pair *s;
8995
8996 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8997 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8998 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8999 SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type));
9000 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9001
9002 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9003 FOR_EACH_VEC_ELT (subst_list, i, s)
9004 TYPE_SIZE (new_type)
9005 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9006 s->discriminant, s->replacement);
9007
9008 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9009 FOR_EACH_VEC_ELT (subst_list, i, s)
9010 TYPE_SIZE_UNIT (new_type)
9011 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9012 s->discriminant, s->replacement);
9013
9014 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9015 FOR_EACH_VEC_ELT (subst_list, i, s)
9016 SET_TYPE_ADA_SIZE
9017 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9018 s->discriminant, s->replacement));
9019
9020 /* Finalize the size. */
9021 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9022 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9023 }
9024
9025 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9026 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9027 the original array type if it has been translated. This association is a
9028 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9029 that for standard DWARF, we also want to get the original type name. */
9030
9031 static void
9032 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
9033 {
9034 Entity_Id gnat_original_array_type
9035 = Underlying_Type (Original_Array_Type (gnat_entity));
9036 tree gnu_original_array_type;
9037
9038 if (!present_gnu_tree (gnat_original_array_type))
9039 return;
9040
9041 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
9042
9043 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
9044 return;
9045
9046 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
9047 {
9048 tree original_name = TYPE_NAME (gnu_original_array_type);
9049
9050 if (TREE_CODE (original_name) == TYPE_DECL)
9051 original_name = DECL_NAME (original_name);
9052
9053 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
9054 TYPE_NAME (gnu_type) = original_name;
9055 }
9056 else
9057 add_parallel_type (gnu_type, gnu_original_array_type);
9058 }
9059 \f
9060 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
9061 type with all size expressions that contain F in a PLACEHOLDER_EXPR
9062 updated by replacing F with R.
9063
9064 The function doesn't update the layout of the type, i.e. it assumes
9065 that the substitution is purely formal. That's why the replacement
9066 value R must itself contain a PLACEHOLDER_EXPR. */
9067
9068 tree
9069 substitute_in_type (tree t, tree f, tree r)
9070 {
9071 tree nt;
9072
9073 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9074
9075 switch (TREE_CODE (t))
9076 {
9077 case INTEGER_TYPE:
9078 case ENUMERAL_TYPE:
9079 case BOOLEAN_TYPE:
9080 case REAL_TYPE:
9081
9082 /* First the domain types of arrays. */
9083 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9084 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9085 {
9086 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9087 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9088
9089 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9090 return t;
9091
9092 nt = copy_type (t);
9093 TYPE_GCC_MIN_VALUE (nt) = low;
9094 TYPE_GCC_MAX_VALUE (nt) = high;
9095
9096 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9097 SET_TYPE_INDEX_TYPE
9098 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9099
9100 return nt;
9101 }
9102
9103 /* Then the subtypes. */
9104 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9105 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9106 {
9107 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9108 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9109
9110 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9111 return t;
9112
9113 nt = copy_type (t);
9114 SET_TYPE_RM_MIN_VALUE (nt, low);
9115 SET_TYPE_RM_MAX_VALUE (nt, high);
9116
9117 return nt;
9118 }
9119
9120 return t;
9121
9122 case COMPLEX_TYPE:
9123 nt = substitute_in_type (TREE_TYPE (t), f, r);
9124 if (nt == TREE_TYPE (t))
9125 return t;
9126
9127 return build_complex_type (nt);
9128
9129 case FUNCTION_TYPE:
9130 /* These should never show up here. */
9131 gcc_unreachable ();
9132
9133 case ARRAY_TYPE:
9134 {
9135 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9136 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9137
9138 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9139 return t;
9140
9141 nt = build_nonshared_array_type (component, domain);
9142 SET_TYPE_ALIGN (nt, TYPE_ALIGN (t));
9143 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9144 SET_TYPE_MODE (nt, TYPE_MODE (t));
9145 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9146 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9147 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
9148 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9149 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9150 return nt;
9151 }
9152
9153 case RECORD_TYPE:
9154 case UNION_TYPE:
9155 case QUAL_UNION_TYPE:
9156 {
9157 bool changed_field = false;
9158 tree field;
9159
9160 /* Start out with no fields, make new fields, and chain them
9161 in. If we haven't actually changed the type of any field,
9162 discard everything we've done and return the old type. */
9163 nt = copy_type (t);
9164 TYPE_FIELDS (nt) = NULL_TREE;
9165
9166 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9167 {
9168 tree new_field = copy_node (field), new_n;
9169
9170 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9171 if (new_n != TREE_TYPE (field))
9172 {
9173 TREE_TYPE (new_field) = new_n;
9174 changed_field = true;
9175 }
9176
9177 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9178 if (new_n != DECL_FIELD_OFFSET (field))
9179 {
9180 DECL_FIELD_OFFSET (new_field) = new_n;
9181 changed_field = true;
9182 }
9183
9184 /* Do the substitution inside the qualifier, if any. */
9185 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9186 {
9187 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9188 if (new_n != DECL_QUALIFIER (field))
9189 {
9190 DECL_QUALIFIER (new_field) = new_n;
9191 changed_field = true;
9192 }
9193 }
9194
9195 DECL_CONTEXT (new_field) = nt;
9196 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9197
9198 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9199 TYPE_FIELDS (nt) = new_field;
9200 }
9201
9202 if (!changed_field)
9203 return t;
9204
9205 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9206 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9207 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9208 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9209 return nt;
9210 }
9211
9212 default:
9213 return t;
9214 }
9215 }
9216 \f
9217 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9218 needed to represent the object. */
9219
9220 tree
9221 rm_size (tree gnu_type)
9222 {
9223 /* For integral types, we store the RM size explicitly. */
9224 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9225 return TYPE_RM_SIZE (gnu_type);
9226
9227 /* Return the RM size of the actual data plus the size of the template. */
9228 if (TREE_CODE (gnu_type) == RECORD_TYPE
9229 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9230 return
9231 size_binop (PLUS_EXPR,
9232 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9233 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9234
9235 /* For record or union types, we store the size explicitly. */
9236 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9237 && !TYPE_FAT_POINTER_P (gnu_type)
9238 && TYPE_ADA_SIZE (gnu_type))
9239 return TYPE_ADA_SIZE (gnu_type);
9240
9241 /* For other types, this is just the size. */
9242 return TYPE_SIZE (gnu_type);
9243 }
9244 \f
9245 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9246 fully-qualified name, possibly with type information encoding.
9247 Otherwise, return the name. */
9248
9249 static const char *
9250 get_entity_char (Entity_Id gnat_entity)
9251 {
9252 Get_Encoded_Name (gnat_entity);
9253 return ggc_strdup (Name_Buffer);
9254 }
9255
9256 tree
9257 get_entity_name (Entity_Id gnat_entity)
9258 {
9259 Get_Encoded_Name (gnat_entity);
9260 return get_identifier_with_length (Name_Buffer, Name_Len);
9261 }
9262
9263 /* Return an identifier representing the external name to be used for
9264 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9265 and the specified suffix. */
9266
9267 tree
9268 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9269 {
9270 const Entity_Kind kind = Ekind (gnat_entity);
9271 const bool has_suffix = (suffix != NULL);
9272 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9273 String_Pointer sp = {suffix, &temp};
9274
9275 Get_External_Name (gnat_entity, has_suffix, sp);
9276
9277 /* A variable using the Stdcall convention lives in a DLL. We adjust
9278 its name to use the jump table, the _imp__NAME contains the address
9279 for the NAME variable. */
9280 if ((kind == E_Variable || kind == E_Constant)
9281 && Has_Stdcall_Convention (gnat_entity))
9282 {
9283 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9284 char *new_name = (char *) alloca (len + 1);
9285 strcpy (new_name, STDCALL_PREFIX);
9286 strcat (new_name, Name_Buffer);
9287 return get_identifier_with_length (new_name, len);
9288 }
9289
9290 return get_identifier_with_length (Name_Buffer, Name_Len);
9291 }
9292
9293 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9294 string, return a new IDENTIFIER_NODE that is the concatenation of
9295 the name followed by "___" and the specified suffix. */
9296
9297 tree
9298 concat_name (tree gnu_name, const char *suffix)
9299 {
9300 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9301 char *new_name = (char *) alloca (len + 1);
9302 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9303 strcat (new_name, "___");
9304 strcat (new_name, suffix);
9305 return get_identifier_with_length (new_name, len);
9306 }
9307
9308 /* Initialize data structures of the decl.c module. */
9309
9310 void
9311 init_gnat_decl (void)
9312 {
9313 /* Initialize the cache of annotated values. */
9314 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9315
9316 /* Initialize the association of dummy types with subprograms. */
9317 dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512);
9318 }
9319
9320 /* Destroy data structures of the decl.c module. */
9321
9322 void
9323 destroy_gnat_decl (void)
9324 {
9325 /* Destroy the cache of annotated values. */
9326 annotate_value_cache->empty ();
9327 annotate_value_cache = NULL;
9328
9329 /* Destroy the association of dummy types with subprograms. */
9330 dummy_to_subprog_map->empty ();
9331 dummy_to_subprog_map = NULL;
9332 }
9333
9334 #include "gt-ada-decl.h"