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