* decl.c (gnat_to_gnu_entity) <E_Procedure>: Do not set "const" flag
[official-gcc.git] / gcc / ada / decl.c
blobfac6f2f06e3c73defedf6044a4a0b0f3cb478ed7
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2006, 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 || From_With_Type (gnat_entity));
181 unsigned int align = 0;
183 /* Since a use of an Itype is a definition, process it as such if it
184 is not in a with'ed unit. */
186 if (!definition && Is_Itype (gnat_entity)
187 && !present_gnu_tree (gnat_entity)
188 && In_Extended_Main_Code_Unit (gnat_entity))
190 /* Ensure that we are in a subprogram mentioned in the Scope
191 chain of this entity, our current scope is global,
192 or that we encountered a task or entry (where we can't currently
193 accurately check scoping). */
194 if (!current_function_decl
195 || DECL_ELABORATION_PROC_P (current_function_decl))
197 process_type (gnat_entity);
198 return get_gnu_tree (gnat_entity);
201 for (gnat_temp = Scope (gnat_entity);
202 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
204 if (Is_Type (gnat_temp))
205 gnat_temp = Underlying_Type (gnat_temp);
207 if (Ekind (gnat_temp) == E_Subprogram_Body)
208 gnat_temp
209 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
211 if (IN (Ekind (gnat_temp), Subprogram_Kind)
212 && Present (Protected_Body_Subprogram (gnat_temp)))
213 gnat_temp = Protected_Body_Subprogram (gnat_temp);
215 if (Ekind (gnat_temp) == E_Entry
216 || Ekind (gnat_temp) == E_Entry_Family
217 || Ekind (gnat_temp) == E_Task_Type
218 || (IN (Ekind (gnat_temp), Subprogram_Kind)
219 && present_gnu_tree (gnat_temp)
220 && (current_function_decl
221 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
223 process_type (gnat_entity);
224 return get_gnu_tree (gnat_entity);
228 /* This abort means the entity "gnat_entity" has an incorrect scope,
229 i.e. that its scope does not correspond to the subprogram in which
230 it is declared */
231 gcc_unreachable ();
234 /* If this is entity 0, something went badly wrong. */
235 gcc_assert (Present (gnat_entity));
237 /* If we've already processed this entity, return what we got last time.
238 If we are defining the node, we should not have already processed it.
239 In that case, we will abort below when we try to save a new GCC tree for
240 this object. We also need to handle the case of getting a dummy type
241 when a Full_View exists. */
243 if (present_gnu_tree (gnat_entity)
244 && (! definition
245 || (Is_Type (gnat_entity) && imported_p)))
247 gnu_decl = get_gnu_tree (gnat_entity);
249 if (TREE_CODE (gnu_decl) == TYPE_DECL
250 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
251 && IN (kind, Incomplete_Or_Private_Kind)
252 && Present (Full_View (gnat_entity)))
254 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
255 NULL_TREE, 0);
257 save_gnu_tree (gnat_entity, NULL_TREE, false);
258 save_gnu_tree (gnat_entity, gnu_decl, false);
261 return gnu_decl;
264 /* If this is a numeric or enumeral type, or an access type, a nonzero
265 Esize must be specified unless it was specified by the programmer. */
266 gcc_assert (!Unknown_Esize (gnat_entity)
267 || Has_Size_Clause (gnat_entity)
268 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
269 && (!IN (kind, Access_Kind)
270 || kind == E_Access_Protected_Subprogram_Type
271 || kind == E_Access_Subtype)));
273 /* Likewise, RM_Size must be specified for all discrete and fixed-point
274 types. */
275 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
276 || !Unknown_RM_Size (gnat_entity));
278 /* Get the name of the entity and set up the line number and filename of
279 the original definition for use in any decl we make. */
280 gnu_entity_id = get_entity_name (gnat_entity);
281 Sloc_to_locus (Sloc (gnat_entity), &input_location);
283 /* If we get here, it means we have not yet done anything with this
284 entity. If we are not defining it here, it must be external,
285 otherwise we should have defined it already. */
286 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
287 || kind == E_Discriminant || kind == E_Component
288 || kind == E_Label
289 || (kind == E_Constant && Present (Full_View (gnat_entity)))
290 || IN (kind, Type_Kind));
292 /* For cases when we are not defining (i.e., we are referencing from
293 another compilation unit) Public entities, show we are at global level
294 for the purpose of computing scopes. Don't do this for components or
295 discriminants since the relevant test is whether or not the record is
296 being defined. But do this for Imported functions or procedures in
297 all cases. */
298 if ((!definition && Is_Public (gnat_entity)
299 && !Is_Statically_Allocated (gnat_entity)
300 && kind != E_Discriminant && kind != E_Component)
301 || (Is_Imported (gnat_entity)
302 && (kind == E_Function || kind == E_Procedure)))
303 force_global++, this_global = true;
305 /* Handle any attributes directly attached to the entity. */
306 if (Has_Gigi_Rep_Item (gnat_entity))
307 prepend_attributes (gnat_entity, &attr_list);
309 /* Machine_Attributes on types are expected to be propagated to subtypes.
310 The corresponding Gigi_Rep_Items are only attached to the first subtype
311 though, so we handle the propagation here. */
312 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
313 && !Is_First_Subtype (gnat_entity)
314 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
315 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
317 switch (kind)
319 case E_Constant:
320 /* If this is a use of a deferred constant, get its full
321 declaration. */
322 if (!definition && Present (Full_View (gnat_entity)))
324 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
325 gnu_expr, definition);
326 saved = true;
327 break;
330 /* If we have an external constant that we are not defining,
331 get the expression that is was defined to represent. We
332 may throw that expression away later if it is not a
333 constant.
334 Do not retrieve the expression if it is an aggregate, because
335 in complex instantiation contexts it may not be expanded */
337 if (!definition
338 && Present (Expression (Declaration_Node (gnat_entity)))
339 && !No_Initialization (Declaration_Node (gnat_entity))
340 && (Nkind (Expression (Declaration_Node (gnat_entity)))
341 != N_Aggregate))
342 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
344 /* Ignore deferred constant definitions; they are processed fully in the
345 front-end. For deferred constant references, get the full
346 definition. On the other hand, constants that are renamings are
347 handled like variable renamings. If No_Initialization is set, this is
348 not a deferred constant but a constant whose value is built
349 manually. */
351 if (definition && !gnu_expr
352 && !No_Initialization (Declaration_Node (gnat_entity))
353 && No (Renamed_Object (gnat_entity)))
355 gnu_decl = error_mark_node;
356 saved = true;
357 break;
359 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
360 && Present (Full_View (gnat_entity)))
362 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
363 NULL_TREE, 0);
364 saved = true;
365 break;
368 goto object;
370 case E_Exception:
371 /* We used to special case VMS exceptions here to directly map them to
372 their associated condition code. Since this code had to be masked
373 dynamically to strip off the severity bits, this caused trouble in
374 the GCC/ZCX case because the "type" pointers we store in the tables
375 have to be static. We now don't special case here anymore, and let
376 the regular processing take place, which leaves us with a regular
377 exception data object for VMS exceptions too. The condition code
378 mapping is taken care of by the front end and the bitmasking by the
379 runtime library. */
380 goto object;
382 case E_Discriminant:
383 case E_Component:
385 /* The GNAT record where the component was defined. */
386 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
388 /* If the variable is an inherited record component (in the case of
389 extended record types), just return the inherited entity, which
390 must be a FIELD_DECL. Likewise for discriminants.
391 For discriminants of untagged records which have explicit
392 stored discriminants, return the entity for the corresponding
393 stored discriminant. Also use Original_Record_Component
394 if the record has a private extension. */
396 if (Present (Original_Record_Component (gnat_entity))
397 && Original_Record_Component (gnat_entity) != gnat_entity)
399 gnu_decl
400 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
401 gnu_expr, definition);
402 saved = true;
403 break;
406 /* If the enclosing record has explicit stored discriminants,
407 then it is an untagged record. If the Corresponding_Discriminant
408 is not empty then this must be a renamed discriminant and its
409 Original_Record_Component must point to the corresponding explicit
410 stored discriminant (i.e., we should have taken the previous
411 branch). */
413 else if (Present (Corresponding_Discriminant (gnat_entity))
414 && Is_Tagged_Type (gnat_record))
416 /* A tagged record has no explicit stored discriminants. */
418 gcc_assert (First_Discriminant (gnat_record)
419 == First_Stored_Discriminant (gnat_record));
420 gnu_decl
421 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
422 gnu_expr, definition);
423 saved = true;
424 break;
427 /* If the enclosing record has explicit stored discriminants,
428 then it is an untagged record. If the Corresponding_Discriminant
429 is not empty then this must be a renamed discriminant and its
430 Original_Record_Component must point to the corresponding explicit
431 stored discriminant (i.e., we should have taken the first
432 branch). */
434 else if (Present (Corresponding_Discriminant (gnat_entity))
435 && (First_Discriminant (gnat_record)
436 != First_Stored_Discriminant (gnat_record)))
437 gcc_unreachable ();
439 /* Otherwise, if we are not defining this and we have no GCC type
440 for the containing record, make one for it. Then we should
441 have made our own equivalent. */
442 else if (!definition && !present_gnu_tree (gnat_record))
444 /* ??? If this is in a record whose scope is a protected
445 type and we have an Original_Record_Component, use it.
446 This is a workaround for major problems in protected type
447 handling. */
449 Entity_Id Scop = Scope (Scope (gnat_entity));
450 if ((Is_Protected_Type (Scop)
451 || (Is_Private_Type (Scop)
452 && Present (Full_View (Scop))
453 && Is_Protected_Type (Full_View (Scop))))
454 && Present (Original_Record_Component (gnat_entity)))
456 gnu_decl
457 = gnat_to_gnu_entity (Original_Record_Component
458 (gnat_entity),
459 gnu_expr, definition);
460 saved = true;
461 break;
464 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
465 gnu_decl = get_gnu_tree (gnat_entity);
466 saved = true;
467 break;
470 else
471 /* Here we have no GCC type and this is a reference rather than a
472 definition. This should never happen. Most likely the cause is a
473 reference before declaration in the gnat tree for gnat_entity. */
474 gcc_unreachable ();
477 case E_Loop_Parameter:
478 case E_Out_Parameter:
479 case E_Variable:
481 /* Simple variables, loop variables, OUT parameters, and exceptions. */
482 object:
484 bool used_by_ref = false;
485 bool const_flag
486 = ((kind == E_Constant || kind == E_Variable)
487 && !Is_Statically_Allocated (gnat_entity)
488 && Is_True_Constant (gnat_entity)
489 && (((Nkind (Declaration_Node (gnat_entity))
490 == N_Object_Declaration)
491 && Present (Expression (Declaration_Node (gnat_entity))))
492 || Present (Renamed_Object (gnat_entity))));
493 bool inner_const_flag = const_flag;
494 bool static_p = Is_Statically_Allocated (gnat_entity);
495 bool mutable_p = false;
496 tree gnu_ext_name = NULL_TREE;
497 tree renamed_obj = NULL_TREE;
499 if (Present (Renamed_Object (gnat_entity)) && !definition)
501 if (kind == E_Exception)
502 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
503 NULL_TREE, 0);
504 else
505 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
508 /* Get the type after elaborating the renamed object. */
509 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
511 /* If this is a loop variable, its type should be the base type.
512 This is because the code for processing a loop determines whether
513 a normal loop end test can be done by comparing the bounds of the
514 loop against those of the base type, which is presumed to be the
515 size used for computation. But this is not correct when the size
516 of the subtype is smaller than the type. */
517 if (kind == E_Loop_Parameter)
518 gnu_type = get_base_type (gnu_type);
520 /* Reject non-renamed objects whose types are unconstrained arrays or
521 any object whose type is a dummy type or VOID_TYPE. */
523 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
524 && No (Renamed_Object (gnat_entity)))
525 || TYPE_IS_DUMMY_P (gnu_type)
526 || TREE_CODE (gnu_type) == VOID_TYPE)
528 gcc_assert (type_annotate_only);
529 if (this_global)
530 force_global--;
531 return error_mark_node;
534 /* If an alignment is specified, use it if valid. Note that
535 exceptions are objects but don't have alignments. We must do this
536 before we validate the size, since the alignment can affect the
537 size. */
538 if (kind != E_Exception && Known_Alignment (gnat_entity))
540 gcc_assert (Present (Alignment (gnat_entity)));
541 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
542 TYPE_ALIGN (gnu_type));
543 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
544 gnat_entity, "PAD", 0, definition, 1);
547 /* If we are defining the object, see if it has a Size value and
548 validate it if so. If we are not defining the object and a Size
549 clause applies, simply retrieve the value. We don't want to ignore
550 the clause and it is expected to have been validated already. Then
551 get the new type, if any. */
552 if (definition)
553 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
554 gnat_entity, VAR_DECL, false,
555 Has_Size_Clause (gnat_entity));
556 else if (Has_Size_Clause (gnat_entity))
557 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
559 if (gnu_size)
561 gnu_type
562 = make_type_from_size (gnu_type, gnu_size,
563 Has_Biased_Representation (gnat_entity));
565 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
566 gnu_size = NULL_TREE;
569 /* If this object has self-referential size, it must be a record with
570 a default value. We are supposed to allocate an object of the
571 maximum size in this case unless it is a constant with an
572 initializing expression, in which case we can get the size from
573 that. Note that the resulting size may still be a variable, so
574 this may end up with an indirect allocation. */
576 if (No (Renamed_Object (gnat_entity))
577 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
579 if (gnu_expr && kind == E_Constant)
580 gnu_size
581 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
582 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
584 /* We may have no GNU_EXPR because No_Initialization is
585 set even though there's an Expression. */
586 else if (kind == E_Constant
587 && (Nkind (Declaration_Node (gnat_entity))
588 == N_Object_Declaration)
589 && Present (Expression (Declaration_Node (gnat_entity))))
590 gnu_size
591 = TYPE_SIZE (gnat_to_gnu_type
592 (Etype
593 (Expression (Declaration_Node (gnat_entity)))));
594 else
596 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
597 mutable_p = true;
601 /* If the size is zero bytes, make it one byte since some linkers have
602 trouble with zero-sized objects. If the object will have a
603 template, that will make it nonzero so don't bother. Also avoid
604 doing that for an object renaming or an object with an address
605 clause, as we would lose useful information on the view size
606 (e.g. for null array slices) and we are not allocating the object
607 here anyway. */
608 if (((gnu_size && integer_zerop (gnu_size))
609 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
610 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
611 || !Is_Array_Type (Etype (gnat_entity)))
612 && !Present (Renamed_Object (gnat_entity))
613 && !Present (Address_Clause (gnat_entity)))
614 gnu_size = bitsize_unit_node;
616 /* If this is an atomic object with no specified size and alignment,
617 but where the size of the type is a constant, set the alignment to
618 the lowest power of two greater than the size, or to the
619 biggest meaningful alignment, whichever is smaller. */
621 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
622 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
624 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
625 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
626 BIGGEST_ALIGNMENT))
627 align = BIGGEST_ALIGNMENT;
628 else
629 align = ((unsigned int) 1
630 << (floor_log2 (tree_low_cst
631 (TYPE_SIZE (gnu_type), 1) - 1)
632 + 1));
635 /* If the object is set to have atomic components, find the component
636 type and validate it.
638 ??? Note that we ignore Has_Volatile_Components on objects; it's
639 not at all clear what to do in that case. */
641 if (Has_Atomic_Components (gnat_entity))
643 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
644 ? TREE_TYPE (gnu_type) : gnu_type);
646 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
647 && TYPE_MULTI_ARRAY_P (gnu_inner))
648 gnu_inner = TREE_TYPE (gnu_inner);
650 check_ok_for_atomic (gnu_inner, gnat_entity, true);
653 /* Now check if the type of the object allows atomic access. Note
654 that we must test the type, even if this object has size and
655 alignment to allow such access, because we will be going
656 inside the padded record to assign to the object. We could fix
657 this by always copying via an intermediate value, but it's not
658 clear it's worth the effort. */
659 if (Is_Atomic (gnat_entity))
660 check_ok_for_atomic (gnu_type, gnat_entity, false);
662 /* If this is an aliased object with an unconstrained nominal subtype,
663 make a type that includes the template. */
664 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
665 && Is_Array_Type (Etype (gnat_entity))
666 && !type_annotate_only)
668 tree gnu_fat
669 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
671 gnu_type
672 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
673 concat_id_with_name (gnu_entity_id,
674 "UNC"));
677 #ifdef MINIMUM_ATOMIC_ALIGNMENT
678 /* If the size is a constant and no alignment is specified, force
679 the alignment to be the minimum valid atomic alignment. The
680 restriction on constant size avoids problems with variable-size
681 temporaries; if the size is variable, there's no issue with
682 atomic access. Also don't do this for a constant, since it isn't
683 necessary and can interfere with constant replacement. Finally,
684 do not do it for Out parameters since that creates an
685 size inconsistency with In parameters. */
686 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
687 && !FLOAT_TYPE_P (gnu_type)
688 && !const_flag && No (Renamed_Object (gnat_entity))
689 && !imported_p && No (Address_Clause (gnat_entity))
690 && kind != E_Out_Parameter
691 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
692 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
693 align = MINIMUM_ATOMIC_ALIGNMENT;
694 #endif
696 /* Make a new type with the desired size and alignment, if needed. */
697 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
698 "PAD", false, definition, true);
700 /* Make a volatile version of this object's type if we are to
701 make the object volatile. Note that 13.3(19) says that we
702 should treat other types of objects as volatile as well. */
703 if ((Treat_As_Volatile (gnat_entity)
704 || Is_Exported (gnat_entity)
705 || Is_Imported (gnat_entity)
706 || Present (Address_Clause (gnat_entity)))
707 && !TYPE_VOLATILE (gnu_type))
708 gnu_type = build_qualified_type (gnu_type,
709 (TYPE_QUALS (gnu_type)
710 | TYPE_QUAL_VOLATILE));
712 /* Convert the expression to the type of the object except in the
713 case where the object's type is unconstrained or the object's type
714 is a padded record whose field is of self-referential size. In
715 the former case, converting will generate unnecessary evaluations
716 of the CONSTRUCTOR to compute the size and in the latter case, we
717 want to only copy the actual data. */
718 if (gnu_expr
719 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
720 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
721 && !(TREE_CODE (gnu_type) == RECORD_TYPE
722 && TYPE_IS_PADDING_P (gnu_type)
723 && (CONTAINS_PLACEHOLDER_P
724 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
725 gnu_expr = convert (gnu_type, gnu_expr);
727 /* See if this is a renaming, and handle appropriately depending on
728 what is renamed and in which context. There are three major
729 cases:
731 1/ This is a constant renaming and we can just make an object
732 with what is renamed as its initial value,
734 2/ We can reuse a stabilized version of what is renamed in place
735 of the renaming,
737 3/ If neither 1 or 2 applies, we make the renaming entity a constant
738 pointer to what is being renamed. */
740 if (Present (Renamed_Object (gnat_entity)))
742 /* If the renamed object had padding, strip off the reference
743 to the inner object and reset our type. */
744 if (TREE_CODE (gnu_expr) == COMPONENT_REF
745 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
746 == RECORD_TYPE)
747 && (TYPE_IS_PADDING_P
748 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
750 gnu_expr = TREE_OPERAND (gnu_expr, 0);
751 gnu_type = TREE_TYPE (gnu_expr);
754 /* Case 1: If this is a constant renaming, treat it as a normal
755 object whose initial value is what is being renamed. We cannot
756 do this if the type is unconstrained or class-wide. */
757 if (const_flag
758 && !TREE_SIDE_EFFECTS (gnu_expr)
759 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
760 && TYPE_MODE (gnu_type) != BLKmode
761 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
762 && !Is_Array_Type (Etype (gnat_entity)))
765 /* Otherwise, see if we can proceed with a stabilized version of
766 the renamed entity or if we need to make a pointer. */
767 else
769 bool stabilized = false;
770 tree maybe_stable_expr = NULL_TREE;
772 /* Case 2: If the renaming entity need not be materialized and
773 the renamed expression is something we can stabilize, use
774 that for the renaming. At the global level, we can only do
775 this if we know no SAVE_EXPRs need be made, because the
776 expression we return might be used in arbitrary conditional
777 branches so we must force the SAVE_EXPRs evaluation
778 immediately and this requires a function context. */
779 if (!Materialize_Entity (gnat_entity)
780 && (!global_bindings_p ()
781 || (staticp (gnu_expr)
782 && !TREE_SIDE_EFFECTS (gnu_expr))))
784 maybe_stable_expr
785 = maybe_stabilize_reference (gnu_expr, true, false,
786 &stabilized);
788 if (stabilized)
790 gnu_decl = maybe_stable_expr;
791 save_gnu_tree (gnat_entity, gnu_decl, true);
792 saved = true;
793 break;
796 /* The stabilization failed. Keep maybe_stable_expr
797 untouched here to let the pointer case below know
798 about that failure. */
801 /* Case 3: Make this into a constant pointer to the object we
802 are to rename and attach the object to the pointer if it is
803 an lvalue that can be stabilized.
805 From the proper scope, attached objects will be referenced
806 directly instead of indirectly via the pointer to avoid
807 subtle aliasing problems with non addressable entities.
808 They have to be stable because we must not evaluate the
809 variables in the expression every time the renaming is used.
810 They also have to be lvalues because the context in which
811 they are reused sometimes requires so. We call pointers
812 with an attached object "renaming" pointers.
814 In the rare cases where we cannot stabilize the renamed
815 object, we just make a "bare" pointer, and the renamed
816 entity is always accessed indirectly through it. */
818 bool expr_has_side_effects = TREE_SIDE_EFFECTS (gnu_expr);
820 inner_const_flag = TREE_READONLY (gnu_expr);
821 const_flag = true;
822 gnu_type = build_reference_type (gnu_type);
824 /* If a previous attempt at unrestricted stabilization
825 failed, there is no point trying again and we can reuse
826 the result without attaching it to the pointer. */
827 if (maybe_stable_expr)
830 /* Otherwise, try to stabilize now, restricting to
831 lvalues only, and attach the expression to the pointer
832 if the stabilization succeeds.
834 Note that this might introduce SAVE_EXPRs and we don't
835 check whether we're at the global level or not. This is
836 fine since we are building a pointer initializer and
837 neither the pointer nor the initializing expression can
838 be accessed before the pointer elaboration has taken
839 place in a correct program.
841 SAVE_EXPRs will be evaluated at the right spots by either
842 create_var_decl->expand_decl_init for the non-global case
843 or build_unit_elab for the global case, and will be
844 attached to the elaboration procedure by the RTL expander
845 in the latter case. We have no need to force an early
846 evaluation here. */
847 else
849 maybe_stable_expr
850 = maybe_stabilize_reference (gnu_expr, true, true,
851 &stabilized);
853 if (stabilized)
854 renamed_obj = maybe_stable_expr;
855 /* Attaching is actually performed downstream, as soon
856 as we have a DECL for the pointer we make. */
859 gnu_expr
860 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
862 /* If the initial expression has side effects, we might
863 still have an unstabilized version at this point (for
864 instance if it involves a function call). Wrap the
865 result into a SAVE_EXPR now, in case it happens to be
866 referenced several times. */
867 if (expr_has_side_effects && ! stabilized)
868 gnu_expr = save_expr (gnu_expr);
870 gnu_size = NULL_TREE;
871 used_by_ref = true;
876 /* If this is an aliased object whose nominal subtype is unconstrained,
877 the object is a record that contains both the template and
878 the object. If there is an initializer, it will have already
879 been converted to the right type, but we need to create the
880 template if there is no initializer. */
881 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
882 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
883 /* Beware that padding might have been introduced
884 via maybe_pad_type above. */
885 || (TYPE_IS_PADDING_P (gnu_type)
886 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
887 == RECORD_TYPE
888 && TYPE_CONTAINS_TEMPLATE_P
889 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
890 && !gnu_expr)
892 tree template_field
893 = TYPE_IS_PADDING_P (gnu_type)
894 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
895 : TYPE_FIELDS (gnu_type);
897 gnu_expr
898 = gnat_build_constructor
899 (gnu_type,
900 tree_cons
901 (template_field,
902 build_template (TREE_TYPE (template_field),
903 TREE_TYPE (TREE_CHAIN (template_field)),
904 NULL_TREE),
905 NULL_TREE));
908 /* If this is a pointer and it does not have an initializing
909 expression, initialize it to NULL, unless the object is
910 imported. */
911 if (definition
912 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
913 && !Is_Imported (gnat_entity) && !gnu_expr)
914 gnu_expr = integer_zero_node;
916 /* If we are defining the object and it has an Address clause we must
917 get the address expression from the saved GCC tree for the
918 object if the object has a Freeze_Node. Otherwise, we elaborate
919 the address expression here since the front-end has guaranteed
920 in that case that the elaboration has no effects. Note that
921 only the latter mechanism is currently in use. */
922 if (definition && Present (Address_Clause (gnat_entity)))
924 tree gnu_address
925 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
926 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
928 save_gnu_tree (gnat_entity, NULL_TREE, false);
930 /* Ignore the size. It's either meaningless or was handled
931 above. */
932 gnu_size = NULL_TREE;
933 gnu_type = build_reference_type (gnu_type);
934 gnu_address = convert (gnu_type, gnu_address);
935 used_by_ref = true;
936 const_flag = !Is_Public (gnat_entity);
938 /* If we don't have an initializing expression for the underlying
939 variable, the initializing expression for the pointer is the
940 specified address. Otherwise, we have to make a COMPOUND_EXPR
941 to assign both the address and the initial value. */
942 if (!gnu_expr)
943 gnu_expr = gnu_address;
944 else
945 gnu_expr
946 = build2 (COMPOUND_EXPR, gnu_type,
947 build_binary_op
948 (MODIFY_EXPR, NULL_TREE,
949 build_unary_op (INDIRECT_REF, NULL_TREE,
950 gnu_address),
951 gnu_expr),
952 gnu_address);
955 /* If it has an address clause and we are not defining it, mark it
956 as an indirect object. Likewise for Stdcall objects that are
957 imported. */
958 if ((!definition && Present (Address_Clause (gnat_entity)))
959 || (Is_Imported (gnat_entity)
960 && Has_Stdcall_Convention (gnat_entity)))
962 gnu_type = build_reference_type (gnu_type);
963 gnu_size = NULL_TREE;
965 gnu_expr = NULL_TREE;
966 /* No point in taking the address of an initializing expression
967 that isn't going to be used. */
969 used_by_ref = true;
972 /* If we are at top level and this object is of variable size,
973 make the actual type a hidden pointer to the real type and
974 make the initializer be a memory allocation and initialization.
975 Likewise for objects we aren't defining (presumed to be
976 external references from other packages), but there we do
977 not set up an initialization.
979 If the object's size overflows, make an allocator too, so that
980 Storage_Error gets raised. Note that we will never free
981 such memory, so we presume it never will get allocated. */
983 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
984 global_bindings_p () || !definition
985 || static_p)
986 || (gnu_size
987 && ! allocatable_size_p (gnu_size,
988 global_bindings_p () || !definition
989 || static_p)))
991 gnu_type = build_reference_type (gnu_type);
992 gnu_size = NULL_TREE;
993 used_by_ref = true;
994 const_flag = true;
996 /* In case this was a aliased object whose nominal subtype is
997 unconstrained, the pointer above will be a thin pointer and
998 build_allocator will automatically make the template.
1000 If we have a template initializer only (that we made above),
1001 pretend there is none and rely on what build_allocator creates
1002 again anyway. Otherwise (if we have a full initializer), get
1003 the data part and feed that to build_allocator.
1005 If we are elaborating a mutable object, tell build_allocator to
1006 ignore a possibly simpler size from the initializer, if any, as
1007 we must allocate the maximum possible size in this case. */
1009 if (definition)
1011 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1013 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1014 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1016 gnu_alloc_type
1017 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1019 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1020 && 1 == VEC_length (constructor_elt,
1021 CONSTRUCTOR_ELTS (gnu_expr)))
1022 gnu_expr = 0;
1023 else
1024 gnu_expr
1025 = build_component_ref
1026 (gnu_expr, NULL_TREE,
1027 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1028 false);
1031 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1032 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1033 && !Is_Imported (gnat_entity))
1034 post_error ("Storage_Error will be raised at run-time?",
1035 gnat_entity);
1037 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1038 0, 0, gnat_entity, mutable_p);
1040 else
1042 gnu_expr = NULL_TREE;
1043 const_flag = false;
1047 /* If this object would go into the stack and has an alignment
1048 larger than the default largest alignment, make a variable
1049 to hold the "aligning type" with a modified initial value,
1050 if any, then point to it and make that the value of this
1051 variable, which is now indirect. */
1052 if (!global_bindings_p () && !static_p && definition
1053 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1055 tree gnu_new_type
1056 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1057 TYPE_SIZE_UNIT (gnu_type));
1058 tree gnu_new_var;
1060 gnu_new_var
1061 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1062 NULL_TREE, gnu_new_type, NULL_TREE, false,
1063 false, false, false, NULL, gnat_entity);
1065 if (gnu_expr)
1066 add_stmt_with_node
1067 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1068 build_component_ref
1069 (gnu_new_var, NULL_TREE,
1070 TYPE_FIELDS (gnu_new_type), false),
1071 gnu_expr),
1072 gnat_entity);
1074 gnu_type = build_reference_type (gnu_type);
1075 gnu_expr
1076 = build_unary_op
1077 (ADDR_EXPR, gnu_type,
1078 build_component_ref (gnu_new_var, NULL_TREE,
1079 TYPE_FIELDS (gnu_new_type), false));
1081 gnu_size = NULL_TREE;
1082 used_by_ref = true;
1083 const_flag = true;
1086 if (const_flag)
1087 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1088 | TYPE_QUAL_CONST));
1090 /* Convert the expression to the type of the object except in the
1091 case where the object's type is unconstrained or the object's type
1092 is a padded record whose field is of self-referential size. In
1093 the former case, converting will generate unnecessary evaluations
1094 of the CONSTRUCTOR to compute the size and in the latter case, we
1095 want to only copy the actual data. */
1096 if (gnu_expr
1097 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1098 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1099 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1100 && TYPE_IS_PADDING_P (gnu_type)
1101 && (CONTAINS_PLACEHOLDER_P
1102 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1103 gnu_expr = convert (gnu_type, gnu_expr);
1105 /* If this name is external or there was a name specified, use it,
1106 unless this is a VMS exception object since this would conflict
1107 with the symbol we need to export in addition. Don't use the
1108 Interface_Name if there is an address clause (see CD30005). */
1109 if (!Is_VMS_Exception (gnat_entity)
1110 && ((Present (Interface_Name (gnat_entity))
1111 && No (Address_Clause (gnat_entity)))
1112 || (Is_Public (gnat_entity)
1113 && (!Is_Imported (gnat_entity)
1114 || Is_Exported (gnat_entity)))))
1115 gnu_ext_name = create_concat_name (gnat_entity, 0);
1117 /* If this is constant initialized to a static constant and the
1118 object has an aggregate type, force it to be statically
1119 allocated. */
1120 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1121 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1122 && (AGGREGATE_TYPE_P (gnu_type)
1123 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1124 && TYPE_IS_PADDING_P (gnu_type))))
1125 static_p = true;
1127 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1128 gnu_expr, const_flag,
1129 Is_Public (gnat_entity),
1130 imported_p || !definition,
1131 static_p, attr_list, gnat_entity);
1132 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1133 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1134 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1136 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1137 DECL_RENAMING_GLOBAL_P (gnu_decl) = global_bindings_p ();
1140 /* If we have an address clause and we've made this indirect, it's
1141 not enough to merely mark the type as volatile since volatile
1142 references only conflict with other volatile references while this
1143 reference must conflict with all other references. So ensure that
1144 the dereferenced value has alias set 0. */
1145 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1146 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1148 if (definition && DECL_SIZE (gnu_decl)
1149 && get_block_jmpbuf_decl ()
1150 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1151 || (flag_stack_check && !STACK_CHECK_BUILTIN
1152 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1153 STACK_CHECK_MAX_VAR_SIZE))))
1154 add_stmt_with_node (build_call_1_expr
1155 (update_setjmp_buf_decl,
1156 build_unary_op (ADDR_EXPR, NULL_TREE,
1157 get_block_jmpbuf_decl ())),
1158 gnat_entity);
1160 /* If this is a public constant or we're not optimizing and we're not
1161 making a VAR_DECL for it, make one just for export or debugger
1162 use. Likewise if the address is taken or if the object or type is
1163 aliased. */
1164 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1165 && (Is_Public (gnat_entity)
1166 || optimize == 0
1167 || Address_Taken (gnat_entity)
1168 || Is_Aliased (gnat_entity)
1169 || Is_Aliased (Etype (gnat_entity))))
1171 tree gnu_corr_var
1172 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1173 gnu_expr, false, Is_Public (gnat_entity),
1174 false, static_p, NULL, gnat_entity);
1176 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1179 /* If this is declared in a block that contains a block with an
1180 exception handler, we must force this variable in memory to
1181 suppress an invalid optimization. */
1182 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1183 && Exception_Mechanism != Back_End_Exceptions)
1184 TREE_ADDRESSABLE (gnu_decl) = 1;
1186 /* Back-annotate the Alignment of the object if not already in the
1187 tree. Likewise for Esize if the object is of a constant size.
1188 But if the "object" is actually a pointer to an object, the
1189 alignment and size are the same as the type, so don't back-annotate
1190 the values for the pointer. */
1191 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1192 Set_Alignment (gnat_entity,
1193 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1195 if (!used_by_ref && Unknown_Esize (gnat_entity)
1196 && DECL_SIZE (gnu_decl))
1198 tree gnu_back_size = DECL_SIZE (gnu_decl);
1200 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1201 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1202 gnu_back_size
1203 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1204 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1206 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1209 break;
1211 case E_Void:
1212 /* Return a TYPE_DECL for "void" that we previously made. */
1213 gnu_decl = void_type_decl_node;
1214 break;
1216 case E_Enumeration_Type:
1217 /* A special case, for the types Character and Wide_Character in
1218 Standard, we do not list all the literals. So if the literals
1219 are not specified, make this an unsigned type. */
1220 if (No (First_Literal (gnat_entity)))
1222 gnu_type = make_unsigned_type (esize);
1223 break;
1226 /* Normal case of non-character type, or non-Standard character type */
1228 /* Here we have a list of enumeral constants in First_Literal.
1229 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1230 the list to be places into TYPE_FIELDS. Each node in the list
1231 is a TREE_LIST node whose TREE_VALUE is the literal name
1232 and whose TREE_PURPOSE is the value of the literal.
1234 Esize contains the number of bits needed to represent the enumeral
1235 type, Type_Low_Bound also points to the first literal and
1236 Type_High_Bound points to the last literal. */
1238 Entity_Id gnat_literal;
1239 tree gnu_literal_list = NULL_TREE;
1241 if (Is_Unsigned_Type (gnat_entity))
1242 gnu_type = make_unsigned_type (esize);
1243 else
1244 gnu_type = make_signed_type (esize);
1246 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1248 for (gnat_literal = First_Literal (gnat_entity);
1249 Present (gnat_literal);
1250 gnat_literal = Next_Literal (gnat_literal))
1252 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1253 gnu_type);
1254 tree gnu_literal
1255 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1256 gnu_type, gnu_value, true, false, false,
1257 false, NULL, gnat_literal);
1259 save_gnu_tree (gnat_literal, gnu_literal, false);
1260 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1261 gnu_value, gnu_literal_list);
1264 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1266 /* Note that the bounds are updated at the end of this function
1267 because to avoid an infinite recursion when we get the bounds of
1268 this type, since those bounds are objects of this type. */
1270 break;
1272 case E_Signed_Integer_Type:
1273 case E_Ordinary_Fixed_Point_Type:
1274 case E_Decimal_Fixed_Point_Type:
1275 /* For integer types, just make a signed type the appropriate number
1276 of bits. */
1277 gnu_type = make_signed_type (esize);
1278 break;
1280 case E_Modular_Integer_Type:
1281 /* For modular types, make the unsigned type of the proper number of
1282 bits and then set up the modulus, if required. */
1284 enum machine_mode mode;
1285 tree gnu_modulus;
1286 tree gnu_high = 0;
1288 if (Is_Packed_Array_Type (gnat_entity))
1289 esize = UI_To_Int (RM_Size (gnat_entity));
1291 /* Find the smallest mode at least ESIZE bits wide and make a class
1292 using that mode. */
1294 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1295 GET_MODE_BITSIZE (mode) < esize;
1296 mode = GET_MODE_WIDER_MODE (mode))
1299 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1300 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1301 = Is_Packed_Array_Type (gnat_entity);
1303 /* Get the modulus in this type. If it overflows, assume it is because
1304 it is equal to 2**Esize. Note that there is no overflow checking
1305 done on unsigned type, so we detect the overflow by looking for
1306 a modulus of zero, which is otherwise invalid. */
1307 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1309 if (!integer_zerop (gnu_modulus))
1311 TYPE_MODULAR_P (gnu_type) = 1;
1312 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1313 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1314 convert (gnu_type, integer_one_node)));
1317 /* If we have to set TYPE_PRECISION different from its natural value,
1318 make a subtype to do do. Likewise if there is a modulus and
1319 it is not one greater than TYPE_MAX_VALUE. */
1320 if (TYPE_PRECISION (gnu_type) != esize
1321 || (TYPE_MODULAR_P (gnu_type)
1322 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1324 tree gnu_subtype = make_node (INTEGER_TYPE);
1326 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1327 TREE_TYPE (gnu_subtype) = gnu_type;
1328 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1329 TYPE_MAX_VALUE (gnu_subtype)
1330 = TYPE_MODULAR_P (gnu_type)
1331 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1332 TYPE_PRECISION (gnu_subtype) = esize;
1333 TYPE_UNSIGNED (gnu_subtype) = 1;
1334 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1335 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1336 = Is_Packed_Array_Type (gnat_entity);
1337 layout_type (gnu_subtype);
1339 gnu_type = gnu_subtype;
1342 break;
1344 case E_Signed_Integer_Subtype:
1345 case E_Enumeration_Subtype:
1346 case E_Modular_Integer_Subtype:
1347 case E_Ordinary_Fixed_Point_Subtype:
1348 case E_Decimal_Fixed_Point_Subtype:
1350 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1351 that we do not want to call build_range_type since we would
1352 like each subtype node to be distinct. This will be important
1353 when memory aliasing is implemented.
1355 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1356 parent type; this fact is used by the arithmetic conversion
1357 functions.
1359 We elaborate the Ancestor_Subtype if it is not in the current
1360 unit and one of our bounds is non-static. We do this to ensure
1361 consistent naming in the case where several subtypes share the same
1362 bounds by always elaborating the first such subtype first, thus
1363 using its name. */
1365 if (definition == 0
1366 && Present (Ancestor_Subtype (gnat_entity))
1367 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1368 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1369 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1370 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1371 gnu_expr, definition);
1373 gnu_type = make_node (INTEGER_TYPE);
1374 if (Is_Packed_Array_Type (gnat_entity))
1376 esize = UI_To_Int (RM_Size (gnat_entity));
1377 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1380 TYPE_PRECISION (gnu_type) = esize;
1381 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1383 TYPE_MIN_VALUE (gnu_type)
1384 = convert (TREE_TYPE (gnu_type),
1385 elaborate_expression (Type_Low_Bound (gnat_entity),
1386 gnat_entity,
1387 get_identifier ("L"), definition, 1,
1388 Needs_Debug_Info (gnat_entity)));
1390 TYPE_MAX_VALUE (gnu_type)
1391 = convert (TREE_TYPE (gnu_type),
1392 elaborate_expression (Type_High_Bound (gnat_entity),
1393 gnat_entity,
1394 get_identifier ("U"), definition, 1,
1395 Needs_Debug_Info (gnat_entity)));
1397 /* One of the above calls might have caused us to be elaborated,
1398 so don't blow up if so. */
1399 if (present_gnu_tree (gnat_entity))
1401 maybe_present = true;
1402 break;
1405 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1406 = Has_Biased_Representation (gnat_entity);
1408 /* This should be an unsigned type if the lower bound is constant
1409 and non-negative or if the base type is unsigned; a signed type
1410 otherwise. */
1411 TYPE_UNSIGNED (gnu_type)
1412 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1413 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1414 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1415 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1416 || Is_Unsigned_Type (gnat_entity));
1418 layout_type (gnu_type);
1420 /* Inherit our alias set from what we're a subtype of. Subtypes
1421 are not different types and a pointer can designate any instance
1422 within a subtype hierarchy. */
1423 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1425 /* If the type we are dealing with is to represent a packed array,
1426 we need to have the bits left justified on big-endian targets
1427 and right justified on little-endian targets. We also need to
1428 ensure that when the value is read (e.g. for comparison of two
1429 such values), we only get the good bits, since the unused bits
1430 are uninitialized. Both goals are accomplished by wrapping the
1431 modular value in an enclosing struct. */
1432 if (Is_Packed_Array_Type (gnat_entity))
1434 tree gnu_field_type = gnu_type;
1435 tree gnu_field;
1437 TYPE_RM_SIZE_NUM (gnu_field_type)
1438 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1439 gnu_type = make_node (RECORD_TYPE);
1440 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1441 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1442 TYPE_PACKED (gnu_type) = 1;
1444 /* Create a stripped-down declaration of the original type, mainly
1445 for debugging. */
1446 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1447 NULL, true, debug_info_p, gnat_entity);
1449 /* Don't notify the field as "addressable", since we won't be taking
1450 it's address and it would prevent create_field_decl from making a
1451 bitfield. */
1452 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1453 gnu_field_type, gnu_type, 1, 0, 0, 0);
1455 finish_record_type (gnu_type, gnu_field, false, false);
1456 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1457 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1459 copy_alias_set (gnu_type, gnu_field_type);
1462 break;
1464 case E_Floating_Point_Type:
1465 /* If this is a VAX floating-point type, use an integer of the proper
1466 size. All the operations will be handled with ASM statements. */
1467 if (Vax_Float (gnat_entity))
1469 gnu_type = make_signed_type (esize);
1470 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1471 SET_TYPE_DIGITS_VALUE (gnu_type,
1472 UI_To_gnu (Digits_Value (gnat_entity),
1473 sizetype));
1474 break;
1477 /* The type of the Low and High bounds can be our type if this is
1478 a type from Standard, so set them at the end of the function. */
1479 gnu_type = make_node (REAL_TYPE);
1480 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1481 layout_type (gnu_type);
1482 break;
1484 case E_Floating_Point_Subtype:
1485 if (Vax_Float (gnat_entity))
1487 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1488 break;
1492 if (definition == 0
1493 && Present (Ancestor_Subtype (gnat_entity))
1494 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1495 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1496 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1497 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1498 gnu_expr, definition);
1500 gnu_type = make_node (REAL_TYPE);
1501 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1502 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1504 TYPE_MIN_VALUE (gnu_type)
1505 = convert (TREE_TYPE (gnu_type),
1506 elaborate_expression (Type_Low_Bound (gnat_entity),
1507 gnat_entity, get_identifier ("L"),
1508 definition, 1,
1509 Needs_Debug_Info (gnat_entity)));
1511 TYPE_MAX_VALUE (gnu_type)
1512 = convert (TREE_TYPE (gnu_type),
1513 elaborate_expression (Type_High_Bound (gnat_entity),
1514 gnat_entity, get_identifier ("U"),
1515 definition, 1,
1516 Needs_Debug_Info (gnat_entity)));
1518 /* One of the above calls might have caused us to be elaborated,
1519 so don't blow up if so. */
1520 if (present_gnu_tree (gnat_entity))
1522 maybe_present = true;
1523 break;
1526 layout_type (gnu_type);
1528 /* Inherit our alias set from what we're a subtype of, as for
1529 integer subtypes. */
1530 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1532 break;
1534 /* Array and String Types and Subtypes
1536 Unconstrained array types are represented by E_Array_Type and
1537 constrained array types are represented by E_Array_Subtype. There
1538 are no actual objects of an unconstrained array type; all we have
1539 are pointers to that type.
1541 The following fields are defined on array types and subtypes:
1543 Component_Type Component type of the array.
1544 Number_Dimensions Number of dimensions (an int).
1545 First_Index Type of first index. */
1547 case E_String_Type:
1548 case E_Array_Type:
1550 tree gnu_template_fields = NULL_TREE;
1551 tree gnu_template_type = make_node (RECORD_TYPE);
1552 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1553 tree gnu_fat_type = make_node (RECORD_TYPE);
1554 int ndim = Number_Dimensions (gnat_entity);
1555 int firstdim
1556 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1557 int nextdim
1558 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1559 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1560 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1561 tree gnu_comp_size = 0;
1562 tree gnu_max_size = size_one_node;
1563 tree gnu_max_size_unit;
1564 int index;
1565 Entity_Id gnat_ind_subtype;
1566 Entity_Id gnat_ind_base_subtype;
1567 tree gnu_template_reference;
1568 tree tem;
1570 TYPE_NAME (gnu_template_type)
1571 = create_concat_name (gnat_entity, "XUB");
1572 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1573 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1574 TYPE_READONLY (gnu_template_type) = 1;
1576 /* Make a node for the array. If we are not defining the array
1577 suppress expanding incomplete types. */
1578 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1580 if (!definition)
1581 defer_incomplete_level++, this_deferred = true;
1583 /* Build the fat pointer type. Use a "void *" object instead of
1584 a pointer to the array type since we don't have the array type
1585 yet (it will reference the fat pointer via the bounds). */
1586 tem = chainon (chainon (NULL_TREE,
1587 create_field_decl (get_identifier ("P_ARRAY"),
1588 ptr_void_type_node,
1589 gnu_fat_type, 0, 0, 0, 0)),
1590 create_field_decl (get_identifier ("P_BOUNDS"),
1591 gnu_ptr_template,
1592 gnu_fat_type, 0, 0, 0, 0));
1594 /* Make sure we can put this into a register. */
1595 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1596 finish_record_type (gnu_fat_type, tem, false, true);
1598 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1599 is the fat pointer. This will be used to access the individual
1600 fields once we build them. */
1601 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1602 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1603 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1604 gnu_template_reference
1605 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1606 TREE_READONLY (gnu_template_reference) = 1;
1608 /* Now create the GCC type for each index and add the fields for
1609 that index to the template. */
1610 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1611 gnat_ind_base_subtype
1612 = First_Index (Implementation_Base_Type (gnat_entity));
1613 index < ndim && index >= 0;
1614 index += nextdim,
1615 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1616 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1618 char field_name[10];
1619 tree gnu_ind_subtype
1620 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1621 tree gnu_base_subtype
1622 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1623 tree gnu_base_min
1624 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1625 tree gnu_base_max
1626 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1627 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1629 /* Make the FIELD_DECLs for the minimum and maximum of this
1630 type and then make extractions of that field from the
1631 template. */
1632 sprintf (field_name, "LB%d", index);
1633 gnu_min_field = create_field_decl (get_identifier (field_name),
1634 gnu_ind_subtype,
1635 gnu_template_type, 0, 0, 0, 0);
1636 field_name[0] = 'U';
1637 gnu_max_field = create_field_decl (get_identifier (field_name),
1638 gnu_ind_subtype,
1639 gnu_template_type, 0, 0, 0, 0);
1641 Sloc_to_locus (Sloc (gnat_entity),
1642 &DECL_SOURCE_LOCATION (gnu_min_field));
1643 Sloc_to_locus (Sloc (gnat_entity),
1644 &DECL_SOURCE_LOCATION (gnu_max_field));
1645 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1647 /* We can't use build_component_ref here since the template
1648 type isn't complete yet. */
1649 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1650 gnu_template_reference, gnu_min_field,
1651 NULL_TREE);
1652 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1653 gnu_template_reference, gnu_max_field,
1654 NULL_TREE);
1655 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1657 /* Make a range type with the new ranges, but using
1658 the Ada subtype. Then we convert to sizetype. */
1659 gnu_index_types[index]
1660 = create_index_type (convert (sizetype, gnu_min),
1661 convert (sizetype, gnu_max),
1662 build_range_type (gnu_ind_subtype,
1663 gnu_min, gnu_max));
1664 /* Update the maximum size of the array, in elements. */
1665 gnu_max_size
1666 = size_binop (MULT_EXPR, gnu_max_size,
1667 size_binop (PLUS_EXPR, size_one_node,
1668 size_binop (MINUS_EXPR, gnu_base_max,
1669 gnu_base_min)));
1671 TYPE_NAME (gnu_index_types[index])
1672 = create_concat_name (gnat_entity, field_name);
1675 for (index = 0; index < ndim; index++)
1676 gnu_template_fields
1677 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1679 /* Install all the fields into the template. */
1680 finish_record_type (gnu_template_type, gnu_template_fields,
1681 false, false);
1682 TYPE_READONLY (gnu_template_type) = 1;
1684 /* Now make the array of arrays and update the pointer to the array
1685 in the fat pointer. Note that it is the first field. */
1687 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1689 /* Get and validate any specified Component_Size, but if Packed,
1690 ignore it since the front end will have taken care of it. */
1691 gnu_comp_size
1692 = validate_size (Component_Size (gnat_entity), tem,
1693 gnat_entity,
1694 (Is_Bit_Packed_Array (gnat_entity)
1695 ? TYPE_DECL : VAR_DECL),
1696 true, Has_Component_Size_Clause (gnat_entity));
1698 if (Has_Atomic_Components (gnat_entity))
1699 check_ok_for_atomic (tem, gnat_entity, true);
1701 /* If the component type is a RECORD_TYPE that has a self-referential
1702 size, use the maxium size. */
1703 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1704 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1705 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1707 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1709 tem = make_type_from_size (tem, gnu_comp_size, false);
1710 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1711 "C_PAD", false, definition, true);
1714 if (Has_Volatile_Components (gnat_entity))
1715 tem = build_qualified_type (tem,
1716 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1718 /* If Component_Size is not already specified, annotate it with the
1719 size of the component. */
1720 if (Unknown_Component_Size (gnat_entity))
1721 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1723 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1724 size_binop (MULT_EXPR, gnu_max_size,
1725 TYPE_SIZE_UNIT (tem)));
1726 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1727 size_binop (MULT_EXPR,
1728 convert (bitsizetype,
1729 gnu_max_size),
1730 TYPE_SIZE (tem)));
1732 for (index = ndim - 1; index >= 0; index--)
1734 tem = build_array_type (tem, gnu_index_types[index]);
1735 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1737 /* If the type below this an multi-array type, then this
1738 does not not have aliased components.
1740 ??? Otherwise, for now, we say that any component of aggregate
1741 type is addressable because the front end may take 'Reference
1742 of it. But we have to make it addressable if it must be passed
1743 by reference or it that is the default. */
1744 TYPE_NONALIASED_COMPONENT (tem)
1745 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1746 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1747 : (!Has_Aliased_Components (gnat_entity)
1748 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1751 /* If an alignment is specified, use it if valid. But ignore it for
1752 types that represent the unpacked base type for packed arrays. */
1753 if (No (Packed_Array_Type (gnat_entity))
1754 && Known_Alignment (gnat_entity))
1756 gcc_assert (Present (Alignment (gnat_entity)));
1757 TYPE_ALIGN (tem)
1758 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1759 TYPE_ALIGN (tem));
1762 TYPE_CONVENTION_FORTRAN_P (tem)
1763 = (Convention (gnat_entity) == Convention_Fortran);
1764 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1766 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1767 corresponding fat pointer. */
1768 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1769 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1770 TYPE_MODE (gnu_type) = BLKmode;
1771 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1772 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1774 /* If the maximum size doesn't overflow, use it. */
1775 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1776 && !TREE_OVERFLOW (gnu_max_size))
1777 TYPE_SIZE (tem)
1778 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1779 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1780 && !TREE_OVERFLOW (gnu_max_size_unit))
1781 TYPE_SIZE_UNIT (tem)
1782 = size_binop (MIN_EXPR, gnu_max_size_unit,
1783 TYPE_SIZE_UNIT (tem));
1785 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1786 tem, NULL, !Comes_From_Source (gnat_entity),
1787 debug_info_p, gnat_entity);
1789 /* Create a record type for the object and its template and
1790 set the template at a negative offset. */
1791 tem = build_unc_object_type (gnu_template_type, tem,
1792 create_concat_name (gnat_entity, "XUT"));
1793 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1794 = size_binop (MINUS_EXPR, size_zero_node,
1795 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1796 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1797 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1798 = bitsize_zero_node;
1799 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1800 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1802 /* Give the thin pointer type a name. */
1803 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1804 build_pointer_type (tem), NULL,
1805 !Comes_From_Source (gnat_entity), debug_info_p,
1806 gnat_entity);
1808 break;
1810 case E_String_Subtype:
1811 case E_Array_Subtype:
1813 /* This is the actual data type for array variables. Multidimensional
1814 arrays are implemented in the gnu tree as arrays of arrays. Note
1815 that for the moment arrays which have sparse enumeration subtypes as
1816 index components create sparse arrays, which is obviously space
1817 inefficient but so much easier to code for now.
1819 Also note that the subtype never refers to the unconstrained
1820 array type, which is somewhat at variance with Ada semantics.
1822 First check to see if this is simply a renaming of the array
1823 type. If so, the result is the array type. */
1825 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1826 if (!Is_Constrained (gnat_entity))
1827 break;
1828 else
1830 int index;
1831 int array_dim = Number_Dimensions (gnat_entity);
1832 int first_dim
1833 = ((Convention (gnat_entity) == Convention_Fortran)
1834 ? array_dim - 1 : 0);
1835 int next_dim
1836 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1837 Entity_Id gnat_ind_subtype;
1838 Entity_Id gnat_ind_base_subtype;
1839 tree gnu_base_type = gnu_type;
1840 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1841 tree gnu_comp_size = NULL_TREE;
1842 tree gnu_max_size = size_one_node;
1843 tree gnu_max_size_unit;
1844 bool need_index_type_struct = false;
1845 bool max_overflow = false;
1847 /* First create the gnu types for each index. Create types for
1848 debugging information to point to the index types if the
1849 are not integer types, have variable bounds, or are
1850 wider than sizetype. */
1852 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1853 gnat_ind_base_subtype
1854 = First_Index (Implementation_Base_Type (gnat_entity));
1855 index < array_dim && index >= 0;
1856 index += next_dim,
1857 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1858 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1860 tree gnu_index_subtype
1861 = get_unpadded_type (Etype (gnat_ind_subtype));
1862 tree gnu_min
1863 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1864 tree gnu_max
1865 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1866 tree gnu_base_subtype
1867 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1868 tree gnu_base_min
1869 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1870 tree gnu_base_max
1871 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1872 tree gnu_base_type = get_base_type (gnu_base_subtype);
1873 tree gnu_base_base_min
1874 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1875 tree gnu_base_base_max
1876 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1877 tree gnu_high;
1878 tree gnu_this_max;
1880 /* If the minimum and maximum values both overflow in
1881 SIZETYPE, but the difference in the original type
1882 does not overflow in SIZETYPE, ignore the overflow
1883 indications. */
1884 if ((TYPE_PRECISION (gnu_index_subtype)
1885 > TYPE_PRECISION (sizetype)
1886 || TYPE_UNSIGNED (gnu_index_subtype)
1887 != TYPE_UNSIGNED (sizetype))
1888 && TREE_CODE (gnu_min) == INTEGER_CST
1889 && TREE_CODE (gnu_max) == INTEGER_CST
1890 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1891 && (!TREE_OVERFLOW
1892 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1893 TYPE_MAX_VALUE (gnu_index_subtype),
1894 TYPE_MIN_VALUE (gnu_index_subtype))))))
1895 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1896 = TREE_CONSTANT_OVERFLOW (gnu_min)
1897 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1899 /* Similarly, if the range is null, use bounds of 1..0 for
1900 the sizetype bounds. */
1901 else if ((TYPE_PRECISION (gnu_index_subtype)
1902 > TYPE_PRECISION (sizetype)
1903 || TYPE_UNSIGNED (gnu_index_subtype)
1904 != TYPE_UNSIGNED (sizetype))
1905 && TREE_CODE (gnu_min) == INTEGER_CST
1906 && TREE_CODE (gnu_max) == INTEGER_CST
1907 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1908 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1909 TYPE_MIN_VALUE (gnu_index_subtype)))
1910 gnu_min = size_one_node, gnu_max = size_zero_node;
1912 /* Now compute the size of this bound. We need to provide
1913 GCC with an upper bound to use but have to deal with the
1914 "superflat" case. There are three ways to do this. If we
1915 can prove that the array can never be superflat, we can
1916 just use the high bound of the index subtype. If we can
1917 prove that the low bound minus one can't overflow, we
1918 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1919 the expression hb >= lb ? hb : lb - 1. */
1920 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1922 /* See if the base array type is already flat. If it is, we
1923 are probably compiling an ACVC test, but it will cause the
1924 code below to malfunction if we don't handle it specially. */
1925 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1926 && TREE_CODE (gnu_base_max) == INTEGER_CST
1927 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1928 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1929 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1930 gnu_high = size_zero_node, gnu_min = size_one_node;
1932 /* If gnu_high is now an integer which overflowed, the array
1933 cannot be superflat. */
1934 else if (TREE_CODE (gnu_high) == INTEGER_CST
1935 && TREE_OVERFLOW (gnu_high))
1936 gnu_high = gnu_max;
1937 else if (TYPE_UNSIGNED (gnu_base_subtype)
1938 || TREE_CODE (gnu_high) == INTEGER_CST)
1939 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1940 else
1941 gnu_high
1942 = build_cond_expr
1943 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1944 gnu_max, gnu_min),
1945 gnu_max, gnu_high);
1947 gnu_index_type[index]
1948 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1950 /* Also compute the maximum size of the array. Here we
1951 see if any constraint on the index type of the base type
1952 can be used in the case of self-referential bound on
1953 the index type of the subtype. We look for a non-"infinite"
1954 and non-self-referential bound from any type involved and
1955 handle each bound separately. */
1957 if ((TREE_CODE (gnu_min) == INTEGER_CST
1958 && !TREE_OVERFLOW (gnu_min)
1959 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1960 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1961 gnu_base_min = gnu_min;
1963 if ((TREE_CODE (gnu_max) == INTEGER_CST
1964 && !TREE_OVERFLOW (gnu_max)
1965 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1966 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1967 gnu_base_max = gnu_max;
1969 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1970 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1971 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1972 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1973 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1974 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1975 max_overflow = true;
1977 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1978 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1980 gnu_this_max
1981 = size_binop (MAX_EXPR,
1982 size_binop (PLUS_EXPR, size_one_node,
1983 size_binop (MINUS_EXPR, gnu_base_max,
1984 gnu_base_min)),
1985 size_zero_node);
1987 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1988 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1989 max_overflow = true;
1991 gnu_max_size
1992 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1994 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1995 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1996 != INTEGER_CST)
1997 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1998 || (TREE_TYPE (gnu_index_subtype)
1999 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2000 != INTEGER_TYPE))
2001 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2002 || (TYPE_PRECISION (gnu_index_subtype)
2003 > TYPE_PRECISION (sizetype)))
2004 need_index_type_struct = true;
2007 /* Then flatten: create the array of arrays. */
2009 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2011 /* One of the above calls might have caused us to be elaborated,
2012 so don't blow up if so. */
2013 if (present_gnu_tree (gnat_entity))
2015 maybe_present = true;
2016 break;
2019 /* Get and validate any specified Component_Size, but if Packed,
2020 ignore it since the front end will have taken care of it. */
2021 gnu_comp_size
2022 = validate_size (Component_Size (gnat_entity), gnu_type,
2023 gnat_entity,
2024 (Is_Bit_Packed_Array (gnat_entity)
2025 ? TYPE_DECL : VAR_DECL),
2026 true, Has_Component_Size_Clause (gnat_entity));
2028 /* If the component type is a RECORD_TYPE that has a self-referential
2029 size, use the maxium size. */
2030 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
2031 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2032 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2034 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
2036 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
2037 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2038 gnat_entity, "C_PAD", false,
2039 definition, true);
2042 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2043 gnu_type = build_qualified_type (gnu_type,
2044 (TYPE_QUALS (gnu_type)
2045 | TYPE_QUAL_VOLATILE));
2047 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2048 TYPE_SIZE_UNIT (gnu_type));
2049 gnu_max_size = size_binop (MULT_EXPR,
2050 convert (bitsizetype, gnu_max_size),
2051 TYPE_SIZE (gnu_type));
2053 for (index = array_dim - 1; index >= 0; index --)
2055 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2056 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2057 /* If the type below this an multi-array type, then this
2058 does not not have aliased components.
2060 ??? Otherwise, for now, we say that any component of aggregate
2061 type is addressable because the front end may take 'Reference
2062 of it. But we have to make it addressable if it must be passed
2063 by reference or it that is the default. */
2064 TYPE_NONALIASED_COMPONENT (gnu_type)
2065 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2066 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
2067 : (!Has_Aliased_Components (gnat_entity)
2068 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
2071 /* If we are at file level and this is a multi-dimensional array, we
2072 need to make a variable corresponding to the stride of the
2073 inner dimensions. */
2074 if (global_bindings_p () && array_dim > 1)
2076 tree gnu_str_name = get_identifier ("ST");
2077 tree gnu_arr_type;
2079 for (gnu_arr_type = TREE_TYPE (gnu_type);
2080 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2081 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2082 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2084 tree eltype = TREE_TYPE (gnu_arr_type);
2086 TYPE_SIZE (gnu_arr_type)
2087 = elaborate_expression_1 (gnat_entity, gnat_entity,
2088 TYPE_SIZE (gnu_arr_type),
2089 gnu_str_name, definition, 0);
2091 /* ??? For now, store the size as a multiple of the
2092 alignment of the element type in bytes so that we
2093 can see the alignment from the tree. */
2094 TYPE_SIZE_UNIT (gnu_arr_type)
2095 = build_binary_op
2096 (MULT_EXPR, sizetype,
2097 elaborate_expression_1
2098 (gnat_entity, gnat_entity,
2099 build_binary_op (EXACT_DIV_EXPR, sizetype,
2100 TYPE_SIZE_UNIT (gnu_arr_type),
2101 size_int (TYPE_ALIGN (eltype)
2102 / BITS_PER_UNIT)),
2103 concat_id_with_name (gnu_str_name, "A_U"),
2104 definition, 0),
2105 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2109 /* If we need to write out a record type giving the names of
2110 the bounds, do it now. */
2111 if (need_index_type_struct && debug_info_p)
2113 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2114 tree gnu_field_list = NULL_TREE;
2115 tree gnu_field;
2117 TYPE_NAME (gnu_bound_rec_type)
2118 = create_concat_name (gnat_entity, "XA");
2120 for (index = array_dim - 1; index >= 0; index--)
2122 tree gnu_type_name
2123 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2125 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2126 gnu_type_name = DECL_NAME (gnu_type_name);
2128 gnu_field = create_field_decl (gnu_type_name,
2129 integer_type_node,
2130 gnu_bound_rec_type,
2131 0, NULL_TREE, NULL_TREE, 0);
2132 TREE_CHAIN (gnu_field) = gnu_field_list;
2133 gnu_field_list = gnu_field;
2136 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2137 false, false);
2140 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2141 = (Convention (gnat_entity) == Convention_Fortran);
2142 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2143 = Is_Packed_Array_Type (gnat_entity);
2145 /* If our size depends on a placeholder and the maximum size doesn't
2146 overflow, use it. */
2147 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2148 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2149 && TREE_OVERFLOW (gnu_max_size))
2150 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2151 && TREE_OVERFLOW (gnu_max_size_unit))
2152 && !max_overflow)
2154 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2155 TYPE_SIZE (gnu_type));
2156 TYPE_SIZE_UNIT (gnu_type)
2157 = size_binop (MIN_EXPR, gnu_max_size_unit,
2158 TYPE_SIZE_UNIT (gnu_type));
2161 /* Set our alias set to that of our base type. This gives all
2162 array subtypes the same alias set. */
2163 copy_alias_set (gnu_type, gnu_base_type);
2166 /* If this is a packed type, make this type the same as the packed
2167 array type, but do some adjusting in the type first. */
2169 if (Present (Packed_Array_Type (gnat_entity)))
2171 Entity_Id gnat_index;
2172 tree gnu_inner_type;
2174 /* First finish the type we had been making so that we output
2175 debugging information for it */
2176 gnu_type
2177 = build_qualified_type (gnu_type,
2178 (TYPE_QUALS (gnu_type)
2179 | (TYPE_QUAL_VOLATILE
2180 * Treat_As_Volatile (gnat_entity))));
2181 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2182 !Comes_From_Source (gnat_entity),
2183 debug_info_p, gnat_entity);
2184 if (!Comes_From_Source (gnat_entity))
2185 DECL_ARTIFICIAL (gnu_decl) = 1;
2187 /* Save it as our equivalent in case the call below elaborates
2188 this type again. */
2189 save_gnu_tree (gnat_entity, gnu_decl, false);
2191 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2192 NULL_TREE, 0);
2193 this_made_decl = true;
2194 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2195 save_gnu_tree (gnat_entity, NULL_TREE, false);
2197 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2198 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2199 || TYPE_IS_PADDING_P (gnu_inner_type)))
2200 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2202 /* We need to point the type we just made to our index type so
2203 the actual bounds can be put into a template. */
2205 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2206 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2207 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2208 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2210 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2212 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2213 If it is, we need to make another type. */
2214 if (TYPE_MODULAR_P (gnu_inner_type))
2216 tree gnu_subtype;
2218 gnu_subtype = make_node (INTEGER_TYPE);
2220 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2221 TYPE_MIN_VALUE (gnu_subtype)
2222 = TYPE_MIN_VALUE (gnu_inner_type);
2223 TYPE_MAX_VALUE (gnu_subtype)
2224 = TYPE_MAX_VALUE (gnu_inner_type);
2225 TYPE_PRECISION (gnu_subtype)
2226 = TYPE_PRECISION (gnu_inner_type);
2227 TYPE_UNSIGNED (gnu_subtype)
2228 = TYPE_UNSIGNED (gnu_inner_type);
2229 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2230 layout_type (gnu_subtype);
2232 gnu_inner_type = gnu_subtype;
2235 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2238 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2240 for (gnat_index = First_Index (gnat_entity);
2241 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2242 SET_TYPE_ACTUAL_BOUNDS
2243 (gnu_inner_type,
2244 tree_cons (NULL_TREE,
2245 get_unpadded_type (Etype (gnat_index)),
2246 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2248 if (Convention (gnat_entity) != Convention_Fortran)
2249 SET_TYPE_ACTUAL_BOUNDS
2250 (gnu_inner_type,
2251 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2253 if (TREE_CODE (gnu_type) == RECORD_TYPE
2254 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2255 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2259 /* Abort if packed array with no packed array type field set. */
2260 else
2261 gcc_assert (!Is_Packed (gnat_entity));
2263 break;
2265 case E_String_Literal_Subtype:
2266 /* Create the type for a string literal. */
2268 Entity_Id gnat_full_type
2269 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2270 && Present (Full_View (Etype (gnat_entity)))
2271 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2272 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2273 tree gnu_string_array_type
2274 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2275 tree gnu_string_index_type
2276 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2277 (TYPE_DOMAIN (gnu_string_array_type))));
2278 tree gnu_lower_bound
2279 = convert (gnu_string_index_type,
2280 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2281 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2282 tree gnu_length = ssize_int (length - 1);
2283 tree gnu_upper_bound
2284 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2285 gnu_lower_bound,
2286 convert (gnu_string_index_type, gnu_length));
2287 tree gnu_range_type
2288 = build_range_type (gnu_string_index_type,
2289 gnu_lower_bound, gnu_upper_bound);
2290 tree gnu_index_type
2291 = create_index_type (convert (sizetype,
2292 TYPE_MIN_VALUE (gnu_range_type)),
2293 convert (sizetype,
2294 TYPE_MAX_VALUE (gnu_range_type)),
2295 gnu_range_type);
2297 gnu_type
2298 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2299 gnu_index_type);
2300 copy_alias_set (gnu_type, gnu_string_type);
2302 break;
2304 /* Record Types and Subtypes
2306 The following fields are defined on record types:
2308 Has_Discriminants True if the record has discriminants
2309 First_Discriminant Points to head of list of discriminants
2310 First_Entity Points to head of list of fields
2311 Is_Tagged_Type True if the record is tagged
2313 Implementation of Ada records and discriminated records:
2315 A record type definition is transformed into the equivalent of a C
2316 struct definition. The fields that are the discriminants which are
2317 found in the Full_Type_Declaration node and the elements of the
2318 Component_List found in the Record_Type_Definition node. The
2319 Component_List can be a recursive structure since each Variant of
2320 the Variant_Part of the Component_List has a Component_List.
2322 Processing of a record type definition comprises starting the list of
2323 field declarations here from the discriminants and the calling the
2324 function components_to_record to add the rest of the fields from the
2325 component list and return the gnu type node. The function
2326 components_to_record will call itself recursively as it traverses
2327 the tree. */
2329 case E_Record_Type:
2330 if (Has_Complex_Representation (gnat_entity))
2332 gnu_type
2333 = build_complex_type
2334 (get_unpadded_type
2335 (Etype (Defining_Entity
2336 (First (Component_Items
2337 (Component_List
2338 (Type_Definition
2339 (Declaration_Node (gnat_entity)))))))));
2341 break;
2345 Node_Id full_definition = Declaration_Node (gnat_entity);
2346 Node_Id record_definition = Type_Definition (full_definition);
2347 Entity_Id gnat_field;
2348 tree gnu_field;
2349 tree gnu_field_list = NULL_TREE;
2350 tree gnu_get_parent;
2351 int packed = (Is_Packed (gnat_entity) ? 1
2352 : (Component_Alignment (gnat_entity)
2353 == Calign_Storage_Unit) ? -1
2354 : 0);
2355 bool has_rep = Has_Specified_Layout (gnat_entity);
2356 bool all_rep = has_rep;
2357 bool is_extension
2358 = (Is_Tagged_Type (gnat_entity)
2359 && Nkind (record_definition) == N_Derived_Type_Definition);
2361 /* See if all fields have a rep clause. Stop when we find one
2362 that doesn't. */
2363 for (gnat_field = First_Entity (gnat_entity);
2364 Present (gnat_field) && all_rep;
2365 gnat_field = Next_Entity (gnat_field))
2366 if ((Ekind (gnat_field) == E_Component
2367 || Ekind (gnat_field) == E_Discriminant)
2368 && No (Component_Clause (gnat_field)))
2369 all_rep = false;
2371 /* If this is a record extension, go a level further to find the
2372 record definition. Also, verify we have a Parent_Subtype. */
2373 if (is_extension)
2375 if (!type_annotate_only
2376 || Present (Record_Extension_Part (record_definition)))
2377 record_definition = Record_Extension_Part (record_definition);
2379 gcc_assert (type_annotate_only
2380 || Present (Parent_Subtype (gnat_entity)));
2383 /* Make a node for the record. If we are not defining the record,
2384 suppress expanding incomplete types. We use the same RECORD_TYPE
2385 as for a dummy type and reset TYPE_DUMMY_P to show it's no longer
2386 a dummy.
2388 It is very tempting to delay resetting this bit until we are done
2389 with completing the type, e.g. to let possible intermediate
2390 elaboration of access types designating the record know it is not
2391 complete and arrange for update_pointer_to to fix things up later.
2393 It would be wrong, however, because dummy types are expected only
2394 to be created for Ada incomplete or private types, which is not
2395 what we have here. Doing so would make other parts of gigi think
2396 we are dealing with a really incomplete or private type, and have
2397 nasty side effects, typically on the generation of the associated
2398 debugging information. */
2399 gnu_type = make_dummy_type (gnat_entity);
2400 TYPE_DUMMY_P (gnu_type) = 0;
2402 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2403 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2405 TYPE_ALIGN (gnu_type) = 0;
2406 TYPE_PACKED (gnu_type) = packed || has_rep;
2408 if (!definition)
2409 defer_incomplete_level++, this_deferred = true;
2411 /* If both a size and rep clause was specified, put the size in
2412 the record type now so that it can get the proper mode. */
2413 if (has_rep && Known_Esize (gnat_entity))
2414 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2416 /* Always set the alignment here so that it can be used to
2417 set the mode, if it is making the alignment stricter. If
2418 it is invalid, it will be checked again below. If this is to
2419 be Atomic, choose a default alignment of a word unless we know
2420 the size and it's smaller. */
2421 if (Known_Alignment (gnat_entity))
2422 TYPE_ALIGN (gnu_type)
2423 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2424 else if (Is_Atomic (gnat_entity))
2425 TYPE_ALIGN (gnu_type)
2426 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2427 : 1 << (floor_log2 (esize - 1) + 1));
2429 /* If we have a Parent_Subtype, make a field for the parent. If
2430 this record has rep clauses, force the position to zero. */
2431 if (Present (Parent_Subtype (gnat_entity)))
2433 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2434 tree gnu_parent;
2436 /* A major complexity here is that the parent subtype will
2437 reference our discriminants in its Discriminant_Constraint
2438 list. But those must reference the parent component of this
2439 record which is of the parent subtype we have not built yet!
2440 To break the circle we first build a dummy COMPONENT_REF which
2441 represents the "get to the parent" operation and initialize
2442 each of those discriminants to a COMPONENT_REF of the above
2443 dummy parent referencing the corresponding discrimant of the
2444 base type of the parent subtype. */
2445 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2446 build0 (PLACEHOLDER_EXPR, gnu_type),
2447 build_decl (FIELD_DECL, NULL_TREE,
2448 NULL_TREE),
2449 NULL_TREE);
2451 if (Has_Discriminants (gnat_entity))
2452 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2453 Present (gnat_field);
2454 gnat_field = Next_Stored_Discriminant (gnat_field))
2455 if (Present (Corresponding_Discriminant (gnat_field)))
2456 save_gnu_tree
2457 (gnat_field,
2458 build3 (COMPONENT_REF,
2459 get_unpadded_type (Etype (gnat_field)),
2460 gnu_get_parent,
2461 gnat_to_gnu_field_decl (Corresponding_Discriminant
2462 (gnat_field)),
2463 NULL_TREE),
2464 true);
2466 /* Then we build the parent subtype. */
2467 gnu_parent = gnat_to_gnu_type (gnat_parent);
2469 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2470 initially built. The discriminants must reference the fields
2471 of the parent subtype and not those of its base type for the
2472 placeholder machinery to properly work. */
2473 if (Has_Discriminants (gnat_entity))
2474 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2475 Present (gnat_field);
2476 gnat_field = Next_Stored_Discriminant (gnat_field))
2477 if (Present (Corresponding_Discriminant (gnat_field)))
2479 Entity_Id field = Empty;
2480 for (field = First_Stored_Discriminant (gnat_parent);
2481 Present (field);
2482 field = Next_Stored_Discriminant (field))
2483 if (same_discriminant_p (gnat_field, field))
2484 break;
2485 gcc_assert (Present (field));
2486 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2487 = gnat_to_gnu_field_decl (field);
2490 /* The "get to the parent" COMPONENT_REF must be given its
2491 proper type... */
2492 TREE_TYPE (gnu_get_parent) = gnu_parent;
2494 /* ...and reference the _parent field of this record. */
2495 gnu_field_list
2496 = create_field_decl (get_identifier
2497 (Get_Name_String (Name_uParent)),
2498 gnu_parent, gnu_type, 0,
2499 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2500 has_rep ? bitsize_zero_node : 0, 1);
2501 DECL_INTERNAL_P (gnu_field_list) = 1;
2502 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2505 /* Make the fields for the discriminants and put them into the record
2506 unless it's an Unchecked_Union. */
2507 if (Has_Discriminants (gnat_entity))
2508 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2509 Present (gnat_field);
2510 gnat_field = Next_Stored_Discriminant (gnat_field))
2512 /* If this is a record extension and this discriminant
2513 is the renaming of another discriminant, we've already
2514 handled the discriminant above. */
2515 if (Present (Parent_Subtype (gnat_entity))
2516 && Present (Corresponding_Discriminant (gnat_field)))
2517 continue;
2519 gnu_field
2520 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2522 /* Make an expression using a PLACEHOLDER_EXPR from the
2523 FIELD_DECL node just created and link that with the
2524 corresponding GNAT defining identifier. Then add to the
2525 list of fields. */
2526 save_gnu_tree (gnat_field,
2527 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2528 build0 (PLACEHOLDER_EXPR,
2529 DECL_CONTEXT (gnu_field)),
2530 gnu_field, NULL_TREE),
2531 true);
2533 if (!Is_Unchecked_Union (gnat_entity))
2535 TREE_CHAIN (gnu_field) = gnu_field_list;
2536 gnu_field_list = gnu_field;
2540 /* Put the discriminants into the record (backwards), so we can
2541 know the appropriate discriminant to use for the names of the
2542 variants. */
2543 TYPE_FIELDS (gnu_type) = gnu_field_list;
2545 /* Add the listed fields into the record and finish up. */
2546 components_to_record (gnu_type, Component_List (record_definition),
2547 gnu_field_list, packed, definition, NULL,
2548 false, all_rep, this_deferred,
2549 Is_Unchecked_Union (gnat_entity));
2551 if (this_deferred)
2553 debug_deferred = true;
2554 defer_debug_level++;
2556 defer_debug_incomplete_list
2557 = tree_cons (NULL_TREE, gnu_type,
2558 defer_debug_incomplete_list);
2561 /* We used to remove the associations of the discriminants and
2562 _Parent for validity checking, but we may need them if there's
2563 Freeze_Node for a subtype used in this record. */
2565 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2566 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2568 /* If it is a tagged record force the type to BLKmode to insure
2569 that these objects will always be placed in memory. Do the
2570 same thing for limited record types. */
2571 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2572 TYPE_MODE (gnu_type) = BLKmode;
2574 /* If this is a derived type, we must make the alias set of this type
2575 the same as that of the type we are derived from. We assume here
2576 that the other type is already frozen. */
2577 if (Etype (gnat_entity) != gnat_entity
2578 && !(Is_Private_Type (Etype (gnat_entity))
2579 && Full_View (Etype (gnat_entity)) == gnat_entity))
2580 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2582 /* Fill in locations of fields. */
2583 annotate_rep (gnat_entity, gnu_type);
2585 /* If there are any entities in the chain corresponding to
2586 components that we did not elaborate, ensure we elaborate their
2587 types if they are Itypes. */
2588 for (gnat_temp = First_Entity (gnat_entity);
2589 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2590 if ((Ekind (gnat_temp) == E_Component
2591 || Ekind (gnat_temp) == E_Discriminant)
2592 && Is_Itype (Etype (gnat_temp))
2593 && !present_gnu_tree (gnat_temp))
2594 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2596 break;
2598 case E_Class_Wide_Subtype:
2599 /* If an equivalent type is present, that is what we should use.
2600 Otherwise, fall through to handle this like a record subtype
2601 since it may have constraints. */
2603 if (Present (Equivalent_Type (gnat_entity)))
2605 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2606 NULL_TREE, 0);
2607 maybe_present = true;
2608 break;
2611 /* ... fall through ... */
2613 case E_Record_Subtype:
2615 /* If Cloned_Subtype is Present it means this record subtype has
2616 identical layout to that type or subtype and we should use
2617 that GCC type for this one. The front end guarantees that
2618 the component list is shared. */
2619 if (Present (Cloned_Subtype (gnat_entity)))
2621 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2622 NULL_TREE, 0);
2623 maybe_present = true;
2626 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2627 changing the type, make a new type with each field having the
2628 type of the field in the new subtype but having the position
2629 computed by transforming every discriminant reference according
2630 to the constraints. We don't see any difference between
2631 private and nonprivate type here since derivations from types should
2632 have been deferred until the completion of the private type. */
2633 else
2635 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2636 tree gnu_base_type;
2637 tree gnu_orig_type;
2639 if (!definition)
2640 defer_incomplete_level++, this_deferred = true;
2642 /* Get the base type initially for its alignment and sizes. But
2643 if it is a padded type, we do all the other work with the
2644 unpadded type. */
2645 gnu_type = gnu_orig_type = gnu_base_type
2646 = gnat_to_gnu_type (gnat_base_type);
2648 if (TREE_CODE (gnu_type) == RECORD_TYPE
2649 && TYPE_IS_PADDING_P (gnu_type))
2650 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2652 if (present_gnu_tree (gnat_entity))
2654 maybe_present = true;
2655 break;
2658 /* When the type has discriminants, and these discriminants
2659 affect the shape of what it built, factor them in.
2661 If we are making a subtype of an Unchecked_Union (must be an
2662 Itype), just return the type.
2664 We can't just use Is_Constrained because private subtypes without
2665 discriminants of full types with discriminants with default
2666 expressions are Is_Constrained but aren't constrained! */
2668 if (IN (Ekind (gnat_base_type), Record_Kind)
2669 && !Is_For_Access_Subtype (gnat_entity)
2670 && !Is_Unchecked_Union (gnat_base_type)
2671 && Is_Constrained (gnat_entity)
2672 && Stored_Constraint (gnat_entity) != No_Elist
2673 && Present (Discriminant_Constraint (gnat_entity)))
2675 Entity_Id gnat_field;
2676 tree gnu_field_list = 0;
2677 tree gnu_pos_list
2678 = compute_field_positions (gnu_orig_type, NULL_TREE,
2679 size_zero_node, bitsize_zero_node,
2680 BIGGEST_ALIGNMENT);
2681 tree gnu_subst_list
2682 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2683 definition);
2684 tree gnu_temp;
2686 gnu_type = make_node (RECORD_TYPE);
2687 TYPE_NAME (gnu_type) = gnu_entity_id;
2688 TYPE_STUB_DECL (gnu_type)
2689 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2690 gnat_entity);
2691 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2693 for (gnat_field = First_Entity (gnat_entity);
2694 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2695 if ((Ekind (gnat_field) == E_Component
2696 || Ekind (gnat_field) == E_Discriminant)
2697 && (Underlying_Type (Scope (Original_Record_Component
2698 (gnat_field)))
2699 == gnat_base_type)
2700 && (No (Corresponding_Discriminant (gnat_field))
2701 || !Is_Tagged_Type (gnat_base_type)))
2703 tree gnu_old_field
2704 = gnat_to_gnu_field_decl (Original_Record_Component
2705 (gnat_field));
2706 tree gnu_offset
2707 = TREE_VALUE (purpose_member (gnu_old_field,
2708 gnu_pos_list));
2709 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2710 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2711 tree gnu_field_type
2712 = gnat_to_gnu_type (Etype (gnat_field));
2713 tree gnu_size = TYPE_SIZE (gnu_field_type);
2714 tree gnu_new_pos = 0;
2715 unsigned int offset_align
2716 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2718 tree gnu_field;
2720 /* If there was a component clause, the field types must be
2721 the same for the type and subtype, so copy the data from
2722 the old field to avoid recomputation here. Also if the
2723 field is justified modular and the optimization in
2724 gnat_to_gnu_field was applied. */
2725 if (Present (Component_Clause
2726 (Original_Record_Component (gnat_field)))
2727 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2728 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2729 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2730 == TREE_TYPE (gnu_old_field)))
2732 gnu_size = DECL_SIZE (gnu_old_field);
2733 gnu_field_type = TREE_TYPE (gnu_old_field);
2736 /* If this was a bitfield, get the size from the old field.
2737 Also ensure the type can be placed into a bitfield. */
2738 else if (DECL_BIT_FIELD (gnu_old_field))
2740 gnu_size = DECL_SIZE (gnu_old_field);
2741 if (TYPE_MODE (gnu_field_type) == BLKmode
2742 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2743 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2744 gnu_field_type = make_packable_type (gnu_field_type);
2747 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2748 for (gnu_temp = gnu_subst_list;
2749 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2750 gnu_pos = substitute_in_expr (gnu_pos,
2751 TREE_PURPOSE (gnu_temp),
2752 TREE_VALUE (gnu_temp));
2754 /* If the size is now a constant, we can set it as the
2755 size of the field when we make it. Otherwise, we need
2756 to deal with it specially. */
2757 if (TREE_CONSTANT (gnu_pos))
2758 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2760 gnu_field
2761 = create_field_decl
2762 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2763 0, gnu_size, gnu_new_pos,
2764 !DECL_NONADDRESSABLE_P (gnu_old_field));
2766 if (!TREE_CONSTANT (gnu_pos))
2768 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2769 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2770 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2771 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2772 DECL_SIZE (gnu_field) = gnu_size;
2773 DECL_SIZE_UNIT (gnu_field)
2774 = convert (sizetype,
2775 size_binop (CEIL_DIV_EXPR, gnu_size,
2776 bitsize_unit_node));
2777 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2780 DECL_INTERNAL_P (gnu_field)
2781 = DECL_INTERNAL_P (gnu_old_field);
2782 SET_DECL_ORIGINAL_FIELD
2783 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2784 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2785 : gnu_old_field));
2786 DECL_DISCRIMINANT_NUMBER (gnu_field)
2787 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2788 TREE_THIS_VOLATILE (gnu_field)
2789 = TREE_THIS_VOLATILE (gnu_old_field);
2790 TREE_CHAIN (gnu_field) = gnu_field_list;
2791 gnu_field_list = gnu_field;
2792 save_gnu_tree (gnat_field, gnu_field, false);
2795 /* Now go through the entities again looking for Itypes that
2796 we have not elaborated but should (e.g., Etypes of fields
2797 that have Original_Components). */
2798 for (gnat_field = First_Entity (gnat_entity);
2799 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2800 if ((Ekind (gnat_field) == E_Discriminant
2801 || Ekind (gnat_field) == E_Component)
2802 && !present_gnu_tree (Etype (gnat_field)))
2803 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2805 finish_record_type (gnu_type, nreverse (gnu_field_list),
2806 true, false);
2808 /* Now set the size, alignment and alias set of the new type to
2809 match that of the old one, doing any substitutions, as
2810 above. */
2811 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2812 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2813 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2814 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2815 copy_alias_set (gnu_type, gnu_base_type);
2817 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2818 for (gnu_temp = gnu_subst_list;
2819 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2820 TYPE_SIZE (gnu_type)
2821 = substitute_in_expr (TYPE_SIZE (gnu_type),
2822 TREE_PURPOSE (gnu_temp),
2823 TREE_VALUE (gnu_temp));
2825 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2826 for (gnu_temp = gnu_subst_list;
2827 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2828 TYPE_SIZE_UNIT (gnu_type)
2829 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2830 TREE_PURPOSE (gnu_temp),
2831 TREE_VALUE (gnu_temp));
2833 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2834 for (gnu_temp = gnu_subst_list;
2835 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2836 SET_TYPE_ADA_SIZE
2837 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2838 TREE_PURPOSE (gnu_temp),
2839 TREE_VALUE (gnu_temp)));
2841 /* Recompute the mode of this record type now that we know its
2842 actual size. */
2843 compute_record_mode (gnu_type);
2845 /* Fill in locations of fields. */
2846 annotate_rep (gnat_entity, gnu_type);
2849 /* If we've made a new type, record it and make an XVS type to show
2850 what this is a subtype of. Some debuggers require the XVS
2851 type to be output first, so do it in that order. */
2852 if (gnu_type != gnu_orig_type)
2854 if (debug_info_p)
2856 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2857 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2859 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2860 gnu_orig_name = DECL_NAME (gnu_orig_name);
2862 TYPE_NAME (gnu_subtype_marker)
2863 = create_concat_name (gnat_entity, "XVS");
2864 finish_record_type (gnu_subtype_marker,
2865 create_field_decl (gnu_orig_name,
2866 integer_type_node,
2867 gnu_subtype_marker,
2868 0, NULL_TREE,
2869 NULL_TREE, 0),
2870 false, false);
2873 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2874 TYPE_NAME (gnu_type) = gnu_entity_id;
2875 TYPE_STUB_DECL (gnu_type)
2876 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2877 NULL, true, debug_info_p, gnat_entity);
2880 /* Otherwise, go down all the components in the new type and
2881 make them equivalent to those in the base type. */
2882 else
2883 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2884 gnat_temp = Next_Entity (gnat_temp))
2885 if ((Ekind (gnat_temp) == E_Discriminant
2886 && !Is_Unchecked_Union (gnat_base_type))
2887 || Ekind (gnat_temp) == E_Component)
2888 save_gnu_tree (gnat_temp,
2889 gnat_to_gnu_field_decl
2890 (Original_Record_Component (gnat_temp)), false);
2892 break;
2894 case E_Access_Subprogram_Type:
2895 case E_Anonymous_Access_Subprogram_Type:
2896 /* If we are not defining this entity, and we have incomplete
2897 entities being processed above us, make a dummy type and
2898 fill it in later. */
2899 if (!definition && defer_incomplete_level != 0)
2901 struct incomplete *p
2902 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2904 gnu_type
2905 = build_pointer_type
2906 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2907 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2908 !Comes_From_Source (gnat_entity),
2909 debug_info_p, gnat_entity);
2910 save_gnu_tree (gnat_entity, gnu_decl, false);
2911 this_made_decl = saved = true;
2913 p->old_type = TREE_TYPE (gnu_type);
2914 p->full_type = Directly_Designated_Type (gnat_entity);
2915 p->next = defer_incomplete_list;
2916 defer_incomplete_list = p;
2917 break;
2920 /* ... fall through ... */
2922 case E_Allocator_Type:
2923 case E_Access_Type:
2924 case E_Access_Attribute_Type:
2925 case E_Anonymous_Access_Type:
2926 case E_General_Access_Type:
2928 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2929 Entity_Id gnat_desig_full
2930 = ((IN (Ekind (Etype (gnat_desig_type)),
2931 Incomplete_Or_Private_Kind))
2932 ? Full_View (gnat_desig_type) : 0);
2933 /* We want to know if we'll be seeing the freeze node for any
2934 incomplete type we may be pointing to. */
2935 bool in_main_unit
2936 = (Present (gnat_desig_full)
2937 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2938 : In_Extended_Main_Code_Unit (gnat_desig_type));
2939 bool got_fat_p = false;
2940 bool made_dummy = false;
2941 tree gnu_desig_type = NULL_TREE;
2942 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2944 if (!targetm.valid_pointer_mode (p_mode))
2945 p_mode = ptr_mode;
2947 if (No (gnat_desig_full)
2948 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2949 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2950 && Present (Equivalent_Type (gnat_desig_type)))))
2952 if (Present (Equivalent_Type (gnat_desig_type)))
2954 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2955 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2956 gnat_desig_full = Full_View (gnat_desig_full);
2958 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2959 Incomplete_Or_Private_Kind))
2960 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2963 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2964 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2966 /* If either the designated type or its full view is an
2967 unconstrained array subtype, replace it with the type it's a
2968 subtype of. This avoids problems with multiple copies of
2969 unconstrained array types. */
2970 if (Ekind (gnat_desig_type) == E_Array_Subtype
2971 && !Is_Constrained (gnat_desig_type))
2972 gnat_desig_type = Etype (gnat_desig_type);
2973 if (Present (gnat_desig_full)
2974 && Ekind (gnat_desig_full) == E_Array_Subtype
2975 && !Is_Constrained (gnat_desig_full))
2976 gnat_desig_full = Etype (gnat_desig_full);
2978 /* If the designated type is a subtype of an incomplete record type,
2979 use the parent type to avoid order of elaboration issues. This
2980 can lose some code efficiency, but there is no alternative. */
2981 if (Present (gnat_desig_full)
2982 && Ekind (gnat_desig_full) == E_Record_Subtype
2983 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2984 gnat_desig_full = Etype (gnat_desig_full);
2986 /* If we are pointing to an incomplete type whose completion is an
2987 unconstrained array, make a fat pointer type instead of a pointer
2988 to VOID. The two types in our fields will be pointers to VOID and
2989 will be replaced in update_pointer_to. Similarly, if the type
2990 itself is a dummy type or an unconstrained array. Also make
2991 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2992 pointers to it. */
2994 if ((Present (gnat_desig_full)
2995 && Is_Array_Type (gnat_desig_full)
2996 && !Is_Constrained (gnat_desig_full))
2997 || (present_gnu_tree (gnat_desig_type)
2998 && TYPE_IS_DUMMY_P (TREE_TYPE
2999 (get_gnu_tree (gnat_desig_type)))
3000 && Is_Array_Type (gnat_desig_type)
3001 && !Is_Constrained (gnat_desig_type))
3002 || (present_gnu_tree (gnat_desig_type)
3003 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
3004 == UNCONSTRAINED_ARRAY_TYPE)
3005 && !(TYPE_POINTER_TO (TREE_TYPE
3006 (get_gnu_tree (gnat_desig_type)))))
3007 || (No (gnat_desig_full) && !in_main_unit
3008 && defer_incomplete_level
3009 && !present_gnu_tree (gnat_desig_type)
3010 && Is_Array_Type (gnat_desig_type)
3011 && !Is_Constrained (gnat_desig_type)))
3013 tree gnu_old
3014 = (present_gnu_tree (gnat_desig_type)
3015 ? gnat_to_gnu_type (gnat_desig_type)
3016 : make_dummy_type (gnat_desig_type));
3017 tree fields;
3019 /* Show the dummy we get will be a fat pointer. */
3020 got_fat_p = made_dummy = true;
3022 /* If the call above got something that has a pointer, that
3023 pointer is our type. This could have happened either
3024 because the type was elaborated or because somebody
3025 else executed the code below. */
3026 gnu_type = TYPE_POINTER_TO (gnu_old);
3027 if (!gnu_type)
3029 gnu_type = make_node (RECORD_TYPE);
3030 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3031 TYPE_POINTER_TO (gnu_old) = gnu_type;
3033 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3034 fields
3035 = chainon (chainon (NULL_TREE,
3036 create_field_decl
3037 (get_identifier ("P_ARRAY"),
3038 ptr_void_type_node, gnu_type,
3039 0, 0, 0, 0)),
3040 create_field_decl (get_identifier ("P_BOUNDS"),
3041 ptr_void_type_node,
3042 gnu_type, 0, 0, 0, 0));
3044 /* Make sure we can place this into a register. */
3045 TYPE_ALIGN (gnu_type)
3046 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3047 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3048 finish_record_type (gnu_type, fields, false, true);
3050 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3051 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3052 = concat_id_with_name (get_entity_name (gnat_desig_type),
3053 "XUT");
3054 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3058 /* If we already know what the full type is, use it. */
3059 else if (Present (gnat_desig_full)
3060 && present_gnu_tree (gnat_desig_full))
3061 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3063 /* Get the type of the thing we are to point to and build a pointer
3064 to it. If it is a reference to an incomplete or private type with a
3065 full view that is a record, make a dummy type node and get the
3066 actual type later when we have verified it is safe. */
3067 else if (!in_main_unit
3068 && !present_gnu_tree (gnat_desig_type)
3069 && Present (gnat_desig_full)
3070 && !present_gnu_tree (gnat_desig_full)
3071 && Is_Record_Type (gnat_desig_full))
3073 gnu_desig_type = make_dummy_type (gnat_desig_type);
3074 made_dummy = true;
3077 /* Likewise if we are pointing to a record or array and we are to defer
3078 elaborating incomplete types. We do this since this access type
3079 may be the full view of some private type. Note that the
3080 unconstrained array case is handled above. */
3081 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
3082 && !present_gnu_tree (gnat_desig_type)
3083 && ((Is_Record_Type (gnat_desig_type)
3084 || Is_Array_Type (gnat_desig_type))
3085 || (Present (gnat_desig_full)
3086 && (Is_Record_Type (gnat_desig_full)
3087 || Is_Array_Type (gnat_desig_full)))))
3089 gnu_desig_type = make_dummy_type (gnat_desig_type);
3090 made_dummy = true;
3092 else if (gnat_desig_type == gnat_entity)
3094 gnu_type
3095 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3096 p_mode,
3097 No_Strict_Aliasing (gnat_entity));
3098 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3100 else
3101 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3103 /* It is possible that the above call to gnat_to_gnu_type resolved our
3104 type. If so, just return it. */
3105 if (present_gnu_tree (gnat_entity))
3107 maybe_present = true;
3108 break;
3111 /* If we have a GCC type for the designated type, possibly modify it
3112 if we are pointing only to constant objects and then make a pointer
3113 to it. Don't do this for unconstrained arrays. */
3114 if (!gnu_type && gnu_desig_type)
3116 if (Is_Access_Constant (gnat_entity)
3117 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3119 gnu_desig_type
3120 = build_qualified_type
3121 (gnu_desig_type,
3122 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3124 /* Some extra processing is required if we are building a
3125 pointer to an incomplete type (in the GCC sense). We might
3126 have such a type if we just made a dummy, or directly out
3127 of the call to gnat_to_gnu_type above if we are processing
3128 an access type for a record component designating the
3129 record type itself. */
3130 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3132 /* We must ensure that the pointer to variant we make will
3133 be processed by update_pointer_to when the initial type
3134 is completed. Pretend we made a dummy and let further
3135 processing act as usual. */
3136 made_dummy = true;
3138 /* We must ensure that update_pointer_to will not retrieve
3139 the dummy variant when building a properly qualified
3140 version of the complete type. We take advantage of the
3141 fact that get_qualified_type is requiring TYPE_NAMEs to
3142 match to influence build_qualified_type and then also
3143 update_pointer_to here. */
3144 TYPE_NAME (gnu_desig_type)
3145 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3149 gnu_type
3150 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3151 No_Strict_Aliasing (gnat_entity));
3154 /* If we are not defining this object and we made a dummy pointer,
3155 save our current definition, evaluate the actual type, and replace
3156 the tentative type we made with the actual one. If we are to defer
3157 actually looking up the actual type, make an entry in the
3158 deferred list. */
3160 if (!in_main_unit && made_dummy)
3162 tree gnu_old_type
3163 = TYPE_FAT_POINTER_P (gnu_type)
3164 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3166 if (esize == POINTER_SIZE
3167 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3168 gnu_type
3169 = build_pointer_type
3170 (TYPE_OBJECT_RECORD_TYPE
3171 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3173 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3174 !Comes_From_Source (gnat_entity),
3175 debug_info_p, gnat_entity);
3176 save_gnu_tree (gnat_entity, gnu_decl, false);
3177 this_made_decl = saved = true;
3179 if (defer_incomplete_level == 0)
3180 /* Note that the call to gnat_to_gnu_type here might have
3181 updated gnu_old_type directly, in which case it is not a
3182 dummy type any more when we get into update_pointer_to.
3184 This may happen for instance when the designated type is a
3185 record type, because their elaboration starts with an
3186 initial node from make_dummy_type, which may yield the same
3187 node as the one we got.
3189 Besides, variants of this non-dummy type might have been
3190 created along the way. update_pointer_to is expected to
3191 properly take care of those situations. */
3192 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3193 gnat_to_gnu_type (gnat_desig_type));
3194 else
3196 struct incomplete *p
3197 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3199 p->old_type = gnu_old_type;
3200 p->full_type = gnat_desig_type;
3201 p->next = defer_incomplete_list;
3202 defer_incomplete_list = p;
3206 break;
3208 case E_Access_Protected_Subprogram_Type:
3209 case E_Anonymous_Access_Protected_Subprogram_Type:
3210 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3211 gnu_type = build_pointer_type (void_type_node);
3212 else
3213 /* The runtime representation is the equivalent type. */
3214 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3216 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3217 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3218 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3219 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3220 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3221 NULL_TREE, 0);
3223 break;
3225 case E_Access_Subtype:
3227 /* We treat this as identical to its base type; any constraint is
3228 meaningful only to the front end.
3230 The designated type must be elaborated as well, if it does
3231 not have its own freeze node. Designated (sub)types created
3232 for constrained components of records with discriminants are
3233 not frozen by the front end and thus not elaborated by gigi,
3234 because their use may appear before the base type is frozen,
3235 and because it is not clear that they are needed anywhere in
3236 Gigi. With the current model, there is no correct place where
3237 they could be elaborated. */
3239 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3240 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3241 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3242 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3243 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3245 /* If we are not defining this entity, and we have incomplete
3246 entities being processed above us, make a dummy type and
3247 elaborate it later. */
3248 if (!definition && defer_incomplete_level != 0)
3250 struct incomplete *p
3251 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3252 tree gnu_ptr_type
3253 = build_pointer_type
3254 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3256 p->old_type = TREE_TYPE (gnu_ptr_type);
3257 p->full_type = Directly_Designated_Type (gnat_entity);
3258 p->next = defer_incomplete_list;
3259 defer_incomplete_list = p;
3261 else if (IN (Ekind (Base_Type
3262 (Directly_Designated_Type (gnat_entity))),
3263 Incomplete_Or_Private_Kind))
3265 else
3266 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3267 NULL_TREE, 0);
3270 maybe_present = true;
3271 break;
3273 /* Subprogram Entities
3275 The following access functions are defined for subprograms (functions
3276 or procedures):
3278 First_Formal The first formal parameter.
3279 Is_Imported Indicates that the subprogram has appeared in
3280 an INTERFACE or IMPORT pragma. For now we
3281 assume that the external language is C.
3282 Is_Inlined True if the subprogram is to be inlined.
3284 In addition for function subprograms we have:
3286 Etype Return type of the function.
3288 Each parameter is first checked by calling must_pass_by_ref on its
3289 type to determine if it is passed by reference. For parameters which
3290 are copied in, if they are Ada IN OUT or OUT parameters, their return
3291 value becomes part of a record which becomes the return type of the
3292 function (C function - note that this applies only to Ada procedures
3293 so there is no Ada return type). Additional code to store back the
3294 parameters will be generated on the caller side. This transformation
3295 is done here, not in the front-end.
3297 The intended result of the transformation can be seen from the
3298 equivalent source rewritings that follow:
3300 struct temp {int a,b};
3301 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3302 .. ..
3303 end P; return {A,B};
3305 procedure call
3308 temp t;
3309 P(X,Y); t = P(X,Y);
3310 X = t.a , Y = t.b;
3313 For subprogram types we need to perform mainly the same conversions to
3314 GCC form that are needed for procedures and function declarations. The
3315 only difference is that at the end, we make a type declaration instead
3316 of a function declaration. */
3318 case E_Subprogram_Type:
3319 case E_Function:
3320 case E_Procedure:
3322 /* The first GCC parameter declaration (a PARM_DECL node). The
3323 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3324 actually is the head of this parameter list. */
3325 tree gnu_param_list = NULL_TREE;
3326 /* The type returned by a function. If the subprogram is a procedure
3327 this type should be void_type_node. */
3328 tree gnu_return_type = void_type_node;
3329 /* List of fields in return type of procedure with copy in copy out
3330 parameters. */
3331 tree gnu_field_list = NULL_TREE;
3332 /* Non-null for subprograms containing parameters passed by copy in
3333 copy out (Ada IN OUT or OUT parameters not passed by reference),
3334 in which case it is the list of nodes used to specify the values of
3335 the in out/out parameters that are returned as a record upon
3336 procedure return. The TREE_PURPOSE of an element of this list is
3337 a field of the record and the TREE_VALUE is the PARM_DECL
3338 corresponding to that field. This list will be saved in the
3339 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3340 tree gnu_return_list = NULL_TREE;
3341 /* If an import pragma asks to map this subprogram to a GCC builtin,
3342 this is the builtin DECL node. */
3343 tree gnu_builtin_decl = NULL_TREE;
3344 Entity_Id gnat_param;
3345 bool inline_flag = Is_Inlined (gnat_entity);
3346 bool public_flag = Is_Public (gnat_entity);
3347 bool extern_flag
3348 = (Is_Public (gnat_entity) && !definition) || imported_p;
3349 bool pure_flag = Is_Pure (gnat_entity);
3350 bool volatile_flag = No_Return (gnat_entity);
3351 bool returns_by_ref = false;
3352 bool returns_unconstrained = false;
3353 bool returns_by_target_ptr = false;
3354 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3355 bool has_copy_in_out = false;
3356 int parmnum;
3358 if (kind == E_Subprogram_Type && !definition)
3359 /* A parameter may refer to this type, so defer completion
3360 of any incomplete types. */
3361 defer_incomplete_level++, this_deferred = true;
3363 /* If the subprogram has an alias, it is probably inherited, so
3364 we can use the original one. If the original "subprogram"
3365 is actually an enumeration literal, it may be the first use
3366 of its type, so we must elaborate that type now. */
3367 if (Present (Alias (gnat_entity)))
3369 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3370 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3372 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3373 gnu_expr, 0);
3375 /* Elaborate any Itypes in the parameters of this entity. */
3376 for (gnat_temp = First_Formal (gnat_entity);
3377 Present (gnat_temp);
3378 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3379 if (Is_Itype (Etype (gnat_temp)))
3380 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3382 break;
3385 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3386 corresponding DECL node.
3388 We still want the parameter associations to take place because the
3389 proper generation of calls depends on it (a GNAT parameter without
3390 a corresponding GCC tree has a very specific meaning), so we don't
3391 just break here. */
3392 if (Convention (gnat_entity) == Convention_Intrinsic)
3393 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3395 /* ??? What if we don't find the builtin node above ? warn ? err ?
3396 In the current state we neither warn nor err, and calls will just
3397 be handled as for regular subprograms. */
3399 if (kind == E_Function || kind == E_Subprogram_Type)
3400 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3402 /* If this function returns by reference, make the actual
3403 return type of this function the pointer and mark the decl. */
3404 if (Returns_By_Ref (gnat_entity))
3406 returns_by_ref = true;
3407 gnu_return_type = build_pointer_type (gnu_return_type);
3410 /* If the Mechanism is By_Reference, ensure the return type uses
3411 the machine's by-reference mechanism, which may not the same
3412 as above (e.g., it might be by passing a fake parameter). */
3413 else if (kind == E_Function
3414 && Mechanism (gnat_entity) == By_Reference)
3416 gnu_return_type = copy_type (gnu_return_type);
3417 TREE_ADDRESSABLE (gnu_return_type) = 1;
3420 /* If we are supposed to return an unconstrained array,
3421 actually return a fat pointer and make a note of that. Return
3422 a pointer to an unconstrained record of variable size. */
3423 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3425 gnu_return_type = TREE_TYPE (gnu_return_type);
3426 returns_unconstrained = true;
3429 /* If the type requires a transient scope, the result is allocated
3430 on the secondary stack, so the result type of the function is
3431 just a pointer. */
3432 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3434 gnu_return_type = build_pointer_type (gnu_return_type);
3435 returns_unconstrained = true;
3438 /* If the type is a padded type and the underlying type would not
3439 be passed by reference or this function has a foreign convention,
3440 return the underlying type. */
3441 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3442 && TYPE_IS_PADDING_P (gnu_return_type)
3443 && (!default_pass_by_ref (TREE_TYPE
3444 (TYPE_FIELDS (gnu_return_type)))
3445 || Has_Foreign_Convention (gnat_entity)))
3446 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3448 /* If the return type is unconstrained, that means it must have a
3449 maximum size. We convert the function into a procedure and its
3450 caller will pass a pointer to an object of that maximum size as the
3451 first parameter when we call the function. */
3452 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3454 returns_by_target_ptr = true;
3455 gnu_param_list
3456 = create_param_decl (get_identifier ("TARGET"),
3457 build_reference_type (gnu_return_type),
3458 true);
3459 gnu_return_type = void_type_node;
3462 /* If the return type has a size that overflows, we cannot have
3463 a function that returns that type. This usage doesn't make
3464 sense anyway, so give an error here. */
3465 if (TYPE_SIZE_UNIT (gnu_return_type)
3466 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3467 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3469 post_error ("cannot return type whose size overflows",
3470 gnat_entity);
3471 gnu_return_type = copy_node (gnu_return_type);
3472 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3473 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3474 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3475 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3478 /* Look at all our parameters and get the type of
3479 each. While doing this, build a copy-out structure if
3480 we need one. */
3482 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3483 Present (gnat_param);
3484 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3486 tree gnu_param_name = get_entity_name (gnat_param);
3487 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3488 tree gnu_param, gnu_field;
3489 bool by_ref_p = false;
3490 bool by_descr_p = false;
3491 bool by_component_ptr_p = false;
3492 bool copy_in_copy_out_flag = false;
3493 bool req_by_copy = false, req_by_ref = false;
3495 /* Builtins are expanded inline and there is no real call sequence
3496 involved. so the type expected by the underlying expander is
3497 always the type of each argument "as is". */
3498 if (gnu_builtin_decl)
3499 req_by_copy = 1;
3501 /* Otherwise, see if a Mechanism was supplied that forced this
3502 parameter to be passed one way or another. */
3503 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3504 req_by_copy = true;
3505 else if (Mechanism (gnat_param) == Default)
3507 else if (Mechanism (gnat_param) == By_Copy)
3508 req_by_copy = true;
3509 else if (Mechanism (gnat_param) == By_Reference)
3510 req_by_ref = true;
3511 else if (Mechanism (gnat_param) <= By_Descriptor)
3512 by_descr_p = true;
3513 else if (Mechanism (gnat_param) > 0)
3515 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3516 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3517 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3518 Mechanism (gnat_param)))
3519 req_by_ref = true;
3520 else
3521 req_by_copy = true;
3523 else
3524 post_error ("unsupported mechanism for&", gnat_param);
3526 /* If this is either a foreign function or if the
3527 underlying type won't be passed by reference, strip off
3528 possible padding type. */
3529 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3530 && TYPE_IS_PADDING_P (gnu_param_type)
3531 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3532 || (!must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3533 (gnu_param_type)))
3534 && (req_by_copy
3535 || !default_pass_by_ref (TREE_TYPE
3536 (TYPE_FIELDS
3537 (gnu_param_type)))))))
3538 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3540 /* If this is an IN parameter it is read-only, so make a variant
3541 of the type that is read-only.
3543 ??? However, if this is an unconstrained array, that type can
3544 be very complex. So skip it for now. Likewise for any other
3545 self-referential type. */
3546 if (Ekind (gnat_param) == E_In_Parameter
3547 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3548 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3549 gnu_param_type
3550 = build_qualified_type (gnu_param_type,
3551 (TYPE_QUALS (gnu_param_type)
3552 | TYPE_QUAL_CONST));
3554 /* For foreign conventions, pass arrays as a pointer to the
3555 underlying type. First check for unconstrained array and get
3556 the underlying array. Then get the component type and build
3557 a pointer to it. */
3558 if (Has_Foreign_Convention (gnat_entity)
3559 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3560 gnu_param_type
3561 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3562 (TREE_TYPE (gnu_param_type))));
3564 if (by_descr_p)
3565 gnu_param_type
3566 = build_pointer_type
3567 (build_vms_descriptor (gnu_param_type,
3568 Mechanism (gnat_param), gnat_entity));
3570 else if (Has_Foreign_Convention (gnat_entity)
3571 && !req_by_copy
3572 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3574 /* Strip off any multi-dimensional entries, then strip
3575 off the last array to get the component type. */
3576 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3577 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3578 gnu_param_type = TREE_TYPE (gnu_param_type);
3580 by_component_ptr_p = true;
3581 gnu_param_type = TREE_TYPE (gnu_param_type);
3583 if (Ekind (gnat_param) == E_In_Parameter)
3584 gnu_param_type
3585 = build_qualified_type (gnu_param_type,
3586 (TYPE_QUALS (gnu_param_type)
3587 | TYPE_QUAL_CONST));
3589 gnu_param_type = build_pointer_type (gnu_param_type);
3592 /* Fat pointers are passed as thin pointers for foreign
3593 conventions. */
3594 else if (Has_Foreign_Convention (gnat_entity)
3595 && TYPE_FAT_POINTER_P (gnu_param_type))
3596 gnu_param_type
3597 = make_type_from_size (gnu_param_type,
3598 size_int (POINTER_SIZE), false);
3600 /* If we must pass or were requested to pass by reference, do so.
3601 If we were requested to pass by copy, do so.
3602 Otherwise, for foreign conventions, pass all in out parameters
3603 or aggregates by reference. For COBOL and Fortran, pass
3604 all integer and FP types that way too. For Convention Ada,
3605 use the standard Ada default. */
3606 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3607 || (!req_by_copy
3608 && ((Has_Foreign_Convention (gnat_entity)
3609 && (Ekind (gnat_param) != E_In_Parameter
3610 || AGGREGATE_TYPE_P (gnu_param_type)))
3611 || (((Convention (gnat_entity)
3612 == Convention_Fortran)
3613 || (Convention (gnat_entity)
3614 == Convention_COBOL))
3615 && (INTEGRAL_TYPE_P (gnu_param_type)
3616 || FLOAT_TYPE_P (gnu_param_type)))
3617 /* For convention Ada, see if we pass by reference
3618 by default. */
3619 || (!Has_Foreign_Convention (gnat_entity)
3620 && default_pass_by_ref (gnu_param_type)))))
3622 gnu_param_type = build_reference_type (gnu_param_type);
3623 by_ref_p = true;
3626 else if (Ekind (gnat_param) != E_In_Parameter)
3627 copy_in_copy_out_flag = true;
3629 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3630 post_error ("?cannot pass & by copy", gnat_param);
3632 /* If this is an OUT parameter that isn't passed by reference
3633 and isn't a pointer or aggregate, we don't make a PARM_DECL
3634 for it. Instead, it will be a VAR_DECL created when we process
3635 the procedure. For the special parameter of Valued_Procedure,
3636 never pass it in.
3638 An exception is made to cover the RM-6.4.1 rule requiring "by
3639 copy" out parameters with discriminants or implicit initial
3640 values to be handled like in out parameters. These type are
3641 normally built as aggregates, and hence passed by reference,
3642 except for some packed arrays which end up encoded in special
3643 integer types.
3645 The exception we need to make is then for packed arrays of
3646 records with discriminants or implicit initial values. We have
3647 no light/easy way to check for the latter case, so we merely
3648 check for packed arrays of records. This may lead to useless
3649 copy-in operations, but in very rare cases only, as these would
3650 be exceptions in a set of already exceptional situations. */
3651 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3652 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3653 || (!by_descr_p
3654 && !POINTER_TYPE_P (gnu_param_type)
3655 && !AGGREGATE_TYPE_P (gnu_param_type)))
3656 && !(Is_Array_Type (Etype (gnat_param))
3657 && Is_Packed (Etype (gnat_param))
3658 && Is_Composite_Type (Component_Type
3659 (Etype (gnat_param)))))
3660 gnu_param = NULL_TREE;
3661 else
3663 gnu_param
3664 = create_param_decl
3665 (gnu_param_name, gnu_param_type,
3666 by_ref_p || by_component_ptr_p
3667 || Ekind (gnat_param) == E_In_Parameter);
3669 DECL_BY_REF_P (gnu_param) = by_ref_p;
3670 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3671 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3672 DECL_POINTS_TO_READONLY_P (gnu_param)
3673 = (Ekind (gnat_param) == E_In_Parameter
3674 && (by_ref_p || by_component_ptr_p));
3675 Sloc_to_locus (Sloc (gnat_param),
3676 &DECL_SOURCE_LOCATION (gnu_param));
3677 save_gnu_tree (gnat_param, gnu_param, false);
3678 gnu_param_list = chainon (gnu_param, gnu_param_list);
3680 /* If a parameter is a pointer, this function may modify
3681 memory through it and thus shouldn't be considered
3682 a pure function. Also, the memory may be modified
3683 between two calls, so they can't be CSE'ed. The latter
3684 case also handles by-ref parameters. */
3685 if (POINTER_TYPE_P (gnu_param_type)
3686 || TYPE_FAT_POINTER_P (gnu_param_type))
3687 pure_flag = false;
3690 if (copy_in_copy_out_flag)
3692 if (!has_copy_in_out)
3694 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3695 gnu_return_type = make_node (RECORD_TYPE);
3696 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3697 has_copy_in_out = true;
3700 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3701 gnu_return_type, 0, 0, 0, 0);
3702 Sloc_to_locus (Sloc (gnat_param),
3703 &DECL_SOURCE_LOCATION (gnu_field));
3704 TREE_CHAIN (gnu_field) = gnu_field_list;
3705 gnu_field_list = gnu_field;
3706 gnu_return_list = tree_cons (gnu_field, gnu_param,
3707 gnu_return_list);
3711 /* Do not compute record for out parameters if subprogram is
3712 stubbed since structures are incomplete for the back-end. */
3713 if (gnu_field_list
3714 && Convention (gnat_entity) != Convention_Stubbed)
3716 /* If all types are not complete, defer emission of debug
3717 information for this record types. Otherwise, we risk emitting
3718 debug information for a dummy type contained in the fields
3719 for that record. */
3720 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3721 false, defer_incomplete_level);
3723 if (defer_incomplete_level)
3725 debug_deferred = true;
3726 defer_debug_level++;
3728 defer_debug_incomplete_list
3729 = tree_cons (NULL_TREE, gnu_return_type,
3730 defer_debug_incomplete_list);
3734 /* If we have a CICO list but it has only one entry, we convert
3735 this function into a function that simply returns that one
3736 object. */
3737 if (list_length (gnu_return_list) == 1)
3738 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3740 if (Has_Stdcall_Convention (gnat_entity))
3742 struct attrib *attr
3743 = (struct attrib *) xmalloc (sizeof (struct attrib));
3745 attr->next = attr_list;
3746 attr->type = ATTR_MACHINE_ATTRIBUTE;
3747 attr->name = get_identifier ("stdcall");
3748 attr->args = NULL_TREE;
3749 attr->error_point = gnat_entity;
3750 attr_list = attr;
3753 /* Both lists ware built in reverse. */
3754 gnu_param_list = nreverse (gnu_param_list);
3755 gnu_return_list = nreverse (gnu_return_list);
3757 gnu_type
3758 = create_subprog_type (gnu_return_type, gnu_param_list,
3759 gnu_return_list, returns_unconstrained,
3760 returns_by_ref,
3761 Function_Returns_With_DSP (gnat_entity),
3762 returns_by_target_ptr);
3764 /* A subprogram (something that doesn't return anything) shouldn't
3765 be considered Pure since there would be no reason for such a
3766 subprogram. Note that procedures with Out (or In Out) parameters
3767 have already been converted into a function with a return type. */
3768 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3769 pure_flag = false;
3771 /* The semantics of "pure" in Ada essentially matches that of "const"
3772 in the back-end. In particular, both properties are orthogonal to
3773 the "nothrow" property. But this is true only if the EH circuitry
3774 is explicit in the internal representation of the back-end. If we
3775 are to completely hide the EH circuitry from it, we need to declare
3776 that calls to pure Ada subprograms that can throw have side effects
3777 since they can trigger an "abnormal" transfer of control flow; thus
3778 they can be neither "const" nor "pure" in the back-end sense. */
3779 gnu_type
3780 = build_qualified_type (gnu_type,
3781 TYPE_QUALS (gnu_type)
3782 | (Exception_Mechanism == Back_End_Exceptions
3783 ? TYPE_QUAL_CONST * pure_flag : 0)
3784 | (TYPE_QUAL_VOLATILE * volatile_flag));
3786 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3788 /* If we have a builtin decl for that function, check the signatures
3789 compatibilities. If the signatures are compatible, use the builtin
3790 decl. If they are not, we expect the checker predicate to have
3791 posted the appropriate errors, and just continue with what we have
3792 so far. */
3793 if (gnu_builtin_decl)
3795 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3797 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3799 gnu_decl = gnu_builtin_decl;
3800 gnu_type = gnu_builtin_type;
3801 break;
3805 /* If there was no specified Interface_Name and the external and
3806 internal names of the subprogram are the same, only use the
3807 internal name to allow disambiguation of nested subprograms. */
3808 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3809 gnu_ext_name = NULL_TREE;
3811 /* If we are defining the subprogram and it has an Address clause
3812 we must get the address expression from the saved GCC tree for the
3813 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3814 the address expression here since the front-end has guaranteed
3815 in that case that the elaboration has no effects. If there is
3816 an Address clause and we are not defining the object, just
3817 make it a constant. */
3818 if (Present (Address_Clause (gnat_entity)))
3820 tree gnu_address = NULL_TREE;
3822 if (definition)
3823 gnu_address
3824 = (present_gnu_tree (gnat_entity)
3825 ? get_gnu_tree (gnat_entity)
3826 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3828 save_gnu_tree (gnat_entity, NULL_TREE, false);
3830 gnu_type = build_reference_type (gnu_type);
3831 if (gnu_address)
3832 gnu_address = convert (gnu_type, gnu_address);
3834 gnu_decl
3835 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3836 gnu_address, false, Is_Public (gnat_entity),
3837 extern_flag, false, NULL, gnat_entity);
3838 DECL_BY_REF_P (gnu_decl) = 1;
3841 else if (kind == E_Subprogram_Type)
3842 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3843 !Comes_From_Source (gnat_entity),
3844 debug_info_p && !defer_incomplete_level,
3845 gnat_entity);
3846 else
3848 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3849 gnu_type, gnu_param_list,
3850 inline_flag, public_flag,
3851 extern_flag, attr_list,
3852 gnat_entity);
3854 DECL_STUBBED_P (gnu_decl)
3855 = Convention (gnat_entity) == Convention_Stubbed;
3858 break;
3860 case E_Incomplete_Type:
3861 case E_Private_Type:
3862 case E_Limited_Private_Type:
3863 case E_Record_Type_With_Private:
3864 case E_Private_Subtype:
3865 case E_Limited_Private_Subtype:
3866 case E_Record_Subtype_With_Private:
3868 /* If this type does not have a full view in the unit we are
3869 compiling, then just get the type from its Etype. */
3870 if (No (Full_View (gnat_entity)))
3872 /* If this is an incomplete type with no full view, it must be
3873 either a limited view brought in by a limited_with clause, in
3874 which case we use the non-limited view, or a Taft Amendement
3875 type, in which case we just return a dummy type. */
3876 if (kind == E_Incomplete_Type)
3878 if (From_With_Type (gnat_entity)
3879 && Present (Non_Limited_View (gnat_entity)))
3880 gnu_decl = gnat_to_gnu_entity (Non_Limited_View (gnat_entity),
3881 NULL_TREE, 0);
3882 else
3883 gnu_type = make_dummy_type (gnat_entity);
3886 else if (Present (Underlying_Full_View (gnat_entity)))
3887 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3888 NULL_TREE, 0);
3889 else
3891 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3892 NULL_TREE, 0);
3893 maybe_present = true;
3896 break;
3899 /* Otherwise, if we are not defining the type now, get the
3900 type from the full view. But always get the type from the full
3901 view for define on use types, since otherwise we won't see them! */
3903 else if (!definition
3904 || (Is_Itype (Full_View (gnat_entity))
3905 && No (Freeze_Node (gnat_entity)))
3906 || (Is_Itype (gnat_entity)
3907 && No (Freeze_Node (Full_View (gnat_entity)))))
3909 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3910 NULL_TREE, 0);
3911 maybe_present = true;
3912 break;
3915 /* For incomplete types, make a dummy type entry which will be
3916 replaced later. */
3917 gnu_type = make_dummy_type (gnat_entity);
3919 /* Save this type as the full declaration's type so we can do any needed
3920 updates when we see it. */
3921 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3922 !Comes_From_Source (gnat_entity),
3923 debug_info_p, gnat_entity);
3924 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3925 break;
3927 /* Simple class_wide types are always viewed as their root_type
3928 by Gigi unless an Equivalent_Type is specified. */
3929 case E_Class_Wide_Type:
3930 if (Present (Equivalent_Type (gnat_entity)))
3931 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3932 else
3933 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3935 maybe_present = true;
3936 break;
3938 case E_Task_Type:
3939 case E_Task_Subtype:
3940 case E_Protected_Type:
3941 case E_Protected_Subtype:
3942 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3943 gnu_type = void_type_node;
3944 else
3945 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3947 maybe_present = true;
3948 break;
3950 case E_Label:
3951 gnu_decl = create_label_decl (gnu_entity_id);
3952 break;
3954 case E_Block:
3955 case E_Loop:
3956 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3957 we've already saved it, so we don't try to. */
3958 gnu_decl = error_mark_node;
3959 saved = true;
3960 break;
3962 default:
3963 gcc_unreachable ();
3966 /* If we had a case where we evaluated another type and it might have
3967 defined this one, handle it here. */
3968 if (maybe_present && present_gnu_tree (gnat_entity))
3970 gnu_decl = get_gnu_tree (gnat_entity);
3971 saved = true;
3974 /* If we are processing a type and there is either no decl for it or
3975 we just made one, do some common processing for the type, such as
3976 handling alignment and possible padding. */
3978 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3980 if (Is_Tagged_Type (gnat_entity)
3981 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3982 TYPE_ALIGN_OK (gnu_type) = 1;
3984 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3985 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3987 /* ??? Don't set the size for a String_Literal since it is either
3988 confirming or we don't handle it properly (if the low bound is
3989 non-constant). */
3990 if (!gnu_size && kind != E_String_Literal_Subtype)
3991 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3992 TYPE_DECL, false,
3993 Has_Size_Clause (gnat_entity));
3995 /* If a size was specified, see if we can make a new type of that size
3996 by rearranging the type, for example from a fat to a thin pointer. */
3997 if (gnu_size)
3999 gnu_type
4000 = make_type_from_size (gnu_type, gnu_size,
4001 Has_Biased_Representation (gnat_entity));
4003 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4004 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4005 gnu_size = 0;
4008 /* If the alignment hasn't already been processed and this is
4009 not an unconstrained array, see if an alignment is specified.
4010 If not, we pick a default alignment for atomic objects. */
4011 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4013 else if (Known_Alignment (gnat_entity))
4014 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4015 TYPE_ALIGN (gnu_type));
4016 else if (Is_Atomic (gnat_entity) && !gnu_size
4017 && host_integerp (TYPE_SIZE (gnu_type), 1)
4018 && integer_pow2p (TYPE_SIZE (gnu_type)))
4019 align = MIN (BIGGEST_ALIGNMENT,
4020 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4021 else if (Is_Atomic (gnat_entity) && gnu_size
4022 && host_integerp (gnu_size, 1)
4023 && integer_pow2p (gnu_size))
4024 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4026 /* See if we need to pad the type. If we did, and made a record,
4027 the name of the new type may be changed. So get it back for
4028 us when we make the new TYPE_DECL below. */
4029 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
4030 true, definition, false);
4031 if (TREE_CODE (gnu_type) == RECORD_TYPE
4032 && TYPE_IS_PADDING_P (gnu_type))
4034 gnu_entity_id = TYPE_NAME (gnu_type);
4035 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4036 gnu_entity_id = DECL_NAME (gnu_entity_id);
4039 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4041 /* If we are at global level, GCC will have applied variable_size to
4042 the type, but that won't have done anything. So, if it's not
4043 a constant or self-referential, call elaborate_expression_1 to
4044 make a variable for the size rather than calculating it each time.
4045 Handle both the RM size and the actual size. */
4046 if (global_bindings_p ()
4047 && TYPE_SIZE (gnu_type)
4048 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4049 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4051 if (TREE_CODE (gnu_type) == RECORD_TYPE
4052 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4053 TYPE_SIZE (gnu_type), 0))
4055 TYPE_SIZE (gnu_type)
4056 = elaborate_expression_1 (gnat_entity, gnat_entity,
4057 TYPE_SIZE (gnu_type),
4058 get_identifier ("SIZE"),
4059 definition, 0);
4060 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4062 else
4064 TYPE_SIZE (gnu_type)
4065 = elaborate_expression_1 (gnat_entity, gnat_entity,
4066 TYPE_SIZE (gnu_type),
4067 get_identifier ("SIZE"),
4068 definition, 0);
4070 /* ??? For now, store the size as a multiple of the alignment
4071 in bytes so that we can see the alignment from the tree. */
4072 TYPE_SIZE_UNIT (gnu_type)
4073 = build_binary_op
4074 (MULT_EXPR, sizetype,
4075 elaborate_expression_1
4076 (gnat_entity, gnat_entity,
4077 build_binary_op (EXACT_DIV_EXPR, sizetype,
4078 TYPE_SIZE_UNIT (gnu_type),
4079 size_int (TYPE_ALIGN (gnu_type)
4080 / BITS_PER_UNIT)),
4081 get_identifier ("SIZE_A_UNIT"),
4082 definition, 0),
4083 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4085 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4086 SET_TYPE_ADA_SIZE
4087 (gnu_type,
4088 elaborate_expression_1 (gnat_entity,
4089 gnat_entity,
4090 TYPE_ADA_SIZE (gnu_type),
4091 get_identifier ("RM_SIZE"),
4092 definition, 0));
4096 /* If this is a record type or subtype, call elaborate_expression_1 on
4097 any field position. Do this for both global and local types.
4098 Skip any fields that we haven't made trees for to avoid problems with
4099 class wide types. */
4100 if (IN (kind, Record_Kind))
4101 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4102 gnat_temp = Next_Entity (gnat_temp))
4103 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4105 tree gnu_field = get_gnu_tree (gnat_temp);
4107 /* ??? Unfortunately, GCC needs to be able to prove the
4108 alignment of this offset and if it's a variable, it can't.
4109 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4110 right now, we have to put in an explicit multiply and
4111 divide by that value. */
4112 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4113 DECL_FIELD_OFFSET (gnu_field)
4114 = build_binary_op
4115 (MULT_EXPR, sizetype,
4116 elaborate_expression_1
4117 (gnat_temp, gnat_temp,
4118 build_binary_op (EXACT_DIV_EXPR, sizetype,
4119 DECL_FIELD_OFFSET (gnu_field),
4120 size_int (DECL_OFFSET_ALIGN (gnu_field)
4121 / BITS_PER_UNIT)),
4122 get_identifier ("OFFSET"),
4123 definition, 0),
4124 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4127 gnu_type = build_qualified_type (gnu_type,
4128 (TYPE_QUALS (gnu_type)
4129 | (TYPE_QUAL_VOLATILE
4130 * Treat_As_Volatile (gnat_entity))));
4132 if (Is_Atomic (gnat_entity))
4133 check_ok_for_atomic (gnu_type, gnat_entity, false);
4135 if (Known_Alignment (gnat_entity))
4136 TYPE_USER_ALIGN (gnu_type) = 1;
4138 if (!gnu_decl)
4139 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4140 !Comes_From_Source (gnat_entity),
4141 debug_info_p, gnat_entity);
4142 else
4143 TREE_TYPE (gnu_decl) = gnu_type;
4146 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4148 gnu_type = TREE_TYPE (gnu_decl);
4150 /* Back-annotate the Alignment of the type if not already in the
4151 tree. Likewise for sizes. */
4152 if (Unknown_Alignment (gnat_entity))
4153 Set_Alignment (gnat_entity,
4154 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4156 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4158 /* If the size is self-referential, we annotate the maximum
4159 value of that size. */
4160 tree gnu_size = TYPE_SIZE (gnu_type);
4162 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4163 gnu_size = max_size (gnu_size, true);
4165 Set_Esize (gnat_entity, annotate_value (gnu_size));
4167 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4169 /* In this mode the tag and the parent components are not
4170 generated by the front-end, so the sizes must be adjusted
4171 explicitly now. */
4173 int size_offset;
4174 int new_size;
4176 if (Is_Derived_Type (gnat_entity))
4178 size_offset
4179 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4180 Set_Alignment (gnat_entity,
4181 Alignment (Etype (Base_Type (gnat_entity))));
4183 else
4184 size_offset = POINTER_SIZE;
4186 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4187 Set_Esize (gnat_entity,
4188 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4189 / POINTER_SIZE) * POINTER_SIZE));
4190 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4194 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4195 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4198 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4199 DECL_ARTIFICIAL (gnu_decl) = 1;
4201 if (!debug_info_p && DECL_P (gnu_decl)
4202 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4203 && No (Renamed_Object (gnat_entity)))
4204 DECL_IGNORED_P (gnu_decl) = 1;
4206 /* If we haven't already, associate the ..._DECL node that we just made with
4207 the input GNAT entity node. */
4208 if (!saved)
4209 save_gnu_tree (gnat_entity, gnu_decl, false);
4211 /* If this is an enumeral or floating-point type, we were not able to set
4212 the bounds since they refer to the type. These bounds are always static.
4214 For enumeration types, also write debugging information and declare the
4215 enumeration literal table, if needed. */
4217 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4218 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4220 tree gnu_scalar_type = gnu_type;
4222 /* If this is a padded type, we need to use the underlying type. */
4223 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4224 && TYPE_IS_PADDING_P (gnu_scalar_type))
4225 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4227 /* If this is a floating point type and we haven't set a floating
4228 point type yet, use this in the evaluation of the bounds. */
4229 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4230 longest_float_type_node = gnu_type;
4232 TYPE_MIN_VALUE (gnu_scalar_type)
4233 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4234 TYPE_MAX_VALUE (gnu_scalar_type)
4235 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4237 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4239 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4241 /* Since this has both a typedef and a tag, avoid outputting
4242 the name twice. */
4243 DECL_ARTIFICIAL (gnu_decl) = 1;
4244 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4248 /* If we deferred processing of incomplete types, re-enable it. If there
4249 were no other disables and we have some to process, do so. */
4250 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4252 struct incomplete *incp = defer_incomplete_list;
4253 struct incomplete *next;
4255 defer_incomplete_list = NULL;
4256 for (; incp; incp = next)
4258 next = incp->next;
4260 if (incp->old_type)
4261 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4262 gnat_to_gnu_type (incp->full_type));
4263 free (incp);
4267 /* If we are not defining this type, see if it's in the incomplete list.
4268 If so, handle that list entry now. */
4269 else if (!definition)
4271 struct incomplete *incp;
4273 for (incp = defer_incomplete_list; incp; incp = incp->next)
4274 if (incp->old_type && incp->full_type == gnat_entity)
4276 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4277 TREE_TYPE (gnu_decl));
4278 incp->old_type = NULL_TREE;
4282 /* If there are no incomplete types and we have deferred emission
4283 of debug information, check whether we have finished defining
4284 all nested records.
4285 If so, handle the list now. */
4287 if (debug_deferred)
4288 defer_debug_level--;
4290 if (defer_debug_incomplete_list
4291 && !defer_incomplete_level
4292 && !defer_debug_level)
4294 tree c, n;
4296 defer_debug_incomplete_list = nreverse (defer_debug_incomplete_list);
4298 for (c = defer_debug_incomplete_list; c; c = n)
4300 n = TREE_CHAIN (c);
4301 write_record_type_debug_info (TREE_VALUE (c));
4304 defer_debug_incomplete_list = 0;
4307 if (this_global)
4308 force_global--;
4310 if (Is_Packed_Array_Type (gnat_entity)
4311 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4312 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4313 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4314 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4316 return gnu_decl;
4319 /* Similar, but if the returned value is a COMPONENT_REF, return the
4320 FIELD_DECL. */
4322 tree
4323 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4325 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4327 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4328 gnu_field = TREE_OPERAND (gnu_field, 1);
4330 return gnu_field;
4333 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
4335 static
4336 bool same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
4338 while (Present (Corresponding_Discriminant (discr1)))
4339 discr1 = Corresponding_Discriminant (discr1);
4341 while (Present (Corresponding_Discriminant (discr2)))
4342 discr2 = Corresponding_Discriminant (discr2);
4344 return
4345 Original_Record_Component (discr1) == Original_Record_Component (discr2);
4348 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4349 be elaborated at the point of its definition, but do nothing else. */
4351 void
4352 elaborate_entity (Entity_Id gnat_entity)
4354 switch (Ekind (gnat_entity))
4356 case E_Signed_Integer_Subtype:
4357 case E_Modular_Integer_Subtype:
4358 case E_Enumeration_Subtype:
4359 case E_Ordinary_Fixed_Point_Subtype:
4360 case E_Decimal_Fixed_Point_Subtype:
4361 case E_Floating_Point_Subtype:
4363 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4364 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4366 /* ??? Tests for avoiding static constraint error expression
4367 is needed until the front stops generating bogus conversions
4368 on bounds of real types. */
4370 if (!Raises_Constraint_Error (gnat_lb))
4371 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4372 1, 0, Needs_Debug_Info (gnat_entity));
4373 if (!Raises_Constraint_Error (gnat_hb))
4374 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4375 1, 0, Needs_Debug_Info (gnat_entity));
4376 break;
4379 case E_Record_Type:
4381 Node_Id full_definition = Declaration_Node (gnat_entity);
4382 Node_Id record_definition = Type_Definition (full_definition);
4384 /* If this is a record extension, go a level further to find the
4385 record definition. */
4386 if (Nkind (record_definition) == N_Derived_Type_Definition)
4387 record_definition = Record_Extension_Part (record_definition);
4389 break;
4391 case E_Record_Subtype:
4392 case E_Private_Subtype:
4393 case E_Limited_Private_Subtype:
4394 case E_Record_Subtype_With_Private:
4395 if (Is_Constrained (gnat_entity)
4396 && Has_Discriminants (Base_Type (gnat_entity))
4397 && Present (Discriminant_Constraint (gnat_entity)))
4399 Node_Id gnat_discriminant_expr;
4400 Entity_Id gnat_field;
4402 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4403 gnat_discriminant_expr
4404 = First_Elmt (Discriminant_Constraint (gnat_entity));
4405 Present (gnat_field);
4406 gnat_field = Next_Discriminant (gnat_field),
4407 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4408 /* ??? For now, ignore access discriminants. */
4409 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4410 elaborate_expression (Node (gnat_discriminant_expr),
4411 gnat_entity,
4412 get_entity_name (gnat_field), 1, 0, 0);
4414 break;
4419 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4420 any entities on its entity chain similarly. */
4422 void
4423 mark_out_of_scope (Entity_Id gnat_entity)
4425 Entity_Id gnat_sub_entity;
4426 unsigned int kind = Ekind (gnat_entity);
4428 /* If this has an entity list, process all in the list. */
4429 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4430 || IN (kind, Private_Kind)
4431 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4432 || kind == E_Function || kind == E_Generic_Function
4433 || kind == E_Generic_Package || kind == E_Generic_Procedure
4434 || kind == E_Loop || kind == E_Operator || kind == E_Package
4435 || kind == E_Package_Body || kind == E_Procedure
4436 || kind == E_Record_Type || kind == E_Record_Subtype
4437 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4438 for (gnat_sub_entity = First_Entity (gnat_entity);
4439 Present (gnat_sub_entity);
4440 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4441 if (Scope (gnat_sub_entity) == gnat_entity
4442 && gnat_sub_entity != gnat_entity)
4443 mark_out_of_scope (gnat_sub_entity);
4445 /* Now clear this if it has been defined, but only do so if it isn't
4446 a subprogram or parameter. We could refine this, but it isn't
4447 worth it. If this is statically allocated, it is supposed to
4448 hang around out of cope. */
4449 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4450 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4452 save_gnu_tree (gnat_entity, NULL_TREE, true);
4453 save_gnu_tree (gnat_entity, error_mark_node, true);
4457 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4458 is a multi-dimensional array type, do this recursively. */
4460 static void
4461 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4463 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4464 of a one-dimensional array, since the padding has the same alias set
4465 as the field type, but if it's a multi-dimensional array, we need to
4466 see the inner types. */
4467 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4468 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4469 || TYPE_IS_PADDING_P (gnu_old_type)))
4470 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4472 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4473 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4474 so we need to go down to what does. */
4475 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4476 gnu_old_type
4477 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4479 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4480 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4481 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4482 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4484 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4485 record_component_aliases (gnu_new_type);
4488 /* Return a TREE_LIST describing the substitutions needed to reflect
4489 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4490 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4491 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4492 gives the tree for the discriminant and TREE_VALUES is the replacement
4493 value. They are in the form of operands to substitute_in_expr.
4494 DEFINITION is as in gnat_to_gnu_entity. */
4496 static tree
4497 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4498 tree gnu_list, bool definition)
4500 Entity_Id gnat_discrim;
4501 Node_Id gnat_value;
4503 if (No (gnat_type))
4504 gnat_type = Implementation_Base_Type (gnat_subtype);
4506 if (Has_Discriminants (gnat_type))
4507 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4508 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4509 Present (gnat_discrim);
4510 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4511 gnat_value = Next_Elmt (gnat_value))
4512 /* Ignore access discriminants. */
4513 if (!Is_Access_Type (Etype (Node (gnat_value))))
4514 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4515 elaborate_expression
4516 (Node (gnat_value), gnat_subtype,
4517 get_entity_name (gnat_discrim), definition,
4518 1, 0),
4519 gnu_list);
4521 return gnu_list;
4524 /* For the following two functions: for each GNAT entity, the GCC
4525 tree node used as a dummy for that entity, if any. */
4527 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4529 /* Initialize the above table. */
4531 void
4532 init_dummy_type (void)
4534 Node_Id gnat_node;
4536 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4538 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4539 dummy_node_table[gnat_node] = NULL_TREE;
4541 dummy_node_table -= First_Node_Id;
4544 /* Make a dummy type corresponding to GNAT_TYPE. */
4546 tree
4547 make_dummy_type (Entity_Id gnat_type)
4549 Entity_Id gnat_underlying;
4550 tree gnu_type;
4551 enum tree_code code;
4553 /* Find a full type for GNAT_TYPE, taking into account any class wide
4554 types. */
4555 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4556 gnat_type = Equivalent_Type (gnat_type);
4557 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4558 gnat_type = Root_Type (gnat_type);
4560 for (gnat_underlying = gnat_type;
4561 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4562 && Present (Full_View (gnat_underlying)));
4563 gnat_underlying = Full_View (gnat_underlying))
4566 /* If it there already a dummy type, use that one. Else make one. */
4567 if (dummy_node_table[gnat_underlying])
4568 return dummy_node_table[gnat_underlying];
4570 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4571 it an ENUMERAL_TYPE. */
4572 if (Is_Record_Type (gnat_underlying))
4574 Node_Id component_list
4575 = Component_List (Type_Definition
4576 (Declaration_Node
4577 (Implementation_Base_Type (gnat_underlying))));
4578 Node_Id component;
4580 /* Make this a UNION_TYPE unless it's either not an Unchecked_Union or
4581 we have a non-discriminant field outside a variant. In either case,
4582 it's a RECORD_TYPE. */
4583 code = UNION_TYPE;
4584 if (!Is_Unchecked_Union (gnat_underlying))
4585 code = RECORD_TYPE;
4586 else
4587 for (component = First_Non_Pragma (Component_Items (component_list));
4588 Present (component); component = Next_Non_Pragma (component))
4589 if (Ekind (Defining_Entity (component)) == E_Component)
4590 code = RECORD_TYPE;
4592 else
4593 code = ENUMERAL_TYPE;
4595 gnu_type = make_node (code);
4596 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4597 TYPE_DUMMY_P (gnu_type) = 1;
4598 if (AGGREGATE_TYPE_P (gnu_type))
4599 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4601 dummy_node_table[gnat_underlying] = gnu_type;
4603 return gnu_type;
4606 /* Return true if the size represented by GNU_SIZE can be handled by an
4607 allocation. If STATIC_P is true, consider only what can be done with a
4608 static allocation. */
4610 static bool
4611 allocatable_size_p (tree gnu_size, bool static_p)
4613 HOST_WIDE_INT our_size;
4615 /* If this is not a static allocation, the only case we want to forbid
4616 is an overflowing size. That will be converted into a raise a
4617 Storage_Error. */
4618 if (!static_p)
4619 return !(TREE_CODE (gnu_size) == INTEGER_CST
4620 && TREE_CONSTANT_OVERFLOW (gnu_size));
4622 /* Otherwise, we need to deal with both variable sizes and constant
4623 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4624 since assemblers may not like very large sizes. */
4625 if (!host_integerp (gnu_size, 1))
4626 return false;
4628 our_size = tree_low_cst (gnu_size, 1);
4629 return (int) our_size == our_size;
4632 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4634 static void
4635 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4637 Node_Id gnat_temp;
4639 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4640 gnat_temp = Next_Rep_Item (gnat_temp))
4641 if (Nkind (gnat_temp) == N_Pragma)
4643 struct attrib *attr;
4644 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4645 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4646 enum attr_type etype;
4648 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4649 && Present (Next (First (gnat_assoc)))
4650 && (Nkind (Expression (Next (First (gnat_assoc))))
4651 == N_String_Literal))
4653 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4654 (gnat_to_gnu
4655 (Expression (Next
4656 (First (gnat_assoc))))));
4657 if (Present (Next (Next (First (gnat_assoc))))
4658 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4659 == N_String_Literal))
4660 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4661 (gnat_to_gnu
4662 (Expression
4663 (Next (Next
4664 (First (gnat_assoc)))))));
4667 switch (Get_Pragma_Id (Chars (gnat_temp)))
4669 case Pragma_Machine_Attribute:
4670 etype = ATTR_MACHINE_ATTRIBUTE;
4671 break;
4673 case Pragma_Linker_Alias:
4674 etype = ATTR_LINK_ALIAS;
4675 break;
4677 case Pragma_Linker_Section:
4678 etype = ATTR_LINK_SECTION;
4679 break;
4681 case Pragma_Linker_Constructor:
4682 etype = ATTR_LINK_CONSTRUCTOR;
4683 break;
4685 case Pragma_Linker_Destructor:
4686 etype = ATTR_LINK_DESTRUCTOR;
4687 break;
4689 case Pragma_Weak_External:
4690 etype = ATTR_WEAK_EXTERNAL;
4691 break;
4693 default:
4694 continue;
4697 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4698 attr->next = *attr_list;
4699 attr->type = etype;
4700 attr->name = gnu_arg0;
4702 /* If we have an argument specified together with an attribute name,
4703 make it a single TREE_VALUE entry in a list of arguments, as GCC
4704 expects it. */
4705 if (gnu_arg1 != NULL_TREE)
4706 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4707 else
4708 attr->args = NULL_TREE;
4710 attr->error_point
4711 = Present (Next (First (gnat_assoc)))
4712 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4713 *attr_list = attr;
4717 /* Get the unpadded version of a GNAT type. */
4719 tree
4720 get_unpadded_type (Entity_Id gnat_entity)
4722 tree type = gnat_to_gnu_type (gnat_entity);
4724 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4725 type = TREE_TYPE (TYPE_FIELDS (type));
4727 return type;
4730 /* Called when we need to protect a variable object using a save_expr. */
4732 tree
4733 maybe_variable (tree gnu_operand)
4735 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4736 || TREE_CODE (gnu_operand) == SAVE_EXPR
4737 || TREE_CODE (gnu_operand) == NULL_EXPR)
4738 return gnu_operand;
4740 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4742 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4743 TREE_TYPE (gnu_operand),
4744 variable_size (TREE_OPERAND (gnu_operand, 0)));
4746 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4747 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4748 return gnu_result;
4750 else
4751 return variable_size (gnu_operand);
4754 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4755 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4756 return the GCC tree to use for that expression. GNU_NAME is the
4757 qualification to use if an external name is appropriate and DEFINITION is
4758 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4759 we need a result. Otherwise, we are just elaborating this for
4760 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4761 purposes even if it isn't needed for code generation. */
4763 static tree
4764 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4765 tree gnu_name, bool definition, bool need_value,
4766 bool need_debug)
4768 tree gnu_expr;
4770 /* If we already elaborated this expression (e.g., it was involved
4771 in the definition of a private type), use the old value. */
4772 if (present_gnu_tree (gnat_expr))
4773 return get_gnu_tree (gnat_expr);
4775 /* If we don't need a value and this is static or a discriminant, we
4776 don't need to do anything. */
4777 else if (!need_value
4778 && (Is_OK_Static_Expression (gnat_expr)
4779 || (Nkind (gnat_expr) == N_Identifier
4780 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4781 return 0;
4783 /* Otherwise, convert this tree to its GCC equivalent. */
4784 gnu_expr
4785 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4786 gnu_name, definition, need_debug);
4788 /* Save the expression in case we try to elaborate this entity again. Since
4789 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4790 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4791 save_gnu_tree (gnat_expr, gnu_expr, true);
4793 return need_value ? gnu_expr : error_mark_node;
4796 /* Similar, but take a GNU expression. */
4798 static tree
4799 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4800 tree gnu_expr, tree gnu_name, bool definition,
4801 bool need_debug)
4803 tree gnu_decl = NULL_TREE;
4804 /* Strip any conversions to see if the expression is a readonly variable.
4805 ??? This really should remain readonly, but we have to think about
4806 the typing of the tree here. */
4807 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4808 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4809 bool expr_variable;
4811 /* In most cases, we won't see a naked FIELD_DECL here because a
4812 discriminant reference will have been replaced with a COMPONENT_REF
4813 when the type is being elaborated. However, there are some cases
4814 involving child types where we will. So convert it to a COMPONENT_REF
4815 here. We have to hope it will be at the highest level of the
4816 expression in these cases. */
4817 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4818 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4819 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4820 gnu_expr, NULL_TREE);
4822 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4823 that is a constant, make a variable that is initialized to contain the
4824 bound when the package containing the definition is elaborated. If
4825 this entity is defined at top level and a bound or discriminant value
4826 isn't a constant or a reference to a discriminant, replace the bound
4827 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4828 rely here on the fact that an expression cannot contain both the
4829 discriminant and some other variable. */
4831 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4832 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4833 && TREE_READONLY (gnu_inner_expr))
4834 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4836 /* If this is a static expression or contains a discriminant, we don't
4837 need the variable for debugging (and can't elaborate anyway if a
4838 discriminant). */
4839 if (need_debug
4840 && (Is_OK_Static_Expression (gnat_expr)
4841 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4842 need_debug = false;
4844 /* Now create the variable if we need it. */
4845 if (need_debug || (expr_variable && expr_global))
4846 gnu_decl
4847 = create_var_decl (create_concat_name (gnat_entity,
4848 IDENTIFIER_POINTER (gnu_name)),
4849 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
4850 !need_debug, Is_Public (gnat_entity),
4851 !definition, false, NULL, gnat_entity);
4853 /* We only need to use this variable if we are in global context since GCC
4854 can do the right thing in the local case. */
4855 if (expr_global && expr_variable)
4856 return gnu_decl;
4857 else if (!expr_variable)
4858 return gnu_expr;
4859 else
4860 return maybe_variable (gnu_expr);
4863 /* Create a record type that contains a field of TYPE with a starting bit
4864 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4866 tree
4867 make_aligning_type (tree type, int align, tree size)
4869 tree record_type = make_node (RECORD_TYPE);
4870 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4871 tree size_addr_place = convert (sizetype,
4872 build_unary_op (ADDR_EXPR, NULL_TREE,
4873 place));
4874 tree name = TYPE_NAME (type);
4875 tree pos, field;
4877 if (TREE_CODE (name) == TYPE_DECL)
4878 name = DECL_NAME (name);
4880 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4882 /* The bit position is obtained by "and"ing the alignment minus 1
4883 with the two's complement of the address and multiplying
4884 by the number of bits per unit. Do all this in sizetype. */
4885 pos = size_binop (MULT_EXPR,
4886 convert (bitsizetype,
4887 size_binop (BIT_AND_EXPR,
4888 size_diffop (size_zero_node,
4889 size_addr_place),
4890 ssize_int ((align / BITS_PER_UNIT)
4891 - 1))),
4892 bitsize_unit_node);
4894 /* Create the field, with -1 as the 'addressable' indication to avoid the
4895 creation of a bitfield. We don't need one, it would have damaging
4896 consequences on the alignment computation, and create_field_decl would
4897 make one without this special argument, for instance because of the
4898 complex position expression. */
4899 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4900 pos, -1);
4902 finish_record_type (record_type, field, true, false);
4903 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4904 TYPE_SIZE (record_type)
4905 = size_binop (PLUS_EXPR,
4906 size_binop (MULT_EXPR, convert (bitsizetype, size),
4907 bitsize_unit_node),
4908 bitsize_int (align));
4909 TYPE_SIZE_UNIT (record_type)
4910 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4911 copy_alias_set (record_type, type);
4912 return record_type;
4915 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4916 being used as the field type of a packed record. See if we can rewrite it
4917 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4918 return the new type. If not, return the original type. */
4920 static tree
4921 make_packable_type (tree type)
4923 tree new_type = make_node (TREE_CODE (type));
4924 tree field_list = NULL_TREE;
4925 tree old_field;
4927 /* Copy the name and flags from the old type to that of the new and set
4928 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4929 also copy the size. */
4930 TYPE_NAME (new_type) = TYPE_NAME (type);
4931 TYPE_JUSTIFIED_MODULAR_P (new_type)
4932 = TYPE_JUSTIFIED_MODULAR_P (type);
4933 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4935 if (TREE_CODE (type) == RECORD_TYPE)
4936 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4937 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4939 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4940 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4943 TYPE_ALIGN (new_type)
4944 = ((HOST_WIDE_INT) 1
4945 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4947 /* Now copy the fields, keeping the position and size. */
4948 for (old_field = TYPE_FIELDS (type); old_field;
4949 old_field = TREE_CHAIN (old_field))
4951 tree new_field_type = TREE_TYPE (old_field);
4952 tree new_field;
4954 if (TYPE_MODE (new_field_type) == BLKmode
4955 && (TREE_CODE (new_field_type) == RECORD_TYPE
4956 || TREE_CODE (new_field_type) == UNION_TYPE
4957 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4958 && host_integerp (TYPE_SIZE (new_field_type), 1))
4959 new_field_type = make_packable_type (new_field_type);
4961 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4962 new_type, TYPE_PACKED (type),
4963 DECL_SIZE (old_field),
4964 bit_position (old_field),
4965 !DECL_NONADDRESSABLE_P (old_field));
4967 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4968 SET_DECL_ORIGINAL_FIELD
4969 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4970 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4972 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4973 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4975 TREE_CHAIN (new_field) = field_list;
4976 field_list = new_field;
4979 finish_record_type (new_type, nreverse (field_list), true, true);
4980 copy_alias_set (new_type, type);
4981 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4984 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4985 if needed. We have already verified that SIZE and TYPE are large enough.
4987 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4988 to issue a warning.
4990 IS_USER_TYPE is true if we must be sure we complete the original type.
4992 DEFINITION is true if this type is being defined.
4994 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4995 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4996 type. */
4998 tree
4999 maybe_pad_type (tree type, tree size, unsigned int align,
5000 Entity_Id gnat_entity, const char *name_trailer,
5001 bool is_user_type, bool definition, bool same_rm_size)
5003 tree orig_size = TYPE_SIZE (type);
5004 tree record;
5005 tree field;
5007 /* If TYPE is a padded type, see if it agrees with any size and alignment
5008 we were given. If so, return the original type. Otherwise, strip
5009 off the padding, since we will either be returning the inner type
5010 or repadding it. If no size or alignment is specified, use that of
5011 the original padded type. */
5013 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5015 if ((!size
5016 || operand_equal_p (round_up (size,
5017 MAX (align, TYPE_ALIGN (type))),
5018 round_up (TYPE_SIZE (type),
5019 MAX (align, TYPE_ALIGN (type))),
5021 && (align == 0 || align == TYPE_ALIGN (type)))
5022 return type;
5024 if (!size)
5025 size = TYPE_SIZE (type);
5026 if (align == 0)
5027 align = TYPE_ALIGN (type);
5029 type = TREE_TYPE (TYPE_FIELDS (type));
5030 orig_size = TYPE_SIZE (type);
5033 /* If the size is either not being changed or is being made smaller (which
5034 is not done here (and is only valid for bitfields anyway), show the size
5035 isn't changing. Likewise, clear the alignment if it isn't being
5036 changed. Then return if we aren't doing anything. */
5038 if (size
5039 && (operand_equal_p (size, orig_size, 0)
5040 || (TREE_CODE (orig_size) == INTEGER_CST
5041 && tree_int_cst_lt (size, orig_size))))
5042 size = NULL_TREE;
5044 if (align == TYPE_ALIGN (type))
5045 align = 0;
5047 if (align == 0 && !size)
5048 return type;
5050 /* We used to modify the record in place in some cases, but that could
5051 generate incorrect debugging information. So make a new record
5052 type and name. */
5053 record = make_node (RECORD_TYPE);
5055 if (Present (gnat_entity))
5056 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5058 /* If we were making a type, complete the original type and give it a
5059 name. */
5060 if (is_user_type)
5061 create_type_decl (get_entity_name (gnat_entity), type,
5062 NULL, !Comes_From_Source (gnat_entity),
5063 !(TYPE_NAME (type)
5064 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5065 && DECL_IGNORED_P (TYPE_NAME (type))),
5066 gnat_entity);
5068 /* If we are changing the alignment and the input type is a record with
5069 BLKmode and a small constant size, try to make a form that has an
5070 integral mode. That might allow this record to have an integral mode,
5071 which will be much more efficient. There is no point in doing this if a
5072 size is specified unless it is also smaller than the biggest alignment
5073 and it is incorrect to do this if the size of the original type is not a
5074 multiple of the alignment. */
5075 if (align != 0
5076 && TREE_CODE (type) == RECORD_TYPE
5077 && TYPE_MODE (type) == BLKmode
5078 && host_integerp (orig_size, 1)
5079 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
5080 && (!size
5081 || (TREE_CODE (size) == INTEGER_CST
5082 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
5083 && tree_low_cst (orig_size, 1) % align == 0)
5084 type = make_packable_type (type);
5086 field = create_field_decl (get_identifier ("F"), type, record, 0,
5087 NULL_TREE, bitsize_zero_node, 1);
5089 DECL_INTERNAL_P (field) = 1;
5090 TYPE_SIZE (record) = size ? size : orig_size;
5091 TYPE_SIZE_UNIT (record)
5092 = (size ? convert (sizetype,
5093 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
5094 : TYPE_SIZE_UNIT (type));
5096 TYPE_ALIGN (record) = align;
5097 TYPE_IS_PADDING_P (record) = 1;
5098 TYPE_VOLATILE (record)
5099 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5100 finish_record_type (record, field, true, false);
5102 /* Keep the RM_Size of the padded record as that of the old record
5103 if requested. */
5104 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
5106 /* Unless debugging information isn't being written for the input type,
5107 write a record that shows what we are a subtype of and also make a
5108 variable that indicates our size, if variable. */
5109 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
5110 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
5111 || !DECL_IGNORED_P (TYPE_NAME (type))))
5113 tree marker = make_node (RECORD_TYPE);
5114 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
5115 ? DECL_NAME (TYPE_NAME (record))
5116 : TYPE_NAME (record));
5117 tree orig_name = TYPE_NAME (type);
5119 if (TREE_CODE (orig_name) == TYPE_DECL)
5120 orig_name = DECL_NAME (orig_name);
5122 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5123 finish_record_type (marker,
5124 create_field_decl (orig_name, integer_type_node,
5125 marker, 0, NULL_TREE, NULL_TREE,
5127 false, false);
5129 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5130 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5131 bitsizetype, TYPE_SIZE (record), false, false, false,
5132 false, NULL, gnat_entity);
5135 type = record;
5137 if (CONTAINS_PLACEHOLDER_P (orig_size))
5138 orig_size = max_size (orig_size, true);
5140 /* If the size was widened explicitly, maybe give a warning. */
5141 if (size && Present (gnat_entity)
5142 && !operand_equal_p (size, orig_size, 0)
5143 && !(TREE_CODE (size) == INTEGER_CST
5144 && TREE_CODE (orig_size) == INTEGER_CST
5145 && tree_int_cst_lt (size, orig_size)))
5147 Node_Id gnat_error_node = Empty;
5149 if (Is_Packed_Array_Type (gnat_entity))
5150 gnat_entity = Associated_Node_For_Itype (gnat_entity);
5152 if ((Ekind (gnat_entity) == E_Component
5153 || Ekind (gnat_entity) == E_Discriminant)
5154 && Present (Component_Clause (gnat_entity)))
5155 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5156 else if (Present (Size_Clause (gnat_entity)))
5157 gnat_error_node = Expression (Size_Clause (gnat_entity));
5159 /* Generate message only for entities that come from source, since
5160 if we have an entity created by expansion, the message will be
5161 generated for some other corresponding source entity. */
5162 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5163 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5164 gnat_entity,
5165 size_diffop (size, orig_size));
5167 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5168 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5169 gnat_entity, gnat_entity,
5170 size_diffop (size, orig_size));
5173 return type;
5176 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5177 the value passed against the list of choices. */
5179 tree
5180 choices_to_gnu (tree operand, Node_Id choices)
5182 Node_Id choice;
5183 Node_Id gnat_temp;
5184 tree result = integer_zero_node;
5185 tree this_test, low = 0, high = 0, single = 0;
5187 for (choice = First (choices); Present (choice); choice = Next (choice))
5189 switch (Nkind (choice))
5191 case N_Range:
5192 low = gnat_to_gnu (Low_Bound (choice));
5193 high = gnat_to_gnu (High_Bound (choice));
5195 /* There's no good type to use here, so we might as well use
5196 integer_type_node. */
5197 this_test
5198 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5199 build_binary_op (GE_EXPR, integer_type_node,
5200 operand, low),
5201 build_binary_op (LE_EXPR, integer_type_node,
5202 operand, high));
5204 break;
5206 case N_Subtype_Indication:
5207 gnat_temp = Range_Expression (Constraint (choice));
5208 low = gnat_to_gnu (Low_Bound (gnat_temp));
5209 high = gnat_to_gnu (High_Bound (gnat_temp));
5211 this_test
5212 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5213 build_binary_op (GE_EXPR, integer_type_node,
5214 operand, low),
5215 build_binary_op (LE_EXPR, integer_type_node,
5216 operand, high));
5217 break;
5219 case N_Identifier:
5220 case N_Expanded_Name:
5221 /* This represents either a subtype range, an enumeration
5222 literal, or a constant Ekind says which. If an enumeration
5223 literal or constant, fall through to the next case. */
5224 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5225 && Ekind (Entity (choice)) != E_Constant)
5227 tree type = gnat_to_gnu_type (Entity (choice));
5229 low = TYPE_MIN_VALUE (type);
5230 high = TYPE_MAX_VALUE (type);
5232 this_test
5233 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5234 build_binary_op (GE_EXPR, integer_type_node,
5235 operand, low),
5236 build_binary_op (LE_EXPR, integer_type_node,
5237 operand, high));
5238 break;
5240 /* ... fall through ... */
5241 case N_Character_Literal:
5242 case N_Integer_Literal:
5243 single = gnat_to_gnu (choice);
5244 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5245 single);
5246 break;
5248 case N_Others_Choice:
5249 this_test = integer_one_node;
5250 break;
5252 default:
5253 gcc_unreachable ();
5256 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5257 result, this_test);
5260 return result;
5263 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5264 placed in GNU_RECORD_TYPE.
5266 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5267 record has a Component_Alignment of Storage_Unit.
5269 DEFINITION is true if this field is for a record being defined. */
5271 static tree
5272 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5273 bool definition)
5275 tree gnu_field_id = get_entity_name (gnat_field);
5276 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5277 tree gnu_pos = 0;
5278 tree gnu_size = 0;
5279 tree gnu_field;
5280 bool needs_strict_alignment
5281 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5282 || Treat_As_Volatile (gnat_field));
5284 /* If this field requires strict alignment or contains an item of
5285 variable sized, pretend it isn't packed. */
5286 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5287 packed = 0;
5289 /* For packed records, this is one of the few occasions on which we use
5290 the official RM size for discrete or fixed-point components, instead
5291 of the normal GNAT size stored in Esize. See description in Einfo:
5292 "Handling of Type'Size Values" for further details. */
5294 if (packed == 1)
5295 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5296 gnat_field, FIELD_DECL, false, true);
5298 if (Known_Static_Esize (gnat_field))
5299 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5300 gnat_field, FIELD_DECL, false, true);
5302 /* If we have a specified size that's smaller than that of the field type,
5303 or a position is specified, and the field type is also a record that's
5304 BLKmode and with a small constant size, see if we can get an integral
5305 mode form of the type when appropriate. If we can, show a size was
5306 specified for the field if there wasn't one already, so we know to make
5307 this a bitfield and avoid making things wider.
5309 Doing this is first useful if the record is packed because we can then
5310 place the field at a non-byte-aligned position and so achieve tighter
5311 packing.
5313 This is in addition *required* if the field shares a byte with another
5314 field and the front-end lets the back-end handle the references, because
5315 GCC does not handle BLKmode bitfields properly.
5317 We avoid the transformation if it is not required or potentially useful,
5318 as it might entail an increase of the field's alignment and have ripple
5319 effects on the outer record type. A typical case is a field known to be
5320 byte aligned and not to share a byte with another field.
5322 Besides, we don't even look the possibility of a transformation in cases
5323 known to be in error already, for instance when an invalid size results
5324 from a component clause. */
5326 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5327 && TYPE_MODE (gnu_field_type) == BLKmode
5328 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5329 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5330 && (packed == 1
5331 || (gnu_size
5332 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))
5333 || (Present (Component_Clause (gnat_field)) && gnu_size != 0)))
5335 /* See what the alternate type and size would be. */
5336 tree gnu_packable_type = make_packable_type (gnu_field_type);
5338 bool has_byte_aligned_clause
5339 = Present (Component_Clause (gnat_field))
5340 && (UI_To_Int (Component_Bit_Offset (gnat_field))
5341 % BITS_PER_UNIT == 0);
5343 /* Compute whether we should avoid the substitution. */
5344 int reject =
5345 /* There is no point substituting if there is no change. */
5346 (gnu_packable_type == gnu_field_type
5348 /* ... nor when the field is known to be byte aligned and not to
5349 share a byte with another field. */
5350 (has_byte_aligned_clause
5351 && value_factor_p (gnu_size, BITS_PER_UNIT))
5353 /* The size of an aliased field must be an exact multiple of the
5354 type's alignment, which the substitution might increase. Reject
5355 substitutions that would so invalidate a component clause when the
5356 specified position is byte aligned, as the change would have no
5357 real benefit from the packing standpoint anyway. */
5358 (Is_Aliased (gnat_field)
5359 && has_byte_aligned_clause
5360 && ! value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)))
5363 /* Substitute unless told otherwise. */
5364 if (!reject)
5366 gnu_field_type = gnu_packable_type;
5368 if (gnu_size == 0)
5369 gnu_size = rm_size (gnu_field_type);
5373 /* If we are packing the record and the field is BLKmode, round the
5374 size up to a byte boundary. */
5375 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5376 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5378 if (Present (Component_Clause (gnat_field)))
5380 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5381 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5382 gnat_field, FIELD_DECL, false, true);
5384 /* Ensure the position does not overlap with the parent subtype,
5385 if there is one. */
5386 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5388 tree gnu_parent
5389 = gnat_to_gnu_type (Parent_Subtype
5390 (Underlying_Type (Scope (gnat_field))));
5392 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5393 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5395 post_error_ne_tree
5396 ("offset of& must be beyond parent{, minimum allowed is ^}",
5397 First_Bit (Component_Clause (gnat_field)), gnat_field,
5398 TYPE_SIZE_UNIT (gnu_parent));
5402 /* If this field needs strict alignment, ensure the record is
5403 sufficiently aligned and that that position and size are
5404 consistent with the alignment. */
5405 if (needs_strict_alignment)
5407 tree gnu_rounded_size = round_up (rm_size (gnu_field_type),
5408 TYPE_ALIGN (gnu_field_type));
5410 TYPE_ALIGN (gnu_record_type)
5411 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5413 /* If Atomic, the size must match exactly that of the field. */
5414 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5415 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5417 post_error_ne_tree
5418 ("atomic field& must be natural size of type{ (^)}",
5419 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5420 TYPE_SIZE (gnu_field_type));
5422 gnu_size = NULL_TREE;
5425 /* If Aliased, the size must match exactly the rounded size. We
5426 used to be more accommodating here and accept greater sizes, but
5427 fully supporting this case on big-endian platforms would require
5428 switching to a more involved layout for the field. */
5429 else if (Is_Aliased (gnat_field)
5430 && gnu_size
5431 && ! operand_equal_p (gnu_size, gnu_rounded_size, 0))
5433 post_error_ne_tree
5434 ("size of aliased field& must be ^ bits",
5435 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5436 gnu_rounded_size);
5437 gnu_size = NULL_TREE;
5440 if (!integer_zerop (size_binop
5441 (TRUNC_MOD_EXPR, gnu_pos,
5442 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5444 if (Is_Aliased (gnat_field))
5445 post_error_ne_num
5446 ("position of aliased field& must be multiple of ^ bits",
5447 First_Bit (Component_Clause (gnat_field)), gnat_field,
5448 TYPE_ALIGN (gnu_field_type));
5450 else if (Treat_As_Volatile (gnat_field))
5451 post_error_ne_num
5452 ("position of volatile field& must be multiple of ^ bits",
5453 First_Bit (Component_Clause (gnat_field)), gnat_field,
5454 TYPE_ALIGN (gnu_field_type));
5456 else if (Strict_Alignment (Etype (gnat_field)))
5457 post_error_ne_num
5458 ("position of & with aliased or tagged components not multiple of ^ bits",
5459 First_Bit (Component_Clause (gnat_field)), gnat_field,
5460 TYPE_ALIGN (gnu_field_type));
5461 else
5462 gcc_unreachable ();
5464 gnu_pos = NULL_TREE;
5468 if (Is_Atomic (gnat_field))
5469 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5472 /* If the record has rep clauses and this is the tag field, make a rep
5473 clause for it as well. */
5474 else if (Has_Specified_Layout (Scope (gnat_field))
5475 && Chars (gnat_field) == Name_uTag)
5477 gnu_pos = bitsize_zero_node;
5478 gnu_size = TYPE_SIZE (gnu_field_type);
5481 /* We need to make the size the maximum for the type if it is
5482 self-referential and an unconstrained type. In that case, we can't
5483 pack the field since we can't make a copy to align it. */
5484 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5485 && !gnu_size
5486 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5487 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5489 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5490 packed = 0;
5493 /* If no size is specified (or if there was an error), don't specify a
5494 position. */
5495 if (!gnu_size)
5496 gnu_pos = NULL_TREE;
5497 else
5499 /* If the field's type is justified modular, we would need to remove
5500 the wrapper to (better) meet the layout requirements. However we
5501 can do so only if the field is not aliased to preserve the unique
5502 layout and if the prescribed size is not greater than that of the
5503 packed array to preserve the justification. */
5504 if (!needs_strict_alignment
5505 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5506 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5507 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5508 <= 0)
5509 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5511 gnu_field_type
5512 = make_type_from_size (gnu_field_type, gnu_size,
5513 Has_Biased_Representation (gnat_field));
5514 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5515 "PAD", false, definition, true);
5518 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5519 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5521 /* Now create the decl for the field. */
5522 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5523 packed, gnu_size, gnu_pos,
5524 Is_Aliased (gnat_field));
5525 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5526 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5528 if (Ekind (gnat_field) == E_Discriminant)
5529 DECL_DISCRIMINANT_NUMBER (gnu_field)
5530 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5532 return gnu_field;
5535 /* Return true if TYPE is a type with variable size, a padding type with a
5536 field of variable size or is a record that has a field such a field. */
5538 static bool
5539 is_variable_size (tree type)
5541 tree field;
5543 /* We need not be concerned about this at all if we don't have
5544 strict alignment. */
5545 if (!STRICT_ALIGNMENT)
5546 return false;
5547 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5548 return true;
5549 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5550 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5551 return true;
5552 else if (TREE_CODE (type) != RECORD_TYPE
5553 && TREE_CODE (type) != UNION_TYPE
5554 && TREE_CODE (type) != QUAL_UNION_TYPE)
5555 return false;
5557 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5558 if (is_variable_size (TREE_TYPE (field)))
5559 return true;
5561 return false;
5564 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5565 of GCC trees for fields that are in the record and have already been
5566 processed. When called from gnat_to_gnu_entity during the processing of a
5567 record type definition, the GCC nodes for the discriminants will be on
5568 the chain. The other calls to this function are recursive calls from
5569 itself for the Component_List of a variant and the chain is empty.
5571 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5572 for a record type with "pragma component_alignment (storage_unit)".
5574 DEFINITION is true if we are defining this record.
5576 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5577 with a rep clause is to be added. If it is nonzero, that is all that
5578 should be done with such fields.
5580 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5581 laying out the record. This means the alignment only serves to force fields
5582 to be bitfields, but not require the record to be that aligned. This is
5583 used for variants.
5585 ALL_REP, if true, means a rep clause was found for all the fields. This
5586 simplifies the logic since we know we're not in the mixed case.
5588 DEFER_DEBUG, if true, means that the debugging routines should not be
5589 called when finishing constructing the record type.
5591 UNCHECKED_UNION, if tree, means that we are building a type for a record
5592 with a Pragma Unchecked_Union.
5594 The processing of the component list fills in the chain with all of the
5595 fields of the record and then the record type is finished. */
5597 static void
5598 components_to_record (tree gnu_record_type, Node_Id component_list,
5599 tree gnu_field_list, int packed, bool definition,
5600 tree *p_gnu_rep_list, bool cancel_alignment,
5601 bool all_rep, bool defer_debug, bool unchecked_union)
5603 Node_Id component_decl;
5604 Entity_Id gnat_field;
5605 Node_Id variant_part;
5606 tree gnu_our_rep_list = NULL_TREE;
5607 tree gnu_field, gnu_last;
5608 bool layout_with_rep = false;
5609 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5611 /* For each variable within each component declaration create a GCC field
5612 and add it to the list, skipping any pragmas in the list. */
5614 if (Present (Component_Items (component_list)))
5615 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5616 Present (component_decl);
5617 component_decl = Next_Non_Pragma (component_decl))
5619 gnat_field = Defining_Entity (component_decl);
5621 if (Chars (gnat_field) == Name_uParent)
5622 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5623 else
5625 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5626 packed, definition);
5628 /* If this is the _Tag field, put it before any discriminants,
5629 instead of after them as is the case for all other fields.
5630 Ignore field of void type if only annotating. */
5631 if (Chars (gnat_field) == Name_uTag)
5632 gnu_field_list = chainon (gnu_field_list, gnu_field);
5633 else
5635 TREE_CHAIN (gnu_field) = gnu_field_list;
5636 gnu_field_list = gnu_field;
5640 save_gnu_tree (gnat_field, gnu_field, false);
5643 /* At the end of the component list there may be a variant part. */
5644 variant_part = Variant_Part (component_list);
5646 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5647 mutually exclusive and should go in the same memory. To do this we need
5648 to treat each variant as a record whose elements are created from the
5649 component list for the variant. So here we create the records from the
5650 lists for the variants and put them all into the QUAL_UNION_TYPE.
5651 If this is an Unchecked_Union, we make a UNION_TYPE instead or
5652 use GNU_RECORD_TYPE if there are no fields so far. */
5653 if (Present (variant_part))
5655 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5656 Node_Id variant;
5657 tree gnu_name = TYPE_NAME (gnu_record_type);
5658 tree gnu_var_name
5659 = concat_id_with_name (get_identifier (Get_Name_String
5660 (Chars (Name (variant_part)))),
5661 "XVN");
5662 tree gnu_union_type;
5663 tree gnu_union_name;
5664 tree gnu_union_field;
5665 tree gnu_variant_list = NULL_TREE;
5667 if (TREE_CODE (gnu_name) == TYPE_DECL)
5668 gnu_name = DECL_NAME (gnu_name);
5670 gnu_union_name = concat_id_with_name (gnu_name,
5671 IDENTIFIER_POINTER (gnu_var_name));
5673 if (!gnu_field_list && TREE_CODE (gnu_record_type) == UNION_TYPE)
5674 gnu_union_type = gnu_record_type;
5675 else
5678 gnu_union_type
5679 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
5681 TYPE_NAME (gnu_union_type) = gnu_union_name;
5682 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5685 for (variant = First_Non_Pragma (Variants (variant_part));
5686 Present (variant);
5687 variant = Next_Non_Pragma (variant))
5689 tree gnu_variant_type = make_node (RECORD_TYPE);
5690 tree gnu_inner_name;
5691 tree gnu_qual;
5693 Get_Variant_Encoding (variant);
5694 gnu_inner_name = get_identifier (Name_Buffer);
5695 TYPE_NAME (gnu_variant_type)
5696 = concat_id_with_name (gnu_union_name,
5697 IDENTIFIER_POINTER (gnu_inner_name));
5699 /* Set the alignment of the inner type in case we need to make
5700 inner objects into bitfields, but then clear it out
5701 so the record actually gets only the alignment required. */
5702 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5703 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5705 /* Similarly, if the outer record has a size specified and all fields
5706 have record rep clauses, we can propagate the size into the
5707 variant part. */
5708 if (all_rep_and_size)
5710 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5711 TYPE_SIZE_UNIT (gnu_variant_type)
5712 = TYPE_SIZE_UNIT (gnu_record_type);
5715 /* Create the record for the variant. Note that we defer emitting
5716 debug info for it until after we are sure to actually use it. */
5717 components_to_record (gnu_variant_type, Component_List (variant),
5718 NULL_TREE, packed, definition,
5719 &gnu_our_rep_list, !all_rep_and_size, all_rep,
5720 true, unchecked_union);
5722 gnu_qual = choices_to_gnu (gnu_discriminant,
5723 Discrete_Choices (variant));
5725 Set_Present_Expr (variant, annotate_value (gnu_qual));
5727 /* If this is an Unchecked_Union and we have exactly one field,
5728 use that field here. */
5729 if (unchecked_union && TYPE_FIELDS (gnu_variant_type)
5730 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
5731 gnu_field = TYPE_FIELDS (gnu_variant_type);
5732 else
5734 /* Emit debug info for the record. We used to throw away
5735 empty records but we no longer do that because we need
5736 them to generate complete debug info for the variant;
5737 otherwise, the union type definition will be lacking
5738 the fields associated with these empty variants. */
5739 write_record_type_debug_info (gnu_variant_type);
5741 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5742 gnu_union_type, 0,
5743 (all_rep_and_size
5744 ? TYPE_SIZE (gnu_record_type)
5745 : 0),
5746 (all_rep_and_size
5747 ? bitsize_zero_node : 0),
5750 DECL_INTERNAL_P (gnu_field) = 1;
5752 if (!unchecked_union)
5753 DECL_QUALIFIER (gnu_field) = gnu_qual;
5756 TREE_CHAIN (gnu_field) = gnu_variant_list;
5757 gnu_variant_list = gnu_field;
5760 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5761 if (gnu_variant_list)
5763 if (all_rep_and_size)
5765 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5766 TYPE_SIZE_UNIT (gnu_union_type)
5767 = TYPE_SIZE_UNIT (gnu_record_type);
5770 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5771 all_rep_and_size, false);
5773 /* If GNU_UNION_TYPE is our record type, it means we must have an
5774 Unchecked_Union with no fields. Verify that and, if so, just
5775 return. */
5776 if (gnu_union_type == gnu_record_type)
5778 gcc_assert (!gnu_field_list && unchecked_union);
5779 return;
5782 gnu_union_field
5783 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5784 packed,
5785 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5786 all_rep ? bitsize_zero_node : 0, 0);
5788 DECL_INTERNAL_P (gnu_union_field) = 1;
5789 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5790 gnu_field_list = gnu_union_field;
5794 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5795 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5796 in a separate pass since we want to handle the discriminants but can't
5797 play with them until we've used them in debugging data above.
5799 ??? Note: if we then reorder them, debugging information will be wrong,
5800 but there's nothing that can be done about this at the moment. */
5802 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5804 if (DECL_FIELD_OFFSET (gnu_field))
5806 tree gnu_next = TREE_CHAIN (gnu_field);
5808 if (!gnu_last)
5809 gnu_field_list = gnu_next;
5810 else
5811 TREE_CHAIN (gnu_last) = gnu_next;
5813 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5814 gnu_our_rep_list = gnu_field;
5815 gnu_field = gnu_next;
5817 else
5819 gnu_last = gnu_field;
5820 gnu_field = TREE_CHAIN (gnu_field);
5824 /* If we have any items in our rep'ed field list, it is not the case that all
5825 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5826 set it and ignore the items. */
5827 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5828 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5829 else if (gnu_our_rep_list)
5831 /* Otherwise, sort the fields by bit position and put them into their
5832 own record if we have any fields without rep clauses. */
5833 tree gnu_rep_type
5834 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5835 int len = list_length (gnu_our_rep_list);
5836 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5837 int i;
5839 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5840 gnu_field = TREE_CHAIN (gnu_field), i++)
5841 gnu_arr[i] = gnu_field;
5843 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5845 /* Put the fields in the list in order of increasing position, which
5846 means we start from the end. */
5847 gnu_our_rep_list = NULL_TREE;
5848 for (i = len - 1; i >= 0; i--)
5850 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5851 gnu_our_rep_list = gnu_arr[i];
5852 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5855 if (gnu_field_list)
5857 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5858 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5859 gnu_record_type, 0, 0, 0, 1);
5860 DECL_INTERNAL_P (gnu_field) = 1;
5861 gnu_field_list = chainon (gnu_field_list, gnu_field);
5863 else
5865 layout_with_rep = true;
5866 gnu_field_list = nreverse (gnu_our_rep_list);
5870 if (cancel_alignment)
5871 TYPE_ALIGN (gnu_record_type) = 0;
5873 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5874 layout_with_rep, defer_debug);
5877 /* Called via qsort from the above. Returns -1, 1, depending on the
5878 bit positions and ordinals of the two fields. Use DECL_UID to ensure
5879 a stable sort. */
5881 static int
5882 compare_field_bitpos (const PTR rt1, const PTR rt2)
5884 tree *t1 = (tree *) rt1;
5885 tree *t2 = (tree *) rt2;
5887 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5888 return DECL_UID (*t1) < DECL_UID (*t2) ? -1 : 1;
5889 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5890 return -1;
5891 else
5892 return 1;
5895 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5896 placed into an Esize, Component_Bit_Offset, or Component_Size value
5897 in the GNAT tree. */
5899 static Uint
5900 annotate_value (tree gnu_size)
5902 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5903 TCode tcode;
5904 Node_Ref_Or_Val ops[3], ret;
5905 int i;
5906 int size;
5908 /* See if we've already saved the value for this node. */
5909 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5910 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5912 /* If we do not return inside this switch, TCODE will be set to the
5913 code to use for a Create_Node operand and LEN (set above) will be
5914 the number of recursive calls for us to make. */
5916 switch (TREE_CODE (gnu_size))
5918 case INTEGER_CST:
5919 if (TREE_OVERFLOW (gnu_size))
5920 return No_Uint;
5922 /* This may have come from a conversion from some smaller type,
5923 so ensure this is in bitsizetype. */
5924 gnu_size = convert (bitsizetype, gnu_size);
5926 /* For negative values, use NEGATE_EXPR of the supplied value. */
5927 if (tree_int_cst_sgn (gnu_size) < 0)
5929 /* The ridiculous code below is to handle the case of the largest
5930 negative integer. */
5931 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5932 bool adjust = false;
5933 tree temp;
5935 if (TREE_CONSTANT_OVERFLOW (negative_size))
5937 negative_size
5938 = size_binop (MINUS_EXPR, bitsize_zero_node,
5939 size_binop (PLUS_EXPR, gnu_size,
5940 bitsize_one_node));
5941 adjust = true;
5944 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5945 if (adjust)
5946 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5948 return annotate_value (temp);
5951 if (!host_integerp (gnu_size, 1))
5952 return No_Uint;
5954 size = tree_low_cst (gnu_size, 1);
5956 /* This peculiar test is to make sure that the size fits in an int
5957 on machines where HOST_WIDE_INT is not "int". */
5958 if (tree_low_cst (gnu_size, 1) == size)
5959 return UI_From_Int (size);
5960 else
5961 return No_Uint;
5963 case COMPONENT_REF:
5964 /* The only case we handle here is a simple discriminant reference. */
5965 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5966 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5967 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5968 return Create_Node (Discrim_Val,
5969 annotate_value (DECL_DISCRIMINANT_NUMBER
5970 (TREE_OPERAND (gnu_size, 1))),
5971 No_Uint, No_Uint);
5972 else
5973 return No_Uint;
5975 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5976 return annotate_value (TREE_OPERAND (gnu_size, 0));
5978 /* Now just list the operations we handle. */
5979 case COND_EXPR: tcode = Cond_Expr; break;
5980 case PLUS_EXPR: tcode = Plus_Expr; break;
5981 case MINUS_EXPR: tcode = Minus_Expr; break;
5982 case MULT_EXPR: tcode = Mult_Expr; break;
5983 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5984 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5985 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5986 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5987 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5988 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5989 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5990 case NEGATE_EXPR: tcode = Negate_Expr; break;
5991 case MIN_EXPR: tcode = Min_Expr; break;
5992 case MAX_EXPR: tcode = Max_Expr; break;
5993 case ABS_EXPR: tcode = Abs_Expr; break;
5994 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5995 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5996 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5997 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5998 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5999 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
6000 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
6001 case LT_EXPR: tcode = Lt_Expr; break;
6002 case LE_EXPR: tcode = Le_Expr; break;
6003 case GT_EXPR: tcode = Gt_Expr; break;
6004 case GE_EXPR: tcode = Ge_Expr; break;
6005 case EQ_EXPR: tcode = Eq_Expr; break;
6006 case NE_EXPR: tcode = Ne_Expr; break;
6008 default:
6009 return No_Uint;
6012 /* Now get each of the operands that's relevant for this code. If any
6013 cannot be expressed as a repinfo node, say we can't. */
6014 for (i = 0; i < 3; i++)
6015 ops[i] = No_Uint;
6017 for (i = 0; i < len; i++)
6019 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6020 if (ops[i] == No_Uint)
6021 return No_Uint;
6024 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6025 TREE_COMPLEXITY (gnu_size) = ret;
6026 return ret;
6029 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6030 GCC type, set Component_Bit_Offset and Esize to the position and size
6031 used by Gigi. */
6033 static void
6034 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6036 tree gnu_list;
6037 tree gnu_entry;
6038 Entity_Id gnat_field;
6040 /* We operate by first making a list of all fields and their positions
6041 (we can get the sizes easily at any time) by a recursive call
6042 and then update all the sizes into the tree. */
6043 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6044 size_zero_node, bitsize_zero_node,
6045 BIGGEST_ALIGNMENT);
6047 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6048 gnat_field = Next_Entity (gnat_field))
6049 if ((Ekind (gnat_field) == E_Component
6050 || (Ekind (gnat_field) == E_Discriminant
6051 && !Is_Unchecked_Union (Scope (gnat_field)))))
6053 tree parent_offset = bitsize_zero_node;
6055 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6056 gnu_list);
6058 if (gnu_entry)
6060 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6062 /* In this mode the tag and parent components have not been
6063 generated, so we add the appropriate offset to each
6064 component. For a component appearing in the current
6065 extension, the offset is the size of the parent. */
6066 if (Is_Derived_Type (gnat_entity)
6067 && Original_Record_Component (gnat_field) == gnat_field)
6068 parent_offset
6069 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6070 bitsizetype);
6071 else
6072 parent_offset = bitsize_int (POINTER_SIZE);
6075 Set_Component_Bit_Offset
6076 (gnat_field,
6077 annotate_value
6078 (size_binop (PLUS_EXPR,
6079 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6080 TREE_VALUE (TREE_VALUE
6081 (TREE_VALUE (gnu_entry)))),
6082 parent_offset)));
6084 Set_Esize (gnat_field,
6085 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6087 else if (Is_Tagged_Type (gnat_entity)
6088 && Is_Derived_Type (gnat_entity))
6090 /* If there is no gnu_entry, this is an inherited component whose
6091 position is the same as in the parent type. */
6092 Set_Component_Bit_Offset
6093 (gnat_field,
6094 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6095 Set_Esize (gnat_field,
6096 Esize (Original_Record_Component (gnat_field)));
6101 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6102 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6103 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6104 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6105 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6106 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6107 so far. */
6109 static tree
6110 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6111 tree gnu_bitpos, unsigned int offset_align)
6113 tree gnu_field;
6114 tree gnu_result = gnu_list;
6116 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6117 gnu_field = TREE_CHAIN (gnu_field))
6119 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6120 DECL_FIELD_BIT_OFFSET (gnu_field));
6121 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6122 DECL_FIELD_OFFSET (gnu_field));
6123 unsigned int our_offset_align
6124 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6126 gnu_result
6127 = tree_cons (gnu_field,
6128 tree_cons (gnu_our_offset,
6129 tree_cons (size_int (our_offset_align),
6130 gnu_our_bitpos, NULL_TREE),
6131 NULL_TREE),
6132 gnu_result);
6134 if (DECL_INTERNAL_P (gnu_field))
6135 gnu_result
6136 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6137 gnu_our_offset, gnu_our_bitpos,
6138 our_offset_align);
6141 return gnu_result;
6144 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6145 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6146 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6147 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6148 for the size of a field. COMPONENT_P is true if we are being called
6149 to process the Component_Size of GNAT_OBJECT. This is used for error
6150 message handling and to indicate to use the object size of GNU_TYPE.
6151 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6152 it means that a size of zero should be treated as an unspecified size. */
6154 static tree
6155 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6156 enum tree_code kind, bool component_p, bool zero_ok)
6158 Node_Id gnat_error_node;
6159 tree type_size
6160 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
6161 tree size;
6163 /* Find the node to use for errors. */
6164 if ((Ekind (gnat_object) == E_Component
6165 || Ekind (gnat_object) == E_Discriminant)
6166 && Present (Component_Clause (gnat_object)))
6167 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6168 else if (Present (Size_Clause (gnat_object)))
6169 gnat_error_node = Expression (Size_Clause (gnat_object));
6170 else
6171 gnat_error_node = gnat_object;
6173 /* Return 0 if no size was specified, either because Esize was not Present or
6174 the specified size was zero. */
6175 if (No (uint_size) || uint_size == No_Uint)
6176 return NULL_TREE;
6178 /* Get the size as a tree. Give an error if a size was specified, but cannot
6179 be represented as in sizetype. */
6180 size = UI_To_gnu (uint_size, bitsizetype);
6181 if (TREE_OVERFLOW (size))
6183 post_error_ne (component_p ? "component size of & is too large"
6184 : "size of & is too large",
6185 gnat_error_node, gnat_object);
6186 return NULL_TREE;
6189 /* Ignore a negative size since that corresponds to our back-annotation.
6190 Also ignore a zero size unless a size clause exists. */
6191 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6192 return NULL_TREE;
6194 /* The size of objects is always a multiple of a byte. */
6195 if (kind == VAR_DECL
6196 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6198 if (component_p)
6199 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6200 gnat_error_node, gnat_object);
6201 else
6202 post_error_ne ("size for& is not a multiple of Storage_Unit",
6203 gnat_error_node, gnat_object);
6204 return NULL_TREE;
6207 /* If this is an integral type or a packed array type, the front-end has
6208 verified the size, so we need not do it here (which would entail
6209 checking against the bounds). However, if this is an aliased object, it
6210 may not be smaller than the type of the object. */
6211 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6212 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6213 return size;
6215 /* If the object is a record that contains a template, add the size of
6216 the template to the specified size. */
6217 if (TREE_CODE (gnu_type) == RECORD_TYPE
6218 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6219 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6221 /* Modify the size of the type to be that of the maximum size if it has a
6222 discriminant or the size of a thin pointer if this is a fat pointer. */
6223 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6224 type_size = max_size (type_size, true);
6225 else if (TYPE_FAT_POINTER_P (gnu_type))
6226 type_size = bitsize_int (POINTER_SIZE);
6228 /* If this is an access type, the minimum size is that given by the smallest
6229 integral mode that's valid for pointers. */
6230 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6232 enum machine_mode p_mode;
6234 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6235 !targetm.valid_pointer_mode (p_mode);
6236 p_mode = GET_MODE_WIDER_MODE (p_mode))
6239 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6242 /* If the size of the object is a constant, the new size must not be
6243 smaller. */
6244 if (TREE_CODE (type_size) != INTEGER_CST
6245 || TREE_OVERFLOW (type_size)
6246 || tree_int_cst_lt (size, type_size))
6248 if (component_p)
6249 post_error_ne_tree
6250 ("component size for& too small{, minimum allowed is ^}",
6251 gnat_error_node, gnat_object, type_size);
6252 else
6253 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6254 gnat_error_node, gnat_object, type_size);
6256 if (kind == VAR_DECL && !component_p
6257 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6258 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6259 post_error_ne_tree_2
6260 ("\\size of ^ is not a multiple of alignment (^ bits)",
6261 gnat_error_node, gnat_object, rm_size (gnu_type),
6262 TYPE_ALIGN (gnu_type));
6264 else if (INTEGRAL_TYPE_P (gnu_type))
6265 post_error_ne ("\\size would be legal if & were not aliased!",
6266 gnat_error_node, gnat_object);
6268 return NULL_TREE;
6271 return size;
6274 /* Similarly, but both validate and process a value of RM_Size. This
6275 routine is only called for types. */
6277 static void
6278 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6280 /* Only give an error if a Value_Size clause was explicitly given.
6281 Otherwise, we'd be duplicating an error on the Size clause. */
6282 Node_Id gnat_attr_node
6283 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6284 tree old_size = rm_size (gnu_type);
6285 tree size;
6287 /* Get the size as a tree. Do nothing if none was specified, either
6288 because RM_Size was not Present or if the specified size was zero.
6289 Give an error if a size was specified, but cannot be represented as
6290 in sizetype. */
6291 if (No (uint_size) || uint_size == No_Uint)
6292 return;
6294 size = UI_To_gnu (uint_size, bitsizetype);
6295 if (TREE_OVERFLOW (size))
6297 if (Present (gnat_attr_node))
6298 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6299 gnat_entity);
6301 return;
6304 /* Ignore a negative size since that corresponds to our back-annotation.
6305 Also ignore a zero size unless a size clause exists, a Value_Size
6306 clause exists, or this is an integer type, in which case the
6307 front end will have always set it. */
6308 else if (tree_int_cst_sgn (size) < 0
6309 || (integer_zerop (size) && No (gnat_attr_node)
6310 && !Has_Size_Clause (gnat_entity)
6311 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6312 return;
6314 /* If the old size is self-referential, get the maximum size. */
6315 if (CONTAINS_PLACEHOLDER_P (old_size))
6316 old_size = max_size (old_size, true);
6318 /* If the size of the object is a constant, the new size must not be
6319 smaller (the front end checks this for scalar types). */
6320 if (TREE_CODE (old_size) != INTEGER_CST
6321 || TREE_OVERFLOW (old_size)
6322 || (AGGREGATE_TYPE_P (gnu_type)
6323 && tree_int_cst_lt (size, old_size)))
6325 if (Present (gnat_attr_node))
6326 post_error_ne_tree
6327 ("Value_Size for& too small{, minimum allowed is ^}",
6328 gnat_attr_node, gnat_entity, old_size);
6330 return;
6333 /* Otherwise, set the RM_Size. */
6334 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6335 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6336 TYPE_RM_SIZE_NUM (gnu_type) = size;
6337 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6338 TYPE_RM_SIZE_NUM (gnu_type) = size;
6339 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6340 || TREE_CODE (gnu_type) == UNION_TYPE
6341 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6342 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6343 SET_TYPE_ADA_SIZE (gnu_type, size);
6346 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6347 If TYPE is the best type, return it. Otherwise, make a new type. We
6348 only support new integral and pointer types. BIASED_P is nonzero if
6349 we are making a biased type. */
6351 static tree
6352 make_type_from_size (tree type, tree size_tree, bool biased_p)
6354 tree new_type;
6355 unsigned HOST_WIDE_INT size;
6356 bool unsigned_p;
6358 /* If size indicates an error, just return TYPE to avoid propagating the
6359 error. Likewise if it's too large to represent. */
6360 if (!size_tree || !host_integerp (size_tree, 1))
6361 return type;
6363 size = tree_low_cst (size_tree, 1);
6364 switch (TREE_CODE (type))
6366 case INTEGER_TYPE:
6367 case ENUMERAL_TYPE:
6368 /* Only do something if the type is not already the proper size and is
6369 not a packed array type. */
6370 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6371 || (TYPE_PRECISION (type) == size
6372 && biased_p == (TREE_CODE (type) == INTEGER_CST
6373 && TYPE_BIASED_REPRESENTATION_P (type))))
6374 break;
6376 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6377 && TYPE_BIASED_REPRESENTATION_P (type));
6378 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6380 size = MIN (size, LONG_LONG_TYPE_SIZE);
6381 new_type
6382 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6383 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6384 TYPE_MIN_VALUE (new_type)
6385 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6386 TYPE_MAX_VALUE (new_type)
6387 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6388 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6389 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6390 return new_type;
6392 case RECORD_TYPE:
6393 /* Do something if this is a fat pointer, in which case we
6394 may need to return the thin pointer. */
6395 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6396 return
6397 build_pointer_type
6398 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6399 break;
6401 case POINTER_TYPE:
6402 /* Only do something if this is a thin pointer, in which case we
6403 may need to return the fat pointer. */
6404 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6405 return
6406 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6408 break;
6410 default:
6411 break;
6414 return type;
6417 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6418 a type or object whose present alignment is ALIGN. If this alignment is
6419 valid, return it. Otherwise, give an error and return ALIGN. */
6421 static unsigned int
6422 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6424 Node_Id gnat_error_node = gnat_entity;
6425 unsigned int new_align;
6427 #ifndef MAX_OFILE_ALIGNMENT
6428 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6429 #endif
6431 if (Present (Alignment_Clause (gnat_entity)))
6432 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6434 /* Don't worry about checking alignment if alignment was not specified
6435 by the source program and we already posted an error for this entity. */
6437 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6438 return align;
6440 /* Within GCC, an alignment is an integer, so we must make sure a
6441 value is specified that fits in that range. Also, alignments of
6442 more than MAX_OFILE_ALIGNMENT can't be supported. */
6444 if (! UI_Is_In_Int_Range (alignment)
6445 || ((new_align = UI_To_Int (alignment))
6446 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6447 post_error_ne_num ("largest supported alignment for& is ^",
6448 gnat_error_node, gnat_entity,
6449 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6450 else if (!(Present (Alignment_Clause (gnat_entity))
6451 && From_At_Mod (Alignment_Clause (gnat_entity)))
6452 && new_align * BITS_PER_UNIT < align)
6453 post_error_ne_num ("alignment for& must be at least ^",
6454 gnat_error_node, gnat_entity,
6455 align / BITS_PER_UNIT);
6456 else
6457 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6459 return align;
6462 /* Verify that OBJECT, a type or decl, is something we can implement
6463 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6464 if we require atomic components. */
6466 static void
6467 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6469 Node_Id gnat_error_point = gnat_entity;
6470 Node_Id gnat_node;
6471 enum machine_mode mode;
6472 unsigned int align;
6473 tree size;
6475 /* There are three case of what OBJECT can be. It can be a type, in which
6476 case we take the size, alignment and mode from the type. It can be a
6477 declaration that was indirect, in which case the relevant values are
6478 that of the type being pointed to, or it can be a normal declaration,
6479 in which case the values are of the decl. The code below assumes that
6480 OBJECT is either a type or a decl. */
6481 if (TYPE_P (object))
6483 mode = TYPE_MODE (object);
6484 align = TYPE_ALIGN (object);
6485 size = TYPE_SIZE (object);
6487 else if (DECL_BY_REF_P (object))
6489 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6490 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6491 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6493 else
6495 mode = DECL_MODE (object);
6496 align = DECL_ALIGN (object);
6497 size = DECL_SIZE (object);
6500 /* Consider all floating-point types atomic and any types that that are
6501 represented by integers no wider than a machine word. */
6502 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6503 || ((GET_MODE_CLASS (mode) == MODE_INT
6504 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6505 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6506 return;
6508 /* For the moment, also allow anything that has an alignment equal
6509 to its size and which is smaller than a word. */
6510 if (size && TREE_CODE (size) == INTEGER_CST
6511 && compare_tree_int (size, align) == 0
6512 && align <= BITS_PER_WORD)
6513 return;
6515 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6516 gnat_node = Next_Rep_Item (gnat_node))
6518 if (!comp_p && Nkind (gnat_node) == N_Pragma
6519 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6520 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6521 else if (comp_p && Nkind (gnat_node) == N_Pragma
6522 && (Get_Pragma_Id (Chars (gnat_node))
6523 == Pragma_Atomic_Components))
6524 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6527 if (comp_p)
6528 post_error_ne ("atomic access to component of & cannot be guaranteed",
6529 gnat_error_point, gnat_entity);
6530 else
6531 post_error_ne ("atomic access to & cannot be guaranteed",
6532 gnat_error_point, gnat_entity);
6535 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6536 have compatible signatures so that a call using one type may be safely
6537 issued if the actual target function type is the other. Return 1 if it is
6538 the case, 0 otherwise, and post errors on the incompatibilities.
6540 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6541 that calls to the subprogram will have arguments suitable for the later
6542 underlying builtin expansion. */
6544 static int
6545 compatible_signatures_p (tree ftype1, tree ftype2)
6547 /* As of now, we only perform very trivial tests and consider it's the
6548 programmer's responsibility to ensure the type correctness in the Ada
6549 declaration, as in the regular Import cases.
6551 Mismatches typically result in either error messages from the builtin
6552 expander, internal compiler errors, or in a real call sequence. This
6553 should be refined to issue diagnostics helping error detection and
6554 correction. */
6556 /* Almost fake test, ensuring a use of each argument. */
6557 if (ftype1 == ftype2)
6558 return 1;
6560 return 1;
6563 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6564 with all size expressions that contain F updated by replacing F with R.
6565 This is identical to GCC's substitute_in_type except that it knows about
6566 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6567 nothing has changed. */
6569 tree
6570 gnat_substitute_in_type (tree t, tree f, tree r)
6572 tree new = t;
6573 tree tem;
6575 switch (TREE_CODE (t))
6577 case INTEGER_TYPE:
6578 case ENUMERAL_TYPE:
6579 case BOOLEAN_TYPE:
6580 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6581 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6583 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6584 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6586 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6587 return t;
6589 new = build_range_type (TREE_TYPE (t), low, high);
6590 if (TYPE_INDEX_TYPE (t))
6591 SET_TYPE_INDEX_TYPE
6592 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6593 return new;
6596 return t;
6598 case REAL_TYPE:
6599 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6600 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6602 tree low = NULL_TREE, high = NULL_TREE;
6604 if (TYPE_MIN_VALUE (t))
6605 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6606 if (TYPE_MAX_VALUE (t))
6607 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6609 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6610 return t;
6612 t = copy_type (t);
6613 TYPE_MIN_VALUE (t) = low;
6614 TYPE_MAX_VALUE (t) = high;
6616 return t;
6618 case COMPLEX_TYPE:
6619 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6620 if (tem == TREE_TYPE (t))
6621 return t;
6623 return build_complex_type (tem);
6625 case OFFSET_TYPE:
6626 case METHOD_TYPE:
6627 case FUNCTION_TYPE:
6628 case LANG_TYPE:
6629 /* Don't know how to do these yet. */
6630 gcc_unreachable ();
6632 case ARRAY_TYPE:
6634 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6635 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6637 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6638 return t;
6640 new = build_array_type (component, domain);
6641 TYPE_SIZE (new) = 0;
6642 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6643 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6644 layout_type (new);
6645 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6647 /* If we had bounded the sizes of T by a constant, bound the sizes of
6648 NEW by the same constant. */
6649 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6650 TYPE_SIZE (new)
6651 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6652 TYPE_SIZE (new));
6653 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6654 TYPE_SIZE_UNIT (new)
6655 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6656 TYPE_SIZE_UNIT (new));
6657 return new;
6660 case RECORD_TYPE:
6661 case UNION_TYPE:
6662 case QUAL_UNION_TYPE:
6664 tree field;
6665 bool changed_field
6666 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6667 bool field_has_rep = false;
6668 tree last_field = NULL_TREE;
6670 tree new = copy_type (t);
6672 /* Start out with no fields, make new fields, and chain them
6673 in. If we haven't actually changed the type of any field,
6674 discard everything we've done and return the old type. */
6676 TYPE_FIELDS (new) = NULL_TREE;
6677 TYPE_SIZE (new) = NULL_TREE;
6679 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6681 tree new_field = copy_node (field);
6683 TREE_TYPE (new_field)
6684 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6686 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6687 field_has_rep = true;
6688 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6689 changed_field = true;
6691 /* If this is an internal field and the type of this field is
6692 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6693 the type just has one element, treat that as the field.
6694 But don't do this if we are processing a QUAL_UNION_TYPE. */
6695 if (TREE_CODE (t) != QUAL_UNION_TYPE
6696 && DECL_INTERNAL_P (new_field)
6697 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6698 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6700 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6701 continue;
6703 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6705 tree next_new_field
6706 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6708 /* Make sure omitting the union doesn't change
6709 the layout. */
6710 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6711 new_field = next_new_field;
6715 DECL_CONTEXT (new_field) = new;
6716 SET_DECL_ORIGINAL_FIELD (new_field,
6717 (DECL_ORIGINAL_FIELD (field)
6718 ? DECL_ORIGINAL_FIELD (field) : field));
6720 /* If the size of the old field was set at a constant,
6721 propagate the size in case the type's size was variable.
6722 (This occurs in the case of a variant or discriminated
6723 record with a default size used as a field of another
6724 record.) */
6725 DECL_SIZE (new_field)
6726 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6727 ? DECL_SIZE (field) : NULL_TREE;
6728 DECL_SIZE_UNIT (new_field)
6729 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6730 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6732 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6734 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6736 if (new_q != DECL_QUALIFIER (new_field))
6737 changed_field = true;
6739 /* Do the substitution inside the qualifier and if we find
6740 that this field will not be present, omit it. */
6741 DECL_QUALIFIER (new_field) = new_q;
6743 if (integer_zerop (DECL_QUALIFIER (new_field)))
6744 continue;
6747 if (!last_field)
6748 TYPE_FIELDS (new) = new_field;
6749 else
6750 TREE_CHAIN (last_field) = new_field;
6752 last_field = new_field;
6754 /* If this is a qualified type and this field will always be
6755 present, we are done. */
6756 if (TREE_CODE (t) == QUAL_UNION_TYPE
6757 && integer_onep (DECL_QUALIFIER (new_field)))
6758 break;
6761 /* If this used to be a qualified union type, but we now know what
6762 field will be present, make this a normal union. */
6763 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6764 && (!TYPE_FIELDS (new)
6765 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6766 TREE_SET_CODE (new, UNION_TYPE);
6767 else if (!changed_field)
6768 return t;
6770 gcc_assert (!field_has_rep);
6771 layout_type (new);
6773 /* If the size was originally a constant use it. */
6774 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6775 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6777 TYPE_SIZE (new) = TYPE_SIZE (t);
6778 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6779 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6782 return new;
6785 default:
6786 return t;
6790 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6791 needed to represent the object. */
6793 tree
6794 rm_size (tree gnu_type)
6796 /* For integer types, this is the precision. For record types, we store
6797 the size explicitly. For other types, this is just the size. */
6799 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6800 return TYPE_RM_SIZE (gnu_type);
6801 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6802 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6803 /* Return the rm_size of the actual data plus the size of the template. */
6804 return
6805 size_binop (PLUS_EXPR,
6806 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6807 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6808 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6809 || TREE_CODE (gnu_type) == UNION_TYPE
6810 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6811 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6812 && TYPE_ADA_SIZE (gnu_type))
6813 return TYPE_ADA_SIZE (gnu_type);
6814 else
6815 return TYPE_SIZE (gnu_type);
6818 /* Return an identifier representing the external name to be used for
6819 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6820 and the specified suffix. */
6822 tree
6823 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6825 Entity_Kind kind = Ekind (gnat_entity);
6827 const char *str = (!suffix ? "" : suffix);
6828 String_Template temp = {1, strlen (str)};
6829 Fat_Pointer fp = {str, &temp};
6831 Get_External_Name_With_Suffix (gnat_entity, fp);
6833 /* A variable using the Stdcall convention (meaning we are running
6834 on a Windows box) live in a DLL. Here we adjust its name to use
6835 the jump-table, the _imp__NAME contains the address for the NAME
6836 variable. */
6837 if ((kind == E_Variable || kind == E_Constant)
6838 && Has_Stdcall_Convention (gnat_entity))
6840 const char *prefix = "_imp__";
6841 int k, plen = strlen (prefix);
6843 for (k = 0; k <= Name_Len; k++)
6844 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6845 strncpy (Name_Buffer, prefix, plen);
6848 return get_identifier (Name_Buffer);
6851 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6852 fully-qualified name, possibly with type information encoding.
6853 Otherwise, return the name. */
6855 tree
6856 get_entity_name (Entity_Id gnat_entity)
6858 Get_Encoded_Name (gnat_entity);
6859 return get_identifier (Name_Buffer);
6862 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6863 string, return a new IDENTIFIER_NODE that is the concatenation of
6864 the name in GNU_ID and SUFFIX. */
6866 tree
6867 concat_id_with_name (tree gnu_id, const char *suffix)
6869 int len = IDENTIFIER_LENGTH (gnu_id);
6871 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6872 IDENTIFIER_LENGTH (gnu_id));
6873 strncpy (Name_Buffer + len, "___", 3);
6874 len += 3;
6875 strcpy (Name_Buffer + len, suffix);
6876 return get_identifier (Name_Buffer);
6879 #include "gt-ada-decl.h"