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