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