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