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