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