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