* decl.c (gnat_to_gnu_entity): Use TREE_OVERFLOW instead of
[official-gcc.git] / gcc / ada / decl.c
bloba7ee5ce2be8fc0dd610db69cf51c45ffed4acc5e
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2007, 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 2, 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 distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 * *
25 ****************************************************************************/
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "convert.h"
35 #include "ggc.h"
36 #include "obstack.h"
37 #include "target.h"
38 #include "expr.h"
40 #include "ada.h"
41 #include "types.h"
42 #include "atree.h"
43 #include "elists.h"
44 #include "namet.h"
45 #include "nlists.h"
46 #include "repinfo.h"
47 #include "snames.h"
48 #include "stringt.h"
49 #include "uintp.h"
50 #include "fe.h"
51 #include "sinfo.h"
52 #include "einfo.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 /* Convention_Stdcall should be processed in a specific way on Windows targets
57 only. The macro below is a helper to avoid having to check for a Windows
58 specific attribute throughout this unit. */
60 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
61 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
62 #else
63 #define Has_Stdcall_Convention(E) (0)
64 #endif
66 /* These two variables are used to defer recursively expanding incomplete
67 types while we are processing a record or subprogram type. */
69 static int defer_incomplete_level = 0;
70 static struct incomplete
72 struct incomplete *next;
73 tree old_type;
74 Entity_Id full_type;
75 } *defer_incomplete_list = 0;
77 /* These two variables are used to defer emission of debug information for
78 nested incomplete record types */
80 static int defer_debug_level = 0;
81 static tree defer_debug_incomplete_list;
83 static void copy_alias_set (tree, tree);
84 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
85 static bool allocatable_size_p (tree, bool);
86 static void prepend_attributes (Entity_Id, struct attrib **);
87 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
88 static bool is_variable_size (tree);
89 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
90 bool, bool);
91 static tree make_packable_type (tree);
92 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
93 static bool same_discriminant_p (Entity_Id, Entity_Id);
94 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
95 bool, bool, bool, bool);
96 static int compare_field_bitpos (const PTR, const PTR);
97 static Uint annotate_value (tree);
98 static void annotate_rep (Entity_Id, tree);
99 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
100 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
101 static void set_rm_size (Uint, tree, Entity_Id);
102 static tree make_type_from_size (tree, tree, bool);
103 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
104 static void check_ok_for_atomic (tree, Entity_Id, bool);
105 static int compatible_signatures_p (tree ftype1, tree ftype2);
107 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
108 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
109 refer to an Ada type. */
111 tree
112 gnat_to_gnu_type (Entity_Id gnat_entity)
114 tree gnu_decl;
116 /* The back end never attempts to annotate generic types */
117 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
118 return void_type_node;
120 /* Convert the ada entity type into a GCC TYPE_DECL node. */
121 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
122 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
123 return TREE_TYPE (gnu_decl);
126 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
127 entity, this routine returns the equivalent GCC tree for that entity
128 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
129 defining identifier.
131 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
132 initial value (in GCC tree form). This is optional for variables.
133 For renamed entities, GNU_EXPR gives the object being renamed.
135 DEFINITION is nonzero if this call is intended for a definition. This is
136 used for separate compilation where it necessary to know whether an
137 external declaration or a definition should be created if the GCC equivalent
138 was not created previously. The value of 1 is normally used for a nonzero
139 DEFINITION, but a value of 2 is used in special circumstances, defined in
140 the code. */
142 tree
143 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
145 tree gnu_entity_id;
146 tree gnu_type = NULL_TREE;
147 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
148 GNAT tree. This node will be associated with the GNAT node by calling
149 the save_gnu_tree routine at the end of the `switch' statement. */
150 tree gnu_decl = NULL_TREE;
151 /* true if we have already saved gnu_decl as a gnat association. */
152 bool saved = false;
153 /* Nonzero if we incremented defer_incomplete_level. */
154 bool this_deferred = false;
155 /* Nonzero if we incremented defer_debug_level. */
156 bool debug_deferred = false;
157 /* Nonzero if we incremented force_global. */
158 bool this_global = false;
159 /* Nonzero if we should check to see if elaborated during processing. */
160 bool maybe_present = false;
161 /* Nonzero if we made GNU_DECL and its type here. */
162 bool this_made_decl = false;
163 struct attrib *attr_list = NULL;
164 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
165 || debug_info_level == DINFO_LEVEL_VERBOSE);
166 Entity_Kind kind = Ekind (gnat_entity);
167 Entity_Id gnat_temp;
168 unsigned int esize
169 = ((Known_Esize (gnat_entity)
170 && UI_Is_In_Int_Range (Esize (gnat_entity)))
171 ? MIN (UI_To_Int (Esize (gnat_entity)),
172 IN (kind, Float_Kind)
173 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
174 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
175 : LONG_LONG_TYPE_SIZE)
176 : LONG_LONG_TYPE_SIZE);
177 tree gnu_size = 0;
178 bool imported_p
179 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
180 unsigned int align = 0;
182 /* Since a use of an Itype is a definition, process it as such if it
183 is not in a with'ed unit. */
185 if (!definition && Is_Itype (gnat_entity)
186 && !present_gnu_tree (gnat_entity)
187 && In_Extended_Main_Code_Unit (gnat_entity))
189 /* Ensure that we are in a subprogram mentioned in the Scope
190 chain of this entity, our current scope is global,
191 or that we encountered a task or entry (where we can't currently
192 accurately check scoping). */
193 if (!current_function_decl
194 || DECL_ELABORATION_PROC_P (current_function_decl))
196 process_type (gnat_entity);
197 return get_gnu_tree (gnat_entity);
200 for (gnat_temp = Scope (gnat_entity);
201 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
203 if (Is_Type (gnat_temp))
204 gnat_temp = Underlying_Type (gnat_temp);
206 if (Ekind (gnat_temp) == E_Subprogram_Body)
207 gnat_temp
208 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
210 if (IN (Ekind (gnat_temp), Subprogram_Kind)
211 && Present (Protected_Body_Subprogram (gnat_temp)))
212 gnat_temp = Protected_Body_Subprogram (gnat_temp);
214 if (Ekind (gnat_temp) == E_Entry
215 || Ekind (gnat_temp) == E_Entry_Family
216 || Ekind (gnat_temp) == E_Task_Type
217 || (IN (Ekind (gnat_temp), Subprogram_Kind)
218 && present_gnu_tree (gnat_temp)
219 && (current_function_decl
220 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
222 process_type (gnat_entity);
223 return get_gnu_tree (gnat_entity);
227 /* This abort means the entity "gnat_entity" has an incorrect scope,
228 i.e. that its scope does not correspond to the subprogram in which
229 it is declared */
230 gcc_unreachable ();
233 /* If this is entity 0, something went badly wrong. */
234 gcc_assert (Present (gnat_entity));
236 /* If we've already processed this entity, return what we got last time.
237 If we are defining the node, we should not have already processed it.
238 In that case, we will abort below when we try to save a new GCC tree for
239 this object. We also need to handle the case of getting a dummy type
240 when a Full_View exists. */
242 if (present_gnu_tree (gnat_entity)
243 && (! definition
244 || (Is_Type (gnat_entity) && imported_p)))
246 gnu_decl = get_gnu_tree (gnat_entity);
248 if (TREE_CODE (gnu_decl) == TYPE_DECL
249 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
250 && IN (kind, Incomplete_Or_Private_Kind)
251 && Present (Full_View (gnat_entity)))
253 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
254 NULL_TREE, 0);
256 save_gnu_tree (gnat_entity, NULL_TREE, false);
257 save_gnu_tree (gnat_entity, gnu_decl, false);
260 return gnu_decl;
263 /* If this is a numeric or enumeral type, or an access type, a nonzero
264 Esize must be specified unless it was specified by the programmer. */
265 gcc_assert (!Unknown_Esize (gnat_entity)
266 || Has_Size_Clause (gnat_entity)
267 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
268 && (!IN (kind, Access_Kind)
269 || kind == E_Access_Protected_Subprogram_Type
270 || kind == E_Access_Subtype)));
272 /* Likewise, RM_Size must be specified for all discrete and fixed-point
273 types. */
274 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
275 || !Unknown_RM_Size (gnat_entity));
277 /* Get the name of the entity and set up the line number and filename of
278 the original definition for use in any decl we make. */
279 gnu_entity_id = get_entity_name (gnat_entity);
280 Sloc_to_locus (Sloc (gnat_entity), &input_location);
282 /* If we get here, it means we have not yet done anything with this
283 entity. If we are not defining it here, it must be external,
284 otherwise we should have defined it already. */
285 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
286 || kind == E_Discriminant || kind == E_Component
287 || kind == E_Label
288 || (kind == E_Constant && Present (Full_View (gnat_entity)))
289 || IN (kind, Type_Kind));
291 /* For cases when we are not defining (i.e., we are referencing from
292 another compilation unit) Public entities, show we are at global level
293 for the purpose of computing scopes. Don't do this for components or
294 discriminants since the relevant test is whether or not the record is
295 being defined. But do this for Imported functions or procedures in
296 all cases. */
297 if ((!definition && Is_Public (gnat_entity)
298 && !Is_Statically_Allocated (gnat_entity)
299 && kind != E_Discriminant && kind != E_Component)
300 || (Is_Imported (gnat_entity)
301 && (kind == E_Function || kind == E_Procedure)))
302 force_global++, this_global = true;
304 /* Handle any attributes directly attached to the entity. */
305 if (Has_Gigi_Rep_Item (gnat_entity))
306 prepend_attributes (gnat_entity, &attr_list);
308 /* Machine_Attributes on types are expected to be propagated to subtypes.
309 The corresponding Gigi_Rep_Items are only attached to the first subtype
310 though, so we handle the propagation here. */
311 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
312 && !Is_First_Subtype (gnat_entity)
313 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
314 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
316 switch (kind)
318 case E_Constant:
319 /* If this is a use of a deferred constant, get its full
320 declaration. */
321 if (!definition && Present (Full_View (gnat_entity)))
323 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
324 gnu_expr, definition);
325 saved = true;
326 break;
329 /* If we have an external constant that we are not defining,
330 get the expression that is was defined to represent. We
331 may throw that expression away later if it is not a
332 constant.
333 Do not retrieve the expression if it is an aggregate, because
334 in complex instantiation contexts it may not be expanded */
336 if (!definition
337 && Present (Expression (Declaration_Node (gnat_entity)))
338 && !No_Initialization (Declaration_Node (gnat_entity))
339 && (Nkind (Expression (Declaration_Node (gnat_entity)))
340 != N_Aggregate))
341 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
343 /* Ignore deferred constant definitions; they are processed fully in the
344 front-end. For deferred constant references, get the full
345 definition. On the other hand, constants that are renamings are
346 handled like variable renamings. If No_Initialization is set, this is
347 not a deferred constant but a constant whose value is built
348 manually. */
350 if (definition && !gnu_expr
351 && !No_Initialization (Declaration_Node (gnat_entity))
352 && No (Renamed_Object (gnat_entity)))
354 gnu_decl = error_mark_node;
355 saved = true;
356 break;
358 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
359 && Present (Full_View (gnat_entity)))
361 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
362 NULL_TREE, 0);
363 saved = true;
364 break;
367 goto object;
369 case E_Exception:
370 /* We used to special case VMS exceptions here to directly map them to
371 their associated condition code. Since this code had to be masked
372 dynamically to strip off the severity bits, this caused trouble in
373 the GCC/ZCX case because the "type" pointers we store in the tables
374 have to be static. We now don't special case here anymore, and let
375 the regular processing take place, which leaves us with a regular
376 exception data object for VMS exceptions too. The condition code
377 mapping is taken care of by the front end and the bitmasking by the
378 runtime library. */
379 goto object;
381 case E_Discriminant:
382 case E_Component:
384 /* The GNAT record where the component was defined. */
385 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
387 /* If the variable is an inherited record component (in the case of
388 extended record types), just return the inherited entity, which
389 must be a FIELD_DECL. Likewise for discriminants.
390 For discriminants of untagged records which have explicit
391 stored discriminants, return the entity for the corresponding
392 stored discriminant. Also use Original_Record_Component
393 if the record has a private extension. */
395 if (Present (Original_Record_Component (gnat_entity))
396 && Original_Record_Component (gnat_entity) != gnat_entity)
398 gnu_decl
399 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
400 gnu_expr, definition);
401 saved = true;
402 break;
405 /* If the enclosing record has explicit stored discriminants,
406 then it is an untagged record. If the Corresponding_Discriminant
407 is not empty then this must be a renamed discriminant and its
408 Original_Record_Component must point to the corresponding explicit
409 stored discriminant (i.e., we should have taken the previous
410 branch). */
412 else if (Present (Corresponding_Discriminant (gnat_entity))
413 && Is_Tagged_Type (gnat_record))
415 /* A tagged record has no explicit stored discriminants. */
417 gcc_assert (First_Discriminant (gnat_record)
418 == First_Stored_Discriminant (gnat_record));
419 gnu_decl
420 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
421 gnu_expr, definition);
422 saved = true;
423 break;
426 else if (Present (CR_Discriminant (gnat_entity))
427 && type_annotate_only)
429 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
430 gnu_expr, definition);
431 saved = 1;
432 break;
435 /* If the enclosing record has explicit stored discriminants,
436 then it is an untagged record. If the Corresponding_Discriminant
437 is not empty then this must be a renamed discriminant and its
438 Original_Record_Component must point to the corresponding explicit
439 stored discriminant (i.e., we should have taken the first
440 branch). */
442 else if (Present (Corresponding_Discriminant (gnat_entity))
443 && (First_Discriminant (gnat_record)
444 != First_Stored_Discriminant (gnat_record)))
445 gcc_unreachable ();
447 /* Otherwise, if we are not defining this and we have no GCC type
448 for the containing record, make one for it. Then we should
449 have made our own equivalent. */
450 else if (!definition && !present_gnu_tree (gnat_record))
452 /* ??? If this is in a record whose scope is a protected
453 type and we have an Original_Record_Component, use it.
454 This is a workaround for major problems in protected type
455 handling. */
457 Entity_Id Scop = Scope (Scope (gnat_entity));
458 if ((Is_Protected_Type (Scop)
459 || (Is_Private_Type (Scop)
460 && Present (Full_View (Scop))
461 && Is_Protected_Type (Full_View (Scop))))
462 && Present (Original_Record_Component (gnat_entity)))
464 gnu_decl
465 = gnat_to_gnu_entity (Original_Record_Component
466 (gnat_entity),
467 gnu_expr, definition);
468 saved = true;
469 break;
472 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
473 gnu_decl = get_gnu_tree (gnat_entity);
474 saved = true;
475 break;
478 else
479 /* Here we have no GCC type and this is a reference rather than a
480 definition. This should never happen. Most likely the cause is a
481 reference before declaration in the gnat tree for gnat_entity. */
482 gcc_unreachable ();
485 case E_Loop_Parameter:
486 case E_Out_Parameter:
487 case E_Variable:
489 /* Simple variables, loop variables, OUT parameters, and exceptions. */
490 object:
492 bool used_by_ref = false;
493 bool const_flag
494 = ((kind == E_Constant || kind == E_Variable)
495 && !Is_Statically_Allocated (gnat_entity)
496 && Is_True_Constant (gnat_entity)
497 && (((Nkind (Declaration_Node (gnat_entity))
498 == N_Object_Declaration)
499 && Present (Expression (Declaration_Node (gnat_entity))))
500 || Present (Renamed_Object (gnat_entity))));
501 bool inner_const_flag = const_flag;
502 bool static_p = Is_Statically_Allocated (gnat_entity);
503 bool mutable_p = false;
504 tree gnu_ext_name = NULL_TREE;
505 tree renamed_obj = NULL_TREE;
507 if (Present (Renamed_Object (gnat_entity)) && !definition)
509 if (kind == E_Exception)
510 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
511 NULL_TREE, 0);
512 else
513 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
516 /* Get the type after elaborating the renamed object. */
517 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
519 /* If this is a loop variable, its type should be the base type.
520 This is because the code for processing a loop determines whether
521 a normal loop end test can be done by comparing the bounds of the
522 loop against those of the base type, which is presumed to be the
523 size used for computation. But this is not correct when the size
524 of the subtype is smaller than the type. */
525 if (kind == E_Loop_Parameter)
526 gnu_type = get_base_type (gnu_type);
528 /* Reject non-renamed objects whose types are unconstrained arrays or
529 any object whose type is a dummy type or VOID_TYPE. */
531 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
532 && No (Renamed_Object (gnat_entity)))
533 || TYPE_IS_DUMMY_P (gnu_type)
534 || TREE_CODE (gnu_type) == VOID_TYPE)
536 gcc_assert (type_annotate_only);
537 if (this_global)
538 force_global--;
539 return error_mark_node;
542 /* If an alignment is specified, use it if valid. Note that
543 exceptions are objects but don't have alignments. We must do this
544 before we validate the size, since the alignment can affect the
545 size. */
546 if (kind != E_Exception && Known_Alignment (gnat_entity))
548 gcc_assert (Present (Alignment (gnat_entity)));
549 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
550 TYPE_ALIGN (gnu_type));
551 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
552 gnat_entity, "PAD", 0, definition, 1);
555 /* If we are defining the object, see if it has a Size value and
556 validate it if so. If we are not defining the object and a Size
557 clause applies, simply retrieve the value. We don't want to ignore
558 the clause and it is expected to have been validated already. Then
559 get the new type, if any. */
560 if (definition)
561 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
562 gnat_entity, VAR_DECL, false,
563 Has_Size_Clause (gnat_entity));
564 else if (Has_Size_Clause (gnat_entity))
565 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
567 if (gnu_size)
569 gnu_type
570 = make_type_from_size (gnu_type, gnu_size,
571 Has_Biased_Representation (gnat_entity));
573 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
574 gnu_size = NULL_TREE;
577 /* If this object has self-referential size, it must be a record with
578 a default value. We are supposed to allocate an object of the
579 maximum size in this case unless it is a constant with an
580 initializing expression, in which case we can get the size from
581 that. Note that the resulting size may still be a variable, so
582 this may end up with an indirect allocation. */
584 if (No (Renamed_Object (gnat_entity))
585 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
587 if (gnu_expr && kind == E_Constant)
588 gnu_size
589 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
590 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
592 /* We may have no GNU_EXPR because No_Initialization is
593 set even though there's an Expression. */
594 else if (kind == E_Constant
595 && (Nkind (Declaration_Node (gnat_entity))
596 == N_Object_Declaration)
597 && Present (Expression (Declaration_Node (gnat_entity))))
598 gnu_size
599 = TYPE_SIZE (gnat_to_gnu_type
600 (Etype
601 (Expression (Declaration_Node (gnat_entity)))));
602 else
604 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
605 mutable_p = true;
609 /* If the size is zero bytes, make it one byte since some linkers have
610 trouble with zero-sized objects. If the object will have a
611 template, that will make it nonzero so don't bother. Also avoid
612 doing that for an object renaming or an object with an address
613 clause, as we would lose useful information on the view size
614 (e.g. for null array slices) and we are not allocating the object
615 here anyway. */
616 if (((gnu_size && integer_zerop (gnu_size))
617 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
618 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
619 || !Is_Array_Type (Etype (gnat_entity)))
620 && !Present (Renamed_Object (gnat_entity))
621 && !Present (Address_Clause (gnat_entity)))
622 gnu_size = bitsize_unit_node;
624 /* If this is an atomic object with no specified size and alignment,
625 but where the size of the type is a constant, set the alignment to
626 the lowest power of two greater than the size, or to the
627 biggest meaningful alignment, whichever is smaller. */
629 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
630 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
632 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
633 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
634 BIGGEST_ALIGNMENT))
635 align = BIGGEST_ALIGNMENT;
636 else
637 align = ((unsigned int) 1
638 << (floor_log2 (tree_low_cst
639 (TYPE_SIZE (gnu_type), 1) - 1)
640 + 1));
643 /* If the object is set to have atomic components, find the component
644 type and validate it.
646 ??? Note that we ignore Has_Volatile_Components on objects; it's
647 not at all clear what to do in that case. */
649 if (Has_Atomic_Components (gnat_entity))
651 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
652 ? TREE_TYPE (gnu_type) : gnu_type);
654 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
655 && TYPE_MULTI_ARRAY_P (gnu_inner))
656 gnu_inner = TREE_TYPE (gnu_inner);
658 check_ok_for_atomic (gnu_inner, gnat_entity, true);
661 /* Now check if the type of the object allows atomic access. Note
662 that we must test the type, even if this object has size and
663 alignment to allow such access, because we will be going
664 inside the padded record to assign to the object. We could fix
665 this by always copying via an intermediate value, but it's not
666 clear it's worth the effort. */
667 if (Is_Atomic (gnat_entity))
668 check_ok_for_atomic (gnu_type, gnat_entity, false);
670 /* If this is an aliased object with an unconstrained nominal subtype,
671 make a type that includes the template. */
672 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
673 && Is_Array_Type (Etype (gnat_entity))
674 && !type_annotate_only)
676 tree gnu_fat
677 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
679 gnu_type
680 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
681 concat_id_with_name (gnu_entity_id,
682 "UNC"));
685 #ifdef MINIMUM_ATOMIC_ALIGNMENT
686 /* If the size is a constant and no alignment is specified, force
687 the alignment to be the minimum valid atomic alignment. The
688 restriction on constant size avoids problems with variable-size
689 temporaries; if the size is variable, there's no issue with
690 atomic access. Also don't do this for a constant, since it isn't
691 necessary and can interfere with constant replacement. Finally,
692 do not do it for Out parameters since that creates an
693 size inconsistency with In parameters. */
694 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
695 && !FLOAT_TYPE_P (gnu_type)
696 && !const_flag && No (Renamed_Object (gnat_entity))
697 && !imported_p && No (Address_Clause (gnat_entity))
698 && kind != E_Out_Parameter
699 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
700 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
701 align = MINIMUM_ATOMIC_ALIGNMENT;
702 #endif
704 /* Make a new type with the desired size and alignment, if needed. */
705 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
706 "PAD", false, definition, true);
708 /* Make a volatile version of this object's type if we are to
709 make the object volatile. Note that 13.3(19) says that we
710 should treat other types of objects as volatile as well. */
711 if ((Treat_As_Volatile (gnat_entity)
712 || Is_Exported (gnat_entity)
713 || Is_Imported (gnat_entity)
714 || Present (Address_Clause (gnat_entity)))
715 && !TYPE_VOLATILE (gnu_type))
716 gnu_type = build_qualified_type (gnu_type,
717 (TYPE_QUALS (gnu_type)
718 | TYPE_QUAL_VOLATILE));
720 /* Convert the expression to the type of the object except in the
721 case where the object's type is unconstrained or the object's type
722 is a padded record whose field is of self-referential size. In
723 the former case, converting will generate unnecessary evaluations
724 of the CONSTRUCTOR to compute the size and in the latter case, we
725 want to only copy the actual data. */
726 if (gnu_expr
727 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
728 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
729 && !(TREE_CODE (gnu_type) == RECORD_TYPE
730 && TYPE_IS_PADDING_P (gnu_type)
731 && (CONTAINS_PLACEHOLDER_P
732 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
733 gnu_expr = convert (gnu_type, gnu_expr);
735 /* See if this is a renaming, and handle appropriately depending on
736 what is renamed and in which context. There are three major
737 cases:
739 1/ This is a constant renaming and we can just make an object
740 with what is renamed as its initial value,
742 2/ We can reuse a stabilized version of what is renamed in place
743 of the renaming,
745 3/ If neither 1 or 2 applies, we make the renaming entity a constant
746 pointer to what is being renamed. */
748 if (Present (Renamed_Object (gnat_entity)))
750 /* If the renamed object had padding, strip off the reference
751 to the inner object and reset our type. */
752 if (TREE_CODE (gnu_expr) == COMPONENT_REF
753 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
754 == RECORD_TYPE)
755 && (TYPE_IS_PADDING_P
756 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
758 gnu_expr = TREE_OPERAND (gnu_expr, 0);
759 gnu_type = TREE_TYPE (gnu_expr);
762 /* Case 1: If this is a constant renaming, treat it as a normal
763 object whose initial value is what is being renamed. We cannot
764 do this if the type is unconstrained or class-wide. */
765 if (const_flag
766 && !TREE_SIDE_EFFECTS (gnu_expr)
767 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
768 && TYPE_MODE (gnu_type) != BLKmode
769 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
770 && !Is_Array_Type (Etype (gnat_entity)))
773 /* Otherwise, see if we can proceed with a stabilized version of
774 the renamed entity or if we need to make a pointer. */
775 else
777 bool stabilized = false;
778 tree maybe_stable_expr = NULL_TREE;
780 /* Case 2: If the renaming entity need not be materialized and
781 the renamed expression is something we can stabilize, use
782 that for the renaming. At the global level, we can only do
783 this if we know no SAVE_EXPRs need be made, because the
784 expression we return might be used in arbitrary conditional
785 branches so we must force the SAVE_EXPRs evaluation
786 immediately and this requires a function context. */
787 if (!Materialize_Entity (gnat_entity)
788 && (!global_bindings_p ()
789 || (staticp (gnu_expr)
790 && !TREE_SIDE_EFFECTS (gnu_expr))))
792 maybe_stable_expr
793 = maybe_stabilize_reference (gnu_expr, true, false,
794 &stabilized);
796 if (stabilized)
798 gnu_decl = maybe_stable_expr;
799 save_gnu_tree (gnat_entity, gnu_decl, true);
800 saved = true;
801 break;
804 /* The stabilization failed. Keep maybe_stable_expr
805 untouched here to let the pointer case below know
806 about that failure. */
809 /* Case 3: Make this into a constant pointer to the object we
810 are to rename and attach the object to the pointer if it is
811 an lvalue that can be stabilized.
813 From the proper scope, attached objects will be referenced
814 directly instead of indirectly via the pointer to avoid
815 subtle aliasing problems with non addressable entities.
816 They have to be stable because we must not evaluate the
817 variables in the expression every time the renaming is used.
818 They also have to be lvalues because the context in which
819 they are reused sometimes requires so. We call pointers
820 with an attached object "renaming" pointers.
822 In the rare cases where we cannot stabilize the renamed
823 object, we just make a "bare" pointer, and the renamed
824 entity is always accessed indirectly through it. */
826 inner_const_flag = TREE_READONLY (gnu_expr);
827 const_flag = true;
828 gnu_type = build_reference_type (gnu_type);
830 /* If a previous attempt at unrestricted stabilization
831 failed, there is no point trying again and we can reuse
832 the result without attaching it to the pointer. In this
833 case it will only be used as the initializing expression
834 of the pointer and thus needs no special treatment with
835 regard to multiple evaluations. */
836 if (maybe_stable_expr)
839 /* Otherwise, try to stabilize now, restricting to lvalues
840 only, and attach the expression to the pointer if the
841 stabilization succeeds.
843 Note that this might introduce SAVE_EXPRs and we don't
844 check whether we're at the global level or not. This is
845 fine since we are building a pointer initializer and
846 neither the pointer nor the initializing expression can
847 be accessed before the pointer elaboration has taken
848 place in a correct program.
850 SAVE_EXPRs will be evaluated at the right spots by either
851 create_var_decl->expand_decl_init for the non-global case
852 or build_unit_elab for the global case, and will be
853 attached to the elaboration procedure by the RTL expander
854 in the latter case. We have no need to force an early
855 evaluation here. */
856 else
858 maybe_stable_expr
859 = maybe_stabilize_reference (gnu_expr, true, true,
860 &stabilized);
862 if (stabilized)
863 renamed_obj = maybe_stable_expr;
865 /* Attaching is actually performed downstream, as soon
866 as we have a VAR_DECL for the pointer we make. */
869 gnu_expr
870 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
872 gnu_size = NULL_TREE;
873 used_by_ref = true;
878 /* If this is an aliased object whose nominal subtype is unconstrained,
879 the object is a record that contains both the template and
880 the object. If there is an initializer, it will have already
881 been converted to the right type, but we need to create the
882 template if there is no initializer. */
883 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
884 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
885 /* Beware that padding might have been introduced
886 via maybe_pad_type above. */
887 || (TYPE_IS_PADDING_P (gnu_type)
888 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
889 == RECORD_TYPE
890 && TYPE_CONTAINS_TEMPLATE_P
891 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
892 && !gnu_expr)
894 tree template_field
895 = TYPE_IS_PADDING_P (gnu_type)
896 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
897 : TYPE_FIELDS (gnu_type);
899 gnu_expr
900 = gnat_build_constructor
901 (gnu_type,
902 tree_cons
903 (template_field,
904 build_template (TREE_TYPE (template_field),
905 TREE_TYPE (TREE_CHAIN (template_field)),
906 NULL_TREE),
907 NULL_TREE));
910 /* If this is a pointer and it does not have an initializing
911 expression, initialize it to NULL, unless the object is
912 imported. */
913 if (definition
914 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
915 && !Is_Imported (gnat_entity) && !gnu_expr)
916 gnu_expr = integer_zero_node;
918 /* If we are defining the object and it has an Address clause we must
919 get the address expression from the saved GCC tree for the
920 object if the object has a Freeze_Node. Otherwise, we elaborate
921 the address expression here since the front-end has guaranteed
922 in that case that the elaboration has no effects. Note that
923 only the latter mechanism is currently in use. */
924 if (definition && Present (Address_Clause (gnat_entity)))
926 tree gnu_address
927 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
928 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
930 save_gnu_tree (gnat_entity, NULL_TREE, false);
932 /* Ignore the size. It's either meaningless or was handled
933 above. */
934 gnu_size = NULL_TREE;
935 /* The address expression contains a conversion from pointer type
936 to the system__address integer type, which means the address
937 of the underlying object escapes. We therefore have no other
938 choice than forcing the type of the object being defined to
939 alias everything in order to make type-based alias analysis
940 aware that it will dereference the escaped address.
941 ??? This uncovers problems in ACATS at -O2 with the volatility
942 of the original type: it may not be correctly propagated, thus
943 causing PRE to enter an infinite loop creating value numbers
944 out of volatile expressions. Disable it for now. */
945 gnu_type
946 = build_reference_type_for_mode (gnu_type, ptr_mode, false);
947 gnu_address = convert (gnu_type, gnu_address);
948 used_by_ref = true;
949 const_flag = !Is_Public (gnat_entity);
951 /* If we don't have an initializing expression for the underlying
952 variable, the initializing expression for the pointer is the
953 specified address. Otherwise, we have to make a COMPOUND_EXPR
954 to assign both the address and the initial value. */
955 if (!gnu_expr)
956 gnu_expr = gnu_address;
957 else
958 gnu_expr
959 = build2 (COMPOUND_EXPR, gnu_type,
960 build_binary_op
961 (MODIFY_EXPR, NULL_TREE,
962 build_unary_op (INDIRECT_REF, NULL_TREE,
963 gnu_address),
964 gnu_expr),
965 gnu_address);
968 /* If it has an address clause and we are not defining it, mark it
969 as an indirect object. Likewise for Stdcall objects that are
970 imported. */
971 if ((!definition && Present (Address_Clause (gnat_entity)))
972 || (Is_Imported (gnat_entity)
973 && Has_Stdcall_Convention (gnat_entity)))
975 /* See the definition case above for the rationale. */
976 gnu_type
977 = build_reference_type_for_mode (gnu_type, ptr_mode, false);
978 gnu_size = NULL_TREE;
980 gnu_expr = NULL_TREE;
981 /* No point in taking the address of an initializing expression
982 that isn't going to be used. */
984 used_by_ref = true;
987 /* If we are at top level and this object is of variable size,
988 make the actual type a hidden pointer to the real type and
989 make the initializer be a memory allocation and initialization.
990 Likewise for objects we aren't defining (presumed to be
991 external references from other packages), but there we do
992 not set up an initialization.
994 If the object's size overflows, make an allocator too, so that
995 Storage_Error gets raised. Note that we will never free
996 such memory, so we presume it never will get allocated. */
998 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
999 global_bindings_p () || !definition
1000 || static_p)
1001 || (gnu_size
1002 && ! allocatable_size_p (gnu_size,
1003 global_bindings_p () || !definition
1004 || static_p)))
1006 gnu_type = build_reference_type (gnu_type);
1007 gnu_size = NULL_TREE;
1008 used_by_ref = true;
1009 const_flag = true;
1011 /* In case this was a aliased object whose nominal subtype is
1012 unconstrained, the pointer above will be a thin pointer and
1013 build_allocator will automatically make the template.
1015 If we have a template initializer only (that we made above),
1016 pretend there is none and rely on what build_allocator creates
1017 again anyway. Otherwise (if we have a full initializer), get
1018 the data part and feed that to build_allocator.
1020 If we are elaborating a mutable object, tell build_allocator to
1021 ignore a possibly simpler size from the initializer, if any, as
1022 we must allocate the maximum possible size in this case. */
1024 if (definition)
1026 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1028 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1029 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1031 gnu_alloc_type
1032 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1034 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1035 && 1 == VEC_length (constructor_elt,
1036 CONSTRUCTOR_ELTS (gnu_expr)))
1037 gnu_expr = 0;
1038 else
1039 gnu_expr
1040 = build_component_ref
1041 (gnu_expr, NULL_TREE,
1042 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1043 false);
1046 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1047 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1048 && !Is_Imported (gnat_entity))
1049 post_error ("Storage_Error will be raised at run-time?",
1050 gnat_entity);
1052 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1053 0, 0, gnat_entity, mutable_p);
1055 else
1057 gnu_expr = NULL_TREE;
1058 const_flag = false;
1062 /* If this object would go into the stack and has an alignment
1063 larger than the default largest alignment, make a variable
1064 to hold the "aligning type" with a modified initial value,
1065 if any, then point to it and make that the value of this
1066 variable, which is now indirect. */
1067 if (!global_bindings_p () && !static_p && definition
1068 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1070 tree gnu_new_type
1071 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1072 TYPE_SIZE_UNIT (gnu_type));
1073 tree gnu_new_var;
1075 gnu_new_var
1076 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1077 NULL_TREE, gnu_new_type, NULL_TREE, false,
1078 false, false, false, NULL, gnat_entity);
1080 if (gnu_expr)
1081 add_stmt_with_node
1082 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1083 build_component_ref
1084 (gnu_new_var, NULL_TREE,
1085 TYPE_FIELDS (gnu_new_type), false),
1086 gnu_expr),
1087 gnat_entity);
1089 gnu_type = build_reference_type (gnu_type);
1090 gnu_expr
1091 = build_unary_op
1092 (ADDR_EXPR, gnu_type,
1093 build_component_ref (gnu_new_var, NULL_TREE,
1094 TYPE_FIELDS (gnu_new_type), false));
1096 gnu_size = NULL_TREE;
1097 used_by_ref = true;
1098 const_flag = true;
1101 if (const_flag)
1102 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1103 | TYPE_QUAL_CONST));
1105 /* Convert the expression to the type of the object except in the
1106 case where the object's type is unconstrained or the object's type
1107 is a padded record whose field is of self-referential size. In
1108 the former case, converting will generate unnecessary evaluations
1109 of the CONSTRUCTOR to compute the size and in the latter case, we
1110 want to only copy the actual data. */
1111 if (gnu_expr
1112 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1113 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1114 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1115 && TYPE_IS_PADDING_P (gnu_type)
1116 && (CONTAINS_PLACEHOLDER_P
1117 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1118 gnu_expr = convert (gnu_type, gnu_expr);
1120 /* If this name is external or there was a name specified, use it,
1121 unless this is a VMS exception object since this would conflict
1122 with the symbol we need to export in addition. Don't use the
1123 Interface_Name if there is an address clause (see CD30005). */
1124 if (!Is_VMS_Exception (gnat_entity)
1125 && ((Present (Interface_Name (gnat_entity))
1126 && No (Address_Clause (gnat_entity)))
1127 || (Is_Public (gnat_entity)
1128 && (!Is_Imported (gnat_entity)
1129 || Is_Exported (gnat_entity)))))
1130 gnu_ext_name = create_concat_name (gnat_entity, 0);
1132 /* If this is constant initialized to a static constant and the
1133 object has an aggregate type, force it to be statically
1134 allocated. */
1135 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1136 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1137 && (AGGREGATE_TYPE_P (gnu_type)
1138 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1139 && TYPE_IS_PADDING_P (gnu_type))))
1140 static_p = true;
1142 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1143 gnu_expr, const_flag,
1144 Is_Public (gnat_entity),
1145 imported_p || !definition,
1146 static_p, attr_list, gnat_entity);
1147 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1148 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1149 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1151 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1152 if (global_bindings_p ())
1154 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1155 record_global_renaming_pointer (gnu_decl);
1159 if (definition && DECL_SIZE (gnu_decl)
1160 && get_block_jmpbuf_decl ()
1161 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1162 || (flag_stack_check && !STACK_CHECK_BUILTIN
1163 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1164 STACK_CHECK_MAX_VAR_SIZE))))
1165 add_stmt_with_node (build_call_1_expr
1166 (update_setjmp_buf_decl,
1167 build_unary_op (ADDR_EXPR, NULL_TREE,
1168 get_block_jmpbuf_decl ())),
1169 gnat_entity);
1171 /* If this is a public constant or we're not optimizing and we're not
1172 making a VAR_DECL for it, make one just for export or debugger
1173 use. Likewise if the address is taken or if the object or type is
1174 aliased. */
1175 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1176 && (Is_Public (gnat_entity)
1177 || optimize == 0
1178 || Address_Taken (gnat_entity)
1179 || Is_Aliased (gnat_entity)
1180 || Is_Aliased (Etype (gnat_entity))))
1182 tree gnu_corr_var
1183 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1184 gnu_expr, true, Is_Public (gnat_entity),
1185 false, static_p, NULL, gnat_entity);
1187 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1190 /* If this is declared in a block that contains a block with an
1191 exception handler, we must force this variable in memory to
1192 suppress an invalid optimization. */
1193 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1194 && Exception_Mechanism != Back_End_Exceptions)
1195 TREE_ADDRESSABLE (gnu_decl) = 1;
1197 /* Back-annotate the Alignment of the object if not already in the
1198 tree. Likewise for Esize if the object is of a constant size.
1199 But if the "object" is actually a pointer to an object, the
1200 alignment and size are the same as the type, so don't back-annotate
1201 the values for the pointer. */
1202 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1203 Set_Alignment (gnat_entity,
1204 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1206 if (!used_by_ref && Unknown_Esize (gnat_entity)
1207 && DECL_SIZE (gnu_decl))
1209 tree gnu_back_size = DECL_SIZE (gnu_decl);
1211 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1212 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1213 gnu_back_size
1214 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1215 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1217 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1220 break;
1222 case E_Void:
1223 /* Return a TYPE_DECL for "void" that we previously made. */
1224 gnu_decl = void_type_decl_node;
1225 break;
1227 case E_Enumeration_Type:
1228 /* A special case, for the types Character and Wide_Character in
1229 Standard, we do not list all the literals. So if the literals
1230 are not specified, make this an unsigned type. */
1231 if (No (First_Literal (gnat_entity)))
1233 gnu_type = make_unsigned_type (esize);
1234 TYPE_NAME (gnu_type) = gnu_entity_id;
1236 /* Set the TYPE_STRING_FLAG for Ada Character and
1237 Wide_Character types. This is needed by the dwarf-2 debug writer to
1238 distinguish between unsigned integer types and character types. */
1239 TYPE_STRING_FLAG (gnu_type) = 1;
1240 break;
1243 /* Normal case of non-character type, or non-Standard character type */
1245 /* Here we have a list of enumeral constants in First_Literal.
1246 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1247 the list to be places into TYPE_FIELDS. Each node in the list
1248 is a TREE_LIST node whose TREE_VALUE is the literal name
1249 and whose TREE_PURPOSE is the value of the literal.
1251 Esize contains the number of bits needed to represent the enumeral
1252 type, Type_Low_Bound also points to the first literal and
1253 Type_High_Bound points to the last literal. */
1255 Entity_Id gnat_literal;
1256 tree gnu_literal_list = NULL_TREE;
1258 if (Is_Unsigned_Type (gnat_entity))
1259 gnu_type = make_unsigned_type (esize);
1260 else
1261 gnu_type = make_signed_type (esize);
1263 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1265 for (gnat_literal = First_Literal (gnat_entity);
1266 Present (gnat_literal);
1267 gnat_literal = Next_Literal (gnat_literal))
1269 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1270 gnu_type);
1271 tree gnu_literal
1272 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1273 gnu_type, gnu_value, true, false, false,
1274 false, NULL, gnat_literal);
1276 save_gnu_tree (gnat_literal, gnu_literal, false);
1277 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1278 gnu_value, gnu_literal_list);
1281 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1283 /* Note that the bounds are updated at the end of this function
1284 because to avoid an infinite recursion when we get the bounds of
1285 this type, since those bounds are objects of this type. */
1287 break;
1289 case E_Signed_Integer_Type:
1290 case E_Ordinary_Fixed_Point_Type:
1291 case E_Decimal_Fixed_Point_Type:
1292 /* For integer types, just make a signed type the appropriate number
1293 of bits. */
1294 gnu_type = make_signed_type (esize);
1295 break;
1297 case E_Modular_Integer_Type:
1298 /* For modular types, make the unsigned type of the proper number of
1299 bits and then set up the modulus, if required. */
1301 enum machine_mode mode;
1302 tree gnu_modulus;
1303 tree gnu_high = 0;
1305 if (Is_Packed_Array_Type (gnat_entity))
1306 esize = UI_To_Int (RM_Size (gnat_entity));
1308 /* Find the smallest mode at least ESIZE bits wide and make a class
1309 using that mode. */
1311 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1312 GET_MODE_BITSIZE (mode) < esize;
1313 mode = GET_MODE_WIDER_MODE (mode))
1316 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1317 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1318 = Is_Packed_Array_Type (gnat_entity);
1320 /* Get the modulus in this type. If it overflows, assume it is because
1321 it is equal to 2**Esize. Note that there is no overflow checking
1322 done on unsigned type, so we detect the overflow by looking for
1323 a modulus of zero, which is otherwise invalid. */
1324 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1326 if (!integer_zerop (gnu_modulus))
1328 TYPE_MODULAR_P (gnu_type) = 1;
1329 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1330 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1331 convert (gnu_type, integer_one_node)));
1334 /* If we have to set TYPE_PRECISION different from its natural value,
1335 make a subtype to do do. Likewise if there is a modulus and
1336 it is not one greater than TYPE_MAX_VALUE. */
1337 if (TYPE_PRECISION (gnu_type) != esize
1338 || (TYPE_MODULAR_P (gnu_type)
1339 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1341 tree gnu_subtype = make_node (INTEGER_TYPE);
1343 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1344 TREE_TYPE (gnu_subtype) = gnu_type;
1345 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1346 TYPE_MAX_VALUE (gnu_subtype)
1347 = TYPE_MODULAR_P (gnu_type)
1348 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1349 TYPE_PRECISION (gnu_subtype) = esize;
1350 TYPE_UNSIGNED (gnu_subtype) = 1;
1351 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1352 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1353 = Is_Packed_Array_Type (gnat_entity);
1354 layout_type (gnu_subtype);
1356 gnu_type = gnu_subtype;
1359 break;
1361 case E_Signed_Integer_Subtype:
1362 case E_Enumeration_Subtype:
1363 case E_Modular_Integer_Subtype:
1364 case E_Ordinary_Fixed_Point_Subtype:
1365 case E_Decimal_Fixed_Point_Subtype:
1367 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1368 that we do not want to call build_range_type since we would
1369 like each subtype node to be distinct. This will be important
1370 when memory aliasing is implemented.
1372 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1373 parent type; this fact is used by the arithmetic conversion
1374 functions.
1376 We elaborate the Ancestor_Subtype if it is not in the current
1377 unit and one of our bounds is non-static. We do this to ensure
1378 consistent naming in the case where several subtypes share the same
1379 bounds by always elaborating the first such subtype first, thus
1380 using its name. */
1382 if (definition == 0
1383 && Present (Ancestor_Subtype (gnat_entity))
1384 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1385 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1386 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1387 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1388 gnu_expr, definition);
1390 gnu_type = make_node (INTEGER_TYPE);
1391 if (Is_Packed_Array_Type (gnat_entity))
1393 esize = UI_To_Int (RM_Size (gnat_entity));
1394 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1397 TYPE_PRECISION (gnu_type) = esize;
1398 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1400 TYPE_MIN_VALUE (gnu_type)
1401 = convert (TREE_TYPE (gnu_type),
1402 elaborate_expression (Type_Low_Bound (gnat_entity),
1403 gnat_entity,
1404 get_identifier ("L"), definition, 1,
1405 Needs_Debug_Info (gnat_entity)));
1407 TYPE_MAX_VALUE (gnu_type)
1408 = convert (TREE_TYPE (gnu_type),
1409 elaborate_expression (Type_High_Bound (gnat_entity),
1410 gnat_entity,
1411 get_identifier ("U"), definition, 1,
1412 Needs_Debug_Info (gnat_entity)));
1414 /* One of the above calls might have caused us to be elaborated,
1415 so don't blow up if so. */
1416 if (present_gnu_tree (gnat_entity))
1418 maybe_present = true;
1419 break;
1422 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1423 = Has_Biased_Representation (gnat_entity);
1425 /* This should be an unsigned type if the lower bound is constant
1426 and non-negative or if the base type is unsigned; a signed type
1427 otherwise. */
1428 TYPE_UNSIGNED (gnu_type)
1429 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1430 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1431 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1432 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1433 || Is_Unsigned_Type (gnat_entity));
1435 layout_type (gnu_type);
1437 /* Inherit our alias set from what we're a subtype of. Subtypes
1438 are not different types and a pointer can designate any instance
1439 within a subtype hierarchy. */
1440 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1442 /* If the type we are dealing with is to represent a packed array,
1443 we need to have the bits left justified on big-endian targets
1444 and right justified on little-endian targets. We also need to
1445 ensure that when the value is read (e.g. for comparison of two
1446 such values), we only get the good bits, since the unused bits
1447 are uninitialized. Both goals are accomplished by wrapping the
1448 modular value in an enclosing struct. */
1449 if (Is_Packed_Array_Type (gnat_entity))
1451 tree gnu_field_type = gnu_type;
1452 tree gnu_field;
1454 TYPE_RM_SIZE_NUM (gnu_field_type)
1455 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1456 gnu_type = make_node (RECORD_TYPE);
1457 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1458 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1459 TYPE_PACKED (gnu_type) = 1;
1461 /* Create a stripped-down declaration of the original type, mainly
1462 for debugging. */
1463 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1464 NULL, true, debug_info_p, gnat_entity);
1466 /* Don't notify the field as "addressable", since we won't be taking
1467 it's address and it would prevent create_field_decl from making a
1468 bitfield. */
1469 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1470 gnu_field_type, gnu_type, 1, 0, 0, 0);
1472 finish_record_type (gnu_type, gnu_field, false, false);
1473 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1474 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1476 copy_alias_set (gnu_type, gnu_field_type);
1479 break;
1481 case E_Floating_Point_Type:
1482 /* If this is a VAX floating-point type, use an integer of the proper
1483 size. All the operations will be handled with ASM statements. */
1484 if (Vax_Float (gnat_entity))
1486 gnu_type = make_signed_type (esize);
1487 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1488 SET_TYPE_DIGITS_VALUE (gnu_type,
1489 UI_To_gnu (Digits_Value (gnat_entity),
1490 sizetype));
1491 break;
1494 /* The type of the Low and High bounds can be our type if this is
1495 a type from Standard, so set them at the end of the function. */
1496 gnu_type = make_node (REAL_TYPE);
1497 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1498 layout_type (gnu_type);
1499 break;
1501 case E_Floating_Point_Subtype:
1502 if (Vax_Float (gnat_entity))
1504 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1505 break;
1509 if (definition == 0
1510 && Present (Ancestor_Subtype (gnat_entity))
1511 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1512 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1513 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1514 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1515 gnu_expr, definition);
1517 gnu_type = make_node (REAL_TYPE);
1518 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1519 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1521 TYPE_MIN_VALUE (gnu_type)
1522 = convert (TREE_TYPE (gnu_type),
1523 elaborate_expression (Type_Low_Bound (gnat_entity),
1524 gnat_entity, get_identifier ("L"),
1525 definition, 1,
1526 Needs_Debug_Info (gnat_entity)));
1528 TYPE_MAX_VALUE (gnu_type)
1529 = convert (TREE_TYPE (gnu_type),
1530 elaborate_expression (Type_High_Bound (gnat_entity),
1531 gnat_entity, get_identifier ("U"),
1532 definition, 1,
1533 Needs_Debug_Info (gnat_entity)));
1535 /* One of the above calls might have caused us to be elaborated,
1536 so don't blow up if so. */
1537 if (present_gnu_tree (gnat_entity))
1539 maybe_present = true;
1540 break;
1543 layout_type (gnu_type);
1545 /* Inherit our alias set from what we're a subtype of, as for
1546 integer subtypes. */
1547 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1549 break;
1551 /* Array and String Types and Subtypes
1553 Unconstrained array types are represented by E_Array_Type and
1554 constrained array types are represented by E_Array_Subtype. There
1555 are no actual objects of an unconstrained array type; all we have
1556 are pointers to that type.
1558 The following fields are defined on array types and subtypes:
1560 Component_Type Component type of the array.
1561 Number_Dimensions Number of dimensions (an int).
1562 First_Index Type of first index. */
1564 case E_String_Type:
1565 case E_Array_Type:
1567 tree gnu_template_fields = NULL_TREE;
1568 tree gnu_template_type = make_node (RECORD_TYPE);
1569 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1570 tree gnu_fat_type = make_node (RECORD_TYPE);
1571 int ndim = Number_Dimensions (gnat_entity);
1572 int firstdim
1573 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1574 int nextdim
1575 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1576 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1577 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1578 tree gnu_comp_size = 0;
1579 tree gnu_max_size = size_one_node;
1580 tree gnu_max_size_unit;
1581 int index;
1582 Entity_Id gnat_ind_subtype;
1583 Entity_Id gnat_ind_base_subtype;
1584 tree gnu_template_reference;
1585 tree tem;
1587 TYPE_NAME (gnu_template_type)
1588 = create_concat_name (gnat_entity, "XUB");
1589 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1590 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1591 TYPE_READONLY (gnu_template_type) = 1;
1593 /* Make a node for the array. If we are not defining the array
1594 suppress expanding incomplete types. */
1595 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1597 if (!definition)
1598 defer_incomplete_level++, this_deferred = true;
1600 /* Build the fat pointer type. Use a "void *" object instead of
1601 a pointer to the array type since we don't have the array type
1602 yet (it will reference the fat pointer via the bounds). */
1603 tem = chainon (chainon (NULL_TREE,
1604 create_field_decl (get_identifier ("P_ARRAY"),
1605 ptr_void_type_node,
1606 gnu_fat_type, 0, 0, 0, 0)),
1607 create_field_decl (get_identifier ("P_BOUNDS"),
1608 gnu_ptr_template,
1609 gnu_fat_type, 0, 0, 0, 0));
1611 /* Make sure we can put this into a register. */
1612 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1613 finish_record_type (gnu_fat_type, tem, false, true);
1615 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1616 is the fat pointer. This will be used to access the individual
1617 fields once we build them. */
1618 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1619 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1620 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1621 gnu_template_reference
1622 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1623 TREE_READONLY (gnu_template_reference) = 1;
1625 /* Now create the GCC type for each index and add the fields for
1626 that index to the template. */
1627 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1628 gnat_ind_base_subtype
1629 = First_Index (Implementation_Base_Type (gnat_entity));
1630 index < ndim && index >= 0;
1631 index += nextdim,
1632 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1633 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1635 char field_name[10];
1636 tree gnu_ind_subtype
1637 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1638 tree gnu_base_subtype
1639 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1640 tree gnu_base_min
1641 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1642 tree gnu_base_max
1643 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1644 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1646 /* Make the FIELD_DECLs for the minimum and maximum of this
1647 type and then make extractions of that field from the
1648 template. */
1649 sprintf (field_name, "LB%d", index);
1650 gnu_min_field = create_field_decl (get_identifier (field_name),
1651 gnu_ind_subtype,
1652 gnu_template_type, 0, 0, 0, 0);
1653 field_name[0] = 'U';
1654 gnu_max_field = create_field_decl (get_identifier (field_name),
1655 gnu_ind_subtype,
1656 gnu_template_type, 0, 0, 0, 0);
1658 Sloc_to_locus (Sloc (gnat_entity),
1659 &DECL_SOURCE_LOCATION (gnu_min_field));
1660 Sloc_to_locus (Sloc (gnat_entity),
1661 &DECL_SOURCE_LOCATION (gnu_max_field));
1662 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1664 /* We can't use build_component_ref here since the template
1665 type isn't complete yet. */
1666 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1667 gnu_template_reference, gnu_min_field,
1668 NULL_TREE);
1669 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1670 gnu_template_reference, gnu_max_field,
1671 NULL_TREE);
1672 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1674 /* Make a range type with the new ranges, but using
1675 the Ada subtype. Then we convert to sizetype. */
1676 gnu_index_types[index]
1677 = create_index_type (convert (sizetype, gnu_min),
1678 convert (sizetype, gnu_max),
1679 build_range_type (gnu_ind_subtype,
1680 gnu_min, gnu_max));
1681 /* Update the maximum size of the array, in elements. */
1682 gnu_max_size
1683 = size_binop (MULT_EXPR, gnu_max_size,
1684 size_binop (PLUS_EXPR, size_one_node,
1685 size_binop (MINUS_EXPR, gnu_base_max,
1686 gnu_base_min)));
1688 TYPE_NAME (gnu_index_types[index])
1689 = create_concat_name (gnat_entity, field_name);
1692 for (index = 0; index < ndim; index++)
1693 gnu_template_fields
1694 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1696 /* Install all the fields into the template. */
1697 finish_record_type (gnu_template_type, gnu_template_fields,
1698 false, false);
1699 TYPE_READONLY (gnu_template_type) = 1;
1701 /* Now make the array of arrays and update the pointer to the array
1702 in the fat pointer. Note that it is the first field. */
1704 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1706 /* Get and validate any specified Component_Size, but if Packed,
1707 ignore it since the front end will have taken care of it. */
1708 gnu_comp_size
1709 = validate_size (Component_Size (gnat_entity), tem,
1710 gnat_entity,
1711 (Is_Bit_Packed_Array (gnat_entity)
1712 ? TYPE_DECL : VAR_DECL),
1713 true, Has_Component_Size_Clause (gnat_entity));
1715 if (Has_Atomic_Components (gnat_entity))
1716 check_ok_for_atomic (tem, gnat_entity, true);
1718 /* If the component type is a RECORD_TYPE that has a self-referential
1719 size, use the maxium size. */
1720 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1721 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1722 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1724 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1726 tem = make_type_from_size (tem, gnu_comp_size, false);
1727 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1728 "C_PAD", false, definition, true);
1731 if (Has_Volatile_Components (gnat_entity))
1732 tem = build_qualified_type (tem,
1733 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1735 /* If Component_Size is not already specified, annotate it with the
1736 size of the component. */
1737 if (Unknown_Component_Size (gnat_entity))
1738 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1740 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1741 size_binop (MULT_EXPR, gnu_max_size,
1742 TYPE_SIZE_UNIT (tem)));
1743 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1744 size_binop (MULT_EXPR,
1745 convert (bitsizetype,
1746 gnu_max_size),
1747 TYPE_SIZE (tem)));
1749 for (index = ndim - 1; index >= 0; index--)
1751 tem = build_array_type (tem, gnu_index_types[index]);
1752 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1754 /* If the type below this is a multi-array type, then this
1755 does not have aliased components. But we have to make
1756 them addressable if it must be passed by reference or
1757 if that is the default. */
1758 if ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1759 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem)))
1760 || (!Has_Aliased_Components (gnat_entity)
1761 && !must_pass_by_ref (TREE_TYPE (tem))
1762 && !default_pass_by_ref (TREE_TYPE (tem))))
1763 TYPE_NONALIASED_COMPONENT (tem) = 1;
1766 /* If an alignment is specified, use it if valid. But ignore it for
1767 types that represent the unpacked base type for packed arrays. */
1768 if (No (Packed_Array_Type (gnat_entity))
1769 && Known_Alignment (gnat_entity))
1771 gcc_assert (Present (Alignment (gnat_entity)));
1772 TYPE_ALIGN (tem)
1773 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1774 TYPE_ALIGN (tem));
1777 TYPE_CONVENTION_FORTRAN_P (tem)
1778 = (Convention (gnat_entity) == Convention_Fortran);
1779 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1781 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1782 corresponding fat pointer. */
1783 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1784 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1785 TYPE_MODE (gnu_type) = BLKmode;
1786 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1787 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1789 /* If the maximum size doesn't overflow, use it. */
1790 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1791 && !TREE_OVERFLOW (gnu_max_size))
1792 TYPE_SIZE (tem)
1793 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1794 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1795 && !TREE_OVERFLOW (gnu_max_size_unit))
1796 TYPE_SIZE_UNIT (tem)
1797 = size_binop (MIN_EXPR, gnu_max_size_unit,
1798 TYPE_SIZE_UNIT (tem));
1800 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1801 tem, NULL, !Comes_From_Source (gnat_entity),
1802 debug_info_p, gnat_entity);
1804 /* Create a record type for the object and its template and
1805 set the template at a negative offset. */
1806 tem = build_unc_object_type (gnu_template_type, tem,
1807 create_concat_name (gnat_entity, "XUT"));
1808 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1809 = size_binop (MINUS_EXPR, size_zero_node,
1810 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1811 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1812 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1813 = bitsize_zero_node;
1814 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1815 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1817 /* Give the thin pointer type a name. */
1818 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1819 build_pointer_type (tem), NULL,
1820 !Comes_From_Source (gnat_entity), debug_info_p,
1821 gnat_entity);
1823 break;
1825 case E_String_Subtype:
1826 case E_Array_Subtype:
1828 /* This is the actual data type for array variables. Multidimensional
1829 arrays are implemented in the gnu tree as arrays of arrays. Note
1830 that for the moment arrays which have sparse enumeration subtypes as
1831 index components create sparse arrays, which is obviously space
1832 inefficient but so much easier to code for now.
1834 Also note that the subtype never refers to the unconstrained
1835 array type, which is somewhat at variance with Ada semantics.
1837 First check to see if this is simply a renaming of the array
1838 type. If so, the result is the array type. */
1840 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1841 if (!Is_Constrained (gnat_entity))
1842 break;
1843 else
1845 int index;
1846 int array_dim = Number_Dimensions (gnat_entity);
1847 int first_dim
1848 = ((Convention (gnat_entity) == Convention_Fortran)
1849 ? array_dim - 1 : 0);
1850 int next_dim
1851 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1852 Entity_Id gnat_ind_subtype;
1853 Entity_Id gnat_ind_base_subtype;
1854 tree gnu_base_type = gnu_type;
1855 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1856 tree gnu_comp_size = NULL_TREE;
1857 tree gnu_max_size = size_one_node;
1858 tree gnu_max_size_unit;
1859 bool need_index_type_struct = false;
1860 bool max_overflow = false;
1862 /* First create the gnu types for each index. Create types for
1863 debugging information to point to the index types if the
1864 are not integer types, have variable bounds, or are
1865 wider than sizetype. */
1867 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1868 gnat_ind_base_subtype
1869 = First_Index (Implementation_Base_Type (gnat_entity));
1870 index < array_dim && index >= 0;
1871 index += next_dim,
1872 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1873 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1875 tree gnu_index_subtype
1876 = get_unpadded_type (Etype (gnat_ind_subtype));
1877 tree gnu_min
1878 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1879 tree gnu_max
1880 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1881 tree gnu_base_subtype
1882 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1883 tree gnu_base_min
1884 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1885 tree gnu_base_max
1886 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1887 tree gnu_base_type = get_base_type (gnu_base_subtype);
1888 tree gnu_base_base_min
1889 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1890 tree gnu_base_base_max
1891 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1892 tree gnu_high;
1893 tree gnu_this_max;
1895 /* If the minimum and maximum values both overflow in
1896 SIZETYPE, but the difference in the original type
1897 does not overflow in SIZETYPE, ignore the overflow
1898 indications. */
1899 if ((TYPE_PRECISION (gnu_index_subtype)
1900 > TYPE_PRECISION (sizetype)
1901 || TYPE_UNSIGNED (gnu_index_subtype)
1902 != TYPE_UNSIGNED (sizetype))
1903 && TREE_CODE (gnu_min) == INTEGER_CST
1904 && TREE_CODE (gnu_max) == INTEGER_CST
1905 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1906 && (!TREE_OVERFLOW
1907 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1908 TYPE_MAX_VALUE (gnu_index_subtype),
1909 TYPE_MIN_VALUE (gnu_index_subtype))))))
1910 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max) = 0;
1912 /* Similarly, if the range is null, use bounds of 1..0 for
1913 the sizetype bounds. */
1914 else if ((TYPE_PRECISION (gnu_index_subtype)
1915 > TYPE_PRECISION (sizetype)
1916 || TYPE_UNSIGNED (gnu_index_subtype)
1917 != TYPE_UNSIGNED (sizetype))
1918 && TREE_CODE (gnu_min) == INTEGER_CST
1919 && TREE_CODE (gnu_max) == INTEGER_CST
1920 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1921 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1922 TYPE_MIN_VALUE (gnu_index_subtype)))
1923 gnu_min = size_one_node, gnu_max = size_zero_node;
1925 /* Now compute the size of this bound. We need to provide
1926 GCC with an upper bound to use but have to deal with the
1927 "superflat" case. There are three ways to do this. If we
1928 can prove that the array can never be superflat, we can
1929 just use the high bound of the index subtype. If we can
1930 prove that the low bound minus one can't overflow, we
1931 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1932 the expression hb >= lb ? hb : lb - 1. */
1933 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1935 /* See if the base array type is already flat. If it is, we
1936 are probably compiling an ACVC test, but it will cause the
1937 code below to malfunction if we don't handle it specially. */
1938 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1939 && TREE_CODE (gnu_base_max) == INTEGER_CST
1940 && !TREE_OVERFLOW (gnu_base_min)
1941 && !TREE_OVERFLOW (gnu_base_max)
1942 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1943 gnu_high = size_zero_node, gnu_min = size_one_node;
1945 /* If gnu_high is now an integer which overflowed, the array
1946 cannot be superflat. */
1947 else if (TREE_CODE (gnu_high) == INTEGER_CST
1948 && TREE_OVERFLOW (gnu_high))
1949 gnu_high = gnu_max;
1950 else if (TYPE_UNSIGNED (gnu_base_subtype)
1951 || TREE_CODE (gnu_high) == INTEGER_CST)
1952 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1953 else
1954 gnu_high
1955 = build_cond_expr
1956 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1957 gnu_max, gnu_min),
1958 gnu_max, gnu_high);
1960 gnu_index_type[index]
1961 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1963 /* Also compute the maximum size of the array. Here we
1964 see if any constraint on the index type of the base type
1965 can be used in the case of self-referential bound on
1966 the index type of the subtype. We look for a non-"infinite"
1967 and non-self-referential bound from any type involved and
1968 handle each bound separately. */
1970 if ((TREE_CODE (gnu_min) == INTEGER_CST
1971 && !TREE_OVERFLOW (gnu_min)
1972 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1973 || !CONTAINS_PLACEHOLDER_P (gnu_min)
1974 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
1975 && !TREE_OVERFLOW (gnu_base_min)))
1976 gnu_base_min = gnu_min;
1978 if ((TREE_CODE (gnu_max) == INTEGER_CST
1979 && !TREE_OVERFLOW (gnu_max)
1980 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1981 || !CONTAINS_PLACEHOLDER_P (gnu_max)
1982 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
1983 && !TREE_OVERFLOW (gnu_base_max)))
1984 gnu_base_max = gnu_max;
1986 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1987 && TREE_OVERFLOW (gnu_base_min))
1988 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1989 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1990 && TREE_OVERFLOW (gnu_base_max))
1991 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1992 max_overflow = true;
1994 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1995 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1997 gnu_this_max
1998 = size_binop (MAX_EXPR,
1999 size_binop (PLUS_EXPR, size_one_node,
2000 size_binop (MINUS_EXPR, gnu_base_max,
2001 gnu_base_min)),
2002 size_zero_node);
2004 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2005 && TREE_OVERFLOW (gnu_this_max))
2006 max_overflow = true;
2008 gnu_max_size
2009 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2011 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2012 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2013 != INTEGER_CST)
2014 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2015 || (TREE_TYPE (gnu_index_subtype)
2016 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2017 != INTEGER_TYPE))
2018 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2019 || (TYPE_PRECISION (gnu_index_subtype)
2020 > TYPE_PRECISION (sizetype)))
2021 need_index_type_struct = true;
2024 /* Then flatten: create the array of arrays. */
2026 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2028 /* One of the above calls might have caused us to be elaborated,
2029 so don't blow up if so. */
2030 if (present_gnu_tree (gnat_entity))
2032 maybe_present = true;
2033 break;
2036 /* Get and validate any specified Component_Size, but if Packed,
2037 ignore it since the front end will have taken care of it. */
2038 gnu_comp_size
2039 = validate_size (Component_Size (gnat_entity), gnu_type,
2040 gnat_entity,
2041 (Is_Bit_Packed_Array (gnat_entity)
2042 ? TYPE_DECL : VAR_DECL),
2043 true, Has_Component_Size_Clause (gnat_entity));
2045 /* If the component type is a RECORD_TYPE that has a self-referential
2046 size, use the maxium size. */
2047 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
2048 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2049 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2051 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
2053 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
2054 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2055 gnat_entity, "C_PAD", false,
2056 definition, true);
2059 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2060 gnu_type = build_qualified_type (gnu_type,
2061 (TYPE_QUALS (gnu_type)
2062 | TYPE_QUAL_VOLATILE));
2064 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2065 TYPE_SIZE_UNIT (gnu_type));
2066 gnu_max_size = size_binop (MULT_EXPR,
2067 convert (bitsizetype, gnu_max_size),
2068 TYPE_SIZE (gnu_type));
2070 for (index = array_dim - 1; index >= 0; index --)
2072 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2073 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2075 /* If the type below this is a multi-array type, then this
2076 does not have aliased components. But we have to make
2077 them addressable if it must be passed by reference or
2078 if that is the default. */
2079 if ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2080 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2081 || (!Has_Aliased_Components (gnat_entity)
2082 && !must_pass_by_ref (TREE_TYPE (gnu_type))
2083 && !default_pass_by_ref (TREE_TYPE (gnu_type))))
2084 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2087 /* If we are at file level and this is a multi-dimensional array, we
2088 need to make a variable corresponding to the stride of the
2089 inner dimensions. */
2090 if (global_bindings_p () && array_dim > 1)
2092 tree gnu_str_name = get_identifier ("ST");
2093 tree gnu_arr_type;
2095 for (gnu_arr_type = TREE_TYPE (gnu_type);
2096 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2097 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2098 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2100 tree eltype = TREE_TYPE (gnu_arr_type);
2102 TYPE_SIZE (gnu_arr_type)
2103 = elaborate_expression_1 (gnat_entity, gnat_entity,
2104 TYPE_SIZE (gnu_arr_type),
2105 gnu_str_name, definition, 0);
2107 /* ??? For now, store the size as a multiple of the
2108 alignment of the element type in bytes so that we
2109 can see the alignment from the tree. */
2110 TYPE_SIZE_UNIT (gnu_arr_type)
2111 = build_binary_op
2112 (MULT_EXPR, sizetype,
2113 elaborate_expression_1
2114 (gnat_entity, gnat_entity,
2115 build_binary_op (EXACT_DIV_EXPR, sizetype,
2116 TYPE_SIZE_UNIT (gnu_arr_type),
2117 size_int (TYPE_ALIGN (eltype)
2118 / BITS_PER_UNIT)),
2119 concat_id_with_name (gnu_str_name, "A_U"),
2120 definition, 0),
2121 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2125 /* If we need to write out a record type giving the names of
2126 the bounds, do it now. */
2127 if (need_index_type_struct && debug_info_p)
2129 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2130 tree gnu_field_list = NULL_TREE;
2131 tree gnu_field;
2133 TYPE_NAME (gnu_bound_rec_type)
2134 = create_concat_name (gnat_entity, "XA");
2136 for (index = array_dim - 1; index >= 0; index--)
2138 tree gnu_type_name
2139 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2141 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2142 gnu_type_name = DECL_NAME (gnu_type_name);
2144 gnu_field = create_field_decl (gnu_type_name,
2145 integer_type_node,
2146 gnu_bound_rec_type,
2147 0, NULL_TREE, NULL_TREE, 0);
2148 TREE_CHAIN (gnu_field) = gnu_field_list;
2149 gnu_field_list = gnu_field;
2152 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2153 false, false);
2156 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2157 = (Convention (gnat_entity) == Convention_Fortran);
2158 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2159 = Is_Packed_Array_Type (gnat_entity);
2161 /* If our size depends on a placeholder and the maximum size doesn't
2162 overflow, use it. */
2163 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2164 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2165 && TREE_OVERFLOW (gnu_max_size))
2166 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2167 && TREE_OVERFLOW (gnu_max_size_unit))
2168 && !max_overflow)
2170 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2171 TYPE_SIZE (gnu_type));
2172 TYPE_SIZE_UNIT (gnu_type)
2173 = size_binop (MIN_EXPR, gnu_max_size_unit,
2174 TYPE_SIZE_UNIT (gnu_type));
2177 /* Set our alias set to that of our base type. This gives all
2178 array subtypes the same alias set. */
2179 copy_alias_set (gnu_type, gnu_base_type);
2182 /* If this is a packed type, make this type the same as the packed
2183 array type, but do some adjusting in the type first. */
2185 if (Present (Packed_Array_Type (gnat_entity)))
2187 Entity_Id gnat_index;
2188 tree gnu_inner_type;
2190 /* First finish the type we had been making so that we output
2191 debugging information for it */
2192 gnu_type
2193 = build_qualified_type (gnu_type,
2194 (TYPE_QUALS (gnu_type)
2195 | (TYPE_QUAL_VOLATILE
2196 * Treat_As_Volatile (gnat_entity))));
2197 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2198 !Comes_From_Source (gnat_entity),
2199 debug_info_p, gnat_entity);
2200 if (!Comes_From_Source (gnat_entity))
2201 DECL_ARTIFICIAL (gnu_decl) = 1;
2203 /* Save it as our equivalent in case the call below elaborates
2204 this type again. */
2205 save_gnu_tree (gnat_entity, gnu_decl, false);
2207 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2208 NULL_TREE, 0);
2209 this_made_decl = true;
2210 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2211 save_gnu_tree (gnat_entity, NULL_TREE, false);
2213 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2214 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2215 || TYPE_IS_PADDING_P (gnu_inner_type)))
2216 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2218 /* We need to point the type we just made to our index type so
2219 the actual bounds can be put into a template. */
2221 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2222 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2223 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2224 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2226 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2228 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2229 If it is, we need to make another type. */
2230 if (TYPE_MODULAR_P (gnu_inner_type))
2232 tree gnu_subtype;
2234 gnu_subtype = make_node (INTEGER_TYPE);
2236 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2237 TYPE_MIN_VALUE (gnu_subtype)
2238 = TYPE_MIN_VALUE (gnu_inner_type);
2239 TYPE_MAX_VALUE (gnu_subtype)
2240 = TYPE_MAX_VALUE (gnu_inner_type);
2241 TYPE_PRECISION (gnu_subtype)
2242 = TYPE_PRECISION (gnu_inner_type);
2243 TYPE_UNSIGNED (gnu_subtype)
2244 = TYPE_UNSIGNED (gnu_inner_type);
2245 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2246 layout_type (gnu_subtype);
2248 gnu_inner_type = gnu_subtype;
2251 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2254 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2256 for (gnat_index = First_Index (gnat_entity);
2257 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2258 SET_TYPE_ACTUAL_BOUNDS
2259 (gnu_inner_type,
2260 tree_cons (NULL_TREE,
2261 get_unpadded_type (Etype (gnat_index)),
2262 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2264 if (Convention (gnat_entity) != Convention_Fortran)
2265 SET_TYPE_ACTUAL_BOUNDS
2266 (gnu_inner_type,
2267 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2269 if (TREE_CODE (gnu_type) == RECORD_TYPE
2270 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2271 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2275 /* Abort if packed array with no packed array type field set. */
2276 else
2277 gcc_assert (!Is_Packed (gnat_entity));
2279 break;
2281 case E_String_Literal_Subtype:
2282 /* Create the type for a string literal. */
2284 Entity_Id gnat_full_type
2285 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2286 && Present (Full_View (Etype (gnat_entity)))
2287 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2288 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2289 tree gnu_string_array_type
2290 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2291 tree gnu_string_index_type
2292 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2293 (TYPE_DOMAIN (gnu_string_array_type))));
2294 tree gnu_lower_bound
2295 = convert (gnu_string_index_type,
2296 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2297 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2298 tree gnu_length = ssize_int (length - 1);
2299 tree gnu_upper_bound
2300 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2301 gnu_lower_bound,
2302 convert (gnu_string_index_type, gnu_length));
2303 tree gnu_range_type
2304 = build_range_type (gnu_string_index_type,
2305 gnu_lower_bound, gnu_upper_bound);
2306 tree gnu_index_type
2307 = create_index_type (convert (sizetype,
2308 TYPE_MIN_VALUE (gnu_range_type)),
2309 convert (sizetype,
2310 TYPE_MAX_VALUE (gnu_range_type)),
2311 gnu_range_type);
2313 gnu_type
2314 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2315 gnu_index_type);
2316 copy_alias_set (gnu_type, gnu_string_type);
2318 break;
2320 /* Record Types and Subtypes
2322 The following fields are defined on record types:
2324 Has_Discriminants True if the record has discriminants
2325 First_Discriminant Points to head of list of discriminants
2326 First_Entity Points to head of list of fields
2327 Is_Tagged_Type True if the record is tagged
2329 Implementation of Ada records and discriminated records:
2331 A record type definition is transformed into the equivalent of a C
2332 struct definition. The fields that are the discriminants which are
2333 found in the Full_Type_Declaration node and the elements of the
2334 Component_List found in the Record_Type_Definition node. The
2335 Component_List can be a recursive structure since each Variant of
2336 the Variant_Part of the Component_List has a Component_List.
2338 Processing of a record type definition comprises starting the list of
2339 field declarations here from the discriminants and the calling the
2340 function components_to_record to add the rest of the fields from the
2341 component list and return the gnu type node. The function
2342 components_to_record will call itself recursively as it traverses
2343 the tree. */
2345 case E_Record_Type:
2346 if (Has_Complex_Representation (gnat_entity))
2348 gnu_type
2349 = build_complex_type
2350 (get_unpadded_type
2351 (Etype (Defining_Entity
2352 (First (Component_Items
2353 (Component_List
2354 (Type_Definition
2355 (Declaration_Node (gnat_entity)))))))));
2357 break;
2361 Node_Id full_definition = Declaration_Node (gnat_entity);
2362 Node_Id record_definition = Type_Definition (full_definition);
2363 Entity_Id gnat_field;
2364 tree gnu_field;
2365 tree gnu_field_list = NULL_TREE;
2366 tree gnu_get_parent;
2367 int packed = (Is_Packed (gnat_entity) ? 1
2368 : (Component_Alignment (gnat_entity)
2369 == Calign_Storage_Unit) ? -1
2370 : 0);
2371 bool has_rep = Has_Specified_Layout (gnat_entity);
2372 bool all_rep = has_rep;
2373 bool is_extension
2374 = (Is_Tagged_Type (gnat_entity)
2375 && Nkind (record_definition) == N_Derived_Type_Definition);
2377 /* See if all fields have a rep clause. Stop when we find one
2378 that doesn't. */
2379 for (gnat_field = First_Entity (gnat_entity);
2380 Present (gnat_field) && all_rep;
2381 gnat_field = Next_Entity (gnat_field))
2382 if ((Ekind (gnat_field) == E_Component
2383 || Ekind (gnat_field) == E_Discriminant)
2384 && No (Component_Clause (gnat_field)))
2385 all_rep = false;
2387 /* If this is a record extension, go a level further to find the
2388 record definition. Also, verify we have a Parent_Subtype. */
2389 if (is_extension)
2391 if (!type_annotate_only
2392 || Present (Record_Extension_Part (record_definition)))
2393 record_definition = Record_Extension_Part (record_definition);
2395 gcc_assert (type_annotate_only
2396 || Present (Parent_Subtype (gnat_entity)));
2399 /* Make a node for the record. If we are not defining the record,
2400 suppress expanding incomplete types. */
2401 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2402 TYPE_NAME (gnu_type) = gnu_entity_id;
2403 /* ??? We should have create_type_decl like in the E_Record_Subtype
2404 case below. Unfortunately this would cause GNU_TYPE to be marked
2405 as visited, thus precluding the subtrees of the type that will be
2406 built below from being marked as visited when the real TYPE_DECL
2407 is eventually created. A solution could be to devise a special
2408 version of the function under the name create_type_stub_decl. */
2409 TYPE_STUB_DECL (gnu_type)
2410 = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
2411 TYPE_ALIGN (gnu_type) = 0;
2412 TYPE_PACKED (gnu_type) = packed || has_rep;
2414 if (!definition)
2415 defer_incomplete_level++, this_deferred = true;
2417 /* If both a size and rep clause was specified, put the size in
2418 the record type now so that it can get the proper mode. */
2419 if (has_rep && Known_Esize (gnat_entity))
2420 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2422 /* Always set the alignment here so that it can be used to
2423 set the mode, if it is making the alignment stricter. If
2424 it is invalid, it will be checked again below. If this is to
2425 be Atomic, choose a default alignment of a word unless we know
2426 the size and it's smaller. */
2427 if (Known_Alignment (gnat_entity))
2428 TYPE_ALIGN (gnu_type)
2429 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2430 else if (Is_Atomic (gnat_entity))
2431 TYPE_ALIGN (gnu_type)
2432 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2433 : 1 << (floor_log2 (esize - 1) + 1));
2435 /* If we have a Parent_Subtype, make a field for the parent. If
2436 this record has rep clauses, force the position to zero. */
2437 if (Present (Parent_Subtype (gnat_entity)))
2439 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2440 tree gnu_parent;
2442 /* A major complexity here is that the parent subtype will
2443 reference our discriminants in its Discriminant_Constraint
2444 list. But those must reference the parent component of this
2445 record which is of the parent subtype we have not built yet!
2446 To break the circle we first build a dummy COMPONENT_REF which
2447 represents the "get to the parent" operation and initialize
2448 each of those discriminants to a COMPONENT_REF of the above
2449 dummy parent referencing the corresponding discriminant of the
2450 base type of the parent subtype. */
2451 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2452 build0 (PLACEHOLDER_EXPR, gnu_type),
2453 build_decl (FIELD_DECL, NULL_TREE,
2454 NULL_TREE),
2455 NULL_TREE);
2457 if (Has_Discriminants (gnat_entity))
2458 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2459 Present (gnat_field);
2460 gnat_field = Next_Stored_Discriminant (gnat_field))
2461 if (Present (Corresponding_Discriminant (gnat_field)))
2462 save_gnu_tree
2463 (gnat_field,
2464 build3 (COMPONENT_REF,
2465 get_unpadded_type (Etype (gnat_field)),
2466 gnu_get_parent,
2467 gnat_to_gnu_field_decl (Corresponding_Discriminant
2468 (gnat_field)),
2469 NULL_TREE),
2470 true);
2472 /* Then we build the parent subtype. */
2473 gnu_parent = gnat_to_gnu_type (gnat_parent);
2475 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2476 initially built. The discriminants must reference the fields
2477 of the parent subtype and not those of its base type for the
2478 placeholder machinery to properly work. */
2479 if (Has_Discriminants (gnat_entity))
2480 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2481 Present (gnat_field);
2482 gnat_field = Next_Stored_Discriminant (gnat_field))
2483 if (Present (Corresponding_Discriminant (gnat_field)))
2485 Entity_Id field = Empty;
2486 for (field = First_Stored_Discriminant (gnat_parent);
2487 Present (field);
2488 field = Next_Stored_Discriminant (field))
2489 if (same_discriminant_p (gnat_field, field))
2490 break;
2491 gcc_assert (Present (field));
2492 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2493 = gnat_to_gnu_field_decl (field);
2496 /* The "get to the parent" COMPONENT_REF must be given its
2497 proper type... */
2498 TREE_TYPE (gnu_get_parent) = gnu_parent;
2500 /* ...and reference the _parent field of this record. */
2501 gnu_field_list
2502 = create_field_decl (get_identifier
2503 (Get_Name_String (Name_uParent)),
2504 gnu_parent, gnu_type, 0,
2505 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2506 has_rep ? bitsize_zero_node : 0, 1);
2507 DECL_INTERNAL_P (gnu_field_list) = 1;
2508 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2511 /* Make the fields for the discriminants and put them into the record
2512 unless it's an Unchecked_Union. */
2513 if (Has_Discriminants (gnat_entity))
2514 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2515 Present (gnat_field);
2516 gnat_field = Next_Stored_Discriminant (gnat_field))
2518 /* If this is a record extension and this discriminant
2519 is the renaming of another discriminant, we've already
2520 handled the discriminant above. */
2521 if (Present (Parent_Subtype (gnat_entity))
2522 && Present (Corresponding_Discriminant (gnat_field)))
2523 continue;
2525 gnu_field
2526 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2528 /* Make an expression using a PLACEHOLDER_EXPR from the
2529 FIELD_DECL node just created and link that with the
2530 corresponding GNAT defining identifier. Then add to the
2531 list of fields. */
2532 save_gnu_tree (gnat_field,
2533 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2534 build0 (PLACEHOLDER_EXPR,
2535 DECL_CONTEXT (gnu_field)),
2536 gnu_field, NULL_TREE),
2537 true);
2539 if (!Is_Unchecked_Union (gnat_entity))
2541 TREE_CHAIN (gnu_field) = gnu_field_list;
2542 gnu_field_list = gnu_field;
2546 /* Put the discriminants into the record (backwards), so we can
2547 know the appropriate discriminant to use for the names of the
2548 variants. */
2549 TYPE_FIELDS (gnu_type) = gnu_field_list;
2551 /* Add the listed fields into the record and finish up. */
2552 components_to_record (gnu_type, Component_List (record_definition),
2553 gnu_field_list, packed, definition, NULL,
2554 false, all_rep, this_deferred,
2555 Is_Unchecked_Union (gnat_entity));
2557 if (this_deferred)
2559 debug_deferred = true;
2560 defer_debug_level++;
2562 defer_debug_incomplete_list
2563 = tree_cons (NULL_TREE, gnu_type,
2564 defer_debug_incomplete_list);
2567 /* We used to remove the associations of the discriminants and
2568 _Parent for validity checking, but we may need them if there's
2569 Freeze_Node for a subtype used in this record. */
2571 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2572 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2574 /* If it is a tagged record force the type to BLKmode to insure
2575 that these objects will always be placed in memory. Do the
2576 same thing for limited record types. */
2577 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2578 TYPE_MODE (gnu_type) = BLKmode;
2580 /* If this is a derived type, we must make the alias set of this type
2581 the same as that of the type we are derived from. We assume here
2582 that the other type is already frozen. */
2583 if (Etype (gnat_entity) != gnat_entity
2584 && !(Is_Private_Type (Etype (gnat_entity))
2585 && Full_View (Etype (gnat_entity)) == gnat_entity))
2586 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2588 /* Fill in locations of fields. */
2589 annotate_rep (gnat_entity, gnu_type);
2591 /* If there are any entities in the chain corresponding to
2592 components that we did not elaborate, ensure we elaborate their
2593 types if they are Itypes. */
2594 for (gnat_temp = First_Entity (gnat_entity);
2595 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2596 if ((Ekind (gnat_temp) == E_Component
2597 || Ekind (gnat_temp) == E_Discriminant)
2598 && Is_Itype (Etype (gnat_temp))
2599 && !present_gnu_tree (gnat_temp))
2600 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2602 break;
2604 case E_Class_Wide_Subtype:
2605 /* If an equivalent type is present, that is what we should use.
2606 Otherwise, fall through to handle this like a record subtype
2607 since it may have constraints. */
2609 if (Present (Equivalent_Type (gnat_entity)))
2611 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2612 NULL_TREE, 0);
2613 maybe_present = true;
2614 break;
2617 /* ... fall through ... */
2619 case E_Record_Subtype:
2621 /* If Cloned_Subtype is Present it means this record subtype has
2622 identical layout to that type or subtype and we should use
2623 that GCC type for this one. The front end guarantees that
2624 the component list is shared. */
2625 if (Present (Cloned_Subtype (gnat_entity)))
2627 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2628 NULL_TREE, 0);
2629 maybe_present = true;
2632 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2633 changing the type, make a new type with each field having the
2634 type of the field in the new subtype but having the position
2635 computed by transforming every discriminant reference according
2636 to the constraints. We don't see any difference between
2637 private and nonprivate type here since derivations from types should
2638 have been deferred until the completion of the private type. */
2639 else
2641 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2642 tree gnu_base_type;
2643 tree gnu_orig_type;
2645 if (!definition)
2646 defer_incomplete_level++, this_deferred = true;
2648 /* Get the base type initially for its alignment and sizes. But
2649 if it is a padded type, we do all the other work with the
2650 unpadded type. */
2651 gnu_type = gnu_orig_type = gnu_base_type
2652 = gnat_to_gnu_type (gnat_base_type);
2654 if (TREE_CODE (gnu_type) == RECORD_TYPE
2655 && TYPE_IS_PADDING_P (gnu_type))
2656 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2658 if (present_gnu_tree (gnat_entity))
2660 maybe_present = true;
2661 break;
2664 /* When the type has discriminants, and these discriminants
2665 affect the shape of what it built, factor them in.
2667 If we are making a subtype of an Unchecked_Union (must be an
2668 Itype), just return the type.
2670 We can't just use Is_Constrained because private subtypes without
2671 discriminants of full types with discriminants with default
2672 expressions are Is_Constrained but aren't constrained! */
2674 if (IN (Ekind (gnat_base_type), Record_Kind)
2675 && !Is_For_Access_Subtype (gnat_entity)
2676 && !Is_Unchecked_Union (gnat_base_type)
2677 && Is_Constrained (gnat_entity)
2678 && Stored_Constraint (gnat_entity) != No_Elist
2679 && Present (Discriminant_Constraint (gnat_entity)))
2681 Entity_Id gnat_field;
2682 tree gnu_field_list = 0;
2683 tree gnu_pos_list
2684 = compute_field_positions (gnu_orig_type, NULL_TREE,
2685 size_zero_node, bitsize_zero_node,
2686 BIGGEST_ALIGNMENT);
2687 tree gnu_subst_list
2688 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2689 definition);
2690 tree gnu_temp;
2692 gnu_type = make_node (RECORD_TYPE);
2693 TYPE_NAME (gnu_type) = gnu_entity_id;
2694 TYPE_STUB_DECL (gnu_type)
2695 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2696 gnat_entity);
2697 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2699 for (gnat_field = First_Entity (gnat_entity);
2700 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2701 if ((Ekind (gnat_field) == E_Component
2702 || Ekind (gnat_field) == E_Discriminant)
2703 && (Underlying_Type (Scope (Original_Record_Component
2704 (gnat_field)))
2705 == gnat_base_type)
2706 && (No (Corresponding_Discriminant (gnat_field))
2707 || !Is_Tagged_Type (gnat_base_type)))
2709 tree gnu_old_field
2710 = gnat_to_gnu_field_decl (Original_Record_Component
2711 (gnat_field));
2712 tree gnu_offset
2713 = TREE_VALUE (purpose_member (gnu_old_field,
2714 gnu_pos_list));
2715 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2716 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2717 tree gnu_field_type
2718 = gnat_to_gnu_type (Etype (gnat_field));
2719 tree gnu_size = TYPE_SIZE (gnu_field_type);
2720 tree gnu_new_pos = 0;
2721 unsigned int offset_align
2722 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2724 tree gnu_field;
2726 /* If there was a component clause, the field types must be
2727 the same for the type and subtype, so copy the data from
2728 the old field to avoid recomputation here. Also if the
2729 field is justified modular and the optimization in
2730 gnat_to_gnu_field was applied. */
2731 if (Present (Component_Clause
2732 (Original_Record_Component (gnat_field)))
2733 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2734 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2735 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2736 == TREE_TYPE (gnu_old_field)))
2738 gnu_size = DECL_SIZE (gnu_old_field);
2739 gnu_field_type = TREE_TYPE (gnu_old_field);
2742 /* If this was a bitfield, get the size from the old field.
2743 Also ensure the type can be placed into a bitfield. */
2744 else if (DECL_BIT_FIELD (gnu_old_field))
2746 gnu_size = DECL_SIZE (gnu_old_field);
2747 if (TYPE_MODE (gnu_field_type) == BLKmode
2748 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2749 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2750 gnu_field_type = make_packable_type (gnu_field_type);
2753 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2754 for (gnu_temp = gnu_subst_list;
2755 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2756 gnu_pos = substitute_in_expr (gnu_pos,
2757 TREE_PURPOSE (gnu_temp),
2758 TREE_VALUE (gnu_temp));
2760 /* If the size is now a constant, we can set it as the
2761 size of the field when we make it. Otherwise, we need
2762 to deal with it specially. */
2763 if (TREE_CONSTANT (gnu_pos))
2764 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2766 gnu_field
2767 = create_field_decl
2768 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2769 0, gnu_size, gnu_new_pos,
2770 !DECL_NONADDRESSABLE_P (gnu_old_field));
2772 if (!TREE_CONSTANT (gnu_pos))
2774 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2775 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2776 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2777 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2778 DECL_SIZE (gnu_field) = gnu_size;
2779 DECL_SIZE_UNIT (gnu_field)
2780 = convert (sizetype,
2781 size_binop (CEIL_DIV_EXPR, gnu_size,
2782 bitsize_unit_node));
2783 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2786 DECL_INTERNAL_P (gnu_field)
2787 = DECL_INTERNAL_P (gnu_old_field);
2788 SET_DECL_ORIGINAL_FIELD
2789 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2790 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2791 : gnu_old_field));
2792 DECL_DISCRIMINANT_NUMBER (gnu_field)
2793 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2794 TREE_THIS_VOLATILE (gnu_field)
2795 = TREE_THIS_VOLATILE (gnu_old_field);
2796 TREE_CHAIN (gnu_field) = gnu_field_list;
2797 gnu_field_list = gnu_field;
2798 save_gnu_tree (gnat_field, gnu_field, false);
2801 /* Now go through the entities again looking for Itypes that
2802 we have not elaborated but should (e.g., Etypes of fields
2803 that have Original_Components). */
2804 for (gnat_field = First_Entity (gnat_entity);
2805 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2806 if ((Ekind (gnat_field) == E_Discriminant
2807 || Ekind (gnat_field) == E_Component)
2808 && !present_gnu_tree (Etype (gnat_field)))
2809 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2811 finish_record_type (gnu_type, nreverse (gnu_field_list),
2812 true, false);
2814 /* Now set the size, alignment and alias set of the new type to
2815 match that of the old one, doing any substitutions, as
2816 above. */
2817 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2818 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2819 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2820 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2821 copy_alias_set (gnu_type, gnu_base_type);
2823 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2824 for (gnu_temp = gnu_subst_list;
2825 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2826 TYPE_SIZE (gnu_type)
2827 = substitute_in_expr (TYPE_SIZE (gnu_type),
2828 TREE_PURPOSE (gnu_temp),
2829 TREE_VALUE (gnu_temp));
2831 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2832 for (gnu_temp = gnu_subst_list;
2833 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2834 TYPE_SIZE_UNIT (gnu_type)
2835 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2836 TREE_PURPOSE (gnu_temp),
2837 TREE_VALUE (gnu_temp));
2839 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2840 for (gnu_temp = gnu_subst_list;
2841 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2842 SET_TYPE_ADA_SIZE
2843 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2844 TREE_PURPOSE (gnu_temp),
2845 TREE_VALUE (gnu_temp)));
2847 /* Recompute the mode of this record type now that we know its
2848 actual size. */
2849 compute_record_mode (gnu_type);
2851 /* Fill in locations of fields. */
2852 annotate_rep (gnat_entity, gnu_type);
2855 /* If we've made a new type, record it and make an XVS type to show
2856 what this is a subtype of. Some debuggers require the XVS
2857 type to be output first, so do it in that order. */
2858 if (gnu_type != gnu_orig_type)
2860 if (debug_info_p)
2862 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2863 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2865 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2866 gnu_orig_name = DECL_NAME (gnu_orig_name);
2868 TYPE_NAME (gnu_subtype_marker)
2869 = create_concat_name (gnat_entity, "XVS");
2870 finish_record_type (gnu_subtype_marker,
2871 create_field_decl (gnu_orig_name,
2872 integer_type_node,
2873 gnu_subtype_marker,
2874 0, NULL_TREE,
2875 NULL_TREE, 0),
2876 false, false);
2879 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2880 TYPE_NAME (gnu_type) = gnu_entity_id;
2881 TYPE_STUB_DECL (gnu_type)
2882 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2883 NULL, true, debug_info_p, gnat_entity);
2886 /* Otherwise, go down all the components in the new type and
2887 make them equivalent to those in the base type. */
2888 else
2889 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2890 gnat_temp = Next_Entity (gnat_temp))
2891 if ((Ekind (gnat_temp) == E_Discriminant
2892 && !Is_Unchecked_Union (gnat_base_type))
2893 || Ekind (gnat_temp) == E_Component)
2894 save_gnu_tree (gnat_temp,
2895 gnat_to_gnu_field_decl
2896 (Original_Record_Component (gnat_temp)), false);
2898 break;
2900 case E_Access_Subprogram_Type:
2901 case E_Anonymous_Access_Subprogram_Type:
2902 /* If we are not defining this entity, and we have incomplete
2903 entities being processed above us, make a dummy type and
2904 fill it in later. */
2905 if (!definition && defer_incomplete_level != 0)
2907 struct incomplete *p
2908 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2910 gnu_type
2911 = build_pointer_type
2912 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2913 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2914 !Comes_From_Source (gnat_entity),
2915 debug_info_p, gnat_entity);
2916 save_gnu_tree (gnat_entity, gnu_decl, false);
2917 this_made_decl = saved = true;
2919 p->old_type = TREE_TYPE (gnu_type);
2920 p->full_type = Directly_Designated_Type (gnat_entity);
2921 p->next = defer_incomplete_list;
2922 defer_incomplete_list = p;
2923 break;
2926 /* ... fall through ... */
2928 case E_Allocator_Type:
2929 case E_Access_Type:
2930 case E_Access_Attribute_Type:
2931 case E_Anonymous_Access_Type:
2932 case E_General_Access_Type:
2934 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2935 /* Get the "full view" of this entity. If this is an incomplete
2936 entity from a limited with, treat its non-limited view as the
2937 full view. Otherwise, if this is an incomplete or private
2938 type, use the full view. */
2939 Entity_Id gnat_desig_full
2940 = (IN (Ekind (gnat_desig_type), Incomplete_Kind)
2941 && From_With_Type (gnat_desig_type))
2942 ? Non_Limited_View (gnat_desig_type)
2943 : IN (Ekind (gnat_desig_type), Incomplete_Or_Private_Kind)
2944 ? Full_View (gnat_desig_type)
2945 : Empty;
2946 /* We want to know if we'll be seeing the freeze node for any
2947 incomplete type we may be pointing to. */
2948 bool in_main_unit
2949 = (Present (gnat_desig_full)
2950 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2951 : In_Extended_Main_Code_Unit (gnat_desig_type));
2952 bool got_fat_p = false;
2953 bool made_dummy = false;
2954 tree gnu_desig_type = NULL_TREE;
2955 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2957 if (!targetm.valid_pointer_mode (p_mode))
2958 p_mode = ptr_mode;
2960 if (No (gnat_desig_full)
2961 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2962 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2963 && Present (Equivalent_Type (gnat_desig_type)))))
2965 if (Present (Equivalent_Type (gnat_desig_type)))
2967 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2968 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2969 gnat_desig_full = Full_View (gnat_desig_full);
2971 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2972 Incomplete_Or_Private_Kind))
2973 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2976 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2977 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2979 /* If either the designated type or its full view is an
2980 unconstrained array subtype, replace it with the type it's a
2981 subtype of. This avoids problems with multiple copies of
2982 unconstrained array types. */
2983 if (Ekind (gnat_desig_type) == E_Array_Subtype
2984 && !Is_Constrained (gnat_desig_type))
2985 gnat_desig_type = Etype (gnat_desig_type);
2986 if (Present (gnat_desig_full)
2987 && Ekind (gnat_desig_full) == E_Array_Subtype
2988 && !Is_Constrained (gnat_desig_full))
2989 gnat_desig_full = Etype (gnat_desig_full);
2991 /* If the designated type is a subtype of an incomplete record type,
2992 use the parent type to avoid order of elaboration issues. This
2993 can lose some code efficiency, but there is no alternative. */
2994 if (Present (gnat_desig_full)
2995 && Ekind (gnat_desig_full) == E_Record_Subtype
2996 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2997 gnat_desig_full = Etype (gnat_desig_full);
2999 /* If we are pointing to an incomplete type whose completion is an
3000 unconstrained array, make a fat pointer type instead of a pointer
3001 to VOID. The two types in our fields will be pointers to VOID and
3002 will be replaced in update_pointer_to. Similarly, if the type
3003 itself is a dummy type or an unconstrained array. Also make
3004 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
3005 pointers to it. */
3007 if ((Present (gnat_desig_full)
3008 && Is_Array_Type (gnat_desig_full)
3009 && !Is_Constrained (gnat_desig_full))
3010 || (present_gnu_tree (gnat_desig_type)
3011 && TYPE_IS_DUMMY_P (TREE_TYPE
3012 (get_gnu_tree (gnat_desig_type)))
3013 && Is_Array_Type (gnat_desig_type)
3014 && !Is_Constrained (gnat_desig_type))
3015 || (present_gnu_tree (gnat_desig_type)
3016 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
3017 == UNCONSTRAINED_ARRAY_TYPE)
3018 && !(TYPE_POINTER_TO (TREE_TYPE
3019 (get_gnu_tree (gnat_desig_type)))))
3020 || (No (gnat_desig_full) && !in_main_unit
3021 && defer_incomplete_level
3022 && !present_gnu_tree (gnat_desig_type)
3023 && Is_Array_Type (gnat_desig_type)
3024 && ! Is_Constrained (gnat_desig_type))
3025 || (in_main_unit && From_With_Type (gnat_entity)
3026 && (Present (gnat_desig_full)
3027 ? Present (Freeze_Node (gnat_desig_full))
3028 : Present (Freeze_Node (gnat_desig_type)))
3029 && Is_Array_Type (gnat_desig_type)
3030 && !Is_Constrained (gnat_desig_type)))
3032 tree gnu_old
3033 = (present_gnu_tree (gnat_desig_type)
3034 ? gnat_to_gnu_type (gnat_desig_type)
3035 : make_dummy_type (gnat_desig_type));
3036 tree fields;
3038 /* Show the dummy we get will be a fat pointer. */
3039 got_fat_p = made_dummy = true;
3041 /* If the call above got something that has a pointer, that
3042 pointer is our type. This could have happened either
3043 because the type was elaborated or because somebody
3044 else executed the code below. */
3045 gnu_type = TYPE_POINTER_TO (gnu_old);
3046 if (!gnu_type)
3048 gnu_type = make_node (RECORD_TYPE);
3049 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3050 TYPE_POINTER_TO (gnu_old) = gnu_type;
3052 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3053 fields
3054 = chainon (chainon (NULL_TREE,
3055 create_field_decl
3056 (get_identifier ("P_ARRAY"),
3057 ptr_void_type_node, gnu_type,
3058 0, 0, 0, 0)),
3059 create_field_decl (get_identifier ("P_BOUNDS"),
3060 ptr_void_type_node,
3061 gnu_type, 0, 0, 0, 0));
3063 /* Make sure we can place this into a register. */
3064 TYPE_ALIGN (gnu_type)
3065 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3066 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3067 finish_record_type (gnu_type, fields, false, true);
3069 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3070 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3071 = concat_id_with_name (get_entity_name (gnat_desig_type),
3072 "XUT");
3073 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3077 /* If we already know what the full type is, use it. */
3078 else if (Present (gnat_desig_full)
3079 && present_gnu_tree (gnat_desig_full))
3080 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3082 /* Get the type of the thing we are to point to and build a pointer
3083 to it. If it is a reference to an incomplete or private type with a
3084 full view that is a record, make a dummy type node and get the
3085 actual type later when we have verified it is safe. */
3086 else if (!in_main_unit
3087 && !present_gnu_tree (gnat_desig_type)
3088 && Present (gnat_desig_full)
3089 && !present_gnu_tree (gnat_desig_full)
3090 && Is_Record_Type (gnat_desig_full))
3092 gnu_desig_type = make_dummy_type (gnat_desig_type);
3093 made_dummy = true;
3096 /* Likewise if we are pointing to a record or array and we are to defer
3097 elaborating incomplete types. We do this since this access type
3098 may be the full view of some private type. Note that the
3099 unconstrained array case is handled above. */
3100 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
3101 && !present_gnu_tree (gnat_desig_type)
3102 && ((Is_Record_Type (gnat_desig_type)
3103 || Is_Array_Type (gnat_desig_type))
3104 || (Present (gnat_desig_full)
3105 && (Is_Record_Type (gnat_desig_full)
3106 || Is_Array_Type (gnat_desig_full)))))
3108 gnu_desig_type = make_dummy_type (gnat_desig_type);
3109 made_dummy = true;
3112 /* If this is a reference from a limited_with type back to our
3113 main unit and there's a Freeze_Node for it, either we have
3114 already processed the declaration and made the dummy type,
3115 in which case we just reuse the latter, or we have not yet,
3116 in which case we make the dummy type and it will be reused
3117 when the declaration is processed. In both cases, the pointer
3118 eventually created below will be automatically adjusted when
3119 the Freeze_Node is processed. Note that the unconstrained
3120 array case is handled above. */
3121 else if (in_main_unit && From_With_Type (gnat_entity)
3122 && (Present (gnat_desig_full)
3123 ? Present (Freeze_Node (gnat_desig_full))
3124 : Present (Freeze_Node (gnat_desig_type))))
3126 gnu_desig_type = make_dummy_type (gnat_desig_type);
3127 made_dummy = true;
3130 else if (gnat_desig_type == gnat_entity)
3132 gnu_type
3133 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3134 p_mode,
3135 No_Strict_Aliasing (gnat_entity));
3136 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3139 else
3140 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3142 /* It is possible that the above call to gnat_to_gnu_type resolved our
3143 type. If so, just return it. */
3144 if (present_gnu_tree (gnat_entity))
3146 maybe_present = true;
3147 break;
3150 /* If we have a GCC type for the designated type, possibly modify it
3151 if we are pointing only to constant objects and then make a pointer
3152 to it. Don't do this for unconstrained arrays. */
3153 if (!gnu_type && gnu_desig_type)
3155 if (Is_Access_Constant (gnat_entity)
3156 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3158 gnu_desig_type
3159 = build_qualified_type
3160 (gnu_desig_type,
3161 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3163 /* Some extra processing is required if we are building a
3164 pointer to an incomplete type (in the GCC sense). We might
3165 have such a type if we just made a dummy, or directly out
3166 of the call to gnat_to_gnu_type above if we are processing
3167 an access type for a record component designating the
3168 record type itself. */
3169 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3171 /* We must ensure that the pointer to variant we make will
3172 be processed by update_pointer_to when the initial type
3173 is completed. Pretend we made a dummy and let further
3174 processing act as usual. */
3175 made_dummy = true;
3177 /* We must ensure that update_pointer_to will not retrieve
3178 the dummy variant when building a properly qualified
3179 version of the complete type. We take advantage of the
3180 fact that get_qualified_type is requiring TYPE_NAMEs to
3181 match to influence build_qualified_type and then also
3182 update_pointer_to here. */
3183 TYPE_NAME (gnu_desig_type)
3184 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3188 gnu_type
3189 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3190 No_Strict_Aliasing (gnat_entity));
3193 /* If we are not defining this object and we made a dummy pointer,
3194 save our current definition, evaluate the actual type, and replace
3195 the tentative type we made with the actual one. If we are to defer
3196 actually looking up the actual type, make an entry in the
3197 deferred list. */
3199 if (!in_main_unit && made_dummy)
3201 tree gnu_old_type
3202 = TYPE_FAT_POINTER_P (gnu_type)
3203 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3205 if (esize == POINTER_SIZE
3206 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3207 gnu_type
3208 = build_pointer_type
3209 (TYPE_OBJECT_RECORD_TYPE
3210 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3212 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3213 !Comes_From_Source (gnat_entity),
3214 debug_info_p, gnat_entity);
3215 save_gnu_tree (gnat_entity, gnu_decl, false);
3216 this_made_decl = saved = true;
3218 if (defer_incomplete_level == 0)
3219 /* Note that the call to gnat_to_gnu_type here might have
3220 updated gnu_old_type directly, in which case it is not a
3221 dummy type any more when we get into update_pointer_to.
3223 This may happen for instance when the designated type is a
3224 record type, because their elaboration starts with an
3225 initial node from make_dummy_type, which may yield the same
3226 node as the one we got.
3228 Besides, variants of this non-dummy type might have been
3229 created along the way. update_pointer_to is expected to
3230 properly take care of those situations. */
3231 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3232 gnat_to_gnu_type (gnat_desig_type));
3233 else
3235 struct incomplete *p
3236 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3238 p->old_type = gnu_old_type;
3239 p->full_type = gnat_desig_type;
3240 p->next = defer_incomplete_list;
3241 defer_incomplete_list = p;
3245 break;
3247 case E_Access_Protected_Subprogram_Type:
3248 case E_Anonymous_Access_Protected_Subprogram_Type:
3249 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3250 gnu_type = build_pointer_type (void_type_node);
3251 else
3253 /* The runtime representation is the equivalent type. */
3254 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3255 maybe_present = 1;
3258 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3259 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3260 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3261 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3262 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3263 NULL_TREE, 0);
3265 break;
3267 case E_Access_Subtype:
3269 /* We treat this as identical to its base type; any constraint is
3270 meaningful only to the front end.
3272 The designated type must be elaborated as well, if it does
3273 not have its own freeze node. Designated (sub)types created
3274 for constrained components of records with discriminants are
3275 not frozen by the front end and thus not elaborated by gigi,
3276 because their use may appear before the base type is frozen,
3277 and because it is not clear that they are needed anywhere in
3278 Gigi. With the current model, there is no correct place where
3279 they could be elaborated. */
3281 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3282 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3283 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3284 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3285 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3287 /* If we are not defining this entity, and we have incomplete
3288 entities being processed above us, make a dummy type and
3289 elaborate it later. */
3290 if (!definition && defer_incomplete_level != 0)
3292 struct incomplete *p
3293 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3294 tree gnu_ptr_type
3295 = build_pointer_type
3296 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3298 p->old_type = TREE_TYPE (gnu_ptr_type);
3299 p->full_type = Directly_Designated_Type (gnat_entity);
3300 p->next = defer_incomplete_list;
3301 defer_incomplete_list = p;
3303 else if (IN (Ekind (Base_Type
3304 (Directly_Designated_Type (gnat_entity))),
3305 Incomplete_Or_Private_Kind))
3307 else
3308 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3309 NULL_TREE, 0);
3312 maybe_present = true;
3313 break;
3315 /* Subprogram Entities
3317 The following access functions are defined for subprograms (functions
3318 or procedures):
3320 First_Formal The first formal parameter.
3321 Is_Imported Indicates that the subprogram has appeared in
3322 an INTERFACE or IMPORT pragma. For now we
3323 assume that the external language is C.
3324 Is_Inlined True if the subprogram is to be inlined.
3326 In addition for function subprograms we have:
3328 Etype Return type of the function.
3330 Each parameter is first checked by calling must_pass_by_ref on its
3331 type to determine if it is passed by reference. For parameters which
3332 are copied in, if they are Ada IN OUT or OUT parameters, their return
3333 value becomes part of a record which becomes the return type of the
3334 function (C function - note that this applies only to Ada procedures
3335 so there is no Ada return type). Additional code to store back the
3336 parameters will be generated on the caller side. This transformation
3337 is done here, not in the front-end.
3339 The intended result of the transformation can be seen from the
3340 equivalent source rewritings that follow:
3342 struct temp {int a,b};
3343 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3344 .. ..
3345 end P; return {A,B};
3347 procedure call
3350 temp t;
3351 P(X,Y); t = P(X,Y);
3352 X = t.a , Y = t.b;
3355 For subprogram types we need to perform mainly the same conversions to
3356 GCC form that are needed for procedures and function declarations. The
3357 only difference is that at the end, we make a type declaration instead
3358 of a function declaration. */
3360 case E_Subprogram_Type:
3361 case E_Function:
3362 case E_Procedure:
3364 /* The first GCC parameter declaration (a PARM_DECL node). The
3365 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3366 actually is the head of this parameter list. */
3367 tree gnu_param_list = NULL_TREE;
3368 /* The type returned by a function. If the subprogram is a procedure
3369 this type should be void_type_node. */
3370 tree gnu_return_type = void_type_node;
3371 /* List of fields in return type of procedure with copy in copy out
3372 parameters. */
3373 tree gnu_field_list = NULL_TREE;
3374 /* Non-null for subprograms containing parameters passed by copy in
3375 copy out (Ada IN OUT or OUT parameters not passed by reference),
3376 in which case it is the list of nodes used to specify the values of
3377 the in out/out parameters that are returned as a record upon
3378 procedure return. The TREE_PURPOSE of an element of this list is
3379 a field of the record and the TREE_VALUE is the PARM_DECL
3380 corresponding to that field. This list will be saved in the
3381 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3382 tree gnu_return_list = NULL_TREE;
3383 /* If an import pragma asks to map this subprogram to a GCC builtin,
3384 this is the builtin DECL node. */
3385 tree gnu_builtin_decl = NULL_TREE;
3386 Entity_Id gnat_param;
3387 bool inline_flag = Is_Inlined (gnat_entity);
3388 bool public_flag = Is_Public (gnat_entity);
3389 bool extern_flag
3390 = (Is_Public (gnat_entity) && !definition) || imported_p;
3391 bool pure_flag = Is_Pure (gnat_entity);
3392 bool volatile_flag = No_Return (gnat_entity);
3393 bool returns_by_ref = false;
3394 bool returns_unconstrained = false;
3395 bool returns_by_target_ptr = false;
3396 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3397 bool has_copy_in_out = false;
3398 int parmnum;
3400 if (kind == E_Subprogram_Type && !definition)
3401 /* A parameter may refer to this type, so defer completion
3402 of any incomplete types. */
3403 defer_incomplete_level++, this_deferred = true;
3405 /* If the subprogram has an alias, it is probably inherited, so
3406 we can use the original one. If the original "subprogram"
3407 is actually an enumeration literal, it may be the first use
3408 of its type, so we must elaborate that type now. */
3409 if (Present (Alias (gnat_entity)))
3411 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3412 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3414 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3415 gnu_expr, 0);
3417 /* Elaborate any Itypes in the parameters of this entity. */
3418 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3419 Present (gnat_temp);
3420 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3421 if (Is_Itype (Etype (gnat_temp)))
3422 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3424 break;
3427 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3428 corresponding DECL node.
3430 We still want the parameter associations to take place because the
3431 proper generation of calls depends on it (a GNAT parameter without
3432 a corresponding GCC tree has a very specific meaning), so we don't
3433 just break here. */
3434 if (Convention (gnat_entity) == Convention_Intrinsic)
3435 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3437 /* ??? What if we don't find the builtin node above ? warn ? err ?
3438 In the current state we neither warn nor err, and calls will just
3439 be handled as for regular subprograms. */
3441 if (kind == E_Function || kind == E_Subprogram_Type)
3442 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3444 /* If this function returns by reference, make the actual
3445 return type of this function the pointer and mark the decl. */
3446 if (Returns_By_Ref (gnat_entity))
3448 returns_by_ref = true;
3449 gnu_return_type = build_pointer_type (gnu_return_type);
3452 /* If the Mechanism is By_Reference, ensure the return type uses
3453 the machine's by-reference mechanism, which may not the same
3454 as above (e.g., it might be by passing a fake parameter). */
3455 else if (kind == E_Function
3456 && Mechanism (gnat_entity) == By_Reference)
3458 TREE_ADDRESSABLE (gnu_return_type) = 1;
3460 /* We expect this bit to be reset by gigi shortly, so can avoid a
3461 type node copy here. This actually also prevents troubles with
3462 the generation of debug information for the function, because
3463 we might have issued such info for this type already, and would
3464 be attaching a distinct type node to the function if we made a
3465 copy here. */
3468 /* If we are supposed to return an unconstrained array,
3469 actually return a fat pointer and make a note of that. Return
3470 a pointer to an unconstrained record of variable size. */
3471 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3473 gnu_return_type = TREE_TYPE (gnu_return_type);
3474 returns_unconstrained = true;
3477 /* If the type requires a transient scope, the result is allocated
3478 on the secondary stack, so the result type of the function is
3479 just a pointer. */
3480 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3482 gnu_return_type = build_pointer_type (gnu_return_type);
3483 returns_unconstrained = true;
3486 /* If the type is a padded type and the underlying type would not
3487 be passed by reference or this function has a foreign convention,
3488 return the underlying type. */
3489 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3490 && TYPE_IS_PADDING_P (gnu_return_type)
3491 && (!default_pass_by_ref (TREE_TYPE
3492 (TYPE_FIELDS (gnu_return_type)))
3493 || Has_Foreign_Convention (gnat_entity)))
3494 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3496 /* If the return type is unconstrained, that means it must have a
3497 maximum size. We convert the function into a procedure and its
3498 caller will pass a pointer to an object of that maximum size as the
3499 first parameter when we call the function. */
3500 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3502 returns_by_target_ptr = true;
3503 gnu_param_list
3504 = create_param_decl (get_identifier ("TARGET"),
3505 build_reference_type (gnu_return_type),
3506 true);
3507 gnu_return_type = void_type_node;
3510 /* If the return type has a size that overflows, we cannot have
3511 a function that returns that type. This usage doesn't make
3512 sense anyway, so give an error here. */
3513 if (TYPE_SIZE_UNIT (gnu_return_type)
3514 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3515 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3517 post_error ("cannot return type whose size overflows",
3518 gnat_entity);
3519 gnu_return_type = copy_node (gnu_return_type);
3520 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3521 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3522 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3523 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3526 /* Look at all our parameters and get the type of
3527 each. While doing this, build a copy-out structure if
3528 we need one. */
3530 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3531 Present (gnat_param);
3532 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3534 tree gnu_param_name = get_entity_name (gnat_param);
3535 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3536 tree gnu_param, gnu_field;
3537 bool by_ref_p = false;
3538 bool by_descr_p = false;
3539 bool by_component_ptr_p = false;
3540 bool copy_in_copy_out_flag = false;
3541 bool req_by_copy = false, req_by_ref = false;
3543 /* Builtins are expanded inline and there is no real call sequence
3544 involved. so the type expected by the underlying expander is
3545 always the type of each argument "as is". */
3546 if (gnu_builtin_decl)
3547 req_by_copy = 1;
3549 /* Otherwise, see if a Mechanism was supplied that forced this
3550 parameter to be passed one way or another. */
3551 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3552 req_by_copy = true;
3553 else if (Mechanism (gnat_param) == Default)
3555 else if (Mechanism (gnat_param) == By_Copy)
3556 req_by_copy = true;
3557 else if (Mechanism (gnat_param) == By_Reference)
3558 req_by_ref = true;
3559 else if (Mechanism (gnat_param) <= By_Descriptor)
3560 by_descr_p = true;
3561 else if (Mechanism (gnat_param) > 0)
3563 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3564 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3565 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3566 Mechanism (gnat_param)))
3567 req_by_ref = true;
3568 else
3569 req_by_copy = true;
3571 else
3572 post_error ("unsupported mechanism for&", gnat_param);
3574 /* If this is either a foreign function or if the
3575 underlying type won't be passed by reference, strip off
3576 possible padding type. */
3577 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3578 && TYPE_IS_PADDING_P (gnu_param_type)
3579 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3580 || (!must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3581 (gnu_param_type)))
3582 && (req_by_copy
3583 || !default_pass_by_ref (TREE_TYPE
3584 (TYPE_FIELDS
3585 (gnu_param_type)))))))
3586 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3588 /* If this is an IN parameter it is read-only, so make a variant
3589 of the type that is read-only.
3591 ??? However, if this is an unconstrained array, that type can
3592 be very complex. So skip it for now. Likewise for any other
3593 self-referential type. */
3594 if (Ekind (gnat_param) == E_In_Parameter
3595 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3596 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3597 gnu_param_type
3598 = build_qualified_type (gnu_param_type,
3599 (TYPE_QUALS (gnu_param_type)
3600 | TYPE_QUAL_CONST));
3602 /* For foreign conventions, pass arrays as a pointer to the
3603 underlying type. First check for unconstrained array and get
3604 the underlying array. Then get the component type and build
3605 a pointer to it. */
3606 if (Has_Foreign_Convention (gnat_entity)
3607 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3608 gnu_param_type
3609 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3610 (TREE_TYPE (gnu_param_type))));
3612 if (by_descr_p)
3613 gnu_param_type
3614 = build_pointer_type
3615 (build_vms_descriptor (gnu_param_type,
3616 Mechanism (gnat_param), gnat_entity));
3618 else if (Has_Foreign_Convention (gnat_entity)
3619 && !req_by_copy
3620 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3622 /* Strip off any multi-dimensional entries, then strip
3623 off the last array to get the component type. */
3624 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3625 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3626 gnu_param_type = TREE_TYPE (gnu_param_type);
3628 by_component_ptr_p = true;
3629 gnu_param_type = TREE_TYPE (gnu_param_type);
3631 if (Ekind (gnat_param) == E_In_Parameter)
3632 gnu_param_type
3633 = build_qualified_type (gnu_param_type,
3634 (TYPE_QUALS (gnu_param_type)
3635 | TYPE_QUAL_CONST));
3637 gnu_param_type = build_pointer_type (gnu_param_type);
3640 /* Fat pointers are passed as thin pointers for foreign
3641 conventions. */
3642 else if (Has_Foreign_Convention (gnat_entity)
3643 && TYPE_FAT_POINTER_P (gnu_param_type))
3644 gnu_param_type
3645 = make_type_from_size (gnu_param_type,
3646 size_int (POINTER_SIZE), false);
3648 /* If we must pass or were requested to pass by reference, do so.
3649 If we were requested to pass by copy, do so.
3650 Otherwise, for foreign conventions, pass all in out parameters
3651 or aggregates by reference. For COBOL and Fortran, pass
3652 all integer and FP types that way too. For Convention Ada,
3653 use the standard Ada default. */
3654 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3655 || (!req_by_copy
3656 && ((Has_Foreign_Convention (gnat_entity)
3657 && (Ekind (gnat_param) != E_In_Parameter
3658 || AGGREGATE_TYPE_P (gnu_param_type)))
3659 || (((Convention (gnat_entity)
3660 == Convention_Fortran)
3661 || (Convention (gnat_entity)
3662 == Convention_COBOL))
3663 && (INTEGRAL_TYPE_P (gnu_param_type)
3664 || FLOAT_TYPE_P (gnu_param_type)))
3665 /* For convention Ada, see if we pass by reference
3666 by default. */
3667 || (!Has_Foreign_Convention (gnat_entity)
3668 && default_pass_by_ref (gnu_param_type)))))
3670 gnu_param_type = build_reference_type (gnu_param_type);
3671 by_ref_p = true;
3674 else if (Ekind (gnat_param) != E_In_Parameter)
3675 copy_in_copy_out_flag = true;
3677 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3678 post_error ("?cannot pass & by copy", gnat_param);
3680 /* If this is an OUT parameter that isn't passed by reference
3681 and isn't a pointer or aggregate, we don't make a PARM_DECL
3682 for it. Instead, it will be a VAR_DECL created when we process
3683 the procedure. For the special parameter of Valued_Procedure,
3684 never pass it in.
3686 An exception is made to cover the RM-6.4.1 rule requiring "by
3687 copy" out parameters with discriminants or implicit initial
3688 values to be handled like in out parameters. These type are
3689 normally built as aggregates, and hence passed by reference,
3690 except for some packed arrays which end up encoded in special
3691 integer types.
3693 The exception we need to make is then for packed arrays of
3694 records with discriminants or implicit initial values. We have
3695 no light/easy way to check for the latter case, so we merely
3696 check for packed arrays of records. This may lead to useless
3697 copy-in operations, but in very rare cases only, as these would
3698 be exceptions in a set of already exceptional situations. */
3699 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3700 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3701 || (!by_descr_p
3702 && !POINTER_TYPE_P (gnu_param_type)
3703 && !AGGREGATE_TYPE_P (gnu_param_type)))
3704 && !(Is_Array_Type (Etype (gnat_param))
3705 && Is_Packed (Etype (gnat_param))
3706 && Is_Composite_Type (Component_Type
3707 (Etype (gnat_param)))))
3708 gnu_param = NULL_TREE;
3709 else
3711 gnu_param
3712 = create_param_decl
3713 (gnu_param_name, gnu_param_type,
3714 by_ref_p || by_component_ptr_p
3715 || Ekind (gnat_param) == E_In_Parameter);
3717 DECL_BY_REF_P (gnu_param) = by_ref_p;
3718 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3719 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3720 DECL_POINTS_TO_READONLY_P (gnu_param)
3721 = (Ekind (gnat_param) == E_In_Parameter
3722 && (by_ref_p || by_component_ptr_p));
3723 Sloc_to_locus (Sloc (gnat_param),
3724 &DECL_SOURCE_LOCATION (gnu_param));
3725 save_gnu_tree (gnat_param, gnu_param, false);
3726 gnu_param_list = chainon (gnu_param, gnu_param_list);
3728 /* If a parameter is a pointer, this function may modify
3729 memory through it and thus shouldn't be considered
3730 a pure function. Also, the memory may be modified
3731 between two calls, so they can't be CSE'ed. The latter
3732 case also handles by-ref parameters. */
3733 if (POINTER_TYPE_P (gnu_param_type)
3734 || TYPE_FAT_POINTER_P (gnu_param_type))
3735 pure_flag = false;
3738 if (copy_in_copy_out_flag)
3740 if (!has_copy_in_out)
3742 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3743 gnu_return_type = make_node (RECORD_TYPE);
3744 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3745 has_copy_in_out = true;
3748 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3749 gnu_return_type, 0, 0, 0, 0);
3750 Sloc_to_locus (Sloc (gnat_param),
3751 &DECL_SOURCE_LOCATION (gnu_field));
3752 TREE_CHAIN (gnu_field) = gnu_field_list;
3753 gnu_field_list = gnu_field;
3754 gnu_return_list = tree_cons (gnu_field, gnu_param,
3755 gnu_return_list);
3759 /* Do not compute record for out parameters if subprogram is
3760 stubbed since structures are incomplete for the back-end. */
3761 if (gnu_field_list
3762 && Convention (gnat_entity) != Convention_Stubbed)
3764 /* If all types are not complete, defer emission of debug
3765 information for this record types. Otherwise, we risk emitting
3766 debug information for a dummy type contained in the fields
3767 for that record. */
3768 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3769 false, defer_incomplete_level);
3771 if (defer_incomplete_level)
3773 debug_deferred = true;
3774 defer_debug_level++;
3776 defer_debug_incomplete_list
3777 = tree_cons (NULL_TREE, gnu_return_type,
3778 defer_debug_incomplete_list);
3782 /* If we have a CICO list but it has only one entry, we convert
3783 this function into a function that simply returns that one
3784 object. */
3785 if (list_length (gnu_return_list) == 1)
3786 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3788 if (Has_Stdcall_Convention (gnat_entity))
3790 struct attrib *attr
3791 = (struct attrib *) xmalloc (sizeof (struct attrib));
3793 attr->next = attr_list;
3794 attr->type = ATTR_MACHINE_ATTRIBUTE;
3795 attr->name = get_identifier ("stdcall");
3796 attr->args = NULL_TREE;
3797 attr->error_point = gnat_entity;
3798 attr_list = attr;
3801 /* Both lists ware built in reverse. */
3802 gnu_param_list = nreverse (gnu_param_list);
3803 gnu_return_list = nreverse (gnu_return_list);
3805 gnu_type
3806 = create_subprog_type (gnu_return_type, gnu_param_list,
3807 gnu_return_list, returns_unconstrained,
3808 returns_by_ref,
3809 Function_Returns_With_DSP (gnat_entity),
3810 returns_by_target_ptr);
3812 /* A subprogram (something that doesn't return anything) shouldn't
3813 be considered Pure since there would be no reason for such a
3814 subprogram. Note that procedures with Out (or In Out) parameters
3815 have already been converted into a function with a return type. */
3816 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3817 pure_flag = false;
3819 /* The semantics of "pure" in Ada essentially matches that of "const"
3820 in the back-end. In particular, both properties are orthogonal to
3821 the "nothrow" property. But this is true only if the EH circuitry
3822 is explicit in the internal representation of the back-end. If we
3823 are to completely hide the EH circuitry from it, we need to declare
3824 that calls to pure Ada subprograms that can throw have side effects
3825 since they can trigger an "abnormal" transfer of control flow; thus
3826 they can be neither "const" nor "pure" in the back-end sense. */
3827 gnu_type
3828 = build_qualified_type (gnu_type,
3829 TYPE_QUALS (gnu_type)
3830 | (Exception_Mechanism == Back_End_Exceptions
3831 ? TYPE_QUAL_CONST * pure_flag : 0)
3832 | (TYPE_QUAL_VOLATILE * volatile_flag));
3834 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3836 /* If we have a builtin decl for that function, check the signatures
3837 compatibilities. If the signatures are compatible, use the builtin
3838 decl. If they are not, we expect the checker predicate to have
3839 posted the appropriate errors, and just continue with what we have
3840 so far. */
3841 if (gnu_builtin_decl)
3843 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3845 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3847 gnu_decl = gnu_builtin_decl;
3848 gnu_type = gnu_builtin_type;
3849 break;
3853 /* If there was no specified Interface_Name and the external and
3854 internal names of the subprogram are the same, only use the
3855 internal name to allow disambiguation of nested subprograms. */
3856 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3857 gnu_ext_name = NULL_TREE;
3859 /* If we are defining the subprogram and it has an Address clause
3860 we must get the address expression from the saved GCC tree for the
3861 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3862 the address expression here since the front-end has guaranteed
3863 in that case that the elaboration has no effects. If there is
3864 an Address clause and we are not defining the object, just
3865 make it a constant. */
3866 if (Present (Address_Clause (gnat_entity)))
3868 tree gnu_address = NULL_TREE;
3870 if (definition)
3871 gnu_address
3872 = (present_gnu_tree (gnat_entity)
3873 ? get_gnu_tree (gnat_entity)
3874 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3876 save_gnu_tree (gnat_entity, NULL_TREE, false);
3878 gnu_type = build_reference_type (gnu_type);
3879 if (gnu_address)
3880 gnu_address = convert (gnu_type, gnu_address);
3882 gnu_decl
3883 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3884 gnu_address, false, Is_Public (gnat_entity),
3885 extern_flag, false, NULL, gnat_entity);
3886 DECL_BY_REF_P (gnu_decl) = 1;
3889 else if (kind == E_Subprogram_Type)
3890 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3891 !Comes_From_Source (gnat_entity),
3892 debug_info_p && !defer_incomplete_level,
3893 gnat_entity);
3894 else
3896 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3897 gnu_type, gnu_param_list,
3898 inline_flag, public_flag,
3899 extern_flag, attr_list,
3900 gnat_entity);
3902 DECL_STUBBED_P (gnu_decl)
3903 = Convention (gnat_entity) == Convention_Stubbed;
3906 break;
3908 case E_Incomplete_Type:
3909 case E_Incomplete_Subtype:
3910 case E_Private_Type:
3911 case E_Private_Subtype:
3912 case E_Limited_Private_Type:
3913 case E_Limited_Private_Subtype:
3914 case E_Record_Type_With_Private:
3915 case E_Record_Subtype_With_Private:
3917 /* Get the "full view" of this entity. If this is an incomplete
3918 entity from a limited with, treat its non-limited view as the
3919 full view. Otherwise, use either the full view or the underlying
3920 full view, whichever is present. This is used in all the tests
3921 below. */
3922 Entity_Id full_view
3923 = (IN (Ekind (gnat_entity), Incomplete_Kind)
3924 && From_With_Type (gnat_entity))
3925 ? Non_Limited_View (gnat_entity)
3926 : Present (Full_View (gnat_entity))
3927 ? Full_View (gnat_entity)
3928 : Underlying_Full_View (gnat_entity);
3930 /* If this is an incomplete type with no full view, it must be a Taft
3931 Amendment type, in which case we return a dummy type. Otherwise,
3932 just get the type from its Etype. */
3933 if (No (full_view))
3935 if (kind == E_Incomplete_Type)
3936 gnu_type = make_dummy_type (gnat_entity);
3937 else
3939 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3940 NULL_TREE, 0);
3941 maybe_present = true;
3943 break;
3946 /* If we already made a type for the full view, reuse it. */
3947 else if (present_gnu_tree (full_view))
3949 gnu_decl = get_gnu_tree (full_view);
3950 break;
3953 /* Otherwise, if we are not defining the type now, get the type
3954 from the full view. But always get the type from the full view
3955 for define on use types, since otherwise we won't see them! */
3956 else if (!definition
3957 || (Is_Itype (full_view)
3958 && No (Freeze_Node (gnat_entity)))
3959 || (Is_Itype (gnat_entity)
3960 && No (Freeze_Node (full_view))))
3962 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
3963 maybe_present = true;
3964 break;
3967 /* For incomplete types, make a dummy type entry which will be
3968 replaced later. */
3969 gnu_type = make_dummy_type (gnat_entity);
3971 /* Save this type as the full declaration's type so we can do any
3972 needed updates when we see it. */
3973 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3974 !Comes_From_Source (gnat_entity),
3975 debug_info_p, gnat_entity);
3976 save_gnu_tree (full_view, gnu_decl, 0);
3977 break;
3980 /* Simple class_wide types are always viewed as their root_type
3981 by Gigi unless an Equivalent_Type is specified. */
3982 case E_Class_Wide_Type:
3983 if (Present (Equivalent_Type (gnat_entity)))
3984 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3985 else
3986 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3988 maybe_present = true;
3989 break;
3991 case E_Task_Type:
3992 case E_Task_Subtype:
3993 case E_Protected_Type:
3994 case E_Protected_Subtype:
3995 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3996 gnu_type = void_type_node;
3997 else
3998 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
4000 maybe_present = true;
4001 break;
4003 case E_Label:
4004 gnu_decl = create_label_decl (gnu_entity_id);
4005 break;
4007 case E_Block:
4008 case E_Loop:
4009 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4010 we've already saved it, so we don't try to. */
4011 gnu_decl = error_mark_node;
4012 saved = true;
4013 break;
4015 default:
4016 gcc_unreachable ();
4019 /* If we had a case where we evaluated another type and it might have
4020 defined this one, handle it here. */
4021 if (maybe_present && present_gnu_tree (gnat_entity))
4023 gnu_decl = get_gnu_tree (gnat_entity);
4024 saved = true;
4027 /* If we are processing a type and there is either no decl for it or
4028 we just made one, do some common processing for the type, such as
4029 handling alignment and possible padding. */
4031 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4033 if (Is_Tagged_Type (gnat_entity)
4034 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4035 TYPE_ALIGN_OK (gnu_type) = 1;
4037 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4038 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4040 /* ??? Don't set the size for a String_Literal since it is either
4041 confirming or we don't handle it properly (if the low bound is
4042 non-constant). */
4043 if (!gnu_size && kind != E_String_Literal_Subtype)
4044 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4045 TYPE_DECL, false,
4046 Has_Size_Clause (gnat_entity));
4048 /* If a size was specified, see if we can make a new type of that size
4049 by rearranging the type, for example from a fat to a thin pointer. */
4050 if (gnu_size)
4052 gnu_type
4053 = make_type_from_size (gnu_type, gnu_size,
4054 Has_Biased_Representation (gnat_entity));
4056 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4057 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4058 gnu_size = 0;
4061 /* If the alignment hasn't already been processed and this is
4062 not an unconstrained array, see if an alignment is specified.
4063 If not, we pick a default alignment for atomic objects. */
4064 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4066 else if (Known_Alignment (gnat_entity))
4067 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4068 TYPE_ALIGN (gnu_type));
4069 else if (Is_Atomic (gnat_entity) && !gnu_size
4070 && host_integerp (TYPE_SIZE (gnu_type), 1)
4071 && integer_pow2p (TYPE_SIZE (gnu_type)))
4072 align = MIN (BIGGEST_ALIGNMENT,
4073 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4074 else if (Is_Atomic (gnat_entity) && gnu_size
4075 && host_integerp (gnu_size, 1)
4076 && integer_pow2p (gnu_size))
4077 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4079 /* See if we need to pad the type. If we did, and made a record,
4080 the name of the new type may be changed. So get it back for
4081 us when we make the new TYPE_DECL below. */
4082 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
4083 true, definition, false);
4084 if (TREE_CODE (gnu_type) == RECORD_TYPE
4085 && TYPE_IS_PADDING_P (gnu_type))
4087 gnu_entity_id = TYPE_NAME (gnu_type);
4088 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4089 gnu_entity_id = DECL_NAME (gnu_entity_id);
4092 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4094 /* If we are at global level, GCC will have applied variable_size to
4095 the type, but that won't have done anything. So, if it's not
4096 a constant or self-referential, call elaborate_expression_1 to
4097 make a variable for the size rather than calculating it each time.
4098 Handle both the RM size and the actual size. */
4099 if (global_bindings_p ()
4100 && TYPE_SIZE (gnu_type)
4101 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4102 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4104 if (TREE_CODE (gnu_type) == RECORD_TYPE
4105 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4106 TYPE_SIZE (gnu_type), 0))
4108 TYPE_SIZE (gnu_type)
4109 = elaborate_expression_1 (gnat_entity, gnat_entity,
4110 TYPE_SIZE (gnu_type),
4111 get_identifier ("SIZE"),
4112 definition, 0);
4113 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4115 else
4117 TYPE_SIZE (gnu_type)
4118 = elaborate_expression_1 (gnat_entity, gnat_entity,
4119 TYPE_SIZE (gnu_type),
4120 get_identifier ("SIZE"),
4121 definition, 0);
4123 /* ??? For now, store the size as a multiple of the alignment
4124 in bytes so that we can see the alignment from the tree. */
4125 TYPE_SIZE_UNIT (gnu_type)
4126 = build_binary_op
4127 (MULT_EXPR, sizetype,
4128 elaborate_expression_1
4129 (gnat_entity, gnat_entity,
4130 build_binary_op (EXACT_DIV_EXPR, sizetype,
4131 TYPE_SIZE_UNIT (gnu_type),
4132 size_int (TYPE_ALIGN (gnu_type)
4133 / BITS_PER_UNIT)),
4134 get_identifier ("SIZE_A_UNIT"),
4135 definition, 0),
4136 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4138 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4139 SET_TYPE_ADA_SIZE
4140 (gnu_type,
4141 elaborate_expression_1 (gnat_entity,
4142 gnat_entity,
4143 TYPE_ADA_SIZE (gnu_type),
4144 get_identifier ("RM_SIZE"),
4145 definition, 0));
4149 /* If this is a record type or subtype, call elaborate_expression_1 on
4150 any field position. Do this for both global and local types.
4151 Skip any fields that we haven't made trees for to avoid problems with
4152 class wide types. */
4153 if (IN (kind, Record_Kind))
4154 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4155 gnat_temp = Next_Entity (gnat_temp))
4156 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4158 tree gnu_field = get_gnu_tree (gnat_temp);
4160 /* ??? Unfortunately, GCC needs to be able to prove the
4161 alignment of this offset and if it's a variable, it can't.
4162 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4163 right now, we have to put in an explicit multiply and
4164 divide by that value. */
4165 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4166 DECL_FIELD_OFFSET (gnu_field)
4167 = build_binary_op
4168 (MULT_EXPR, sizetype,
4169 elaborate_expression_1
4170 (gnat_temp, gnat_temp,
4171 build_binary_op (EXACT_DIV_EXPR, sizetype,
4172 DECL_FIELD_OFFSET (gnu_field),
4173 size_int (DECL_OFFSET_ALIGN (gnu_field)
4174 / BITS_PER_UNIT)),
4175 get_identifier ("OFFSET"),
4176 definition, 0),
4177 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4180 gnu_type = build_qualified_type (gnu_type,
4181 (TYPE_QUALS (gnu_type)
4182 | (TYPE_QUAL_VOLATILE
4183 * Treat_As_Volatile (gnat_entity))));
4185 if (Is_Atomic (gnat_entity))
4186 check_ok_for_atomic (gnu_type, gnat_entity, false);
4188 if (Known_Alignment (gnat_entity))
4189 TYPE_USER_ALIGN (gnu_type) = 1;
4191 if (!gnu_decl)
4192 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4193 !Comes_From_Source (gnat_entity),
4194 debug_info_p, gnat_entity);
4195 else
4196 TREE_TYPE (gnu_decl) = gnu_type;
4199 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4201 gnu_type = TREE_TYPE (gnu_decl);
4203 /* Back-annotate the Alignment of the type if not already in the
4204 tree. Likewise for sizes. */
4205 if (Unknown_Alignment (gnat_entity))
4206 Set_Alignment (gnat_entity,
4207 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4209 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4211 /* If the size is self-referential, we annotate the maximum
4212 value of that size. */
4213 tree gnu_size = TYPE_SIZE (gnu_type);
4215 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4216 gnu_size = max_size (gnu_size, true);
4218 Set_Esize (gnat_entity, annotate_value (gnu_size));
4220 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4222 /* In this mode the tag and the parent components are not
4223 generated by the front-end, so the sizes must be adjusted
4224 explicitly now. */
4226 int size_offset;
4227 int new_size;
4229 if (Is_Derived_Type (gnat_entity))
4231 size_offset
4232 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4233 Set_Alignment (gnat_entity,
4234 Alignment (Etype (Base_Type (gnat_entity))));
4236 else
4237 size_offset = POINTER_SIZE;
4239 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4240 Set_Esize (gnat_entity,
4241 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4242 / POINTER_SIZE) * POINTER_SIZE));
4243 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4247 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4248 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4251 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4252 DECL_ARTIFICIAL (gnu_decl) = 1;
4254 if (!debug_info_p && DECL_P (gnu_decl)
4255 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4256 && No (Renamed_Object (gnat_entity)))
4257 DECL_IGNORED_P (gnu_decl) = 1;
4259 /* If we haven't already, associate the ..._DECL node that we just made with
4260 the input GNAT entity node. */
4261 if (!saved)
4262 save_gnu_tree (gnat_entity, gnu_decl, false);
4264 /* If this is an enumeral or floating-point type, we were not able to set
4265 the bounds since they refer to the type. These bounds are always static.
4267 For enumeration types, also write debugging information and declare the
4268 enumeration literal table, if needed. */
4270 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4271 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4273 tree gnu_scalar_type = gnu_type;
4275 /* If this is a padded type, we need to use the underlying type. */
4276 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4277 && TYPE_IS_PADDING_P (gnu_scalar_type))
4278 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4280 /* If this is a floating point type and we haven't set a floating
4281 point type yet, use this in the evaluation of the bounds. */
4282 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4283 longest_float_type_node = gnu_type;
4285 TYPE_MIN_VALUE (gnu_scalar_type)
4286 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4287 TYPE_MAX_VALUE (gnu_scalar_type)
4288 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4290 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4292 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4294 /* Since this has both a typedef and a tag, avoid outputting
4295 the name twice. */
4296 DECL_ARTIFICIAL (gnu_decl) = 1;
4297 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4301 /* If we deferred processing of incomplete types, re-enable it. If there
4302 were no other disables and we have some to process, do so. */
4303 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4305 struct incomplete *incp = defer_incomplete_list;
4306 struct incomplete *next;
4308 defer_incomplete_list = NULL;
4309 for (; incp; incp = next)
4311 next = incp->next;
4313 if (incp->old_type)
4314 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4315 gnat_to_gnu_type (incp->full_type));
4316 free (incp);
4320 /* If we are not defining this type, see if it's in the incomplete list.
4321 If so, handle that list entry now. */
4322 else if (!definition)
4324 struct incomplete *incp;
4326 for (incp = defer_incomplete_list; incp; incp = incp->next)
4327 if (incp->old_type && incp->full_type == gnat_entity)
4329 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4330 TREE_TYPE (gnu_decl));
4331 incp->old_type = NULL_TREE;
4335 /* If there are no incomplete types and we have deferred emission
4336 of debug information, check whether we have finished defining
4337 all nested records.
4338 If so, handle the list now. */
4340 if (debug_deferred)
4341 defer_debug_level--;
4343 if (defer_debug_incomplete_list
4344 && !defer_incomplete_level
4345 && !defer_debug_level)
4347 tree c, n;
4349 defer_debug_incomplete_list = nreverse (defer_debug_incomplete_list);
4351 for (c = defer_debug_incomplete_list; c; c = n)
4353 n = TREE_CHAIN (c);
4354 write_record_type_debug_info (TREE_VALUE (c));
4357 defer_debug_incomplete_list = 0;
4360 if (this_global)
4361 force_global--;
4363 if (Is_Packed_Array_Type (gnat_entity)
4364 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4365 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4366 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4367 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4369 return gnu_decl;
4372 /* Similar, but if the returned value is a COMPONENT_REF, return the
4373 FIELD_DECL. */
4375 tree
4376 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4378 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4380 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4381 gnu_field = TREE_OPERAND (gnu_field, 1);
4383 return gnu_field;
4386 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
4388 static
4389 bool same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
4391 while (Present (Corresponding_Discriminant (discr1)))
4392 discr1 = Corresponding_Discriminant (discr1);
4394 while (Present (Corresponding_Discriminant (discr2)))
4395 discr2 = Corresponding_Discriminant (discr2);
4397 return
4398 Original_Record_Component (discr1) == Original_Record_Component (discr2);
4401 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4402 be elaborated at the point of its definition, but do nothing else. */
4404 void
4405 elaborate_entity (Entity_Id gnat_entity)
4407 switch (Ekind (gnat_entity))
4409 case E_Signed_Integer_Subtype:
4410 case E_Modular_Integer_Subtype:
4411 case E_Enumeration_Subtype:
4412 case E_Ordinary_Fixed_Point_Subtype:
4413 case E_Decimal_Fixed_Point_Subtype:
4414 case E_Floating_Point_Subtype:
4416 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4417 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4419 /* ??? Tests for avoiding static constraint error expression
4420 is needed until the front stops generating bogus conversions
4421 on bounds of real types. */
4423 if (!Raises_Constraint_Error (gnat_lb))
4424 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4425 1, 0, Needs_Debug_Info (gnat_entity));
4426 if (!Raises_Constraint_Error (gnat_hb))
4427 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4428 1, 0, Needs_Debug_Info (gnat_entity));
4429 break;
4432 case E_Record_Type:
4434 Node_Id full_definition = Declaration_Node (gnat_entity);
4435 Node_Id record_definition = Type_Definition (full_definition);
4437 /* If this is a record extension, go a level further to find the
4438 record definition. */
4439 if (Nkind (record_definition) == N_Derived_Type_Definition)
4440 record_definition = Record_Extension_Part (record_definition);
4442 break;
4444 case E_Record_Subtype:
4445 case E_Private_Subtype:
4446 case E_Limited_Private_Subtype:
4447 case E_Record_Subtype_With_Private:
4448 if (Is_Constrained (gnat_entity)
4449 && Has_Discriminants (Base_Type (gnat_entity))
4450 && Present (Discriminant_Constraint (gnat_entity)))
4452 Node_Id gnat_discriminant_expr;
4453 Entity_Id gnat_field;
4455 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4456 gnat_discriminant_expr
4457 = First_Elmt (Discriminant_Constraint (gnat_entity));
4458 Present (gnat_field);
4459 gnat_field = Next_Discriminant (gnat_field),
4460 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4461 /* ??? For now, ignore access discriminants. */
4462 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4463 elaborate_expression (Node (gnat_discriminant_expr),
4464 gnat_entity,
4465 get_entity_name (gnat_field), 1, 0, 0);
4467 break;
4472 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4473 any entities on its entity chain similarly. */
4475 void
4476 mark_out_of_scope (Entity_Id gnat_entity)
4478 Entity_Id gnat_sub_entity;
4479 unsigned int kind = Ekind (gnat_entity);
4481 /* If this has an entity list, process all in the list. */
4482 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4483 || IN (kind, Private_Kind)
4484 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4485 || kind == E_Function || kind == E_Generic_Function
4486 || kind == E_Generic_Package || kind == E_Generic_Procedure
4487 || kind == E_Loop || kind == E_Operator || kind == E_Package
4488 || kind == E_Package_Body || kind == E_Procedure
4489 || kind == E_Record_Type || kind == E_Record_Subtype
4490 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4491 for (gnat_sub_entity = First_Entity (gnat_entity);
4492 Present (gnat_sub_entity);
4493 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4494 if (Scope (gnat_sub_entity) == gnat_entity
4495 && gnat_sub_entity != gnat_entity)
4496 mark_out_of_scope (gnat_sub_entity);
4498 /* Now clear this if it has been defined, but only do so if it isn't
4499 a subprogram or parameter. We could refine this, but it isn't
4500 worth it. If this is statically allocated, it is supposed to
4501 hang around out of cope. */
4502 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4503 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4505 save_gnu_tree (gnat_entity, NULL_TREE, true);
4506 save_gnu_tree (gnat_entity, error_mark_node, true);
4510 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4511 is a multi-dimensional array type, do this recursively. */
4513 static void
4514 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4516 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4517 of a one-dimensional array, since the padding has the same alias set
4518 as the field type, but if it's a multi-dimensional array, we need to
4519 see the inner types. */
4520 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4521 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4522 || TYPE_IS_PADDING_P (gnu_old_type)))
4523 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4525 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4526 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4527 so we need to go down to what does. */
4528 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4529 gnu_old_type
4530 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4532 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4533 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4534 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4535 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4537 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4538 record_component_aliases (gnu_new_type);
4541 /* Return a TREE_LIST describing the substitutions needed to reflect
4542 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4543 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4544 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4545 gives the tree for the discriminant and TREE_VALUES is the replacement
4546 value. They are in the form of operands to substitute_in_expr.
4547 DEFINITION is as in gnat_to_gnu_entity. */
4549 static tree
4550 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4551 tree gnu_list, bool definition)
4553 Entity_Id gnat_discrim;
4554 Node_Id gnat_value;
4556 if (No (gnat_type))
4557 gnat_type = Implementation_Base_Type (gnat_subtype);
4559 if (Has_Discriminants (gnat_type))
4560 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4561 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4562 Present (gnat_discrim);
4563 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4564 gnat_value = Next_Elmt (gnat_value))
4565 /* Ignore access discriminants. */
4566 if (!Is_Access_Type (Etype (Node (gnat_value))))
4567 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4568 elaborate_expression
4569 (Node (gnat_value), gnat_subtype,
4570 get_entity_name (gnat_discrim), definition,
4571 1, 0),
4572 gnu_list);
4574 return gnu_list;
4577 /* Return true if the size represented by GNU_SIZE can be handled by an
4578 allocation. If STATIC_P is true, consider only what can be done with a
4579 static allocation. */
4581 static bool
4582 allocatable_size_p (tree gnu_size, bool static_p)
4584 HOST_WIDE_INT our_size;
4586 /* If this is not a static allocation, the only case we want to forbid
4587 is an overflowing size. That will be converted into a raise a
4588 Storage_Error. */
4589 if (!static_p)
4590 return !(TREE_CODE (gnu_size) == INTEGER_CST
4591 && TREE_OVERFLOW (gnu_size));
4593 /* Otherwise, we need to deal with both variable sizes and constant
4594 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4595 since assemblers may not like very large sizes. */
4596 if (!host_integerp (gnu_size, 1))
4597 return false;
4599 our_size = tree_low_cst (gnu_size, 1);
4600 return (int) our_size == our_size;
4603 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4605 static void
4606 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4608 Node_Id gnat_temp;
4610 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4611 gnat_temp = Next_Rep_Item (gnat_temp))
4612 if (Nkind (gnat_temp) == N_Pragma)
4614 struct attrib *attr;
4615 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4616 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4617 enum attr_type etype;
4619 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4620 && Present (Next (First (gnat_assoc)))
4621 && (Nkind (Expression (Next (First (gnat_assoc))))
4622 == N_String_Literal))
4624 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4625 (gnat_to_gnu
4626 (Expression (Next
4627 (First (gnat_assoc))))));
4628 if (Present (Next (Next (First (gnat_assoc))))
4629 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4630 == N_String_Literal))
4631 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4632 (gnat_to_gnu
4633 (Expression
4634 (Next (Next
4635 (First (gnat_assoc)))))));
4638 switch (Get_Pragma_Id (Chars (gnat_temp)))
4640 case Pragma_Machine_Attribute:
4641 etype = ATTR_MACHINE_ATTRIBUTE;
4642 break;
4644 case Pragma_Linker_Alias:
4645 etype = ATTR_LINK_ALIAS;
4646 break;
4648 case Pragma_Linker_Section:
4649 etype = ATTR_LINK_SECTION;
4650 break;
4652 case Pragma_Linker_Constructor:
4653 etype = ATTR_LINK_CONSTRUCTOR;
4654 break;
4656 case Pragma_Linker_Destructor:
4657 etype = ATTR_LINK_DESTRUCTOR;
4658 break;
4660 case Pragma_Weak_External:
4661 etype = ATTR_WEAK_EXTERNAL;
4662 break;
4664 default:
4665 continue;
4668 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4669 attr->next = *attr_list;
4670 attr->type = etype;
4671 attr->name = gnu_arg0;
4673 /* If we have an argument specified together with an attribute name,
4674 make it a single TREE_VALUE entry in a list of arguments, as GCC
4675 expects it. */
4676 if (gnu_arg1 != NULL_TREE)
4677 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4678 else
4679 attr->args = NULL_TREE;
4681 attr->error_point
4682 = Present (Next (First (gnat_assoc)))
4683 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4684 *attr_list = attr;
4688 /* Get the unpadded version of a GNAT type. */
4690 tree
4691 get_unpadded_type (Entity_Id gnat_entity)
4693 tree type = gnat_to_gnu_type (gnat_entity);
4695 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4696 type = TREE_TYPE (TYPE_FIELDS (type));
4698 return type;
4701 /* Called when we need to protect a variable object using a save_expr. */
4703 tree
4704 maybe_variable (tree gnu_operand)
4706 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4707 || TREE_CODE (gnu_operand) == SAVE_EXPR
4708 || TREE_CODE (gnu_operand) == NULL_EXPR)
4709 return gnu_operand;
4711 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4713 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4714 TREE_TYPE (gnu_operand),
4715 variable_size (TREE_OPERAND (gnu_operand, 0)));
4717 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4718 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4719 return gnu_result;
4721 else
4722 return variable_size (gnu_operand);
4725 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4726 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4727 return the GCC tree to use for that expression. GNU_NAME is the
4728 qualification to use if an external name is appropriate and DEFINITION is
4729 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4730 we need a result. Otherwise, we are just elaborating this for
4731 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4732 purposes even if it isn't needed for code generation. */
4734 static tree
4735 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4736 tree gnu_name, bool definition, bool need_value,
4737 bool need_debug)
4739 tree gnu_expr;
4741 /* If we already elaborated this expression (e.g., it was involved
4742 in the definition of a private type), use the old value. */
4743 if (present_gnu_tree (gnat_expr))
4744 return get_gnu_tree (gnat_expr);
4746 /* If we don't need a value and this is static or a discriminant, we
4747 don't need to do anything. */
4748 else if (!need_value
4749 && (Is_OK_Static_Expression (gnat_expr)
4750 || (Nkind (gnat_expr) == N_Identifier
4751 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4752 return 0;
4754 /* Otherwise, convert this tree to its GCC equivalent. */
4755 gnu_expr
4756 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4757 gnu_name, definition, need_debug);
4759 /* Save the expression in case we try to elaborate this entity again. Since
4760 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4761 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4762 save_gnu_tree (gnat_expr, gnu_expr, true);
4764 return need_value ? gnu_expr : error_mark_node;
4767 /* Similar, but take a GNU expression. */
4769 static tree
4770 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4771 tree gnu_expr, tree gnu_name, bool definition,
4772 bool need_debug)
4774 tree gnu_decl = NULL_TREE;
4775 /* Strip any conversions to see if the expression is a readonly variable.
4776 ??? This really should remain readonly, but we have to think about
4777 the typing of the tree here. */
4778 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4779 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4780 bool expr_variable;
4782 /* In most cases, we won't see a naked FIELD_DECL here because a
4783 discriminant reference will have been replaced with a COMPONENT_REF
4784 when the type is being elaborated. However, there are some cases
4785 involving child types where we will. So convert it to a COMPONENT_REF
4786 here. We have to hope it will be at the highest level of the
4787 expression in these cases. */
4788 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4789 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4790 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4791 gnu_expr, NULL_TREE);
4793 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4794 that is a constant, make a variable that is initialized to contain the
4795 bound when the package containing the definition is elaborated. If
4796 this entity is defined at top level and a bound or discriminant value
4797 isn't a constant or a reference to a discriminant, replace the bound
4798 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4799 rely here on the fact that an expression cannot contain both the
4800 discriminant and some other variable. */
4802 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4803 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4804 && (TREE_READONLY (gnu_inner_expr)
4805 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
4806 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4808 /* If this is a static expression or contains a discriminant, we don't
4809 need the variable for debugging (and can't elaborate anyway if a
4810 discriminant). */
4811 if (need_debug
4812 && (Is_OK_Static_Expression (gnat_expr)
4813 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4814 need_debug = false;
4816 /* Now create the variable if we need it. */
4817 if (need_debug || (expr_variable && expr_global))
4818 gnu_decl
4819 = create_var_decl (create_concat_name (gnat_entity,
4820 IDENTIFIER_POINTER (gnu_name)),
4821 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
4822 !need_debug, Is_Public (gnat_entity),
4823 !definition, false, NULL, gnat_entity);
4825 /* We only need to use this variable if we are in global context since GCC
4826 can do the right thing in the local case. */
4827 if (expr_global && expr_variable)
4828 return gnu_decl;
4829 else if (!expr_variable)
4830 return gnu_expr;
4831 else
4832 return maybe_variable (gnu_expr);
4835 /* Create a record type that contains a field of TYPE with a starting bit
4836 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4838 tree
4839 make_aligning_type (tree type, int align, tree size)
4841 tree record_type = make_node (RECORD_TYPE);
4842 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4843 tree size_addr_place = convert (sizetype,
4844 build_unary_op (ADDR_EXPR, NULL_TREE,
4845 place));
4846 tree name = TYPE_NAME (type);
4847 tree pos, field;
4849 if (TREE_CODE (name) == TYPE_DECL)
4850 name = DECL_NAME (name);
4852 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4854 /* The bit position is obtained by "and"ing the alignment minus 1
4855 with the two's complement of the address and multiplying
4856 by the number of bits per unit. Do all this in sizetype. */
4857 pos = size_binop (MULT_EXPR,
4858 convert (bitsizetype,
4859 size_binop (BIT_AND_EXPR,
4860 size_diffop (size_zero_node,
4861 size_addr_place),
4862 ssize_int ((align / BITS_PER_UNIT)
4863 - 1))),
4864 bitsize_unit_node);
4866 /* Create the field, with -1 as the 'addressable' indication to avoid the
4867 creation of a bitfield. We don't need one, it would have damaging
4868 consequences on the alignment computation, and create_field_decl would
4869 make one without this special argument, for instance because of the
4870 complex position expression. */
4871 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4872 pos, -1);
4874 finish_record_type (record_type, field, true, false);
4875 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4876 TYPE_SIZE (record_type)
4877 = size_binop (PLUS_EXPR,
4878 size_binop (MULT_EXPR, convert (bitsizetype, size),
4879 bitsize_unit_node),
4880 bitsize_int (align));
4881 TYPE_SIZE_UNIT (record_type)
4882 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4883 copy_alias_set (record_type, type);
4884 return record_type;
4887 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4888 being used as the field type of a packed record. See if we can rewrite it
4889 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4890 return the new type. If not, return the original type. */
4892 static tree
4893 make_packable_type (tree type)
4895 tree new_type = make_node (TREE_CODE (type));
4896 tree field_list = NULL_TREE;
4897 tree old_field;
4899 /* Copy the name and flags from the old type to that of the new and set
4900 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4901 also copy the size. */
4902 TYPE_NAME (new_type) = TYPE_NAME (type);
4903 TYPE_JUSTIFIED_MODULAR_P (new_type)
4904 = TYPE_JUSTIFIED_MODULAR_P (type);
4905 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4907 if (TREE_CODE (type) == RECORD_TYPE)
4908 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4909 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4911 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4912 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4915 TYPE_ALIGN (new_type)
4916 = ((HOST_WIDE_INT) 1
4917 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4919 /* Now copy the fields, keeping the position and size. */
4920 for (old_field = TYPE_FIELDS (type); old_field;
4921 old_field = TREE_CHAIN (old_field))
4923 tree new_field_type = TREE_TYPE (old_field);
4924 tree new_field;
4926 if (TYPE_MODE (new_field_type) == BLKmode
4927 && (TREE_CODE (new_field_type) == RECORD_TYPE
4928 || TREE_CODE (new_field_type) == UNION_TYPE
4929 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4930 && host_integerp (TYPE_SIZE (new_field_type), 1))
4931 new_field_type = make_packable_type (new_field_type);
4933 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4934 new_type, TYPE_PACKED (type),
4935 DECL_SIZE (old_field),
4936 bit_position (old_field),
4937 !DECL_NONADDRESSABLE_P (old_field));
4939 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4940 SET_DECL_ORIGINAL_FIELD
4941 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4942 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4944 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4945 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4947 TREE_CHAIN (new_field) = field_list;
4948 field_list = new_field;
4951 finish_record_type (new_type, nreverse (field_list), true, true);
4952 copy_alias_set (new_type, type);
4953 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4956 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4957 if needed. We have already verified that SIZE and TYPE are large enough.
4959 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4960 to issue a warning.
4962 IS_USER_TYPE is true if we must be sure we complete the original type.
4964 DEFINITION is true if this type is being defined.
4966 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4967 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4968 type. */
4970 tree
4971 maybe_pad_type (tree type, tree size, unsigned int align,
4972 Entity_Id gnat_entity, const char *name_trailer,
4973 bool is_user_type, bool definition, bool same_rm_size)
4975 tree orig_size = TYPE_SIZE (type);
4976 tree record;
4977 tree field;
4979 /* If TYPE is a padded type, see if it agrees with any size and alignment
4980 we were given. If so, return the original type. Otherwise, strip
4981 off the padding, since we will either be returning the inner type
4982 or repadding it. If no size or alignment is specified, use that of
4983 the original padded type. */
4985 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4987 if ((!size
4988 || operand_equal_p (round_up (size,
4989 MAX (align, TYPE_ALIGN (type))),
4990 round_up (TYPE_SIZE (type),
4991 MAX (align, TYPE_ALIGN (type))),
4993 && (align == 0 || align == TYPE_ALIGN (type)))
4994 return type;
4996 if (!size)
4997 size = TYPE_SIZE (type);
4998 if (align == 0)
4999 align = TYPE_ALIGN (type);
5001 type = TREE_TYPE (TYPE_FIELDS (type));
5002 orig_size = TYPE_SIZE (type);
5005 /* If the size is either not being changed or is being made smaller (which
5006 is not done here (and is only valid for bitfields anyway), show the size
5007 isn't changing. Likewise, clear the alignment if it isn't being
5008 changed. Then return if we aren't doing anything. */
5010 if (size
5011 && (operand_equal_p (size, orig_size, 0)
5012 || (TREE_CODE (orig_size) == INTEGER_CST
5013 && tree_int_cst_lt (size, orig_size))))
5014 size = NULL_TREE;
5016 if (align == TYPE_ALIGN (type))
5017 align = 0;
5019 if (align == 0 && !size)
5020 return type;
5022 /* We used to modify the record in place in some cases, but that could
5023 generate incorrect debugging information. So make a new record
5024 type and name. */
5025 record = make_node (RECORD_TYPE);
5027 if (Present (gnat_entity))
5028 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5030 /* If we were making a type, complete the original type and give it a
5031 name. */
5032 if (is_user_type)
5033 create_type_decl (get_entity_name (gnat_entity), type,
5034 NULL, !Comes_From_Source (gnat_entity),
5035 !(TYPE_NAME (type)
5036 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5037 && DECL_IGNORED_P (TYPE_NAME (type))),
5038 gnat_entity);
5040 /* If we are changing the alignment and the input type is a record with
5041 BLKmode and a small constant size, try to make a form that has an
5042 integral mode. That might allow this record to have an integral mode,
5043 which will be much more efficient. There is no point in doing this if a
5044 size is specified unless it is also smaller than the biggest alignment
5045 and it is incorrect to do this if the size of the original type is not a
5046 multiple of the alignment. */
5047 if (align != 0
5048 && TREE_CODE (type) == RECORD_TYPE
5049 && TYPE_MODE (type) == BLKmode
5050 && host_integerp (orig_size, 1)
5051 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
5052 && (!size
5053 || (TREE_CODE (size) == INTEGER_CST
5054 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
5055 && tree_low_cst (orig_size, 1) % align == 0)
5056 type = make_packable_type (type);
5058 field = create_field_decl (get_identifier ("F"), type, record, 0,
5059 NULL_TREE, bitsize_zero_node, 1);
5061 DECL_INTERNAL_P (field) = 1;
5062 TYPE_SIZE (record) = size ? size : orig_size;
5063 TYPE_SIZE_UNIT (record)
5064 = (size ? convert (sizetype,
5065 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
5066 : TYPE_SIZE_UNIT (type));
5068 TYPE_ALIGN (record) = align;
5069 TYPE_IS_PADDING_P (record) = 1;
5070 TYPE_VOLATILE (record)
5071 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5072 finish_record_type (record, field, true, false);
5074 /* Keep the RM_Size of the padded record as that of the old record
5075 if requested. */
5076 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
5078 /* Unless debugging information isn't being written for the input type,
5079 write a record that shows what we are a subtype of and also make a
5080 variable that indicates our size, if variable. */
5081 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
5082 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
5083 || !DECL_IGNORED_P (TYPE_NAME (type))))
5085 tree marker = make_node (RECORD_TYPE);
5086 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
5087 ? DECL_NAME (TYPE_NAME (record))
5088 : TYPE_NAME (record));
5089 tree orig_name = TYPE_NAME (type);
5091 if (TREE_CODE (orig_name) == TYPE_DECL)
5092 orig_name = DECL_NAME (orig_name);
5094 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5095 finish_record_type (marker,
5096 create_field_decl (orig_name, integer_type_node,
5097 marker, 0, NULL_TREE, NULL_TREE,
5099 false, false);
5101 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5102 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5103 bitsizetype, TYPE_SIZE (record), false, false, false,
5104 false, NULL, gnat_entity);
5107 type = record;
5109 if (CONTAINS_PLACEHOLDER_P (orig_size))
5110 orig_size = max_size (orig_size, true);
5112 /* If the size was widened explicitly, maybe give a warning. */
5113 if (size && Present (gnat_entity)
5114 && !operand_equal_p (size, orig_size, 0)
5115 && !(TREE_CODE (size) == INTEGER_CST
5116 && TREE_CODE (orig_size) == INTEGER_CST
5117 && tree_int_cst_lt (size, orig_size)))
5119 Node_Id gnat_error_node = Empty;
5121 if (Is_Packed_Array_Type (gnat_entity))
5122 gnat_entity = Associated_Node_For_Itype (gnat_entity);
5124 if ((Ekind (gnat_entity) == E_Component
5125 || Ekind (gnat_entity) == E_Discriminant)
5126 && Present (Component_Clause (gnat_entity)))
5127 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5128 else if (Present (Size_Clause (gnat_entity)))
5129 gnat_error_node = Expression (Size_Clause (gnat_entity));
5131 /* Generate message only for entities that come from source, since
5132 if we have an entity created by expansion, the message will be
5133 generated for some other corresponding source entity. */
5134 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5135 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5136 gnat_entity,
5137 size_diffop (size, orig_size));
5139 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5140 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5141 gnat_entity, gnat_entity,
5142 size_diffop (size, orig_size));
5145 return type;
5148 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5149 the value passed against the list of choices. */
5151 tree
5152 choices_to_gnu (tree operand, Node_Id choices)
5154 Node_Id choice;
5155 Node_Id gnat_temp;
5156 tree result = integer_zero_node;
5157 tree this_test, low = 0, high = 0, single = 0;
5159 for (choice = First (choices); Present (choice); choice = Next (choice))
5161 switch (Nkind (choice))
5163 case N_Range:
5164 low = gnat_to_gnu (Low_Bound (choice));
5165 high = gnat_to_gnu (High_Bound (choice));
5167 /* There's no good type to use here, so we might as well use
5168 integer_type_node. */
5169 this_test
5170 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5171 build_binary_op (GE_EXPR, integer_type_node,
5172 operand, low),
5173 build_binary_op (LE_EXPR, integer_type_node,
5174 operand, high));
5176 break;
5178 case N_Subtype_Indication:
5179 gnat_temp = Range_Expression (Constraint (choice));
5180 low = gnat_to_gnu (Low_Bound (gnat_temp));
5181 high = gnat_to_gnu (High_Bound (gnat_temp));
5183 this_test
5184 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5185 build_binary_op (GE_EXPR, integer_type_node,
5186 operand, low),
5187 build_binary_op (LE_EXPR, integer_type_node,
5188 operand, high));
5189 break;
5191 case N_Identifier:
5192 case N_Expanded_Name:
5193 /* This represents either a subtype range, an enumeration
5194 literal, or a constant Ekind says which. If an enumeration
5195 literal or constant, fall through to the next case. */
5196 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5197 && Ekind (Entity (choice)) != E_Constant)
5199 tree type = gnat_to_gnu_type (Entity (choice));
5201 low = TYPE_MIN_VALUE (type);
5202 high = TYPE_MAX_VALUE (type);
5204 this_test
5205 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5206 build_binary_op (GE_EXPR, integer_type_node,
5207 operand, low),
5208 build_binary_op (LE_EXPR, integer_type_node,
5209 operand, high));
5210 break;
5212 /* ... fall through ... */
5213 case N_Character_Literal:
5214 case N_Integer_Literal:
5215 single = gnat_to_gnu (choice);
5216 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5217 single);
5218 break;
5220 case N_Others_Choice:
5221 this_test = integer_one_node;
5222 break;
5224 default:
5225 gcc_unreachable ();
5228 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5229 result, this_test);
5232 return result;
5235 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5236 placed in GNU_RECORD_TYPE.
5238 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5239 record has a Component_Alignment of Storage_Unit.
5241 DEFINITION is true if this field is for a record being defined. */
5243 static tree
5244 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5245 bool definition)
5247 tree gnu_field_id = get_entity_name (gnat_field);
5248 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5249 tree gnu_pos = 0;
5250 tree gnu_size = 0;
5251 tree gnu_field;
5252 bool needs_strict_alignment
5253 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5254 || Treat_As_Volatile (gnat_field));
5256 /* If this field requires strict alignment or contains an item of
5257 variable sized, pretend it isn't packed. */
5258 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5259 packed = 0;
5261 /* For packed records, this is one of the few occasions on which we use
5262 the official RM size for discrete or fixed-point components, instead
5263 of the normal GNAT size stored in Esize. See description in Einfo:
5264 "Handling of Type'Size Values" for further details. */
5266 if (packed == 1)
5267 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5268 gnat_field, FIELD_DECL, false, true);
5270 if (Known_Static_Esize (gnat_field))
5271 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5272 gnat_field, FIELD_DECL, false, true);
5274 /* If we have a specified size that's smaller than that of the field type,
5275 or a position is specified, and the field type is also a record that's
5276 BLKmode and with a small constant size, see if we can get an integral
5277 mode form of the type when appropriate. If we can, show a size was
5278 specified for the field if there wasn't one already, so we know to make
5279 this a bitfield and avoid making things wider.
5281 Doing this is first useful if the record is packed because we can then
5282 place the field at a non-byte-aligned position and so achieve tighter
5283 packing.
5285 This is in addition *required* if the field shares a byte with another
5286 field and the front-end lets the back-end handle the references, because
5287 GCC does not handle BLKmode bitfields properly.
5289 We avoid the transformation if it is not required or potentially useful,
5290 as it might entail an increase of the field's alignment and have ripple
5291 effects on the outer record type. A typical case is a field known to be
5292 byte aligned and not to share a byte with another field.
5294 Besides, we don't even look the possibility of a transformation in cases
5295 known to be in error already, for instance when an invalid size results
5296 from a component clause. */
5298 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5299 && TYPE_MODE (gnu_field_type) == BLKmode
5300 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5301 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5302 && (packed == 1
5303 || (gnu_size
5304 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))
5305 || (Present (Component_Clause (gnat_field)) && gnu_size != 0)))
5307 /* See what the alternate type and size would be. */
5308 tree gnu_packable_type = make_packable_type (gnu_field_type);
5310 bool has_byte_aligned_clause
5311 = Present (Component_Clause (gnat_field))
5312 && (UI_To_Int (Component_Bit_Offset (gnat_field))
5313 % BITS_PER_UNIT == 0);
5315 /* Compute whether we should avoid the substitution. */
5316 int reject =
5317 /* There is no point substituting if there is no change. */
5318 (gnu_packable_type == gnu_field_type
5320 /* ... nor when the field is known to be byte aligned and not to
5321 share a byte with another field. */
5322 (has_byte_aligned_clause
5323 && value_factor_p (gnu_size, BITS_PER_UNIT))
5325 /* The size of an aliased field must be an exact multiple of the
5326 type's alignment, which the substitution might increase. Reject
5327 substitutions that would so invalidate a component clause when the
5328 specified position is byte aligned, as the change would have no
5329 real benefit from the packing standpoint anyway. */
5330 (Is_Aliased (gnat_field)
5331 && has_byte_aligned_clause
5332 && ! value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)))
5335 /* Substitute unless told otherwise. */
5336 if (!reject)
5338 gnu_field_type = gnu_packable_type;
5340 if (gnu_size == 0)
5341 gnu_size = rm_size (gnu_field_type);
5345 /* If we are packing the record and the field is BLKmode, round the
5346 size up to a byte boundary. */
5347 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5348 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5350 if (Present (Component_Clause (gnat_field)))
5352 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5353 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5354 gnat_field, FIELD_DECL, false, true);
5356 /* Ensure the position does not overlap with the parent subtype,
5357 if there is one. */
5358 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5360 tree gnu_parent
5361 = gnat_to_gnu_type (Parent_Subtype
5362 (Underlying_Type (Scope (gnat_field))));
5364 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5365 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5367 post_error_ne_tree
5368 ("offset of& must be beyond parent{, minimum allowed is ^}",
5369 First_Bit (Component_Clause (gnat_field)), gnat_field,
5370 TYPE_SIZE_UNIT (gnu_parent));
5374 /* If this field needs strict alignment, ensure the record is
5375 sufficiently aligned and that that position and size are
5376 consistent with the alignment. */
5377 if (needs_strict_alignment)
5379 tree gnu_rounded_size = round_up (rm_size (gnu_field_type),
5380 TYPE_ALIGN (gnu_field_type));
5382 TYPE_ALIGN (gnu_record_type)
5383 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5385 /* If Atomic, the size must match exactly that of the field. */
5386 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5387 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5389 post_error_ne_tree
5390 ("atomic field& must be natural size of type{ (^)}",
5391 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5392 TYPE_SIZE (gnu_field_type));
5394 gnu_size = NULL_TREE;
5397 /* If Aliased, the size must match exactly the rounded size. We
5398 used to be more accommodating here and accept greater sizes, but
5399 fully supporting this case on big-endian platforms would require
5400 switching to a more involved layout for the field. */
5401 else if (Is_Aliased (gnat_field)
5402 && gnu_size
5403 && ! operand_equal_p (gnu_size, gnu_rounded_size, 0))
5405 post_error_ne_tree
5406 ("size of aliased field& must be ^ bits",
5407 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5408 gnu_rounded_size);
5409 gnu_size = NULL_TREE;
5412 if (!integer_zerop (size_binop
5413 (TRUNC_MOD_EXPR, gnu_pos,
5414 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5416 if (Is_Aliased (gnat_field))
5417 post_error_ne_num
5418 ("position of aliased field& must be multiple of ^ bits",
5419 First_Bit (Component_Clause (gnat_field)), gnat_field,
5420 TYPE_ALIGN (gnu_field_type));
5422 else if (Treat_As_Volatile (gnat_field))
5423 post_error_ne_num
5424 ("position of volatile field& must be multiple of ^ bits",
5425 First_Bit (Component_Clause (gnat_field)), gnat_field,
5426 TYPE_ALIGN (gnu_field_type));
5428 else if (Strict_Alignment (Etype (gnat_field)))
5429 post_error_ne_num
5430 ("position of & with aliased or tagged components not multiple of ^ bits",
5431 First_Bit (Component_Clause (gnat_field)), gnat_field,
5432 TYPE_ALIGN (gnu_field_type));
5433 else
5434 gcc_unreachable ();
5436 gnu_pos = NULL_TREE;
5440 if (Is_Atomic (gnat_field))
5441 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5444 /* If the record has rep clauses and this is the tag field, make a rep
5445 clause for it as well. */
5446 else if (Has_Specified_Layout (Scope (gnat_field))
5447 && Chars (gnat_field) == Name_uTag)
5449 gnu_pos = bitsize_zero_node;
5450 gnu_size = TYPE_SIZE (gnu_field_type);
5453 /* We need to make the size the maximum for the type if it is
5454 self-referential and an unconstrained type. In that case, we can't
5455 pack the field since we can't make a copy to align it. */
5456 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5457 && !gnu_size
5458 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5459 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5461 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5462 packed = 0;
5465 /* If no size is specified (or if there was an error), don't specify a
5466 position. */
5467 if (!gnu_size)
5468 gnu_pos = NULL_TREE;
5469 else
5471 /* If the field's type is justified modular, we would need to remove
5472 the wrapper to (better) meet the layout requirements. However we
5473 can do so only if the field is not aliased to preserve the unique
5474 layout and if the prescribed size is not greater than that of the
5475 packed array to preserve the justification. */
5476 if (!needs_strict_alignment
5477 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5478 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5479 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5480 <= 0)
5481 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5483 gnu_field_type
5484 = make_type_from_size (gnu_field_type, gnu_size,
5485 Has_Biased_Representation (gnat_field));
5486 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5487 "PAD", false, definition, true);
5490 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5491 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5493 /* Now create the decl for the field. */
5494 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5495 packed, gnu_size, gnu_pos,
5496 Is_Aliased (gnat_field));
5497 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5498 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5500 if (Ekind (gnat_field) == E_Discriminant)
5501 DECL_DISCRIMINANT_NUMBER (gnu_field)
5502 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5504 return gnu_field;
5507 /* Return true if TYPE is a type with variable size, a padding type with a
5508 field of variable size or is a record that has a field such a field. */
5510 static bool
5511 is_variable_size (tree type)
5513 tree field;
5515 /* We need not be concerned about this at all if we don't have
5516 strict alignment. */
5517 if (!STRICT_ALIGNMENT)
5518 return false;
5519 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5520 return true;
5521 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5522 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5523 return true;
5524 else if (TREE_CODE (type) != RECORD_TYPE
5525 && TREE_CODE (type) != UNION_TYPE
5526 && TREE_CODE (type) != QUAL_UNION_TYPE)
5527 return false;
5529 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5530 if (is_variable_size (TREE_TYPE (field)))
5531 return true;
5533 return false;
5536 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5537 of GCC trees for fields that are in the record and have already been
5538 processed. When called from gnat_to_gnu_entity during the processing of a
5539 record type definition, the GCC nodes for the discriminants will be on
5540 the chain. The other calls to this function are recursive calls from
5541 itself for the Component_List of a variant and the chain is empty.
5543 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5544 for a record type with "pragma component_alignment (storage_unit)".
5546 DEFINITION is true if we are defining this record.
5548 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5549 with a rep clause is to be added. If it is nonzero, that is all that
5550 should be done with such fields.
5552 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5553 laying out the record. This means the alignment only serves to force fields
5554 to be bitfields, but not require the record to be that aligned. This is
5555 used for variants.
5557 ALL_REP, if true, means a rep clause was found for all the fields. This
5558 simplifies the logic since we know we're not in the mixed case.
5560 DEFER_DEBUG, if true, means that the debugging routines should not be
5561 called when finishing constructing the record type.
5563 UNCHECKED_UNION, if tree, means that we are building a type for a record
5564 with a Pragma Unchecked_Union.
5566 The processing of the component list fills in the chain with all of the
5567 fields of the record and then the record type is finished. */
5569 static void
5570 components_to_record (tree gnu_record_type, Node_Id component_list,
5571 tree gnu_field_list, int packed, bool definition,
5572 tree *p_gnu_rep_list, bool cancel_alignment,
5573 bool all_rep, bool defer_debug, bool unchecked_union)
5575 Node_Id component_decl;
5576 Entity_Id gnat_field;
5577 Node_Id variant_part;
5578 tree gnu_our_rep_list = NULL_TREE;
5579 tree gnu_field, gnu_last;
5580 bool layout_with_rep = false;
5581 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5583 /* For each variable within each component declaration create a GCC field
5584 and add it to the list, skipping any pragmas in the list. */
5586 if (Present (Component_Items (component_list)))
5587 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5588 Present (component_decl);
5589 component_decl = Next_Non_Pragma (component_decl))
5591 gnat_field = Defining_Entity (component_decl);
5593 if (Chars (gnat_field) == Name_uParent)
5594 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5595 else
5597 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5598 packed, definition);
5600 /* If this is the _Tag field, put it before any discriminants,
5601 instead of after them as is the case for all other fields.
5602 Ignore field of void type if only annotating. */
5603 if (Chars (gnat_field) == Name_uTag)
5604 gnu_field_list = chainon (gnu_field_list, gnu_field);
5605 else
5607 TREE_CHAIN (gnu_field) = gnu_field_list;
5608 gnu_field_list = gnu_field;
5612 save_gnu_tree (gnat_field, gnu_field, false);
5615 /* At the end of the component list there may be a variant part. */
5616 variant_part = Variant_Part (component_list);
5618 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5619 mutually exclusive and should go in the same memory. To do this we need
5620 to treat each variant as a record whose elements are created from the
5621 component list for the variant. So here we create the records from the
5622 lists for the variants and put them all into the QUAL_UNION_TYPE.
5623 If this is an Unchecked_Union, we make a UNION_TYPE instead or
5624 use GNU_RECORD_TYPE if there are no fields so far. */
5625 if (Present (variant_part))
5627 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5628 Node_Id variant;
5629 tree gnu_name = TYPE_NAME (gnu_record_type);
5630 tree gnu_var_name
5631 = concat_id_with_name (get_identifier (Get_Name_String
5632 (Chars (Name (variant_part)))),
5633 "XVN");
5634 tree gnu_union_type;
5635 tree gnu_union_name;
5636 tree gnu_union_field;
5637 tree gnu_variant_list = NULL_TREE;
5639 if (TREE_CODE (gnu_name) == TYPE_DECL)
5640 gnu_name = DECL_NAME (gnu_name);
5642 gnu_union_name = concat_id_with_name (gnu_name,
5643 IDENTIFIER_POINTER (gnu_var_name));
5645 if (!gnu_field_list && TREE_CODE (gnu_record_type) == UNION_TYPE)
5646 gnu_union_type = gnu_record_type;
5647 else
5650 gnu_union_type
5651 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
5653 TYPE_NAME (gnu_union_type) = gnu_union_name;
5654 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5657 for (variant = First_Non_Pragma (Variants (variant_part));
5658 Present (variant);
5659 variant = Next_Non_Pragma (variant))
5661 tree gnu_variant_type = make_node (RECORD_TYPE);
5662 tree gnu_inner_name;
5663 tree gnu_qual;
5665 Get_Variant_Encoding (variant);
5666 gnu_inner_name = get_identifier (Name_Buffer);
5667 TYPE_NAME (gnu_variant_type)
5668 = concat_id_with_name (gnu_union_name,
5669 IDENTIFIER_POINTER (gnu_inner_name));
5671 /* Set the alignment of the inner type in case we need to make
5672 inner objects into bitfields, but then clear it out
5673 so the record actually gets only the alignment required. */
5674 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5675 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5677 /* Similarly, if the outer record has a size specified and all fields
5678 have record rep clauses, we can propagate the size into the
5679 variant part. */
5680 if (all_rep_and_size)
5682 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5683 TYPE_SIZE_UNIT (gnu_variant_type)
5684 = TYPE_SIZE_UNIT (gnu_record_type);
5687 /* Create the record for the variant. Note that we defer emitting
5688 debug info for it until after we are sure to actually use it. */
5689 components_to_record (gnu_variant_type, Component_List (variant),
5690 NULL_TREE, packed, definition,
5691 &gnu_our_rep_list, !all_rep_and_size, all_rep,
5692 true, unchecked_union);
5694 gnu_qual = choices_to_gnu (gnu_discriminant,
5695 Discrete_Choices (variant));
5697 Set_Present_Expr (variant, annotate_value (gnu_qual));
5699 /* If this is an Unchecked_Union and we have exactly one field,
5700 use that field here. */
5701 if (unchecked_union && TYPE_FIELDS (gnu_variant_type)
5702 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
5703 gnu_field = TYPE_FIELDS (gnu_variant_type);
5704 else
5706 /* Emit debug info for the record. We used to throw away
5707 empty records but we no longer do that because we need
5708 them to generate complete debug info for the variant;
5709 otherwise, the union type definition will be lacking
5710 the fields associated with these empty variants. */
5711 write_record_type_debug_info (gnu_variant_type);
5713 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5714 gnu_union_type, 0,
5715 (all_rep_and_size
5716 ? TYPE_SIZE (gnu_record_type)
5717 : 0),
5718 (all_rep_and_size
5719 ? bitsize_zero_node : 0),
5722 DECL_INTERNAL_P (gnu_field) = 1;
5724 if (!unchecked_union)
5725 DECL_QUALIFIER (gnu_field) = gnu_qual;
5728 TREE_CHAIN (gnu_field) = gnu_variant_list;
5729 gnu_variant_list = gnu_field;
5732 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5733 if (gnu_variant_list)
5735 if (all_rep_and_size)
5737 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5738 TYPE_SIZE_UNIT (gnu_union_type)
5739 = TYPE_SIZE_UNIT (gnu_record_type);
5742 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5743 all_rep_and_size, false);
5745 /* If GNU_UNION_TYPE is our record type, it means we must have an
5746 Unchecked_Union with no fields. Verify that and, if so, just
5747 return. */
5748 if (gnu_union_type == gnu_record_type)
5750 gcc_assert (!gnu_field_list && unchecked_union);
5751 return;
5754 gnu_union_field
5755 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5756 packed,
5757 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5758 all_rep ? bitsize_zero_node : 0, 0);
5760 DECL_INTERNAL_P (gnu_union_field) = 1;
5761 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5762 gnu_field_list = gnu_union_field;
5766 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5767 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5768 in a separate pass since we want to handle the discriminants but can't
5769 play with them until we've used them in debugging data above.
5771 ??? Note: if we then reorder them, debugging information will be wrong,
5772 but there's nothing that can be done about this at the moment. */
5774 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5776 if (DECL_FIELD_OFFSET (gnu_field))
5778 tree gnu_next = TREE_CHAIN (gnu_field);
5780 if (!gnu_last)
5781 gnu_field_list = gnu_next;
5782 else
5783 TREE_CHAIN (gnu_last) = gnu_next;
5785 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5786 gnu_our_rep_list = gnu_field;
5787 gnu_field = gnu_next;
5789 else
5791 gnu_last = gnu_field;
5792 gnu_field = TREE_CHAIN (gnu_field);
5796 /* If we have any items in our rep'ed field list, it is not the case that all
5797 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5798 set it and ignore the items. */
5799 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5800 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5801 else if (gnu_our_rep_list)
5803 /* Otherwise, sort the fields by bit position and put them into their
5804 own record if we have any fields without rep clauses. */
5805 tree gnu_rep_type
5806 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5807 int len = list_length (gnu_our_rep_list);
5808 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5809 int i;
5811 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5812 gnu_field = TREE_CHAIN (gnu_field), i++)
5813 gnu_arr[i] = gnu_field;
5815 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5817 /* Put the fields in the list in order of increasing position, which
5818 means we start from the end. */
5819 gnu_our_rep_list = NULL_TREE;
5820 for (i = len - 1; i >= 0; i--)
5822 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5823 gnu_our_rep_list = gnu_arr[i];
5824 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5827 if (gnu_field_list)
5829 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5830 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5831 gnu_record_type, 0, 0, 0, 1);
5832 DECL_INTERNAL_P (gnu_field) = 1;
5833 gnu_field_list = chainon (gnu_field_list, gnu_field);
5835 else
5837 layout_with_rep = true;
5838 gnu_field_list = nreverse (gnu_our_rep_list);
5842 if (cancel_alignment)
5843 TYPE_ALIGN (gnu_record_type) = 0;
5845 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5846 layout_with_rep, defer_debug);
5849 /* Called via qsort from the above. Returns -1, 1, depending on the
5850 bit positions and ordinals of the two fields. Use DECL_UID to ensure
5851 a stable sort. */
5853 static int
5854 compare_field_bitpos (const PTR rt1, const PTR rt2)
5856 tree *t1 = (tree *) rt1;
5857 tree *t2 = (tree *) rt2;
5859 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5860 return DECL_UID (*t1) < DECL_UID (*t2) ? -1 : 1;
5861 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5862 return -1;
5863 else
5864 return 1;
5867 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5868 placed into an Esize, Component_Bit_Offset, or Component_Size value
5869 in the GNAT tree. */
5871 static Uint
5872 annotate_value (tree gnu_size)
5874 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5875 TCode tcode;
5876 Node_Ref_Or_Val ops[3], ret;
5877 int i;
5878 int size;
5880 /* See if we've already saved the value for this node. */
5881 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5882 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5884 /* If we do not return inside this switch, TCODE will be set to the
5885 code to use for a Create_Node operand and LEN (set above) will be
5886 the number of recursive calls for us to make. */
5888 switch (TREE_CODE (gnu_size))
5890 case INTEGER_CST:
5891 if (TREE_OVERFLOW (gnu_size))
5892 return No_Uint;
5894 /* This may have come from a conversion from some smaller type,
5895 so ensure this is in bitsizetype. */
5896 gnu_size = convert (bitsizetype, gnu_size);
5898 /* For negative values, use NEGATE_EXPR of the supplied value. */
5899 if (tree_int_cst_sgn (gnu_size) < 0)
5901 /* The ridiculous code below is to handle the case of the largest
5902 negative integer. */
5903 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5904 bool adjust = false;
5905 tree temp;
5907 if (TREE_OVERFLOW (negative_size))
5909 negative_size
5910 = size_binop (MINUS_EXPR, bitsize_zero_node,
5911 size_binop (PLUS_EXPR, gnu_size,
5912 bitsize_one_node));
5913 adjust = true;
5916 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5917 if (adjust)
5918 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5920 return annotate_value (temp);
5923 if (!host_integerp (gnu_size, 1))
5924 return No_Uint;
5926 size = tree_low_cst (gnu_size, 1);
5928 /* This peculiar test is to make sure that the size fits in an int
5929 on machines where HOST_WIDE_INT is not "int". */
5930 if (tree_low_cst (gnu_size, 1) == size)
5931 return UI_From_Int (size);
5932 else
5933 return No_Uint;
5935 case COMPONENT_REF:
5936 /* The only case we handle here is a simple discriminant reference. */
5937 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5938 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5939 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5940 return Create_Node (Discrim_Val,
5941 annotate_value (DECL_DISCRIMINANT_NUMBER
5942 (TREE_OPERAND (gnu_size, 1))),
5943 No_Uint, No_Uint);
5944 else
5945 return No_Uint;
5947 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5948 return annotate_value (TREE_OPERAND (gnu_size, 0));
5950 /* Now just list the operations we handle. */
5951 case COND_EXPR: tcode = Cond_Expr; break;
5952 case PLUS_EXPR: tcode = Plus_Expr; break;
5953 case MINUS_EXPR: tcode = Minus_Expr; break;
5954 case MULT_EXPR: tcode = Mult_Expr; break;
5955 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5956 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5957 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5958 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5959 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5960 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5961 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5962 case NEGATE_EXPR: tcode = Negate_Expr; break;
5963 case MIN_EXPR: tcode = Min_Expr; break;
5964 case MAX_EXPR: tcode = Max_Expr; break;
5965 case ABS_EXPR: tcode = Abs_Expr; break;
5966 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5967 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5968 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5969 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5970 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5971 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5972 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
5973 case LT_EXPR: tcode = Lt_Expr; break;
5974 case LE_EXPR: tcode = Le_Expr; break;
5975 case GT_EXPR: tcode = Gt_Expr; break;
5976 case GE_EXPR: tcode = Ge_Expr; break;
5977 case EQ_EXPR: tcode = Eq_Expr; break;
5978 case NE_EXPR: tcode = Ne_Expr; break;
5980 default:
5981 return No_Uint;
5984 /* Now get each of the operands that's relevant for this code. If any
5985 cannot be expressed as a repinfo node, say we can't. */
5986 for (i = 0; i < 3; i++)
5987 ops[i] = No_Uint;
5989 for (i = 0; i < len; i++)
5991 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5992 if (ops[i] == No_Uint)
5993 return No_Uint;
5996 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5997 TREE_COMPLEXITY (gnu_size) = ret;
5998 return ret;
6001 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6002 GCC type, set Component_Bit_Offset and Esize to the position and size
6003 used by Gigi. */
6005 static void
6006 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6008 tree gnu_list;
6009 tree gnu_entry;
6010 Entity_Id gnat_field;
6012 /* We operate by first making a list of all fields and their positions
6013 (we can get the sizes easily at any time) by a recursive call
6014 and then update all the sizes into the tree. */
6015 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6016 size_zero_node, bitsize_zero_node,
6017 BIGGEST_ALIGNMENT);
6019 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6020 gnat_field = Next_Entity (gnat_field))
6021 if ((Ekind (gnat_field) == E_Component
6022 || (Ekind (gnat_field) == E_Discriminant
6023 && !Is_Unchecked_Union (Scope (gnat_field)))))
6025 tree parent_offset = bitsize_zero_node;
6027 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6028 gnu_list);
6030 if (gnu_entry)
6032 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6034 /* In this mode the tag and parent components have not been
6035 generated, so we add the appropriate offset to each
6036 component. For a component appearing in the current
6037 extension, the offset is the size of the parent. */
6038 if (Is_Derived_Type (gnat_entity)
6039 && Original_Record_Component (gnat_field) == gnat_field)
6040 parent_offset
6041 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6042 bitsizetype);
6043 else
6044 parent_offset = bitsize_int (POINTER_SIZE);
6047 Set_Component_Bit_Offset
6048 (gnat_field,
6049 annotate_value
6050 (size_binop (PLUS_EXPR,
6051 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6052 TREE_VALUE (TREE_VALUE
6053 (TREE_VALUE (gnu_entry)))),
6054 parent_offset)));
6056 Set_Esize (gnat_field,
6057 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6059 else if (Is_Tagged_Type (gnat_entity)
6060 && Is_Derived_Type (gnat_entity))
6062 /* If there is no gnu_entry, this is an inherited component whose
6063 position is the same as in the parent type. */
6064 Set_Component_Bit_Offset
6065 (gnat_field,
6066 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6067 Set_Esize (gnat_field,
6068 Esize (Original_Record_Component (gnat_field)));
6073 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6074 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6075 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6076 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6077 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6078 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6079 so far. */
6081 static tree
6082 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6083 tree gnu_bitpos, unsigned int offset_align)
6085 tree gnu_field;
6086 tree gnu_result = gnu_list;
6088 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6089 gnu_field = TREE_CHAIN (gnu_field))
6091 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6092 DECL_FIELD_BIT_OFFSET (gnu_field));
6093 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6094 DECL_FIELD_OFFSET (gnu_field));
6095 unsigned int our_offset_align
6096 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6098 gnu_result
6099 = tree_cons (gnu_field,
6100 tree_cons (gnu_our_offset,
6101 tree_cons (size_int (our_offset_align),
6102 gnu_our_bitpos, NULL_TREE),
6103 NULL_TREE),
6104 gnu_result);
6106 if (DECL_INTERNAL_P (gnu_field))
6107 gnu_result
6108 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6109 gnu_our_offset, gnu_our_bitpos,
6110 our_offset_align);
6113 return gnu_result;
6116 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6117 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6118 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6119 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6120 for the size of a field. COMPONENT_P is true if we are being called
6121 to process the Component_Size of GNAT_OBJECT. This is used for error
6122 message handling and to indicate to use the object size of GNU_TYPE.
6123 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6124 it means that a size of zero should be treated as an unspecified size. */
6126 static tree
6127 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6128 enum tree_code kind, bool component_p, bool zero_ok)
6130 Node_Id gnat_error_node;
6131 tree type_size
6132 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
6133 tree size;
6135 /* Find the node to use for errors. */
6136 if ((Ekind (gnat_object) == E_Component
6137 || Ekind (gnat_object) == E_Discriminant)
6138 && Present (Component_Clause (gnat_object)))
6139 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6140 else if (Present (Size_Clause (gnat_object)))
6141 gnat_error_node = Expression (Size_Clause (gnat_object));
6142 else
6143 gnat_error_node = gnat_object;
6145 /* Return 0 if no size was specified, either because Esize was not Present or
6146 the specified size was zero. */
6147 if (No (uint_size) || uint_size == No_Uint)
6148 return NULL_TREE;
6150 /* Get the size as a tree. Give an error if a size was specified, but cannot
6151 be represented as in sizetype. */
6152 size = UI_To_gnu (uint_size, bitsizetype);
6153 if (TREE_OVERFLOW (size))
6155 post_error_ne (component_p ? "component size of & is too large"
6156 : "size of & is too large",
6157 gnat_error_node, gnat_object);
6158 return NULL_TREE;
6161 /* Ignore a negative size since that corresponds to our back-annotation.
6162 Also ignore a zero size unless a size clause exists. */
6163 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6164 return NULL_TREE;
6166 /* The size of objects is always a multiple of a byte. */
6167 if (kind == VAR_DECL
6168 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6170 if (component_p)
6171 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6172 gnat_error_node, gnat_object);
6173 else
6174 post_error_ne ("size for& is not a multiple of Storage_Unit",
6175 gnat_error_node, gnat_object);
6176 return NULL_TREE;
6179 /* If this is an integral type or a packed array type, the front-end has
6180 verified the size, so we need not do it here (which would entail
6181 checking against the bounds). However, if this is an aliased object, it
6182 may not be smaller than the type of the object. */
6183 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6184 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6185 return size;
6187 /* If the object is a record that contains a template, add the size of
6188 the template to the specified size. */
6189 if (TREE_CODE (gnu_type) == RECORD_TYPE
6190 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6191 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6193 /* Modify the size of the type to be that of the maximum size if it has a
6194 discriminant or the size of a thin pointer if this is a fat pointer. */
6195 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6196 type_size = max_size (type_size, true);
6197 else if (TYPE_FAT_POINTER_P (gnu_type))
6198 type_size = bitsize_int (POINTER_SIZE);
6200 /* If this is an access type, the minimum size is that given by the smallest
6201 integral mode that's valid for pointers. */
6202 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6204 enum machine_mode p_mode;
6206 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6207 !targetm.valid_pointer_mode (p_mode);
6208 p_mode = GET_MODE_WIDER_MODE (p_mode))
6211 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6214 /* If the size of the object is a constant, the new size must not be
6215 smaller. */
6216 if (TREE_CODE (type_size) != INTEGER_CST
6217 || TREE_OVERFLOW (type_size)
6218 || tree_int_cst_lt (size, type_size))
6220 if (component_p)
6221 post_error_ne_tree
6222 ("component size for& too small{, minimum allowed is ^}",
6223 gnat_error_node, gnat_object, type_size);
6224 else
6225 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6226 gnat_error_node, gnat_object, type_size);
6228 if (kind == VAR_DECL && !component_p
6229 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6230 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6231 post_error_ne_tree_2
6232 ("\\size of ^ is not a multiple of alignment (^ bits)",
6233 gnat_error_node, gnat_object, rm_size (gnu_type),
6234 TYPE_ALIGN (gnu_type));
6236 else if (INTEGRAL_TYPE_P (gnu_type))
6237 post_error_ne ("\\size would be legal if & were not aliased!",
6238 gnat_error_node, gnat_object);
6240 return NULL_TREE;
6243 return size;
6246 /* Similarly, but both validate and process a value of RM_Size. This
6247 routine is only called for types. */
6249 static void
6250 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6252 /* Only give an error if a Value_Size clause was explicitly given.
6253 Otherwise, we'd be duplicating an error on the Size clause. */
6254 Node_Id gnat_attr_node
6255 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6256 tree old_size = rm_size (gnu_type);
6257 tree size;
6259 /* Get the size as a tree. Do nothing if none was specified, either
6260 because RM_Size was not Present or if the specified size was zero.
6261 Give an error if a size was specified, but cannot be represented as
6262 in sizetype. */
6263 if (No (uint_size) || uint_size == No_Uint)
6264 return;
6266 size = UI_To_gnu (uint_size, bitsizetype);
6267 if (TREE_OVERFLOW (size))
6269 if (Present (gnat_attr_node))
6270 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6271 gnat_entity);
6273 return;
6276 /* Ignore a negative size since that corresponds to our back-annotation.
6277 Also ignore a zero size unless a size clause exists, a Value_Size
6278 clause exists, or this is an integer type, in which case the
6279 front end will have always set it. */
6280 else if (tree_int_cst_sgn (size) < 0
6281 || (integer_zerop (size) && No (gnat_attr_node)
6282 && !Has_Size_Clause (gnat_entity)
6283 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6284 return;
6286 /* If the old size is self-referential, get the maximum size. */
6287 if (CONTAINS_PLACEHOLDER_P (old_size))
6288 old_size = max_size (old_size, true);
6290 /* If the size of the object is a constant, the new size must not be
6291 smaller (the front end checks this for scalar types). */
6292 if (TREE_CODE (old_size) != INTEGER_CST
6293 || TREE_OVERFLOW (old_size)
6294 || (AGGREGATE_TYPE_P (gnu_type)
6295 && tree_int_cst_lt (size, old_size)))
6297 if (Present (gnat_attr_node))
6298 post_error_ne_tree
6299 ("Value_Size for& too small{, minimum allowed is ^}",
6300 gnat_attr_node, gnat_entity, old_size);
6302 return;
6305 /* Otherwise, set the RM_Size. */
6306 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6307 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6308 TYPE_RM_SIZE_NUM (gnu_type) = size;
6309 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6310 TYPE_RM_SIZE_NUM (gnu_type) = size;
6311 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6312 || TREE_CODE (gnu_type) == UNION_TYPE
6313 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6314 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6315 SET_TYPE_ADA_SIZE (gnu_type, size);
6318 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6319 If TYPE is the best type, return it. Otherwise, make a new type. We
6320 only support new integral and pointer types. BIASED_P is nonzero if
6321 we are making a biased type. */
6323 static tree
6324 make_type_from_size (tree type, tree size_tree, bool biased_p)
6326 tree new_type;
6327 unsigned HOST_WIDE_INT size;
6328 bool unsigned_p;
6330 /* If size indicates an error, just return TYPE to avoid propagating the
6331 error. Likewise if it's too large to represent. */
6332 if (!size_tree || !host_integerp (size_tree, 1))
6333 return type;
6335 size = tree_low_cst (size_tree, 1);
6336 switch (TREE_CODE (type))
6338 case INTEGER_TYPE:
6339 case ENUMERAL_TYPE:
6340 /* Only do something if the type is not already the proper size and is
6341 not a packed array type. */
6342 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6343 || (TYPE_PRECISION (type) == size
6344 && biased_p == (TREE_CODE (type) == INTEGER_CST
6345 && TYPE_BIASED_REPRESENTATION_P (type))))
6346 break;
6348 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6349 && TYPE_BIASED_REPRESENTATION_P (type));
6350 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6352 size = MIN (size, LONG_LONG_TYPE_SIZE);
6353 new_type
6354 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6355 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6356 TYPE_MIN_VALUE (new_type)
6357 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6358 TYPE_MAX_VALUE (new_type)
6359 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6360 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6361 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6362 return new_type;
6364 case RECORD_TYPE:
6365 /* Do something if this is a fat pointer, in which case we
6366 may need to return the thin pointer. */
6367 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6368 return
6369 build_pointer_type
6370 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6371 break;
6373 case POINTER_TYPE:
6374 /* Only do something if this is a thin pointer, in which case we
6375 may need to return the fat pointer. */
6376 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6377 return
6378 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6380 break;
6382 default:
6383 break;
6386 return type;
6389 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6390 a type or object whose present alignment is ALIGN. If this alignment is
6391 valid, return it. Otherwise, give an error and return ALIGN. */
6393 static unsigned int
6394 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6396 Node_Id gnat_error_node = gnat_entity;
6397 unsigned int new_align;
6399 #ifndef MAX_OFILE_ALIGNMENT
6400 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6401 #endif
6403 if (Present (Alignment_Clause (gnat_entity)))
6404 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6406 /* Don't worry about checking alignment if alignment was not specified
6407 by the source program and we already posted an error for this entity. */
6409 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6410 return align;
6412 /* Within GCC, an alignment is an integer, so we must make sure a
6413 value is specified that fits in that range. Also, alignments of
6414 more than MAX_OFILE_ALIGNMENT can't be supported. */
6416 if (! UI_Is_In_Int_Range (alignment)
6417 || ((new_align = UI_To_Int (alignment))
6418 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6419 post_error_ne_num ("largest supported alignment for& is ^",
6420 gnat_error_node, gnat_entity,
6421 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6422 else if (!(Present (Alignment_Clause (gnat_entity))
6423 && From_At_Mod (Alignment_Clause (gnat_entity)))
6424 && new_align * BITS_PER_UNIT < align)
6425 post_error_ne_num ("alignment for& must be at least ^",
6426 gnat_error_node, gnat_entity,
6427 align / BITS_PER_UNIT);
6428 else
6429 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6431 return align;
6434 /* Verify that OBJECT, a type or decl, is something we can implement
6435 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6436 if we require atomic components. */
6438 static void
6439 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6441 Node_Id gnat_error_point = gnat_entity;
6442 Node_Id gnat_node;
6443 enum machine_mode mode;
6444 unsigned int align;
6445 tree size;
6447 /* There are three case of what OBJECT can be. It can be a type, in which
6448 case we take the size, alignment and mode from the type. It can be a
6449 declaration that was indirect, in which case the relevant values are
6450 that of the type being pointed to, or it can be a normal declaration,
6451 in which case the values are of the decl. The code below assumes that
6452 OBJECT is either a type or a decl. */
6453 if (TYPE_P (object))
6455 mode = TYPE_MODE (object);
6456 align = TYPE_ALIGN (object);
6457 size = TYPE_SIZE (object);
6459 else if (DECL_BY_REF_P (object))
6461 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6462 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6463 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6465 else
6467 mode = DECL_MODE (object);
6468 align = DECL_ALIGN (object);
6469 size = DECL_SIZE (object);
6472 /* Consider all floating-point types atomic and any types that that are
6473 represented by integers no wider than a machine word. */
6474 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6475 || ((GET_MODE_CLASS (mode) == MODE_INT
6476 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6477 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6478 return;
6480 /* For the moment, also allow anything that has an alignment equal
6481 to its size and which is smaller than a word. */
6482 if (size && TREE_CODE (size) == INTEGER_CST
6483 && compare_tree_int (size, align) == 0
6484 && align <= BITS_PER_WORD)
6485 return;
6487 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6488 gnat_node = Next_Rep_Item (gnat_node))
6490 if (!comp_p && Nkind (gnat_node) == N_Pragma
6491 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6492 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6493 else if (comp_p && Nkind (gnat_node) == N_Pragma
6494 && (Get_Pragma_Id (Chars (gnat_node))
6495 == Pragma_Atomic_Components))
6496 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6499 if (comp_p)
6500 post_error_ne ("atomic access to component of & cannot be guaranteed",
6501 gnat_error_point, gnat_entity);
6502 else
6503 post_error_ne ("atomic access to & cannot be guaranteed",
6504 gnat_error_point, gnat_entity);
6507 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6508 have compatible signatures so that a call using one type may be safely
6509 issued if the actual target function type is the other. Return 1 if it is
6510 the case, 0 otherwise, and post errors on the incompatibilities.
6512 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6513 that calls to the subprogram will have arguments suitable for the later
6514 underlying builtin expansion. */
6516 static int
6517 compatible_signatures_p (tree ftype1, tree ftype2)
6519 /* As of now, we only perform very trivial tests and consider it's the
6520 programmer's responsibility to ensure the type correctness in the Ada
6521 declaration, as in the regular Import cases.
6523 Mismatches typically result in either error messages from the builtin
6524 expander, internal compiler errors, or in a real call sequence. This
6525 should be refined to issue diagnostics helping error detection and
6526 correction. */
6528 /* Almost fake test, ensuring a use of each argument. */
6529 if (ftype1 == ftype2)
6530 return 1;
6532 return 1;
6535 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6536 with all size expressions that contain F updated by replacing F with R.
6537 This is identical to GCC's substitute_in_type except that it knows about
6538 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6539 nothing has changed. */
6541 tree
6542 gnat_substitute_in_type (tree t, tree f, tree r)
6544 tree new = t;
6545 tree tem;
6547 switch (TREE_CODE (t))
6549 case INTEGER_TYPE:
6550 case ENUMERAL_TYPE:
6551 case BOOLEAN_TYPE:
6552 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6553 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6555 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6556 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6558 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6559 return t;
6561 new = build_range_type (TREE_TYPE (t), low, high);
6562 if (TYPE_INDEX_TYPE (t))
6563 SET_TYPE_INDEX_TYPE
6564 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6565 return new;
6568 return t;
6570 case REAL_TYPE:
6571 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6572 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6574 tree low = NULL_TREE, high = NULL_TREE;
6576 if (TYPE_MIN_VALUE (t))
6577 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6578 if (TYPE_MAX_VALUE (t))
6579 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6581 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6582 return t;
6584 t = copy_type (t);
6585 TYPE_MIN_VALUE (t) = low;
6586 TYPE_MAX_VALUE (t) = high;
6588 return t;
6590 case COMPLEX_TYPE:
6591 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6592 if (tem == TREE_TYPE (t))
6593 return t;
6595 return build_complex_type (tem);
6597 case OFFSET_TYPE:
6598 case METHOD_TYPE:
6599 case FUNCTION_TYPE:
6600 case LANG_TYPE:
6601 /* Don't know how to do these yet. */
6602 gcc_unreachable ();
6604 case ARRAY_TYPE:
6606 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6607 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6609 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6610 return t;
6612 new = build_array_type (component, domain);
6613 TYPE_SIZE (new) = 0;
6614 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6615 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6616 layout_type (new);
6617 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6619 /* If we had bounded the sizes of T by a constant, bound the sizes of
6620 NEW by the same constant. */
6621 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6622 TYPE_SIZE (new)
6623 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6624 TYPE_SIZE (new));
6625 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6626 TYPE_SIZE_UNIT (new)
6627 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6628 TYPE_SIZE_UNIT (new));
6629 return new;
6632 case RECORD_TYPE:
6633 case UNION_TYPE:
6634 case QUAL_UNION_TYPE:
6636 tree field;
6637 bool changed_field
6638 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6639 bool field_has_rep = false;
6640 tree last_field = NULL_TREE;
6642 tree new = copy_type (t);
6644 /* Start out with no fields, make new fields, and chain them
6645 in. If we haven't actually changed the type of any field,
6646 discard everything we've done and return the old type. */
6648 TYPE_FIELDS (new) = NULL_TREE;
6649 TYPE_SIZE (new) = NULL_TREE;
6651 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6653 tree new_field = copy_node (field);
6655 TREE_TYPE (new_field)
6656 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6658 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6659 field_has_rep = true;
6660 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6661 changed_field = true;
6663 /* If this is an internal field and the type of this field is
6664 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6665 the type just has one element, treat that as the field.
6666 But don't do this if we are processing a QUAL_UNION_TYPE. */
6667 if (TREE_CODE (t) != QUAL_UNION_TYPE
6668 && DECL_INTERNAL_P (new_field)
6669 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6670 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6672 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6673 continue;
6675 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6677 tree next_new_field
6678 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6680 /* Make sure omitting the union doesn't change
6681 the layout. */
6682 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6683 new_field = next_new_field;
6687 DECL_CONTEXT (new_field) = new;
6688 SET_DECL_ORIGINAL_FIELD (new_field,
6689 (DECL_ORIGINAL_FIELD (field)
6690 ? DECL_ORIGINAL_FIELD (field) : field));
6692 /* If the size of the old field was set at a constant,
6693 propagate the size in case the type's size was variable.
6694 (This occurs in the case of a variant or discriminated
6695 record with a default size used as a field of another
6696 record.) */
6697 DECL_SIZE (new_field)
6698 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6699 ? DECL_SIZE (field) : NULL_TREE;
6700 DECL_SIZE_UNIT (new_field)
6701 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6702 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6704 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6706 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6708 if (new_q != DECL_QUALIFIER (new_field))
6709 changed_field = true;
6711 /* Do the substitution inside the qualifier and if we find
6712 that this field will not be present, omit it. */
6713 DECL_QUALIFIER (new_field) = new_q;
6715 if (integer_zerop (DECL_QUALIFIER (new_field)))
6716 continue;
6719 if (!last_field)
6720 TYPE_FIELDS (new) = new_field;
6721 else
6722 TREE_CHAIN (last_field) = new_field;
6724 last_field = new_field;
6726 /* If this is a qualified type and this field will always be
6727 present, we are done. */
6728 if (TREE_CODE (t) == QUAL_UNION_TYPE
6729 && integer_onep (DECL_QUALIFIER (new_field)))
6730 break;
6733 /* If this used to be a qualified union type, but we now know what
6734 field will be present, make this a normal union. */
6735 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6736 && (!TYPE_FIELDS (new)
6737 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6738 TREE_SET_CODE (new, UNION_TYPE);
6739 else if (!changed_field)
6740 return t;
6742 gcc_assert (!field_has_rep);
6743 layout_type (new);
6745 /* If the size was originally a constant use it. */
6746 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6747 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6749 TYPE_SIZE (new) = TYPE_SIZE (t);
6750 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6751 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6754 return new;
6757 default:
6758 return t;
6762 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6763 needed to represent the object. */
6765 tree
6766 rm_size (tree gnu_type)
6768 /* For integer types, this is the precision. For record types, we store
6769 the size explicitly. For other types, this is just the size. */
6771 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6772 return TYPE_RM_SIZE (gnu_type);
6773 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6774 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6775 /* Return the rm_size of the actual data plus the size of the template. */
6776 return
6777 size_binop (PLUS_EXPR,
6778 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6779 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6780 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6781 || TREE_CODE (gnu_type) == UNION_TYPE
6782 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6783 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6784 && TYPE_ADA_SIZE (gnu_type))
6785 return TYPE_ADA_SIZE (gnu_type);
6786 else
6787 return TYPE_SIZE (gnu_type);
6790 /* Return an identifier representing the external name to be used for
6791 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6792 and the specified suffix. */
6794 tree
6795 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6797 Entity_Kind kind = Ekind (gnat_entity);
6799 const char *str = (!suffix ? "" : suffix);
6800 String_Template temp = {1, strlen (str)};
6801 Fat_Pointer fp = {str, &temp};
6803 Get_External_Name_With_Suffix (gnat_entity, fp);
6805 /* A variable using the Stdcall convention (meaning we are running
6806 on a Windows box) live in a DLL. Here we adjust its name to use
6807 the jump-table, the _imp__NAME contains the address for the NAME
6808 variable. */
6809 if ((kind == E_Variable || kind == E_Constant)
6810 && Has_Stdcall_Convention (gnat_entity))
6812 const char *prefix = "_imp__";
6813 int k, plen = strlen (prefix);
6815 for (k = 0; k <= Name_Len; k++)
6816 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6817 strncpy (Name_Buffer, prefix, plen);
6820 return get_identifier (Name_Buffer);
6823 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6824 fully-qualified name, possibly with type information encoding.
6825 Otherwise, return the name. */
6827 tree
6828 get_entity_name (Entity_Id gnat_entity)
6830 Get_Encoded_Name (gnat_entity);
6831 return get_identifier (Name_Buffer);
6834 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6835 string, return a new IDENTIFIER_NODE that is the concatenation of
6836 the name in GNU_ID and SUFFIX. */
6838 tree
6839 concat_id_with_name (tree gnu_id, const char *suffix)
6841 int len = IDENTIFIER_LENGTH (gnu_id);
6843 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6844 IDENTIFIER_LENGTH (gnu_id));
6845 strncpy (Name_Buffer + len, "___", 3);
6846 len += 3;
6847 strcpy (Name_Buffer + len, suffix);
6848 return get_identifier (Name_Buffer);