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